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.
7 * Copyright (C) 2016 Zodiac Inflight Innovations
9 * Author: Joonyoung Shim <jy0922.shim@samsung.com>
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
18 #include <linux/acpi.h>
19 #include <linux/dmi.h>
20 #include <linux/module.h>
21 #include <linux/init.h>
22 #include <linux/completion.h>
23 #include <linux/delay.h>
24 #include <linux/firmware.h>
25 #include <linux/i2c.h>
26 #include <linux/platform_data/atmel_mxt_ts.h>
27 #include <linux/input/mt.h>
28 #include <linux/interrupt.h>
30 #include <linux/slab.h>
31 #include <linux/gpio/consumer.h>
32 #include <asm/unaligned.h>
33 #include <media/v4l2-device.h>
34 #include <media/v4l2-ioctl.h>
35 #include <media/videobuf2-v4l2.h>
36 #include <media/videobuf2-vmalloc.h>
39 #define MXT_FW_NAME "maxtouch.fw"
40 #define MXT_CFG_NAME "maxtouch.cfg"
41 #define MXT_CFG_MAGIC "OBP_RAW V1"
44 #define MXT_OBJECT_START 0x07
45 #define MXT_OBJECT_SIZE 6
46 #define MXT_INFO_CHECKSUM_SIZE 3
47 #define MXT_MAX_BLOCK_WRITE 256
50 #define MXT_DEBUG_DIAGNOSTIC_T37 37
51 #define MXT_GEN_MESSAGE_T5 5
52 #define MXT_GEN_COMMAND_T6 6
53 #define MXT_GEN_POWER_T7 7
54 #define MXT_GEN_ACQUIRE_T8 8
55 #define MXT_GEN_DATASOURCE_T53 53
56 #define MXT_TOUCH_MULTI_T9 9
57 #define MXT_TOUCH_KEYARRAY_T15 15
58 #define MXT_TOUCH_PROXIMITY_T23 23
59 #define MXT_TOUCH_PROXKEY_T52 52
60 #define MXT_PROCI_GRIPFACE_T20 20
61 #define MXT_PROCG_NOISE_T22 22
62 #define MXT_PROCI_ONETOUCH_T24 24
63 #define MXT_PROCI_TWOTOUCH_T27 27
64 #define MXT_PROCI_GRIP_T40 40
65 #define MXT_PROCI_PALM_T41 41
66 #define MXT_PROCI_TOUCHSUPPRESSION_T42 42
67 #define MXT_PROCI_STYLUS_T47 47
68 #define MXT_PROCG_NOISESUPPRESSION_T48 48
69 #define MXT_SPT_COMMSCONFIG_T18 18
70 #define MXT_SPT_GPIOPWM_T19 19
71 #define MXT_SPT_SELFTEST_T25 25
72 #define MXT_SPT_CTECONFIG_T28 28
73 #define MXT_SPT_USERDATA_T38 38
74 #define MXT_SPT_DIGITIZER_T43 43
75 #define MXT_SPT_MESSAGECOUNT_T44 44
76 #define MXT_SPT_CTECONFIG_T46 46
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 (1 << 7)
91 #define MXT_T6_STATUS_OFL (1 << 6)
92 #define MXT_T6_STATUS_SIGERR (1 << 5)
93 #define MXT_T6_STATUS_CAL (1 << 4)
94 #define MXT_T6_STATUS_CFGERR (1 << 3)
95 #define MXT_T6_STATUS_COMSERR (1 << 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 (1 << 0)
115 #define MXT_T9_SUPPRESS (1 << 1)
116 #define MXT_T9_AMP (1 << 2)
117 #define MXT_T9_VECTOR (1 << 3)
118 #define MXT_T9_MOVE (1 << 4)
119 #define MXT_T9_RELEASE (1 << 5)
120 #define MXT_T9_PRESS (1 << 6)
121 #define MXT_T9_DETECT (1 << 7)
128 /* MXT_TOUCH_MULTI_T9 orient */
129 #define MXT_T9_ORIENT_SWITCH (1 << 0)
130 #define MXT_T9_ORIENT_INVERTX (1 << 1)
131 #define MXT_T9_ORIENT_INVERTY (1 << 2)
133 /* MXT_SPT_COMMSCONFIG_T18 */
134 #define MXT_COMMS_CTRL 0
135 #define MXT_COMMS_CMD 1
137 /* MXT_DEBUG_DIAGNOSTIC_T37 */
138 #define MXT_DIAGNOSTIC_PAGEUP 0x01
139 #define MXT_DIAGNOSTIC_DELTAS 0x10
140 #define MXT_DIAGNOSTIC_REFS 0x11
141 #define MXT_DIAGNOSTIC_SIZE 128
143 #define MXT_FAMILY_1386 160
144 #define MXT1386_COLUMNS 3
145 #define MXT1386_PAGES_PER_COLUMN 8
148 #ifdef CONFIG_TOUCHSCREEN_ATMEL_MXT_T37
151 u8 data
[MXT_DIAGNOSTIC_SIZE
];
155 /* Define for MXT_GEN_COMMAND_T6 */
156 #define MXT_BOOT_VALUE 0xa5
157 #define MXT_RESET_VALUE 0x01
158 #define MXT_BACKUP_VALUE 0x55
160 /* T100 Multiple Touch Touchscreen */
161 #define MXT_T100_CTRL 0
162 #define MXT_T100_CFG1 1
163 #define MXT_T100_TCHAUX 3
164 #define MXT_T100_XSIZE 9
165 #define MXT_T100_XRANGE 13
166 #define MXT_T100_YSIZE 20
167 #define MXT_T100_YRANGE 24
169 #define MXT_T100_CFG_SWITCHXY BIT(5)
170 #define MXT_T100_CFG_INVERTY BIT(6)
171 #define MXT_T100_CFG_INVERTX BIT(7)
173 #define MXT_T100_TCHAUX_VECT BIT(0)
174 #define MXT_T100_TCHAUX_AMPL BIT(1)
175 #define MXT_T100_TCHAUX_AREA BIT(2)
177 #define MXT_T100_DETECT BIT(7)
178 #define MXT_T100_TYPE_MASK 0x70
181 MXT_T100_TYPE_FINGER
= 1,
182 MXT_T100_TYPE_PASSIVE_STYLUS
= 2,
183 MXT_T100_TYPE_HOVERING_FINGER
= 4,
184 MXT_T100_TYPE_GLOVE
= 5,
185 MXT_T100_TYPE_LARGE_TOUCH
= 6,
188 #define MXT_DISTANCE_ACTIVE_TOUCH 0
189 #define MXT_DISTANCE_HOVERING 1
191 #define MXT_TOUCH_MAJOR_DEFAULT 1
192 #define MXT_PRESSURE_DEFAULT 1
195 #define MXT_BACKUP_TIME 50 /* msec */
196 #define MXT_RESET_TIME 200 /* msec */
197 #define MXT_RESET_TIMEOUT 3000 /* msec */
198 #define MXT_CRC_TIMEOUT 1000 /* msec */
199 #define MXT_FW_RESET_TIME 3000 /* msec */
200 #define MXT_FW_CHG_TIMEOUT 300 /* msec */
202 /* Command to unlock bootloader */
203 #define MXT_UNLOCK_CMD_MSB 0xaa
204 #define MXT_UNLOCK_CMD_LSB 0xdc
206 /* Bootloader mode status */
207 #define MXT_WAITING_BOOTLOAD_CMD 0xc0 /* valid 7 6 bit only */
208 #define MXT_WAITING_FRAME_DATA 0x80 /* valid 7 6 bit only */
209 #define MXT_FRAME_CRC_CHECK 0x02
210 #define MXT_FRAME_CRC_FAIL 0x03
211 #define MXT_FRAME_CRC_PASS 0x04
212 #define MXT_APP_CRC_FAIL 0x40 /* valid 7 8 bit only */
213 #define MXT_BOOT_STATUS_MASK 0x3f
214 #define MXT_BOOT_EXTENDED_ID (1 << 5)
215 #define MXT_BOOT_ID_MASK 0x1f
217 /* Touchscreen absolute values */
218 #define MXT_MAX_AREA 0xff
220 #define MXT_PIXELS_PER_MM 20
236 u8 instances_minus_one
;
242 u16 diag_cmd_address
;
243 struct t37_debug
*t37_buf
;
244 unsigned int t37_pages
;
245 unsigned int t37_nodes
;
247 struct v4l2_device v4l2
;
248 struct v4l2_pix_format format
;
249 struct video_device vdev
;
250 struct vb2_queue queue
;
255 enum v4l_dbg_inputs
{
256 MXT_V4L_INPUT_DELTAS
,
261 static const struct v4l2_file_operations mxt_video_fops
= {
262 .owner
= THIS_MODULE
,
263 .open
= v4l2_fh_open
,
264 .release
= vb2_fop_release
,
265 .unlocked_ioctl
= video_ioctl2
,
266 .read
= vb2_fop_read
,
267 .mmap
= vb2_fop_mmap
,
268 .poll
= vb2_fop_poll
,
271 /* Each client has this additional data */
273 struct i2c_client
*client
;
274 struct input_dev
*input_dev
;
275 char phys
[64]; /* device physical location */
276 const struct mxt_platform_data
*pdata
;
277 struct mxt_object
*object_table
;
278 struct mxt_info info
;
299 u8 last_message_count
;
302 struct t7_config t7_cfg
;
304 struct gpio_desc
*reset_gpio
;
306 /* Cached parameters from object table */
316 u8 T100_reportid_min
;
317 u8 T100_reportid_max
;
319 /* for fw update in bootloader */
320 struct completion bl_completion
;
322 /* for reset handling */
323 struct completion reset_completion
;
325 /* for config update handling */
326 struct completion crc_completion
;
329 struct mxt_vb2_buffer
{
330 struct vb2_buffer vb
;
331 struct list_head list
;
334 static size_t mxt_obj_size(const struct mxt_object
*obj
)
336 return obj
->size_minus_one
+ 1;
339 static size_t mxt_obj_instances(const struct mxt_object
*obj
)
341 return obj
->instances_minus_one
+ 1;
344 static bool mxt_object_readable(unsigned int type
)
347 case MXT_GEN_COMMAND_T6
:
348 case MXT_GEN_POWER_T7
:
349 case MXT_GEN_ACQUIRE_T8
:
350 case MXT_GEN_DATASOURCE_T53
:
351 case MXT_TOUCH_MULTI_T9
:
352 case MXT_TOUCH_KEYARRAY_T15
:
353 case MXT_TOUCH_PROXIMITY_T23
:
354 case MXT_TOUCH_PROXKEY_T52
:
355 case MXT_TOUCH_MULTITOUCHSCREEN_T100
:
356 case MXT_PROCI_GRIPFACE_T20
:
357 case MXT_PROCG_NOISE_T22
:
358 case MXT_PROCI_ONETOUCH_T24
:
359 case MXT_PROCI_TWOTOUCH_T27
:
360 case MXT_PROCI_GRIP_T40
:
361 case MXT_PROCI_PALM_T41
:
362 case MXT_PROCI_TOUCHSUPPRESSION_T42
:
363 case MXT_PROCI_STYLUS_T47
:
364 case MXT_PROCG_NOISESUPPRESSION_T48
:
365 case MXT_SPT_COMMSCONFIG_T18
:
366 case MXT_SPT_GPIOPWM_T19
:
367 case MXT_SPT_SELFTEST_T25
:
368 case MXT_SPT_CTECONFIG_T28
:
369 case MXT_SPT_USERDATA_T38
:
370 case MXT_SPT_DIGITIZER_T43
:
371 case MXT_SPT_CTECONFIG_T46
:
378 static void mxt_dump_message(struct mxt_data
*data
, u8
*message
)
380 dev_dbg(&data
->client
->dev
, "message: %*ph\n",
381 data
->T5_msg_size
, message
);
384 static int mxt_wait_for_completion(struct mxt_data
*data
,
385 struct completion
*comp
,
386 unsigned int timeout_ms
)
388 struct device
*dev
= &data
->client
->dev
;
389 unsigned long timeout
= msecs_to_jiffies(timeout_ms
);
392 ret
= wait_for_completion_interruptible_timeout(comp
, timeout
);
395 } else if (ret
== 0) {
396 dev_err(dev
, "Wait for completion timed out.\n");
402 static int mxt_bootloader_read(struct mxt_data
*data
,
403 u8
*val
, unsigned int count
)
408 msg
.addr
= data
->bootloader_addr
;
409 msg
.flags
= data
->client
->flags
& I2C_M_TEN
;
410 msg
.flags
|= I2C_M_RD
;
414 ret
= i2c_transfer(data
->client
->adapter
, &msg
, 1);
418 ret
= ret
< 0 ? ret
: -EIO
;
419 dev_err(&data
->client
->dev
, "%s: i2c recv failed (%d)\n",
426 static int mxt_bootloader_write(struct mxt_data
*data
,
427 const u8
* const val
, unsigned int count
)
432 msg
.addr
= data
->bootloader_addr
;
433 msg
.flags
= data
->client
->flags
& I2C_M_TEN
;
437 ret
= i2c_transfer(data
->client
->adapter
, &msg
, 1);
441 ret
= ret
< 0 ? ret
: -EIO
;
442 dev_err(&data
->client
->dev
, "%s: i2c send failed (%d)\n",
449 static int mxt_lookup_bootloader_address(struct mxt_data
*data
, bool retry
)
451 u8 appmode
= data
->client
->addr
;
457 /* Chips after 1664S use different scheme */
458 if (retry
|| data
->info
.family_id
>= 0xa2) {
459 bootloader
= appmode
- 0x24;
462 /* Fall through for normal case */
467 bootloader
= appmode
- 0x26;
471 dev_err(&data
->client
->dev
,
472 "Appmode i2c address 0x%02x not found\n",
477 data
->bootloader_addr
= bootloader
;
481 static int mxt_probe_bootloader(struct mxt_data
*data
, bool alt_address
)
483 struct device
*dev
= &data
->client
->dev
;
488 error
= mxt_lookup_bootloader_address(data
, alt_address
);
492 error
= mxt_bootloader_read(data
, &val
, 1);
496 /* Check app crc fail mode */
497 crc_failure
= (val
& ~MXT_BOOT_STATUS_MASK
) == MXT_APP_CRC_FAIL
;
499 dev_err(dev
, "Detected bootloader, status:%02X%s\n",
500 val
, crc_failure
? ", APP_CRC_FAIL" : "");
505 static u8
mxt_get_bootloader_version(struct mxt_data
*data
, u8 val
)
507 struct device
*dev
= &data
->client
->dev
;
510 if (val
& MXT_BOOT_EXTENDED_ID
) {
511 if (mxt_bootloader_read(data
, &buf
[0], 3) != 0) {
512 dev_err(dev
, "%s: i2c failure\n", __func__
);
516 dev_dbg(dev
, "Bootloader ID:%d Version:%d\n", buf
[1], buf
[2]);
520 dev_dbg(dev
, "Bootloader ID:%d\n", val
& MXT_BOOT_ID_MASK
);
526 static int mxt_check_bootloader(struct mxt_data
*data
, unsigned int state
,
529 struct device
*dev
= &data
->client
->dev
;
536 * In application update mode, the interrupt
537 * line signals state transitions. We must wait for the
538 * CHG assertion before reading the status byte.
539 * Once the status byte has been read, the line is deasserted.
541 ret
= mxt_wait_for_completion(data
, &data
->bl_completion
,
545 * TODO: handle -ERESTARTSYS better by terminating
546 * fw update process before returning to userspace
547 * by writing length 0x000 to device (iff we are in
548 * WAITING_FRAME_DATA state).
550 dev_err(dev
, "Update wait error %d\n", ret
);
555 ret
= mxt_bootloader_read(data
, &val
, 1);
559 if (state
== MXT_WAITING_BOOTLOAD_CMD
)
560 val
= mxt_get_bootloader_version(data
, val
);
563 case MXT_WAITING_BOOTLOAD_CMD
:
564 case MXT_WAITING_FRAME_DATA
:
565 case MXT_APP_CRC_FAIL
:
566 val
&= ~MXT_BOOT_STATUS_MASK
;
568 case MXT_FRAME_CRC_PASS
:
569 if (val
== MXT_FRAME_CRC_CHECK
) {
571 } else if (val
== MXT_FRAME_CRC_FAIL
) {
572 dev_err(dev
, "Bootloader CRC fail\n");
581 dev_err(dev
, "Invalid bootloader state %02X != %02X\n",
589 static int mxt_send_bootloader_cmd(struct mxt_data
*data
, bool unlock
)
595 buf
[0] = MXT_UNLOCK_CMD_LSB
;
596 buf
[1] = MXT_UNLOCK_CMD_MSB
;
602 ret
= mxt_bootloader_write(data
, buf
, 2);
609 static int __mxt_read_reg(struct i2c_client
*client
,
610 u16 reg
, u16 len
, void *val
)
612 struct i2c_msg xfer
[2];
617 buf
[1] = (reg
>> 8) & 0xff;
620 xfer
[0].addr
= client
->addr
;
626 xfer
[1].addr
= client
->addr
;
627 xfer
[1].flags
= I2C_M_RD
;
631 ret
= i2c_transfer(client
->adapter
, xfer
, 2);
637 dev_err(&client
->dev
, "%s: i2c transfer failed (%d)\n",
644 static int __mxt_write_reg(struct i2c_client
*client
, u16 reg
, u16 len
,
652 buf
= kmalloc(count
, GFP_KERNEL
);
657 buf
[1] = (reg
>> 8) & 0xff;
658 memcpy(&buf
[2], val
, len
);
660 ret
= i2c_master_send(client
, buf
, count
);
666 dev_err(&client
->dev
, "%s: i2c send failed (%d)\n",
674 static int mxt_write_reg(struct i2c_client
*client
, u16 reg
, u8 val
)
676 return __mxt_write_reg(client
, reg
, 1, &val
);
679 static struct mxt_object
*
680 mxt_get_object(struct mxt_data
*data
, u8 type
)
682 struct mxt_object
*object
;
685 for (i
= 0; i
< data
->info
.object_num
; i
++) {
686 object
= data
->object_table
+ i
;
687 if (object
->type
== type
)
691 dev_warn(&data
->client
->dev
, "Invalid object type T%u\n", type
);
695 static void mxt_proc_t6_messages(struct mxt_data
*data
, u8
*msg
)
697 struct device
*dev
= &data
->client
->dev
;
699 u32 crc
= msg
[2] | (msg
[3] << 8) | (msg
[4] << 16);
701 complete(&data
->crc_completion
);
703 if (crc
!= data
->config_crc
) {
704 data
->config_crc
= crc
;
705 dev_dbg(dev
, "T6 Config Checksum: 0x%06X\n", crc
);
709 if (status
& MXT_T6_STATUS_RESET
)
710 complete(&data
->reset_completion
);
712 /* Output debug if status has changed */
713 if (status
!= data
->t6_status
)
714 dev_dbg(dev
, "T6 Status 0x%02X%s%s%s%s%s%s%s\n",
716 status
== 0 ? " OK" : "",
717 status
& MXT_T6_STATUS_RESET
? " RESET" : "",
718 status
& MXT_T6_STATUS_OFL
? " OFL" : "",
719 status
& MXT_T6_STATUS_SIGERR
? " SIGERR" : "",
720 status
& MXT_T6_STATUS_CAL
? " CAL" : "",
721 status
& MXT_T6_STATUS_CFGERR
? " CFGERR" : "",
722 status
& MXT_T6_STATUS_COMSERR
? " COMSERR" : "");
724 /* Save current status */
725 data
->t6_status
= status
;
728 static int mxt_write_object(struct mxt_data
*data
,
729 u8 type
, u8 offset
, u8 val
)
731 struct mxt_object
*object
;
734 object
= mxt_get_object(data
, type
);
735 if (!object
|| offset
>= mxt_obj_size(object
))
738 reg
= object
->start_address
;
739 return mxt_write_reg(data
->client
, reg
+ offset
, val
);
742 static void mxt_input_button(struct mxt_data
*data
, u8
*message
)
744 struct input_dev
*input
= data
->input_dev
;
745 const struct mxt_platform_data
*pdata
= data
->pdata
;
748 for (i
= 0; i
< pdata
->t19_num_keys
; i
++) {
749 if (pdata
->t19_keymap
[i
] == KEY_RESERVED
)
752 /* Active-low switch */
753 input_report_key(input
, pdata
->t19_keymap
[i
],
754 !(message
[1] & BIT(i
)));
758 static void mxt_input_sync(struct mxt_data
*data
)
760 input_mt_report_pointer_emulation(data
->input_dev
,
761 data
->pdata
->t19_num_keys
);
762 input_sync(data
->input_dev
);
765 static void mxt_proc_t9_message(struct mxt_data
*data
, u8
*message
)
767 struct device
*dev
= &data
->client
->dev
;
768 struct input_dev
*input_dev
= data
->input_dev
;
776 id
= message
[0] - data
->T9_reportid_min
;
778 x
= (message
[2] << 4) | ((message
[4] >> 4) & 0xf);
779 y
= (message
[3] << 4) | ((message
[4] & 0xf));
781 /* Handle 10/12 bit switching */
782 if (data
->max_x
< 1024)
784 if (data
->max_y
< 1024)
788 amplitude
= message
[6];
791 "[%u] %c%c%c%c%c%c%c%c x: %5u y: %5u area: %3u amp: %3u\n",
793 (status
& MXT_T9_DETECT
) ? 'D' : '.',
794 (status
& MXT_T9_PRESS
) ? 'P' : '.',
795 (status
& MXT_T9_RELEASE
) ? 'R' : '.',
796 (status
& MXT_T9_MOVE
) ? 'M' : '.',
797 (status
& MXT_T9_VECTOR
) ? 'V' : '.',
798 (status
& MXT_T9_AMP
) ? 'A' : '.',
799 (status
& MXT_T9_SUPPRESS
) ? 'S' : '.',
800 (status
& MXT_T9_UNGRIP
) ? 'U' : '.',
801 x
, y
, area
, amplitude
);
803 input_mt_slot(input_dev
, id
);
805 if (status
& MXT_T9_DETECT
) {
807 * Multiple bits may be set if the host is slow to read
808 * the status messages, indicating all the events that
811 if (status
& MXT_T9_RELEASE
) {
812 input_mt_report_slot_state(input_dev
,
814 mxt_input_sync(data
);
818 input_mt_report_slot_state(input_dev
, MT_TOOL_FINGER
, 1);
819 input_report_abs(input_dev
, ABS_MT_POSITION_X
, x
);
820 input_report_abs(input_dev
, ABS_MT_POSITION_Y
, y
);
821 input_report_abs(input_dev
, ABS_MT_PRESSURE
, amplitude
);
822 input_report_abs(input_dev
, ABS_MT_TOUCH_MAJOR
, area
);
824 /* Touch no longer active, close out slot */
825 input_mt_report_slot_state(input_dev
, MT_TOOL_FINGER
, 0);
828 data
->update_input
= true;
831 static void mxt_proc_t100_message(struct mxt_data
*data
, u8
*message
)
833 struct device
*dev
= &data
->client
->dev
;
834 struct input_dev
*input_dev
= data
->input_dev
;
846 id
= message
[0] - data
->T100_reportid_min
- 2;
848 /* ignore SCRSTATUS events */
853 x
= get_unaligned_le16(&message
[2]);
854 y
= get_unaligned_le16(&message
[4]);
856 if (status
& MXT_T100_DETECT
) {
857 type
= (status
& MXT_T100_TYPE_MASK
) >> 4;
860 case MXT_T100_TYPE_HOVERING_FINGER
:
861 tool
= MT_TOOL_FINGER
;
862 distance
= MXT_DISTANCE_HOVERING
;
864 if (data
->t100_aux_vect
)
865 orientation
= message
[data
->t100_aux_vect
];
869 case MXT_T100_TYPE_FINGER
:
870 case MXT_T100_TYPE_GLOVE
:
871 tool
= MT_TOOL_FINGER
;
872 distance
= MXT_DISTANCE_ACTIVE_TOUCH
;
874 if (data
->t100_aux_area
)
875 major
= message
[data
->t100_aux_area
];
877 if (data
->t100_aux_ampl
)
878 pressure
= message
[data
->t100_aux_ampl
];
880 if (data
->t100_aux_vect
)
881 orientation
= message
[data
->t100_aux_vect
];
885 case MXT_T100_TYPE_PASSIVE_STYLUS
:
889 * Passive stylus is reported with size zero so
892 major
= MXT_TOUCH_MAJOR_DEFAULT
;
894 if (data
->t100_aux_ampl
)
895 pressure
= message
[data
->t100_aux_ampl
];
899 case MXT_T100_TYPE_LARGE_TOUCH
:
900 /* Ignore suppressed touch */
904 dev_dbg(dev
, "Unexpected T100 type\n");
910 * Values reported should be non-zero if tool is touching the
913 if (!pressure
&& type
!= MXT_T100_TYPE_HOVERING_FINGER
)
914 pressure
= MXT_PRESSURE_DEFAULT
;
916 input_mt_slot(input_dev
, id
);
918 if (status
& MXT_T100_DETECT
) {
919 dev_dbg(dev
, "[%u] type:%u x:%u y:%u a:%02X p:%02X v:%02X\n",
920 id
, type
, x
, y
, major
, pressure
, orientation
);
922 input_mt_report_slot_state(input_dev
, tool
, 1);
923 input_report_abs(input_dev
, ABS_MT_POSITION_X
, x
);
924 input_report_abs(input_dev
, ABS_MT_POSITION_Y
, y
);
925 input_report_abs(input_dev
, ABS_MT_TOUCH_MAJOR
, major
);
926 input_report_abs(input_dev
, ABS_MT_PRESSURE
, pressure
);
927 input_report_abs(input_dev
, ABS_MT_DISTANCE
, distance
);
928 input_report_abs(input_dev
, ABS_MT_ORIENTATION
, orientation
);
930 dev_dbg(dev
, "[%u] release\n", id
);
933 input_mt_report_slot_state(input_dev
, 0, 0);
936 data
->update_input
= true;
939 static int mxt_proc_message(struct mxt_data
*data
, u8
*message
)
941 u8 report_id
= message
[0];
943 if (report_id
== MXT_RPTID_NOMSG
)
946 if (report_id
== data
->T6_reportid
) {
947 mxt_proc_t6_messages(data
, message
);
948 } else if (!data
->input_dev
) {
950 * Do not report events if input device
951 * is not yet registered.
953 mxt_dump_message(data
, message
);
954 } else if (report_id
>= data
->T9_reportid_min
&&
955 report_id
<= data
->T9_reportid_max
) {
956 mxt_proc_t9_message(data
, message
);
957 } else if (report_id
>= data
->T100_reportid_min
&&
958 report_id
<= data
->T100_reportid_max
) {
959 mxt_proc_t100_message(data
, message
);
960 } else if (report_id
== data
->T19_reportid
) {
961 mxt_input_button(data
, message
);
962 data
->update_input
= true;
964 mxt_dump_message(data
, message
);
970 static int mxt_read_and_process_messages(struct mxt_data
*data
, u8 count
)
972 struct device
*dev
= &data
->client
->dev
;
977 /* Safety check for msg_buf */
978 if (count
> data
->max_reportid
)
981 /* Process remaining messages if necessary */
982 ret
= __mxt_read_reg(data
->client
, data
->T5_address
,
983 data
->T5_msg_size
* count
, data
->msg_buf
);
985 dev_err(dev
, "Failed to read %u messages (%d)\n", count
, ret
);
989 for (i
= 0; i
< count
; i
++) {
990 ret
= mxt_proc_message(data
,
991 data
->msg_buf
+ data
->T5_msg_size
* i
);
997 /* return number of messages read */
1001 static irqreturn_t
mxt_process_messages_t44(struct mxt_data
*data
)
1003 struct device
*dev
= &data
->client
->dev
;
1007 /* Read T44 and T5 together */
1008 ret
= __mxt_read_reg(data
->client
, data
->T44_address
,
1009 data
->T5_msg_size
+ 1, data
->msg_buf
);
1011 dev_err(dev
, "Failed to read T44 and T5 (%d)\n", ret
);
1015 count
= data
->msg_buf
[0];
1018 * This condition may be caused by the CHG line being configured in
1019 * Mode 0. It results in unnecessary I2C operations but it is benign.
1024 if (count
> data
->max_reportid
) {
1025 dev_warn(dev
, "T44 count %d exceeded max report id\n", count
);
1026 count
= data
->max_reportid
;
1029 /* Process first message */
1030 ret
= mxt_proc_message(data
, data
->msg_buf
+ 1);
1032 dev_warn(dev
, "Unexpected invalid message\n");
1036 num_left
= count
- 1;
1038 /* Process remaining messages if necessary */
1040 ret
= mxt_read_and_process_messages(data
, num_left
);
1043 else if (ret
!= num_left
)
1044 dev_warn(dev
, "Unexpected invalid message\n");
1048 if (data
->update_input
) {
1049 mxt_input_sync(data
);
1050 data
->update_input
= false;
1056 static int mxt_process_messages_until_invalid(struct mxt_data
*data
)
1058 struct device
*dev
= &data
->client
->dev
;
1062 count
= data
->max_reportid
;
1064 /* Read messages until we force an invalid */
1066 read
= mxt_read_and_process_messages(data
, count
);
1071 if (data
->update_input
) {
1072 mxt_input_sync(data
);
1073 data
->update_input
= false;
1076 dev_err(dev
, "CHG pin isn't cleared\n");
1080 static irqreturn_t
mxt_process_messages(struct mxt_data
*data
)
1082 int total_handled
, num_handled
;
1083 u8 count
= data
->last_message_count
;
1085 if (count
< 1 || count
> data
->max_reportid
)
1088 /* include final invalid message */
1089 total_handled
= mxt_read_and_process_messages(data
, count
+ 1);
1090 if (total_handled
< 0)
1092 /* if there were invalid messages, then we are done */
1093 else if (total_handled
<= count
)
1096 /* keep reading two msgs until one is invalid or reportid limit */
1098 num_handled
= mxt_read_and_process_messages(data
, 2);
1099 if (num_handled
< 0)
1102 total_handled
+= num_handled
;
1104 if (num_handled
< 2)
1106 } while (total_handled
< data
->num_touchids
);
1109 data
->last_message_count
= total_handled
;
1111 if (data
->update_input
) {
1112 mxt_input_sync(data
);
1113 data
->update_input
= false;
1119 static irqreturn_t
mxt_interrupt(int irq
, void *dev_id
)
1121 struct mxt_data
*data
= dev_id
;
1123 if (data
->in_bootloader
) {
1124 /* bootloader state transition completion */
1125 complete(&data
->bl_completion
);
1129 if (!data
->object_table
)
1132 if (data
->T44_address
) {
1133 return mxt_process_messages_t44(data
);
1135 return mxt_process_messages(data
);
1139 static int mxt_t6_command(struct mxt_data
*data
, u16 cmd_offset
,
1140 u8 value
, bool wait
)
1143 u8 command_register
;
1144 int timeout_counter
= 0;
1147 reg
= data
->T6_address
+ cmd_offset
;
1149 ret
= mxt_write_reg(data
->client
, reg
, value
);
1158 ret
= __mxt_read_reg(data
->client
, reg
, 1, &command_register
);
1161 } while (command_register
!= 0 && timeout_counter
++ <= 100);
1163 if (timeout_counter
> 100) {
1164 dev_err(&data
->client
->dev
, "Command failed!\n");
1171 static int mxt_acquire_irq(struct mxt_data
*data
)
1175 enable_irq(data
->irq
);
1177 error
= mxt_process_messages_until_invalid(data
);
1184 static int mxt_soft_reset(struct mxt_data
*data
)
1186 struct device
*dev
= &data
->client
->dev
;
1189 dev_info(dev
, "Resetting device\n");
1191 disable_irq(data
->irq
);
1193 reinit_completion(&data
->reset_completion
);
1195 ret
= mxt_t6_command(data
, MXT_COMMAND_RESET
, MXT_RESET_VALUE
, false);
1199 /* Ignore CHG line for 100ms after reset */
1202 mxt_acquire_irq(data
);
1204 ret
= mxt_wait_for_completion(data
, &data
->reset_completion
,
1212 static void mxt_update_crc(struct mxt_data
*data
, u8 cmd
, u8 value
)
1215 * On failure, CRC is set to 0 and config will always be
1218 data
->config_crc
= 0;
1219 reinit_completion(&data
->crc_completion
);
1221 mxt_t6_command(data
, cmd
, value
, true);
1224 * Wait for crc message. On failure, CRC is set to 0 and config will
1225 * always be downloaded.
1227 mxt_wait_for_completion(data
, &data
->crc_completion
, MXT_CRC_TIMEOUT
);
1230 static void mxt_calc_crc24(u32
*crc
, u8 firstbyte
, u8 secondbyte
)
1232 static const unsigned int crcpoly
= 0x80001B;
1236 data_word
= (secondbyte
<< 8) | firstbyte
;
1237 result
= ((*crc
<< 1) ^ data_word
);
1239 if (result
& 0x1000000)
1245 static u32
mxt_calculate_crc(u8
*base
, off_t start_off
, off_t end_off
)
1248 u8
*ptr
= base
+ start_off
;
1249 u8
*last_val
= base
+ end_off
- 1;
1251 if (end_off
< start_off
)
1254 while (ptr
< last_val
) {
1255 mxt_calc_crc24(&crc
, *ptr
, *(ptr
+ 1));
1259 /* if len is odd, fill the last byte with 0 */
1260 if (ptr
== last_val
)
1261 mxt_calc_crc24(&crc
, *ptr
, 0);
1263 /* Mask to 24-bit */
1269 static int mxt_prepare_cfg_mem(struct mxt_data
*data
,
1270 const struct firmware
*cfg
,
1271 unsigned int data_pos
,
1272 unsigned int cfg_start_ofs
,
1274 size_t config_mem_size
)
1276 struct device
*dev
= &data
->client
->dev
;
1277 struct mxt_object
*object
;
1278 unsigned int type
, instance
, size
, byte_offset
;
1285 while (data_pos
< cfg
->size
) {
1286 /* Read type, instance, length */
1287 ret
= sscanf(cfg
->data
+ data_pos
, "%x %x %x%n",
1288 &type
, &instance
, &size
, &offset
);
1292 } else if (ret
!= 3) {
1293 dev_err(dev
, "Bad format: failed to parse object\n");
1298 object
= mxt_get_object(data
, type
);
1301 for (i
= 0; i
< size
; i
++) {
1302 ret
= sscanf(cfg
->data
+ data_pos
, "%hhx%n",
1305 dev_err(dev
, "Bad format in T%d at %d\n",
1314 if (size
> mxt_obj_size(object
)) {
1316 * Either we are in fallback mode due to wrong
1317 * config or config from a later fw version,
1318 * or the file is corrupt or hand-edited.
1320 dev_warn(dev
, "Discarding %zu byte(s) in T%u\n",
1321 size
- mxt_obj_size(object
), type
);
1322 } else if (mxt_obj_size(object
) > size
) {
1324 * If firmware is upgraded, new bytes may be added to
1325 * end of objects. It is generally forward compatible
1326 * to zero these bytes - previous behaviour will be
1327 * retained. However this does invalidate the CRC and
1328 * will force fallback mode until the configuration is
1329 * updated. We warn here but do nothing else - the
1330 * malloc has zeroed the entire configuration.
1332 dev_warn(dev
, "Zeroing %zu byte(s) in T%d\n",
1333 mxt_obj_size(object
) - size
, type
);
1336 if (instance
>= mxt_obj_instances(object
)) {
1337 dev_err(dev
, "Object instances exceeded!\n");
1341 reg
= object
->start_address
+ mxt_obj_size(object
) * instance
;
1343 for (i
= 0; i
< size
; i
++) {
1344 ret
= sscanf(cfg
->data
+ data_pos
, "%hhx%n",
1348 dev_err(dev
, "Bad format in T%d at %d\n",
1354 if (i
> mxt_obj_size(object
))
1357 byte_offset
= reg
+ i
- cfg_start_ofs
;
1359 if (byte_offset
>= 0 && byte_offset
< config_mem_size
) {
1360 *(config_mem
+ byte_offset
) = val
;
1362 dev_err(dev
, "Bad object: reg:%d, T%d, ofs=%d\n",
1363 reg
, object
->type
, byte_offset
);
1372 static int mxt_upload_cfg_mem(struct mxt_data
*data
, unsigned int cfg_start
,
1373 u8
*config_mem
, size_t config_mem_size
)
1375 unsigned int byte_offset
= 0;
1378 /* Write configuration as blocks */
1379 while (byte_offset
< config_mem_size
) {
1380 unsigned int size
= config_mem_size
- byte_offset
;
1382 if (size
> MXT_MAX_BLOCK_WRITE
)
1383 size
= MXT_MAX_BLOCK_WRITE
;
1385 error
= __mxt_write_reg(data
->client
,
1386 cfg_start
+ byte_offset
,
1387 size
, config_mem
+ byte_offset
);
1389 dev_err(&data
->client
->dev
,
1390 "Config write error, ret=%d\n", error
);
1394 byte_offset
+= size
;
1400 static int mxt_init_t7_power_cfg(struct mxt_data
*data
);
1403 * mxt_update_cfg - download configuration to chip
1405 * Atmel Raw Config File Format
1407 * The first four lines of the raw config file contain:
1409 * 2) Chip ID Information (first 7 bytes of device memory)
1410 * 3) Chip Information Block 24-bit CRC Checksum
1411 * 4) Chip Configuration 24-bit CRC Checksum
1413 * The rest of the file consists of one line per object instance:
1414 * <TYPE> <INSTANCE> <SIZE> <CONTENTS>
1416 * <TYPE> - 2-byte object type as hex
1417 * <INSTANCE> - 2-byte object instance number as hex
1418 * <SIZE> - 2-byte object size as hex
1419 * <CONTENTS> - array of <SIZE> 1-byte hex values
1421 static int mxt_update_cfg(struct mxt_data
*data
, const struct firmware
*cfg
)
1423 struct device
*dev
= &data
->client
->dev
;
1424 struct mxt_info cfg_info
;
1430 u32 info_crc
, config_crc
, calculated_crc
;
1432 size_t config_mem_size
;
1434 mxt_update_crc(data
, MXT_COMMAND_REPORTALL
, 1);
1436 if (strncmp(cfg
->data
, MXT_CFG_MAGIC
, strlen(MXT_CFG_MAGIC
))) {
1437 dev_err(dev
, "Unrecognised config file\n");
1441 data_pos
= strlen(MXT_CFG_MAGIC
);
1443 /* Load information block and check */
1444 for (i
= 0; i
< sizeof(struct mxt_info
); i
++) {
1445 ret
= sscanf(cfg
->data
+ data_pos
, "%hhx%n",
1446 (unsigned char *)&cfg_info
+ i
,
1449 dev_err(dev
, "Bad format\n");
1456 if (cfg_info
.family_id
!= data
->info
.family_id
) {
1457 dev_err(dev
, "Family ID mismatch!\n");
1461 if (cfg_info
.variant_id
!= data
->info
.variant_id
) {
1462 dev_err(dev
, "Variant ID mismatch!\n");
1467 ret
= sscanf(cfg
->data
+ data_pos
, "%x%n", &info_crc
, &offset
);
1469 dev_err(dev
, "Bad format: failed to parse Info CRC\n");
1474 ret
= sscanf(cfg
->data
+ data_pos
, "%x%n", &config_crc
, &offset
);
1476 dev_err(dev
, "Bad format: failed to parse Config CRC\n");
1482 * The Info Block CRC is calculated over mxt_info and the object
1483 * table. If it does not match then we are trying to load the
1484 * configuration from a different chip or firmware version, so
1485 * the configuration CRC is invalid anyway.
1487 if (info_crc
== data
->info_crc
) {
1488 if (config_crc
== 0 || data
->config_crc
== 0) {
1489 dev_info(dev
, "CRC zero, attempting to apply config\n");
1490 } else if (config_crc
== data
->config_crc
) {
1491 dev_dbg(dev
, "Config CRC 0x%06X: OK\n",
1495 dev_info(dev
, "Config CRC 0x%06X: does not match file 0x%06X\n",
1496 data
->config_crc
, config_crc
);
1500 "Warning: Info CRC error - device=0x%06X file=0x%06X\n",
1501 data
->info_crc
, info_crc
);
1504 /* Malloc memory to store configuration */
1505 cfg_start_ofs
= MXT_OBJECT_START
+
1506 data
->info
.object_num
* sizeof(struct mxt_object
) +
1507 MXT_INFO_CHECKSUM_SIZE
;
1508 config_mem_size
= data
->mem_size
- cfg_start_ofs
;
1509 config_mem
= kzalloc(config_mem_size
, GFP_KERNEL
);
1511 dev_err(dev
, "Failed to allocate memory\n");
1515 ret
= mxt_prepare_cfg_mem(data
, cfg
, data_pos
, cfg_start_ofs
,
1516 config_mem
, config_mem_size
);
1520 /* Calculate crc of the received configs (not the raw config file) */
1521 if (data
->T7_address
< cfg_start_ofs
) {
1522 dev_err(dev
, "Bad T7 address, T7addr = %x, config offset %x\n",
1523 data
->T7_address
, cfg_start_ofs
);
1528 calculated_crc
= mxt_calculate_crc(config_mem
,
1529 data
->T7_address
- cfg_start_ofs
,
1532 if (config_crc
> 0 && config_crc
!= calculated_crc
)
1533 dev_warn(dev
, "Config CRC error, calculated=%06X, file=%06X\n",
1534 calculated_crc
, config_crc
);
1536 ret
= mxt_upload_cfg_mem(data
, cfg_start_ofs
,
1537 config_mem
, config_mem_size
);
1541 mxt_update_crc(data
, MXT_COMMAND_BACKUPNV
, MXT_BACKUP_VALUE
);
1543 ret
= mxt_soft_reset(data
);
1547 dev_info(dev
, "Config successfully updated\n");
1549 /* T7 config may have changed */
1550 mxt_init_t7_power_cfg(data
);
1557 static int mxt_get_info(struct mxt_data
*data
)
1559 struct i2c_client
*client
= data
->client
;
1560 struct mxt_info
*info
= &data
->info
;
1563 /* Read 7-byte info block starting at address 0 */
1564 error
= __mxt_read_reg(client
, 0, sizeof(*info
), info
);
1571 static void mxt_free_input_device(struct mxt_data
*data
)
1573 if (data
->input_dev
) {
1574 input_unregister_device(data
->input_dev
);
1575 data
->input_dev
= NULL
;
1579 static void mxt_free_object_table(struct mxt_data
*data
)
1581 #ifdef CONFIG_TOUCHSCREEN_ATMEL_MXT_T37
1582 video_unregister_device(&data
->dbg
.vdev
);
1583 v4l2_device_unregister(&data
->dbg
.v4l2
);
1586 kfree(data
->object_table
);
1587 data
->object_table
= NULL
;
1588 kfree(data
->msg_buf
);
1589 data
->msg_buf
= NULL
;
1590 data
->T5_address
= 0;
1591 data
->T5_msg_size
= 0;
1592 data
->T6_reportid
= 0;
1593 data
->T7_address
= 0;
1594 data
->T9_reportid_min
= 0;
1595 data
->T9_reportid_max
= 0;
1596 data
->T19_reportid
= 0;
1597 data
->T44_address
= 0;
1598 data
->T100_reportid_min
= 0;
1599 data
->T100_reportid_max
= 0;
1600 data
->max_reportid
= 0;
1603 static int mxt_get_object_table(struct mxt_data
*data
)
1605 struct i2c_client
*client
= data
->client
;
1607 struct mxt_object
*object_table
;
1613 table_size
= data
->info
.object_num
* sizeof(struct mxt_object
);
1614 object_table
= kzalloc(table_size
, GFP_KERNEL
);
1615 if (!object_table
) {
1616 dev_err(&data
->client
->dev
, "Failed to allocate memory\n");
1620 error
= __mxt_read_reg(client
, MXT_OBJECT_START
, table_size
,
1623 kfree(object_table
);
1627 /* Valid Report IDs start counting from 1 */
1630 for (i
= 0; i
< data
->info
.object_num
; i
++) {
1631 struct mxt_object
*object
= object_table
+ i
;
1634 le16_to_cpus(&object
->start_address
);
1636 if (object
->num_report_ids
) {
1638 reportid
+= object
->num_report_ids
*
1639 mxt_obj_instances(object
);
1640 max_id
= reportid
- 1;
1646 dev_dbg(&data
->client
->dev
,
1647 "T%u Start:%u Size:%zu Instances:%zu Report IDs:%u-%u\n",
1648 object
->type
, object
->start_address
,
1649 mxt_obj_size(object
), mxt_obj_instances(object
),
1652 switch (object
->type
) {
1653 case MXT_GEN_MESSAGE_T5
:
1654 if (data
->info
.family_id
== 0x80 &&
1655 data
->info
.version
< 0x20) {
1657 * On mXT224 firmware versions prior to V2.0
1658 * read and discard unused CRC byte otherwise
1659 * DMA reads are misaligned.
1661 data
->T5_msg_size
= mxt_obj_size(object
);
1663 /* CRC not enabled, so skip last byte */
1664 data
->T5_msg_size
= mxt_obj_size(object
) - 1;
1666 data
->T5_address
= object
->start_address
;
1668 case MXT_GEN_COMMAND_T6
:
1669 data
->T6_reportid
= min_id
;
1670 data
->T6_address
= object
->start_address
;
1672 case MXT_GEN_POWER_T7
:
1673 data
->T7_address
= object
->start_address
;
1675 case MXT_TOUCH_MULTI_T9
:
1676 data
->multitouch
= MXT_TOUCH_MULTI_T9
;
1677 data
->T9_reportid_min
= min_id
;
1678 data
->T9_reportid_max
= max_id
;
1679 data
->num_touchids
= object
->num_report_ids
1680 * mxt_obj_instances(object
);
1682 case MXT_SPT_MESSAGECOUNT_T44
:
1683 data
->T44_address
= object
->start_address
;
1685 case MXT_SPT_GPIOPWM_T19
:
1686 data
->T19_reportid
= min_id
;
1688 case MXT_TOUCH_MULTITOUCHSCREEN_T100
:
1689 data
->multitouch
= MXT_TOUCH_MULTITOUCHSCREEN_T100
;
1690 data
->T100_reportid_min
= min_id
;
1691 data
->T100_reportid_max
= max_id
;
1692 /* first two report IDs reserved */
1693 data
->num_touchids
= object
->num_report_ids
- 2;
1697 end_address
= object
->start_address
1698 + mxt_obj_size(object
) * mxt_obj_instances(object
) - 1;
1700 if (end_address
>= data
->mem_size
)
1701 data
->mem_size
= end_address
+ 1;
1704 /* Store maximum reportid */
1705 data
->max_reportid
= reportid
;
1707 /* If T44 exists, T5 position has to be directly after */
1708 if (data
->T44_address
&& (data
->T5_address
!= data
->T44_address
+ 1)) {
1709 dev_err(&client
->dev
, "Invalid T44 position\n");
1711 goto free_object_table
;
1714 data
->msg_buf
= kcalloc(data
->max_reportid
,
1715 data
->T5_msg_size
, GFP_KERNEL
);
1716 if (!data
->msg_buf
) {
1717 dev_err(&client
->dev
, "Failed to allocate message buffer\n");
1719 goto free_object_table
;
1722 data
->object_table
= object_table
;
1727 mxt_free_object_table(data
);
1731 static int mxt_read_t9_resolution(struct mxt_data
*data
)
1733 struct i2c_client
*client
= data
->client
;
1735 struct t9_range range
;
1736 unsigned char orient
;
1737 struct mxt_object
*object
;
1739 object
= mxt_get_object(data
, MXT_TOUCH_MULTI_T9
);
1743 error
= __mxt_read_reg(client
,
1744 object
->start_address
+ MXT_T9_XSIZE
,
1745 sizeof(data
->xsize
), &data
->xsize
);
1749 error
= __mxt_read_reg(client
,
1750 object
->start_address
+ MXT_T9_YSIZE
,
1751 sizeof(data
->ysize
), &data
->ysize
);
1755 error
= __mxt_read_reg(client
,
1756 object
->start_address
+ MXT_T9_RANGE
,
1757 sizeof(range
), &range
);
1761 data
->max_x
= get_unaligned_le16(&range
.x
);
1762 data
->max_y
= get_unaligned_le16(&range
.y
);
1764 error
= __mxt_read_reg(client
,
1765 object
->start_address
+ MXT_T9_ORIENT
,
1770 data
->xy_switch
= orient
& MXT_T9_ORIENT_SWITCH
;
1771 data
->invertx
= orient
& MXT_T9_ORIENT_INVERTX
;
1772 data
->inverty
= orient
& MXT_T9_ORIENT_INVERTY
;
1777 static int mxt_read_t100_config(struct mxt_data
*data
)
1779 struct i2c_client
*client
= data
->client
;
1781 struct mxt_object
*object
;
1782 u16 range_x
, range_y
;
1786 object
= mxt_get_object(data
, MXT_TOUCH_MULTITOUCHSCREEN_T100
);
1790 /* read touchscreen dimensions */
1791 error
= __mxt_read_reg(client
,
1792 object
->start_address
+ MXT_T100_XRANGE
,
1793 sizeof(range_x
), &range_x
);
1797 data
->max_x
= get_unaligned_le16(&range_x
);
1799 error
= __mxt_read_reg(client
,
1800 object
->start_address
+ MXT_T100_YRANGE
,
1801 sizeof(range_y
), &range_y
);
1805 data
->max_y
= get_unaligned_le16(&range_y
);
1807 error
= __mxt_read_reg(client
,
1808 object
->start_address
+ MXT_T100_XSIZE
,
1809 sizeof(data
->xsize
), &data
->xsize
);
1813 error
= __mxt_read_reg(client
,
1814 object
->start_address
+ MXT_T100_YSIZE
,
1815 sizeof(data
->ysize
), &data
->ysize
);
1819 /* read orientation config */
1820 error
= __mxt_read_reg(client
,
1821 object
->start_address
+ MXT_T100_CFG1
,
1826 data
->xy_switch
= cfg
& MXT_T100_CFG_SWITCHXY
;
1827 data
->invertx
= cfg
& MXT_T100_CFG_INVERTX
;
1828 data
->inverty
= cfg
& MXT_T100_CFG_INVERTY
;
1830 /* allocate aux bytes */
1831 error
= __mxt_read_reg(client
,
1832 object
->start_address
+ MXT_T100_TCHAUX
,
1839 if (tchaux
& MXT_T100_TCHAUX_VECT
)
1840 data
->t100_aux_vect
= aux
++;
1842 if (tchaux
& MXT_T100_TCHAUX_AMPL
)
1843 data
->t100_aux_ampl
= aux
++;
1845 if (tchaux
& MXT_T100_TCHAUX_AREA
)
1846 data
->t100_aux_area
= aux
++;
1848 dev_dbg(&client
->dev
,
1849 "T100 aux mappings vect:%u ampl:%u area:%u\n",
1850 data
->t100_aux_vect
, data
->t100_aux_ampl
, data
->t100_aux_area
);
1855 static int mxt_input_open(struct input_dev
*dev
);
1856 static void mxt_input_close(struct input_dev
*dev
);
1858 static void mxt_set_up_as_touchpad(struct input_dev
*input_dev
,
1859 struct mxt_data
*data
)
1861 const struct mxt_platform_data
*pdata
= data
->pdata
;
1864 input_dev
->name
= "Atmel maXTouch Touchpad";
1866 __set_bit(INPUT_PROP_BUTTONPAD
, input_dev
->propbit
);
1868 input_abs_set_res(input_dev
, ABS_X
, MXT_PIXELS_PER_MM
);
1869 input_abs_set_res(input_dev
, ABS_Y
, MXT_PIXELS_PER_MM
);
1870 input_abs_set_res(input_dev
, ABS_MT_POSITION_X
,
1872 input_abs_set_res(input_dev
, ABS_MT_POSITION_Y
,
1875 for (i
= 0; i
< pdata
->t19_num_keys
; i
++)
1876 if (pdata
->t19_keymap
[i
] != KEY_RESERVED
)
1877 input_set_capability(input_dev
, EV_KEY
,
1878 pdata
->t19_keymap
[i
]);
1881 static int mxt_initialize_input_device(struct mxt_data
*data
)
1883 const struct mxt_platform_data
*pdata
= data
->pdata
;
1884 struct device
*dev
= &data
->client
->dev
;
1885 struct input_dev
*input_dev
;
1887 unsigned int num_mt_slots
;
1888 unsigned int mt_flags
= 0;
1890 switch (data
->multitouch
) {
1891 case MXT_TOUCH_MULTI_T9
:
1892 num_mt_slots
= data
->T9_reportid_max
- data
->T9_reportid_min
+ 1;
1893 error
= mxt_read_t9_resolution(data
);
1895 dev_warn(dev
, "Failed to initialize T9 resolution\n");
1898 case MXT_TOUCH_MULTITOUCHSCREEN_T100
:
1899 num_mt_slots
= data
->num_touchids
;
1900 error
= mxt_read_t100_config(data
);
1902 dev_warn(dev
, "Failed to read T100 config\n");
1906 dev_err(dev
, "Invalid multitouch object\n");
1910 /* Handle default values and orientation switch */
1911 if (data
->max_x
== 0)
1914 if (data
->max_y
== 0)
1917 if (data
->xy_switch
)
1918 swap(data
->max_x
, data
->max_y
);
1920 dev_info(dev
, "Touchscreen size X%uY%u\n", data
->max_x
, data
->max_y
);
1922 /* Register input device */
1923 input_dev
= input_allocate_device();
1925 dev_err(dev
, "Failed to allocate memory\n");
1929 input_dev
->name
= "Atmel maXTouch Touchscreen";
1930 input_dev
->phys
= data
->phys
;
1931 input_dev
->id
.bustype
= BUS_I2C
;
1932 input_dev
->dev
.parent
= dev
;
1933 input_dev
->open
= mxt_input_open
;
1934 input_dev
->close
= mxt_input_close
;
1936 input_set_capability(input_dev
, EV_KEY
, BTN_TOUCH
);
1938 /* For single touch */
1939 input_set_abs_params(input_dev
, ABS_X
, 0, data
->max_x
, 0, 0);
1940 input_set_abs_params(input_dev
, ABS_Y
, 0, data
->max_y
, 0, 0);
1942 if (data
->multitouch
== MXT_TOUCH_MULTI_T9
||
1943 (data
->multitouch
== MXT_TOUCH_MULTITOUCHSCREEN_T100
&&
1944 data
->t100_aux_ampl
)) {
1945 input_set_abs_params(input_dev
, ABS_PRESSURE
, 0, 255, 0, 0);
1948 /* If device has buttons we assume it is a touchpad */
1949 if (pdata
->t19_num_keys
) {
1950 mxt_set_up_as_touchpad(input_dev
, data
);
1951 mt_flags
|= INPUT_MT_POINTER
;
1953 mt_flags
|= INPUT_MT_DIRECT
;
1956 /* For multi touch */
1957 error
= input_mt_init_slots(input_dev
, num_mt_slots
, mt_flags
);
1959 dev_err(dev
, "Error %d initialising slots\n", error
);
1963 if (data
->multitouch
== MXT_TOUCH_MULTITOUCHSCREEN_T100
) {
1964 input_set_abs_params(input_dev
, ABS_MT_TOOL_TYPE
,
1965 0, MT_TOOL_MAX
, 0, 0);
1966 input_set_abs_params(input_dev
, ABS_MT_DISTANCE
,
1967 MXT_DISTANCE_ACTIVE_TOUCH
,
1968 MXT_DISTANCE_HOVERING
,
1972 input_set_abs_params(input_dev
, ABS_MT_POSITION_X
,
1973 0, data
->max_x
, 0, 0);
1974 input_set_abs_params(input_dev
, ABS_MT_POSITION_Y
,
1975 0, data
->max_y
, 0, 0);
1977 if (data
->multitouch
== MXT_TOUCH_MULTI_T9
||
1978 (data
->multitouch
== MXT_TOUCH_MULTITOUCHSCREEN_T100
&&
1979 data
->t100_aux_area
)) {
1980 input_set_abs_params(input_dev
, ABS_MT_TOUCH_MAJOR
,
1981 0, MXT_MAX_AREA
, 0, 0);
1984 if (data
->multitouch
== MXT_TOUCH_MULTI_T9
||
1985 (data
->multitouch
== MXT_TOUCH_MULTITOUCHSCREEN_T100
&&
1986 data
->t100_aux_ampl
)) {
1987 input_set_abs_params(input_dev
, ABS_MT_PRESSURE
,
1991 if (data
->multitouch
== MXT_TOUCH_MULTITOUCHSCREEN_T100
&&
1992 data
->t100_aux_vect
) {
1993 input_set_abs_params(input_dev
, ABS_MT_ORIENTATION
,
1997 if (data
->multitouch
== MXT_TOUCH_MULTITOUCHSCREEN_T100
&&
1998 data
->t100_aux_ampl
) {
1999 input_set_abs_params(input_dev
, ABS_MT_PRESSURE
,
2003 if (data
->multitouch
== MXT_TOUCH_MULTITOUCHSCREEN_T100
&&
2004 data
->t100_aux_vect
) {
2005 input_set_abs_params(input_dev
, ABS_MT_ORIENTATION
,
2009 input_set_drvdata(input_dev
, data
);
2011 error
= input_register_device(input_dev
);
2013 dev_err(dev
, "Error %d registering input device\n", error
);
2017 data
->input_dev
= input_dev
;
2022 input_free_device(input_dev
);
2026 static int mxt_configure_objects(struct mxt_data
*data
,
2027 const struct firmware
*cfg
);
2029 static void mxt_config_cb(const struct firmware
*cfg
, void *ctx
)
2031 mxt_configure_objects(ctx
, cfg
);
2032 release_firmware(cfg
);
2035 static int mxt_initialize(struct mxt_data
*data
)
2037 struct i2c_client
*client
= data
->client
;
2038 int recovery_attempts
= 0;
2042 error
= mxt_get_info(data
);
2046 /* Check bootloader state */
2047 error
= mxt_probe_bootloader(data
, false);
2049 dev_info(&client
->dev
, "Trying alternate bootloader address\n");
2050 error
= mxt_probe_bootloader(data
, true);
2052 /* Chip is not in appmode or bootloader mode */
2057 /* OK, we are in bootloader, see if we can recover */
2058 if (++recovery_attempts
> 1) {
2059 dev_err(&client
->dev
, "Could not recover from bootloader mode\n");
2061 * We can reflash from this state, so do not
2062 * abort initialization.
2064 data
->in_bootloader
= true;
2068 /* Attempt to exit bootloader into app mode */
2069 mxt_send_bootloader_cmd(data
, false);
2070 msleep(MXT_FW_RESET_TIME
);
2073 /* Get object table information */
2074 error
= mxt_get_object_table(data
);
2076 dev_err(&client
->dev
, "Error %d reading object table\n", error
);
2080 error
= mxt_acquire_irq(data
);
2082 goto err_free_object_table
;
2084 error
= request_firmware_nowait(THIS_MODULE
, true, MXT_CFG_NAME
,
2085 &client
->dev
, GFP_KERNEL
, data
,
2088 dev_err(&client
->dev
, "Failed to invoke firmware loader: %d\n",
2090 goto err_free_object_table
;
2095 err_free_object_table
:
2096 mxt_free_object_table(data
);
2100 static int mxt_set_t7_power_cfg(struct mxt_data
*data
, u8 sleep
)
2102 struct device
*dev
= &data
->client
->dev
;
2104 struct t7_config
*new_config
;
2105 struct t7_config deepsleep
= { .active
= 0, .idle
= 0 };
2107 if (sleep
== MXT_POWER_CFG_DEEPSLEEP
)
2108 new_config
= &deepsleep
;
2110 new_config
= &data
->t7_cfg
;
2112 error
= __mxt_write_reg(data
->client
, data
->T7_address
,
2113 sizeof(data
->t7_cfg
), new_config
);
2117 dev_dbg(dev
, "Set T7 ACTV:%d IDLE:%d\n",
2118 new_config
->active
, new_config
->idle
);
2123 static int mxt_init_t7_power_cfg(struct mxt_data
*data
)
2125 struct device
*dev
= &data
->client
->dev
;
2130 error
= __mxt_read_reg(data
->client
, data
->T7_address
,
2131 sizeof(data
->t7_cfg
), &data
->t7_cfg
);
2135 if (data
->t7_cfg
.active
== 0 || data
->t7_cfg
.idle
== 0) {
2137 dev_dbg(dev
, "T7 cfg zero, resetting\n");
2138 mxt_soft_reset(data
);
2142 dev_dbg(dev
, "T7 cfg zero after reset, overriding\n");
2143 data
->t7_cfg
.active
= 20;
2144 data
->t7_cfg
.idle
= 100;
2145 return mxt_set_t7_power_cfg(data
, MXT_POWER_CFG_RUN
);
2149 dev_dbg(dev
, "Initialized power cfg: ACTV %d, IDLE %d\n",
2150 data
->t7_cfg
.active
, data
->t7_cfg
.idle
);
2154 #ifdef CONFIG_TOUCHSCREEN_ATMEL_MXT_T37
2155 static u16
mxt_get_debug_value(struct mxt_data
*data
, unsigned int x
,
2158 struct mxt_info
*info
= &data
->info
;
2159 struct mxt_dbg
*dbg
= &data
->dbg
;
2160 unsigned int ofs
, page
;
2161 unsigned int col
= 0;
2162 unsigned int col_width
;
2164 if (info
->family_id
== MXT_FAMILY_1386
) {
2165 col_width
= info
->matrix_ysize
/ MXT1386_COLUMNS
;
2166 col
= y
/ col_width
;
2169 col_width
= info
->matrix_ysize
;
2172 ofs
= (y
+ (x
* col_width
)) * sizeof(u16
);
2173 page
= ofs
/ MXT_DIAGNOSTIC_SIZE
;
2174 ofs
%= MXT_DIAGNOSTIC_SIZE
;
2176 if (info
->family_id
== MXT_FAMILY_1386
)
2177 page
+= col
* MXT1386_PAGES_PER_COLUMN
;
2179 return get_unaligned_le16(&dbg
->t37_buf
[page
].data
[ofs
]);
2182 static int mxt_convert_debug_pages(struct mxt_data
*data
, u16
*outbuf
)
2184 struct mxt_dbg
*dbg
= &data
->dbg
;
2187 unsigned int i
, rx
, ry
;
2189 for (i
= 0; i
< dbg
->t37_nodes
; i
++) {
2190 /* Handle orientation */
2191 rx
= data
->xy_switch
? y
: x
;
2192 ry
= data
->xy_switch
? x
: y
;
2193 rx
= data
->invertx
? (data
->xsize
- 1 - rx
) : rx
;
2194 ry
= data
->inverty
? (data
->ysize
- 1 - ry
) : ry
;
2196 outbuf
[i
] = mxt_get_debug_value(data
, rx
, ry
);
2199 if (++x
>= (data
->xy_switch
? data
->ysize
: data
->xsize
)) {
2208 static int mxt_read_diagnostic_debug(struct mxt_data
*data
, u8 mode
,
2211 struct mxt_dbg
*dbg
= &data
->dbg
;
2216 struct t37_debug
*p
;
2219 for (page
= 0; page
< dbg
->t37_pages
; page
++) {
2220 p
= dbg
->t37_buf
+ page
;
2222 ret
= mxt_write_reg(data
->client
, dbg
->diag_cmd_address
,
2230 /* Read back command byte */
2231 ret
= __mxt_read_reg(data
->client
, dbg
->diag_cmd_address
,
2232 sizeof(cmd_poll
), &cmd_poll
);
2236 /* Field is cleared once the command has been processed */
2238 if (retries
++ > 100)
2246 ret
= __mxt_read_reg(data
->client
, dbg
->t37_address
,
2247 sizeof(struct t37_debug
), p
);
2251 if (p
->mode
!= mode
|| p
->page
!= page
) {
2252 dev_err(&data
->client
->dev
, "T37 page mismatch\n");
2256 dev_dbg(&data
->client
->dev
, "%s page:%d retries:%d\n",
2257 __func__
, page
, retries
);
2259 /* For remaining pages, write PAGEUP rather than mode */
2260 cmd
= MXT_DIAGNOSTIC_PAGEUP
;
2263 return mxt_convert_debug_pages(data
, outbuf
);
2266 static int mxt_queue_setup(struct vb2_queue
*q
,
2267 unsigned int *nbuffers
, unsigned int *nplanes
,
2268 unsigned int sizes
[], struct device
*alloc_devs
[])
2270 struct mxt_data
*data
= q
->drv_priv
;
2271 size_t size
= data
->dbg
.t37_nodes
* sizeof(u16
);
2274 return sizes
[0] < size
? -EINVAL
: 0;
2282 static void mxt_buffer_queue(struct vb2_buffer
*vb
)
2284 struct mxt_data
*data
= vb2_get_drv_priv(vb
->vb2_queue
);
2289 ptr
= vb2_plane_vaddr(vb
, 0);
2291 dev_err(&data
->client
->dev
, "Error acquiring frame ptr\n");
2295 switch (data
->dbg
.input
) {
2296 case MXT_V4L_INPUT_DELTAS
:
2298 mode
= MXT_DIAGNOSTIC_DELTAS
;
2301 case MXT_V4L_INPUT_REFS
:
2302 mode
= MXT_DIAGNOSTIC_REFS
;
2306 ret
= mxt_read_diagnostic_debug(data
, mode
, ptr
);
2310 vb2_set_plane_payload(vb
, 0, data
->dbg
.t37_nodes
* sizeof(u16
));
2311 vb2_buffer_done(vb
, VB2_BUF_STATE_DONE
);
2315 vb2_buffer_done(vb
, VB2_BUF_STATE_ERROR
);
2318 /* V4L2 structures */
2319 static const struct vb2_ops mxt_queue_ops
= {
2320 .queue_setup
= mxt_queue_setup
,
2321 .buf_queue
= mxt_buffer_queue
,
2322 .wait_prepare
= vb2_ops_wait_prepare
,
2323 .wait_finish
= vb2_ops_wait_finish
,
2326 static const struct vb2_queue mxt_queue
= {
2327 .type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
,
2328 .io_modes
= VB2_MMAP
| VB2_USERPTR
| VB2_DMABUF
| VB2_READ
,
2329 .buf_struct_size
= sizeof(struct mxt_vb2_buffer
),
2330 .ops
= &mxt_queue_ops
,
2331 .mem_ops
= &vb2_vmalloc_memops
,
2332 .timestamp_flags
= V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
,
2333 .min_buffers_needed
= 1,
2336 static int mxt_vidioc_querycap(struct file
*file
, void *priv
,
2337 struct v4l2_capability
*cap
)
2339 struct mxt_data
*data
= video_drvdata(file
);
2341 strlcpy(cap
->driver
, "atmel_mxt_ts", sizeof(cap
->driver
));
2342 strlcpy(cap
->card
, "atmel_mxt_ts touch", sizeof(cap
->card
));
2343 snprintf(cap
->bus_info
, sizeof(cap
->bus_info
),
2344 "I2C:%s", dev_name(&data
->client
->dev
));
2348 static int mxt_vidioc_enum_input(struct file
*file
, void *priv
,
2349 struct v4l2_input
*i
)
2351 if (i
->index
>= MXT_V4L_INPUT_MAX
)
2354 i
->type
= V4L2_INPUT_TYPE_TOUCH
;
2357 case MXT_V4L_INPUT_REFS
:
2358 strlcpy(i
->name
, "Mutual Capacitance References",
2361 case MXT_V4L_INPUT_DELTAS
:
2362 strlcpy(i
->name
, "Mutual Capacitance Deltas", sizeof(i
->name
));
2369 static int mxt_set_input(struct mxt_data
*data
, unsigned int i
)
2371 struct v4l2_pix_format
*f
= &data
->dbg
.format
;
2373 if (i
>= MXT_V4L_INPUT_MAX
)
2376 if (i
== MXT_V4L_INPUT_DELTAS
)
2377 f
->pixelformat
= V4L2_TCH_FMT_DELTA_TD16
;
2379 f
->pixelformat
= V4L2_TCH_FMT_TU16
;
2381 f
->width
= data
->xy_switch
? data
->ysize
: data
->xsize
;
2382 f
->height
= data
->xy_switch
? data
->xsize
: data
->ysize
;
2383 f
->field
= V4L2_FIELD_NONE
;
2384 f
->colorspace
= V4L2_COLORSPACE_RAW
;
2385 f
->bytesperline
= f
->width
* sizeof(u16
);
2386 f
->sizeimage
= f
->width
* f
->height
* sizeof(u16
);
2388 data
->dbg
.input
= i
;
2393 static int mxt_vidioc_s_input(struct file
*file
, void *priv
, unsigned int i
)
2395 return mxt_set_input(video_drvdata(file
), i
);
2398 static int mxt_vidioc_g_input(struct file
*file
, void *priv
, unsigned int *i
)
2400 struct mxt_data
*data
= video_drvdata(file
);
2402 *i
= data
->dbg
.input
;
2407 static int mxt_vidioc_fmt(struct file
*file
, void *priv
, struct v4l2_format
*f
)
2409 struct mxt_data
*data
= video_drvdata(file
);
2411 f
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
2412 f
->fmt
.pix
= data
->dbg
.format
;
2417 static int mxt_vidioc_enum_fmt(struct file
*file
, void *priv
,
2418 struct v4l2_fmtdesc
*fmt
)
2420 if (fmt
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
2423 switch (fmt
->index
) {
2425 fmt
->pixelformat
= V4L2_TCH_FMT_TU16
;
2429 fmt
->pixelformat
= V4L2_TCH_FMT_DELTA_TD16
;
2439 static int mxt_vidioc_g_parm(struct file
*file
, void *fh
,
2440 struct v4l2_streamparm
*a
)
2442 if (a
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
2445 a
->parm
.capture
.readbuffers
= 1;
2446 a
->parm
.capture
.timeperframe
.numerator
= 1;
2447 a
->parm
.capture
.timeperframe
.denominator
= 10;
2451 static const struct v4l2_ioctl_ops mxt_video_ioctl_ops
= {
2452 .vidioc_querycap
= mxt_vidioc_querycap
,
2454 .vidioc_enum_fmt_vid_cap
= mxt_vidioc_enum_fmt
,
2455 .vidioc_s_fmt_vid_cap
= mxt_vidioc_fmt
,
2456 .vidioc_g_fmt_vid_cap
= mxt_vidioc_fmt
,
2457 .vidioc_try_fmt_vid_cap
= mxt_vidioc_fmt
,
2458 .vidioc_g_parm
= mxt_vidioc_g_parm
,
2460 .vidioc_enum_input
= mxt_vidioc_enum_input
,
2461 .vidioc_g_input
= mxt_vidioc_g_input
,
2462 .vidioc_s_input
= mxt_vidioc_s_input
,
2464 .vidioc_reqbufs
= vb2_ioctl_reqbufs
,
2465 .vidioc_create_bufs
= vb2_ioctl_create_bufs
,
2466 .vidioc_querybuf
= vb2_ioctl_querybuf
,
2467 .vidioc_qbuf
= vb2_ioctl_qbuf
,
2468 .vidioc_dqbuf
= vb2_ioctl_dqbuf
,
2469 .vidioc_expbuf
= vb2_ioctl_expbuf
,
2471 .vidioc_streamon
= vb2_ioctl_streamon
,
2472 .vidioc_streamoff
= vb2_ioctl_streamoff
,
2475 static const struct video_device mxt_video_device
= {
2476 .name
= "Atmel maxTouch",
2477 .fops
= &mxt_video_fops
,
2478 .ioctl_ops
= &mxt_video_ioctl_ops
,
2479 .release
= video_device_release_empty
,
2480 .device_caps
= V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_TOUCH
|
2481 V4L2_CAP_READWRITE
| V4L2_CAP_STREAMING
,
2484 static void mxt_debug_init(struct mxt_data
*data
)
2486 struct mxt_info
*info
= &data
->info
;
2487 struct mxt_dbg
*dbg
= &data
->dbg
;
2488 struct mxt_object
*object
;
2491 object
= mxt_get_object(data
, MXT_GEN_COMMAND_T6
);
2495 dbg
->diag_cmd_address
= object
->start_address
+ MXT_COMMAND_DIAGNOSTIC
;
2497 object
= mxt_get_object(data
, MXT_DEBUG_DIAGNOSTIC_T37
);
2501 if (mxt_obj_size(object
) != sizeof(struct t37_debug
)) {
2502 dev_warn(&data
->client
->dev
, "Bad T37 size");
2506 dbg
->t37_address
= object
->start_address
;
2508 /* Calculate size of data and allocate buffer */
2509 dbg
->t37_nodes
= data
->xsize
* data
->ysize
;
2511 if (info
->family_id
== MXT_FAMILY_1386
)
2512 dbg
->t37_pages
= MXT1386_COLUMNS
* MXT1386_PAGES_PER_COLUMN
;
2514 dbg
->t37_pages
= DIV_ROUND_UP(data
->xsize
*
2515 info
->matrix_ysize
*
2517 sizeof(dbg
->t37_buf
->data
));
2519 dbg
->t37_buf
= devm_kmalloc_array(&data
->client
->dev
, dbg
->t37_pages
,
2520 sizeof(struct t37_debug
), GFP_KERNEL
);
2524 /* init channel to zero */
2525 mxt_set_input(data
, 0);
2527 /* register video device */
2528 snprintf(dbg
->v4l2
.name
, sizeof(dbg
->v4l2
.name
), "%s", "atmel_mxt_ts");
2529 error
= v4l2_device_register(&data
->client
->dev
, &dbg
->v4l2
);
2533 /* initialize the queue */
2534 mutex_init(&dbg
->lock
);
2535 dbg
->queue
= mxt_queue
;
2536 dbg
->queue
.drv_priv
= data
;
2537 dbg
->queue
.lock
= &dbg
->lock
;
2538 dbg
->queue
.dev
= &data
->client
->dev
;
2540 error
= vb2_queue_init(&dbg
->queue
);
2542 goto error_unreg_v4l2
;
2544 dbg
->vdev
= mxt_video_device
;
2545 dbg
->vdev
.v4l2_dev
= &dbg
->v4l2
;
2546 dbg
->vdev
.lock
= &dbg
->lock
;
2547 dbg
->vdev
.vfl_dir
= VFL_DIR_RX
;
2548 dbg
->vdev
.queue
= &dbg
->queue
;
2549 video_set_drvdata(&dbg
->vdev
, data
);
2551 error
= video_register_device(&dbg
->vdev
, VFL_TYPE_TOUCH
, -1);
2553 goto error_unreg_v4l2
;
2558 v4l2_device_unregister(&dbg
->v4l2
);
2560 dev_warn(&data
->client
->dev
, "Error initializing T37\n");
2563 static void mxt_debug_init(struct mxt_data
*data
)
2568 static int mxt_configure_objects(struct mxt_data
*data
,
2569 const struct firmware
*cfg
)
2571 struct device
*dev
= &data
->client
->dev
;
2572 struct mxt_info
*info
= &data
->info
;
2575 error
= mxt_init_t7_power_cfg(data
);
2577 dev_err(dev
, "Failed to initialize power cfg\n");
2582 error
= mxt_update_cfg(data
, cfg
);
2584 dev_warn(dev
, "Error %d updating config\n", error
);
2587 if (data
->multitouch
) {
2588 error
= mxt_initialize_input_device(data
);
2592 dev_warn(dev
, "No touch object detected\n");
2595 mxt_debug_init(data
);
2598 "Family: %u Variant: %u Firmware V%u.%u.%02X Objects: %u\n",
2599 info
->family_id
, info
->variant_id
, info
->version
>> 4,
2600 info
->version
& 0xf, info
->build
, info
->object_num
);
2605 /* Firmware Version is returned as Major.Minor.Build */
2606 static ssize_t
mxt_fw_version_show(struct device
*dev
,
2607 struct device_attribute
*attr
, char *buf
)
2609 struct mxt_data
*data
= dev_get_drvdata(dev
);
2610 struct mxt_info
*info
= &data
->info
;
2611 return scnprintf(buf
, PAGE_SIZE
, "%u.%u.%02X\n",
2612 info
->version
>> 4, info
->version
& 0xf, info
->build
);
2615 /* Hardware Version is returned as FamilyID.VariantID */
2616 static ssize_t
mxt_hw_version_show(struct device
*dev
,
2617 struct device_attribute
*attr
, char *buf
)
2619 struct mxt_data
*data
= dev_get_drvdata(dev
);
2620 struct mxt_info
*info
= &data
->info
;
2621 return scnprintf(buf
, PAGE_SIZE
, "%u.%u\n",
2622 info
->family_id
, info
->variant_id
);
2625 static ssize_t
mxt_show_instance(char *buf
, int count
,
2626 struct mxt_object
*object
, int instance
,
2631 if (mxt_obj_instances(object
) > 1)
2632 count
+= scnprintf(buf
+ count
, PAGE_SIZE
- count
,
2633 "Instance %u\n", instance
);
2635 for (i
= 0; i
< mxt_obj_size(object
); i
++)
2636 count
+= scnprintf(buf
+ count
, PAGE_SIZE
- count
,
2637 "\t[%2u]: %02x (%d)\n", i
, val
[i
], val
[i
]);
2638 count
+= scnprintf(buf
+ count
, PAGE_SIZE
- count
, "\n");
2643 static ssize_t
mxt_object_show(struct device
*dev
,
2644 struct device_attribute
*attr
, char *buf
)
2646 struct mxt_data
*data
= dev_get_drvdata(dev
);
2647 struct mxt_object
*object
;
2653 /* Pre-allocate buffer large enough to hold max sized object. */
2654 obuf
= kmalloc(256, GFP_KERNEL
);
2659 for (i
= 0; i
< data
->info
.object_num
; i
++) {
2660 object
= data
->object_table
+ i
;
2662 if (!mxt_object_readable(object
->type
))
2665 count
+= scnprintf(buf
+ count
, PAGE_SIZE
- count
,
2666 "T%u:\n", object
->type
);
2668 for (j
= 0; j
< mxt_obj_instances(object
); j
++) {
2669 u16 size
= mxt_obj_size(object
);
2670 u16 addr
= object
->start_address
+ j
* size
;
2672 error
= __mxt_read_reg(data
->client
, addr
, size
, obuf
);
2676 count
= mxt_show_instance(buf
, count
, object
, j
, obuf
);
2682 return error
?: count
;
2685 static int mxt_check_firmware_format(struct device
*dev
,
2686 const struct firmware
*fw
)
2688 unsigned int pos
= 0;
2691 while (pos
< fw
->size
) {
2692 c
= *(fw
->data
+ pos
);
2694 if (c
< '0' || (c
> '9' && c
< 'A') || c
> 'F')
2701 * To convert file try:
2702 * xxd -r -p mXTXXX__APP_VX-X-XX.enc > maxtouch.fw
2704 dev_err(dev
, "Aborting: firmware file must be in binary format\n");
2709 static int mxt_load_fw(struct device
*dev
, const char *fn
)
2711 struct mxt_data
*data
= dev_get_drvdata(dev
);
2712 const struct firmware
*fw
= NULL
;
2713 unsigned int frame_size
;
2714 unsigned int pos
= 0;
2715 unsigned int retry
= 0;
2716 unsigned int frame
= 0;
2719 ret
= request_firmware(&fw
, fn
, dev
);
2721 dev_err(dev
, "Unable to open firmware %s\n", fn
);
2725 /* Check for incorrect enc file */
2726 ret
= mxt_check_firmware_format(dev
, fw
);
2728 goto release_firmware
;
2730 if (!data
->in_bootloader
) {
2731 /* Change to the bootloader mode */
2732 data
->in_bootloader
= true;
2734 ret
= mxt_t6_command(data
, MXT_COMMAND_RESET
,
2735 MXT_BOOT_VALUE
, false);
2737 goto release_firmware
;
2739 msleep(MXT_RESET_TIME
);
2741 /* Do not need to scan since we know family ID */
2742 ret
= mxt_lookup_bootloader_address(data
, 0);
2744 goto release_firmware
;
2746 mxt_free_input_device(data
);
2747 mxt_free_object_table(data
);
2749 enable_irq(data
->irq
);
2752 reinit_completion(&data
->bl_completion
);
2754 ret
= mxt_check_bootloader(data
, MXT_WAITING_BOOTLOAD_CMD
, false);
2756 /* Bootloader may still be unlocked from previous attempt */
2757 ret
= mxt_check_bootloader(data
, MXT_WAITING_FRAME_DATA
, false);
2761 dev_info(dev
, "Unlocking bootloader\n");
2763 /* Unlock bootloader */
2764 ret
= mxt_send_bootloader_cmd(data
, true);
2769 while (pos
< fw
->size
) {
2770 ret
= mxt_check_bootloader(data
, MXT_WAITING_FRAME_DATA
, true);
2774 frame_size
= ((*(fw
->data
+ pos
) << 8) | *(fw
->data
+ pos
+ 1));
2776 /* Take account of CRC bytes */
2779 /* Write one frame to device */
2780 ret
= mxt_bootloader_write(data
, fw
->data
+ pos
, frame_size
);
2784 ret
= mxt_check_bootloader(data
, MXT_FRAME_CRC_PASS
, true);
2788 /* Back off by 20ms per retry */
2792 dev_err(dev
, "Retry count exceeded\n");
2801 if (frame
% 50 == 0)
2802 dev_dbg(dev
, "Sent %d frames, %d/%zd bytes\n",
2803 frame
, pos
, fw
->size
);
2806 /* Wait for flash. */
2807 ret
= mxt_wait_for_completion(data
, &data
->bl_completion
,
2812 dev_dbg(dev
, "Sent %d frames, %d bytes\n", frame
, pos
);
2815 * Wait for device to reset. Some bootloader versions do not assert
2816 * the CHG line after bootloading has finished, so ignore potential
2819 mxt_wait_for_completion(data
, &data
->bl_completion
, MXT_FW_RESET_TIME
);
2821 data
->in_bootloader
= false;
2824 disable_irq(data
->irq
);
2826 release_firmware(fw
);
2830 static ssize_t
mxt_update_fw_store(struct device
*dev
,
2831 struct device_attribute
*attr
,
2832 const char *buf
, size_t count
)
2834 struct mxt_data
*data
= dev_get_drvdata(dev
);
2837 error
= mxt_load_fw(dev
, MXT_FW_NAME
);
2839 dev_err(dev
, "The firmware update failed(%d)\n", error
);
2842 dev_info(dev
, "The firmware update succeeded\n");
2844 error
= mxt_initialize(data
);
2852 static DEVICE_ATTR(fw_version
, S_IRUGO
, mxt_fw_version_show
, NULL
);
2853 static DEVICE_ATTR(hw_version
, S_IRUGO
, mxt_hw_version_show
, NULL
);
2854 static DEVICE_ATTR(object
, S_IRUGO
, mxt_object_show
, NULL
);
2855 static DEVICE_ATTR(update_fw
, S_IWUSR
, NULL
, mxt_update_fw_store
);
2857 static struct attribute
*mxt_attrs
[] = {
2858 &dev_attr_fw_version
.attr
,
2859 &dev_attr_hw_version
.attr
,
2860 &dev_attr_object
.attr
,
2861 &dev_attr_update_fw
.attr
,
2865 static const struct attribute_group mxt_attr_group
= {
2869 static void mxt_start(struct mxt_data
*data
)
2871 switch (data
->pdata
->suspend_mode
) {
2872 case MXT_SUSPEND_T9_CTRL
:
2873 mxt_soft_reset(data
);
2876 /* 0x83 = SCANEN | RPTEN | ENABLE */
2877 mxt_write_object(data
,
2878 MXT_TOUCH_MULTI_T9
, MXT_T9_CTRL
, 0x83);
2881 case MXT_SUSPEND_DEEP_SLEEP
:
2883 mxt_set_t7_power_cfg(data
, MXT_POWER_CFG_RUN
);
2885 /* Recalibrate since chip has been in deep sleep */
2886 mxt_t6_command(data
, MXT_COMMAND_CALIBRATE
, 1, false);
2892 static void mxt_stop(struct mxt_data
*data
)
2894 switch (data
->pdata
->suspend_mode
) {
2895 case MXT_SUSPEND_T9_CTRL
:
2897 mxt_write_object(data
,
2898 MXT_TOUCH_MULTI_T9
, MXT_T9_CTRL
, 0);
2901 case MXT_SUSPEND_DEEP_SLEEP
:
2903 mxt_set_t7_power_cfg(data
, MXT_POWER_CFG_DEEPSLEEP
);
2908 static int mxt_input_open(struct input_dev
*dev
)
2910 struct mxt_data
*data
= input_get_drvdata(dev
);
2917 static void mxt_input_close(struct input_dev
*dev
)
2919 struct mxt_data
*data
= input_get_drvdata(dev
);
2925 static const struct mxt_platform_data
*mxt_parse_dt(struct i2c_client
*client
)
2927 struct mxt_platform_data
*pdata
;
2928 struct device_node
*np
= client
->dev
.of_node
;
2933 return ERR_PTR(-ENOENT
);
2935 pdata
= devm_kzalloc(&client
->dev
, sizeof(*pdata
), GFP_KERNEL
);
2937 return ERR_PTR(-ENOMEM
);
2939 if (of_find_property(np
, "linux,gpio-keymap", &proplen
)) {
2940 pdata
->t19_num_keys
= proplen
/ sizeof(u32
);
2942 keymap
= devm_kzalloc(&client
->dev
,
2943 pdata
->t19_num_keys
* sizeof(keymap
[0]),
2946 return ERR_PTR(-ENOMEM
);
2948 ret
= of_property_read_u32_array(np
, "linux,gpio-keymap",
2949 keymap
, pdata
->t19_num_keys
);
2951 dev_warn(&client
->dev
,
2952 "Couldn't read linux,gpio-keymap: %d\n", ret
);
2954 pdata
->t19_keymap
= keymap
;
2957 pdata
->suspend_mode
= MXT_SUSPEND_DEEP_SLEEP
;
2962 static const struct mxt_platform_data
*mxt_parse_dt(struct i2c_client
*client
)
2964 return ERR_PTR(-ENOENT
);
2970 struct mxt_acpi_platform_data
{
2972 struct mxt_platform_data pdata
;
2975 static unsigned int samus_touchpad_buttons
[] = {
2982 static struct mxt_acpi_platform_data samus_platform_data
[] = {
2987 .t19_num_keys
= ARRAY_SIZE(samus_touchpad_buttons
),
2988 .t19_keymap
= samus_touchpad_buttons
,
2998 static unsigned int chromebook_tp_buttons
[] = {
3007 static struct mxt_acpi_platform_data chromebook_platform_data
[] = {
3012 .t19_num_keys
= ARRAY_SIZE(chromebook_tp_buttons
),
3013 .t19_keymap
= chromebook_tp_buttons
,
3023 static const struct dmi_system_id mxt_dmi_table
[] = {
3025 /* 2015 Google Pixel */
3026 .ident
= "Chromebook Pixel 2",
3028 DMI_MATCH(DMI_SYS_VENDOR
, "GOOGLE"),
3029 DMI_MATCH(DMI_PRODUCT_NAME
, "Samus"),
3031 .driver_data
= samus_platform_data
,
3034 /* Other Google Chromebooks */
3035 .ident
= "Chromebook",
3037 DMI_MATCH(DMI_SYS_VENDOR
, "GOOGLE"),
3039 .driver_data
= chromebook_platform_data
,
3044 static const struct mxt_platform_data
*mxt_parse_acpi(struct i2c_client
*client
)
3046 struct acpi_device
*adev
;
3047 const struct dmi_system_id
*system_id
;
3048 const struct mxt_acpi_platform_data
*acpi_pdata
;
3051 * Ignore ACPI devices representing bootloader mode.
3053 * This is a bit of a hack: Google Chromebook BIOS creates ACPI
3054 * devices for both application and bootloader modes, but we are
3055 * interested in application mode only (if device is in bootloader
3056 * mode we'll end up switching into application anyway). So far
3057 * application mode addresses were all above 0x40, so we'll use it
3060 if (client
->addr
< 0x40)
3061 return ERR_PTR(-ENXIO
);
3063 adev
= ACPI_COMPANION(&client
->dev
);
3065 return ERR_PTR(-ENOENT
);
3067 system_id
= dmi_first_match(mxt_dmi_table
);
3069 return ERR_PTR(-ENOENT
);
3071 acpi_pdata
= system_id
->driver_data
;
3073 return ERR_PTR(-ENOENT
);
3075 while (acpi_pdata
->hid
) {
3076 if (!strcmp(acpi_device_hid(adev
), acpi_pdata
->hid
))
3077 return &acpi_pdata
->pdata
;
3082 return ERR_PTR(-ENOENT
);
3085 static const struct mxt_platform_data
*mxt_parse_acpi(struct i2c_client
*client
)
3087 return ERR_PTR(-ENOENT
);
3091 static const struct mxt_platform_data
*
3092 mxt_get_platform_data(struct i2c_client
*client
)
3094 const struct mxt_platform_data
*pdata
;
3096 pdata
= dev_get_platdata(&client
->dev
);
3100 pdata
= mxt_parse_dt(client
);
3101 if (!IS_ERR(pdata
) || PTR_ERR(pdata
) != -ENOENT
)
3104 pdata
= mxt_parse_acpi(client
);
3105 if (!IS_ERR(pdata
) || PTR_ERR(pdata
) != -ENOENT
)
3108 dev_err(&client
->dev
, "No platform data specified\n");
3109 return ERR_PTR(-EINVAL
);
3112 static int mxt_probe(struct i2c_client
*client
, const struct i2c_device_id
*id
)
3114 struct mxt_data
*data
;
3115 const struct mxt_platform_data
*pdata
;
3118 pdata
= mxt_get_platform_data(client
);
3120 return PTR_ERR(pdata
);
3122 data
= devm_kzalloc(&client
->dev
, sizeof(struct mxt_data
), GFP_KERNEL
);
3126 snprintf(data
->phys
, sizeof(data
->phys
), "i2c-%u-%04x/input0",
3127 client
->adapter
->nr
, client
->addr
);
3129 data
->client
= client
;
3130 data
->pdata
= pdata
;
3131 data
->irq
= client
->irq
;
3132 i2c_set_clientdata(client
, data
);
3134 init_completion(&data
->bl_completion
);
3135 init_completion(&data
->reset_completion
);
3136 init_completion(&data
->crc_completion
);
3138 data
->reset_gpio
= devm_gpiod_get_optional(&client
->dev
,
3139 "reset", GPIOD_OUT_LOW
);
3140 if (IS_ERR(data
->reset_gpio
)) {
3141 error
= PTR_ERR(data
->reset_gpio
);
3142 dev_err(&client
->dev
, "Failed to get reset gpio: %d\n", error
);
3146 error
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
3147 NULL
, mxt_interrupt
,
3148 pdata
->irqflags
| IRQF_ONESHOT
,
3149 client
->name
, data
);
3151 dev_err(&client
->dev
, "Failed to register interrupt\n");
3155 if (data
->reset_gpio
) {
3156 data
->in_bootloader
= true;
3157 msleep(MXT_RESET_TIME
);
3158 reinit_completion(&data
->bl_completion
);
3159 gpiod_set_value(data
->reset_gpio
, 1);
3160 error
= mxt_wait_for_completion(data
, &data
->bl_completion
,
3164 data
->in_bootloader
= false;
3167 disable_irq(client
->irq
);
3169 error
= mxt_initialize(data
);
3173 error
= sysfs_create_group(&client
->dev
.kobj
, &mxt_attr_group
);
3175 dev_err(&client
->dev
, "Failure %d creating sysfs group\n",
3177 goto err_free_object
;
3183 mxt_free_input_device(data
);
3184 mxt_free_object_table(data
);
3188 static int mxt_remove(struct i2c_client
*client
)
3190 struct mxt_data
*data
= i2c_get_clientdata(client
);
3192 disable_irq(data
->irq
);
3193 sysfs_remove_group(&client
->dev
.kobj
, &mxt_attr_group
);
3194 mxt_free_input_device(data
);
3195 mxt_free_object_table(data
);
3200 static int __maybe_unused
mxt_suspend(struct device
*dev
)
3202 struct i2c_client
*client
= to_i2c_client(dev
);
3203 struct mxt_data
*data
= i2c_get_clientdata(client
);
3204 struct input_dev
*input_dev
= data
->input_dev
;
3209 mutex_lock(&input_dev
->mutex
);
3211 if (input_dev
->users
)
3214 mutex_unlock(&input_dev
->mutex
);
3219 static int __maybe_unused
mxt_resume(struct device
*dev
)
3221 struct i2c_client
*client
= to_i2c_client(dev
);
3222 struct mxt_data
*data
= i2c_get_clientdata(client
);
3223 struct input_dev
*input_dev
= data
->input_dev
;
3228 mutex_lock(&input_dev
->mutex
);
3230 if (input_dev
->users
)
3233 mutex_unlock(&input_dev
->mutex
);
3238 static SIMPLE_DEV_PM_OPS(mxt_pm_ops
, mxt_suspend
, mxt_resume
);
3240 static const struct of_device_id mxt_of_match
[] = {
3241 { .compatible
= "atmel,maxtouch", },
3244 MODULE_DEVICE_TABLE(of
, mxt_of_match
);
3247 static const struct acpi_device_id mxt_acpi_id
[] = {
3248 { "ATML0000", 0 }, /* Touchpad */
3249 { "ATML0001", 0 }, /* Touchscreen */
3252 MODULE_DEVICE_TABLE(acpi
, mxt_acpi_id
);
3255 static const struct i2c_device_id mxt_id
[] = {
3256 { "qt602240_ts", 0 },
3257 { "atmel_mxt_ts", 0 },
3258 { "atmel_mxt_tp", 0 },
3263 MODULE_DEVICE_TABLE(i2c
, mxt_id
);
3265 static struct i2c_driver mxt_driver
= {
3267 .name
= "atmel_mxt_ts",
3268 .of_match_table
= of_match_ptr(mxt_of_match
),
3269 .acpi_match_table
= ACPI_PTR(mxt_acpi_id
),
3273 .remove
= mxt_remove
,
3277 module_i2c_driver(mxt_driver
);
3279 /* Module information */
3280 MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
3281 MODULE_DESCRIPTION("Atmel maXTouch Touchscreen driver");
3282 MODULE_LICENSE("GPL");