1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Atmel maXTouch Touchscreen driver
5 * Copyright (C) 2010 Samsung Electronics Co.Ltd
6 * Copyright (C) 2011-2014 Atmel Corporation
7 * Copyright (C) 2012 Google, Inc.
8 * Copyright (C) 2016 Zodiac Inflight Innovations
10 * Author: Joonyoung Shim <jy0922.shim@samsung.com>
13 #include <linux/acpi.h>
14 #include <linux/dmi.h>
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/completion.h>
18 #include <linux/delay.h>
19 #include <linux/firmware.h>
20 #include <linux/i2c.h>
21 #include <linux/input/mt.h>
22 #include <linux/interrupt.h>
23 #include <linux/irq.h>
25 #include <linux/property.h>
26 #include <linux/slab.h>
27 #include <linux/regulator/consumer.h>
28 #include <linux/gpio/consumer.h>
29 #include <asm/unaligned.h>
30 #include <media/v4l2-device.h>
31 #include <media/v4l2-ioctl.h>
32 #include <media/videobuf2-v4l2.h>
33 #include <media/videobuf2-vmalloc.h>
36 #define MXT_FW_NAME "maxtouch.fw"
37 #define MXT_CFG_NAME "maxtouch.cfg"
38 #define MXT_CFG_MAGIC "OBP_RAW V1"
41 #define MXT_OBJECT_START 0x07
42 #define MXT_OBJECT_SIZE 6
43 #define MXT_INFO_CHECKSUM_SIZE 3
44 #define MXT_MAX_BLOCK_WRITE 256
47 #define MXT_DEBUG_DIAGNOSTIC_T37 37
48 #define MXT_GEN_MESSAGE_T5 5
49 #define MXT_GEN_COMMAND_T6 6
50 #define MXT_GEN_POWER_T7 7
51 #define MXT_GEN_ACQUIRE_T8 8
52 #define MXT_GEN_DATASOURCE_T53 53
53 #define MXT_TOUCH_MULTI_T9 9
54 #define MXT_TOUCH_KEYARRAY_T15 15
55 #define MXT_TOUCH_PROXIMITY_T23 23
56 #define MXT_TOUCH_PROXKEY_T52 52
57 #define MXT_PROCI_GRIPFACE_T20 20
58 #define MXT_PROCG_NOISE_T22 22
59 #define MXT_PROCI_ONETOUCH_T24 24
60 #define MXT_PROCI_TWOTOUCH_T27 27
61 #define MXT_PROCI_GRIP_T40 40
62 #define MXT_PROCI_PALM_T41 41
63 #define MXT_PROCI_TOUCHSUPPRESSION_T42 42
64 #define MXT_PROCI_STYLUS_T47 47
65 #define MXT_PROCG_NOISESUPPRESSION_T48 48
66 #define MXT_SPT_COMMSCONFIG_T18 18
67 #define MXT_SPT_GPIOPWM_T19 19
68 #define MXT_SPT_SELFTEST_T25 25
69 #define MXT_SPT_CTECONFIG_T28 28
70 #define MXT_SPT_USERDATA_T38 38
71 #define MXT_SPT_DIGITIZER_T43 43
72 #define MXT_SPT_MESSAGECOUNT_T44 44
73 #define MXT_SPT_CTECONFIG_T46 46
74 #define MXT_SPT_DYNAMICCONFIGURATIONCONTAINER_T71 71
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 BIT(7)
89 #define MXT_T6_STATUS_OFL BIT(6)
90 #define MXT_T6_STATUS_SIGERR BIT(5)
91 #define MXT_T6_STATUS_CAL BIT(4)
92 #define MXT_T6_STATUS_CFGERR BIT(3)
93 #define MXT_T6_STATUS_COMSERR BIT(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 BIT(0)
113 #define MXT_T9_SUPPRESS BIT(1)
114 #define MXT_T9_AMP BIT(2)
115 #define MXT_T9_VECTOR BIT(3)
116 #define MXT_T9_MOVE BIT(4)
117 #define MXT_T9_RELEASE BIT(5)
118 #define MXT_T9_PRESS BIT(6)
119 #define MXT_T9_DETECT BIT(7)
126 /* MXT_TOUCH_MULTI_T9 orient */
127 #define MXT_T9_ORIENT_SWITCH BIT(0)
128 #define MXT_T9_ORIENT_INVERTX BIT(1)
129 #define MXT_T9_ORIENT_INVERTY BIT(2)
131 /* MXT_SPT_COMMSCONFIG_T18 */
132 #define MXT_COMMS_CTRL 0
133 #define MXT_COMMS_CMD 1
134 #define MXT_COMMS_RETRIGEN BIT(6)
136 /* MXT_DEBUG_DIAGNOSTIC_T37 */
137 #define MXT_DIAGNOSTIC_PAGEUP 0x01
138 #define MXT_DIAGNOSTIC_DELTAS 0x10
139 #define MXT_DIAGNOSTIC_REFS 0x11
140 #define MXT_DIAGNOSTIC_SIZE 128
142 #define MXT_FAMILY_1386 160
143 #define MXT1386_COLUMNS 3
144 #define MXT1386_PAGES_PER_COLUMN 8
147 #ifdef CONFIG_TOUCHSCREEN_ATMEL_MXT_T37
150 u8 data
[MXT_DIAGNOSTIC_SIZE
];
154 /* Define for MXT_GEN_COMMAND_T6 */
155 #define MXT_BOOT_VALUE 0xa5
156 #define MXT_RESET_VALUE 0x01
157 #define MXT_BACKUP_VALUE 0x55
159 /* T100 Multiple Touch Touchscreen */
160 #define MXT_T100_CTRL 0
161 #define MXT_T100_CFG1 1
162 #define MXT_T100_TCHAUX 3
163 #define MXT_T100_XSIZE 9
164 #define MXT_T100_XRANGE 13
165 #define MXT_T100_YSIZE 20
166 #define MXT_T100_YRANGE 24
168 #define MXT_T100_CFG_SWITCHXY BIT(5)
169 #define MXT_T100_CFG_INVERTY BIT(6)
170 #define MXT_T100_CFG_INVERTX BIT(7)
172 #define MXT_T100_TCHAUX_VECT BIT(0)
173 #define MXT_T100_TCHAUX_AMPL BIT(1)
174 #define MXT_T100_TCHAUX_AREA BIT(2)
176 #define MXT_T100_DETECT BIT(7)
177 #define MXT_T100_TYPE_MASK 0x70
180 MXT_T100_TYPE_FINGER
= 1,
181 MXT_T100_TYPE_PASSIVE_STYLUS
= 2,
182 MXT_T100_TYPE_HOVERING_FINGER
= 4,
183 MXT_T100_TYPE_GLOVE
= 5,
184 MXT_T100_TYPE_LARGE_TOUCH
= 6,
187 #define MXT_DISTANCE_ACTIVE_TOUCH 0
188 #define MXT_DISTANCE_HOVERING 1
190 #define MXT_TOUCH_MAJOR_DEFAULT 1
191 #define MXT_PRESSURE_DEFAULT 1
194 #define MXT_BACKUP_TIME 50 /* msec */
195 #define MXT_RESET_GPIO_TIME 20 /* msec */
196 #define MXT_RESET_INVALID_CHG 100 /* msec */
197 #define MXT_RESET_TIME 200 /* msec */
198 #define MXT_RESET_TIMEOUT 3000 /* msec */
199 #define MXT_CRC_TIMEOUT 1000 /* msec */
200 #define MXT_FW_RESET_TIME 3000 /* msec */
201 #define MXT_FW_CHG_TIMEOUT 300 /* msec */
203 /* Command to unlock bootloader */
204 #define MXT_UNLOCK_CMD_MSB 0xaa
205 #define MXT_UNLOCK_CMD_LSB 0xdc
207 /* Bootloader mode status */
208 #define MXT_WAITING_BOOTLOAD_CMD 0xc0 /* valid 7 6 bit only */
209 #define MXT_WAITING_FRAME_DATA 0x80 /* valid 7 6 bit only */
210 #define MXT_FRAME_CRC_CHECK 0x02
211 #define MXT_FRAME_CRC_FAIL 0x03
212 #define MXT_FRAME_CRC_PASS 0x04
213 #define MXT_APP_CRC_FAIL 0x40 /* valid 7 8 bit only */
214 #define MXT_BOOT_STATUS_MASK 0x3f
215 #define MXT_BOOT_EXTENDED_ID BIT(5)
216 #define MXT_BOOT_ID_MASK 0x1f
218 /* Touchscreen absolute values */
219 #define MXT_MAX_AREA 0xff
221 #define MXT_PIXELS_PER_MM 20
237 u8 instances_minus_one
;
243 u16 diag_cmd_address
;
244 struct t37_debug
*t37_buf
;
245 unsigned int t37_pages
;
246 unsigned int t37_nodes
;
248 struct v4l2_device v4l2
;
249 struct v4l2_pix_format format
;
250 struct video_device vdev
;
251 struct vb2_queue queue
;
256 enum v4l_dbg_inputs
{
257 MXT_V4L_INPUT_DELTAS
,
262 enum mxt_suspend_mode
{
263 MXT_SUSPEND_DEEP_SLEEP
= 0,
264 MXT_SUSPEND_T9_CTRL
= 1,
267 /* Config update context */
277 struct mxt_info info
;
280 /* Each client has this additional data */
282 struct i2c_client
*client
;
283 struct input_dev
*input_dev
;
284 char phys
[64]; /* device physical location */
285 struct mxt_object
*object_table
;
286 struct mxt_info
*info
;
287 void *raw_info_block
;
308 u8 last_message_count
;
311 struct t7_config t7_cfg
;
313 struct regulator_bulk_data regulators
[2];
314 struct gpio_desc
*reset_gpio
;
315 bool use_retrigen_workaround
;
317 /* Cached parameters from object table */
329 u8 T100_reportid_min
;
330 u8 T100_reportid_max
;
332 /* for fw update in bootloader */
333 struct completion bl_completion
;
335 /* for reset handling */
336 struct completion reset_completion
;
338 /* for config update handling */
339 struct completion crc_completion
;
342 unsigned int t19_num_keys
;
344 enum mxt_suspend_mode suspend_mode
;
347 struct mxt_vb2_buffer
{
348 struct vb2_buffer vb
;
349 struct list_head list
;
352 static size_t mxt_obj_size(const struct mxt_object
*obj
)
354 return obj
->size_minus_one
+ 1;
357 static size_t mxt_obj_instances(const struct mxt_object
*obj
)
359 return obj
->instances_minus_one
+ 1;
362 static bool mxt_object_readable(unsigned int type
)
365 case MXT_GEN_COMMAND_T6
:
366 case MXT_GEN_POWER_T7
:
367 case MXT_GEN_ACQUIRE_T8
:
368 case MXT_GEN_DATASOURCE_T53
:
369 case MXT_TOUCH_MULTI_T9
:
370 case MXT_TOUCH_KEYARRAY_T15
:
371 case MXT_TOUCH_PROXIMITY_T23
:
372 case MXT_TOUCH_PROXKEY_T52
:
373 case MXT_TOUCH_MULTITOUCHSCREEN_T100
:
374 case MXT_PROCI_GRIPFACE_T20
:
375 case MXT_PROCG_NOISE_T22
:
376 case MXT_PROCI_ONETOUCH_T24
:
377 case MXT_PROCI_TWOTOUCH_T27
:
378 case MXT_PROCI_GRIP_T40
:
379 case MXT_PROCI_PALM_T41
:
380 case MXT_PROCI_TOUCHSUPPRESSION_T42
:
381 case MXT_PROCI_STYLUS_T47
:
382 case MXT_PROCG_NOISESUPPRESSION_T48
:
383 case MXT_SPT_COMMSCONFIG_T18
:
384 case MXT_SPT_GPIOPWM_T19
:
385 case MXT_SPT_SELFTEST_T25
:
386 case MXT_SPT_CTECONFIG_T28
:
387 case MXT_SPT_USERDATA_T38
:
388 case MXT_SPT_DIGITIZER_T43
:
389 case MXT_SPT_CTECONFIG_T46
:
390 case MXT_SPT_DYNAMICCONFIGURATIONCONTAINER_T71
:
397 static void mxt_dump_message(struct mxt_data
*data
, u8
*message
)
399 dev_dbg(&data
->client
->dev
, "message: %*ph\n",
400 data
->T5_msg_size
, message
);
403 static int mxt_wait_for_completion(struct mxt_data
*data
,
404 struct completion
*comp
,
405 unsigned int timeout_ms
)
407 struct device
*dev
= &data
->client
->dev
;
408 unsigned long timeout
= msecs_to_jiffies(timeout_ms
);
411 ret
= wait_for_completion_interruptible_timeout(comp
, timeout
);
414 } else if (ret
== 0) {
415 dev_err(dev
, "Wait for completion timed out.\n");
421 static int mxt_bootloader_read(struct mxt_data
*data
,
422 u8
*val
, unsigned int count
)
427 msg
.addr
= data
->bootloader_addr
;
428 msg
.flags
= data
->client
->flags
& I2C_M_TEN
;
429 msg
.flags
|= I2C_M_RD
;
433 ret
= i2c_transfer(data
->client
->adapter
, &msg
, 1);
437 ret
= ret
< 0 ? ret
: -EIO
;
438 dev_err(&data
->client
->dev
, "%s: i2c recv failed (%d)\n",
445 static int mxt_bootloader_write(struct mxt_data
*data
,
446 const u8
* const val
, unsigned int count
)
451 msg
.addr
= data
->bootloader_addr
;
452 msg
.flags
= data
->client
->flags
& I2C_M_TEN
;
456 ret
= i2c_transfer(data
->client
->adapter
, &msg
, 1);
460 ret
= ret
< 0 ? ret
: -EIO
;
461 dev_err(&data
->client
->dev
, "%s: i2c send failed (%d)\n",
468 static int mxt_lookup_bootloader_address(struct mxt_data
*data
, bool retry
)
470 u8 appmode
= data
->client
->addr
;
472 u8 family_id
= data
->info
? data
->info
->family_id
: 0;
477 /* Chips after 1664S use different scheme */
478 if (retry
|| family_id
>= 0xa2) {
479 bootloader
= appmode
- 0x24;
482 fallthrough
; /* for normal case */
487 bootloader
= appmode
- 0x26;
491 dev_err(&data
->client
->dev
,
492 "Appmode i2c address 0x%02x not found\n",
497 data
->bootloader_addr
= bootloader
;
501 static int mxt_probe_bootloader(struct mxt_data
*data
, bool alt_address
)
503 struct device
*dev
= &data
->client
->dev
;
508 error
= mxt_lookup_bootloader_address(data
, alt_address
);
512 error
= mxt_bootloader_read(data
, &val
, 1);
516 /* Check app crc fail mode */
517 crc_failure
= (val
& ~MXT_BOOT_STATUS_MASK
) == MXT_APP_CRC_FAIL
;
519 dev_err(dev
, "Detected bootloader, status:%02X%s\n",
520 val
, crc_failure
? ", APP_CRC_FAIL" : "");
525 static u8
mxt_get_bootloader_version(struct mxt_data
*data
, u8 val
)
527 struct device
*dev
= &data
->client
->dev
;
530 if (val
& MXT_BOOT_EXTENDED_ID
) {
531 if (mxt_bootloader_read(data
, &buf
[0], 3) != 0) {
532 dev_err(dev
, "%s: i2c failure\n", __func__
);
536 dev_dbg(dev
, "Bootloader ID:%d Version:%d\n", buf
[1], buf
[2]);
540 dev_dbg(dev
, "Bootloader ID:%d\n", val
& MXT_BOOT_ID_MASK
);
546 static int mxt_check_bootloader(struct mxt_data
*data
, unsigned int state
,
549 struct device
*dev
= &data
->client
->dev
;
556 * In application update mode, the interrupt
557 * line signals state transitions. We must wait for the
558 * CHG assertion before reading the status byte.
559 * Once the status byte has been read, the line is deasserted.
561 ret
= mxt_wait_for_completion(data
, &data
->bl_completion
,
565 * TODO: handle -ERESTARTSYS better by terminating
566 * fw update process before returning to userspace
567 * by writing length 0x000 to device (iff we are in
568 * WAITING_FRAME_DATA state).
570 dev_err(dev
, "Update wait error %d\n", ret
);
575 ret
= mxt_bootloader_read(data
, &val
, 1);
579 if (state
== MXT_WAITING_BOOTLOAD_CMD
)
580 val
= mxt_get_bootloader_version(data
, val
);
583 case MXT_WAITING_BOOTLOAD_CMD
:
584 case MXT_WAITING_FRAME_DATA
:
585 case MXT_APP_CRC_FAIL
:
586 val
&= ~MXT_BOOT_STATUS_MASK
;
588 case MXT_FRAME_CRC_PASS
:
589 if (val
== MXT_FRAME_CRC_CHECK
) {
591 } else if (val
== MXT_FRAME_CRC_FAIL
) {
592 dev_err(dev
, "Bootloader CRC fail\n");
601 dev_err(dev
, "Invalid bootloader state %02X != %02X\n",
609 static int mxt_send_bootloader_cmd(struct mxt_data
*data
, bool unlock
)
614 buf
[0] = MXT_UNLOCK_CMD_LSB
;
615 buf
[1] = MXT_UNLOCK_CMD_MSB
;
621 return mxt_bootloader_write(data
, buf
, sizeof(buf
));
624 static int __mxt_read_reg(struct i2c_client
*client
,
625 u16 reg
, u16 len
, void *val
)
627 struct i2c_msg xfer
[2];
632 buf
[1] = (reg
>> 8) & 0xff;
635 xfer
[0].addr
= client
->addr
;
641 xfer
[1].addr
= client
->addr
;
642 xfer
[1].flags
= I2C_M_RD
;
646 ret
= i2c_transfer(client
->adapter
, xfer
, 2);
652 dev_err(&client
->dev
, "%s: i2c transfer failed (%d)\n",
659 static int __mxt_write_reg(struct i2c_client
*client
, u16 reg
, u16 len
,
667 buf
= kmalloc(count
, GFP_KERNEL
);
672 buf
[1] = (reg
>> 8) & 0xff;
673 memcpy(&buf
[2], val
, len
);
675 ret
= i2c_master_send(client
, buf
, count
);
681 dev_err(&client
->dev
, "%s: i2c send failed (%d)\n",
689 static int mxt_write_reg(struct i2c_client
*client
, u16 reg
, u8 val
)
691 return __mxt_write_reg(client
, reg
, 1, &val
);
694 static struct mxt_object
*
695 mxt_get_object(struct mxt_data
*data
, u8 type
)
697 struct mxt_object
*object
;
700 for (i
= 0; i
< data
->info
->object_num
; i
++) {
701 object
= data
->object_table
+ i
;
702 if (object
->type
== type
)
706 dev_warn(&data
->client
->dev
, "Invalid object type T%u\n", type
);
710 static void mxt_proc_t6_messages(struct mxt_data
*data
, u8
*msg
)
712 struct device
*dev
= &data
->client
->dev
;
714 u32 crc
= msg
[2] | (msg
[3] << 8) | (msg
[4] << 16);
716 if (crc
!= data
->config_crc
) {
717 data
->config_crc
= crc
;
718 dev_dbg(dev
, "T6 Config Checksum: 0x%06X\n", crc
);
721 complete(&data
->crc_completion
);
724 if (status
& MXT_T6_STATUS_RESET
)
725 complete(&data
->reset_completion
);
727 /* Output debug if status has changed */
728 if (status
!= data
->t6_status
)
729 dev_dbg(dev
, "T6 Status 0x%02X%s%s%s%s%s%s%s\n",
731 status
== 0 ? " OK" : "",
732 status
& MXT_T6_STATUS_RESET
? " RESET" : "",
733 status
& MXT_T6_STATUS_OFL
? " OFL" : "",
734 status
& MXT_T6_STATUS_SIGERR
? " SIGERR" : "",
735 status
& MXT_T6_STATUS_CAL
? " CAL" : "",
736 status
& MXT_T6_STATUS_CFGERR
? " CFGERR" : "",
737 status
& MXT_T6_STATUS_COMSERR
? " COMSERR" : "");
739 /* Save current status */
740 data
->t6_status
= status
;
743 static int mxt_write_object(struct mxt_data
*data
,
744 u8 type
, u8 offset
, u8 val
)
746 struct mxt_object
*object
;
749 object
= mxt_get_object(data
, type
);
750 if (!object
|| offset
>= mxt_obj_size(object
))
753 reg
= object
->start_address
;
754 return mxt_write_reg(data
->client
, reg
+ offset
, val
);
757 static void mxt_input_button(struct mxt_data
*data
, u8
*message
)
759 struct input_dev
*input
= data
->input_dev
;
762 for (i
= 0; i
< data
->t19_num_keys
; i
++) {
763 if (data
->t19_keymap
[i
] == KEY_RESERVED
)
766 /* Active-low switch */
767 input_report_key(input
, data
->t19_keymap
[i
],
768 !(message
[1] & BIT(i
)));
772 static void mxt_input_sync(struct mxt_data
*data
)
774 input_mt_report_pointer_emulation(data
->input_dev
,
776 input_sync(data
->input_dev
);
779 static void mxt_proc_t9_message(struct mxt_data
*data
, u8
*message
)
781 struct device
*dev
= &data
->client
->dev
;
782 struct input_dev
*input_dev
= data
->input_dev
;
790 id
= message
[0] - data
->T9_reportid_min
;
792 x
= (message
[2] << 4) | ((message
[4] >> 4) & 0xf);
793 y
= (message
[3] << 4) | ((message
[4] & 0xf));
795 /* Handle 10/12 bit switching */
796 if (data
->max_x
< 1024)
798 if (data
->max_y
< 1024)
802 amplitude
= message
[6];
805 "[%u] %c%c%c%c%c%c%c%c x: %5u y: %5u area: %3u amp: %3u\n",
807 (status
& MXT_T9_DETECT
) ? 'D' : '.',
808 (status
& MXT_T9_PRESS
) ? 'P' : '.',
809 (status
& MXT_T9_RELEASE
) ? 'R' : '.',
810 (status
& MXT_T9_MOVE
) ? 'M' : '.',
811 (status
& MXT_T9_VECTOR
) ? 'V' : '.',
812 (status
& MXT_T9_AMP
) ? 'A' : '.',
813 (status
& MXT_T9_SUPPRESS
) ? 'S' : '.',
814 (status
& MXT_T9_UNGRIP
) ? 'U' : '.',
815 x
, y
, area
, amplitude
);
817 input_mt_slot(input_dev
, id
);
819 if (status
& MXT_T9_DETECT
) {
821 * Multiple bits may be set if the host is slow to read
822 * the status messages, indicating all the events that
825 if (status
& MXT_T9_RELEASE
) {
826 input_mt_report_slot_inactive(input_dev
);
827 mxt_input_sync(data
);
830 /* if active, pressure must be non-zero */
832 amplitude
= MXT_PRESSURE_DEFAULT
;
835 input_mt_report_slot_state(input_dev
, MT_TOOL_FINGER
, 1);
836 input_report_abs(input_dev
, ABS_MT_POSITION_X
, x
);
837 input_report_abs(input_dev
, ABS_MT_POSITION_Y
, y
);
838 input_report_abs(input_dev
, ABS_MT_PRESSURE
, amplitude
);
839 input_report_abs(input_dev
, ABS_MT_TOUCH_MAJOR
, area
);
841 /* Touch no longer active, close out slot */
842 input_mt_report_slot_inactive(input_dev
);
845 data
->update_input
= true;
848 static void mxt_proc_t100_message(struct mxt_data
*data
, u8
*message
)
850 struct device
*dev
= &data
->client
->dev
;
851 struct input_dev
*input_dev
= data
->input_dev
;
863 id
= message
[0] - data
->T100_reportid_min
- 2;
865 /* ignore SCRSTATUS events */
870 x
= get_unaligned_le16(&message
[2]);
871 y
= get_unaligned_le16(&message
[4]);
873 if (status
& MXT_T100_DETECT
) {
874 type
= (status
& MXT_T100_TYPE_MASK
) >> 4;
877 case MXT_T100_TYPE_HOVERING_FINGER
:
878 tool
= MT_TOOL_FINGER
;
879 distance
= MXT_DISTANCE_HOVERING
;
881 if (data
->t100_aux_vect
)
882 orientation
= message
[data
->t100_aux_vect
];
886 case MXT_T100_TYPE_FINGER
:
887 case MXT_T100_TYPE_GLOVE
:
888 tool
= MT_TOOL_FINGER
;
889 distance
= MXT_DISTANCE_ACTIVE_TOUCH
;
891 if (data
->t100_aux_area
)
892 major
= message
[data
->t100_aux_area
];
894 if (data
->t100_aux_ampl
)
895 pressure
= message
[data
->t100_aux_ampl
];
897 if (data
->t100_aux_vect
)
898 orientation
= message
[data
->t100_aux_vect
];
902 case MXT_T100_TYPE_PASSIVE_STYLUS
:
906 * Passive stylus is reported with size zero so
909 major
= MXT_TOUCH_MAJOR_DEFAULT
;
911 if (data
->t100_aux_ampl
)
912 pressure
= message
[data
->t100_aux_ampl
];
916 case MXT_T100_TYPE_LARGE_TOUCH
:
917 /* Ignore suppressed touch */
921 dev_dbg(dev
, "Unexpected T100 type\n");
927 * Values reported should be non-zero if tool is touching the
930 if (!pressure
&& type
!= MXT_T100_TYPE_HOVERING_FINGER
)
931 pressure
= MXT_PRESSURE_DEFAULT
;
933 input_mt_slot(input_dev
, id
);
935 if (status
& MXT_T100_DETECT
) {
936 dev_dbg(dev
, "[%u] type:%u x:%u y:%u a:%02X p:%02X v:%02X\n",
937 id
, type
, x
, y
, major
, pressure
, orientation
);
939 input_mt_report_slot_state(input_dev
, tool
, 1);
940 input_report_abs(input_dev
, ABS_MT_POSITION_X
, x
);
941 input_report_abs(input_dev
, ABS_MT_POSITION_Y
, y
);
942 input_report_abs(input_dev
, ABS_MT_TOUCH_MAJOR
, major
);
943 input_report_abs(input_dev
, ABS_MT_PRESSURE
, pressure
);
944 input_report_abs(input_dev
, ABS_MT_DISTANCE
, distance
);
945 input_report_abs(input_dev
, ABS_MT_ORIENTATION
, orientation
);
947 dev_dbg(dev
, "[%u] release\n", id
);
950 input_mt_report_slot_inactive(input_dev
);
953 data
->update_input
= true;
956 static int mxt_proc_message(struct mxt_data
*data
, u8
*message
)
958 u8 report_id
= message
[0];
960 if (report_id
== MXT_RPTID_NOMSG
)
963 if (report_id
== data
->T6_reportid
) {
964 mxt_proc_t6_messages(data
, message
);
965 } else if (!data
->input_dev
) {
967 * Do not report events if input device
968 * is not yet registered.
970 mxt_dump_message(data
, message
);
971 } else if (report_id
>= data
->T9_reportid_min
&&
972 report_id
<= data
->T9_reportid_max
) {
973 mxt_proc_t9_message(data
, message
);
974 } else if (report_id
>= data
->T100_reportid_min
&&
975 report_id
<= data
->T100_reportid_max
) {
976 mxt_proc_t100_message(data
, message
);
977 } else if (report_id
== data
->T19_reportid
) {
978 mxt_input_button(data
, message
);
979 data
->update_input
= true;
981 mxt_dump_message(data
, message
);
987 static int mxt_read_and_process_messages(struct mxt_data
*data
, u8 count
)
989 struct device
*dev
= &data
->client
->dev
;
994 /* Safety check for msg_buf */
995 if (count
> data
->max_reportid
)
998 /* Process remaining messages if necessary */
999 ret
= __mxt_read_reg(data
->client
, data
->T5_address
,
1000 data
->T5_msg_size
* count
, data
->msg_buf
);
1002 dev_err(dev
, "Failed to read %u messages (%d)\n", count
, ret
);
1006 for (i
= 0; i
< count
; i
++) {
1007 ret
= mxt_proc_message(data
,
1008 data
->msg_buf
+ data
->T5_msg_size
* i
);
1014 /* return number of messages read */
1018 static irqreturn_t
mxt_process_messages_t44(struct mxt_data
*data
)
1020 struct device
*dev
= &data
->client
->dev
;
1024 /* Read T44 and T5 together */
1025 ret
= __mxt_read_reg(data
->client
, data
->T44_address
,
1026 data
->T5_msg_size
+ 1, data
->msg_buf
);
1028 dev_err(dev
, "Failed to read T44 and T5 (%d)\n", ret
);
1032 count
= data
->msg_buf
[0];
1035 * This condition may be caused by the CHG line being configured in
1036 * Mode 0. It results in unnecessary I2C operations but it is benign.
1041 if (count
> data
->max_reportid
) {
1042 dev_warn(dev
, "T44 count %d exceeded max report id\n", count
);
1043 count
= data
->max_reportid
;
1046 /* Process first message */
1047 ret
= mxt_proc_message(data
, data
->msg_buf
+ 1);
1049 dev_warn(dev
, "Unexpected invalid message\n");
1053 num_left
= count
- 1;
1055 /* Process remaining messages if necessary */
1057 ret
= mxt_read_and_process_messages(data
, num_left
);
1060 else if (ret
!= num_left
)
1061 dev_warn(dev
, "Unexpected invalid message\n");
1065 if (data
->update_input
) {
1066 mxt_input_sync(data
);
1067 data
->update_input
= false;
1073 static int mxt_process_messages_until_invalid(struct mxt_data
*data
)
1075 struct device
*dev
= &data
->client
->dev
;
1079 count
= data
->max_reportid
;
1081 /* Read messages until we force an invalid */
1083 read
= mxt_read_and_process_messages(data
, count
);
1088 if (data
->update_input
) {
1089 mxt_input_sync(data
);
1090 data
->update_input
= false;
1093 dev_err(dev
, "CHG pin isn't cleared\n");
1097 static irqreturn_t
mxt_process_messages(struct mxt_data
*data
)
1099 int total_handled
, num_handled
;
1100 u8 count
= data
->last_message_count
;
1102 if (count
< 1 || count
> data
->max_reportid
)
1105 /* include final invalid message */
1106 total_handled
= mxt_read_and_process_messages(data
, count
+ 1);
1107 if (total_handled
< 0)
1109 /* if there were invalid messages, then we are done */
1110 else if (total_handled
<= count
)
1113 /* keep reading two msgs until one is invalid or reportid limit */
1115 num_handled
= mxt_read_and_process_messages(data
, 2);
1116 if (num_handled
< 0)
1119 total_handled
+= num_handled
;
1121 if (num_handled
< 2)
1123 } while (total_handled
< data
->num_touchids
);
1126 data
->last_message_count
= total_handled
;
1128 if (data
->update_input
) {
1129 mxt_input_sync(data
);
1130 data
->update_input
= false;
1136 static irqreturn_t
mxt_interrupt(int irq
, void *dev_id
)
1138 struct mxt_data
*data
= dev_id
;
1140 if (data
->in_bootloader
) {
1141 /* bootloader state transition completion */
1142 complete(&data
->bl_completion
);
1146 if (!data
->object_table
)
1149 if (data
->T44_address
) {
1150 return mxt_process_messages_t44(data
);
1152 return mxt_process_messages(data
);
1156 static int mxt_t6_command(struct mxt_data
*data
, u16 cmd_offset
,
1157 u8 value
, bool wait
)
1160 u8 command_register
;
1161 int timeout_counter
= 0;
1164 reg
= data
->T6_address
+ cmd_offset
;
1166 ret
= mxt_write_reg(data
->client
, reg
, value
);
1175 ret
= __mxt_read_reg(data
->client
, reg
, 1, &command_register
);
1178 } while (command_register
!= 0 && timeout_counter
++ <= 100);
1180 if (timeout_counter
> 100) {
1181 dev_err(&data
->client
->dev
, "Command failed!\n");
1188 static int mxt_acquire_irq(struct mxt_data
*data
)
1192 enable_irq(data
->irq
);
1194 if (data
->use_retrigen_workaround
) {
1195 error
= mxt_process_messages_until_invalid(data
);
1203 static int mxt_soft_reset(struct mxt_data
*data
)
1205 struct device
*dev
= &data
->client
->dev
;
1208 dev_info(dev
, "Resetting device\n");
1210 disable_irq(data
->irq
);
1212 reinit_completion(&data
->reset_completion
);
1214 ret
= mxt_t6_command(data
, MXT_COMMAND_RESET
, MXT_RESET_VALUE
, false);
1218 /* Ignore CHG line for 100ms after reset */
1219 msleep(MXT_RESET_INVALID_CHG
);
1221 mxt_acquire_irq(data
);
1223 ret
= mxt_wait_for_completion(data
, &data
->reset_completion
,
1231 static void mxt_update_crc(struct mxt_data
*data
, u8 cmd
, u8 value
)
1234 * On failure, CRC is set to 0 and config will always be
1237 data
->config_crc
= 0;
1238 reinit_completion(&data
->crc_completion
);
1240 mxt_t6_command(data
, cmd
, value
, true);
1243 * Wait for crc message. On failure, CRC is set to 0 and config will
1244 * always be downloaded.
1246 mxt_wait_for_completion(data
, &data
->crc_completion
, MXT_CRC_TIMEOUT
);
1249 static void mxt_calc_crc24(u32
*crc
, u8 firstbyte
, u8 secondbyte
)
1251 static const unsigned int crcpoly
= 0x80001B;
1255 data_word
= (secondbyte
<< 8) | firstbyte
;
1256 result
= ((*crc
<< 1) ^ data_word
);
1258 if (result
& 0x1000000)
1264 static u32
mxt_calculate_crc(u8
*base
, off_t start_off
, off_t end_off
)
1267 u8
*ptr
= base
+ start_off
;
1268 u8
*last_val
= base
+ end_off
- 1;
1270 if (end_off
< start_off
)
1273 while (ptr
< last_val
) {
1274 mxt_calc_crc24(&crc
, *ptr
, *(ptr
+ 1));
1278 /* if len is odd, fill the last byte with 0 */
1279 if (ptr
== last_val
)
1280 mxt_calc_crc24(&crc
, *ptr
, 0);
1282 /* Mask to 24-bit */
1288 static int mxt_check_retrigen(struct mxt_data
*data
)
1290 struct i2c_client
*client
= data
->client
;
1293 struct irq_data
*irqd
;
1295 data
->use_retrigen_workaround
= false;
1297 irqd
= irq_get_irq_data(data
->irq
);
1301 if (irqd_is_level_type(irqd
))
1304 if (data
->T18_address
) {
1305 error
= __mxt_read_reg(client
,
1306 data
->T18_address
+ MXT_COMMS_CTRL
,
1311 if (val
& MXT_COMMS_RETRIGEN
)
1315 dev_warn(&client
->dev
, "Enabling RETRIGEN workaround\n");
1316 data
->use_retrigen_workaround
= true;
1320 static int mxt_prepare_cfg_mem(struct mxt_data
*data
, struct mxt_cfg
*cfg
)
1322 struct device
*dev
= &data
->client
->dev
;
1323 struct mxt_object
*object
;
1324 unsigned int type
, instance
, size
, byte_offset
;
1331 while (cfg
->raw_pos
< cfg
->raw_size
) {
1332 /* Read type, instance, length */
1333 ret
= sscanf(cfg
->raw
+ cfg
->raw_pos
, "%x %x %x%n",
1334 &type
, &instance
, &size
, &offset
);
1338 } else if (ret
!= 3) {
1339 dev_err(dev
, "Bad format: failed to parse object\n");
1342 cfg
->raw_pos
+= offset
;
1344 object
= mxt_get_object(data
, type
);
1347 for (i
= 0; i
< size
; i
++) {
1348 ret
= sscanf(cfg
->raw
+ cfg
->raw_pos
, "%hhx%n",
1351 dev_err(dev
, "Bad format in T%d at %d\n",
1355 cfg
->raw_pos
+= offset
;
1360 if (size
> mxt_obj_size(object
)) {
1362 * Either we are in fallback mode due to wrong
1363 * config or config from a later fw version,
1364 * or the file is corrupt or hand-edited.
1366 dev_warn(dev
, "Discarding %zu byte(s) in T%u\n",
1367 size
- mxt_obj_size(object
), type
);
1368 } else if (mxt_obj_size(object
) > size
) {
1370 * If firmware is upgraded, new bytes may be added to
1371 * end of objects. It is generally forward compatible
1372 * to zero these bytes - previous behaviour will be
1373 * retained. However this does invalidate the CRC and
1374 * will force fallback mode until the configuration is
1375 * updated. We warn here but do nothing else - the
1376 * malloc has zeroed the entire configuration.
1378 dev_warn(dev
, "Zeroing %zu byte(s) in T%d\n",
1379 mxt_obj_size(object
) - size
, type
);
1382 if (instance
>= mxt_obj_instances(object
)) {
1383 dev_err(dev
, "Object instances exceeded!\n");
1387 reg
= object
->start_address
+ mxt_obj_size(object
) * instance
;
1389 for (i
= 0; i
< size
; i
++) {
1390 ret
= sscanf(cfg
->raw
+ cfg
->raw_pos
, "%hhx%n",
1394 dev_err(dev
, "Bad format in T%d at %d\n",
1398 cfg
->raw_pos
+= offset
;
1400 if (i
> mxt_obj_size(object
))
1403 byte_offset
= reg
+ i
- cfg
->start_ofs
;
1405 if (byte_offset
>= 0 && byte_offset
< cfg
->mem_size
) {
1406 *(cfg
->mem
+ byte_offset
) = val
;
1408 dev_err(dev
, "Bad object: reg:%d, T%d, ofs=%d\n",
1409 reg
, object
->type
, byte_offset
);
1418 static int mxt_upload_cfg_mem(struct mxt_data
*data
, struct mxt_cfg
*cfg
)
1420 unsigned int byte_offset
= 0;
1423 /* Write configuration as blocks */
1424 while (byte_offset
< cfg
->mem_size
) {
1425 unsigned int size
= cfg
->mem_size
- byte_offset
;
1427 if (size
> MXT_MAX_BLOCK_WRITE
)
1428 size
= MXT_MAX_BLOCK_WRITE
;
1430 error
= __mxt_write_reg(data
->client
,
1431 cfg
->start_ofs
+ byte_offset
,
1432 size
, cfg
->mem
+ byte_offset
);
1434 dev_err(&data
->client
->dev
,
1435 "Config write error, ret=%d\n", error
);
1439 byte_offset
+= size
;
1445 static int mxt_init_t7_power_cfg(struct mxt_data
*data
);
1448 * mxt_update_cfg - download configuration to chip
1450 * Atmel Raw Config File Format
1452 * The first four lines of the raw config file contain:
1454 * 2) Chip ID Information (first 7 bytes of device memory)
1455 * 3) Chip Information Block 24-bit CRC Checksum
1456 * 4) Chip Configuration 24-bit CRC Checksum
1458 * The rest of the file consists of one line per object instance:
1459 * <TYPE> <INSTANCE> <SIZE> <CONTENTS>
1461 * <TYPE> - 2-byte object type as hex
1462 * <INSTANCE> - 2-byte object instance number as hex
1463 * <SIZE> - 2-byte object size as hex
1464 * <CONTENTS> - array of <SIZE> 1-byte hex values
1466 static int mxt_update_cfg(struct mxt_data
*data
, const struct firmware
*fw
)
1468 struct device
*dev
= &data
->client
->dev
;
1473 u32 info_crc
, config_crc
, calculated_crc
;
1476 /* Make zero terminated copy of the OBP_RAW file */
1477 cfg
.raw
= kmemdup_nul(fw
->data
, fw
->size
, GFP_KERNEL
);
1481 cfg
.raw_size
= fw
->size
;
1483 mxt_update_crc(data
, MXT_COMMAND_REPORTALL
, 1);
1485 if (strncmp(cfg
.raw
, MXT_CFG_MAGIC
, strlen(MXT_CFG_MAGIC
))) {
1486 dev_err(dev
, "Unrecognised config file\n");
1491 cfg
.raw_pos
= strlen(MXT_CFG_MAGIC
);
1493 /* Load information block and check */
1494 for (i
= 0; i
< sizeof(struct mxt_info
); i
++) {
1495 ret
= sscanf(cfg
.raw
+ cfg
.raw_pos
, "%hhx%n",
1496 (unsigned char *)&cfg
.info
+ i
,
1499 dev_err(dev
, "Bad format\n");
1504 cfg
.raw_pos
+= offset
;
1507 if (cfg
.info
.family_id
!= data
->info
->family_id
) {
1508 dev_err(dev
, "Family ID mismatch!\n");
1513 if (cfg
.info
.variant_id
!= data
->info
->variant_id
) {
1514 dev_err(dev
, "Variant ID mismatch!\n");
1520 ret
= sscanf(cfg
.raw
+ cfg
.raw_pos
, "%x%n", &info_crc
, &offset
);
1522 dev_err(dev
, "Bad format: failed to parse Info CRC\n");
1526 cfg
.raw_pos
+= offset
;
1528 ret
= sscanf(cfg
.raw
+ cfg
.raw_pos
, "%x%n", &config_crc
, &offset
);
1530 dev_err(dev
, "Bad format: failed to parse Config CRC\n");
1534 cfg
.raw_pos
+= offset
;
1537 * The Info Block CRC is calculated over mxt_info and the object
1538 * table. If it does not match then we are trying to load the
1539 * configuration from a different chip or firmware version, so
1540 * the configuration CRC is invalid anyway.
1542 if (info_crc
== data
->info_crc
) {
1543 if (config_crc
== 0 || data
->config_crc
== 0) {
1544 dev_info(dev
, "CRC zero, attempting to apply config\n");
1545 } else if (config_crc
== data
->config_crc
) {
1546 dev_dbg(dev
, "Config CRC 0x%06X: OK\n",
1551 dev_info(dev
, "Config CRC 0x%06X: does not match file 0x%06X\n",
1552 data
->config_crc
, config_crc
);
1556 "Warning: Info CRC error - device=0x%06X file=0x%06X\n",
1557 data
->info_crc
, info_crc
);
1560 /* Malloc memory to store configuration */
1561 cfg
.start_ofs
= MXT_OBJECT_START
+
1562 data
->info
->object_num
* sizeof(struct mxt_object
) +
1563 MXT_INFO_CHECKSUM_SIZE
;
1564 cfg
.mem_size
= data
->mem_size
- cfg
.start_ofs
;
1565 cfg
.mem
= kzalloc(cfg
.mem_size
, GFP_KERNEL
);
1571 ret
= mxt_prepare_cfg_mem(data
, &cfg
);
1575 /* Calculate crc of the received configs (not the raw config file) */
1576 if (data
->T71_address
)
1577 crc_start
= data
->T71_address
;
1578 else if (data
->T7_address
)
1579 crc_start
= data
->T7_address
;
1581 dev_warn(dev
, "Could not find CRC start\n");
1583 if (crc_start
> cfg
.start_ofs
) {
1584 calculated_crc
= mxt_calculate_crc(cfg
.mem
,
1585 crc_start
- cfg
.start_ofs
,
1588 if (config_crc
> 0 && config_crc
!= calculated_crc
)
1589 dev_warn(dev
, "Config CRC in file inconsistent, calculated=%06X, file=%06X\n",
1590 calculated_crc
, config_crc
);
1593 ret
= mxt_upload_cfg_mem(data
, &cfg
);
1597 mxt_update_crc(data
, MXT_COMMAND_BACKUPNV
, MXT_BACKUP_VALUE
);
1599 ret
= mxt_check_retrigen(data
);
1603 ret
= mxt_soft_reset(data
);
1607 dev_info(dev
, "Config successfully updated\n");
1609 /* T7 config may have changed */
1610 mxt_init_t7_power_cfg(data
);
1619 static void mxt_free_input_device(struct mxt_data
*data
)
1621 if (data
->input_dev
) {
1622 input_unregister_device(data
->input_dev
);
1623 data
->input_dev
= NULL
;
1627 static void mxt_free_object_table(struct mxt_data
*data
)
1629 #ifdef CONFIG_TOUCHSCREEN_ATMEL_MXT_T37
1630 video_unregister_device(&data
->dbg
.vdev
);
1631 v4l2_device_unregister(&data
->dbg
.v4l2
);
1633 data
->object_table
= NULL
;
1635 kfree(data
->raw_info_block
);
1636 data
->raw_info_block
= NULL
;
1637 kfree(data
->msg_buf
);
1638 data
->msg_buf
= NULL
;
1639 data
->T5_address
= 0;
1640 data
->T5_msg_size
= 0;
1641 data
->T6_reportid
= 0;
1642 data
->T7_address
= 0;
1643 data
->T71_address
= 0;
1644 data
->T9_reportid_min
= 0;
1645 data
->T9_reportid_max
= 0;
1646 data
->T18_address
= 0;
1647 data
->T19_reportid
= 0;
1648 data
->T44_address
= 0;
1649 data
->T100_reportid_min
= 0;
1650 data
->T100_reportid_max
= 0;
1651 data
->max_reportid
= 0;
1654 static int mxt_parse_object_table(struct mxt_data
*data
,
1655 struct mxt_object
*object_table
)
1657 struct i2c_client
*client
= data
->client
;
1662 /* Valid Report IDs start counting from 1 */
1665 for (i
= 0; i
< data
->info
->object_num
; i
++) {
1666 struct mxt_object
*object
= object_table
+ i
;
1669 le16_to_cpus(&object
->start_address
);
1671 if (object
->num_report_ids
) {
1673 reportid
+= object
->num_report_ids
*
1674 mxt_obj_instances(object
);
1675 max_id
= reportid
- 1;
1681 dev_dbg(&data
->client
->dev
,
1682 "T%u Start:%u Size:%zu Instances:%zu Report IDs:%u-%u\n",
1683 object
->type
, object
->start_address
,
1684 mxt_obj_size(object
), mxt_obj_instances(object
),
1687 switch (object
->type
) {
1688 case MXT_GEN_MESSAGE_T5
:
1689 if (data
->info
->family_id
== 0x80 &&
1690 data
->info
->version
< 0x20) {
1692 * On mXT224 firmware versions prior to V2.0
1693 * read and discard unused CRC byte otherwise
1694 * DMA reads are misaligned.
1696 data
->T5_msg_size
= mxt_obj_size(object
);
1698 /* CRC not enabled, so skip last byte */
1699 data
->T5_msg_size
= mxt_obj_size(object
) - 1;
1701 data
->T5_address
= object
->start_address
;
1703 case MXT_GEN_COMMAND_T6
:
1704 data
->T6_reportid
= min_id
;
1705 data
->T6_address
= object
->start_address
;
1707 case MXT_GEN_POWER_T7
:
1708 data
->T7_address
= object
->start_address
;
1710 case MXT_SPT_DYNAMICCONFIGURATIONCONTAINER_T71
:
1711 data
->T71_address
= object
->start_address
;
1713 case MXT_TOUCH_MULTI_T9
:
1714 data
->multitouch
= MXT_TOUCH_MULTI_T9
;
1715 /* Only handle messages from first T9 instance */
1716 data
->T9_reportid_min
= min_id
;
1717 data
->T9_reportid_max
= min_id
+
1718 object
->num_report_ids
- 1;
1719 data
->num_touchids
= object
->num_report_ids
;
1721 case MXT_SPT_COMMSCONFIG_T18
:
1722 data
->T18_address
= object
->start_address
;
1724 case MXT_SPT_MESSAGECOUNT_T44
:
1725 data
->T44_address
= object
->start_address
;
1727 case MXT_SPT_GPIOPWM_T19
:
1728 data
->T19_reportid
= min_id
;
1730 case MXT_TOUCH_MULTITOUCHSCREEN_T100
:
1731 data
->multitouch
= MXT_TOUCH_MULTITOUCHSCREEN_T100
;
1732 data
->T100_reportid_min
= min_id
;
1733 data
->T100_reportid_max
= max_id
;
1734 /* first two report IDs reserved */
1735 data
->num_touchids
= object
->num_report_ids
- 2;
1739 end_address
= object
->start_address
1740 + mxt_obj_size(object
) * mxt_obj_instances(object
) - 1;
1742 if (end_address
>= data
->mem_size
)
1743 data
->mem_size
= end_address
+ 1;
1746 /* Store maximum reportid */
1747 data
->max_reportid
= reportid
;
1749 /* If T44 exists, T5 position has to be directly after */
1750 if (data
->T44_address
&& (data
->T5_address
!= data
->T44_address
+ 1)) {
1751 dev_err(&client
->dev
, "Invalid T44 position\n");
1755 data
->msg_buf
= kcalloc(data
->max_reportid
,
1756 data
->T5_msg_size
, GFP_KERNEL
);
1763 static int mxt_read_info_block(struct mxt_data
*data
)
1765 struct i2c_client
*client
= data
->client
;
1769 uint8_t num_objects
;
1773 /* If info block already allocated, free it */
1774 if (data
->raw_info_block
)
1775 mxt_free_object_table(data
);
1777 /* Read 7-byte ID information block starting at address 0 */
1778 size
= sizeof(struct mxt_info
);
1779 id_buf
= kzalloc(size
, GFP_KERNEL
);
1783 error
= __mxt_read_reg(client
, 0, size
, id_buf
);
1787 /* Resize buffer to give space for rest of info block */
1788 num_objects
= ((struct mxt_info
*)id_buf
)->object_num
;
1789 size
+= (num_objects
* sizeof(struct mxt_object
))
1790 + MXT_INFO_CHECKSUM_SIZE
;
1792 buf
= krealloc(id_buf
, size
, GFP_KERNEL
);
1799 /* Read rest of info block */
1800 error
= __mxt_read_reg(client
, MXT_OBJECT_START
,
1801 size
- MXT_OBJECT_START
,
1802 id_buf
+ MXT_OBJECT_START
);
1806 /* Extract & calculate checksum */
1807 crc_ptr
= id_buf
+ size
- MXT_INFO_CHECKSUM_SIZE
;
1808 data
->info_crc
= crc_ptr
[0] | (crc_ptr
[1] << 8) | (crc_ptr
[2] << 16);
1810 calculated_crc
= mxt_calculate_crc(id_buf
, 0,
1811 size
- MXT_INFO_CHECKSUM_SIZE
);
1814 * CRC mismatch can be caused by data corruption due to I2C comms
1815 * issue or else device is not using Object Based Protocol (eg i2c-hid)
1817 if ((data
->info_crc
== 0) || (data
->info_crc
!= calculated_crc
)) {
1818 dev_err(&client
->dev
,
1819 "Info Block CRC error calculated=0x%06X read=0x%06X\n",
1820 calculated_crc
, data
->info_crc
);
1825 data
->raw_info_block
= id_buf
;
1826 data
->info
= (struct mxt_info
*)id_buf
;
1828 dev_info(&client
->dev
,
1829 "Family: %u Variant: %u Firmware V%u.%u.%02X Objects: %u\n",
1830 data
->info
->family_id
, data
->info
->variant_id
,
1831 data
->info
->version
>> 4, data
->info
->version
& 0xf,
1832 data
->info
->build
, data
->info
->object_num
);
1834 /* Parse object table information */
1835 error
= mxt_parse_object_table(data
, id_buf
+ MXT_OBJECT_START
);
1837 dev_err(&client
->dev
, "Error %d parsing object table\n", error
);
1838 mxt_free_object_table(data
);
1842 data
->object_table
= (struct mxt_object
*)(id_buf
+ MXT_OBJECT_START
);
1851 static int mxt_read_t9_resolution(struct mxt_data
*data
)
1853 struct i2c_client
*client
= data
->client
;
1855 struct t9_range range
;
1856 unsigned char orient
;
1857 struct mxt_object
*object
;
1859 object
= mxt_get_object(data
, MXT_TOUCH_MULTI_T9
);
1863 error
= __mxt_read_reg(client
,
1864 object
->start_address
+ MXT_T9_XSIZE
,
1865 sizeof(data
->xsize
), &data
->xsize
);
1869 error
= __mxt_read_reg(client
,
1870 object
->start_address
+ MXT_T9_YSIZE
,
1871 sizeof(data
->ysize
), &data
->ysize
);
1875 error
= __mxt_read_reg(client
,
1876 object
->start_address
+ MXT_T9_RANGE
,
1877 sizeof(range
), &range
);
1881 data
->max_x
= get_unaligned_le16(&range
.x
);
1882 data
->max_y
= get_unaligned_le16(&range
.y
);
1884 error
= __mxt_read_reg(client
,
1885 object
->start_address
+ MXT_T9_ORIENT
,
1890 data
->xy_switch
= orient
& MXT_T9_ORIENT_SWITCH
;
1891 data
->invertx
= orient
& MXT_T9_ORIENT_INVERTX
;
1892 data
->inverty
= orient
& MXT_T9_ORIENT_INVERTY
;
1897 static int mxt_read_t100_config(struct mxt_data
*data
)
1899 struct i2c_client
*client
= data
->client
;
1901 struct mxt_object
*object
;
1902 u16 range_x
, range_y
;
1906 object
= mxt_get_object(data
, MXT_TOUCH_MULTITOUCHSCREEN_T100
);
1910 /* read touchscreen dimensions */
1911 error
= __mxt_read_reg(client
,
1912 object
->start_address
+ MXT_T100_XRANGE
,
1913 sizeof(range_x
), &range_x
);
1917 data
->max_x
= get_unaligned_le16(&range_x
);
1919 error
= __mxt_read_reg(client
,
1920 object
->start_address
+ MXT_T100_YRANGE
,
1921 sizeof(range_y
), &range_y
);
1925 data
->max_y
= get_unaligned_le16(&range_y
);
1927 error
= __mxt_read_reg(client
,
1928 object
->start_address
+ MXT_T100_XSIZE
,
1929 sizeof(data
->xsize
), &data
->xsize
);
1933 error
= __mxt_read_reg(client
,
1934 object
->start_address
+ MXT_T100_YSIZE
,
1935 sizeof(data
->ysize
), &data
->ysize
);
1939 /* read orientation config */
1940 error
= __mxt_read_reg(client
,
1941 object
->start_address
+ MXT_T100_CFG1
,
1946 data
->xy_switch
= cfg
& MXT_T100_CFG_SWITCHXY
;
1947 data
->invertx
= cfg
& MXT_T100_CFG_INVERTX
;
1948 data
->inverty
= cfg
& MXT_T100_CFG_INVERTY
;
1950 /* allocate aux bytes */
1951 error
= __mxt_read_reg(client
,
1952 object
->start_address
+ MXT_T100_TCHAUX
,
1959 if (tchaux
& MXT_T100_TCHAUX_VECT
)
1960 data
->t100_aux_vect
= aux
++;
1962 if (tchaux
& MXT_T100_TCHAUX_AMPL
)
1963 data
->t100_aux_ampl
= aux
++;
1965 if (tchaux
& MXT_T100_TCHAUX_AREA
)
1966 data
->t100_aux_area
= aux
++;
1968 dev_dbg(&client
->dev
,
1969 "T100 aux mappings vect:%u ampl:%u area:%u\n",
1970 data
->t100_aux_vect
, data
->t100_aux_ampl
, data
->t100_aux_area
);
1975 static int mxt_input_open(struct input_dev
*dev
);
1976 static void mxt_input_close(struct input_dev
*dev
);
1978 static void mxt_set_up_as_touchpad(struct input_dev
*input_dev
,
1979 struct mxt_data
*data
)
1983 input_dev
->name
= "Atmel maXTouch Touchpad";
1985 __set_bit(INPUT_PROP_BUTTONPAD
, input_dev
->propbit
);
1987 input_abs_set_res(input_dev
, ABS_X
, MXT_PIXELS_PER_MM
);
1988 input_abs_set_res(input_dev
, ABS_Y
, MXT_PIXELS_PER_MM
);
1989 input_abs_set_res(input_dev
, ABS_MT_POSITION_X
,
1991 input_abs_set_res(input_dev
, ABS_MT_POSITION_Y
,
1994 for (i
= 0; i
< data
->t19_num_keys
; i
++)
1995 if (data
->t19_keymap
[i
] != KEY_RESERVED
)
1996 input_set_capability(input_dev
, EV_KEY
,
1997 data
->t19_keymap
[i
]);
2000 static int mxt_initialize_input_device(struct mxt_data
*data
)
2002 struct device
*dev
= &data
->client
->dev
;
2003 struct input_dev
*input_dev
;
2005 unsigned int num_mt_slots
;
2006 unsigned int mt_flags
= 0;
2008 switch (data
->multitouch
) {
2009 case MXT_TOUCH_MULTI_T9
:
2010 num_mt_slots
= data
->T9_reportid_max
- data
->T9_reportid_min
+ 1;
2011 error
= mxt_read_t9_resolution(data
);
2013 dev_warn(dev
, "Failed to initialize T9 resolution\n");
2016 case MXT_TOUCH_MULTITOUCHSCREEN_T100
:
2017 num_mt_slots
= data
->num_touchids
;
2018 error
= mxt_read_t100_config(data
);
2020 dev_warn(dev
, "Failed to read T100 config\n");
2024 dev_err(dev
, "Invalid multitouch object\n");
2028 /* Handle default values and orientation switch */
2029 if (data
->max_x
== 0)
2032 if (data
->max_y
== 0)
2035 if (data
->xy_switch
)
2036 swap(data
->max_x
, data
->max_y
);
2038 dev_info(dev
, "Touchscreen size X%uY%u\n", data
->max_x
, data
->max_y
);
2040 /* Register input device */
2041 input_dev
= input_allocate_device();
2045 input_dev
->name
= "Atmel maXTouch Touchscreen";
2046 input_dev
->phys
= data
->phys
;
2047 input_dev
->id
.bustype
= BUS_I2C
;
2048 input_dev
->dev
.parent
= dev
;
2049 input_dev
->open
= mxt_input_open
;
2050 input_dev
->close
= mxt_input_close
;
2052 input_set_capability(input_dev
, EV_KEY
, BTN_TOUCH
);
2054 /* For single touch */
2055 input_set_abs_params(input_dev
, ABS_X
, 0, data
->max_x
, 0, 0);
2056 input_set_abs_params(input_dev
, ABS_Y
, 0, data
->max_y
, 0, 0);
2058 if (data
->multitouch
== MXT_TOUCH_MULTI_T9
||
2059 (data
->multitouch
== MXT_TOUCH_MULTITOUCHSCREEN_T100
&&
2060 data
->t100_aux_ampl
)) {
2061 input_set_abs_params(input_dev
, ABS_PRESSURE
, 0, 255, 0, 0);
2064 /* If device has buttons we assume it is a touchpad */
2065 if (data
->t19_num_keys
) {
2066 mxt_set_up_as_touchpad(input_dev
, data
);
2067 mt_flags
|= INPUT_MT_POINTER
;
2069 mt_flags
|= INPUT_MT_DIRECT
;
2072 /* For multi touch */
2073 error
= input_mt_init_slots(input_dev
, num_mt_slots
, mt_flags
);
2075 dev_err(dev
, "Error %d initialising slots\n", error
);
2079 if (data
->multitouch
== MXT_TOUCH_MULTITOUCHSCREEN_T100
) {
2080 input_set_abs_params(input_dev
, ABS_MT_TOOL_TYPE
,
2081 0, MT_TOOL_MAX
, 0, 0);
2082 input_set_abs_params(input_dev
, ABS_MT_DISTANCE
,
2083 MXT_DISTANCE_ACTIVE_TOUCH
,
2084 MXT_DISTANCE_HOVERING
,
2088 input_set_abs_params(input_dev
, ABS_MT_POSITION_X
,
2089 0, data
->max_x
, 0, 0);
2090 input_set_abs_params(input_dev
, ABS_MT_POSITION_Y
,
2091 0, data
->max_y
, 0, 0);
2093 if (data
->multitouch
== MXT_TOUCH_MULTI_T9
||
2094 (data
->multitouch
== MXT_TOUCH_MULTITOUCHSCREEN_T100
&&
2095 data
->t100_aux_area
)) {
2096 input_set_abs_params(input_dev
, ABS_MT_TOUCH_MAJOR
,
2097 0, MXT_MAX_AREA
, 0, 0);
2100 if (data
->multitouch
== MXT_TOUCH_MULTI_T9
||
2101 (data
->multitouch
== MXT_TOUCH_MULTITOUCHSCREEN_T100
&&
2102 data
->t100_aux_ampl
)) {
2103 input_set_abs_params(input_dev
, ABS_MT_PRESSURE
,
2107 if (data
->multitouch
== MXT_TOUCH_MULTITOUCHSCREEN_T100
&&
2108 data
->t100_aux_vect
) {
2109 input_set_abs_params(input_dev
, ABS_MT_ORIENTATION
,
2113 if (data
->multitouch
== MXT_TOUCH_MULTITOUCHSCREEN_T100
&&
2114 data
->t100_aux_vect
) {
2115 input_set_abs_params(input_dev
, ABS_MT_ORIENTATION
,
2119 input_set_drvdata(input_dev
, data
);
2121 error
= input_register_device(input_dev
);
2123 dev_err(dev
, "Error %d registering input device\n", error
);
2127 data
->input_dev
= input_dev
;
2132 input_free_device(input_dev
);
2136 static int mxt_configure_objects(struct mxt_data
*data
,
2137 const struct firmware
*cfg
);
2139 static void mxt_config_cb(const struct firmware
*cfg
, void *ctx
)
2141 mxt_configure_objects(ctx
, cfg
);
2142 release_firmware(cfg
);
2145 static int mxt_initialize(struct mxt_data
*data
)
2147 struct i2c_client
*client
= data
->client
;
2148 int recovery_attempts
= 0;
2152 error
= mxt_read_info_block(data
);
2156 /* Check bootloader state */
2157 error
= mxt_probe_bootloader(data
, false);
2159 dev_info(&client
->dev
, "Trying alternate bootloader address\n");
2160 error
= mxt_probe_bootloader(data
, true);
2162 /* Chip is not in appmode or bootloader mode */
2167 /* OK, we are in bootloader, see if we can recover */
2168 if (++recovery_attempts
> 1) {
2169 dev_err(&client
->dev
, "Could not recover from bootloader mode\n");
2171 * We can reflash from this state, so do not
2172 * abort initialization.
2174 data
->in_bootloader
= true;
2178 /* Attempt to exit bootloader into app mode */
2179 mxt_send_bootloader_cmd(data
, false);
2180 msleep(MXT_FW_RESET_TIME
);
2183 error
= mxt_check_retrigen(data
);
2187 error
= mxt_acquire_irq(data
);
2191 error
= request_firmware_nowait(THIS_MODULE
, true, MXT_CFG_NAME
,
2192 &client
->dev
, GFP_KERNEL
, data
,
2195 dev_err(&client
->dev
, "Failed to invoke firmware loader: %d\n",
2203 static int mxt_set_t7_power_cfg(struct mxt_data
*data
, u8 sleep
)
2205 struct device
*dev
= &data
->client
->dev
;
2207 struct t7_config
*new_config
;
2208 struct t7_config deepsleep
= { .active
= 0, .idle
= 0 };
2210 if (sleep
== MXT_POWER_CFG_DEEPSLEEP
)
2211 new_config
= &deepsleep
;
2213 new_config
= &data
->t7_cfg
;
2215 error
= __mxt_write_reg(data
->client
, data
->T7_address
,
2216 sizeof(data
->t7_cfg
), new_config
);
2220 dev_dbg(dev
, "Set T7 ACTV:%d IDLE:%d\n",
2221 new_config
->active
, new_config
->idle
);
2226 static int mxt_init_t7_power_cfg(struct mxt_data
*data
)
2228 struct device
*dev
= &data
->client
->dev
;
2233 error
= __mxt_read_reg(data
->client
, data
->T7_address
,
2234 sizeof(data
->t7_cfg
), &data
->t7_cfg
);
2238 if (data
->t7_cfg
.active
== 0 || data
->t7_cfg
.idle
== 0) {
2240 dev_dbg(dev
, "T7 cfg zero, resetting\n");
2241 mxt_soft_reset(data
);
2245 dev_dbg(dev
, "T7 cfg zero after reset, overriding\n");
2246 data
->t7_cfg
.active
= 20;
2247 data
->t7_cfg
.idle
= 100;
2248 return mxt_set_t7_power_cfg(data
, MXT_POWER_CFG_RUN
);
2252 dev_dbg(dev
, "Initialized power cfg: ACTV %d, IDLE %d\n",
2253 data
->t7_cfg
.active
, data
->t7_cfg
.idle
);
2257 #ifdef CONFIG_TOUCHSCREEN_ATMEL_MXT_T37
2258 static const struct v4l2_file_operations mxt_video_fops
= {
2259 .owner
= THIS_MODULE
,
2260 .open
= v4l2_fh_open
,
2261 .release
= vb2_fop_release
,
2262 .unlocked_ioctl
= video_ioctl2
,
2263 .read
= vb2_fop_read
,
2264 .mmap
= vb2_fop_mmap
,
2265 .poll
= vb2_fop_poll
,
2268 static u16
mxt_get_debug_value(struct mxt_data
*data
, unsigned int x
,
2271 struct mxt_info
*info
= data
->info
;
2272 struct mxt_dbg
*dbg
= &data
->dbg
;
2273 unsigned int ofs
, page
;
2274 unsigned int col
= 0;
2275 unsigned int col_width
;
2277 if (info
->family_id
== MXT_FAMILY_1386
) {
2278 col_width
= info
->matrix_ysize
/ MXT1386_COLUMNS
;
2279 col
= y
/ col_width
;
2282 col_width
= info
->matrix_ysize
;
2285 ofs
= (y
+ (x
* col_width
)) * sizeof(u16
);
2286 page
= ofs
/ MXT_DIAGNOSTIC_SIZE
;
2287 ofs
%= MXT_DIAGNOSTIC_SIZE
;
2289 if (info
->family_id
== MXT_FAMILY_1386
)
2290 page
+= col
* MXT1386_PAGES_PER_COLUMN
;
2292 return get_unaligned_le16(&dbg
->t37_buf
[page
].data
[ofs
]);
2295 static int mxt_convert_debug_pages(struct mxt_data
*data
, u16
*outbuf
)
2297 struct mxt_dbg
*dbg
= &data
->dbg
;
2300 unsigned int i
, rx
, ry
;
2302 for (i
= 0; i
< dbg
->t37_nodes
; i
++) {
2303 /* Handle orientation */
2304 rx
= data
->xy_switch
? y
: x
;
2305 ry
= data
->xy_switch
? x
: y
;
2306 rx
= data
->invertx
? (data
->xsize
- 1 - rx
) : rx
;
2307 ry
= data
->inverty
? (data
->ysize
- 1 - ry
) : ry
;
2309 outbuf
[i
] = mxt_get_debug_value(data
, rx
, ry
);
2312 if (++x
>= (data
->xy_switch
? data
->ysize
: data
->xsize
)) {
2321 static int mxt_read_diagnostic_debug(struct mxt_data
*data
, u8 mode
,
2324 struct mxt_dbg
*dbg
= &data
->dbg
;
2329 struct t37_debug
*p
;
2332 for (page
= 0; page
< dbg
->t37_pages
; page
++) {
2333 p
= dbg
->t37_buf
+ page
;
2335 ret
= mxt_write_reg(data
->client
, dbg
->diag_cmd_address
,
2343 /* Read back command byte */
2344 ret
= __mxt_read_reg(data
->client
, dbg
->diag_cmd_address
,
2345 sizeof(cmd_poll
), &cmd_poll
);
2349 /* Field is cleared once the command has been processed */
2351 if (retries
++ > 100)
2359 ret
= __mxt_read_reg(data
->client
, dbg
->t37_address
,
2360 sizeof(struct t37_debug
), p
);
2364 if (p
->mode
!= mode
|| p
->page
!= page
) {
2365 dev_err(&data
->client
->dev
, "T37 page mismatch\n");
2369 dev_dbg(&data
->client
->dev
, "%s page:%d retries:%d\n",
2370 __func__
, page
, retries
);
2372 /* For remaining pages, write PAGEUP rather than mode */
2373 cmd
= MXT_DIAGNOSTIC_PAGEUP
;
2376 return mxt_convert_debug_pages(data
, outbuf
);
2379 static int mxt_queue_setup(struct vb2_queue
*q
,
2380 unsigned int *nbuffers
, unsigned int *nplanes
,
2381 unsigned int sizes
[], struct device
*alloc_devs
[])
2383 struct mxt_data
*data
= q
->drv_priv
;
2384 size_t size
= data
->dbg
.t37_nodes
* sizeof(u16
);
2387 return sizes
[0] < size
? -EINVAL
: 0;
2395 static void mxt_buffer_queue(struct vb2_buffer
*vb
)
2397 struct mxt_data
*data
= vb2_get_drv_priv(vb
->vb2_queue
);
2402 ptr
= vb2_plane_vaddr(vb
, 0);
2404 dev_err(&data
->client
->dev
, "Error acquiring frame ptr\n");
2408 switch (data
->dbg
.input
) {
2409 case MXT_V4L_INPUT_DELTAS
:
2411 mode
= MXT_DIAGNOSTIC_DELTAS
;
2414 case MXT_V4L_INPUT_REFS
:
2415 mode
= MXT_DIAGNOSTIC_REFS
;
2419 ret
= mxt_read_diagnostic_debug(data
, mode
, ptr
);
2423 vb2_set_plane_payload(vb
, 0, data
->dbg
.t37_nodes
* sizeof(u16
));
2424 vb2_buffer_done(vb
, VB2_BUF_STATE_DONE
);
2428 vb2_buffer_done(vb
, VB2_BUF_STATE_ERROR
);
2431 /* V4L2 structures */
2432 static const struct vb2_ops mxt_queue_ops
= {
2433 .queue_setup
= mxt_queue_setup
,
2434 .buf_queue
= mxt_buffer_queue
,
2435 .wait_prepare
= vb2_ops_wait_prepare
,
2436 .wait_finish
= vb2_ops_wait_finish
,
2439 static const struct vb2_queue mxt_queue
= {
2440 .type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
,
2441 .io_modes
= VB2_MMAP
| VB2_USERPTR
| VB2_DMABUF
| VB2_READ
,
2442 .buf_struct_size
= sizeof(struct mxt_vb2_buffer
),
2443 .ops
= &mxt_queue_ops
,
2444 .mem_ops
= &vb2_vmalloc_memops
,
2445 .timestamp_flags
= V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
,
2446 .min_buffers_needed
= 1,
2449 static int mxt_vidioc_querycap(struct file
*file
, void *priv
,
2450 struct v4l2_capability
*cap
)
2452 struct mxt_data
*data
= video_drvdata(file
);
2454 strlcpy(cap
->driver
, "atmel_mxt_ts", sizeof(cap
->driver
));
2455 strlcpy(cap
->card
, "atmel_mxt_ts touch", sizeof(cap
->card
));
2456 snprintf(cap
->bus_info
, sizeof(cap
->bus_info
),
2457 "I2C:%s", dev_name(&data
->client
->dev
));
2461 static int mxt_vidioc_enum_input(struct file
*file
, void *priv
,
2462 struct v4l2_input
*i
)
2464 if (i
->index
>= MXT_V4L_INPUT_MAX
)
2467 i
->type
= V4L2_INPUT_TYPE_TOUCH
;
2470 case MXT_V4L_INPUT_REFS
:
2471 strlcpy(i
->name
, "Mutual Capacitance References",
2474 case MXT_V4L_INPUT_DELTAS
:
2475 strlcpy(i
->name
, "Mutual Capacitance Deltas", sizeof(i
->name
));
2482 static int mxt_set_input(struct mxt_data
*data
, unsigned int i
)
2484 struct v4l2_pix_format
*f
= &data
->dbg
.format
;
2486 if (i
>= MXT_V4L_INPUT_MAX
)
2489 if (i
== MXT_V4L_INPUT_DELTAS
)
2490 f
->pixelformat
= V4L2_TCH_FMT_DELTA_TD16
;
2492 f
->pixelformat
= V4L2_TCH_FMT_TU16
;
2494 f
->width
= data
->xy_switch
? data
->ysize
: data
->xsize
;
2495 f
->height
= data
->xy_switch
? data
->xsize
: data
->ysize
;
2496 f
->field
= V4L2_FIELD_NONE
;
2497 f
->colorspace
= V4L2_COLORSPACE_RAW
;
2498 f
->bytesperline
= f
->width
* sizeof(u16
);
2499 f
->sizeimage
= f
->width
* f
->height
* sizeof(u16
);
2501 data
->dbg
.input
= i
;
2506 static int mxt_vidioc_s_input(struct file
*file
, void *priv
, unsigned int i
)
2508 return mxt_set_input(video_drvdata(file
), i
);
2511 static int mxt_vidioc_g_input(struct file
*file
, void *priv
, unsigned int *i
)
2513 struct mxt_data
*data
= video_drvdata(file
);
2515 *i
= data
->dbg
.input
;
2520 static int mxt_vidioc_fmt(struct file
*file
, void *priv
, struct v4l2_format
*f
)
2522 struct mxt_data
*data
= video_drvdata(file
);
2524 f
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
2525 f
->fmt
.pix
= data
->dbg
.format
;
2530 static int mxt_vidioc_enum_fmt(struct file
*file
, void *priv
,
2531 struct v4l2_fmtdesc
*fmt
)
2533 if (fmt
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
2536 switch (fmt
->index
) {
2538 fmt
->pixelformat
= V4L2_TCH_FMT_TU16
;
2542 fmt
->pixelformat
= V4L2_TCH_FMT_DELTA_TD16
;
2552 static int mxt_vidioc_g_parm(struct file
*file
, void *fh
,
2553 struct v4l2_streamparm
*a
)
2555 if (a
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
2558 a
->parm
.capture
.readbuffers
= 1;
2559 a
->parm
.capture
.timeperframe
.numerator
= 1;
2560 a
->parm
.capture
.timeperframe
.denominator
= 10;
2564 static const struct v4l2_ioctl_ops mxt_video_ioctl_ops
= {
2565 .vidioc_querycap
= mxt_vidioc_querycap
,
2567 .vidioc_enum_fmt_vid_cap
= mxt_vidioc_enum_fmt
,
2568 .vidioc_s_fmt_vid_cap
= mxt_vidioc_fmt
,
2569 .vidioc_g_fmt_vid_cap
= mxt_vidioc_fmt
,
2570 .vidioc_try_fmt_vid_cap
= mxt_vidioc_fmt
,
2571 .vidioc_g_parm
= mxt_vidioc_g_parm
,
2573 .vidioc_enum_input
= mxt_vidioc_enum_input
,
2574 .vidioc_g_input
= mxt_vidioc_g_input
,
2575 .vidioc_s_input
= mxt_vidioc_s_input
,
2577 .vidioc_reqbufs
= vb2_ioctl_reqbufs
,
2578 .vidioc_create_bufs
= vb2_ioctl_create_bufs
,
2579 .vidioc_querybuf
= vb2_ioctl_querybuf
,
2580 .vidioc_qbuf
= vb2_ioctl_qbuf
,
2581 .vidioc_dqbuf
= vb2_ioctl_dqbuf
,
2582 .vidioc_expbuf
= vb2_ioctl_expbuf
,
2584 .vidioc_streamon
= vb2_ioctl_streamon
,
2585 .vidioc_streamoff
= vb2_ioctl_streamoff
,
2588 static const struct video_device mxt_video_device
= {
2589 .name
= "Atmel maxTouch",
2590 .fops
= &mxt_video_fops
,
2591 .ioctl_ops
= &mxt_video_ioctl_ops
,
2592 .release
= video_device_release_empty
,
2593 .device_caps
= V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_TOUCH
|
2594 V4L2_CAP_READWRITE
| V4L2_CAP_STREAMING
,
2597 static void mxt_debug_init(struct mxt_data
*data
)
2599 struct mxt_info
*info
= data
->info
;
2600 struct mxt_dbg
*dbg
= &data
->dbg
;
2601 struct mxt_object
*object
;
2604 object
= mxt_get_object(data
, MXT_GEN_COMMAND_T6
);
2608 dbg
->diag_cmd_address
= object
->start_address
+ MXT_COMMAND_DIAGNOSTIC
;
2610 object
= mxt_get_object(data
, MXT_DEBUG_DIAGNOSTIC_T37
);
2614 if (mxt_obj_size(object
) != sizeof(struct t37_debug
)) {
2615 dev_warn(&data
->client
->dev
, "Bad T37 size");
2619 dbg
->t37_address
= object
->start_address
;
2621 /* Calculate size of data and allocate buffer */
2622 dbg
->t37_nodes
= data
->xsize
* data
->ysize
;
2624 if (info
->family_id
== MXT_FAMILY_1386
)
2625 dbg
->t37_pages
= MXT1386_COLUMNS
* MXT1386_PAGES_PER_COLUMN
;
2627 dbg
->t37_pages
= DIV_ROUND_UP(data
->xsize
*
2628 info
->matrix_ysize
*
2630 sizeof(dbg
->t37_buf
->data
));
2632 dbg
->t37_buf
= devm_kmalloc_array(&data
->client
->dev
, dbg
->t37_pages
,
2633 sizeof(struct t37_debug
), GFP_KERNEL
);
2637 /* init channel to zero */
2638 mxt_set_input(data
, 0);
2640 /* register video device */
2641 snprintf(dbg
->v4l2
.name
, sizeof(dbg
->v4l2
.name
), "%s", "atmel_mxt_ts");
2642 error
= v4l2_device_register(&data
->client
->dev
, &dbg
->v4l2
);
2646 /* initialize the queue */
2647 mutex_init(&dbg
->lock
);
2648 dbg
->queue
= mxt_queue
;
2649 dbg
->queue
.drv_priv
= data
;
2650 dbg
->queue
.lock
= &dbg
->lock
;
2651 dbg
->queue
.dev
= &data
->client
->dev
;
2653 error
= vb2_queue_init(&dbg
->queue
);
2655 goto error_unreg_v4l2
;
2657 dbg
->vdev
= mxt_video_device
;
2658 dbg
->vdev
.v4l2_dev
= &dbg
->v4l2
;
2659 dbg
->vdev
.lock
= &dbg
->lock
;
2660 dbg
->vdev
.vfl_dir
= VFL_DIR_RX
;
2661 dbg
->vdev
.queue
= &dbg
->queue
;
2662 video_set_drvdata(&dbg
->vdev
, data
);
2664 error
= video_register_device(&dbg
->vdev
, VFL_TYPE_TOUCH
, -1);
2666 goto error_unreg_v4l2
;
2671 v4l2_device_unregister(&dbg
->v4l2
);
2673 dev_warn(&data
->client
->dev
, "Error initializing T37\n");
2676 static void mxt_debug_init(struct mxt_data
*data
)
2681 static int mxt_configure_objects(struct mxt_data
*data
,
2682 const struct firmware
*cfg
)
2684 struct device
*dev
= &data
->client
->dev
;
2687 error
= mxt_init_t7_power_cfg(data
);
2689 dev_err(dev
, "Failed to initialize power cfg\n");
2694 error
= mxt_update_cfg(data
, cfg
);
2696 dev_warn(dev
, "Error %d updating config\n", error
);
2699 if (data
->multitouch
) {
2700 error
= mxt_initialize_input_device(data
);
2704 dev_warn(dev
, "No touch object detected\n");
2707 mxt_debug_init(data
);
2712 /* Firmware Version is returned as Major.Minor.Build */
2713 static ssize_t
mxt_fw_version_show(struct device
*dev
,
2714 struct device_attribute
*attr
, char *buf
)
2716 struct mxt_data
*data
= dev_get_drvdata(dev
);
2717 struct mxt_info
*info
= data
->info
;
2718 return scnprintf(buf
, PAGE_SIZE
, "%u.%u.%02X\n",
2719 info
->version
>> 4, info
->version
& 0xf, info
->build
);
2722 /* Hardware Version is returned as FamilyID.VariantID */
2723 static ssize_t
mxt_hw_version_show(struct device
*dev
,
2724 struct device_attribute
*attr
, char *buf
)
2726 struct mxt_data
*data
= dev_get_drvdata(dev
);
2727 struct mxt_info
*info
= data
->info
;
2728 return scnprintf(buf
, PAGE_SIZE
, "%u.%u\n",
2729 info
->family_id
, info
->variant_id
);
2732 static ssize_t
mxt_show_instance(char *buf
, int count
,
2733 struct mxt_object
*object
, int instance
,
2738 if (mxt_obj_instances(object
) > 1)
2739 count
+= scnprintf(buf
+ count
, PAGE_SIZE
- count
,
2740 "Instance %u\n", instance
);
2742 for (i
= 0; i
< mxt_obj_size(object
); i
++)
2743 count
+= scnprintf(buf
+ count
, PAGE_SIZE
- count
,
2744 "\t[%2u]: %02x (%d)\n", i
, val
[i
], val
[i
]);
2745 count
+= scnprintf(buf
+ count
, PAGE_SIZE
- count
, "\n");
2750 static ssize_t
mxt_object_show(struct device
*dev
,
2751 struct device_attribute
*attr
, char *buf
)
2753 struct mxt_data
*data
= dev_get_drvdata(dev
);
2754 struct mxt_object
*object
;
2760 /* Pre-allocate buffer large enough to hold max sized object. */
2761 obuf
= kmalloc(256, GFP_KERNEL
);
2766 for (i
= 0; i
< data
->info
->object_num
; i
++) {
2767 object
= data
->object_table
+ i
;
2769 if (!mxt_object_readable(object
->type
))
2772 count
+= scnprintf(buf
+ count
, PAGE_SIZE
- count
,
2773 "T%u:\n", object
->type
);
2775 for (j
= 0; j
< mxt_obj_instances(object
); j
++) {
2776 u16 size
= mxt_obj_size(object
);
2777 u16 addr
= object
->start_address
+ j
* size
;
2779 error
= __mxt_read_reg(data
->client
, addr
, size
, obuf
);
2783 count
= mxt_show_instance(buf
, count
, object
, j
, obuf
);
2789 return error
?: count
;
2792 static int mxt_check_firmware_format(struct device
*dev
,
2793 const struct firmware
*fw
)
2795 unsigned int pos
= 0;
2798 while (pos
< fw
->size
) {
2799 c
= *(fw
->data
+ pos
);
2801 if (c
< '0' || (c
> '9' && c
< 'A') || c
> 'F')
2808 * To convert file try:
2809 * xxd -r -p mXTXXX__APP_VX-X-XX.enc > maxtouch.fw
2811 dev_err(dev
, "Aborting: firmware file must be in binary format\n");
2816 static int mxt_load_fw(struct device
*dev
, const char *fn
)
2818 struct mxt_data
*data
= dev_get_drvdata(dev
);
2819 const struct firmware
*fw
= NULL
;
2820 unsigned int frame_size
;
2821 unsigned int pos
= 0;
2822 unsigned int retry
= 0;
2823 unsigned int frame
= 0;
2826 ret
= request_firmware(&fw
, fn
, dev
);
2828 dev_err(dev
, "Unable to open firmware %s\n", fn
);
2832 /* Check for incorrect enc file */
2833 ret
= mxt_check_firmware_format(dev
, fw
);
2835 goto release_firmware
;
2837 if (!data
->in_bootloader
) {
2838 /* Change to the bootloader mode */
2839 data
->in_bootloader
= true;
2841 ret
= mxt_t6_command(data
, MXT_COMMAND_RESET
,
2842 MXT_BOOT_VALUE
, false);
2844 goto release_firmware
;
2846 msleep(MXT_RESET_TIME
);
2848 /* Do not need to scan since we know family ID */
2849 ret
= mxt_lookup_bootloader_address(data
, 0);
2851 goto release_firmware
;
2853 mxt_free_input_device(data
);
2854 mxt_free_object_table(data
);
2856 enable_irq(data
->irq
);
2859 reinit_completion(&data
->bl_completion
);
2861 ret
= mxt_check_bootloader(data
, MXT_WAITING_BOOTLOAD_CMD
, false);
2863 /* Bootloader may still be unlocked from previous attempt */
2864 ret
= mxt_check_bootloader(data
, MXT_WAITING_FRAME_DATA
, false);
2868 dev_info(dev
, "Unlocking bootloader\n");
2870 /* Unlock bootloader */
2871 ret
= mxt_send_bootloader_cmd(data
, true);
2876 while (pos
< fw
->size
) {
2877 ret
= mxt_check_bootloader(data
, MXT_WAITING_FRAME_DATA
, true);
2881 frame_size
= ((*(fw
->data
+ pos
) << 8) | *(fw
->data
+ pos
+ 1));
2883 /* Take account of CRC bytes */
2886 /* Write one frame to device */
2887 ret
= mxt_bootloader_write(data
, fw
->data
+ pos
, frame_size
);
2891 ret
= mxt_check_bootloader(data
, MXT_FRAME_CRC_PASS
, true);
2895 /* Back off by 20ms per retry */
2899 dev_err(dev
, "Retry count exceeded\n");
2908 if (frame
% 50 == 0)
2909 dev_dbg(dev
, "Sent %d frames, %d/%zd bytes\n",
2910 frame
, pos
, fw
->size
);
2913 /* Wait for flash. */
2914 ret
= mxt_wait_for_completion(data
, &data
->bl_completion
,
2919 dev_dbg(dev
, "Sent %d frames, %d bytes\n", frame
, pos
);
2922 * Wait for device to reset. Some bootloader versions do not assert
2923 * the CHG line after bootloading has finished, so ignore potential
2926 mxt_wait_for_completion(data
, &data
->bl_completion
, MXT_FW_RESET_TIME
);
2928 data
->in_bootloader
= false;
2931 disable_irq(data
->irq
);
2933 release_firmware(fw
);
2937 static ssize_t
mxt_update_fw_store(struct device
*dev
,
2938 struct device_attribute
*attr
,
2939 const char *buf
, size_t count
)
2941 struct mxt_data
*data
= dev_get_drvdata(dev
);
2944 error
= mxt_load_fw(dev
, MXT_FW_NAME
);
2946 dev_err(dev
, "The firmware update failed(%d)\n", error
);
2949 dev_info(dev
, "The firmware update succeeded\n");
2951 error
= mxt_initialize(data
);
2959 static DEVICE_ATTR(fw_version
, S_IRUGO
, mxt_fw_version_show
, NULL
);
2960 static DEVICE_ATTR(hw_version
, S_IRUGO
, mxt_hw_version_show
, NULL
);
2961 static DEVICE_ATTR(object
, S_IRUGO
, mxt_object_show
, NULL
);
2962 static DEVICE_ATTR(update_fw
, S_IWUSR
, NULL
, mxt_update_fw_store
);
2964 static struct attribute
*mxt_attrs
[] = {
2965 &dev_attr_fw_version
.attr
,
2966 &dev_attr_hw_version
.attr
,
2967 &dev_attr_object
.attr
,
2968 &dev_attr_update_fw
.attr
,
2972 static const struct attribute_group mxt_attr_group
= {
2976 static void mxt_start(struct mxt_data
*data
)
2978 switch (data
->suspend_mode
) {
2979 case MXT_SUSPEND_T9_CTRL
:
2980 mxt_soft_reset(data
);
2983 /* 0x83 = SCANEN | RPTEN | ENABLE */
2984 mxt_write_object(data
,
2985 MXT_TOUCH_MULTI_T9
, MXT_T9_CTRL
, 0x83);
2988 case MXT_SUSPEND_DEEP_SLEEP
:
2990 mxt_set_t7_power_cfg(data
, MXT_POWER_CFG_RUN
);
2992 /* Recalibrate since chip has been in deep sleep */
2993 mxt_t6_command(data
, MXT_COMMAND_CALIBRATE
, 1, false);
2998 static void mxt_stop(struct mxt_data
*data
)
3000 switch (data
->suspend_mode
) {
3001 case MXT_SUSPEND_T9_CTRL
:
3003 mxt_write_object(data
,
3004 MXT_TOUCH_MULTI_T9
, MXT_T9_CTRL
, 0);
3007 case MXT_SUSPEND_DEEP_SLEEP
:
3009 mxt_set_t7_power_cfg(data
, MXT_POWER_CFG_DEEPSLEEP
);
3014 static int mxt_input_open(struct input_dev
*dev
)
3016 struct mxt_data
*data
= input_get_drvdata(dev
);
3023 static void mxt_input_close(struct input_dev
*dev
)
3025 struct mxt_data
*data
= input_get_drvdata(dev
);
3030 static int mxt_parse_device_properties(struct mxt_data
*data
)
3032 static const char keymap_property
[] = "linux,gpio-keymap";
3033 struct device
*dev
= &data
->client
->dev
;
3038 if (device_property_present(dev
, keymap_property
)) {
3039 n_keys
= device_property_count_u32(dev
, keymap_property
);
3041 error
= n_keys
< 0 ? n_keys
: -EINVAL
;
3042 dev_err(dev
, "invalid/malformed '%s' property: %d\n",
3043 keymap_property
, error
);
3047 keymap
= devm_kmalloc_array(dev
, n_keys
, sizeof(*keymap
),
3052 error
= device_property_read_u32_array(dev
, keymap_property
,
3055 dev_err(dev
, "failed to parse '%s' property: %d\n",
3056 keymap_property
, error
);
3060 data
->t19_keymap
= keymap
;
3061 data
->t19_num_keys
= n_keys
;
3067 static const struct dmi_system_id chromebook_T9_suspend_dmi
[] = {
3070 DMI_MATCH(DMI_SYS_VENDOR
, "GOOGLE"),
3071 DMI_MATCH(DMI_PRODUCT_NAME
, "Link"),
3076 DMI_MATCH(DMI_PRODUCT_NAME
, "Peppy"),
3082 static int mxt_probe(struct i2c_client
*client
, const struct i2c_device_id
*id
)
3084 struct mxt_data
*data
;
3088 * Ignore devices that do not have device properties attached to
3089 * them, as we need help determining whether we are dealing with
3090 * touch screen or touchpad.
3092 * So far on x86 the only users of Atmel touch controllers are
3093 * Chromebooks, and chromeos_laptop driver will ensure that
3094 * necessary properties are provided (if firmware does not do that).
3096 if (!device_property_present(&client
->dev
, "compatible"))
3100 * Ignore ACPI devices representing bootloader mode.
3102 * This is a bit of a hack: Google Chromebook BIOS creates ACPI
3103 * devices for both application and bootloader modes, but we are
3104 * interested in application mode only (if device is in bootloader
3105 * mode we'll end up switching into application anyway). So far
3106 * application mode addresses were all above 0x40, so we'll use it
3109 if (ACPI_COMPANION(&client
->dev
) && client
->addr
< 0x40)
3112 data
= devm_kzalloc(&client
->dev
, sizeof(struct mxt_data
), GFP_KERNEL
);
3116 snprintf(data
->phys
, sizeof(data
->phys
), "i2c-%u-%04x/input0",
3117 client
->adapter
->nr
, client
->addr
);
3119 data
->client
= client
;
3120 data
->irq
= client
->irq
;
3121 i2c_set_clientdata(client
, data
);
3123 init_completion(&data
->bl_completion
);
3124 init_completion(&data
->reset_completion
);
3125 init_completion(&data
->crc_completion
);
3127 data
->suspend_mode
= dmi_check_system(chromebook_T9_suspend_dmi
) ?
3128 MXT_SUSPEND_T9_CTRL
: MXT_SUSPEND_DEEP_SLEEP
;
3130 error
= mxt_parse_device_properties(data
);
3135 * VDDA is the analog voltage supply 2.57..3.47 V
3136 * VDD is the digital voltage supply 1.71..3.47 V
3138 data
->regulators
[0].supply
= "vdda";
3139 data
->regulators
[1].supply
= "vdd";
3140 error
= devm_regulator_bulk_get(&client
->dev
, ARRAY_SIZE(data
->regulators
),
3143 if (error
!= -EPROBE_DEFER
)
3144 dev_err(&client
->dev
, "Failed to get regulators %d\n",
3149 /* Request the RESET line as asserted so we go into reset */
3150 data
->reset_gpio
= devm_gpiod_get_optional(&client
->dev
,
3151 "reset", GPIOD_OUT_HIGH
);
3152 if (IS_ERR(data
->reset_gpio
)) {
3153 error
= PTR_ERR(data
->reset_gpio
);
3154 dev_err(&client
->dev
, "Failed to get reset gpio: %d\n", error
);
3158 error
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
3159 NULL
, mxt_interrupt
, IRQF_ONESHOT
,
3160 client
->name
, data
);
3162 dev_err(&client
->dev
, "Failed to register interrupt\n");
3166 disable_irq(client
->irq
);
3168 error
= regulator_bulk_enable(ARRAY_SIZE(data
->regulators
),
3171 dev_err(&client
->dev
, "failed to enable regulators: %d\n",
3176 * The device takes 40ms to come up after power-on according
3177 * to the mXT224 datasheet, page 13.
3179 msleep(MXT_BACKUP_TIME
);
3181 if (data
->reset_gpio
) {
3182 /* Wait a while and then de-assert the RESET GPIO line */
3183 msleep(MXT_RESET_GPIO_TIME
);
3184 gpiod_set_value(data
->reset_gpio
, 0);
3185 msleep(MXT_RESET_INVALID_CHG
);
3188 error
= mxt_initialize(data
);
3190 goto err_disable_regulators
;
3192 error
= sysfs_create_group(&client
->dev
.kobj
, &mxt_attr_group
);
3194 dev_err(&client
->dev
, "Failure %d creating sysfs group\n",
3196 goto err_free_object
;
3202 mxt_free_input_device(data
);
3203 mxt_free_object_table(data
);
3204 err_disable_regulators
:
3205 regulator_bulk_disable(ARRAY_SIZE(data
->regulators
),
3210 static int mxt_remove(struct i2c_client
*client
)
3212 struct mxt_data
*data
= i2c_get_clientdata(client
);
3214 disable_irq(data
->irq
);
3215 sysfs_remove_group(&client
->dev
.kobj
, &mxt_attr_group
);
3216 mxt_free_input_device(data
);
3217 mxt_free_object_table(data
);
3218 regulator_bulk_disable(ARRAY_SIZE(data
->regulators
),
3224 static int __maybe_unused
mxt_suspend(struct device
*dev
)
3226 struct i2c_client
*client
= to_i2c_client(dev
);
3227 struct mxt_data
*data
= i2c_get_clientdata(client
);
3228 struct input_dev
*input_dev
= data
->input_dev
;
3233 mutex_lock(&input_dev
->mutex
);
3235 if (input_device_enabled(input_dev
))
3238 mutex_unlock(&input_dev
->mutex
);
3240 disable_irq(data
->irq
);
3245 static int __maybe_unused
mxt_resume(struct device
*dev
)
3247 struct i2c_client
*client
= to_i2c_client(dev
);
3248 struct mxt_data
*data
= i2c_get_clientdata(client
);
3249 struct input_dev
*input_dev
= data
->input_dev
;
3254 enable_irq(data
->irq
);
3256 mutex_lock(&input_dev
->mutex
);
3258 if (input_device_enabled(input_dev
))
3261 mutex_unlock(&input_dev
->mutex
);
3266 static SIMPLE_DEV_PM_OPS(mxt_pm_ops
, mxt_suspend
, mxt_resume
);
3268 static const struct of_device_id mxt_of_match
[] = {
3269 { .compatible
= "atmel,maxtouch", },
3270 /* Compatibles listed below are deprecated */
3271 { .compatible
= "atmel,qt602240_ts", },
3272 { .compatible
= "atmel,atmel_mxt_ts", },
3273 { .compatible
= "atmel,atmel_mxt_tp", },
3274 { .compatible
= "atmel,mXT224", },
3277 MODULE_DEVICE_TABLE(of
, mxt_of_match
);
3280 static const struct acpi_device_id mxt_acpi_id
[] = {
3281 { "ATML0000", 0 }, /* Touchpad */
3282 { "ATML0001", 0 }, /* Touchscreen */
3285 MODULE_DEVICE_TABLE(acpi
, mxt_acpi_id
);
3288 static const struct i2c_device_id mxt_id
[] = {
3289 { "qt602240_ts", 0 },
3290 { "atmel_mxt_ts", 0 },
3291 { "atmel_mxt_tp", 0 },
3296 MODULE_DEVICE_TABLE(i2c
, mxt_id
);
3298 static struct i2c_driver mxt_driver
= {
3300 .name
= "atmel_mxt_ts",
3301 .of_match_table
= mxt_of_match
,
3302 .acpi_match_table
= ACPI_PTR(mxt_acpi_id
),
3306 .remove
= mxt_remove
,
3310 module_i2c_driver(mxt_driver
);
3312 /* Module information */
3313 MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
3314 MODULE_DESCRIPTION("Atmel maXTouch Touchscreen driver");
3315 MODULE_LICENSE("GPL");