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 <asm/unaligned.h>
32 #include <media/v4l2-device.h>
33 #include <media/v4l2-ioctl.h>
34 #include <media/videobuf2-v4l2.h>
35 #include <media/videobuf2-vmalloc.h>
38 #define MXT_FW_NAME "/*(DEBLOBBED)*/"
39 #define MXT_CFG_NAME "maxtouch.cfg"
40 #define MXT_CFG_MAGIC "OBP_RAW V1"
43 #define MXT_OBJECT_START 0x07
44 #define MXT_OBJECT_SIZE 6
45 #define MXT_INFO_CHECKSUM_SIZE 3
46 #define MXT_MAX_BLOCK_WRITE 256
49 #define MXT_DEBUG_DIAGNOSTIC_T37 37
50 #define MXT_GEN_MESSAGE_T5 5
51 #define MXT_GEN_COMMAND_T6 6
52 #define MXT_GEN_POWER_T7 7
53 #define MXT_GEN_ACQUIRE_T8 8
54 #define MXT_GEN_DATASOURCE_T53 53
55 #define MXT_TOUCH_MULTI_T9 9
56 #define MXT_TOUCH_KEYARRAY_T15 15
57 #define MXT_TOUCH_PROXIMITY_T23 23
58 #define MXT_TOUCH_PROXKEY_T52 52
59 #define MXT_PROCI_GRIPFACE_T20 20
60 #define MXT_PROCG_NOISE_T22 22
61 #define MXT_PROCI_ONETOUCH_T24 24
62 #define MXT_PROCI_TWOTOUCH_T27 27
63 #define MXT_PROCI_GRIP_T40 40
64 #define MXT_PROCI_PALM_T41 41
65 #define MXT_PROCI_TOUCHSUPPRESSION_T42 42
66 #define MXT_PROCI_STYLUS_T47 47
67 #define MXT_PROCG_NOISESUPPRESSION_T48 48
68 #define MXT_SPT_COMMSCONFIG_T18 18
69 #define MXT_SPT_GPIOPWM_T19 19
70 #define MXT_SPT_SELFTEST_T25 25
71 #define MXT_SPT_CTECONFIG_T28 28
72 #define MXT_SPT_USERDATA_T38 38
73 #define MXT_SPT_DIGITIZER_T43 43
74 #define MXT_SPT_MESSAGECOUNT_T44 44
75 #define MXT_SPT_CTECONFIG_T46 46
76 #define MXT_TOUCH_MULTITOUCHSCREEN_T100 100
78 /* MXT_GEN_MESSAGE_T5 object */
79 #define MXT_RPTID_NOMSG 0xff
81 /* MXT_GEN_COMMAND_T6 field */
82 #define MXT_COMMAND_RESET 0
83 #define MXT_COMMAND_BACKUPNV 1
84 #define MXT_COMMAND_CALIBRATE 2
85 #define MXT_COMMAND_REPORTALL 3
86 #define MXT_COMMAND_DIAGNOSTIC 5
88 /* Define for T6 status byte */
89 #define MXT_T6_STATUS_RESET (1 << 7)
90 #define MXT_T6_STATUS_OFL (1 << 6)
91 #define MXT_T6_STATUS_SIGERR (1 << 5)
92 #define MXT_T6_STATUS_CAL (1 << 4)
93 #define MXT_T6_STATUS_CFGERR (1 << 3)
94 #define MXT_T6_STATUS_COMSERR (1 << 2)
96 /* MXT_GEN_POWER_T7 field */
102 #define MXT_POWER_CFG_RUN 0
103 #define MXT_POWER_CFG_DEEPSLEEP 1
105 /* MXT_TOUCH_MULTI_T9 field */
106 #define MXT_T9_CTRL 0
107 #define MXT_T9_XSIZE 3
108 #define MXT_T9_YSIZE 4
109 #define MXT_T9_ORIENT 9
110 #define MXT_T9_RANGE 18
112 /* MXT_TOUCH_MULTI_T9 status */
113 #define MXT_T9_UNGRIP (1 << 0)
114 #define MXT_T9_SUPPRESS (1 << 1)
115 #define MXT_T9_AMP (1 << 2)
116 #define MXT_T9_VECTOR (1 << 3)
117 #define MXT_T9_MOVE (1 << 4)
118 #define MXT_T9_RELEASE (1 << 5)
119 #define MXT_T9_PRESS (1 << 6)
120 #define MXT_T9_DETECT (1 << 7)
127 /* MXT_TOUCH_MULTI_T9 orient */
128 #define MXT_T9_ORIENT_SWITCH (1 << 0)
129 #define MXT_T9_ORIENT_INVERTX (1 << 1)
130 #define MXT_T9_ORIENT_INVERTY (1 << 2)
132 /* MXT_SPT_COMMSCONFIG_T18 */
133 #define MXT_COMMS_CTRL 0
134 #define MXT_COMMS_CMD 1
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_TIME 200 /* msec */
196 #define MXT_RESET_TIMEOUT 3000 /* msec */
197 #define MXT_CRC_TIMEOUT 1000 /* msec */
198 #define MXT_FW_RESET_TIME 3000 /* msec */
199 #define MXT_FW_CHG_TIMEOUT 300 /* msec */
201 /* Command to unlock bootloader */
202 #define MXT_UNLOCK_CMD_MSB 0xaa
203 #define MXT_UNLOCK_CMD_LSB 0xdc
205 /* Bootloader mode status */
206 #define MXT_WAITING_BOOTLOAD_CMD 0xc0 /* valid 7 6 bit only */
207 #define MXT_WAITING_FRAME_DATA 0x80 /* valid 7 6 bit only */
208 #define MXT_FRAME_CRC_CHECK 0x02
209 #define MXT_FRAME_CRC_FAIL 0x03
210 #define MXT_FRAME_CRC_PASS 0x04
211 #define MXT_APP_CRC_FAIL 0x40 /* valid 7 8 bit only */
212 #define MXT_BOOT_STATUS_MASK 0x3f
213 #define MXT_BOOT_EXTENDED_ID (1 << 5)
214 #define MXT_BOOT_ID_MASK 0x1f
216 /* Touchscreen absolute values */
217 #define MXT_MAX_AREA 0xff
219 #define MXT_PIXELS_PER_MM 20
235 u8 instances_minus_one;
241 u16 diag_cmd_address;
242 struct t37_debug *t37_buf;
243 unsigned int t37_pages;
244 unsigned int t37_nodes;
246 struct v4l2_device v4l2;
247 struct v4l2_pix_format format;
248 struct video_device vdev;
249 struct vb2_queue queue;
254 enum v4l_dbg_inputs {
255 MXT_V4L_INPUT_DELTAS,
260 static const struct v4l2_file_operations mxt_video_fops = {
261 .owner = THIS_MODULE,
262 .open = v4l2_fh_open,
263 .release = vb2_fop_release,
264 .unlocked_ioctl = video_ioctl2,
265 .read = vb2_fop_read,
266 .mmap = vb2_fop_mmap,
267 .poll = vb2_fop_poll,
270 /* Each client has this additional data */
272 struct i2c_client *client;
273 struct input_dev *input_dev;
274 char phys[64]; /* device physical location */
275 const struct mxt_platform_data *pdata;
276 struct mxt_object *object_table;
277 struct mxt_info info;
298 u8 last_message_count;
301 struct t7_config t7_cfg;
304 /* Cached parameters from object table */
314 u8 T100_reportid_min;
315 u8 T100_reportid_max;
317 /* for fw update in bootloader */
318 struct completion bl_completion;
320 /* for reset handling */
321 struct completion reset_completion;
323 /* for config update handling */
324 struct completion crc_completion;
327 struct mxt_vb2_buffer {
328 struct vb2_buffer vb;
329 struct list_head list;
332 static size_t mxt_obj_size(const struct mxt_object *obj)
334 return obj->size_minus_one + 1;
337 static size_t mxt_obj_instances(const struct mxt_object *obj)
339 return obj->instances_minus_one + 1;
342 static bool mxt_object_readable(unsigned int type)
345 case MXT_GEN_COMMAND_T6:
346 case MXT_GEN_POWER_T7:
347 case MXT_GEN_ACQUIRE_T8:
348 case MXT_GEN_DATASOURCE_T53:
349 case MXT_TOUCH_MULTI_T9:
350 case MXT_TOUCH_KEYARRAY_T15:
351 case MXT_TOUCH_PROXIMITY_T23:
352 case MXT_TOUCH_PROXKEY_T52:
353 case MXT_PROCI_GRIPFACE_T20:
354 case MXT_PROCG_NOISE_T22:
355 case MXT_PROCI_ONETOUCH_T24:
356 case MXT_PROCI_TWOTOUCH_T27:
357 case MXT_PROCI_GRIP_T40:
358 case MXT_PROCI_PALM_T41:
359 case MXT_PROCI_TOUCHSUPPRESSION_T42:
360 case MXT_PROCI_STYLUS_T47:
361 case MXT_PROCG_NOISESUPPRESSION_T48:
362 case MXT_SPT_COMMSCONFIG_T18:
363 case MXT_SPT_GPIOPWM_T19:
364 case MXT_SPT_SELFTEST_T25:
365 case MXT_SPT_CTECONFIG_T28:
366 case MXT_SPT_USERDATA_T38:
367 case MXT_SPT_DIGITIZER_T43:
368 case MXT_SPT_CTECONFIG_T46:
375 static void mxt_dump_message(struct mxt_data *data, u8 *message)
377 dev_dbg(&data->client->dev, "message: %*ph\n",
378 data->T5_msg_size, message);
381 static int mxt_wait_for_completion(struct mxt_data *data,
382 struct completion *comp,
383 unsigned int timeout_ms)
385 struct device *dev = &data->client->dev;
386 unsigned long timeout = msecs_to_jiffies(timeout_ms);
389 ret = wait_for_completion_interruptible_timeout(comp, timeout);
392 } else if (ret == 0) {
393 dev_err(dev, "Wait for completion timed out.\n");
399 static int mxt_bootloader_read(struct mxt_data *data,
400 u8 *val, unsigned int count)
405 msg.addr = data->bootloader_addr;
406 msg.flags = data->client->flags & I2C_M_TEN;
407 msg.flags |= I2C_M_RD;
411 ret = i2c_transfer(data->client->adapter, &msg, 1);
415 ret = ret < 0 ? ret : -EIO;
416 dev_err(&data->client->dev, "%s: i2c recv failed (%d)\n",
423 static int mxt_bootloader_write(struct mxt_data *data,
424 const u8 * const val, unsigned int count)
429 msg.addr = data->bootloader_addr;
430 msg.flags = data->client->flags & I2C_M_TEN;
434 ret = i2c_transfer(data->client->adapter, &msg, 1);
438 ret = ret < 0 ? ret : -EIO;
439 dev_err(&data->client->dev, "%s: i2c send failed (%d)\n",
446 static int mxt_lookup_bootloader_address(struct mxt_data *data, bool retry)
448 u8 appmode = data->client->addr;
454 /* Chips after 1664S use different scheme */
455 if (retry || data->info.family_id >= 0xa2) {
456 bootloader = appmode - 0x24;
459 /* Fall through for normal case */
464 bootloader = appmode - 0x26;
468 dev_err(&data->client->dev,
469 "Appmode i2c address 0x%02x not found\n",
474 data->bootloader_addr = bootloader;
478 static int mxt_probe_bootloader(struct mxt_data *data, bool alt_address)
480 struct device *dev = &data->client->dev;
485 error = mxt_lookup_bootloader_address(data, alt_address);
489 error = mxt_bootloader_read(data, &val, 1);
493 /* Check app crc fail mode */
494 crc_failure = (val & ~MXT_BOOT_STATUS_MASK) == MXT_APP_CRC_FAIL;
496 dev_err(dev, "Detected bootloader, status:%02X%s\n",
497 val, crc_failure ? ", APP_CRC_FAIL" : "");
502 static u8 mxt_get_bootloader_version(struct mxt_data *data, u8 val)
504 struct device *dev = &data->client->dev;
507 if (val & MXT_BOOT_EXTENDED_ID) {
508 if (mxt_bootloader_read(data, &buf[0], 3) != 0) {
509 dev_err(dev, "%s: i2c failure\n", __func__);
513 dev_dbg(dev, "Bootloader ID:%d Version:%d\n", buf[1], buf[2]);
517 dev_dbg(dev, "Bootloader ID:%d\n", val & MXT_BOOT_ID_MASK);
523 static int mxt_check_bootloader(struct mxt_data *data, unsigned int state,
526 struct device *dev = &data->client->dev;
533 * In application update mode, the interrupt
534 * line signals state transitions. We must wait for the
535 * CHG assertion before reading the status byte.
536 * Once the status byte has been read, the line is deasserted.
538 ret = mxt_wait_for_completion(data, &data->bl_completion,
542 * TODO: handle -ERESTARTSYS better by terminating
543 * fw update process before returning to userspace
544 * by writing length 0x000 to device (iff we are in
545 * WAITING_FRAME_DATA state).
547 dev_err(dev, "Update wait error %d\n", ret);
552 ret = mxt_bootloader_read(data, &val, 1);
556 if (state == MXT_WAITING_BOOTLOAD_CMD)
557 val = mxt_get_bootloader_version(data, val);
560 case MXT_WAITING_BOOTLOAD_CMD:
561 case MXT_WAITING_FRAME_DATA:
562 case MXT_APP_CRC_FAIL:
563 val &= ~MXT_BOOT_STATUS_MASK;
565 case MXT_FRAME_CRC_PASS:
566 if (val == MXT_FRAME_CRC_CHECK) {
568 } else if (val == MXT_FRAME_CRC_FAIL) {
569 dev_err(dev, "Bootloader CRC fail\n");
578 dev_err(dev, "Invalid bootloader state %02X != %02X\n",
586 static int mxt_send_bootloader_cmd(struct mxt_data *data, bool unlock)
592 buf[0] = MXT_UNLOCK_CMD_LSB;
593 buf[1] = MXT_UNLOCK_CMD_MSB;
599 ret = mxt_bootloader_write(data, buf, 2);
606 static int __mxt_read_reg(struct i2c_client *client,
607 u16 reg, u16 len, void *val)
609 struct i2c_msg xfer[2];
614 buf[1] = (reg >> 8) & 0xff;
617 xfer[0].addr = client->addr;
623 xfer[1].addr = client->addr;
624 xfer[1].flags = I2C_M_RD;
628 ret = i2c_transfer(client->adapter, xfer, 2);
634 dev_err(&client->dev, "%s: i2c transfer failed (%d)\n",
641 static int __mxt_write_reg(struct i2c_client *client, u16 reg, u16 len,
649 buf = kmalloc(count, GFP_KERNEL);
654 buf[1] = (reg >> 8) & 0xff;
655 memcpy(&buf[2], val, len);
657 ret = i2c_master_send(client, buf, count);
663 dev_err(&client->dev, "%s: i2c send failed (%d)\n",
671 static int mxt_write_reg(struct i2c_client *client, u16 reg, u8 val)
673 return __mxt_write_reg(client, reg, 1, &val);
676 static struct mxt_object *
677 mxt_get_object(struct mxt_data *data, u8 type)
679 struct mxt_object *object;
682 for (i = 0; i < data->info.object_num; i++) {
683 object = data->object_table + i;
684 if (object->type == type)
688 dev_warn(&data->client->dev, "Invalid object type T%u\n", type);
692 static void mxt_proc_t6_messages(struct mxt_data *data, u8 *msg)
694 struct device *dev = &data->client->dev;
696 u32 crc = msg[2] | (msg[3] << 8) | (msg[4] << 16);
698 complete(&data->crc_completion);
700 if (crc != data->config_crc) {
701 data->config_crc = crc;
702 dev_dbg(dev, "T6 Config Checksum: 0x%06X\n", crc);
706 if (status & MXT_T6_STATUS_RESET)
707 complete(&data->reset_completion);
709 /* Output debug if status has changed */
710 if (status != data->t6_status)
711 dev_dbg(dev, "T6 Status 0x%02X%s%s%s%s%s%s%s\n",
713 status == 0 ? " OK" : "",
714 status & MXT_T6_STATUS_RESET ? " RESET" : "",
715 status & MXT_T6_STATUS_OFL ? " OFL" : "",
716 status & MXT_T6_STATUS_SIGERR ? " SIGERR" : "",
717 status & MXT_T6_STATUS_CAL ? " CAL" : "",
718 status & MXT_T6_STATUS_CFGERR ? " CFGERR" : "",
719 status & MXT_T6_STATUS_COMSERR ? " COMSERR" : "");
721 /* Save current status */
722 data->t6_status = status;
725 static int mxt_write_object(struct mxt_data *data,
726 u8 type, u8 offset, u8 val)
728 struct mxt_object *object;
731 object = mxt_get_object(data, type);
732 if (!object || offset >= mxt_obj_size(object))
735 reg = object->start_address;
736 return mxt_write_reg(data->client, reg + offset, val);
739 static void mxt_input_button(struct mxt_data *data, u8 *message)
741 struct input_dev *input = data->input_dev;
742 const struct mxt_platform_data *pdata = data->pdata;
745 for (i = 0; i < pdata->t19_num_keys; i++) {
746 if (pdata->t19_keymap[i] == KEY_RESERVED)
749 /* Active-low switch */
750 input_report_key(input, pdata->t19_keymap[i],
751 !(message[1] & BIT(i)));
755 static void mxt_input_sync(struct mxt_data *data)
757 input_mt_report_pointer_emulation(data->input_dev,
758 data->pdata->t19_num_keys);
759 input_sync(data->input_dev);
762 static void mxt_proc_t9_message(struct mxt_data *data, u8 *message)
764 struct device *dev = &data->client->dev;
765 struct input_dev *input_dev = data->input_dev;
773 id = message[0] - data->T9_reportid_min;
775 x = (message[2] << 4) | ((message[4] >> 4) & 0xf);
776 y = (message[3] << 4) | ((message[4] & 0xf));
778 /* Handle 10/12 bit switching */
779 if (data->max_x < 1024)
781 if (data->max_y < 1024)
785 amplitude = message[6];
788 "[%u] %c%c%c%c%c%c%c%c x: %5u y: %5u area: %3u amp: %3u\n",
790 (status & MXT_T9_DETECT) ? 'D' : '.',
791 (status & MXT_T9_PRESS) ? 'P' : '.',
792 (status & MXT_T9_RELEASE) ? 'R' : '.',
793 (status & MXT_T9_MOVE) ? 'M' : '.',
794 (status & MXT_T9_VECTOR) ? 'V' : '.',
795 (status & MXT_T9_AMP) ? 'A' : '.',
796 (status & MXT_T9_SUPPRESS) ? 'S' : '.',
797 (status & MXT_T9_UNGRIP) ? 'U' : '.',
798 x, y, area, amplitude);
800 input_mt_slot(input_dev, id);
802 if (status & MXT_T9_DETECT) {
804 * Multiple bits may be set if the host is slow to read
805 * the status messages, indicating all the events that
808 if (status & MXT_T9_RELEASE) {
809 input_mt_report_slot_state(input_dev,
811 mxt_input_sync(data);
815 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER, 1);
816 input_report_abs(input_dev, ABS_MT_POSITION_X, x);
817 input_report_abs(input_dev, ABS_MT_POSITION_Y, y);
818 input_report_abs(input_dev, ABS_MT_PRESSURE, amplitude);
819 input_report_abs(input_dev, ABS_MT_TOUCH_MAJOR, area);
821 /* Touch no longer active, close out slot */
822 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER, 0);
825 data->update_input = true;
828 static void mxt_proc_t100_message(struct mxt_data *data, u8 *message)
830 struct device *dev = &data->client->dev;
831 struct input_dev *input_dev = data->input_dev;
843 id = message[0] - data->T100_reportid_min - 2;
845 /* ignore SCRSTATUS events */
850 x = get_unaligned_le16(&message[2]);
851 y = get_unaligned_le16(&message[4]);
853 if (status & MXT_T100_DETECT) {
854 type = (status & MXT_T100_TYPE_MASK) >> 4;
857 case MXT_T100_TYPE_HOVERING_FINGER:
858 tool = MT_TOOL_FINGER;
859 distance = MXT_DISTANCE_HOVERING;
861 if (data->t100_aux_vect)
862 orientation = message[data->t100_aux_vect];
866 case MXT_T100_TYPE_FINGER:
867 case MXT_T100_TYPE_GLOVE:
868 tool = MT_TOOL_FINGER;
869 distance = MXT_DISTANCE_ACTIVE_TOUCH;
871 if (data->t100_aux_area)
872 major = message[data->t100_aux_area];
874 if (data->t100_aux_ampl)
875 pressure = message[data->t100_aux_ampl];
877 if (data->t100_aux_vect)
878 orientation = message[data->t100_aux_vect];
882 case MXT_T100_TYPE_PASSIVE_STYLUS:
886 * Passive stylus is reported with size zero so
889 major = MXT_TOUCH_MAJOR_DEFAULT;
891 if (data->t100_aux_ampl)
892 pressure = message[data->t100_aux_ampl];
896 case MXT_T100_TYPE_LARGE_TOUCH:
897 /* Ignore suppressed touch */
901 dev_dbg(dev, "Unexpected T100 type\n");
907 * Values reported should be non-zero if tool is touching the
910 if (!pressure && type != MXT_T100_TYPE_HOVERING_FINGER)
911 pressure = MXT_PRESSURE_DEFAULT;
913 input_mt_slot(input_dev, id);
915 if (status & MXT_T100_DETECT) {
916 dev_dbg(dev, "[%u] type:%u x:%u y:%u a:%02X p:%02X v:%02X\n",
917 id, type, x, y, major, pressure, orientation);
919 input_mt_report_slot_state(input_dev, tool, 1);
920 input_report_abs(input_dev, ABS_MT_POSITION_X, x);
921 input_report_abs(input_dev, ABS_MT_POSITION_Y, y);
922 input_report_abs(input_dev, ABS_MT_TOUCH_MAJOR, major);
923 input_report_abs(input_dev, ABS_MT_PRESSURE, pressure);
924 input_report_abs(input_dev, ABS_MT_DISTANCE, distance);
925 input_report_abs(input_dev, ABS_MT_ORIENTATION, orientation);
927 dev_dbg(dev, "[%u] release\n", id);
930 input_mt_report_slot_state(input_dev, 0, 0);
933 data->update_input = true;
936 static int mxt_proc_message(struct mxt_data *data, u8 *message)
938 u8 report_id = message[0];
940 if (report_id == MXT_RPTID_NOMSG)
943 if (report_id == data->T6_reportid) {
944 mxt_proc_t6_messages(data, message);
945 } else if (!data->input_dev) {
947 * Do not report events if input device
948 * is not yet registered.
950 mxt_dump_message(data, message);
951 } else if (report_id >= data->T9_reportid_min &&
952 report_id <= data->T9_reportid_max) {
953 mxt_proc_t9_message(data, message);
954 } else if (report_id >= data->T100_reportid_min &&
955 report_id <= data->T100_reportid_max) {
956 mxt_proc_t100_message(data, message);
957 } else if (report_id == data->T19_reportid) {
958 mxt_input_button(data, message);
959 data->update_input = true;
961 mxt_dump_message(data, message);
967 static int mxt_read_and_process_messages(struct mxt_data *data, u8 count)
969 struct device *dev = &data->client->dev;
974 /* Safety check for msg_buf */
975 if (count > data->max_reportid)
978 /* Process remaining messages if necessary */
979 ret = __mxt_read_reg(data->client, data->T5_address,
980 data->T5_msg_size * count, data->msg_buf);
982 dev_err(dev, "Failed to read %u messages (%d)\n", count, ret);
986 for (i = 0; i < count; i++) {
987 ret = mxt_proc_message(data,
988 data->msg_buf + data->T5_msg_size * i);
994 /* return number of messages read */
998 static irqreturn_t mxt_process_messages_t44(struct mxt_data *data)
1000 struct device *dev = &data->client->dev;
1004 /* Read T44 and T5 together */
1005 ret = __mxt_read_reg(data->client, data->T44_address,
1006 data->T5_msg_size + 1, data->msg_buf);
1008 dev_err(dev, "Failed to read T44 and T5 (%d)\n", ret);
1012 count = data->msg_buf[0];
1015 * This condition may be caused by the CHG line being configured in
1016 * Mode 0. It results in unnecessary I2C operations but it is benign.
1021 if (count > data->max_reportid) {
1022 dev_warn(dev, "T44 count %d exceeded max report id\n", count);
1023 count = data->max_reportid;
1026 /* Process first message */
1027 ret = mxt_proc_message(data, data->msg_buf + 1);
1029 dev_warn(dev, "Unexpected invalid message\n");
1033 num_left = count - 1;
1035 /* Process remaining messages if necessary */
1037 ret = mxt_read_and_process_messages(data, num_left);
1040 else if (ret != num_left)
1041 dev_warn(dev, "Unexpected invalid message\n");
1045 if (data->update_input) {
1046 mxt_input_sync(data);
1047 data->update_input = false;
1053 static int mxt_process_messages_until_invalid(struct mxt_data *data)
1055 struct device *dev = &data->client->dev;
1059 count = data->max_reportid;
1061 /* Read messages until we force an invalid */
1063 read = mxt_read_and_process_messages(data, count);
1068 if (data->update_input) {
1069 mxt_input_sync(data);
1070 data->update_input = false;
1073 dev_err(dev, "CHG pin isn't cleared\n");
1077 static irqreturn_t mxt_process_messages(struct mxt_data *data)
1079 int total_handled, num_handled;
1080 u8 count = data->last_message_count;
1082 if (count < 1 || count > data->max_reportid)
1085 /* include final invalid message */
1086 total_handled = mxt_read_and_process_messages(data, count + 1);
1087 if (total_handled < 0)
1089 /* if there were invalid messages, then we are done */
1090 else if (total_handled <= count)
1093 /* keep reading two msgs until one is invalid or reportid limit */
1095 num_handled = mxt_read_and_process_messages(data, 2);
1096 if (num_handled < 0)
1099 total_handled += num_handled;
1101 if (num_handled < 2)
1103 } while (total_handled < data->num_touchids);
1106 data->last_message_count = total_handled;
1108 if (data->update_input) {
1109 mxt_input_sync(data);
1110 data->update_input = false;
1116 static irqreturn_t mxt_interrupt(int irq, void *dev_id)
1118 struct mxt_data *data = dev_id;
1120 if (data->in_bootloader) {
1121 /* bootloader state transition completion */
1122 complete(&data->bl_completion);
1126 if (!data->object_table)
1129 if (data->T44_address) {
1130 return mxt_process_messages_t44(data);
1132 return mxt_process_messages(data);
1136 static int mxt_t6_command(struct mxt_data *data, u16 cmd_offset,
1137 u8 value, bool wait)
1140 u8 command_register;
1141 int timeout_counter = 0;
1144 reg = data->T6_address + cmd_offset;
1146 ret = mxt_write_reg(data->client, reg, value);
1155 ret = __mxt_read_reg(data->client, reg, 1, &command_register);
1158 } while (command_register != 0 && timeout_counter++ <= 100);
1160 if (timeout_counter > 100) {
1161 dev_err(&data->client->dev, "Command failed!\n");
1168 static int mxt_acquire_irq(struct mxt_data *data)
1172 enable_irq(data->irq);
1174 error = mxt_process_messages_until_invalid(data);
1181 static int mxt_soft_reset(struct mxt_data *data)
1183 struct device *dev = &data->client->dev;
1186 dev_info(dev, "Resetting device\n");
1188 disable_irq(data->irq);
1190 reinit_completion(&data->reset_completion);
1192 ret = mxt_t6_command(data, MXT_COMMAND_RESET, MXT_RESET_VALUE, false);
1196 /* Ignore CHG line for 100ms after reset */
1199 mxt_acquire_irq(data);
1201 ret = mxt_wait_for_completion(data, &data->reset_completion,
1209 static void mxt_update_crc(struct mxt_data *data, u8 cmd, u8 value)
1212 * On failure, CRC is set to 0 and config will always be
1215 data->config_crc = 0;
1216 reinit_completion(&data->crc_completion);
1218 mxt_t6_command(data, cmd, value, true);
1221 * Wait for crc message. On failure, CRC is set to 0 and config will
1222 * always be downloaded.
1224 mxt_wait_for_completion(data, &data->crc_completion, MXT_CRC_TIMEOUT);
1227 static void mxt_calc_crc24(u32 *crc, u8 firstbyte, u8 secondbyte)
1229 static const unsigned int crcpoly = 0x80001B;
1233 data_word = (secondbyte << 8) | firstbyte;
1234 result = ((*crc << 1) ^ data_word);
1236 if (result & 0x1000000)
1242 static u32 mxt_calculate_crc(u8 *base, off_t start_off, off_t end_off)
1245 u8 *ptr = base + start_off;
1246 u8 *last_val = base + end_off - 1;
1248 if (end_off < start_off)
1251 while (ptr < last_val) {
1252 mxt_calc_crc24(&crc, *ptr, *(ptr + 1));
1256 /* if len is odd, fill the last byte with 0 */
1257 if (ptr == last_val)
1258 mxt_calc_crc24(&crc, *ptr, 0);
1260 /* Mask to 24-bit */
1266 static int mxt_prepare_cfg_mem(struct mxt_data *data,
1267 const struct firmware *cfg,
1268 unsigned int data_pos,
1269 unsigned int cfg_start_ofs,
1271 size_t config_mem_size)
1273 struct device *dev = &data->client->dev;
1274 struct mxt_object *object;
1275 unsigned int type, instance, size, byte_offset;
1282 while (data_pos < cfg->size) {
1283 /* Read type, instance, length */
1284 ret = sscanf(cfg->data + data_pos, "%x %x %x%n",
1285 &type, &instance, &size, &offset);
1289 } else if (ret != 3) {
1290 dev_err(dev, "Bad format: failed to parse object\n");
1295 object = mxt_get_object(data, type);
1298 for (i = 0; i < size; i++) {
1299 ret = sscanf(cfg->data + data_pos, "%hhx%n",
1302 dev_err(dev, "Bad format in T%d at %d\n",
1311 if (size > mxt_obj_size(object)) {
1313 * Either we are in fallback mode due to wrong
1314 * config or config from a later fw version,
1315 * or the file is corrupt or hand-edited.
1317 dev_warn(dev, "Discarding %zu byte(s) in T%u\n",
1318 size - mxt_obj_size(object), type);
1319 } else if (mxt_obj_size(object) > size) {
1321 * If firmware is upgraded, new bytes may be added to
1322 * end of objects. It is generally forward compatible
1323 * to zero these bytes - previous behaviour will be
1324 * retained. However this does invalidate the CRC and
1325 * will force fallback mode until the configuration is
1326 * updated. We warn here but do nothing else - the
1327 * malloc has zeroed the entire configuration.
1329 dev_warn(dev, "Zeroing %zu byte(s) in T%d\n",
1330 mxt_obj_size(object) - size, type);
1333 if (instance >= mxt_obj_instances(object)) {
1334 dev_err(dev, "Object instances exceeded!\n");
1338 reg = object->start_address + mxt_obj_size(object) * instance;
1340 for (i = 0; i < size; i++) {
1341 ret = sscanf(cfg->data + data_pos, "%hhx%n",
1345 dev_err(dev, "Bad format in T%d at %d\n",
1351 if (i > mxt_obj_size(object))
1354 byte_offset = reg + i - cfg_start_ofs;
1356 if (byte_offset >= 0 && byte_offset < config_mem_size) {
1357 *(config_mem + byte_offset) = val;
1359 dev_err(dev, "Bad object: reg:%d, T%d, ofs=%d\n",
1360 reg, object->type, byte_offset);
1369 static int mxt_upload_cfg_mem(struct mxt_data *data, unsigned int cfg_start,
1370 u8 *config_mem, size_t config_mem_size)
1372 unsigned int byte_offset = 0;
1375 /* Write configuration as blocks */
1376 while (byte_offset < config_mem_size) {
1377 unsigned int size = config_mem_size - byte_offset;
1379 if (size > MXT_MAX_BLOCK_WRITE)
1380 size = MXT_MAX_BLOCK_WRITE;
1382 error = __mxt_write_reg(data->client,
1383 cfg_start + byte_offset,
1384 size, config_mem + byte_offset);
1386 dev_err(&data->client->dev,
1387 "Config write error, ret=%d\n", error);
1391 byte_offset += size;
1397 static int mxt_init_t7_power_cfg(struct mxt_data *data);
1400 * mxt_update_cfg - download configuration to chip
1402 * Atmel Raw Config File Format
1404 * The first four lines of the raw config file contain:
1406 * 2) Chip ID Information (first 7 bytes of device memory)
1407 * 3) Chip Information Block 24-bit CRC Checksum
1408 * 4) Chip Configuration 24-bit CRC Checksum
1410 * The rest of the file consists of one line per object instance:
1411 * <TYPE> <INSTANCE> <SIZE> <CONTENTS>
1413 * <TYPE> - 2-byte object type as hex
1414 * <INSTANCE> - 2-byte object instance number as hex
1415 * <SIZE> - 2-byte object size as hex
1416 * <CONTENTS> - array of <SIZE> 1-byte hex values
1418 static int mxt_update_cfg(struct mxt_data *data, const struct firmware *cfg)
1420 struct device *dev = &data->client->dev;
1421 struct mxt_info cfg_info;
1427 u32 info_crc, config_crc, calculated_crc;
1429 size_t config_mem_size;
1431 mxt_update_crc(data, MXT_COMMAND_REPORTALL, 1);
1433 if (strncmp(cfg->data, MXT_CFG_MAGIC, strlen(MXT_CFG_MAGIC))) {
1434 dev_err(dev, "Unrecognised config file\n");
1438 data_pos = strlen(MXT_CFG_MAGIC);
1440 /* Load information block and check */
1441 for (i = 0; i < sizeof(struct mxt_info); i++) {
1442 ret = sscanf(cfg->data + data_pos, "%hhx%n",
1443 (unsigned char *)&cfg_info + i,
1446 dev_err(dev, "Bad format\n");
1453 if (cfg_info.family_id != data->info.family_id) {
1454 dev_err(dev, "Family ID mismatch!\n");
1458 if (cfg_info.variant_id != data->info.variant_id) {
1459 dev_err(dev, "Variant ID mismatch!\n");
1464 ret = sscanf(cfg->data + data_pos, "%x%n", &info_crc, &offset);
1466 dev_err(dev, "Bad format: failed to parse Info CRC\n");
1471 ret = sscanf(cfg->data + data_pos, "%x%n", &config_crc, &offset);
1473 dev_err(dev, "Bad format: failed to parse Config CRC\n");
1479 * The Info Block CRC is calculated over mxt_info and the object
1480 * table. If it does not match then we are trying to load the
1481 * configuration from a different chip or firmware version, so
1482 * the configuration CRC is invalid anyway.
1484 if (info_crc == data->info_crc) {
1485 if (config_crc == 0 || data->config_crc == 0) {
1486 dev_info(dev, "CRC zero, attempting to apply config\n");
1487 } else if (config_crc == data->config_crc) {
1488 dev_dbg(dev, "Config CRC 0x%06X: OK\n",
1492 dev_info(dev, "Config CRC 0x%06X: does not match file 0x%06X\n",
1493 data->config_crc, config_crc);
1497 "Warning: Info CRC error - device=0x%06X file=0x%06X\n",
1498 data->info_crc, info_crc);
1501 /* Malloc memory to store configuration */
1502 cfg_start_ofs = MXT_OBJECT_START +
1503 data->info.object_num * sizeof(struct mxt_object) +
1504 MXT_INFO_CHECKSUM_SIZE;
1505 config_mem_size = data->mem_size - cfg_start_ofs;
1506 config_mem = kzalloc(config_mem_size, GFP_KERNEL);
1508 dev_err(dev, "Failed to allocate memory\n");
1512 ret = mxt_prepare_cfg_mem(data, cfg, data_pos, cfg_start_ofs,
1513 config_mem, config_mem_size);
1517 /* Calculate crc of the received configs (not the raw config file) */
1518 if (data->T7_address < cfg_start_ofs) {
1519 dev_err(dev, "Bad T7 address, T7addr = %x, config offset %x\n",
1520 data->T7_address, cfg_start_ofs);
1525 calculated_crc = mxt_calculate_crc(config_mem,
1526 data->T7_address - cfg_start_ofs,
1529 if (config_crc > 0 && config_crc != calculated_crc)
1530 dev_warn(dev, "Config CRC error, calculated=%06X, file=%06X\n",
1531 calculated_crc, config_crc);
1533 ret = mxt_upload_cfg_mem(data, cfg_start_ofs,
1534 config_mem, config_mem_size);
1538 mxt_update_crc(data, MXT_COMMAND_BACKUPNV, MXT_BACKUP_VALUE);
1540 ret = mxt_soft_reset(data);
1544 dev_info(dev, "Config successfully updated\n");
1546 /* T7 config may have changed */
1547 mxt_init_t7_power_cfg(data);
1554 static int mxt_get_info(struct mxt_data *data)
1556 struct i2c_client *client = data->client;
1557 struct mxt_info *info = &data->info;
1560 /* Read 7-byte info block starting at address 0 */
1561 error = __mxt_read_reg(client, 0, sizeof(*info), info);
1568 static void mxt_free_input_device(struct mxt_data *data)
1570 if (data->input_dev) {
1571 input_unregister_device(data->input_dev);
1572 data->input_dev = NULL;
1576 static void mxt_free_object_table(struct mxt_data *data)
1578 #ifdef CONFIG_TOUCHSCREEN_ATMEL_MXT_T37
1579 video_unregister_device(&data->dbg.vdev);
1580 v4l2_device_unregister(&data->dbg.v4l2);
1583 kfree(data->object_table);
1584 data->object_table = NULL;
1585 kfree(data->msg_buf);
1586 data->msg_buf = NULL;
1587 data->T5_address = 0;
1588 data->T5_msg_size = 0;
1589 data->T6_reportid = 0;
1590 data->T7_address = 0;
1591 data->T9_reportid_min = 0;
1592 data->T9_reportid_max = 0;
1593 data->T19_reportid = 0;
1594 data->T44_address = 0;
1595 data->T100_reportid_min = 0;
1596 data->T100_reportid_max = 0;
1597 data->max_reportid = 0;
1600 static int mxt_get_object_table(struct mxt_data *data)
1602 struct i2c_client *client = data->client;
1604 struct mxt_object *object_table;
1610 table_size = data->info.object_num * sizeof(struct mxt_object);
1611 object_table = kzalloc(table_size, GFP_KERNEL);
1612 if (!object_table) {
1613 dev_err(&data->client->dev, "Failed to allocate memory\n");
1617 error = __mxt_read_reg(client, MXT_OBJECT_START, table_size,
1620 kfree(object_table);
1624 /* Valid Report IDs start counting from 1 */
1627 for (i = 0; i < data->info.object_num; i++) {
1628 struct mxt_object *object = object_table + i;
1631 le16_to_cpus(&object->start_address);
1633 if (object->num_report_ids) {
1635 reportid += object->num_report_ids *
1636 mxt_obj_instances(object);
1637 max_id = reportid - 1;
1643 dev_dbg(&data->client->dev,
1644 "T%u Start:%u Size:%zu Instances:%zu Report IDs:%u-%u\n",
1645 object->type, object->start_address,
1646 mxt_obj_size(object), mxt_obj_instances(object),
1649 switch (object->type) {
1650 case MXT_GEN_MESSAGE_T5:
1651 if (data->info.family_id == 0x80 &&
1652 data->info.version < 0x20) {
1654 * On mXT224 firmware versions prior to V2.0
1655 * read and discard unused CRC byte otherwise
1656 * DMA reads are misaligned.
1658 data->T5_msg_size = mxt_obj_size(object);
1660 /* CRC not enabled, so skip last byte */
1661 data->T5_msg_size = mxt_obj_size(object) - 1;
1663 data->T5_address = object->start_address;
1665 case MXT_GEN_COMMAND_T6:
1666 data->T6_reportid = min_id;
1667 data->T6_address = object->start_address;
1669 case MXT_GEN_POWER_T7:
1670 data->T7_address = object->start_address;
1672 case MXT_TOUCH_MULTI_T9:
1673 data->multitouch = MXT_TOUCH_MULTI_T9;
1674 /* Only handle messages from first T9 instance */
1675 data->T9_reportid_min = min_id;
1676 data->T9_reportid_max = min_id +
1677 object->num_report_ids - 1;
1678 data->num_touchids = object->num_report_ids;
1680 case MXT_SPT_MESSAGECOUNT_T44:
1681 data->T44_address = object->start_address;
1683 case MXT_SPT_GPIOPWM_T19:
1684 data->T19_reportid = min_id;
1686 case MXT_TOUCH_MULTITOUCHSCREEN_T100:
1687 data->multitouch = MXT_TOUCH_MULTITOUCHSCREEN_T100;
1688 data->T100_reportid_min = min_id;
1689 data->T100_reportid_max = max_id;
1690 /* first two report IDs reserved */
1691 data->num_touchids = object->num_report_ids - 2;
1695 end_address = object->start_address
1696 + mxt_obj_size(object) * mxt_obj_instances(object) - 1;
1698 if (end_address >= data->mem_size)
1699 data->mem_size = end_address + 1;
1702 /* Store maximum reportid */
1703 data->max_reportid = reportid;
1705 /* If T44 exists, T5 position has to be directly after */
1706 if (data->T44_address && (data->T5_address != data->T44_address + 1)) {
1707 dev_err(&client->dev, "Invalid T44 position\n");
1709 goto free_object_table;
1712 data->msg_buf = kcalloc(data->max_reportid,
1713 data->T5_msg_size, GFP_KERNEL);
1714 if (!data->msg_buf) {
1715 dev_err(&client->dev, "Failed to allocate message buffer\n");
1717 goto free_object_table;
1720 data->object_table = object_table;
1725 mxt_free_object_table(data);
1729 static int mxt_read_t9_resolution(struct mxt_data *data)
1731 struct i2c_client *client = data->client;
1733 struct t9_range range;
1734 unsigned char orient;
1735 struct mxt_object *object;
1737 object = mxt_get_object(data, MXT_TOUCH_MULTI_T9);
1741 error = __mxt_read_reg(client,
1742 object->start_address + MXT_T9_XSIZE,
1743 sizeof(data->xsize), &data->xsize);
1747 error = __mxt_read_reg(client,
1748 object->start_address + MXT_T9_YSIZE,
1749 sizeof(data->ysize), &data->ysize);
1753 error = __mxt_read_reg(client,
1754 object->start_address + MXT_T9_RANGE,
1755 sizeof(range), &range);
1759 data->max_x = get_unaligned_le16(&range.x);
1760 data->max_y = get_unaligned_le16(&range.y);
1762 error = __mxt_read_reg(client,
1763 object->start_address + MXT_T9_ORIENT,
1768 data->xy_switch = orient & MXT_T9_ORIENT_SWITCH;
1769 data->invertx = orient & MXT_T9_ORIENT_INVERTX;
1770 data->inverty = orient & MXT_T9_ORIENT_INVERTY;
1775 static int mxt_read_t100_config(struct mxt_data *data)
1777 struct i2c_client *client = data->client;
1779 struct mxt_object *object;
1780 u16 range_x, range_y;
1784 object = mxt_get_object(data, MXT_TOUCH_MULTITOUCHSCREEN_T100);
1788 /* read touchscreen dimensions */
1789 error = __mxt_read_reg(client,
1790 object->start_address + MXT_T100_XRANGE,
1791 sizeof(range_x), &range_x);
1795 data->max_x = get_unaligned_le16(&range_x);
1797 error = __mxt_read_reg(client,
1798 object->start_address + MXT_T100_YRANGE,
1799 sizeof(range_y), &range_y);
1803 data->max_y = get_unaligned_le16(&range_y);
1805 error = __mxt_read_reg(client,
1806 object->start_address + MXT_T100_XSIZE,
1807 sizeof(data->xsize), &data->xsize);
1811 error = __mxt_read_reg(client,
1812 object->start_address + MXT_T100_YSIZE,
1813 sizeof(data->ysize), &data->ysize);
1817 /* read orientation config */
1818 error = __mxt_read_reg(client,
1819 object->start_address + MXT_T100_CFG1,
1824 data->xy_switch = cfg & MXT_T100_CFG_SWITCHXY;
1825 data->invertx = cfg & MXT_T100_CFG_INVERTX;
1826 data->inverty = cfg & MXT_T100_CFG_INVERTY;
1828 /* allocate aux bytes */
1829 error = __mxt_read_reg(client,
1830 object->start_address + MXT_T100_TCHAUX,
1837 if (tchaux & MXT_T100_TCHAUX_VECT)
1838 data->t100_aux_vect = aux++;
1840 if (tchaux & MXT_T100_TCHAUX_AMPL)
1841 data->t100_aux_ampl = aux++;
1843 if (tchaux & MXT_T100_TCHAUX_AREA)
1844 data->t100_aux_area = aux++;
1846 dev_dbg(&client->dev,
1847 "T100 aux mappings vect:%u ampl:%u area:%u\n",
1848 data->t100_aux_vect, data->t100_aux_ampl, data->t100_aux_area);
1853 static int mxt_input_open(struct input_dev *dev);
1854 static void mxt_input_close(struct input_dev *dev);
1856 static void mxt_set_up_as_touchpad(struct input_dev *input_dev,
1857 struct mxt_data *data)
1859 const struct mxt_platform_data *pdata = data->pdata;
1862 input_dev->name = "Atmel maXTouch Touchpad";
1864 __set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit);
1866 input_abs_set_res(input_dev, ABS_X, MXT_PIXELS_PER_MM);
1867 input_abs_set_res(input_dev, ABS_Y, MXT_PIXELS_PER_MM);
1868 input_abs_set_res(input_dev, ABS_MT_POSITION_X,
1870 input_abs_set_res(input_dev, ABS_MT_POSITION_Y,
1873 for (i = 0; i < pdata->t19_num_keys; i++)
1874 if (pdata->t19_keymap[i] != KEY_RESERVED)
1875 input_set_capability(input_dev, EV_KEY,
1876 pdata->t19_keymap[i]);
1879 static int mxt_initialize_input_device(struct mxt_data *data)
1881 const struct mxt_platform_data *pdata = data->pdata;
1882 struct device *dev = &data->client->dev;
1883 struct input_dev *input_dev;
1885 unsigned int num_mt_slots;
1886 unsigned int mt_flags = 0;
1888 switch (data->multitouch) {
1889 case MXT_TOUCH_MULTI_T9:
1890 num_mt_slots = data->T9_reportid_max - data->T9_reportid_min + 1;
1891 error = mxt_read_t9_resolution(data);
1893 dev_warn(dev, "Failed to initialize T9 resolution\n");
1896 case MXT_TOUCH_MULTITOUCHSCREEN_T100:
1897 num_mt_slots = data->num_touchids;
1898 error = mxt_read_t100_config(data);
1900 dev_warn(dev, "Failed to read T100 config\n");
1904 dev_err(dev, "Invalid multitouch object\n");
1908 /* Handle default values and orientation switch */
1909 if (data->max_x == 0)
1912 if (data->max_y == 0)
1915 if (data->xy_switch)
1916 swap(data->max_x, data->max_y);
1918 dev_info(dev, "Touchscreen size X%uY%u\n", data->max_x, data->max_y);
1920 /* Register input device */
1921 input_dev = input_allocate_device();
1923 dev_err(dev, "Failed to allocate memory\n");
1927 input_dev->name = "Atmel maXTouch Touchscreen";
1928 input_dev->phys = data->phys;
1929 input_dev->id.bustype = BUS_I2C;
1930 input_dev->dev.parent = dev;
1931 input_dev->open = mxt_input_open;
1932 input_dev->close = mxt_input_close;
1934 input_set_capability(input_dev, EV_KEY, BTN_TOUCH);
1936 /* For single touch */
1937 input_set_abs_params(input_dev, ABS_X, 0, data->max_x, 0, 0);
1938 input_set_abs_params(input_dev, ABS_Y, 0, data->max_y, 0, 0);
1940 if (data->multitouch == MXT_TOUCH_MULTI_T9 ||
1941 (data->multitouch == MXT_TOUCH_MULTITOUCHSCREEN_T100 &&
1942 data->t100_aux_ampl)) {
1943 input_set_abs_params(input_dev, ABS_PRESSURE, 0, 255, 0, 0);
1946 /* If device has buttons we assume it is a touchpad */
1947 if (pdata->t19_num_keys) {
1948 mxt_set_up_as_touchpad(input_dev, data);
1949 mt_flags |= INPUT_MT_POINTER;
1951 mt_flags |= INPUT_MT_DIRECT;
1954 /* For multi touch */
1955 error = input_mt_init_slots(input_dev, num_mt_slots, mt_flags);
1957 dev_err(dev, "Error %d initialising slots\n", error);
1961 if (data->multitouch == MXT_TOUCH_MULTITOUCHSCREEN_T100) {
1962 input_set_abs_params(input_dev, ABS_MT_TOOL_TYPE,
1963 0, MT_TOOL_MAX, 0, 0);
1964 input_set_abs_params(input_dev, ABS_MT_DISTANCE,
1965 MXT_DISTANCE_ACTIVE_TOUCH,
1966 MXT_DISTANCE_HOVERING,
1970 input_set_abs_params(input_dev, ABS_MT_POSITION_X,
1971 0, data->max_x, 0, 0);
1972 input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
1973 0, data->max_y, 0, 0);
1975 if (data->multitouch == MXT_TOUCH_MULTI_T9 ||
1976 (data->multitouch == MXT_TOUCH_MULTITOUCHSCREEN_T100 &&
1977 data->t100_aux_area)) {
1978 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR,
1979 0, MXT_MAX_AREA, 0, 0);
1982 if (data->multitouch == MXT_TOUCH_MULTI_T9 ||
1983 (data->multitouch == MXT_TOUCH_MULTITOUCHSCREEN_T100 &&
1984 data->t100_aux_ampl)) {
1985 input_set_abs_params(input_dev, ABS_MT_PRESSURE,
1989 if (data->multitouch == MXT_TOUCH_MULTITOUCHSCREEN_T100 &&
1990 data->t100_aux_vect) {
1991 input_set_abs_params(input_dev, ABS_MT_ORIENTATION,
1995 if (data->multitouch == MXT_TOUCH_MULTITOUCHSCREEN_T100 &&
1996 data->t100_aux_ampl) {
1997 input_set_abs_params(input_dev, ABS_MT_PRESSURE,
2001 if (data->multitouch == MXT_TOUCH_MULTITOUCHSCREEN_T100 &&
2002 data->t100_aux_vect) {
2003 input_set_abs_params(input_dev, ABS_MT_ORIENTATION,
2007 input_set_drvdata(input_dev, data);
2009 error = input_register_device(input_dev);
2011 dev_err(dev, "Error %d registering input device\n", error);
2015 data->input_dev = input_dev;
2020 input_free_device(input_dev);
2024 static int mxt_configure_objects(struct mxt_data *data,
2025 const struct firmware *cfg);
2027 static void mxt_config_cb(const struct firmware *cfg, void *ctx)
2029 mxt_configure_objects(ctx, cfg);
2030 release_firmware(cfg);
2033 static int mxt_initialize(struct mxt_data *data)
2035 struct i2c_client *client = data->client;
2036 int recovery_attempts = 0;
2040 error = mxt_get_info(data);
2044 /* Check bootloader state */
2045 error = mxt_probe_bootloader(data, false);
2047 dev_info(&client->dev, "Trying alternate bootloader address\n");
2048 error = mxt_probe_bootloader(data, true);
2050 /* Chip is not in appmode or bootloader mode */
2055 /* OK, we are in bootloader, see if we can recover */
2056 if (++recovery_attempts > 1) {
2057 dev_err(&client->dev, "Could not recover from bootloader mode\n");
2059 * We can reflash from this state, so do not
2060 * abort initialization.
2062 data->in_bootloader = true;
2066 /* Attempt to exit bootloader into app mode */
2067 mxt_send_bootloader_cmd(data, false);
2068 msleep(MXT_FW_RESET_TIME);
2071 /* Get object table information */
2072 error = mxt_get_object_table(data);
2074 dev_err(&client->dev, "Error %d reading object table\n", error);
2078 error = mxt_acquire_irq(data);
2080 goto err_free_object_table;
2082 error = reject_firmware_nowait(THIS_MODULE, true, MXT_CFG_NAME,
2083 &client->dev, GFP_KERNEL, data,
2086 dev_err(&client->dev, "Failed to invoke firmware loader: %d\n",
2088 goto err_free_object_table;
2093 err_free_object_table:
2094 mxt_free_object_table(data);
2098 static int mxt_set_t7_power_cfg(struct mxt_data *data, u8 sleep)
2100 struct device *dev = &data->client->dev;
2102 struct t7_config *new_config;
2103 struct t7_config deepsleep = { .active = 0, .idle = 0 };
2105 if (sleep == MXT_POWER_CFG_DEEPSLEEP)
2106 new_config = &deepsleep;
2108 new_config = &data->t7_cfg;
2110 error = __mxt_write_reg(data->client, data->T7_address,
2111 sizeof(data->t7_cfg), new_config);
2115 dev_dbg(dev, "Set T7 ACTV:%d IDLE:%d\n",
2116 new_config->active, new_config->idle);
2121 static int mxt_init_t7_power_cfg(struct mxt_data *data)
2123 struct device *dev = &data->client->dev;
2128 error = __mxt_read_reg(data->client, data->T7_address,
2129 sizeof(data->t7_cfg), &data->t7_cfg);
2133 if (data->t7_cfg.active == 0 || data->t7_cfg.idle == 0) {
2135 dev_dbg(dev, "T7 cfg zero, resetting\n");
2136 mxt_soft_reset(data);
2140 dev_dbg(dev, "T7 cfg zero after reset, overriding\n");
2141 data->t7_cfg.active = 20;
2142 data->t7_cfg.idle = 100;
2143 return mxt_set_t7_power_cfg(data, MXT_POWER_CFG_RUN);
2147 dev_dbg(dev, "Initialized power cfg: ACTV %d, IDLE %d\n",
2148 data->t7_cfg.active, data->t7_cfg.idle);
2152 #ifdef CONFIG_TOUCHSCREEN_ATMEL_MXT_T37
2153 static u16 mxt_get_debug_value(struct mxt_data *data, unsigned int x,
2156 struct mxt_info *info = &data->info;
2157 struct mxt_dbg *dbg = &data->dbg;
2158 unsigned int ofs, page;
2159 unsigned int col = 0;
2160 unsigned int col_width;
2162 if (info->family_id == MXT_FAMILY_1386) {
2163 col_width = info->matrix_ysize / MXT1386_COLUMNS;
2164 col = y / col_width;
2167 col_width = info->matrix_ysize;
2170 ofs = (y + (x * col_width)) * sizeof(u16);
2171 page = ofs / MXT_DIAGNOSTIC_SIZE;
2172 ofs %= MXT_DIAGNOSTIC_SIZE;
2174 if (info->family_id == MXT_FAMILY_1386)
2175 page += col * MXT1386_PAGES_PER_COLUMN;
2177 return get_unaligned_le16(&dbg->t37_buf[page].data[ofs]);
2180 static int mxt_convert_debug_pages(struct mxt_data *data, u16 *outbuf)
2182 struct mxt_dbg *dbg = &data->dbg;
2185 unsigned int i, rx, ry;
2187 for (i = 0; i < dbg->t37_nodes; i++) {
2188 /* Handle orientation */
2189 rx = data->xy_switch ? y : x;
2190 ry = data->xy_switch ? x : y;
2191 rx = data->invertx ? (data->xsize - 1 - rx) : rx;
2192 ry = data->inverty ? (data->ysize - 1 - ry) : ry;
2194 outbuf[i] = mxt_get_debug_value(data, rx, ry);
2197 if (++x >= (data->xy_switch ? data->ysize : data->xsize)) {
2206 static int mxt_read_diagnostic_debug(struct mxt_data *data, u8 mode,
2209 struct mxt_dbg *dbg = &data->dbg;
2214 struct t37_debug *p;
2217 for (page = 0; page < dbg->t37_pages; page++) {
2218 p = dbg->t37_buf + page;
2220 ret = mxt_write_reg(data->client, dbg->diag_cmd_address,
2228 /* Read back command byte */
2229 ret = __mxt_read_reg(data->client, dbg->diag_cmd_address,
2230 sizeof(cmd_poll), &cmd_poll);
2234 /* Field is cleared once the command has been processed */
2236 if (retries++ > 100)
2244 ret = __mxt_read_reg(data->client, dbg->t37_address,
2245 sizeof(struct t37_debug), p);
2249 if (p->mode != mode || p->page != page) {
2250 dev_err(&data->client->dev, "T37 page mismatch\n");
2254 dev_dbg(&data->client->dev, "%s page:%d retries:%d\n",
2255 __func__, page, retries);
2257 /* For remaining pages, write PAGEUP rather than mode */
2258 cmd = MXT_DIAGNOSTIC_PAGEUP;
2261 return mxt_convert_debug_pages(data, outbuf);
2264 static int mxt_queue_setup(struct vb2_queue *q,
2265 unsigned int *nbuffers, unsigned int *nplanes,
2266 unsigned int sizes[], struct device *alloc_devs[])
2268 struct mxt_data *data = q->drv_priv;
2269 size_t size = data->dbg.t37_nodes * sizeof(u16);
2272 return sizes[0] < size ? -EINVAL : 0;
2280 static void mxt_buffer_queue(struct vb2_buffer *vb)
2282 struct mxt_data *data = vb2_get_drv_priv(vb->vb2_queue);
2287 ptr = vb2_plane_vaddr(vb, 0);
2289 dev_err(&data->client->dev, "Error acquiring frame ptr\n");
2293 switch (data->dbg.input) {
2294 case MXT_V4L_INPUT_DELTAS:
2296 mode = MXT_DIAGNOSTIC_DELTAS;
2299 case MXT_V4L_INPUT_REFS:
2300 mode = MXT_DIAGNOSTIC_REFS;
2304 ret = mxt_read_diagnostic_debug(data, mode, ptr);
2308 vb2_set_plane_payload(vb, 0, data->dbg.t37_nodes * sizeof(u16));
2309 vb2_buffer_done(vb, VB2_BUF_STATE_DONE);
2313 vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
2316 /* V4L2 structures */
2317 static const struct vb2_ops mxt_queue_ops = {
2318 .queue_setup = mxt_queue_setup,
2319 .buf_queue = mxt_buffer_queue,
2320 .wait_prepare = vb2_ops_wait_prepare,
2321 .wait_finish = vb2_ops_wait_finish,
2324 static const struct vb2_queue mxt_queue = {
2325 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
2326 .io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ,
2327 .buf_struct_size = sizeof(struct mxt_vb2_buffer),
2328 .ops = &mxt_queue_ops,
2329 .mem_ops = &vb2_vmalloc_memops,
2330 .timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC,
2331 .min_buffers_needed = 1,
2334 static int mxt_vidioc_querycap(struct file *file, void *priv,
2335 struct v4l2_capability *cap)
2337 struct mxt_data *data = video_drvdata(file);
2339 strlcpy(cap->driver, "atmel_mxt_ts", sizeof(cap->driver));
2340 strlcpy(cap->card, "atmel_mxt_ts touch", sizeof(cap->card));
2341 snprintf(cap->bus_info, sizeof(cap->bus_info),
2342 "I2C:%s", dev_name(&data->client->dev));
2346 static int mxt_vidioc_enum_input(struct file *file, void *priv,
2347 struct v4l2_input *i)
2349 if (i->index >= MXT_V4L_INPUT_MAX)
2352 i->type = V4L2_INPUT_TYPE_TOUCH;
2355 case MXT_V4L_INPUT_REFS:
2356 strlcpy(i->name, "Mutual Capacitance References",
2359 case MXT_V4L_INPUT_DELTAS:
2360 strlcpy(i->name, "Mutual Capacitance Deltas", sizeof(i->name));
2367 static int mxt_set_input(struct mxt_data *data, unsigned int i)
2369 struct v4l2_pix_format *f = &data->dbg.format;
2371 if (i >= MXT_V4L_INPUT_MAX)
2374 if (i == MXT_V4L_INPUT_DELTAS)
2375 f->pixelformat = V4L2_TCH_FMT_DELTA_TD16;
2377 f->pixelformat = V4L2_TCH_FMT_TU16;
2379 f->width = data->xy_switch ? data->ysize : data->xsize;
2380 f->height = data->xy_switch ? data->xsize : data->ysize;
2381 f->field = V4L2_FIELD_NONE;
2382 f->colorspace = V4L2_COLORSPACE_RAW;
2383 f->bytesperline = f->width * sizeof(u16);
2384 f->sizeimage = f->width * f->height * sizeof(u16);
2386 data->dbg.input = i;
2391 static int mxt_vidioc_s_input(struct file *file, void *priv, unsigned int i)
2393 return mxt_set_input(video_drvdata(file), i);
2396 static int mxt_vidioc_g_input(struct file *file, void *priv, unsigned int *i)
2398 struct mxt_data *data = video_drvdata(file);
2400 *i = data->dbg.input;
2405 static int mxt_vidioc_fmt(struct file *file, void *priv, struct v4l2_format *f)
2407 struct mxt_data *data = video_drvdata(file);
2409 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2410 f->fmt.pix = data->dbg.format;
2415 static int mxt_vidioc_enum_fmt(struct file *file, void *priv,
2416 struct v4l2_fmtdesc *fmt)
2418 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2421 switch (fmt->index) {
2423 fmt->pixelformat = V4L2_TCH_FMT_TU16;
2427 fmt->pixelformat = V4L2_TCH_FMT_DELTA_TD16;
2437 static int mxt_vidioc_g_parm(struct file *file, void *fh,
2438 struct v4l2_streamparm *a)
2440 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2443 a->parm.capture.readbuffers = 1;
2444 a->parm.capture.timeperframe.numerator = 1;
2445 a->parm.capture.timeperframe.denominator = 10;
2449 static const struct v4l2_ioctl_ops mxt_video_ioctl_ops = {
2450 .vidioc_querycap = mxt_vidioc_querycap,
2452 .vidioc_enum_fmt_vid_cap = mxt_vidioc_enum_fmt,
2453 .vidioc_s_fmt_vid_cap = mxt_vidioc_fmt,
2454 .vidioc_g_fmt_vid_cap = mxt_vidioc_fmt,
2455 .vidioc_try_fmt_vid_cap = mxt_vidioc_fmt,
2456 .vidioc_g_parm = mxt_vidioc_g_parm,
2458 .vidioc_enum_input = mxt_vidioc_enum_input,
2459 .vidioc_g_input = mxt_vidioc_g_input,
2460 .vidioc_s_input = mxt_vidioc_s_input,
2462 .vidioc_reqbufs = vb2_ioctl_reqbufs,
2463 .vidioc_create_bufs = vb2_ioctl_create_bufs,
2464 .vidioc_querybuf = vb2_ioctl_querybuf,
2465 .vidioc_qbuf = vb2_ioctl_qbuf,
2466 .vidioc_dqbuf = vb2_ioctl_dqbuf,
2467 .vidioc_expbuf = vb2_ioctl_expbuf,
2469 .vidioc_streamon = vb2_ioctl_streamon,
2470 .vidioc_streamoff = vb2_ioctl_streamoff,
2473 static const struct video_device mxt_video_device = {
2474 .name = "Atmel maxTouch",
2475 .fops = &mxt_video_fops,
2476 .ioctl_ops = &mxt_video_ioctl_ops,
2477 .release = video_device_release_empty,
2478 .device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_TOUCH |
2479 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING,
2482 static void mxt_debug_init(struct mxt_data *data)
2484 struct mxt_info *info = &data->info;
2485 struct mxt_dbg *dbg = &data->dbg;
2486 struct mxt_object *object;
2489 object = mxt_get_object(data, MXT_GEN_COMMAND_T6);
2493 dbg->diag_cmd_address = object->start_address + MXT_COMMAND_DIAGNOSTIC;
2495 object = mxt_get_object(data, MXT_DEBUG_DIAGNOSTIC_T37);
2499 if (mxt_obj_size(object) != sizeof(struct t37_debug)) {
2500 dev_warn(&data->client->dev, "Bad T37 size");
2504 dbg->t37_address = object->start_address;
2506 /* Calculate size of data and allocate buffer */
2507 dbg->t37_nodes = data->xsize * data->ysize;
2509 if (info->family_id == MXT_FAMILY_1386)
2510 dbg->t37_pages = MXT1386_COLUMNS * MXT1386_PAGES_PER_COLUMN;
2512 dbg->t37_pages = DIV_ROUND_UP(data->xsize *
2513 data->info.matrix_ysize *
2515 sizeof(dbg->t37_buf->data));
2517 dbg->t37_buf = devm_kmalloc_array(&data->client->dev, dbg->t37_pages,
2518 sizeof(struct t37_debug), GFP_KERNEL);
2522 /* init channel to zero */
2523 mxt_set_input(data, 0);
2525 /* register video device */
2526 snprintf(dbg->v4l2.name, sizeof(dbg->v4l2.name), "%s", "atmel_mxt_ts");
2527 error = v4l2_device_register(&data->client->dev, &dbg->v4l2);
2531 /* initialize the queue */
2532 mutex_init(&dbg->lock);
2533 dbg->queue = mxt_queue;
2534 dbg->queue.drv_priv = data;
2535 dbg->queue.lock = &dbg->lock;
2536 dbg->queue.dev = &data->client->dev;
2538 error = vb2_queue_init(&dbg->queue);
2540 goto error_unreg_v4l2;
2542 dbg->vdev = mxt_video_device;
2543 dbg->vdev.v4l2_dev = &dbg->v4l2;
2544 dbg->vdev.lock = &dbg->lock;
2545 dbg->vdev.vfl_dir = VFL_DIR_RX;
2546 dbg->vdev.queue = &dbg->queue;
2547 video_set_drvdata(&dbg->vdev, data);
2549 error = video_register_device(&dbg->vdev, VFL_TYPE_TOUCH, -1);
2551 goto error_unreg_v4l2;
2556 v4l2_device_unregister(&dbg->v4l2);
2558 dev_warn(&data->client->dev, "Error initializing T37\n");
2561 static void mxt_debug_init(struct mxt_data *data)
2566 static int mxt_configure_objects(struct mxt_data *data,
2567 const struct firmware *cfg)
2569 struct device *dev = &data->client->dev;
2570 struct mxt_info *info = &data->info;
2573 error = mxt_init_t7_power_cfg(data);
2575 dev_err(dev, "Failed to initialize power cfg\n");
2580 error = mxt_update_cfg(data, cfg);
2582 dev_warn(dev, "Error %d updating config\n", error);
2585 if (data->multitouch) {
2586 error = mxt_initialize_input_device(data);
2590 dev_warn(dev, "No touch object detected\n");
2593 mxt_debug_init(data);
2596 "Family: %u Variant: %u Firmware V%u.%u.%02X Objects: %u\n",
2597 info->family_id, info->variant_id, info->version >> 4,
2598 info->version & 0xf, info->build, info->object_num);
2603 /* Firmware Version is returned as Major.Minor.Build */
2604 static ssize_t mxt_fw_version_show(struct device *dev,
2605 struct device_attribute *attr, char *buf)
2607 struct mxt_data *data = dev_get_drvdata(dev);
2608 struct mxt_info *info = &data->info;
2609 return scnprintf(buf, PAGE_SIZE, "%u.%u.%02X\n",
2610 info->version >> 4, info->version & 0xf, info->build);
2613 /* Hardware Version is returned as FamilyID.VariantID */
2614 static ssize_t mxt_hw_version_show(struct device *dev,
2615 struct device_attribute *attr, char *buf)
2617 struct mxt_data *data = dev_get_drvdata(dev);
2618 struct mxt_info *info = &data->info;
2619 return scnprintf(buf, PAGE_SIZE, "%u.%u\n",
2620 info->family_id, info->variant_id);
2623 static ssize_t mxt_show_instance(char *buf, int count,
2624 struct mxt_object *object, int instance,
2629 if (mxt_obj_instances(object) > 1)
2630 count += scnprintf(buf + count, PAGE_SIZE - count,
2631 "Instance %u\n", instance);
2633 for (i = 0; i < mxt_obj_size(object); i++)
2634 count += scnprintf(buf + count, PAGE_SIZE - count,
2635 "\t[%2u]: %02x (%d)\n", i, val[i], val[i]);
2636 count += scnprintf(buf + count, PAGE_SIZE - count, "\n");
2641 static ssize_t mxt_object_show(struct device *dev,
2642 struct device_attribute *attr, char *buf)
2644 struct mxt_data *data = dev_get_drvdata(dev);
2645 struct mxt_object *object;
2651 /* Pre-allocate buffer large enough to hold max sized object. */
2652 obuf = kmalloc(256, GFP_KERNEL);
2657 for (i = 0; i < data->info.object_num; i++) {
2658 object = data->object_table + i;
2660 if (!mxt_object_readable(object->type))
2663 count += scnprintf(buf + count, PAGE_SIZE - count,
2664 "T%u:\n", object->type);
2666 for (j = 0; j < mxt_obj_instances(object); j++) {
2667 u16 size = mxt_obj_size(object);
2668 u16 addr = object->start_address + j * size;
2670 error = __mxt_read_reg(data->client, addr, size, obuf);
2674 count = mxt_show_instance(buf, count, object, j, obuf);
2680 return error ?: count;
2683 static int mxt_check_firmware_format(struct device *dev,
2684 const struct firmware *fw)
2686 unsigned int pos = 0;
2689 while (pos < fw->size) {
2690 c = *(fw->data + pos);
2692 if (c < '0' || (c > '9' && c < 'A') || c > 'F')
2699 * To convert file try:
2701 dev_err(dev, "Aborting: firmware file must be in binary format\n");
2706 static int mxt_load_fw(struct device *dev, const char *fn)
2708 struct mxt_data *data = dev_get_drvdata(dev);
2709 const struct firmware *fw = NULL;
2710 unsigned int frame_size;
2711 unsigned int pos = 0;
2712 unsigned int retry = 0;
2713 unsigned int frame = 0;
2716 ret = reject_firmware(&fw, fn, dev);
2718 dev_err(dev, "Unable to open firmware %s\n", fn);
2722 /* Check for incorrect enc file */
2723 ret = mxt_check_firmware_format(dev, fw);
2725 goto release_firmware;
2727 if (!data->in_bootloader) {
2728 /* Change to the bootloader mode */
2729 data->in_bootloader = true;
2731 ret = mxt_t6_command(data, MXT_COMMAND_RESET,
2732 MXT_BOOT_VALUE, false);
2734 goto release_firmware;
2736 msleep(MXT_RESET_TIME);
2738 /* Do not need to scan since we know family ID */
2739 ret = mxt_lookup_bootloader_address(data, 0);
2741 goto release_firmware;
2743 mxt_free_input_device(data);
2744 mxt_free_object_table(data);
2746 enable_irq(data->irq);
2749 reinit_completion(&data->bl_completion);
2751 ret = mxt_check_bootloader(data, MXT_WAITING_BOOTLOAD_CMD, false);
2753 /* Bootloader may still be unlocked from previous attempt */
2754 ret = mxt_check_bootloader(data, MXT_WAITING_FRAME_DATA, false);
2758 dev_info(dev, "Unlocking bootloader\n");
2760 /* Unlock bootloader */
2761 ret = mxt_send_bootloader_cmd(data, true);
2766 while (pos < fw->size) {
2767 ret = mxt_check_bootloader(data, MXT_WAITING_FRAME_DATA, true);
2771 frame_size = ((*(fw->data + pos) << 8) | *(fw->data + pos + 1));
2773 /* Take account of CRC bytes */
2776 /* Write one frame to device */
2777 ret = mxt_bootloader_write(data, fw->data + pos, frame_size);
2781 ret = mxt_check_bootloader(data, MXT_FRAME_CRC_PASS, true);
2785 /* Back off by 20ms per retry */
2789 dev_err(dev, "Retry count exceeded\n");
2798 if (frame % 50 == 0)
2799 dev_dbg(dev, "Sent %d frames, %d/%zd bytes\n",
2800 frame, pos, fw->size);
2803 /* Wait for flash. */
2804 ret = mxt_wait_for_completion(data, &data->bl_completion,
2809 dev_dbg(dev, "Sent %d frames, %d bytes\n", frame, pos);
2812 * Wait for device to reset. Some bootloader versions do not assert
2813 * the CHG line after bootloading has finished, so ignore potential
2816 mxt_wait_for_completion(data, &data->bl_completion, MXT_FW_RESET_TIME);
2818 data->in_bootloader = false;
2821 disable_irq(data->irq);
2823 release_firmware(fw);
2827 static ssize_t mxt_update_fw_store(struct device *dev,
2828 struct device_attribute *attr,
2829 const char *buf, size_t count)
2831 struct mxt_data *data = dev_get_drvdata(dev);
2834 error = mxt_load_fw(dev, MXT_FW_NAME);
2836 dev_err(dev, "The firmware update failed(%d)\n", error);
2839 dev_info(dev, "The firmware update succeeded\n");
2841 error = mxt_initialize(data);
2849 static DEVICE_ATTR(fw_version, S_IRUGO, mxt_fw_version_show, NULL);
2850 static DEVICE_ATTR(hw_version, S_IRUGO, mxt_hw_version_show, NULL);
2851 static DEVICE_ATTR(object, S_IRUGO, mxt_object_show, NULL);
2852 static DEVICE_ATTR(update_fw, S_IWUSR, NULL, mxt_update_fw_store);
2854 static struct attribute *mxt_attrs[] = {
2855 &dev_attr_fw_version.attr,
2856 &dev_attr_hw_version.attr,
2857 &dev_attr_object.attr,
2858 &dev_attr_update_fw.attr,
2862 static const struct attribute_group mxt_attr_group = {
2866 static void mxt_start(struct mxt_data *data)
2868 switch (data->pdata->suspend_mode) {
2869 case MXT_SUSPEND_T9_CTRL:
2870 mxt_soft_reset(data);
2873 /* 0x83 = SCANEN | RPTEN | ENABLE */
2874 mxt_write_object(data,
2875 MXT_TOUCH_MULTI_T9, MXT_T9_CTRL, 0x83);
2878 case MXT_SUSPEND_DEEP_SLEEP:
2880 mxt_set_t7_power_cfg(data, MXT_POWER_CFG_RUN);
2882 /* Recalibrate since chip has been in deep sleep */
2883 mxt_t6_command(data, MXT_COMMAND_CALIBRATE, 1, false);
2889 static void mxt_stop(struct mxt_data *data)
2891 switch (data->pdata->suspend_mode) {
2892 case MXT_SUSPEND_T9_CTRL:
2894 mxt_write_object(data,
2895 MXT_TOUCH_MULTI_T9, MXT_T9_CTRL, 0);
2898 case MXT_SUSPEND_DEEP_SLEEP:
2900 mxt_set_t7_power_cfg(data, MXT_POWER_CFG_DEEPSLEEP);
2905 static int mxt_input_open(struct input_dev *dev)
2907 struct mxt_data *data = input_get_drvdata(dev);
2914 static void mxt_input_close(struct input_dev *dev)
2916 struct mxt_data *data = input_get_drvdata(dev);
2922 static const struct mxt_platform_data *mxt_parse_dt(struct i2c_client *client)
2924 struct mxt_platform_data *pdata;
2925 struct device_node *np = client->dev.of_node;
2930 return ERR_PTR(-ENOENT);
2932 pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL);
2934 return ERR_PTR(-ENOMEM);
2936 if (of_find_property(np, "linux,gpio-keymap", &proplen)) {
2937 pdata->t19_num_keys = proplen / sizeof(u32);
2939 keymap = devm_kzalloc(&client->dev,
2940 pdata->t19_num_keys * sizeof(keymap[0]),
2943 return ERR_PTR(-ENOMEM);
2945 ret = of_property_read_u32_array(np, "linux,gpio-keymap",
2946 keymap, pdata->t19_num_keys);
2948 dev_warn(&client->dev,
2949 "Couldn't read linux,gpio-keymap: %d\n", ret);
2951 pdata->t19_keymap = keymap;
2954 pdata->suspend_mode = MXT_SUSPEND_DEEP_SLEEP;
2959 static const struct mxt_platform_data *mxt_parse_dt(struct i2c_client *client)
2961 return ERR_PTR(-ENOENT);
2967 struct mxt_acpi_platform_data {
2969 struct mxt_platform_data pdata;
2972 static unsigned int samus_touchpad_buttons[] = {
2979 static struct mxt_acpi_platform_data samus_platform_data[] = {
2984 .t19_num_keys = ARRAY_SIZE(samus_touchpad_buttons),
2985 .t19_keymap = samus_touchpad_buttons,
2995 static unsigned int chromebook_tp_buttons[] = {
3004 static struct mxt_acpi_platform_data chromebook_platform_data[] = {
3009 .t19_num_keys = ARRAY_SIZE(chromebook_tp_buttons),
3010 .t19_keymap = chromebook_tp_buttons,
3020 static const struct dmi_system_id mxt_dmi_table[] = {
3022 /* 2015 Google Pixel */
3023 .ident = "Chromebook Pixel 2",
3025 DMI_MATCH(DMI_SYS_VENDOR, "GOOGLE"),
3026 DMI_MATCH(DMI_PRODUCT_NAME, "Samus"),
3028 .driver_data = samus_platform_data,
3031 /* Samsung Chromebook Pro */
3032 .ident = "Samsung Chromebook Pro",
3034 DMI_MATCH(DMI_SYS_VENDOR, "Google"),
3035 DMI_MATCH(DMI_PRODUCT_NAME, "Caroline"),
3037 .driver_data = samus_platform_data,
3040 /* Other Google Chromebooks */
3041 .ident = "Chromebook",
3043 DMI_MATCH(DMI_SYS_VENDOR, "GOOGLE"),
3045 .driver_data = chromebook_platform_data,
3050 static const struct mxt_platform_data *mxt_parse_acpi(struct i2c_client *client)
3052 struct acpi_device *adev;
3053 const struct dmi_system_id *system_id;
3054 const struct mxt_acpi_platform_data *acpi_pdata;
3057 * Ignore ACPI devices representing bootloader mode.
3059 * This is a bit of a hack: Google Chromebook BIOS creates ACPI
3060 * devices for both application and bootloader modes, but we are
3061 * interested in application mode only (if device is in bootloader
3062 * mode we'll end up switching into application anyway). So far
3063 * application mode addresses were all above 0x40, so we'll use it
3066 if (client->addr < 0x40)
3067 return ERR_PTR(-ENXIO);
3069 adev = ACPI_COMPANION(&client->dev);
3071 return ERR_PTR(-ENOENT);
3073 system_id = dmi_first_match(mxt_dmi_table);
3075 return ERR_PTR(-ENOENT);
3077 acpi_pdata = system_id->driver_data;
3079 return ERR_PTR(-ENOENT);
3081 while (acpi_pdata->hid) {
3082 if (!strcmp(acpi_device_hid(adev), acpi_pdata->hid))
3083 return &acpi_pdata->pdata;
3088 return ERR_PTR(-ENOENT);
3091 static const struct mxt_platform_data *mxt_parse_acpi(struct i2c_client *client)
3093 return ERR_PTR(-ENOENT);
3097 static const struct mxt_platform_data *
3098 mxt_get_platform_data(struct i2c_client *client)
3100 const struct mxt_platform_data *pdata;
3102 pdata = dev_get_platdata(&client->dev);
3106 pdata = mxt_parse_dt(client);
3107 if (!IS_ERR(pdata) || PTR_ERR(pdata) != -ENOENT)
3110 pdata = mxt_parse_acpi(client);
3111 if (!IS_ERR(pdata) || PTR_ERR(pdata) != -ENOENT)
3114 dev_err(&client->dev, "No platform data specified\n");
3115 return ERR_PTR(-EINVAL);
3118 static int mxt_probe(struct i2c_client *client, const struct i2c_device_id *id)
3120 struct mxt_data *data;
3121 const struct mxt_platform_data *pdata;
3124 pdata = mxt_get_platform_data(client);
3126 return PTR_ERR(pdata);
3128 data = kzalloc(sizeof(struct mxt_data), GFP_KERNEL);
3130 dev_err(&client->dev, "Failed to allocate memory\n");
3134 snprintf(data->phys, sizeof(data->phys), "i2c-%u-%04x/input0",
3135 client->adapter->nr, client->addr);
3137 data->client = client;
3138 data->pdata = pdata;
3139 data->irq = client->irq;
3140 i2c_set_clientdata(client, data);
3142 init_completion(&data->bl_completion);
3143 init_completion(&data->reset_completion);
3144 init_completion(&data->crc_completion);
3146 error = request_threaded_irq(client->irq, NULL, mxt_interrupt,
3147 pdata->irqflags | IRQF_ONESHOT,
3148 client->name, data);
3150 dev_err(&client->dev, "Failed to register interrupt\n");
3154 disable_irq(client->irq);
3156 error = mxt_initialize(data);
3160 error = sysfs_create_group(&client->dev.kobj, &mxt_attr_group);
3162 dev_err(&client->dev, "Failure %d creating sysfs group\n",
3164 goto err_free_object;
3170 mxt_free_input_device(data);
3171 mxt_free_object_table(data);
3173 free_irq(client->irq, data);
3179 static int mxt_remove(struct i2c_client *client)
3181 struct mxt_data *data = i2c_get_clientdata(client);
3183 sysfs_remove_group(&client->dev.kobj, &mxt_attr_group);
3184 free_irq(data->irq, data);
3185 mxt_free_input_device(data);
3186 mxt_free_object_table(data);
3192 static int __maybe_unused mxt_suspend(struct device *dev)
3194 struct i2c_client *client = to_i2c_client(dev);
3195 struct mxt_data *data = i2c_get_clientdata(client);
3196 struct input_dev *input_dev = data->input_dev;
3201 mutex_lock(&input_dev->mutex);
3203 if (input_dev->users)
3206 mutex_unlock(&input_dev->mutex);
3208 disable_irq(data->irq);
3213 static int __maybe_unused mxt_resume(struct device *dev)
3215 struct i2c_client *client = to_i2c_client(dev);
3216 struct mxt_data *data = i2c_get_clientdata(client);
3217 struct input_dev *input_dev = data->input_dev;
3222 enable_irq(data->irq);
3224 mutex_lock(&input_dev->mutex);
3226 if (input_dev->users)
3229 mutex_unlock(&input_dev->mutex);
3234 static SIMPLE_DEV_PM_OPS(mxt_pm_ops, mxt_suspend, mxt_resume);
3236 static const struct of_device_id mxt_of_match[] = {
3237 { .compatible = "atmel,maxtouch", },
3240 MODULE_DEVICE_TABLE(of, mxt_of_match);
3243 static const struct acpi_device_id mxt_acpi_id[] = {
3244 { "ATML0000", 0 }, /* Touchpad */
3245 { "ATML0001", 0 }, /* Touchscreen */
3248 MODULE_DEVICE_TABLE(acpi, mxt_acpi_id);
3251 static const struct i2c_device_id mxt_id[] = {
3252 { "qt602240_ts", 0 },
3253 { "atmel_mxt_ts", 0 },
3254 { "atmel_mxt_tp", 0 },
3259 MODULE_DEVICE_TABLE(i2c, mxt_id);
3261 static struct i2c_driver mxt_driver = {
3263 .name = "atmel_mxt_ts",
3264 .of_match_table = of_match_ptr(mxt_of_match),
3265 .acpi_match_table = ACPI_PTR(mxt_acpi_id),
3269 .remove = mxt_remove,
3273 module_i2c_driver(mxt_driver);
3275 /* Module information */
3276 MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
3277 MODULE_DESCRIPTION("Atmel maXTouch Touchscreen driver");
3278 MODULE_LICENSE("GPL");