2 * Atmel maXTouch Touchscreen driver
4 * Copyright (C) 2010 Samsung Electronics Co.Ltd
5 * Copyright (C) 2011-2014 Atmel Corporation
6 * Copyright (C) 2012 Google, Inc.
8 * Author: Joonyoung Shim <jy0922.shim@samsung.com>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2 of the License, or (at your
13 * option) any later version.
17 #include <linux/acpi.h>
18 #include <linux/dmi.h>
19 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/completion.h>
22 #include <linux/delay.h>
23 #include <linux/firmware.h>
24 #include <linux/i2c.h>
25 #include <linux/platform_data/atmel_mxt_ts.h>
26 #include <linux/input/mt.h>
27 #include <linux/interrupt.h>
29 #include <linux/slab.h>
30 #include <asm/unaligned.h>
31 #include <media/v4l2-device.h>
32 #include <media/v4l2-ioctl.h>
33 #include <media/videobuf2-v4l2.h>
34 #include <media/videobuf2-vmalloc.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_PROCI_GRIPFACE_T20 20
59 #define MXT_PROCG_NOISE_T22 22
60 #define MXT_PROCI_ONETOUCH_T24 24
61 #define MXT_PROCI_TWOTOUCH_T27 27
62 #define MXT_PROCI_GRIP_T40 40
63 #define MXT_PROCI_PALM_T41 41
64 #define MXT_PROCI_TOUCHSUPPRESSION_T42 42
65 #define MXT_PROCI_STYLUS_T47 47
66 #define MXT_PROCG_NOISESUPPRESSION_T48 48
67 #define MXT_SPT_COMMSCONFIG_T18 18
68 #define MXT_SPT_GPIOPWM_T19 19
69 #define MXT_SPT_SELFTEST_T25 25
70 #define MXT_SPT_CTECONFIG_T28 28
71 #define MXT_SPT_USERDATA_T38 38
72 #define MXT_SPT_DIGITIZER_T43 43
73 #define MXT_SPT_MESSAGECOUNT_T44 44
74 #define MXT_SPT_CTECONFIG_T46 46
75 #define MXT_TOUCH_MULTITOUCHSCREEN_T100 100
77 /* MXT_GEN_MESSAGE_T5 object */
78 #define MXT_RPTID_NOMSG 0xff
80 /* MXT_GEN_COMMAND_T6 field */
81 #define MXT_COMMAND_RESET 0
82 #define MXT_COMMAND_BACKUPNV 1
83 #define MXT_COMMAND_CALIBRATE 2
84 #define MXT_COMMAND_REPORTALL 3
85 #define MXT_COMMAND_DIAGNOSTIC 5
87 /* Define for T6 status byte */
88 #define MXT_T6_STATUS_RESET (1 << 7)
89 #define MXT_T6_STATUS_OFL (1 << 6)
90 #define MXT_T6_STATUS_SIGERR (1 << 5)
91 #define MXT_T6_STATUS_CAL (1 << 4)
92 #define MXT_T6_STATUS_CFGERR (1 << 3)
93 #define MXT_T6_STATUS_COMSERR (1 << 2)
95 /* MXT_GEN_POWER_T7 field */
101 #define MXT_POWER_CFG_RUN 0
102 #define MXT_POWER_CFG_DEEPSLEEP 1
104 /* MXT_TOUCH_MULTI_T9 field */
105 #define MXT_T9_CTRL 0
106 #define MXT_T9_XSIZE 3
107 #define MXT_T9_YSIZE 4
108 #define MXT_T9_ORIENT 9
109 #define MXT_T9_RANGE 18
111 /* MXT_TOUCH_MULTI_T9 status */
112 #define MXT_T9_UNGRIP (1 << 0)
113 #define MXT_T9_SUPPRESS (1 << 1)
114 #define MXT_T9_AMP (1 << 2)
115 #define MXT_T9_VECTOR (1 << 3)
116 #define MXT_T9_MOVE (1 << 4)
117 #define MXT_T9_RELEASE (1 << 5)
118 #define MXT_T9_PRESS (1 << 6)
119 #define MXT_T9_DETECT (1 << 7)
126 /* MXT_TOUCH_MULTI_T9 orient */
127 #define MXT_T9_ORIENT_SWITCH (1 << 0)
128 #define MXT_T9_ORIENT_INVERTX (1 << 1)
129 #define MXT_T9_ORIENT_INVERTY (1 << 2)
131 /* MXT_SPT_COMMSCONFIG_T18 */
132 #define MXT_COMMS_CTRL 0
133 #define MXT_COMMS_CMD 1
135 /* MXT_DEBUG_DIAGNOSTIC_T37 */
136 #define MXT_DIAGNOSTIC_PAGEUP 0x01
137 #define MXT_DIAGNOSTIC_DELTAS 0x10
138 #define MXT_DIAGNOSTIC_REFS 0x11
139 #define MXT_DIAGNOSTIC_SIZE 128
141 #define MXT_FAMILY_1386 160
142 #define MXT1386_COLUMNS 3
143 #define MXT1386_PAGES_PER_COLUMN 8
146 #ifdef CONFIG_TOUCHSCREEN_ATMEL_MXT_T37
149 u8 data
[MXT_DIAGNOSTIC_SIZE
];
153 /* Define for MXT_GEN_COMMAND_T6 */
154 #define MXT_BOOT_VALUE 0xa5
155 #define MXT_RESET_VALUE 0x01
156 #define MXT_BACKUP_VALUE 0x55
158 /* T100 Multiple Touch Touchscreen */
159 #define MXT_T100_CTRL 0
160 #define MXT_T100_CFG1 1
161 #define MXT_T100_TCHAUX 3
162 #define MXT_T100_XSIZE 9
163 #define MXT_T100_XRANGE 13
164 #define MXT_T100_YSIZE 20
165 #define MXT_T100_YRANGE 24
167 #define MXT_T100_CFG_SWITCHXY BIT(5)
168 #define MXT_T100_CFG_INVERTY BIT(6)
169 #define MXT_T100_CFG_INVERTX BIT(7)
171 #define MXT_T100_TCHAUX_VECT BIT(0)
172 #define MXT_T100_TCHAUX_AMPL BIT(1)
173 #define MXT_T100_TCHAUX_AREA BIT(2)
175 #define MXT_T100_DETECT BIT(7)
176 #define MXT_T100_TYPE_MASK 0x70
179 MXT_T100_TYPE_FINGER
= 1,
180 MXT_T100_TYPE_PASSIVE_STYLUS
= 2,
181 MXT_T100_TYPE_HOVERING_FINGER
= 4,
182 MXT_T100_TYPE_GLOVE
= 5,
183 MXT_T100_TYPE_LARGE_TOUCH
= 6,
186 #define MXT_DISTANCE_ACTIVE_TOUCH 0
187 #define MXT_DISTANCE_HOVERING 1
189 #define MXT_TOUCH_MAJOR_DEFAULT 1
190 #define MXT_PRESSURE_DEFAULT 1
193 #define MXT_BACKUP_TIME 50 /* msec */
194 #define MXT_RESET_TIME 200 /* msec */
195 #define MXT_RESET_TIMEOUT 3000 /* msec */
196 #define MXT_CRC_TIMEOUT 1000 /* msec */
197 #define MXT_FW_RESET_TIME 3000 /* msec */
198 #define MXT_FW_CHG_TIMEOUT 300 /* msec */
200 /* Command to unlock bootloader */
201 #define MXT_UNLOCK_CMD_MSB 0xaa
202 #define MXT_UNLOCK_CMD_LSB 0xdc
204 /* Bootloader mode status */
205 #define MXT_WAITING_BOOTLOAD_CMD 0xc0 /* valid 7 6 bit only */
206 #define MXT_WAITING_FRAME_DATA 0x80 /* valid 7 6 bit only */
207 #define MXT_FRAME_CRC_CHECK 0x02
208 #define MXT_FRAME_CRC_FAIL 0x03
209 #define MXT_FRAME_CRC_PASS 0x04
210 #define MXT_APP_CRC_FAIL 0x40 /* valid 7 8 bit only */
211 #define MXT_BOOT_STATUS_MASK 0x3f
212 #define MXT_BOOT_EXTENDED_ID (1 << 5)
213 #define MXT_BOOT_ID_MASK 0x1f
215 /* Touchscreen absolute values */
216 #define MXT_MAX_AREA 0xff
218 #define MXT_PIXELS_PER_MM 20
234 u8 instances_minus_one
;
240 u16 diag_cmd_address
;
241 struct t37_debug
*t37_buf
;
242 unsigned int t37_pages
;
243 unsigned int t37_nodes
;
245 struct v4l2_device v4l2
;
246 struct v4l2_pix_format format
;
247 struct video_device vdev
;
248 struct vb2_queue queue
;
253 enum v4l_dbg_inputs
{
254 MXT_V4L_INPUT_DELTAS
,
259 static const struct v4l2_file_operations mxt_video_fops
= {
260 .owner
= THIS_MODULE
,
261 .open
= v4l2_fh_open
,
262 .release
= vb2_fop_release
,
263 .unlocked_ioctl
= video_ioctl2
,
264 .read
= vb2_fop_read
,
265 .mmap
= vb2_fop_mmap
,
266 .poll
= vb2_fop_poll
,
269 /* Each client has this additional data */
271 struct i2c_client
*client
;
272 struct input_dev
*input_dev
;
273 char phys
[64]; /* device physical location */
274 const struct mxt_platform_data
*pdata
;
275 struct mxt_object
*object_table
;
276 struct mxt_info info
;
297 u8 last_message_count
;
300 struct t7_config t7_cfg
;
303 /* Cached parameters from object table */
313 u8 T100_reportid_min
;
314 u8 T100_reportid_max
;
316 /* for fw update in bootloader */
317 struct completion bl_completion
;
319 /* for reset handling */
320 struct completion reset_completion
;
322 /* for config update handling */
323 struct completion crc_completion
;
326 struct mxt_vb2_buffer
{
327 struct vb2_buffer vb
;
328 struct list_head list
;
331 static size_t mxt_obj_size(const struct mxt_object
*obj
)
333 return obj
->size_minus_one
+ 1;
336 static size_t mxt_obj_instances(const struct mxt_object
*obj
)
338 return obj
->instances_minus_one
+ 1;
341 static bool mxt_object_readable(unsigned int type
)
344 case MXT_GEN_COMMAND_T6
:
345 case MXT_GEN_POWER_T7
:
346 case MXT_GEN_ACQUIRE_T8
:
347 case MXT_GEN_DATASOURCE_T53
:
348 case MXT_TOUCH_MULTI_T9
:
349 case MXT_TOUCH_KEYARRAY_T15
:
350 case MXT_TOUCH_PROXIMITY_T23
:
351 case MXT_TOUCH_PROXKEY_T52
:
352 case MXT_PROCI_GRIPFACE_T20
:
353 case MXT_PROCG_NOISE_T22
:
354 case MXT_PROCI_ONETOUCH_T24
:
355 case MXT_PROCI_TWOTOUCH_T27
:
356 case MXT_PROCI_GRIP_T40
:
357 case MXT_PROCI_PALM_T41
:
358 case MXT_PROCI_TOUCHSUPPRESSION_T42
:
359 case MXT_PROCI_STYLUS_T47
:
360 case MXT_PROCG_NOISESUPPRESSION_T48
:
361 case MXT_SPT_COMMSCONFIG_T18
:
362 case MXT_SPT_GPIOPWM_T19
:
363 case MXT_SPT_SELFTEST_T25
:
364 case MXT_SPT_CTECONFIG_T28
:
365 case MXT_SPT_USERDATA_T38
:
366 case MXT_SPT_DIGITIZER_T43
:
367 case MXT_SPT_CTECONFIG_T46
:
374 static void mxt_dump_message(struct mxt_data
*data
, u8
*message
)
376 dev_dbg(&data
->client
->dev
, "message: %*ph\n",
377 data
->T5_msg_size
, message
);
380 static int mxt_wait_for_completion(struct mxt_data
*data
,
381 struct completion
*comp
,
382 unsigned int timeout_ms
)
384 struct device
*dev
= &data
->client
->dev
;
385 unsigned long timeout
= msecs_to_jiffies(timeout_ms
);
388 ret
= wait_for_completion_interruptible_timeout(comp
, timeout
);
391 } else if (ret
== 0) {
392 dev_err(dev
, "Wait for completion timed out.\n");
398 static int mxt_bootloader_read(struct mxt_data
*data
,
399 u8
*val
, unsigned int count
)
404 msg
.addr
= data
->bootloader_addr
;
405 msg
.flags
= data
->client
->flags
& I2C_M_TEN
;
406 msg
.flags
|= I2C_M_RD
;
410 ret
= i2c_transfer(data
->client
->adapter
, &msg
, 1);
414 ret
= ret
< 0 ? ret
: -EIO
;
415 dev_err(&data
->client
->dev
, "%s: i2c recv failed (%d)\n",
422 static int mxt_bootloader_write(struct mxt_data
*data
,
423 const u8
* const val
, unsigned int count
)
428 msg
.addr
= data
->bootloader_addr
;
429 msg
.flags
= data
->client
->flags
& I2C_M_TEN
;
433 ret
= i2c_transfer(data
->client
->adapter
, &msg
, 1);
437 ret
= ret
< 0 ? ret
: -EIO
;
438 dev_err(&data
->client
->dev
, "%s: i2c send failed (%d)\n",
445 static int mxt_lookup_bootloader_address(struct mxt_data
*data
, bool retry
)
447 u8 appmode
= data
->client
->addr
;
453 /* Chips after 1664S use different scheme */
454 if (retry
|| data
->info
.family_id
>= 0xa2) {
455 bootloader
= appmode
- 0x24;
458 /* Fall through for normal case */
463 bootloader
= appmode
- 0x26;
467 dev_err(&data
->client
->dev
,
468 "Appmode i2c address 0x%02x not found\n",
473 data
->bootloader_addr
= bootloader
;
477 static int mxt_probe_bootloader(struct mxt_data
*data
, bool alt_address
)
479 struct device
*dev
= &data
->client
->dev
;
484 error
= mxt_lookup_bootloader_address(data
, alt_address
);
488 error
= mxt_bootloader_read(data
, &val
, 1);
492 /* Check app crc fail mode */
493 crc_failure
= (val
& ~MXT_BOOT_STATUS_MASK
) == MXT_APP_CRC_FAIL
;
495 dev_err(dev
, "Detected bootloader, status:%02X%s\n",
496 val
, crc_failure
? ", APP_CRC_FAIL" : "");
501 static u8
mxt_get_bootloader_version(struct mxt_data
*data
, u8 val
)
503 struct device
*dev
= &data
->client
->dev
;
506 if (val
& MXT_BOOT_EXTENDED_ID
) {
507 if (mxt_bootloader_read(data
, &buf
[0], 3) != 0) {
508 dev_err(dev
, "%s: i2c failure\n", __func__
);
512 dev_dbg(dev
, "Bootloader ID:%d Version:%d\n", buf
[1], buf
[2]);
516 dev_dbg(dev
, "Bootloader ID:%d\n", val
& MXT_BOOT_ID_MASK
);
522 static int mxt_check_bootloader(struct mxt_data
*data
, unsigned int state
,
525 struct device
*dev
= &data
->client
->dev
;
532 * In application update mode, the interrupt
533 * line signals state transitions. We must wait for the
534 * CHG assertion before reading the status byte.
535 * Once the status byte has been read, the line is deasserted.
537 ret
= mxt_wait_for_completion(data
, &data
->bl_completion
,
541 * TODO: handle -ERESTARTSYS better by terminating
542 * fw update process before returning to userspace
543 * by writing length 0x000 to device (iff we are in
544 * WAITING_FRAME_DATA state).
546 dev_err(dev
, "Update wait error %d\n", ret
);
551 ret
= mxt_bootloader_read(data
, &val
, 1);
555 if (state
== MXT_WAITING_BOOTLOAD_CMD
)
556 val
= mxt_get_bootloader_version(data
, val
);
559 case MXT_WAITING_BOOTLOAD_CMD
:
560 case MXT_WAITING_FRAME_DATA
:
561 case MXT_APP_CRC_FAIL
:
562 val
&= ~MXT_BOOT_STATUS_MASK
;
564 case MXT_FRAME_CRC_PASS
:
565 if (val
== MXT_FRAME_CRC_CHECK
) {
567 } else if (val
== MXT_FRAME_CRC_FAIL
) {
568 dev_err(dev
, "Bootloader CRC fail\n");
577 dev_err(dev
, "Invalid bootloader state %02X != %02X\n",
585 static int mxt_send_bootloader_cmd(struct mxt_data
*data
, bool unlock
)
591 buf
[0] = MXT_UNLOCK_CMD_LSB
;
592 buf
[1] = MXT_UNLOCK_CMD_MSB
;
598 ret
= mxt_bootloader_write(data
, buf
, 2);
605 static int __mxt_read_reg(struct i2c_client
*client
,
606 u16 reg
, u16 len
, void *val
)
608 struct i2c_msg xfer
[2];
613 buf
[1] = (reg
>> 8) & 0xff;
616 xfer
[0].addr
= client
->addr
;
622 xfer
[1].addr
= client
->addr
;
623 xfer
[1].flags
= I2C_M_RD
;
627 ret
= i2c_transfer(client
->adapter
, xfer
, 2);
633 dev_err(&client
->dev
, "%s: i2c transfer failed (%d)\n",
640 static int __mxt_write_reg(struct i2c_client
*client
, u16 reg
, u16 len
,
648 buf
= kmalloc(count
, GFP_KERNEL
);
653 buf
[1] = (reg
>> 8) & 0xff;
654 memcpy(&buf
[2], val
, len
);
656 ret
= i2c_master_send(client
, buf
, count
);
662 dev_err(&client
->dev
, "%s: i2c send failed (%d)\n",
670 static int mxt_write_reg(struct i2c_client
*client
, u16 reg
, u8 val
)
672 return __mxt_write_reg(client
, reg
, 1, &val
);
675 static struct mxt_object
*
676 mxt_get_object(struct mxt_data
*data
, u8 type
)
678 struct mxt_object
*object
;
681 for (i
= 0; i
< data
->info
.object_num
; i
++) {
682 object
= data
->object_table
+ i
;
683 if (object
->type
== type
)
687 dev_warn(&data
->client
->dev
, "Invalid object type T%u\n", type
);
691 static void mxt_proc_t6_messages(struct mxt_data
*data
, u8
*msg
)
693 struct device
*dev
= &data
->client
->dev
;
695 u32 crc
= msg
[2] | (msg
[3] << 8) | (msg
[4] << 16);
697 complete(&data
->crc_completion
);
699 if (crc
!= data
->config_crc
) {
700 data
->config_crc
= crc
;
701 dev_dbg(dev
, "T6 Config Checksum: 0x%06X\n", crc
);
705 if (status
& MXT_T6_STATUS_RESET
)
706 complete(&data
->reset_completion
);
708 /* Output debug if status has changed */
709 if (status
!= data
->t6_status
)
710 dev_dbg(dev
, "T6 Status 0x%02X%s%s%s%s%s%s%s\n",
712 status
== 0 ? " OK" : "",
713 status
& MXT_T6_STATUS_RESET
? " RESET" : "",
714 status
& MXT_T6_STATUS_OFL
? " OFL" : "",
715 status
& MXT_T6_STATUS_SIGERR
? " SIGERR" : "",
716 status
& MXT_T6_STATUS_CAL
? " CAL" : "",
717 status
& MXT_T6_STATUS_CFGERR
? " CFGERR" : "",
718 status
& MXT_T6_STATUS_COMSERR
? " COMSERR" : "");
720 /* Save current status */
721 data
->t6_status
= status
;
724 static int mxt_write_object(struct mxt_data
*data
,
725 u8 type
, u8 offset
, u8 val
)
727 struct mxt_object
*object
;
730 object
= mxt_get_object(data
, type
);
731 if (!object
|| offset
>= mxt_obj_size(object
))
734 reg
= object
->start_address
;
735 return mxt_write_reg(data
->client
, reg
+ offset
, val
);
738 static void mxt_input_button(struct mxt_data
*data
, u8
*message
)
740 struct input_dev
*input
= data
->input_dev
;
741 const struct mxt_platform_data
*pdata
= data
->pdata
;
744 for (i
= 0; i
< pdata
->t19_num_keys
; i
++) {
745 if (pdata
->t19_keymap
[i
] == KEY_RESERVED
)
748 /* Active-low switch */
749 input_report_key(input
, pdata
->t19_keymap
[i
],
750 !(message
[1] & BIT(i
)));
754 static void mxt_input_sync(struct mxt_data
*data
)
756 input_mt_report_pointer_emulation(data
->input_dev
,
757 data
->pdata
->t19_num_keys
);
758 input_sync(data
->input_dev
);
761 static void mxt_proc_t9_message(struct mxt_data
*data
, u8
*message
)
763 struct device
*dev
= &data
->client
->dev
;
764 struct input_dev
*input_dev
= data
->input_dev
;
772 id
= message
[0] - data
->T9_reportid_min
;
774 x
= (message
[2] << 4) | ((message
[4] >> 4) & 0xf);
775 y
= (message
[3] << 4) | ((message
[4] & 0xf));
777 /* Handle 10/12 bit switching */
778 if (data
->max_x
< 1024)
780 if (data
->max_y
< 1024)
784 amplitude
= message
[6];
787 "[%u] %c%c%c%c%c%c%c%c x: %5u y: %5u area: %3u amp: %3u\n",
789 (status
& MXT_T9_DETECT
) ? 'D' : '.',
790 (status
& MXT_T9_PRESS
) ? 'P' : '.',
791 (status
& MXT_T9_RELEASE
) ? 'R' : '.',
792 (status
& MXT_T9_MOVE
) ? 'M' : '.',
793 (status
& MXT_T9_VECTOR
) ? 'V' : '.',
794 (status
& MXT_T9_AMP
) ? 'A' : '.',
795 (status
& MXT_T9_SUPPRESS
) ? 'S' : '.',
796 (status
& MXT_T9_UNGRIP
) ? 'U' : '.',
797 x
, y
, area
, amplitude
);
799 input_mt_slot(input_dev
, id
);
801 if (status
& MXT_T9_DETECT
) {
803 * Multiple bits may be set if the host is slow to read
804 * the status messages, indicating all the events that
807 if (status
& MXT_T9_RELEASE
) {
808 input_mt_report_slot_state(input_dev
,
810 mxt_input_sync(data
);
814 input_mt_report_slot_state(input_dev
, MT_TOOL_FINGER
, 1);
815 input_report_abs(input_dev
, ABS_MT_POSITION_X
, x
);
816 input_report_abs(input_dev
, ABS_MT_POSITION_Y
, y
);
817 input_report_abs(input_dev
, ABS_MT_PRESSURE
, amplitude
);
818 input_report_abs(input_dev
, ABS_MT_TOUCH_MAJOR
, area
);
820 /* Touch no longer active, close out slot */
821 input_mt_report_slot_state(input_dev
, MT_TOOL_FINGER
, 0);
824 data
->update_input
= true;
827 static void mxt_proc_t100_message(struct mxt_data
*data
, u8
*message
)
829 struct device
*dev
= &data
->client
->dev
;
830 struct input_dev
*input_dev
= data
->input_dev
;
842 id
= message
[0] - data
->T100_reportid_min
- 2;
844 /* ignore SCRSTATUS events */
849 x
= get_unaligned_le16(&message
[2]);
850 y
= get_unaligned_le16(&message
[4]);
852 if (status
& MXT_T100_DETECT
) {
853 type
= (status
& MXT_T100_TYPE_MASK
) >> 4;
856 case MXT_T100_TYPE_HOVERING_FINGER
:
857 tool
= MT_TOOL_FINGER
;
858 distance
= MXT_DISTANCE_HOVERING
;
860 if (data
->t100_aux_vect
)
861 orientation
= message
[data
->t100_aux_vect
];
865 case MXT_T100_TYPE_FINGER
:
866 case MXT_T100_TYPE_GLOVE
:
867 tool
= MT_TOOL_FINGER
;
868 distance
= MXT_DISTANCE_ACTIVE_TOUCH
;
870 if (data
->t100_aux_area
)
871 major
= message
[data
->t100_aux_area
];
873 if (data
->t100_aux_ampl
)
874 pressure
= message
[data
->t100_aux_ampl
];
876 if (data
->t100_aux_vect
)
877 orientation
= message
[data
->t100_aux_vect
];
881 case MXT_T100_TYPE_PASSIVE_STYLUS
:
885 * Passive stylus is reported with size zero so
888 major
= MXT_TOUCH_MAJOR_DEFAULT
;
890 if (data
->t100_aux_ampl
)
891 pressure
= message
[data
->t100_aux_ampl
];
895 case MXT_T100_TYPE_LARGE_TOUCH
:
896 /* Ignore suppressed touch */
900 dev_dbg(dev
, "Unexpected T100 type\n");
906 * Values reported should be non-zero if tool is touching the
909 if (!pressure
&& type
!= MXT_T100_TYPE_HOVERING_FINGER
)
910 pressure
= MXT_PRESSURE_DEFAULT
;
912 input_mt_slot(input_dev
, id
);
914 if (status
& MXT_T100_DETECT
) {
915 dev_dbg(dev
, "[%u] type:%u x:%u y:%u a:%02X p:%02X v:%02X\n",
916 id
, type
, x
, y
, major
, pressure
, orientation
);
918 input_mt_report_slot_state(input_dev
, tool
, 1);
919 input_report_abs(input_dev
, ABS_MT_POSITION_X
, x
);
920 input_report_abs(input_dev
, ABS_MT_POSITION_Y
, y
);
921 input_report_abs(input_dev
, ABS_MT_TOUCH_MAJOR
, major
);
922 input_report_abs(input_dev
, ABS_MT_PRESSURE
, pressure
);
923 input_report_abs(input_dev
, ABS_MT_DISTANCE
, distance
);
924 input_report_abs(input_dev
, ABS_MT_ORIENTATION
, orientation
);
926 dev_dbg(dev
, "[%u] release\n", id
);
929 input_mt_report_slot_state(input_dev
, 0, 0);
932 data
->update_input
= true;
935 static int mxt_proc_message(struct mxt_data
*data
, u8
*message
)
937 u8 report_id
= message
[0];
939 if (report_id
== MXT_RPTID_NOMSG
)
942 if (report_id
== data
->T6_reportid
) {
943 mxt_proc_t6_messages(data
, message
);
944 } else if (!data
->input_dev
) {
946 * Do not report events if input device
947 * is not yet registered.
949 mxt_dump_message(data
, message
);
950 } else if (report_id
>= data
->T9_reportid_min
&&
951 report_id
<= data
->T9_reportid_max
) {
952 mxt_proc_t9_message(data
, message
);
953 } else if (report_id
>= data
->T100_reportid_min
&&
954 report_id
<= data
->T100_reportid_max
) {
955 mxt_proc_t100_message(data
, message
);
956 } else if (report_id
== data
->T19_reportid
) {
957 mxt_input_button(data
, message
);
958 data
->update_input
= true;
960 mxt_dump_message(data
, message
);
966 static int mxt_read_and_process_messages(struct mxt_data
*data
, u8 count
)
968 struct device
*dev
= &data
->client
->dev
;
973 /* Safety check for msg_buf */
974 if (count
> data
->max_reportid
)
977 /* Process remaining messages if necessary */
978 ret
= __mxt_read_reg(data
->client
, data
->T5_address
,
979 data
->T5_msg_size
* count
, data
->msg_buf
);
981 dev_err(dev
, "Failed to read %u messages (%d)\n", count
, ret
);
985 for (i
= 0; i
< count
; i
++) {
986 ret
= mxt_proc_message(data
,
987 data
->msg_buf
+ data
->T5_msg_size
* i
);
993 /* return number of messages read */
997 static irqreturn_t
mxt_process_messages_t44(struct mxt_data
*data
)
999 struct device
*dev
= &data
->client
->dev
;
1003 /* Read T44 and T5 together */
1004 ret
= __mxt_read_reg(data
->client
, data
->T44_address
,
1005 data
->T5_msg_size
+ 1, data
->msg_buf
);
1007 dev_err(dev
, "Failed to read T44 and T5 (%d)\n", ret
);
1011 count
= data
->msg_buf
[0];
1014 * This condition may be caused by the CHG line being configured in
1015 * Mode 0. It results in unnecessary I2C operations but it is benign.
1020 if (count
> data
->max_reportid
) {
1021 dev_warn(dev
, "T44 count %d exceeded max report id\n", count
);
1022 count
= data
->max_reportid
;
1025 /* Process first message */
1026 ret
= mxt_proc_message(data
, data
->msg_buf
+ 1);
1028 dev_warn(dev
, "Unexpected invalid message\n");
1032 num_left
= count
- 1;
1034 /* Process remaining messages if necessary */
1036 ret
= mxt_read_and_process_messages(data
, num_left
);
1039 else if (ret
!= num_left
)
1040 dev_warn(dev
, "Unexpected invalid message\n");
1044 if (data
->update_input
) {
1045 mxt_input_sync(data
);
1046 data
->update_input
= false;
1052 static int mxt_process_messages_until_invalid(struct mxt_data
*data
)
1054 struct device
*dev
= &data
->client
->dev
;
1058 count
= data
->max_reportid
;
1060 /* Read messages until we force an invalid */
1062 read
= mxt_read_and_process_messages(data
, count
);
1067 if (data
->update_input
) {
1068 mxt_input_sync(data
);
1069 data
->update_input
= false;
1072 dev_err(dev
, "CHG pin isn't cleared\n");
1076 static irqreturn_t
mxt_process_messages(struct mxt_data
*data
)
1078 int total_handled
, num_handled
;
1079 u8 count
= data
->last_message_count
;
1081 if (count
< 1 || count
> data
->max_reportid
)
1084 /* include final invalid message */
1085 total_handled
= mxt_read_and_process_messages(data
, count
+ 1);
1086 if (total_handled
< 0)
1088 /* if there were invalid messages, then we are done */
1089 else if (total_handled
<= count
)
1092 /* keep reading two msgs until one is invalid or reportid limit */
1094 num_handled
= mxt_read_and_process_messages(data
, 2);
1095 if (num_handled
< 0)
1098 total_handled
+= num_handled
;
1100 if (num_handled
< 2)
1102 } while (total_handled
< data
->num_touchids
);
1105 data
->last_message_count
= total_handled
;
1107 if (data
->update_input
) {
1108 mxt_input_sync(data
);
1109 data
->update_input
= false;
1115 static irqreturn_t
mxt_interrupt(int irq
, void *dev_id
)
1117 struct mxt_data
*data
= dev_id
;
1119 if (data
->in_bootloader
) {
1120 /* bootloader state transition completion */
1121 complete(&data
->bl_completion
);
1125 if (!data
->object_table
)
1128 if (data
->T44_address
) {
1129 return mxt_process_messages_t44(data
);
1131 return mxt_process_messages(data
);
1135 static int mxt_t6_command(struct mxt_data
*data
, u16 cmd_offset
,
1136 u8 value
, bool wait
)
1139 u8 command_register
;
1140 int timeout_counter
= 0;
1143 reg
= data
->T6_address
+ cmd_offset
;
1145 ret
= mxt_write_reg(data
->client
, reg
, value
);
1154 ret
= __mxt_read_reg(data
->client
, reg
, 1, &command_register
);
1157 } while (command_register
!= 0 && timeout_counter
++ <= 100);
1159 if (timeout_counter
> 100) {
1160 dev_err(&data
->client
->dev
, "Command failed!\n");
1167 static int mxt_acquire_irq(struct mxt_data
*data
)
1171 enable_irq(data
->irq
);
1173 error
= mxt_process_messages_until_invalid(data
);
1180 static int mxt_soft_reset(struct mxt_data
*data
)
1182 struct device
*dev
= &data
->client
->dev
;
1185 dev_info(dev
, "Resetting device\n");
1187 disable_irq(data
->irq
);
1189 reinit_completion(&data
->reset_completion
);
1191 ret
= mxt_t6_command(data
, MXT_COMMAND_RESET
, MXT_RESET_VALUE
, false);
1195 /* Ignore CHG line for 100ms after reset */
1198 mxt_acquire_irq(data
);
1200 ret
= mxt_wait_for_completion(data
, &data
->reset_completion
,
1208 static void mxt_update_crc(struct mxt_data
*data
, u8 cmd
, u8 value
)
1211 * On failure, CRC is set to 0 and config will always be
1214 data
->config_crc
= 0;
1215 reinit_completion(&data
->crc_completion
);
1217 mxt_t6_command(data
, cmd
, value
, true);
1220 * Wait for crc message. On failure, CRC is set to 0 and config will
1221 * always be downloaded.
1223 mxt_wait_for_completion(data
, &data
->crc_completion
, MXT_CRC_TIMEOUT
);
1226 static void mxt_calc_crc24(u32
*crc
, u8 firstbyte
, u8 secondbyte
)
1228 static const unsigned int crcpoly
= 0x80001B;
1232 data_word
= (secondbyte
<< 8) | firstbyte
;
1233 result
= ((*crc
<< 1) ^ data_word
);
1235 if (result
& 0x1000000)
1241 static u32
mxt_calculate_crc(u8
*base
, off_t start_off
, off_t end_off
)
1244 u8
*ptr
= base
+ start_off
;
1245 u8
*last_val
= base
+ end_off
- 1;
1247 if (end_off
< start_off
)
1250 while (ptr
< last_val
) {
1251 mxt_calc_crc24(&crc
, *ptr
, *(ptr
+ 1));
1255 /* if len is odd, fill the last byte with 0 */
1256 if (ptr
== last_val
)
1257 mxt_calc_crc24(&crc
, *ptr
, 0);
1259 /* Mask to 24-bit */
1265 static int mxt_prepare_cfg_mem(struct mxt_data
*data
,
1266 const struct firmware
*cfg
,
1267 unsigned int data_pos
,
1268 unsigned int cfg_start_ofs
,
1270 size_t config_mem_size
)
1272 struct device
*dev
= &data
->client
->dev
;
1273 struct mxt_object
*object
;
1274 unsigned int type
, instance
, size
, byte_offset
;
1281 while (data_pos
< cfg
->size
) {
1282 /* Read type, instance, length */
1283 ret
= sscanf(cfg
->data
+ data_pos
, "%x %x %x%n",
1284 &type
, &instance
, &size
, &offset
);
1288 } else if (ret
!= 3) {
1289 dev_err(dev
, "Bad format: failed to parse object\n");
1294 object
= mxt_get_object(data
, type
);
1297 for (i
= 0; i
< size
; i
++) {
1298 ret
= sscanf(cfg
->data
+ data_pos
, "%hhx%n",
1301 dev_err(dev
, "Bad format in T%d at %d\n",
1310 if (size
> mxt_obj_size(object
)) {
1312 * Either we are in fallback mode due to wrong
1313 * config or config from a later fw version,
1314 * or the file is corrupt or hand-edited.
1316 dev_warn(dev
, "Discarding %zu byte(s) in T%u\n",
1317 size
- mxt_obj_size(object
), type
);
1318 } else if (mxt_obj_size(object
) > size
) {
1320 * If firmware is upgraded, new bytes may be added to
1321 * end of objects. It is generally forward compatible
1322 * to zero these bytes - previous behaviour will be
1323 * retained. However this does invalidate the CRC and
1324 * will force fallback mode until the configuration is
1325 * updated. We warn here but do nothing else - the
1326 * malloc has zeroed the entire configuration.
1328 dev_warn(dev
, "Zeroing %zu byte(s) in T%d\n",
1329 mxt_obj_size(object
) - size
, type
);
1332 if (instance
>= mxt_obj_instances(object
)) {
1333 dev_err(dev
, "Object instances exceeded!\n");
1337 reg
= object
->start_address
+ mxt_obj_size(object
) * instance
;
1339 for (i
= 0; i
< size
; i
++) {
1340 ret
= sscanf(cfg
->data
+ data_pos
, "%hhx%n",
1344 dev_err(dev
, "Bad format in T%d at %d\n",
1350 if (i
> mxt_obj_size(object
))
1353 byte_offset
= reg
+ i
- cfg_start_ofs
;
1355 if (byte_offset
>= 0 && byte_offset
< config_mem_size
) {
1356 *(config_mem
+ byte_offset
) = val
;
1358 dev_err(dev
, "Bad object: reg:%d, T%d, ofs=%d\n",
1359 reg
, object
->type
, byte_offset
);
1368 static int mxt_upload_cfg_mem(struct mxt_data
*data
, unsigned int cfg_start
,
1369 u8
*config_mem
, size_t config_mem_size
)
1371 unsigned int byte_offset
= 0;
1374 /* Write configuration as blocks */
1375 while (byte_offset
< config_mem_size
) {
1376 unsigned int size
= config_mem_size
- byte_offset
;
1378 if (size
> MXT_MAX_BLOCK_WRITE
)
1379 size
= MXT_MAX_BLOCK_WRITE
;
1381 error
= __mxt_write_reg(data
->client
,
1382 cfg_start
+ byte_offset
,
1383 size
, config_mem
+ byte_offset
);
1385 dev_err(&data
->client
->dev
,
1386 "Config write error, ret=%d\n", error
);
1390 byte_offset
+= size
;
1396 static int mxt_init_t7_power_cfg(struct mxt_data
*data
);
1399 * mxt_update_cfg - download configuration to chip
1401 * Atmel Raw Config File Format
1403 * The first four lines of the raw config file contain:
1405 * 2) Chip ID Information (first 7 bytes of device memory)
1406 * 3) Chip Information Block 24-bit CRC Checksum
1407 * 4) Chip Configuration 24-bit CRC Checksum
1409 * The rest of the file consists of one line per object instance:
1410 * <TYPE> <INSTANCE> <SIZE> <CONTENTS>
1412 * <TYPE> - 2-byte object type as hex
1413 * <INSTANCE> - 2-byte object instance number as hex
1414 * <SIZE> - 2-byte object size as hex
1415 * <CONTENTS> - array of <SIZE> 1-byte hex values
1417 static int mxt_update_cfg(struct mxt_data
*data
, const struct firmware
*cfg
)
1419 struct device
*dev
= &data
->client
->dev
;
1420 struct mxt_info cfg_info
;
1426 u32 info_crc
, config_crc
, calculated_crc
;
1428 size_t config_mem_size
;
1430 mxt_update_crc(data
, MXT_COMMAND_REPORTALL
, 1);
1432 if (strncmp(cfg
->data
, MXT_CFG_MAGIC
, strlen(MXT_CFG_MAGIC
))) {
1433 dev_err(dev
, "Unrecognised config file\n");
1437 data_pos
= strlen(MXT_CFG_MAGIC
);
1439 /* Load information block and check */
1440 for (i
= 0; i
< sizeof(struct mxt_info
); i
++) {
1441 ret
= sscanf(cfg
->data
+ data_pos
, "%hhx%n",
1442 (unsigned char *)&cfg_info
+ i
,
1445 dev_err(dev
, "Bad format\n");
1452 if (cfg_info
.family_id
!= data
->info
.family_id
) {
1453 dev_err(dev
, "Family ID mismatch!\n");
1457 if (cfg_info
.variant_id
!= data
->info
.variant_id
) {
1458 dev_err(dev
, "Variant ID mismatch!\n");
1463 ret
= sscanf(cfg
->data
+ data_pos
, "%x%n", &info_crc
, &offset
);
1465 dev_err(dev
, "Bad format: failed to parse Info CRC\n");
1470 ret
= sscanf(cfg
->data
+ data_pos
, "%x%n", &config_crc
, &offset
);
1472 dev_err(dev
, "Bad format: failed to parse Config CRC\n");
1478 * The Info Block CRC is calculated over mxt_info and the object
1479 * table. If it does not match then we are trying to load the
1480 * configuration from a different chip or firmware version, so
1481 * the configuration CRC is invalid anyway.
1483 if (info_crc
== data
->info_crc
) {
1484 if (config_crc
== 0 || data
->config_crc
== 0) {
1485 dev_info(dev
, "CRC zero, attempting to apply config\n");
1486 } else if (config_crc
== data
->config_crc
) {
1487 dev_dbg(dev
, "Config CRC 0x%06X: OK\n",
1491 dev_info(dev
, "Config CRC 0x%06X: does not match file 0x%06X\n",
1492 data
->config_crc
, config_crc
);
1496 "Warning: Info CRC error - device=0x%06X file=0x%06X\n",
1497 data
->info_crc
, info_crc
);
1500 /* Malloc memory to store configuration */
1501 cfg_start_ofs
= MXT_OBJECT_START
+
1502 data
->info
.object_num
* sizeof(struct mxt_object
) +
1503 MXT_INFO_CHECKSUM_SIZE
;
1504 config_mem_size
= data
->mem_size
- cfg_start_ofs
;
1505 config_mem
= kzalloc(config_mem_size
, GFP_KERNEL
);
1507 dev_err(dev
, "Failed to allocate memory\n");
1511 ret
= mxt_prepare_cfg_mem(data
, cfg
, data_pos
, cfg_start_ofs
,
1512 config_mem
, config_mem_size
);
1516 /* Calculate crc of the received configs (not the raw config file) */
1517 if (data
->T7_address
< cfg_start_ofs
) {
1518 dev_err(dev
, "Bad T7 address, T7addr = %x, config offset %x\n",
1519 data
->T7_address
, cfg_start_ofs
);
1524 calculated_crc
= mxt_calculate_crc(config_mem
,
1525 data
->T7_address
- cfg_start_ofs
,
1528 if (config_crc
> 0 && config_crc
!= calculated_crc
)
1529 dev_warn(dev
, "Config CRC error, calculated=%06X, file=%06X\n",
1530 calculated_crc
, config_crc
);
1532 ret
= mxt_upload_cfg_mem(data
, cfg_start_ofs
,
1533 config_mem
, config_mem_size
);
1537 mxt_update_crc(data
, MXT_COMMAND_BACKUPNV
, MXT_BACKUP_VALUE
);
1539 ret
= mxt_soft_reset(data
);
1543 dev_info(dev
, "Config successfully updated\n");
1545 /* T7 config may have changed */
1546 mxt_init_t7_power_cfg(data
);
1553 static int mxt_get_info(struct mxt_data
*data
)
1555 struct i2c_client
*client
= data
->client
;
1556 struct mxt_info
*info
= &data
->info
;
1559 /* Read 7-byte info block starting at address 0 */
1560 error
= __mxt_read_reg(client
, 0, sizeof(*info
), info
);
1567 static void mxt_free_input_device(struct mxt_data
*data
)
1569 if (data
->input_dev
) {
1570 input_unregister_device(data
->input_dev
);
1571 data
->input_dev
= NULL
;
1575 static void mxt_free_object_table(struct mxt_data
*data
)
1577 #ifdef CONFIG_TOUCHSCREEN_ATMEL_MXT_T37
1578 video_unregister_device(&data
->dbg
.vdev
);
1579 v4l2_device_unregister(&data
->dbg
.v4l2
);
1582 kfree(data
->object_table
);
1583 data
->object_table
= NULL
;
1584 kfree(data
->msg_buf
);
1585 data
->msg_buf
= NULL
;
1586 data
->T5_address
= 0;
1587 data
->T5_msg_size
= 0;
1588 data
->T6_reportid
= 0;
1589 data
->T7_address
= 0;
1590 data
->T9_reportid_min
= 0;
1591 data
->T9_reportid_max
= 0;
1592 data
->T19_reportid
= 0;
1593 data
->T44_address
= 0;
1594 data
->T100_reportid_min
= 0;
1595 data
->T100_reportid_max
= 0;
1596 data
->max_reportid
= 0;
1599 static int mxt_get_object_table(struct mxt_data
*data
)
1601 struct i2c_client
*client
= data
->client
;
1603 struct mxt_object
*object_table
;
1609 table_size
= data
->info
.object_num
* sizeof(struct mxt_object
);
1610 object_table
= kzalloc(table_size
, GFP_KERNEL
);
1611 if (!object_table
) {
1612 dev_err(&data
->client
->dev
, "Failed to allocate memory\n");
1616 error
= __mxt_read_reg(client
, MXT_OBJECT_START
, table_size
,
1619 kfree(object_table
);
1623 /* Valid Report IDs start counting from 1 */
1626 for (i
= 0; i
< data
->info
.object_num
; i
++) {
1627 struct mxt_object
*object
= object_table
+ i
;
1630 le16_to_cpus(&object
->start_address
);
1632 if (object
->num_report_ids
) {
1634 reportid
+= object
->num_report_ids
*
1635 mxt_obj_instances(object
);
1636 max_id
= reportid
- 1;
1642 dev_dbg(&data
->client
->dev
,
1643 "T%u Start:%u Size:%zu Instances:%zu Report IDs:%u-%u\n",
1644 object
->type
, object
->start_address
,
1645 mxt_obj_size(object
), mxt_obj_instances(object
),
1648 switch (object
->type
) {
1649 case MXT_GEN_MESSAGE_T5
:
1650 if (data
->info
.family_id
== 0x80 &&
1651 data
->info
.version
< 0x20) {
1653 * On mXT224 firmware versions prior to V2.0
1654 * read and discard unused CRC byte otherwise
1655 * DMA reads are misaligned.
1657 data
->T5_msg_size
= mxt_obj_size(object
);
1659 /* CRC not enabled, so skip last byte */
1660 data
->T5_msg_size
= mxt_obj_size(object
) - 1;
1662 data
->T5_address
= object
->start_address
;
1664 case MXT_GEN_COMMAND_T6
:
1665 data
->T6_reportid
= min_id
;
1666 data
->T6_address
= object
->start_address
;
1668 case MXT_GEN_POWER_T7
:
1669 data
->T7_address
= object
->start_address
;
1671 case MXT_TOUCH_MULTI_T9
:
1672 data
->multitouch
= MXT_TOUCH_MULTI_T9
;
1673 data
->T9_reportid_min
= min_id
;
1674 data
->T9_reportid_max
= max_id
;
1675 data
->num_touchids
= object
->num_report_ids
1676 * mxt_obj_instances(object
);
1678 case MXT_SPT_MESSAGECOUNT_T44
:
1679 data
->T44_address
= object
->start_address
;
1681 case MXT_SPT_GPIOPWM_T19
:
1682 data
->T19_reportid
= min_id
;
1684 case MXT_TOUCH_MULTITOUCHSCREEN_T100
:
1685 data
->multitouch
= MXT_TOUCH_MULTITOUCHSCREEN_T100
;
1686 data
->T100_reportid_min
= min_id
;
1687 data
->T100_reportid_max
= max_id
;
1688 /* first two report IDs reserved */
1689 data
->num_touchids
= object
->num_report_ids
- 2;
1693 end_address
= object
->start_address
1694 + mxt_obj_size(object
) * mxt_obj_instances(object
) - 1;
1696 if (end_address
>= data
->mem_size
)
1697 data
->mem_size
= end_address
+ 1;
1700 /* Store maximum reportid */
1701 data
->max_reportid
= reportid
;
1703 /* If T44 exists, T5 position has to be directly after */
1704 if (data
->T44_address
&& (data
->T5_address
!= data
->T44_address
+ 1)) {
1705 dev_err(&client
->dev
, "Invalid T44 position\n");
1707 goto free_object_table
;
1710 data
->msg_buf
= kcalloc(data
->max_reportid
,
1711 data
->T5_msg_size
, GFP_KERNEL
);
1712 if (!data
->msg_buf
) {
1713 dev_err(&client
->dev
, "Failed to allocate message buffer\n");
1715 goto free_object_table
;
1718 data
->object_table
= object_table
;
1723 mxt_free_object_table(data
);
1727 static int mxt_read_t9_resolution(struct mxt_data
*data
)
1729 struct i2c_client
*client
= data
->client
;
1731 struct t9_range range
;
1732 unsigned char orient
;
1733 struct mxt_object
*object
;
1735 object
= mxt_get_object(data
, MXT_TOUCH_MULTI_T9
);
1739 error
= __mxt_read_reg(client
,
1740 object
->start_address
+ MXT_T9_XSIZE
,
1741 sizeof(data
->xsize
), &data
->xsize
);
1745 error
= __mxt_read_reg(client
,
1746 object
->start_address
+ MXT_T9_YSIZE
,
1747 sizeof(data
->ysize
), &data
->ysize
);
1751 error
= __mxt_read_reg(client
,
1752 object
->start_address
+ MXT_T9_RANGE
,
1753 sizeof(range
), &range
);
1757 data
->max_x
= get_unaligned_le16(&range
.x
);
1758 data
->max_y
= get_unaligned_le16(&range
.y
);
1760 error
= __mxt_read_reg(client
,
1761 object
->start_address
+ MXT_T9_ORIENT
,
1766 data
->xy_switch
= orient
& MXT_T9_ORIENT_SWITCH
;
1767 data
->invertx
= orient
& MXT_T9_ORIENT_INVERTX
;
1768 data
->inverty
= orient
& MXT_T9_ORIENT_INVERTY
;
1773 static int mxt_read_t100_config(struct mxt_data
*data
)
1775 struct i2c_client
*client
= data
->client
;
1777 struct mxt_object
*object
;
1778 u16 range_x
, range_y
;
1782 object
= mxt_get_object(data
, MXT_TOUCH_MULTITOUCHSCREEN_T100
);
1786 /* read touchscreen dimensions */
1787 error
= __mxt_read_reg(client
,
1788 object
->start_address
+ MXT_T100_XRANGE
,
1789 sizeof(range_x
), &range_x
);
1793 data
->max_x
= get_unaligned_le16(&range_x
);
1795 error
= __mxt_read_reg(client
,
1796 object
->start_address
+ MXT_T100_YRANGE
,
1797 sizeof(range_y
), &range_y
);
1801 data
->max_y
= get_unaligned_le16(&range_y
);
1803 error
= __mxt_read_reg(client
,
1804 object
->start_address
+ MXT_T100_XSIZE
,
1805 sizeof(data
->xsize
), &data
->xsize
);
1809 error
= __mxt_read_reg(client
,
1810 object
->start_address
+ MXT_T100_YSIZE
,
1811 sizeof(data
->ysize
), &data
->ysize
);
1815 /* read orientation config */
1816 error
= __mxt_read_reg(client
,
1817 object
->start_address
+ MXT_T100_CFG1
,
1822 data
->xy_switch
= cfg
& MXT_T100_CFG_SWITCHXY
;
1823 data
->invertx
= cfg
& MXT_T100_CFG_INVERTX
;
1824 data
->inverty
= cfg
& MXT_T100_CFG_INVERTY
;
1826 /* allocate aux bytes */
1827 error
= __mxt_read_reg(client
,
1828 object
->start_address
+ MXT_T100_TCHAUX
,
1835 if (tchaux
& MXT_T100_TCHAUX_VECT
)
1836 data
->t100_aux_vect
= aux
++;
1838 if (tchaux
& MXT_T100_TCHAUX_AMPL
)
1839 data
->t100_aux_ampl
= aux
++;
1841 if (tchaux
& MXT_T100_TCHAUX_AREA
)
1842 data
->t100_aux_area
= aux
++;
1844 dev_dbg(&client
->dev
,
1845 "T100 aux mappings vect:%u ampl:%u area:%u\n",
1846 data
->t100_aux_vect
, data
->t100_aux_ampl
, data
->t100_aux_area
);
1851 static int mxt_input_open(struct input_dev
*dev
);
1852 static void mxt_input_close(struct input_dev
*dev
);
1854 static void mxt_set_up_as_touchpad(struct input_dev
*input_dev
,
1855 struct mxt_data
*data
)
1857 const struct mxt_platform_data
*pdata
= data
->pdata
;
1860 input_dev
->name
= "Atmel maXTouch Touchpad";
1862 __set_bit(INPUT_PROP_BUTTONPAD
, input_dev
->propbit
);
1864 input_abs_set_res(input_dev
, ABS_X
, MXT_PIXELS_PER_MM
);
1865 input_abs_set_res(input_dev
, ABS_Y
, MXT_PIXELS_PER_MM
);
1866 input_abs_set_res(input_dev
, ABS_MT_POSITION_X
,
1868 input_abs_set_res(input_dev
, ABS_MT_POSITION_Y
,
1871 for (i
= 0; i
< pdata
->t19_num_keys
; i
++)
1872 if (pdata
->t19_keymap
[i
] != KEY_RESERVED
)
1873 input_set_capability(input_dev
, EV_KEY
,
1874 pdata
->t19_keymap
[i
]);
1877 static int mxt_initialize_input_device(struct mxt_data
*data
)
1879 const struct mxt_platform_data
*pdata
= data
->pdata
;
1880 struct device
*dev
= &data
->client
->dev
;
1881 struct input_dev
*input_dev
;
1883 unsigned int num_mt_slots
;
1884 unsigned int mt_flags
= 0;
1886 switch (data
->multitouch
) {
1887 case MXT_TOUCH_MULTI_T9
:
1888 num_mt_slots
= data
->T9_reportid_max
- data
->T9_reportid_min
+ 1;
1889 error
= mxt_read_t9_resolution(data
);
1891 dev_warn(dev
, "Failed to initialize T9 resolution\n");
1894 case MXT_TOUCH_MULTITOUCHSCREEN_T100
:
1895 num_mt_slots
= data
->num_touchids
;
1896 error
= mxt_read_t100_config(data
);
1898 dev_warn(dev
, "Failed to read T100 config\n");
1902 dev_err(dev
, "Invalid multitouch object\n");
1906 /* Handle default values and orientation switch */
1907 if (data
->max_x
== 0)
1910 if (data
->max_y
== 0)
1913 if (data
->xy_switch
)
1914 swap(data
->max_x
, data
->max_y
);
1916 dev_info(dev
, "Touchscreen size X%uY%u\n", data
->max_x
, data
->max_y
);
1918 /* Register input device */
1919 input_dev
= input_allocate_device();
1921 dev_err(dev
, "Failed to allocate memory\n");
1925 input_dev
->name
= "Atmel maXTouch Touchscreen";
1926 input_dev
->phys
= data
->phys
;
1927 input_dev
->id
.bustype
= BUS_I2C
;
1928 input_dev
->dev
.parent
= dev
;
1929 input_dev
->open
= mxt_input_open
;
1930 input_dev
->close
= mxt_input_close
;
1932 input_set_capability(input_dev
, EV_KEY
, BTN_TOUCH
);
1934 /* For single touch */
1935 input_set_abs_params(input_dev
, ABS_X
, 0, data
->max_x
, 0, 0);
1936 input_set_abs_params(input_dev
, ABS_Y
, 0, data
->max_y
, 0, 0);
1938 if (data
->multitouch
== MXT_TOUCH_MULTI_T9
||
1939 (data
->multitouch
== MXT_TOUCH_MULTITOUCHSCREEN_T100
&&
1940 data
->t100_aux_ampl
)) {
1941 input_set_abs_params(input_dev
, ABS_PRESSURE
, 0, 255, 0, 0);
1944 /* If device has buttons we assume it is a touchpad */
1945 if (pdata
->t19_num_keys
) {
1946 mxt_set_up_as_touchpad(input_dev
, data
);
1947 mt_flags
|= INPUT_MT_POINTER
;
1949 mt_flags
|= INPUT_MT_DIRECT
;
1952 /* For multi touch */
1953 error
= input_mt_init_slots(input_dev
, num_mt_slots
, mt_flags
);
1955 dev_err(dev
, "Error %d initialising slots\n", error
);
1959 if (data
->multitouch
== MXT_TOUCH_MULTITOUCHSCREEN_T100
) {
1960 input_set_abs_params(input_dev
, ABS_MT_TOOL_TYPE
,
1961 0, MT_TOOL_MAX
, 0, 0);
1962 input_set_abs_params(input_dev
, ABS_MT_DISTANCE
,
1963 MXT_DISTANCE_ACTIVE_TOUCH
,
1964 MXT_DISTANCE_HOVERING
,
1968 input_set_abs_params(input_dev
, ABS_MT_POSITION_X
,
1969 0, data
->max_x
, 0, 0);
1970 input_set_abs_params(input_dev
, ABS_MT_POSITION_Y
,
1971 0, data
->max_y
, 0, 0);
1973 if (data
->multitouch
== MXT_TOUCH_MULTI_T9
||
1974 (data
->multitouch
== MXT_TOUCH_MULTITOUCHSCREEN_T100
&&
1975 data
->t100_aux_area
)) {
1976 input_set_abs_params(input_dev
, ABS_MT_TOUCH_MAJOR
,
1977 0, MXT_MAX_AREA
, 0, 0);
1980 if (data
->multitouch
== MXT_TOUCH_MULTI_T9
||
1981 (data
->multitouch
== MXT_TOUCH_MULTITOUCHSCREEN_T100
&&
1982 data
->t100_aux_ampl
)) {
1983 input_set_abs_params(input_dev
, ABS_MT_PRESSURE
,
1987 if (data
->multitouch
== MXT_TOUCH_MULTITOUCHSCREEN_T100
&&
1988 data
->t100_aux_vect
) {
1989 input_set_abs_params(input_dev
, ABS_MT_ORIENTATION
,
1993 if (data
->multitouch
== MXT_TOUCH_MULTITOUCHSCREEN_T100
&&
1994 data
->t100_aux_ampl
) {
1995 input_set_abs_params(input_dev
, ABS_MT_PRESSURE
,
1999 if (data
->multitouch
== MXT_TOUCH_MULTITOUCHSCREEN_T100
&&
2000 data
->t100_aux_vect
) {
2001 input_set_abs_params(input_dev
, ABS_MT_ORIENTATION
,
2005 input_set_drvdata(input_dev
, data
);
2007 error
= input_register_device(input_dev
);
2009 dev_err(dev
, "Error %d registering input device\n", error
);
2013 data
->input_dev
= input_dev
;
2018 input_free_device(input_dev
);
2022 static int mxt_configure_objects(struct mxt_data
*data
,
2023 const struct firmware
*cfg
);
2025 static void mxt_config_cb(const struct firmware
*cfg
, void *ctx
)
2027 mxt_configure_objects(ctx
, cfg
);
2028 release_firmware(cfg
);
2031 static int mxt_initialize(struct mxt_data
*data
)
2033 struct i2c_client
*client
= data
->client
;
2034 int recovery_attempts
= 0;
2038 error
= mxt_get_info(data
);
2042 /* Check bootloader state */
2043 error
= mxt_probe_bootloader(data
, false);
2045 dev_info(&client
->dev
, "Trying alternate bootloader address\n");
2046 error
= mxt_probe_bootloader(data
, true);
2048 /* Chip is not in appmode or bootloader mode */
2053 /* OK, we are in bootloader, see if we can recover */
2054 if (++recovery_attempts
> 1) {
2055 dev_err(&client
->dev
, "Could not recover from bootloader mode\n");
2057 * We can reflash from this state, so do not
2058 * abort initialization.
2060 data
->in_bootloader
= true;
2064 /* Attempt to exit bootloader into app mode */
2065 mxt_send_bootloader_cmd(data
, false);
2066 msleep(MXT_FW_RESET_TIME
);
2069 /* Get object table information */
2070 error
= mxt_get_object_table(data
);
2072 dev_err(&client
->dev
, "Error %d reading object table\n", error
);
2076 error
= mxt_acquire_irq(data
);
2078 goto err_free_object_table
;
2080 error
= request_firmware_nowait(THIS_MODULE
, true, MXT_CFG_NAME
,
2081 &client
->dev
, GFP_KERNEL
, data
,
2084 dev_err(&client
->dev
, "Failed to invoke firmware loader: %d\n",
2086 goto err_free_object_table
;
2091 err_free_object_table
:
2092 mxt_free_object_table(data
);
2096 static int mxt_set_t7_power_cfg(struct mxt_data
*data
, u8 sleep
)
2098 struct device
*dev
= &data
->client
->dev
;
2100 struct t7_config
*new_config
;
2101 struct t7_config deepsleep
= { .active
= 0, .idle
= 0 };
2103 if (sleep
== MXT_POWER_CFG_DEEPSLEEP
)
2104 new_config
= &deepsleep
;
2106 new_config
= &data
->t7_cfg
;
2108 error
= __mxt_write_reg(data
->client
, data
->T7_address
,
2109 sizeof(data
->t7_cfg
), new_config
);
2113 dev_dbg(dev
, "Set T7 ACTV:%d IDLE:%d\n",
2114 new_config
->active
, new_config
->idle
);
2119 static int mxt_init_t7_power_cfg(struct mxt_data
*data
)
2121 struct device
*dev
= &data
->client
->dev
;
2126 error
= __mxt_read_reg(data
->client
, data
->T7_address
,
2127 sizeof(data
->t7_cfg
), &data
->t7_cfg
);
2131 if (data
->t7_cfg
.active
== 0 || data
->t7_cfg
.idle
== 0) {
2133 dev_dbg(dev
, "T7 cfg zero, resetting\n");
2134 mxt_soft_reset(data
);
2138 dev_dbg(dev
, "T7 cfg zero after reset, overriding\n");
2139 data
->t7_cfg
.active
= 20;
2140 data
->t7_cfg
.idle
= 100;
2141 return mxt_set_t7_power_cfg(data
, MXT_POWER_CFG_RUN
);
2145 dev_dbg(dev
, "Initialized power cfg: ACTV %d, IDLE %d\n",
2146 data
->t7_cfg
.active
, data
->t7_cfg
.idle
);
2150 #ifdef CONFIG_TOUCHSCREEN_ATMEL_MXT_T37
2151 static u16
mxt_get_debug_value(struct mxt_data
*data
, unsigned int x
,
2154 struct mxt_info
*info
= &data
->info
;
2155 struct mxt_dbg
*dbg
= &data
->dbg
;
2156 unsigned int ofs
, page
;
2157 unsigned int col
= 0;
2158 unsigned int col_width
;
2160 if (info
->family_id
== MXT_FAMILY_1386
) {
2161 col_width
= info
->matrix_ysize
/ MXT1386_COLUMNS
;
2162 col
= y
/ col_width
;
2165 col_width
= info
->matrix_ysize
;
2168 ofs
= (y
+ (x
* col_width
)) * sizeof(u16
);
2169 page
= ofs
/ MXT_DIAGNOSTIC_SIZE
;
2170 ofs
%= MXT_DIAGNOSTIC_SIZE
;
2172 if (info
->family_id
== MXT_FAMILY_1386
)
2173 page
+= col
* MXT1386_PAGES_PER_COLUMN
;
2175 return get_unaligned_le16(&dbg
->t37_buf
[page
].data
[ofs
]);
2178 static int mxt_convert_debug_pages(struct mxt_data
*data
, u16
*outbuf
)
2180 struct mxt_dbg
*dbg
= &data
->dbg
;
2183 unsigned int i
, rx
, ry
;
2185 for (i
= 0; i
< dbg
->t37_nodes
; i
++) {
2186 /* Handle orientation */
2187 rx
= data
->xy_switch
? y
: x
;
2188 ry
= data
->xy_switch
? x
: y
;
2189 rx
= data
->invertx
? (data
->xsize
- 1 - rx
) : rx
;
2190 ry
= data
->inverty
? (data
->ysize
- 1 - ry
) : ry
;
2192 outbuf
[i
] = mxt_get_debug_value(data
, rx
, ry
);
2195 if (++x
>= (data
->xy_switch
? data
->ysize
: data
->xsize
)) {
2204 static int mxt_read_diagnostic_debug(struct mxt_data
*data
, u8 mode
,
2207 struct mxt_dbg
*dbg
= &data
->dbg
;
2212 struct t37_debug
*p
;
2215 for (page
= 0; page
< dbg
->t37_pages
; page
++) {
2216 p
= dbg
->t37_buf
+ page
;
2218 ret
= mxt_write_reg(data
->client
, dbg
->diag_cmd_address
,
2226 /* Read back command byte */
2227 ret
= __mxt_read_reg(data
->client
, dbg
->diag_cmd_address
,
2228 sizeof(cmd_poll
), &cmd_poll
);
2232 /* Field is cleared once the command has been processed */
2234 if (retries
++ > 100)
2242 ret
= __mxt_read_reg(data
->client
, dbg
->t37_address
,
2243 sizeof(struct t37_debug
), p
);
2247 if (p
->mode
!= mode
|| p
->page
!= page
) {
2248 dev_err(&data
->client
->dev
, "T37 page mismatch\n");
2252 dev_dbg(&data
->client
->dev
, "%s page:%d retries:%d\n",
2253 __func__
, page
, retries
);
2255 /* For remaining pages, write PAGEUP rather than mode */
2256 cmd
= MXT_DIAGNOSTIC_PAGEUP
;
2259 return mxt_convert_debug_pages(data
, outbuf
);
2262 static int mxt_queue_setup(struct vb2_queue
*q
,
2263 unsigned int *nbuffers
, unsigned int *nplanes
,
2264 unsigned int sizes
[], struct device
*alloc_devs
[])
2266 struct mxt_data
*data
= q
->drv_priv
;
2267 size_t size
= data
->dbg
.t37_nodes
* sizeof(u16
);
2270 return sizes
[0] < size
? -EINVAL
: 0;
2278 static void mxt_buffer_queue(struct vb2_buffer
*vb
)
2280 struct mxt_data
*data
= vb2_get_drv_priv(vb
->vb2_queue
);
2285 ptr
= vb2_plane_vaddr(vb
, 0);
2287 dev_err(&data
->client
->dev
, "Error acquiring frame ptr\n");
2291 switch (data
->dbg
.input
) {
2292 case MXT_V4L_INPUT_DELTAS
:
2294 mode
= MXT_DIAGNOSTIC_DELTAS
;
2297 case MXT_V4L_INPUT_REFS
:
2298 mode
= MXT_DIAGNOSTIC_REFS
;
2302 ret
= mxt_read_diagnostic_debug(data
, mode
, ptr
);
2306 vb2_set_plane_payload(vb
, 0, data
->dbg
.t37_nodes
* sizeof(u16
));
2307 vb2_buffer_done(vb
, VB2_BUF_STATE_DONE
);
2311 vb2_buffer_done(vb
, VB2_BUF_STATE_ERROR
);
2314 /* V4L2 structures */
2315 static const struct vb2_ops mxt_queue_ops
= {
2316 .queue_setup
= mxt_queue_setup
,
2317 .buf_queue
= mxt_buffer_queue
,
2318 .wait_prepare
= vb2_ops_wait_prepare
,
2319 .wait_finish
= vb2_ops_wait_finish
,
2322 static const struct vb2_queue mxt_queue
= {
2323 .type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
,
2324 .io_modes
= VB2_MMAP
| VB2_USERPTR
| VB2_DMABUF
| VB2_READ
,
2325 .buf_struct_size
= sizeof(struct mxt_vb2_buffer
),
2326 .ops
= &mxt_queue_ops
,
2327 .mem_ops
= &vb2_vmalloc_memops
,
2328 .timestamp_flags
= V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
,
2329 .min_buffers_needed
= 1,
2332 static int mxt_vidioc_querycap(struct file
*file
, void *priv
,
2333 struct v4l2_capability
*cap
)
2335 struct mxt_data
*data
= video_drvdata(file
);
2337 strlcpy(cap
->driver
, "atmel_mxt_ts", sizeof(cap
->driver
));
2338 strlcpy(cap
->card
, "atmel_mxt_ts touch", sizeof(cap
->card
));
2339 snprintf(cap
->bus_info
, sizeof(cap
->bus_info
),
2340 "I2C:%s", dev_name(&data
->client
->dev
));
2344 static int mxt_vidioc_enum_input(struct file
*file
, void *priv
,
2345 struct v4l2_input
*i
)
2347 if (i
->index
>= MXT_V4L_INPUT_MAX
)
2350 i
->type
= V4L2_INPUT_TYPE_TOUCH
;
2353 case MXT_V4L_INPUT_REFS
:
2354 strlcpy(i
->name
, "Mutual Capacitance References",
2357 case MXT_V4L_INPUT_DELTAS
:
2358 strlcpy(i
->name
, "Mutual Capacitance Deltas", sizeof(i
->name
));
2365 static int mxt_set_input(struct mxt_data
*data
, unsigned int i
)
2367 struct v4l2_pix_format
*f
= &data
->dbg
.format
;
2369 if (i
>= MXT_V4L_INPUT_MAX
)
2372 if (i
== MXT_V4L_INPUT_DELTAS
)
2373 f
->pixelformat
= V4L2_TCH_FMT_DELTA_TD16
;
2375 f
->pixelformat
= V4L2_TCH_FMT_TU16
;
2377 f
->width
= data
->xy_switch
? data
->ysize
: data
->xsize
;
2378 f
->height
= data
->xy_switch
? data
->xsize
: data
->ysize
;
2379 f
->field
= V4L2_FIELD_NONE
;
2380 f
->colorspace
= V4L2_COLORSPACE_RAW
;
2381 f
->bytesperline
= f
->width
* sizeof(u16
);
2382 f
->sizeimage
= f
->width
* f
->height
* sizeof(u16
);
2384 data
->dbg
.input
= i
;
2389 static int mxt_vidioc_s_input(struct file
*file
, void *priv
, unsigned int i
)
2391 return mxt_set_input(video_drvdata(file
), i
);
2394 static int mxt_vidioc_g_input(struct file
*file
, void *priv
, unsigned int *i
)
2396 struct mxt_data
*data
= video_drvdata(file
);
2398 *i
= data
->dbg
.input
;
2403 static int mxt_vidioc_fmt(struct file
*file
, void *priv
, struct v4l2_format
*f
)
2405 struct mxt_data
*data
= video_drvdata(file
);
2407 f
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
2408 f
->fmt
.pix
= data
->dbg
.format
;
2413 static int mxt_vidioc_enum_fmt(struct file
*file
, void *priv
,
2414 struct v4l2_fmtdesc
*fmt
)
2416 if (fmt
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
2419 switch (fmt
->index
) {
2421 fmt
->pixelformat
= V4L2_TCH_FMT_TU16
;
2425 fmt
->pixelformat
= V4L2_TCH_FMT_DELTA_TD16
;
2435 static int mxt_vidioc_g_parm(struct file
*file
, void *fh
,
2436 struct v4l2_streamparm
*a
)
2438 if (a
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
2441 a
->parm
.capture
.readbuffers
= 1;
2442 a
->parm
.capture
.timeperframe
.numerator
= 1;
2443 a
->parm
.capture
.timeperframe
.denominator
= 10;
2447 static const struct v4l2_ioctl_ops mxt_video_ioctl_ops
= {
2448 .vidioc_querycap
= mxt_vidioc_querycap
,
2450 .vidioc_enum_fmt_vid_cap
= mxt_vidioc_enum_fmt
,
2451 .vidioc_s_fmt_vid_cap
= mxt_vidioc_fmt
,
2452 .vidioc_g_fmt_vid_cap
= mxt_vidioc_fmt
,
2453 .vidioc_try_fmt_vid_cap
= mxt_vidioc_fmt
,
2454 .vidioc_g_parm
= mxt_vidioc_g_parm
,
2456 .vidioc_enum_input
= mxt_vidioc_enum_input
,
2457 .vidioc_g_input
= mxt_vidioc_g_input
,
2458 .vidioc_s_input
= mxt_vidioc_s_input
,
2460 .vidioc_reqbufs
= vb2_ioctl_reqbufs
,
2461 .vidioc_create_bufs
= vb2_ioctl_create_bufs
,
2462 .vidioc_querybuf
= vb2_ioctl_querybuf
,
2463 .vidioc_qbuf
= vb2_ioctl_qbuf
,
2464 .vidioc_dqbuf
= vb2_ioctl_dqbuf
,
2465 .vidioc_expbuf
= vb2_ioctl_expbuf
,
2467 .vidioc_streamon
= vb2_ioctl_streamon
,
2468 .vidioc_streamoff
= vb2_ioctl_streamoff
,
2471 static const struct video_device mxt_video_device
= {
2472 .name
= "Atmel maxTouch",
2473 .fops
= &mxt_video_fops
,
2474 .ioctl_ops
= &mxt_video_ioctl_ops
,
2475 .release
= video_device_release_empty
,
2476 .device_caps
= V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_TOUCH
|
2477 V4L2_CAP_READWRITE
| V4L2_CAP_STREAMING
,
2480 static void mxt_debug_init(struct mxt_data
*data
)
2482 struct mxt_info
*info
= &data
->info
;
2483 struct mxt_dbg
*dbg
= &data
->dbg
;
2484 struct mxt_object
*object
;
2487 object
= mxt_get_object(data
, MXT_GEN_COMMAND_T6
);
2491 dbg
->diag_cmd_address
= object
->start_address
+ MXT_COMMAND_DIAGNOSTIC
;
2493 object
= mxt_get_object(data
, MXT_DEBUG_DIAGNOSTIC_T37
);
2497 if (mxt_obj_size(object
) != sizeof(struct t37_debug
)) {
2498 dev_warn(&data
->client
->dev
, "Bad T37 size");
2502 dbg
->t37_address
= object
->start_address
;
2504 /* Calculate size of data and allocate buffer */
2505 dbg
->t37_nodes
= data
->xsize
* data
->ysize
;
2507 if (info
->family_id
== MXT_FAMILY_1386
)
2508 dbg
->t37_pages
= MXT1386_COLUMNS
* MXT1386_PAGES_PER_COLUMN
;
2510 dbg
->t37_pages
= DIV_ROUND_UP(data
->xsize
*
2511 data
->info
.matrix_ysize
*
2513 sizeof(dbg
->t37_buf
->data
));
2515 dbg
->t37_buf
= devm_kmalloc_array(&data
->client
->dev
, dbg
->t37_pages
,
2516 sizeof(struct t37_debug
), GFP_KERNEL
);
2520 /* init channel to zero */
2521 mxt_set_input(data
, 0);
2523 /* register video device */
2524 snprintf(dbg
->v4l2
.name
, sizeof(dbg
->v4l2
.name
), "%s", "atmel_mxt_ts");
2525 error
= v4l2_device_register(&data
->client
->dev
, &dbg
->v4l2
);
2529 /* initialize the queue */
2530 mutex_init(&dbg
->lock
);
2531 dbg
->queue
= mxt_queue
;
2532 dbg
->queue
.drv_priv
= data
;
2533 dbg
->queue
.lock
= &dbg
->lock
;
2534 dbg
->queue
.dev
= &data
->client
->dev
;
2536 error
= vb2_queue_init(&dbg
->queue
);
2538 goto error_unreg_v4l2
;
2540 dbg
->vdev
= mxt_video_device
;
2541 dbg
->vdev
.v4l2_dev
= &dbg
->v4l2
;
2542 dbg
->vdev
.lock
= &dbg
->lock
;
2543 dbg
->vdev
.vfl_dir
= VFL_DIR_RX
;
2544 dbg
->vdev
.queue
= &dbg
->queue
;
2545 video_set_drvdata(&dbg
->vdev
, data
);
2547 error
= video_register_device(&dbg
->vdev
, VFL_TYPE_TOUCH
, -1);
2549 goto error_unreg_v4l2
;
2554 v4l2_device_unregister(&dbg
->v4l2
);
2556 dev_warn(&data
->client
->dev
, "Error initializing T37\n");
2559 static void mxt_debug_init(struct mxt_data
*data
)
2564 static int mxt_configure_objects(struct mxt_data
*data
,
2565 const struct firmware
*cfg
)
2567 struct device
*dev
= &data
->client
->dev
;
2568 struct mxt_info
*info
= &data
->info
;
2571 error
= mxt_init_t7_power_cfg(data
);
2573 dev_err(dev
, "Failed to initialize power cfg\n");
2578 error
= mxt_update_cfg(data
, cfg
);
2580 dev_warn(dev
, "Error %d updating config\n", error
);
2583 if (data
->multitouch
) {
2584 error
= mxt_initialize_input_device(data
);
2588 dev_warn(dev
, "No touch object detected\n");
2591 mxt_debug_init(data
);
2594 "Family: %u Variant: %u Firmware V%u.%u.%02X Objects: %u\n",
2595 info
->family_id
, info
->variant_id
, info
->version
>> 4,
2596 info
->version
& 0xf, info
->build
, info
->object_num
);
2601 /* Firmware Version is returned as Major.Minor.Build */
2602 static ssize_t
mxt_fw_version_show(struct device
*dev
,
2603 struct device_attribute
*attr
, char *buf
)
2605 struct mxt_data
*data
= dev_get_drvdata(dev
);
2606 struct mxt_info
*info
= &data
->info
;
2607 return scnprintf(buf
, PAGE_SIZE
, "%u.%u.%02X\n",
2608 info
->version
>> 4, info
->version
& 0xf, info
->build
);
2611 /* Hardware Version is returned as FamilyID.VariantID */
2612 static ssize_t
mxt_hw_version_show(struct device
*dev
,
2613 struct device_attribute
*attr
, char *buf
)
2615 struct mxt_data
*data
= dev_get_drvdata(dev
);
2616 struct mxt_info
*info
= &data
->info
;
2617 return scnprintf(buf
, PAGE_SIZE
, "%u.%u\n",
2618 info
->family_id
, info
->variant_id
);
2621 static ssize_t
mxt_show_instance(char *buf
, int count
,
2622 struct mxt_object
*object
, int instance
,
2627 if (mxt_obj_instances(object
) > 1)
2628 count
+= scnprintf(buf
+ count
, PAGE_SIZE
- count
,
2629 "Instance %u\n", instance
);
2631 for (i
= 0; i
< mxt_obj_size(object
); i
++)
2632 count
+= scnprintf(buf
+ count
, PAGE_SIZE
- count
,
2633 "\t[%2u]: %02x (%d)\n", i
, val
[i
], val
[i
]);
2634 count
+= scnprintf(buf
+ count
, PAGE_SIZE
- count
, "\n");
2639 static ssize_t
mxt_object_show(struct device
*dev
,
2640 struct device_attribute
*attr
, char *buf
)
2642 struct mxt_data
*data
= dev_get_drvdata(dev
);
2643 struct mxt_object
*object
;
2649 /* Pre-allocate buffer large enough to hold max sized object. */
2650 obuf
= kmalloc(256, GFP_KERNEL
);
2655 for (i
= 0; i
< data
->info
.object_num
; i
++) {
2656 object
= data
->object_table
+ i
;
2658 if (!mxt_object_readable(object
->type
))
2661 count
+= scnprintf(buf
+ count
, PAGE_SIZE
- count
,
2662 "T%u:\n", object
->type
);
2664 for (j
= 0; j
< mxt_obj_instances(object
); j
++) {
2665 u16 size
= mxt_obj_size(object
);
2666 u16 addr
= object
->start_address
+ j
* size
;
2668 error
= __mxt_read_reg(data
->client
, addr
, size
, obuf
);
2672 count
= mxt_show_instance(buf
, count
, object
, j
, obuf
);
2678 return error
?: count
;
2681 static int mxt_check_firmware_format(struct device
*dev
,
2682 const struct firmware
*fw
)
2684 unsigned int pos
= 0;
2687 while (pos
< fw
->size
) {
2688 c
= *(fw
->data
+ pos
);
2690 if (c
< '0' || (c
> '9' && c
< 'A') || c
> 'F')
2697 * To convert file try:
2698 * xxd -r -p mXTXXX__APP_VX-X-XX.enc > maxtouch.fw
2700 dev_err(dev
, "Aborting: firmware file must be in binary format\n");
2705 static int mxt_load_fw(struct device
*dev
, const char *fn
)
2707 struct mxt_data
*data
= dev_get_drvdata(dev
);
2708 const struct firmware
*fw
= NULL
;
2709 unsigned int frame_size
;
2710 unsigned int pos
= 0;
2711 unsigned int retry
= 0;
2712 unsigned int frame
= 0;
2715 ret
= request_firmware(&fw
, fn
, dev
);
2717 dev_err(dev
, "Unable to open firmware %s\n", fn
);
2721 /* Check for incorrect enc file */
2722 ret
= mxt_check_firmware_format(dev
, fw
);
2724 goto release_firmware
;
2726 if (!data
->in_bootloader
) {
2727 /* Change to the bootloader mode */
2728 data
->in_bootloader
= true;
2730 ret
= mxt_t6_command(data
, MXT_COMMAND_RESET
,
2731 MXT_BOOT_VALUE
, false);
2733 goto release_firmware
;
2735 msleep(MXT_RESET_TIME
);
2737 /* Do not need to scan since we know family ID */
2738 ret
= mxt_lookup_bootloader_address(data
, 0);
2740 goto release_firmware
;
2742 mxt_free_input_device(data
);
2743 mxt_free_object_table(data
);
2745 enable_irq(data
->irq
);
2748 reinit_completion(&data
->bl_completion
);
2750 ret
= mxt_check_bootloader(data
, MXT_WAITING_BOOTLOAD_CMD
, false);
2752 /* Bootloader may still be unlocked from previous attempt */
2753 ret
= mxt_check_bootloader(data
, MXT_WAITING_FRAME_DATA
, false);
2757 dev_info(dev
, "Unlocking bootloader\n");
2759 /* Unlock bootloader */
2760 ret
= mxt_send_bootloader_cmd(data
, true);
2765 while (pos
< fw
->size
) {
2766 ret
= mxt_check_bootloader(data
, MXT_WAITING_FRAME_DATA
, true);
2770 frame_size
= ((*(fw
->data
+ pos
) << 8) | *(fw
->data
+ pos
+ 1));
2772 /* Take account of CRC bytes */
2775 /* Write one frame to device */
2776 ret
= mxt_bootloader_write(data
, fw
->data
+ pos
, frame_size
);
2780 ret
= mxt_check_bootloader(data
, MXT_FRAME_CRC_PASS
, true);
2784 /* Back off by 20ms per retry */
2788 dev_err(dev
, "Retry count exceeded\n");
2797 if (frame
% 50 == 0)
2798 dev_dbg(dev
, "Sent %d frames, %d/%zd bytes\n",
2799 frame
, pos
, fw
->size
);
2802 /* Wait for flash. */
2803 ret
= mxt_wait_for_completion(data
, &data
->bl_completion
,
2808 dev_dbg(dev
, "Sent %d frames, %d bytes\n", frame
, pos
);
2811 * Wait for device to reset. Some bootloader versions do not assert
2812 * the CHG line after bootloading has finished, so ignore potential
2815 mxt_wait_for_completion(data
, &data
->bl_completion
, MXT_FW_RESET_TIME
);
2817 data
->in_bootloader
= false;
2820 disable_irq(data
->irq
);
2822 release_firmware(fw
);
2826 static ssize_t
mxt_update_fw_store(struct device
*dev
,
2827 struct device_attribute
*attr
,
2828 const char *buf
, size_t count
)
2830 struct mxt_data
*data
= dev_get_drvdata(dev
);
2833 error
= mxt_load_fw(dev
, MXT_FW_NAME
);
2835 dev_err(dev
, "The firmware update failed(%d)\n", error
);
2838 dev_info(dev
, "The firmware update succeeded\n");
2840 error
= mxt_initialize(data
);
2848 static DEVICE_ATTR(fw_version
, S_IRUGO
, mxt_fw_version_show
, NULL
);
2849 static DEVICE_ATTR(hw_version
, S_IRUGO
, mxt_hw_version_show
, NULL
);
2850 static DEVICE_ATTR(object
, S_IRUGO
, mxt_object_show
, NULL
);
2851 static DEVICE_ATTR(update_fw
, S_IWUSR
, NULL
, mxt_update_fw_store
);
2853 static struct attribute
*mxt_attrs
[] = {
2854 &dev_attr_fw_version
.attr
,
2855 &dev_attr_hw_version
.attr
,
2856 &dev_attr_object
.attr
,
2857 &dev_attr_update_fw
.attr
,
2861 static const struct attribute_group mxt_attr_group
= {
2865 static void mxt_start(struct mxt_data
*data
)
2867 switch (data
->pdata
->suspend_mode
) {
2868 case MXT_SUSPEND_T9_CTRL
:
2869 mxt_soft_reset(data
);
2872 /* 0x83 = SCANEN | RPTEN | ENABLE */
2873 mxt_write_object(data
,
2874 MXT_TOUCH_MULTI_T9
, MXT_T9_CTRL
, 0x83);
2877 case MXT_SUSPEND_DEEP_SLEEP
:
2879 mxt_set_t7_power_cfg(data
, MXT_POWER_CFG_RUN
);
2881 /* Recalibrate since chip has been in deep sleep */
2882 mxt_t6_command(data
, MXT_COMMAND_CALIBRATE
, 1, false);
2888 static void mxt_stop(struct mxt_data
*data
)
2890 switch (data
->pdata
->suspend_mode
) {
2891 case MXT_SUSPEND_T9_CTRL
:
2893 mxt_write_object(data
,
2894 MXT_TOUCH_MULTI_T9
, MXT_T9_CTRL
, 0);
2897 case MXT_SUSPEND_DEEP_SLEEP
:
2899 mxt_set_t7_power_cfg(data
, MXT_POWER_CFG_DEEPSLEEP
);
2904 static int mxt_input_open(struct input_dev
*dev
)
2906 struct mxt_data
*data
= input_get_drvdata(dev
);
2913 static void mxt_input_close(struct input_dev
*dev
)
2915 struct mxt_data
*data
= input_get_drvdata(dev
);
2921 static const struct mxt_platform_data
*mxt_parse_dt(struct i2c_client
*client
)
2923 struct mxt_platform_data
*pdata
;
2924 struct device_node
*np
= client
->dev
.of_node
;
2929 return ERR_PTR(-ENOENT
);
2931 pdata
= devm_kzalloc(&client
->dev
, sizeof(*pdata
), GFP_KERNEL
);
2933 return ERR_PTR(-ENOMEM
);
2935 if (of_find_property(np
, "linux,gpio-keymap", &proplen
)) {
2936 pdata
->t19_num_keys
= proplen
/ sizeof(u32
);
2938 keymap
= devm_kzalloc(&client
->dev
,
2939 pdata
->t19_num_keys
* sizeof(keymap
[0]),
2942 return ERR_PTR(-ENOMEM
);
2944 ret
= of_property_read_u32_array(np
, "linux,gpio-keymap",
2945 keymap
, pdata
->t19_num_keys
);
2947 dev_warn(&client
->dev
,
2948 "Couldn't read linux,gpio-keymap: %d\n", ret
);
2950 pdata
->t19_keymap
= keymap
;
2953 pdata
->suspend_mode
= MXT_SUSPEND_DEEP_SLEEP
;
2958 static const struct mxt_platform_data
*mxt_parse_dt(struct i2c_client
*client
)
2960 return ERR_PTR(-ENOENT
);
2966 struct mxt_acpi_platform_data
{
2968 struct mxt_platform_data pdata
;
2971 static unsigned int samus_touchpad_buttons
[] = {
2978 static struct mxt_acpi_platform_data samus_platform_data
[] = {
2983 .t19_num_keys
= ARRAY_SIZE(samus_touchpad_buttons
),
2984 .t19_keymap
= samus_touchpad_buttons
,
2994 static unsigned int chromebook_tp_buttons
[] = {
3003 static struct mxt_acpi_platform_data chromebook_platform_data
[] = {
3008 .t19_num_keys
= ARRAY_SIZE(chromebook_tp_buttons
),
3009 .t19_keymap
= chromebook_tp_buttons
,
3019 static const struct dmi_system_id mxt_dmi_table
[] = {
3021 /* 2015 Google Pixel */
3022 .ident
= "Chromebook Pixel 2",
3024 DMI_MATCH(DMI_SYS_VENDOR
, "GOOGLE"),
3025 DMI_MATCH(DMI_PRODUCT_NAME
, "Samus"),
3027 .driver_data
= samus_platform_data
,
3030 /* Other Google Chromebooks */
3031 .ident
= "Chromebook",
3033 DMI_MATCH(DMI_SYS_VENDOR
, "GOOGLE"),
3035 .driver_data
= chromebook_platform_data
,
3040 static const struct mxt_platform_data
*mxt_parse_acpi(struct i2c_client
*client
)
3042 struct acpi_device
*adev
;
3043 const struct dmi_system_id
*system_id
;
3044 const struct mxt_acpi_platform_data
*acpi_pdata
;
3047 * Ignore ACPI devices representing bootloader mode.
3049 * This is a bit of a hack: Google Chromebook BIOS creates ACPI
3050 * devices for both application and bootloader modes, but we are
3051 * interested in application mode only (if device is in bootloader
3052 * mode we'll end up switching into application anyway). So far
3053 * application mode addresses were all above 0x40, so we'll use it
3056 if (client
->addr
< 0x40)
3057 return ERR_PTR(-ENXIO
);
3059 adev
= ACPI_COMPANION(&client
->dev
);
3061 return ERR_PTR(-ENOENT
);
3063 system_id
= dmi_first_match(mxt_dmi_table
);
3065 return ERR_PTR(-ENOENT
);
3067 acpi_pdata
= system_id
->driver_data
;
3069 return ERR_PTR(-ENOENT
);
3071 while (acpi_pdata
->hid
) {
3072 if (!strcmp(acpi_device_hid(adev
), acpi_pdata
->hid
))
3073 return &acpi_pdata
->pdata
;
3078 return ERR_PTR(-ENOENT
);
3081 static const struct mxt_platform_data
*mxt_parse_acpi(struct i2c_client
*client
)
3083 return ERR_PTR(-ENOENT
);
3087 static const struct mxt_platform_data
*
3088 mxt_get_platform_data(struct i2c_client
*client
)
3090 const struct mxt_platform_data
*pdata
;
3092 pdata
= dev_get_platdata(&client
->dev
);
3096 pdata
= mxt_parse_dt(client
);
3097 if (!IS_ERR(pdata
) || PTR_ERR(pdata
) != -ENOENT
)
3100 pdata
= mxt_parse_acpi(client
);
3101 if (!IS_ERR(pdata
) || PTR_ERR(pdata
) != -ENOENT
)
3104 dev_err(&client
->dev
, "No platform data specified\n");
3105 return ERR_PTR(-EINVAL
);
3108 static int mxt_probe(struct i2c_client
*client
, const struct i2c_device_id
*id
)
3110 struct mxt_data
*data
;
3111 const struct mxt_platform_data
*pdata
;
3114 pdata
= mxt_get_platform_data(client
);
3116 return PTR_ERR(pdata
);
3118 data
= kzalloc(sizeof(struct mxt_data
), GFP_KERNEL
);
3120 dev_err(&client
->dev
, "Failed to allocate memory\n");
3124 snprintf(data
->phys
, sizeof(data
->phys
), "i2c-%u-%04x/input0",
3125 client
->adapter
->nr
, client
->addr
);
3127 data
->client
= client
;
3128 data
->pdata
= pdata
;
3129 data
->irq
= client
->irq
;
3130 i2c_set_clientdata(client
, data
);
3132 init_completion(&data
->bl_completion
);
3133 init_completion(&data
->reset_completion
);
3134 init_completion(&data
->crc_completion
);
3136 error
= request_threaded_irq(client
->irq
, NULL
, mxt_interrupt
,
3137 pdata
->irqflags
| IRQF_ONESHOT
,
3138 client
->name
, data
);
3140 dev_err(&client
->dev
, "Failed to register interrupt\n");
3144 disable_irq(client
->irq
);
3146 error
= mxt_initialize(data
);
3150 error
= sysfs_create_group(&client
->dev
.kobj
, &mxt_attr_group
);
3152 dev_err(&client
->dev
, "Failure %d creating sysfs group\n",
3154 goto err_free_object
;
3160 mxt_free_input_device(data
);
3161 mxt_free_object_table(data
);
3163 free_irq(client
->irq
, data
);
3169 static int mxt_remove(struct i2c_client
*client
)
3171 struct mxt_data
*data
= i2c_get_clientdata(client
);
3173 sysfs_remove_group(&client
->dev
.kobj
, &mxt_attr_group
);
3174 free_irq(data
->irq
, data
);
3175 mxt_free_input_device(data
);
3176 mxt_free_object_table(data
);
3182 static int __maybe_unused
mxt_suspend(struct device
*dev
)
3184 struct i2c_client
*client
= to_i2c_client(dev
);
3185 struct mxt_data
*data
= i2c_get_clientdata(client
);
3186 struct input_dev
*input_dev
= data
->input_dev
;
3191 mutex_lock(&input_dev
->mutex
);
3193 if (input_dev
->users
)
3196 mutex_unlock(&input_dev
->mutex
);
3201 static int __maybe_unused
mxt_resume(struct device
*dev
)
3203 struct i2c_client
*client
= to_i2c_client(dev
);
3204 struct mxt_data
*data
= i2c_get_clientdata(client
);
3205 struct input_dev
*input_dev
= data
->input_dev
;
3210 mutex_lock(&input_dev
->mutex
);
3212 if (input_dev
->users
)
3215 mutex_unlock(&input_dev
->mutex
);
3220 static SIMPLE_DEV_PM_OPS(mxt_pm_ops
, mxt_suspend
, mxt_resume
);
3222 static const struct of_device_id mxt_of_match
[] = {
3223 { .compatible
= "atmel,maxtouch", },
3226 MODULE_DEVICE_TABLE(of
, mxt_of_match
);
3229 static const struct acpi_device_id mxt_acpi_id
[] = {
3230 { "ATML0000", 0 }, /* Touchpad */
3231 { "ATML0001", 0 }, /* Touchscreen */
3234 MODULE_DEVICE_TABLE(acpi
, mxt_acpi_id
);
3237 static const struct i2c_device_id mxt_id
[] = {
3238 { "qt602240_ts", 0 },
3239 { "atmel_mxt_ts", 0 },
3240 { "atmel_mxt_tp", 0 },
3245 MODULE_DEVICE_TABLE(i2c
, mxt_id
);
3247 static struct i2c_driver mxt_driver
= {
3249 .name
= "atmel_mxt_ts",
3250 .of_match_table
= of_match_ptr(mxt_of_match
),
3251 .acpi_match_table
= ACPI_PTR(mxt_acpi_id
),
3255 .remove
= mxt_remove
,
3259 module_i2c_driver(mxt_driver
);
3261 /* Module information */
3262 MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
3263 MODULE_DESCRIPTION("Atmel maXTouch Touchscreen driver");
3264 MODULE_LICENSE("GPL");