2 * Atmel maXTouch Touchscreen driver
4 * Copyright (C) 2010 Samsung Electronics Co.Ltd
5 * Author: Joonyoung Shim <jy0922.shim@samsung.com>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/delay.h>
17 #include <linux/firmware.h>
18 #include <linux/i2c.h>
19 #include <linux/i2c/atmel_mxt_ts.h>
20 #include <linux/input/mt.h>
21 #include <linux/interrupt.h>
22 #include <linux/slab.h>
30 #define MXT_APP_LOW 0x4a
31 #define MXT_APP_HIGH 0x4b
32 #define MXT_BOOT_LOW 0x24
33 #define MXT_BOOT_HIGH 0x25
36 #define MXT_FW_NAME "maxtouch.fw"
40 #define MXT_FAMILY_ID 0x00
41 #define MXT_VARIANT_ID 0x01
42 #define MXT_VERSION 0x02
43 #define MXT_BUILD 0x03
44 #define MXT_MATRIX_X_SIZE 0x04
45 #define MXT_MATRIX_Y_SIZE 0x05
46 #define MXT_OBJECT_NUM 0x06
47 #define MXT_OBJECT_START 0x07
49 #define MXT_OBJECT_SIZE 6
52 #define MXT_DEBUG_DIAGNOSTIC_T37 37
53 #define MXT_GEN_MESSAGE_T5 5
54 #define MXT_GEN_COMMAND_T6 6
55 #define MXT_GEN_POWER_T7 7
56 #define MXT_GEN_ACQUIRE_T8 8
57 #define MXT_GEN_DATASOURCE_T53 53
58 #define MXT_TOUCH_MULTI_T9 9
59 #define MXT_TOUCH_KEYARRAY_T15 15
60 #define MXT_TOUCH_PROXIMITY_T23 23
61 #define MXT_TOUCH_PROXKEY_T52 52
62 #define MXT_PROCI_GRIPFACE_T20 20
63 #define MXT_PROCG_NOISE_T22 22
64 #define MXT_PROCI_ONETOUCH_T24 24
65 #define MXT_PROCI_TWOTOUCH_T27 27
66 #define MXT_PROCI_GRIP_T40 40
67 #define MXT_PROCI_PALM_T41 41
68 #define MXT_PROCI_TOUCHSUPPRESSION_T42 42
69 #define MXT_PROCI_STYLUS_T47 47
70 #define MXT_PROCG_NOISESUPPRESSION_T48 48
71 #define MXT_SPT_COMMSCONFIG_T18 18
72 #define MXT_SPT_GPIOPWM_T19 19
73 #define MXT_SPT_SELFTEST_T25 25
74 #define MXT_SPT_CTECONFIG_T28 28
75 #define MXT_SPT_USERDATA_T38 38
76 #define MXT_SPT_DIGITIZER_T43 43
77 #define MXT_SPT_MESSAGECOUNT_T44 44
78 #define MXT_SPT_CTECONFIG_T46 46
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 /* MXT_GEN_POWER_T7 field */
88 #define MXT_POWER_IDLEACQINT 0
89 #define MXT_POWER_ACTVACQINT 1
90 #define MXT_POWER_ACTV2IDLETO 2
92 /* MXT_GEN_ACQUIRE_T8 field */
93 #define MXT_ACQUIRE_CHRGTIME 0
94 #define MXT_ACQUIRE_TCHDRIFT 2
95 #define MXT_ACQUIRE_DRIFTST 3
96 #define MXT_ACQUIRE_TCHAUTOCAL 4
97 #define MXT_ACQUIRE_SYNC 5
98 #define MXT_ACQUIRE_ATCHCALST 6
99 #define MXT_ACQUIRE_ATCHCALSTHR 7
101 /* MXT_TOUCH_MULTI_T9 field */
102 #define MXT_TOUCH_CTRL 0
103 #define MXT_TOUCH_XORIGIN 1
104 #define MXT_TOUCH_YORIGIN 2
105 #define MXT_TOUCH_XSIZE 3
106 #define MXT_TOUCH_YSIZE 4
107 #define MXT_TOUCH_BLEN 6
108 #define MXT_TOUCH_TCHTHR 7
109 #define MXT_TOUCH_TCHDI 8
110 #define MXT_TOUCH_ORIENT 9
111 #define MXT_TOUCH_MOVHYSTI 11
112 #define MXT_TOUCH_MOVHYSTN 12
113 #define MXT_TOUCH_NUMTOUCH 14
114 #define MXT_TOUCH_MRGHYST 15
115 #define MXT_TOUCH_MRGTHR 16
116 #define MXT_TOUCH_AMPHYST 17
117 #define MXT_TOUCH_XRANGE_LSB 18
118 #define MXT_TOUCH_XRANGE_MSB 19
119 #define MXT_TOUCH_YRANGE_LSB 20
120 #define MXT_TOUCH_YRANGE_MSB 21
121 #define MXT_TOUCH_XLOCLIP 22
122 #define MXT_TOUCH_XHICLIP 23
123 #define MXT_TOUCH_YLOCLIP 24
124 #define MXT_TOUCH_YHICLIP 25
125 #define MXT_TOUCH_XEDGECTRL 26
126 #define MXT_TOUCH_XEDGEDIST 27
127 #define MXT_TOUCH_YEDGECTRL 28
128 #define MXT_TOUCH_YEDGEDIST 29
129 #define MXT_TOUCH_JUMPLIMIT 30
131 /* MXT_PROCI_GRIPFACE_T20 field */
132 #define MXT_GRIPFACE_CTRL 0
133 #define MXT_GRIPFACE_XLOGRIP 1
134 #define MXT_GRIPFACE_XHIGRIP 2
135 #define MXT_GRIPFACE_YLOGRIP 3
136 #define MXT_GRIPFACE_YHIGRIP 4
137 #define MXT_GRIPFACE_MAXTCHS 5
138 #define MXT_GRIPFACE_SZTHR1 7
139 #define MXT_GRIPFACE_SZTHR2 8
140 #define MXT_GRIPFACE_SHPTHR1 9
141 #define MXT_GRIPFACE_SHPTHR2 10
142 #define MXT_GRIPFACE_SUPEXTTO 11
144 /* MXT_PROCI_NOISE field */
145 #define MXT_NOISE_CTRL 0
146 #define MXT_NOISE_OUTFLEN 1
147 #define MXT_NOISE_GCAFUL_LSB 3
148 #define MXT_NOISE_GCAFUL_MSB 4
149 #define MXT_NOISE_GCAFLL_LSB 5
150 #define MXT_NOISE_GCAFLL_MSB 6
151 #define MXT_NOISE_ACTVGCAFVALID 7
152 #define MXT_NOISE_NOISETHR 8
153 #define MXT_NOISE_FREQHOPSCALE 10
154 #define MXT_NOISE_FREQ0 11
155 #define MXT_NOISE_FREQ1 12
156 #define MXT_NOISE_FREQ2 13
157 #define MXT_NOISE_FREQ3 14
158 #define MXT_NOISE_FREQ4 15
159 #define MXT_NOISE_IDLEGCAFVALID 16
161 /* MXT_SPT_COMMSCONFIG_T18 */
162 #define MXT_COMMS_CTRL 0
163 #define MXT_COMMS_CMD 1
165 /* MXT_SPT_CTECONFIG_T28 field */
166 #define MXT_CTE_CTRL 0
167 #define MXT_CTE_CMD 1
168 #define MXT_CTE_MODE 2
169 #define MXT_CTE_IDLEGCAFDEPTH 3
170 #define MXT_CTE_ACTVGCAFDEPTH 4
171 #define MXT_CTE_VOLTAGE 5
173 #define MXT_VOLTAGE_DEFAULT 2700000
174 #define MXT_VOLTAGE_STEP 10000
176 /* Define for MXT_GEN_COMMAND_T6 */
177 #define MXT_BOOT_VALUE 0xa5
178 #define MXT_BACKUP_VALUE 0x55
179 #define MXT_BACKUP_TIME 25 /* msec */
180 #define MXT_RESET_TIME 65 /* msec */
182 #define MXT_FWRESET_TIME 175 /* msec */
184 /* Command to unlock bootloader */
185 #define MXT_UNLOCK_CMD_MSB 0xaa
186 #define MXT_UNLOCK_CMD_LSB 0xdc
188 /* Bootloader mode status */
189 #define MXT_WAITING_BOOTLOAD_CMD 0xc0 /* valid 7 6 bit only */
190 #define MXT_WAITING_FRAME_DATA 0x80 /* valid 7 6 bit only */
191 #define MXT_FRAME_CRC_CHECK 0x02
192 #define MXT_FRAME_CRC_FAIL 0x03
193 #define MXT_FRAME_CRC_PASS 0x04
194 #define MXT_APP_CRC_FAIL 0x40 /* valid 7 8 bit only */
195 #define MXT_BOOT_STATUS_MASK 0x3f
198 #define MXT_UNGRIP (1 << 0)
199 #define MXT_SUPPRESS (1 << 1)
200 #define MXT_AMP (1 << 2)
201 #define MXT_VECTOR (1 << 3)
202 #define MXT_MOVE (1 << 4)
203 #define MXT_RELEASE (1 << 5)
204 #define MXT_PRESS (1 << 6)
205 #define MXT_DETECT (1 << 7)
207 /* Touch orient bits */
208 #define MXT_XY_SWITCH (1 << 0)
209 #define MXT_X_INVERT (1 << 1)
210 #define MXT_Y_INVERT (1 << 2)
212 /* Touchscreen absolute values */
213 #define MXT_MAX_AREA 0xff
228 u8 size; /* Size of each instance - 1 */
229 u8 instances; /* Number of instances - 1 */
238 /* Each client has this additional data */
240 struct i2c_client *client;
241 struct input_dev *input_dev;
242 char phys[64]; /* device physical location */
243 const struct mxt_platform_data *pdata;
244 struct mxt_object *object_table;
245 struct mxt_info info;
250 /* Cached parameters from object table */
256 static bool mxt_object_readable(unsigned int type)
259 case MXT_GEN_COMMAND_T6:
260 case MXT_GEN_POWER_T7:
261 case MXT_GEN_ACQUIRE_T8:
262 case MXT_GEN_DATASOURCE_T53:
263 case MXT_TOUCH_MULTI_T9:
264 case MXT_TOUCH_KEYARRAY_T15:
265 case MXT_TOUCH_PROXIMITY_T23:
266 case MXT_TOUCH_PROXKEY_T52:
267 case MXT_PROCI_GRIPFACE_T20:
268 case MXT_PROCG_NOISE_T22:
269 case MXT_PROCI_ONETOUCH_T24:
270 case MXT_PROCI_TWOTOUCH_T27:
271 case MXT_PROCI_GRIP_T40:
272 case MXT_PROCI_PALM_T41:
273 case MXT_PROCI_TOUCHSUPPRESSION_T42:
274 case MXT_PROCI_STYLUS_T47:
275 case MXT_PROCG_NOISESUPPRESSION_T48:
276 case MXT_SPT_COMMSCONFIG_T18:
277 case MXT_SPT_GPIOPWM_T19:
278 case MXT_SPT_SELFTEST_T25:
279 case MXT_SPT_CTECONFIG_T28:
280 case MXT_SPT_USERDATA_T38:
281 case MXT_SPT_DIGITIZER_T43:
282 case MXT_SPT_CTECONFIG_T46:
289 static bool mxt_object_writable(unsigned int type)
292 case MXT_GEN_COMMAND_T6:
293 case MXT_GEN_POWER_T7:
294 case MXT_GEN_ACQUIRE_T8:
295 case MXT_TOUCH_MULTI_T9:
296 case MXT_TOUCH_KEYARRAY_T15:
297 case MXT_TOUCH_PROXIMITY_T23:
298 case MXT_TOUCH_PROXKEY_T52:
299 case MXT_PROCI_GRIPFACE_T20:
300 case MXT_PROCG_NOISE_T22:
301 case MXT_PROCI_ONETOUCH_T24:
302 case MXT_PROCI_TWOTOUCH_T27:
303 case MXT_PROCI_GRIP_T40:
304 case MXT_PROCI_PALM_T41:
305 case MXT_PROCI_TOUCHSUPPRESSION_T42:
306 case MXT_PROCI_STYLUS_T47:
307 case MXT_PROCG_NOISESUPPRESSION_T48:
308 case MXT_SPT_COMMSCONFIG_T18:
309 case MXT_SPT_GPIOPWM_T19:
310 case MXT_SPT_SELFTEST_T25:
311 case MXT_SPT_CTECONFIG_T28:
312 case MXT_SPT_DIGITIZER_T43:
313 case MXT_SPT_CTECONFIG_T46:
320 static void mxt_dump_message(struct device *dev,
321 struct mxt_message *message)
323 dev_dbg(dev, "reportid: %u\tmessage: %02x %02x %02x %02x %02x %02x %02x\n",
324 message->reportid, message->message[0], message->message[1],
325 message->message[2], message->message[3], message->message[4],
326 message->message[5], message->message[6]);
329 static int mxt_check_bootloader(struct i2c_client *client,
335 if (i2c_master_recv(client, &val, 1) != 1) {
336 dev_err(&client->dev, "%s: i2c recv failed\n", __func__);
341 case MXT_WAITING_BOOTLOAD_CMD:
342 case MXT_WAITING_FRAME_DATA:
343 val &= ~MXT_BOOT_STATUS_MASK;
345 case MXT_FRAME_CRC_PASS:
346 if (val == MXT_FRAME_CRC_CHECK)
354 dev_err(&client->dev, "Unvalid bootloader mode state\n");
361 static int mxt_unlock_bootloader(struct i2c_client *client)
365 buf[0] = MXT_UNLOCK_CMD_LSB;
366 buf[1] = MXT_UNLOCK_CMD_MSB;
368 if (i2c_master_send(client, buf, 2) != 2) {
369 dev_err(&client->dev, "%s: i2c send failed\n", __func__);
376 static int mxt_fw_write(struct i2c_client *client,
377 const u8 *data, unsigned int frame_size)
379 if (i2c_master_send(client, data, frame_size) != frame_size) {
380 dev_err(&client->dev, "%s: i2c send failed\n", __func__);
387 static int __mxt_read_reg(struct i2c_client *client,
388 u16 reg, u16 len, void *val)
390 struct i2c_msg xfer[2];
395 buf[1] = (reg >> 8) & 0xff;
398 xfer[0].addr = client->addr;
404 xfer[1].addr = client->addr;
405 xfer[1].flags = I2C_M_RD;
409 ret = i2c_transfer(client->adapter, xfer, 2);
415 dev_err(&client->dev, "%s: i2c transfer failed (%d)\n",
422 static int mxt_read_reg(struct i2c_client *client, u16 reg, u8 *val)
424 return __mxt_read_reg(client, reg, 1, val);
427 static int __mxt_write_reg(struct i2c_client *client, u16 reg, u16 len,
435 buf = kmalloc(count, GFP_KERNEL);
440 buf[1] = (reg >> 8) & 0xff;
441 memcpy(&buf[2], val, len);
443 ret = i2c_master_send(client, buf, count);
449 dev_err(&client->dev, "%s: i2c send failed (%d)\n",
457 static int mxt_write_reg(struct i2c_client *client, u16 reg, u8 val)
459 return __mxt_write_reg(client, reg, 1, &val);
462 static struct mxt_object *
463 mxt_get_object(struct mxt_data *data, u8 type)
465 struct mxt_object *object;
468 for (i = 0; i < data->info.object_num; i++) {
469 object = data->object_table + i;
470 if (object->type == type)
474 dev_err(&data->client->dev, "Invalid object type\n");
478 static int mxt_read_message(struct mxt_data *data,
479 struct mxt_message *message)
481 struct mxt_object *object;
484 object = mxt_get_object(data, MXT_GEN_MESSAGE_T5);
488 reg = object->start_address;
489 return __mxt_read_reg(data->client, reg,
490 sizeof(struct mxt_message), message);
493 static int mxt_write_object(struct mxt_data *data,
494 u8 type, u8 offset, u8 val)
496 struct mxt_object *object;
499 object = mxt_get_object(data, type);
500 if (!object || offset >= object->size + 1)
503 reg = object->start_address;
504 return mxt_write_reg(data->client, reg + offset, val);
507 static void mxt_input_touchevent(struct mxt_data *data,
508 struct mxt_message *message, int id)
510 struct device *dev = &data->client->dev;
511 u8 status = message->message[0];
512 struct input_dev *input_dev = data->input_dev;
518 x = (message->message[1] << 4) | ((message->message[3] >> 4) & 0xf);
519 y = (message->message[2] << 4) | ((message->message[3] & 0xf));
520 if (data->max_x < 1024)
522 if (data->max_y < 1024)
525 area = message->message[4];
526 pressure = message->message[5];
529 "[%u] %c%c%c%c%c%c%c%c x: %5u y: %5u area: %3u amp: %3u\n",
531 (status & MXT_DETECT) ? 'D' : '.',
532 (status & MXT_PRESS) ? 'P' : '.',
533 (status & MXT_RELEASE) ? 'R' : '.',
534 (status & MXT_MOVE) ? 'M' : '.',
535 (status & MXT_VECTOR) ? 'V' : '.',
536 (status & MXT_AMP) ? 'A' : '.',
537 (status & MXT_SUPPRESS) ? 'S' : '.',
538 (status & MXT_UNGRIP) ? 'U' : '.',
539 x, y, area, pressure);
541 input_mt_slot(input_dev, id);
542 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER,
543 status & MXT_DETECT);
545 if (status & MXT_DETECT) {
546 input_report_abs(input_dev, ABS_MT_POSITION_X, x);
547 input_report_abs(input_dev, ABS_MT_POSITION_Y, y);
548 input_report_abs(input_dev, ABS_MT_PRESSURE, pressure);
549 input_report_abs(input_dev, ABS_MT_TOUCH_MAJOR, area);
553 static unsigned mxt_extract_T6_csum(const u8 *csum)
555 return csum[0] | (csum[1] << 8) | (csum[2] << 16);
558 static bool mxt_is_T9_message(struct mxt_data *data, struct mxt_message *msg)
560 u8 id = msg->reportid;
561 return (id >= data->T9_reportid_min && id <= data->T9_reportid_max);
564 static irqreturn_t mxt_interrupt(int irq, void *dev_id)
566 struct mxt_data *data = dev_id;
567 struct mxt_message message;
568 const u8 *payload = &message.message[0];
569 struct device *dev = &data->client->dev;
571 bool update_input = false;
574 if (mxt_read_message(data, &message)) {
575 dev_err(dev, "Failed to read message\n");
579 reportid = message.reportid;
581 if (reportid == data->T6_reportid) {
582 u8 status = payload[0];
583 unsigned csum = mxt_extract_T6_csum(&payload[1]);
584 dev_dbg(dev, "Status: %02x Config Checksum: %06x\n",
586 } else if (mxt_is_T9_message(data, &message)) {
587 int id = reportid - data->T9_reportid_min;
588 mxt_input_touchevent(data, &message, id);
591 mxt_dump_message(dev, &message);
593 } while (reportid != 0xff);
596 input_mt_report_pointer_emulation(data->input_dev, false);
597 input_sync(data->input_dev);
604 static int mxt_check_reg_init(struct mxt_data *data)
606 const struct mxt_platform_data *pdata = data->pdata;
607 struct mxt_object *object;
608 struct device *dev = &data->client->dev;
613 if (!pdata->config) {
614 dev_dbg(dev, "No cfg data defined, skipping reg init\n");
618 for (i = 0; i < data->info.object_num; i++) {
619 object = data->object_table + i;
621 if (!mxt_object_writable(object->type))
624 size = (object->size + 1) * (object->instances + 1);
625 if (index + size > pdata->config_length) {
626 dev_err(dev, "Not enough config data!\n");
630 ret = __mxt_write_reg(data->client, object->start_address,
631 size, &pdata->config[index]);
640 static int mxt_make_highchg(struct mxt_data *data)
642 struct device *dev = &data->client->dev;
643 struct mxt_message message;
647 /* Read dummy message to make high CHG pin */
649 error = mxt_read_message(data, &message);
652 } while (message.reportid != 0xff && --count);
655 dev_err(dev, "CHG pin isn't cleared\n");
662 static void mxt_handle_pdata(struct mxt_data *data)
664 const struct mxt_platform_data *pdata = data->pdata;
667 /* Set touchscreen lines */
668 mxt_write_object(data, MXT_TOUCH_MULTI_T9, MXT_TOUCH_XSIZE,
670 mxt_write_object(data, MXT_TOUCH_MULTI_T9, MXT_TOUCH_YSIZE,
673 /* Set touchscreen orient */
674 mxt_write_object(data, MXT_TOUCH_MULTI_T9, MXT_TOUCH_ORIENT,
677 /* Set touchscreen burst length */
678 mxt_write_object(data, MXT_TOUCH_MULTI_T9,
679 MXT_TOUCH_BLEN, pdata->blen);
681 /* Set touchscreen threshold */
682 mxt_write_object(data, MXT_TOUCH_MULTI_T9,
683 MXT_TOUCH_TCHTHR, pdata->threshold);
685 /* Set touchscreen resolution */
686 mxt_write_object(data, MXT_TOUCH_MULTI_T9,
687 MXT_TOUCH_XRANGE_LSB, (pdata->x_size - 1) & 0xff);
688 mxt_write_object(data, MXT_TOUCH_MULTI_T9,
689 MXT_TOUCH_XRANGE_MSB, (pdata->x_size - 1) >> 8);
690 mxt_write_object(data, MXT_TOUCH_MULTI_T9,
691 MXT_TOUCH_YRANGE_LSB, (pdata->y_size - 1) & 0xff);
692 mxt_write_object(data, MXT_TOUCH_MULTI_T9,
693 MXT_TOUCH_YRANGE_MSB, (pdata->y_size - 1) >> 8);
695 /* Set touchscreen voltage */
696 if (pdata->voltage) {
697 if (pdata->voltage < MXT_VOLTAGE_DEFAULT) {
698 voltage = (MXT_VOLTAGE_DEFAULT - pdata->voltage) /
700 voltage = 0xff - voltage + 1;
702 voltage = (pdata->voltage - MXT_VOLTAGE_DEFAULT) /
705 mxt_write_object(data, MXT_SPT_CTECONFIG_T28,
706 MXT_CTE_VOLTAGE, voltage);
710 static int mxt_get_info(struct mxt_data *data)
712 struct i2c_client *client = data->client;
713 struct mxt_info *info = &data->info;
716 /* Read 7-byte info block starting at address 0 */
717 error = __mxt_read_reg(client, MXT_INFO, sizeof(*info), info);
724 static int mxt_get_object_table(struct mxt_data *data)
726 struct i2c_client *client = data->client;
732 table_size = data->info.object_num * sizeof(struct mxt_object);
733 error = __mxt_read_reg(client, MXT_OBJECT_START, table_size,
738 /* Valid Report IDs start counting from 1 */
740 for (i = 0; i < data->info.object_num; i++) {
741 struct mxt_object *object = data->object_table + i;
744 le16_to_cpus(&object->start_address);
746 if (object->num_report_ids) {
748 reportid += object->num_report_ids *
749 (object->instances + 1);
750 max_id = reportid - 1;
756 dev_dbg(&data->client->dev,
757 "Type %2d Start %3d Size %3d Instances %2d ReportIDs %3u : %3u\n",
758 object->type, object->start_address, object->size + 1,
759 object->instances + 1, min_id, max_id);
761 switch (object->type) {
762 case MXT_GEN_COMMAND_T6:
763 data->T6_reportid = min_id;
765 case MXT_TOUCH_MULTI_T9:
766 data->T9_reportid_min = min_id;
767 data->T9_reportid_max = max_id;
775 static void mxt_free_object_table(struct mxt_data *data)
777 kfree(data->object_table);
778 data->object_table = NULL;
779 data->T6_reportid = 0;
780 data->T9_reportid_min = 0;
781 data->T9_reportid_max = 0;
785 static int mxt_initialize(struct mxt_data *data)
787 struct i2c_client *client = data->client;
788 struct mxt_info *info = &data->info;
792 error = mxt_get_info(data);
796 data->object_table = kcalloc(info->object_num,
797 sizeof(struct mxt_object),
799 if (!data->object_table) {
800 dev_err(&client->dev, "Failed to allocate memory\n");
804 /* Get object table information */
805 error = mxt_get_object_table(data);
807 goto err_free_object_table;
809 /* Check register init values */
810 error = mxt_check_reg_init(data);
812 goto err_free_object_table;
814 mxt_handle_pdata(data);
816 /* Backup to memory */
817 mxt_write_object(data, MXT_GEN_COMMAND_T6,
818 MXT_COMMAND_BACKUPNV,
820 msleep(MXT_BACKUP_TIME);
823 mxt_write_object(data, MXT_GEN_COMMAND_T6,
824 MXT_COMMAND_RESET, 1);
825 msleep(MXT_RESET_TIME);
827 /* Update matrix size at info struct */
828 error = mxt_read_reg(client, MXT_MATRIX_X_SIZE, &val);
830 goto err_free_object_table;
831 info->matrix_xsize = val;
833 error = mxt_read_reg(client, MXT_MATRIX_Y_SIZE, &val);
835 goto err_free_object_table;
836 info->matrix_ysize = val;
838 dev_info(&client->dev,
839 "Family ID: %u Variant ID: %u Major.Minor.Build: %u.%u.%02X\n",
840 info->family_id, info->variant_id, info->version >> 4,
841 info->version & 0xf, info->build);
843 dev_info(&client->dev,
844 "Matrix X Size: %u Matrix Y Size: %u Object Num: %u\n",
845 info->matrix_xsize, info->matrix_ysize,
850 err_free_object_table:
851 mxt_free_object_table(data);
855 static void mxt_calc_resolution(struct mxt_data *data)
857 unsigned int max_x = data->pdata->x_size - 1;
858 unsigned int max_y = data->pdata->y_size - 1;
860 if (data->pdata->orient & MXT_XY_SWITCH) {
869 /* Firmware Version is returned as Major.Minor.Build */
870 static ssize_t mxt_fw_version_show(struct device *dev,
871 struct device_attribute *attr, char *buf)
873 struct mxt_data *data = dev_get_drvdata(dev);
874 struct mxt_info *info = &data->info;
875 return scnprintf(buf, PAGE_SIZE, "%u.%u.%02X\n",
876 info->version >> 4, info->version & 0xf, info->build);
879 /* Hardware Version is returned as FamilyID.VariantID */
880 static ssize_t mxt_hw_version_show(struct device *dev,
881 struct device_attribute *attr, char *buf)
883 struct mxt_data *data = dev_get_drvdata(dev);
884 struct mxt_info *info = &data->info;
885 return scnprintf(buf, PAGE_SIZE, "%u.%u\n",
886 info->family_id, info->variant_id);
889 static ssize_t mxt_show_instance(char *buf, int count,
890 struct mxt_object *object, int instance,
895 if (object->instances > 0)
896 count += scnprintf(buf + count, PAGE_SIZE - count,
897 "Instance %u\n", instance);
899 for (i = 0; i < object->size + 1; i++)
900 count += scnprintf(buf + count, PAGE_SIZE - count,
901 "\t[%2u]: %02x (%d)\n", i, val[i], val[i]);
902 count += scnprintf(buf + count, PAGE_SIZE - count, "\n");
907 static ssize_t mxt_object_show(struct device *dev,
908 struct device_attribute *attr, char *buf)
910 struct mxt_data *data = dev_get_drvdata(dev);
911 struct mxt_object *object;
917 /* Pre-allocate buffer large enough to hold max sized object. */
918 obuf = kmalloc(256, GFP_KERNEL);
923 for (i = 0; i < data->info.object_num; i++) {
924 object = data->object_table + i;
926 if (!mxt_object_readable(object->type))
929 count += scnprintf(buf + count, PAGE_SIZE - count,
930 "T%u:\n", object->type);
932 for (j = 0; j < object->instances + 1; j++) {
933 u16 size = object->size + 1;
934 u16 addr = object->start_address + j * size;
936 error = __mxt_read_reg(data->client, addr, size, obuf);
940 count = mxt_show_instance(buf, count, object, j, obuf);
946 return error ?: count;
949 static int mxt_load_fw(struct device *dev, const char *fn)
951 struct mxt_data *data = dev_get_drvdata(dev);
952 struct i2c_client *client = data->client;
953 const struct firmware *fw = NULL;
954 unsigned int frame_size;
955 unsigned int pos = 0;
958 ret = request_firmware(&fw, fn, dev);
960 dev_err(dev, "Unable to open firmware %s\n", fn);
964 /* Change to the bootloader mode */
965 mxt_write_object(data, MXT_GEN_COMMAND_T6,
966 MXT_COMMAND_RESET, MXT_BOOT_VALUE);
967 msleep(MXT_RESET_TIME);
969 /* Change to slave address of bootloader */
970 if (client->addr == MXT_APP_LOW)
971 client->addr = MXT_BOOT_LOW;
973 client->addr = MXT_BOOT_HIGH;
975 ret = mxt_check_bootloader(client, MXT_WAITING_BOOTLOAD_CMD);
979 /* Unlock bootloader */
980 mxt_unlock_bootloader(client);
982 while (pos < fw->size) {
983 ret = mxt_check_bootloader(client,
984 MXT_WAITING_FRAME_DATA);
988 frame_size = ((*(fw->data + pos) << 8) | *(fw->data + pos + 1));
990 /* We should add 2 at frame size as the the firmware data is not
991 * included the CRC bytes.
995 /* Write one frame to device */
996 mxt_fw_write(client, fw->data + pos, frame_size);
998 ret = mxt_check_bootloader(client,
1005 dev_dbg(dev, "Updated %d bytes / %zd bytes\n", pos, fw->size);
1009 release_firmware(fw);
1011 /* Change to slave address of application */
1012 if (client->addr == MXT_BOOT_LOW)
1013 client->addr = MXT_APP_LOW;
1015 client->addr = MXT_APP_HIGH;
1020 static ssize_t mxt_update_fw_store(struct device *dev,
1021 struct device_attribute *attr,
1022 const char *buf, size_t count)
1024 struct mxt_data *data = dev_get_drvdata(dev);
1027 disable_irq(data->irq);
1029 error = mxt_load_fw(dev, MXT_FW_NAME);
1031 dev_err(dev, "The firmware update failed(%d)\n", error);
1034 dev_dbg(dev, "The firmware update succeeded\n");
1036 /* Wait for reset */
1037 msleep(MXT_FWRESET_TIME);
1039 mxt_free_object_table(data);
1041 mxt_initialize(data);
1044 enable_irq(data->irq);
1046 error = mxt_make_highchg(data);
1053 static DEVICE_ATTR(fw_version, S_IRUGO, mxt_fw_version_show, NULL);
1054 static DEVICE_ATTR(hw_version, S_IRUGO, mxt_hw_version_show, NULL);
1055 static DEVICE_ATTR(object, S_IRUGO, mxt_object_show, NULL);
1056 static DEVICE_ATTR(update_fw, S_IWUSR, NULL, mxt_update_fw_store);
1058 static struct attribute *mxt_attrs[] = {
1059 &dev_attr_fw_version.attr,
1060 &dev_attr_hw_version.attr,
1061 &dev_attr_object.attr,
1062 &dev_attr_update_fw.attr,
1066 static const struct attribute_group mxt_attr_group = {
1070 static void mxt_start(struct mxt_data *data)
1073 mxt_write_object(data,
1074 MXT_TOUCH_MULTI_T9, MXT_TOUCH_CTRL, 0x83);
1077 static void mxt_stop(struct mxt_data *data)
1080 mxt_write_object(data,
1081 MXT_TOUCH_MULTI_T9, MXT_TOUCH_CTRL, 0);
1084 static int mxt_input_open(struct input_dev *dev)
1086 struct mxt_data *data = input_get_drvdata(dev);
1093 static void mxt_input_close(struct input_dev *dev)
1095 struct mxt_data *data = input_get_drvdata(dev);
1100 static int __devinit mxt_probe(struct i2c_client *client,
1101 const struct i2c_device_id *id)
1103 const struct mxt_platform_data *pdata = client->dev.platform_data;
1104 struct mxt_data *data;
1105 struct input_dev *input_dev;
1107 unsigned int num_mt_slots;
1112 data = kzalloc(sizeof(struct mxt_data), GFP_KERNEL);
1113 input_dev = input_allocate_device();
1114 if (!data || !input_dev) {
1115 dev_err(&client->dev, "Failed to allocate memory\n");
1120 input_dev->name = "Atmel maXTouch Touchscreen";
1121 snprintf(data->phys, sizeof(data->phys), "i2c-%u-%04x/input0",
1122 client->adapter->nr, client->addr);
1123 input_dev->phys = data->phys;
1125 input_dev->id.bustype = BUS_I2C;
1126 input_dev->dev.parent = &client->dev;
1127 input_dev->open = mxt_input_open;
1128 input_dev->close = mxt_input_close;
1130 data->client = client;
1131 data->input_dev = input_dev;
1132 data->pdata = pdata;
1133 data->irq = client->irq;
1135 mxt_calc_resolution(data);
1137 error = mxt_initialize(data);
1141 __set_bit(EV_ABS, input_dev->evbit);
1142 __set_bit(EV_KEY, input_dev->evbit);
1143 __set_bit(BTN_TOUCH, input_dev->keybit);
1145 /* For single touch */
1146 input_set_abs_params(input_dev, ABS_X,
1147 0, data->max_x, 0, 0);
1148 input_set_abs_params(input_dev, ABS_Y,
1149 0, data->max_y, 0, 0);
1150 input_set_abs_params(input_dev, ABS_PRESSURE,
1153 /* For multi touch */
1154 num_mt_slots = data->T9_reportid_max - data->T9_reportid_min + 1;
1155 error = input_mt_init_slots(input_dev, num_mt_slots);
1157 goto err_free_object;
1158 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR,
1159 0, MXT_MAX_AREA, 0, 0);
1160 input_set_abs_params(input_dev, ABS_MT_POSITION_X,
1161 0, data->max_x, 0, 0);
1162 input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
1163 0, data->max_y, 0, 0);
1164 input_set_abs_params(input_dev, ABS_MT_PRESSURE,
1167 input_set_drvdata(input_dev, data);
1168 i2c_set_clientdata(client, data);
1170 error = request_threaded_irq(client->irq, NULL, mxt_interrupt,
1171 pdata->irqflags, client->name, data);
1173 dev_err(&client->dev, "Failed to register interrupt\n");
1174 goto err_free_object;
1177 error = mxt_make_highchg(data);
1181 error = input_register_device(input_dev);
1185 error = sysfs_create_group(&client->dev.kobj, &mxt_attr_group);
1187 dev_warn(&client->dev, "error creating sysfs entries.\n");
1192 free_irq(client->irq, data);
1194 kfree(data->object_table);
1196 input_free_device(input_dev);
1201 static int __devexit mxt_remove(struct i2c_client *client)
1203 struct mxt_data *data = i2c_get_clientdata(client);
1205 sysfs_remove_group(&client->dev.kobj, &mxt_attr_group);
1206 free_irq(data->irq, data);
1207 input_unregister_device(data->input_dev);
1208 kfree(data->object_table);
1214 #ifdef CONFIG_PM_SLEEP
1215 static int mxt_suspend(struct device *dev)
1217 struct i2c_client *client = to_i2c_client(dev);
1218 struct mxt_data *data = i2c_get_clientdata(client);
1219 struct input_dev *input_dev = data->input_dev;
1221 mutex_lock(&input_dev->mutex);
1223 if (input_dev->users)
1226 mutex_unlock(&input_dev->mutex);
1231 static int mxt_resume(struct device *dev)
1233 struct i2c_client *client = to_i2c_client(dev);
1234 struct mxt_data *data = i2c_get_clientdata(client);
1235 struct input_dev *input_dev = data->input_dev;
1238 mxt_write_object(data, MXT_GEN_COMMAND_T6,
1239 MXT_COMMAND_RESET, 1);
1241 msleep(MXT_RESET_TIME);
1243 mutex_lock(&input_dev->mutex);
1245 if (input_dev->users)
1248 mutex_unlock(&input_dev->mutex);
1254 static SIMPLE_DEV_PM_OPS(mxt_pm_ops, mxt_suspend, mxt_resume);
1256 static const struct i2c_device_id mxt_id[] = {
1257 { "qt602240_ts", 0 },
1258 { "atmel_mxt_ts", 0 },
1262 MODULE_DEVICE_TABLE(i2c, mxt_id);
1264 static struct i2c_driver mxt_driver = {
1266 .name = "atmel_mxt_ts",
1267 .owner = THIS_MODULE,
1271 .remove = __devexit_p(mxt_remove),
1275 module_i2c_driver(mxt_driver);
1277 /* Module information */
1278 MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
1279 MODULE_DESCRIPTION("Atmel maXTouch Touchscreen driver");
1280 MODULE_LICENSE("GPL");