PM / sleep: Asynchronous threads for suspend_noirq
[linux/fpc-iii.git] / drivers / input / touchscreen / atmel_mxt_ts.c
bloba70400754e92374be987679c12a89f49407c3dc3
1 /*
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/delay.h>
16 #include <linux/firmware.h>
17 #include <linux/i2c.h>
18 #include <linux/i2c/atmel_mxt_ts.h>
19 #include <linux/input/mt.h>
20 #include <linux/interrupt.h>
21 #include <linux/slab.h>
23 /* Version */
24 #define MXT_VER_20 20
25 #define MXT_VER_21 21
26 #define MXT_VER_22 22
28 /* Slave addresses */
29 #define MXT_APP_LOW 0x4a
30 #define MXT_APP_HIGH 0x4b
31 #define MXT_BOOT_LOW 0x24
32 #define MXT_BOOT_HIGH 0x25
34 /* Firmware */
35 #define MXT_FW_NAME "maxtouch.fw"
37 /* Registers */
38 #define MXT_INFO 0x00
39 #define MXT_FAMILY_ID 0x00
40 #define MXT_VARIANT_ID 0x01
41 #define MXT_VERSION 0x02
42 #define MXT_BUILD 0x03
43 #define MXT_MATRIX_X_SIZE 0x04
44 #define MXT_MATRIX_Y_SIZE 0x05
45 #define MXT_OBJECT_NUM 0x06
46 #define MXT_OBJECT_START 0x07
48 #define MXT_OBJECT_SIZE 6
50 /* Object types */
51 #define MXT_DEBUG_DIAGNOSTIC_T37 37
52 #define MXT_GEN_MESSAGE_T5 5
53 #define MXT_GEN_COMMAND_T6 6
54 #define MXT_GEN_POWER_T7 7
55 #define MXT_GEN_ACQUIRE_T8 8
56 #define MXT_GEN_DATASOURCE_T53 53
57 #define MXT_TOUCH_MULTI_T9 9
58 #define MXT_TOUCH_KEYARRAY_T15 15
59 #define MXT_TOUCH_PROXIMITY_T23 23
60 #define MXT_TOUCH_PROXKEY_T52 52
61 #define MXT_PROCI_GRIPFACE_T20 20
62 #define MXT_PROCG_NOISE_T22 22
63 #define MXT_PROCI_ONETOUCH_T24 24
64 #define MXT_PROCI_TWOTOUCH_T27 27
65 #define MXT_PROCI_GRIP_T40 40
66 #define MXT_PROCI_PALM_T41 41
67 #define MXT_PROCI_TOUCHSUPPRESSION_T42 42
68 #define MXT_PROCI_STYLUS_T47 47
69 #define MXT_PROCG_NOISESUPPRESSION_T48 48
70 #define MXT_SPT_COMMSCONFIG_T18 18
71 #define MXT_SPT_GPIOPWM_T19 19
72 #define MXT_SPT_SELFTEST_T25 25
73 #define MXT_SPT_CTECONFIG_T28 28
74 #define MXT_SPT_USERDATA_T38 38
75 #define MXT_SPT_DIGITIZER_T43 43
76 #define MXT_SPT_MESSAGECOUNT_T44 44
77 #define MXT_SPT_CTECONFIG_T46 46
79 /* MXT_GEN_COMMAND_T6 field */
80 #define MXT_COMMAND_RESET 0
81 #define MXT_COMMAND_BACKUPNV 1
82 #define MXT_COMMAND_CALIBRATE 2
83 #define MXT_COMMAND_REPORTALL 3
84 #define MXT_COMMAND_DIAGNOSTIC 5
86 /* MXT_GEN_POWER_T7 field */
87 #define MXT_POWER_IDLEACQINT 0
88 #define MXT_POWER_ACTVACQINT 1
89 #define MXT_POWER_ACTV2IDLETO 2
91 /* MXT_GEN_ACQUIRE_T8 field */
92 #define MXT_ACQUIRE_CHRGTIME 0
93 #define MXT_ACQUIRE_TCHDRIFT 2
94 #define MXT_ACQUIRE_DRIFTST 3
95 #define MXT_ACQUIRE_TCHAUTOCAL 4
96 #define MXT_ACQUIRE_SYNC 5
97 #define MXT_ACQUIRE_ATCHCALST 6
98 #define MXT_ACQUIRE_ATCHCALSTHR 7
100 /* MXT_TOUCH_MULTI_T9 field */
101 #define MXT_TOUCH_CTRL 0
102 #define MXT_TOUCH_XORIGIN 1
103 #define MXT_TOUCH_YORIGIN 2
104 #define MXT_TOUCH_XSIZE 3
105 #define MXT_TOUCH_YSIZE 4
106 #define MXT_TOUCH_BLEN 6
107 #define MXT_TOUCH_TCHTHR 7
108 #define MXT_TOUCH_TCHDI 8
109 #define MXT_TOUCH_ORIENT 9
110 #define MXT_TOUCH_MOVHYSTI 11
111 #define MXT_TOUCH_MOVHYSTN 12
112 #define MXT_TOUCH_NUMTOUCH 14
113 #define MXT_TOUCH_MRGHYST 15
114 #define MXT_TOUCH_MRGTHR 16
115 #define MXT_TOUCH_AMPHYST 17
116 #define MXT_TOUCH_XRANGE_LSB 18
117 #define MXT_TOUCH_XRANGE_MSB 19
118 #define MXT_TOUCH_YRANGE_LSB 20
119 #define MXT_TOUCH_YRANGE_MSB 21
120 #define MXT_TOUCH_XLOCLIP 22
121 #define MXT_TOUCH_XHICLIP 23
122 #define MXT_TOUCH_YLOCLIP 24
123 #define MXT_TOUCH_YHICLIP 25
124 #define MXT_TOUCH_XEDGECTRL 26
125 #define MXT_TOUCH_XEDGEDIST 27
126 #define MXT_TOUCH_YEDGECTRL 28
127 #define MXT_TOUCH_YEDGEDIST 29
128 #define MXT_TOUCH_JUMPLIMIT 30
130 /* MXT_PROCI_GRIPFACE_T20 field */
131 #define MXT_GRIPFACE_CTRL 0
132 #define MXT_GRIPFACE_XLOGRIP 1
133 #define MXT_GRIPFACE_XHIGRIP 2
134 #define MXT_GRIPFACE_YLOGRIP 3
135 #define MXT_GRIPFACE_YHIGRIP 4
136 #define MXT_GRIPFACE_MAXTCHS 5
137 #define MXT_GRIPFACE_SZTHR1 7
138 #define MXT_GRIPFACE_SZTHR2 8
139 #define MXT_GRIPFACE_SHPTHR1 9
140 #define MXT_GRIPFACE_SHPTHR2 10
141 #define MXT_GRIPFACE_SUPEXTTO 11
143 /* MXT_PROCI_NOISE field */
144 #define MXT_NOISE_CTRL 0
145 #define MXT_NOISE_OUTFLEN 1
146 #define MXT_NOISE_GCAFUL_LSB 3
147 #define MXT_NOISE_GCAFUL_MSB 4
148 #define MXT_NOISE_GCAFLL_LSB 5
149 #define MXT_NOISE_GCAFLL_MSB 6
150 #define MXT_NOISE_ACTVGCAFVALID 7
151 #define MXT_NOISE_NOISETHR 8
152 #define MXT_NOISE_FREQHOPSCALE 10
153 #define MXT_NOISE_FREQ0 11
154 #define MXT_NOISE_FREQ1 12
155 #define MXT_NOISE_FREQ2 13
156 #define MXT_NOISE_FREQ3 14
157 #define MXT_NOISE_FREQ4 15
158 #define MXT_NOISE_IDLEGCAFVALID 16
160 /* MXT_SPT_COMMSCONFIG_T18 */
161 #define MXT_COMMS_CTRL 0
162 #define MXT_COMMS_CMD 1
164 /* MXT_SPT_CTECONFIG_T28 field */
165 #define MXT_CTE_CTRL 0
166 #define MXT_CTE_CMD 1
167 #define MXT_CTE_MODE 2
168 #define MXT_CTE_IDLEGCAFDEPTH 3
169 #define MXT_CTE_ACTVGCAFDEPTH 4
170 #define MXT_CTE_VOLTAGE 5
172 #define MXT_VOLTAGE_DEFAULT 2700000
173 #define MXT_VOLTAGE_STEP 10000
175 /* Define for MXT_GEN_COMMAND_T6 */
176 #define MXT_BOOT_VALUE 0xa5
177 #define MXT_BACKUP_VALUE 0x55
178 #define MXT_BACKUP_TIME 50 /* msec */
179 #define MXT_RESET_TIME 200 /* msec */
181 #define MXT_FWRESET_TIME 175 /* msec */
183 /* MXT_SPT_GPIOPWM_T19 field */
184 #define MXT_GPIO0_MASK 0x04
185 #define MXT_GPIO1_MASK 0x08
186 #define MXT_GPIO2_MASK 0x10
187 #define MXT_GPIO3_MASK 0x20
189 /* Command to unlock bootloader */
190 #define MXT_UNLOCK_CMD_MSB 0xaa
191 #define MXT_UNLOCK_CMD_LSB 0xdc
193 /* Bootloader mode status */
194 #define MXT_WAITING_BOOTLOAD_CMD 0xc0 /* valid 7 6 bit only */
195 #define MXT_WAITING_FRAME_DATA 0x80 /* valid 7 6 bit only */
196 #define MXT_FRAME_CRC_CHECK 0x02
197 #define MXT_FRAME_CRC_FAIL 0x03
198 #define MXT_FRAME_CRC_PASS 0x04
199 #define MXT_APP_CRC_FAIL 0x40 /* valid 7 8 bit only */
200 #define MXT_BOOT_STATUS_MASK 0x3f
202 /* Touch status */
203 #define MXT_UNGRIP (1 << 0)
204 #define MXT_SUPPRESS (1 << 1)
205 #define MXT_AMP (1 << 2)
206 #define MXT_VECTOR (1 << 3)
207 #define MXT_MOVE (1 << 4)
208 #define MXT_RELEASE (1 << 5)
209 #define MXT_PRESS (1 << 6)
210 #define MXT_DETECT (1 << 7)
212 /* Touch orient bits */
213 #define MXT_XY_SWITCH (1 << 0)
214 #define MXT_X_INVERT (1 << 1)
215 #define MXT_Y_INVERT (1 << 2)
217 /* Touchscreen absolute values */
218 #define MXT_MAX_AREA 0xff
220 #define MXT_PIXELS_PER_MM 20
222 struct mxt_info {
223 u8 family_id;
224 u8 variant_id;
225 u8 version;
226 u8 build;
227 u8 matrix_xsize;
228 u8 matrix_ysize;
229 u8 object_num;
232 struct mxt_object {
233 u8 type;
234 u16 start_address;
235 u8 size; /* Size of each instance - 1 */
236 u8 instances; /* Number of instances - 1 */
237 u8 num_report_ids;
238 } __packed;
240 struct mxt_message {
241 u8 reportid;
242 u8 message[7];
245 /* Each client has this additional data */
246 struct mxt_data {
247 struct i2c_client *client;
248 struct input_dev *input_dev;
249 char phys[64]; /* device physical location */
250 const struct mxt_platform_data *pdata;
251 struct mxt_object *object_table;
252 struct mxt_info info;
253 bool is_tp;
255 unsigned int irq;
256 unsigned int max_x;
257 unsigned int max_y;
259 /* Cached parameters from object table */
260 u8 T6_reportid;
261 u8 T9_reportid_min;
262 u8 T9_reportid_max;
263 u8 T19_reportid;
266 static bool mxt_object_readable(unsigned int type)
268 switch (type) {
269 case MXT_GEN_COMMAND_T6:
270 case MXT_GEN_POWER_T7:
271 case MXT_GEN_ACQUIRE_T8:
272 case MXT_GEN_DATASOURCE_T53:
273 case MXT_TOUCH_MULTI_T9:
274 case MXT_TOUCH_KEYARRAY_T15:
275 case MXT_TOUCH_PROXIMITY_T23:
276 case MXT_TOUCH_PROXKEY_T52:
277 case MXT_PROCI_GRIPFACE_T20:
278 case MXT_PROCG_NOISE_T22:
279 case MXT_PROCI_ONETOUCH_T24:
280 case MXT_PROCI_TWOTOUCH_T27:
281 case MXT_PROCI_GRIP_T40:
282 case MXT_PROCI_PALM_T41:
283 case MXT_PROCI_TOUCHSUPPRESSION_T42:
284 case MXT_PROCI_STYLUS_T47:
285 case MXT_PROCG_NOISESUPPRESSION_T48:
286 case MXT_SPT_COMMSCONFIG_T18:
287 case MXT_SPT_GPIOPWM_T19:
288 case MXT_SPT_SELFTEST_T25:
289 case MXT_SPT_CTECONFIG_T28:
290 case MXT_SPT_USERDATA_T38:
291 case MXT_SPT_DIGITIZER_T43:
292 case MXT_SPT_CTECONFIG_T46:
293 return true;
294 default:
295 return false;
299 static bool mxt_object_writable(unsigned int type)
301 switch (type) {
302 case MXT_GEN_COMMAND_T6:
303 case MXT_GEN_POWER_T7:
304 case MXT_GEN_ACQUIRE_T8:
305 case MXT_TOUCH_MULTI_T9:
306 case MXT_TOUCH_KEYARRAY_T15:
307 case MXT_TOUCH_PROXIMITY_T23:
308 case MXT_TOUCH_PROXKEY_T52:
309 case MXT_PROCI_GRIPFACE_T20:
310 case MXT_PROCG_NOISE_T22:
311 case MXT_PROCI_ONETOUCH_T24:
312 case MXT_PROCI_TWOTOUCH_T27:
313 case MXT_PROCI_GRIP_T40:
314 case MXT_PROCI_PALM_T41:
315 case MXT_PROCI_TOUCHSUPPRESSION_T42:
316 case MXT_PROCI_STYLUS_T47:
317 case MXT_PROCG_NOISESUPPRESSION_T48:
318 case MXT_SPT_COMMSCONFIG_T18:
319 case MXT_SPT_GPIOPWM_T19:
320 case MXT_SPT_SELFTEST_T25:
321 case MXT_SPT_CTECONFIG_T28:
322 case MXT_SPT_DIGITIZER_T43:
323 case MXT_SPT_CTECONFIG_T46:
324 return true;
325 default:
326 return false;
330 static void mxt_dump_message(struct device *dev,
331 struct mxt_message *message)
333 dev_dbg(dev, "reportid: %u\tmessage: %*ph\n",
334 message->reportid, 7, message->message);
337 static int mxt_check_bootloader(struct i2c_client *client,
338 unsigned int state)
340 u8 val;
342 recheck:
343 if (i2c_master_recv(client, &val, 1) != 1) {
344 dev_err(&client->dev, "%s: i2c recv failed\n", __func__);
345 return -EIO;
348 switch (state) {
349 case MXT_WAITING_BOOTLOAD_CMD:
350 case MXT_WAITING_FRAME_DATA:
351 val &= ~MXT_BOOT_STATUS_MASK;
352 break;
353 case MXT_FRAME_CRC_PASS:
354 if (val == MXT_FRAME_CRC_CHECK)
355 goto recheck;
356 break;
357 default:
358 return -EINVAL;
361 if (val != state) {
362 dev_err(&client->dev, "Unvalid bootloader mode state\n");
363 return -EINVAL;
366 return 0;
369 static int mxt_unlock_bootloader(struct i2c_client *client)
371 u8 buf[2];
373 buf[0] = MXT_UNLOCK_CMD_LSB;
374 buf[1] = MXT_UNLOCK_CMD_MSB;
376 if (i2c_master_send(client, buf, 2) != 2) {
377 dev_err(&client->dev, "%s: i2c send failed\n", __func__);
378 return -EIO;
381 return 0;
384 static int mxt_fw_write(struct i2c_client *client,
385 const u8 *data, unsigned int frame_size)
387 if (i2c_master_send(client, data, frame_size) != frame_size) {
388 dev_err(&client->dev, "%s: i2c send failed\n", __func__);
389 return -EIO;
392 return 0;
395 static int __mxt_read_reg(struct i2c_client *client,
396 u16 reg, u16 len, void *val)
398 struct i2c_msg xfer[2];
399 u8 buf[2];
400 int ret;
402 buf[0] = reg & 0xff;
403 buf[1] = (reg >> 8) & 0xff;
405 /* Write register */
406 xfer[0].addr = client->addr;
407 xfer[0].flags = 0;
408 xfer[0].len = 2;
409 xfer[0].buf = buf;
411 /* Read data */
412 xfer[1].addr = client->addr;
413 xfer[1].flags = I2C_M_RD;
414 xfer[1].len = len;
415 xfer[1].buf = val;
417 ret = i2c_transfer(client->adapter, xfer, 2);
418 if (ret == 2) {
419 ret = 0;
420 } else {
421 if (ret >= 0)
422 ret = -EIO;
423 dev_err(&client->dev, "%s: i2c transfer failed (%d)\n",
424 __func__, ret);
427 return ret;
430 static int mxt_read_reg(struct i2c_client *client, u16 reg, u8 *val)
432 return __mxt_read_reg(client, reg, 1, val);
435 static int __mxt_write_reg(struct i2c_client *client, u16 reg, u16 len,
436 const void *val)
438 u8 *buf;
439 size_t count;
440 int ret;
442 count = len + 2;
443 buf = kmalloc(count, GFP_KERNEL);
444 if (!buf)
445 return -ENOMEM;
447 buf[0] = reg & 0xff;
448 buf[1] = (reg >> 8) & 0xff;
449 memcpy(&buf[2], val, len);
451 ret = i2c_master_send(client, buf, count);
452 if (ret == count) {
453 ret = 0;
454 } else {
455 if (ret >= 0)
456 ret = -EIO;
457 dev_err(&client->dev, "%s: i2c send failed (%d)\n",
458 __func__, ret);
461 kfree(buf);
462 return ret;
465 static int mxt_write_reg(struct i2c_client *client, u16 reg, u8 val)
467 return __mxt_write_reg(client, reg, 1, &val);
470 static struct mxt_object *
471 mxt_get_object(struct mxt_data *data, u8 type)
473 struct mxt_object *object;
474 int i;
476 for (i = 0; i < data->info.object_num; i++) {
477 object = data->object_table + i;
478 if (object->type == type)
479 return object;
482 dev_err(&data->client->dev, "Invalid object type\n");
483 return NULL;
486 static int mxt_read_message(struct mxt_data *data,
487 struct mxt_message *message)
489 struct mxt_object *object;
490 u16 reg;
492 object = mxt_get_object(data, MXT_GEN_MESSAGE_T5);
493 if (!object)
494 return -EINVAL;
496 reg = object->start_address;
497 return __mxt_read_reg(data->client, reg,
498 sizeof(struct mxt_message), message);
501 static int mxt_write_object(struct mxt_data *data,
502 u8 type, u8 offset, u8 val)
504 struct mxt_object *object;
505 u16 reg;
507 object = mxt_get_object(data, type);
508 if (!object || offset >= object->size + 1)
509 return -EINVAL;
511 reg = object->start_address;
512 return mxt_write_reg(data->client, reg + offset, val);
515 static void mxt_input_button(struct mxt_data *data, struct mxt_message *message)
517 struct input_dev *input = data->input_dev;
518 bool button;
519 int i;
521 /* Active-low switch */
522 for (i = 0; i < MXT_NUM_GPIO; i++) {
523 if (data->pdata->key_map[i] == KEY_RESERVED)
524 continue;
525 button = !(message->message[0] & MXT_GPIO0_MASK << i);
526 input_report_key(input, data->pdata->key_map[i], button);
530 static void mxt_input_touchevent(struct mxt_data *data,
531 struct mxt_message *message, int id)
533 struct device *dev = &data->client->dev;
534 u8 status = message->message[0];
535 struct input_dev *input_dev = data->input_dev;
536 int x;
537 int y;
538 int area;
539 int pressure;
541 x = (message->message[1] << 4) | ((message->message[3] >> 4) & 0xf);
542 y = (message->message[2] << 4) | ((message->message[3] & 0xf));
543 if (data->max_x < 1024)
544 x = x >> 2;
545 if (data->max_y < 1024)
546 y = y >> 2;
548 area = message->message[4];
549 pressure = message->message[5];
551 dev_dbg(dev,
552 "[%u] %c%c%c%c%c%c%c%c x: %5u y: %5u area: %3u amp: %3u\n",
554 (status & MXT_DETECT) ? 'D' : '.',
555 (status & MXT_PRESS) ? 'P' : '.',
556 (status & MXT_RELEASE) ? 'R' : '.',
557 (status & MXT_MOVE) ? 'M' : '.',
558 (status & MXT_VECTOR) ? 'V' : '.',
559 (status & MXT_AMP) ? 'A' : '.',
560 (status & MXT_SUPPRESS) ? 'S' : '.',
561 (status & MXT_UNGRIP) ? 'U' : '.',
562 x, y, area, pressure);
564 input_mt_slot(input_dev, id);
565 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER,
566 status & MXT_DETECT);
568 if (status & MXT_DETECT) {
569 input_report_abs(input_dev, ABS_MT_POSITION_X, x);
570 input_report_abs(input_dev, ABS_MT_POSITION_Y, y);
571 input_report_abs(input_dev, ABS_MT_PRESSURE, pressure);
572 input_report_abs(input_dev, ABS_MT_TOUCH_MAJOR, area);
576 static unsigned mxt_extract_T6_csum(const u8 *csum)
578 return csum[0] | (csum[1] << 8) | (csum[2] << 16);
581 static bool mxt_is_T9_message(struct mxt_data *data, struct mxt_message *msg)
583 u8 id = msg->reportid;
584 return (id >= data->T9_reportid_min && id <= data->T9_reportid_max);
587 static irqreturn_t mxt_interrupt(int irq, void *dev_id)
589 struct mxt_data *data = dev_id;
590 struct mxt_message message;
591 const u8 *payload = &message.message[0];
592 struct device *dev = &data->client->dev;
593 u8 reportid;
594 bool update_input = false;
596 do {
597 if (mxt_read_message(data, &message)) {
598 dev_err(dev, "Failed to read message\n");
599 goto end;
602 reportid = message.reportid;
604 if (reportid == data->T6_reportid) {
605 u8 status = payload[0];
606 unsigned csum = mxt_extract_T6_csum(&payload[1]);
607 dev_dbg(dev, "Status: %02x Config Checksum: %06x\n",
608 status, csum);
609 } else if (mxt_is_T9_message(data, &message)) {
610 int id = reportid - data->T9_reportid_min;
611 mxt_input_touchevent(data, &message, id);
612 update_input = true;
613 } else if (message.reportid == data->T19_reportid) {
614 mxt_input_button(data, &message);
615 update_input = true;
616 } else {
617 mxt_dump_message(dev, &message);
619 } while (reportid != 0xff);
621 if (update_input) {
622 input_mt_report_pointer_emulation(data->input_dev, false);
623 input_sync(data->input_dev);
626 end:
627 return IRQ_HANDLED;
630 static int mxt_check_reg_init(struct mxt_data *data)
632 const struct mxt_platform_data *pdata = data->pdata;
633 struct mxt_object *object;
634 struct device *dev = &data->client->dev;
635 int index = 0;
636 int i, size;
637 int ret;
639 if (!pdata->config) {
640 dev_dbg(dev, "No cfg data defined, skipping reg init\n");
641 return 0;
644 for (i = 0; i < data->info.object_num; i++) {
645 object = data->object_table + i;
647 if (!mxt_object_writable(object->type))
648 continue;
650 size = (object->size + 1) * (object->instances + 1);
651 if (index + size > pdata->config_length) {
652 dev_err(dev, "Not enough config data!\n");
653 return -EINVAL;
656 ret = __mxt_write_reg(data->client, object->start_address,
657 size, &pdata->config[index]);
658 if (ret)
659 return ret;
660 index += size;
663 return 0;
666 static int mxt_make_highchg(struct mxt_data *data)
668 struct device *dev = &data->client->dev;
669 struct mxt_message message;
670 int count = 10;
671 int error;
673 /* Read dummy message to make high CHG pin */
674 do {
675 error = mxt_read_message(data, &message);
676 if (error)
677 return error;
678 } while (message.reportid != 0xff && --count);
680 if (!count) {
681 dev_err(dev, "CHG pin isn't cleared\n");
682 return -EBUSY;
685 return 0;
688 static void mxt_handle_pdata(struct mxt_data *data)
690 const struct mxt_platform_data *pdata = data->pdata;
691 u8 voltage;
693 /* Set touchscreen lines */
694 mxt_write_object(data, MXT_TOUCH_MULTI_T9, MXT_TOUCH_XSIZE,
695 pdata->x_line);
696 mxt_write_object(data, MXT_TOUCH_MULTI_T9, MXT_TOUCH_YSIZE,
697 pdata->y_line);
699 /* Set touchscreen orient */
700 mxt_write_object(data, MXT_TOUCH_MULTI_T9, MXT_TOUCH_ORIENT,
701 pdata->orient);
703 /* Set touchscreen burst length */
704 mxt_write_object(data, MXT_TOUCH_MULTI_T9,
705 MXT_TOUCH_BLEN, pdata->blen);
707 /* Set touchscreen threshold */
708 mxt_write_object(data, MXT_TOUCH_MULTI_T9,
709 MXT_TOUCH_TCHTHR, pdata->threshold);
711 /* Set touchscreen resolution */
712 mxt_write_object(data, MXT_TOUCH_MULTI_T9,
713 MXT_TOUCH_XRANGE_LSB, (pdata->x_size - 1) & 0xff);
714 mxt_write_object(data, MXT_TOUCH_MULTI_T9,
715 MXT_TOUCH_XRANGE_MSB, (pdata->x_size - 1) >> 8);
716 mxt_write_object(data, MXT_TOUCH_MULTI_T9,
717 MXT_TOUCH_YRANGE_LSB, (pdata->y_size - 1) & 0xff);
718 mxt_write_object(data, MXT_TOUCH_MULTI_T9,
719 MXT_TOUCH_YRANGE_MSB, (pdata->y_size - 1) >> 8);
721 /* Set touchscreen voltage */
722 if (pdata->voltage) {
723 if (pdata->voltage < MXT_VOLTAGE_DEFAULT) {
724 voltage = (MXT_VOLTAGE_DEFAULT - pdata->voltage) /
725 MXT_VOLTAGE_STEP;
726 voltage = 0xff - voltage + 1;
727 } else
728 voltage = (pdata->voltage - MXT_VOLTAGE_DEFAULT) /
729 MXT_VOLTAGE_STEP;
731 mxt_write_object(data, MXT_SPT_CTECONFIG_T28,
732 MXT_CTE_VOLTAGE, voltage);
736 static int mxt_get_info(struct mxt_data *data)
738 struct i2c_client *client = data->client;
739 struct mxt_info *info = &data->info;
740 int error;
742 /* Read 7-byte info block starting at address 0 */
743 error = __mxt_read_reg(client, MXT_INFO, sizeof(*info), info);
744 if (error)
745 return error;
747 return 0;
750 static int mxt_get_object_table(struct mxt_data *data)
752 struct i2c_client *client = data->client;
753 size_t table_size;
754 int error;
755 int i;
756 u8 reportid;
758 table_size = data->info.object_num * sizeof(struct mxt_object);
759 error = __mxt_read_reg(client, MXT_OBJECT_START, table_size,
760 data->object_table);
761 if (error)
762 return error;
764 /* Valid Report IDs start counting from 1 */
765 reportid = 1;
766 for (i = 0; i < data->info.object_num; i++) {
767 struct mxt_object *object = data->object_table + i;
768 u8 min_id, max_id;
770 le16_to_cpus(&object->start_address);
772 if (object->num_report_ids) {
773 min_id = reportid;
774 reportid += object->num_report_ids *
775 (object->instances + 1);
776 max_id = reportid - 1;
777 } else {
778 min_id = 0;
779 max_id = 0;
782 dev_dbg(&data->client->dev,
783 "Type %2d Start %3d Size %3d Instances %2d ReportIDs %3u : %3u\n",
784 object->type, object->start_address, object->size + 1,
785 object->instances + 1, min_id, max_id);
787 switch (object->type) {
788 case MXT_GEN_COMMAND_T6:
789 data->T6_reportid = min_id;
790 break;
791 case MXT_TOUCH_MULTI_T9:
792 data->T9_reportid_min = min_id;
793 data->T9_reportid_max = max_id;
794 break;
795 case MXT_SPT_GPIOPWM_T19:
796 data->T19_reportid = min_id;
797 break;
801 return 0;
804 static void mxt_free_object_table(struct mxt_data *data)
806 kfree(data->object_table);
807 data->object_table = NULL;
808 data->T6_reportid = 0;
809 data->T9_reportid_min = 0;
810 data->T9_reportid_max = 0;
811 data->T19_reportid = 0;
814 static int mxt_initialize(struct mxt_data *data)
816 struct i2c_client *client = data->client;
817 struct mxt_info *info = &data->info;
818 int error;
819 u8 val;
821 error = mxt_get_info(data);
822 if (error)
823 return error;
825 data->object_table = kcalloc(info->object_num,
826 sizeof(struct mxt_object),
827 GFP_KERNEL);
828 if (!data->object_table) {
829 dev_err(&client->dev, "Failed to allocate memory\n");
830 return -ENOMEM;
833 /* Get object table information */
834 error = mxt_get_object_table(data);
835 if (error)
836 goto err_free_object_table;
838 /* Check register init values */
839 error = mxt_check_reg_init(data);
840 if (error)
841 goto err_free_object_table;
843 mxt_handle_pdata(data);
845 /* Backup to memory */
846 mxt_write_object(data, MXT_GEN_COMMAND_T6,
847 MXT_COMMAND_BACKUPNV,
848 MXT_BACKUP_VALUE);
849 msleep(MXT_BACKUP_TIME);
851 /* Soft reset */
852 mxt_write_object(data, MXT_GEN_COMMAND_T6,
853 MXT_COMMAND_RESET, 1);
854 msleep(MXT_RESET_TIME);
856 /* Update matrix size at info struct */
857 error = mxt_read_reg(client, MXT_MATRIX_X_SIZE, &val);
858 if (error)
859 goto err_free_object_table;
860 info->matrix_xsize = val;
862 error = mxt_read_reg(client, MXT_MATRIX_Y_SIZE, &val);
863 if (error)
864 goto err_free_object_table;
865 info->matrix_ysize = val;
867 dev_info(&client->dev,
868 "Family ID: %u Variant ID: %u Major.Minor.Build: %u.%u.%02X\n",
869 info->family_id, info->variant_id, info->version >> 4,
870 info->version & 0xf, info->build);
872 dev_info(&client->dev,
873 "Matrix X Size: %u Matrix Y Size: %u Object Num: %u\n",
874 info->matrix_xsize, info->matrix_ysize,
875 info->object_num);
877 return 0;
879 err_free_object_table:
880 mxt_free_object_table(data);
881 return error;
884 static void mxt_calc_resolution(struct mxt_data *data)
886 unsigned int max_x = data->pdata->x_size - 1;
887 unsigned int max_y = data->pdata->y_size - 1;
889 if (data->pdata->orient & MXT_XY_SWITCH) {
890 data->max_x = max_y;
891 data->max_y = max_x;
892 } else {
893 data->max_x = max_x;
894 data->max_y = max_y;
898 /* Firmware Version is returned as Major.Minor.Build */
899 static ssize_t mxt_fw_version_show(struct device *dev,
900 struct device_attribute *attr, char *buf)
902 struct mxt_data *data = dev_get_drvdata(dev);
903 struct mxt_info *info = &data->info;
904 return scnprintf(buf, PAGE_SIZE, "%u.%u.%02X\n",
905 info->version >> 4, info->version & 0xf, info->build);
908 /* Hardware Version is returned as FamilyID.VariantID */
909 static ssize_t mxt_hw_version_show(struct device *dev,
910 struct device_attribute *attr, char *buf)
912 struct mxt_data *data = dev_get_drvdata(dev);
913 struct mxt_info *info = &data->info;
914 return scnprintf(buf, PAGE_SIZE, "%u.%u\n",
915 info->family_id, info->variant_id);
918 static ssize_t mxt_show_instance(char *buf, int count,
919 struct mxt_object *object, int instance,
920 const u8 *val)
922 int i;
924 if (object->instances > 0)
925 count += scnprintf(buf + count, PAGE_SIZE - count,
926 "Instance %u\n", instance);
928 for (i = 0; i < object->size + 1; i++)
929 count += scnprintf(buf + count, PAGE_SIZE - count,
930 "\t[%2u]: %02x (%d)\n", i, val[i], val[i]);
931 count += scnprintf(buf + count, PAGE_SIZE - count, "\n");
933 return count;
936 static ssize_t mxt_object_show(struct device *dev,
937 struct device_attribute *attr, char *buf)
939 struct mxt_data *data = dev_get_drvdata(dev);
940 struct mxt_object *object;
941 int count = 0;
942 int i, j;
943 int error;
944 u8 *obuf;
946 /* Pre-allocate buffer large enough to hold max sized object. */
947 obuf = kmalloc(256, GFP_KERNEL);
948 if (!obuf)
949 return -ENOMEM;
951 error = 0;
952 for (i = 0; i < data->info.object_num; i++) {
953 object = data->object_table + i;
955 if (!mxt_object_readable(object->type))
956 continue;
958 count += scnprintf(buf + count, PAGE_SIZE - count,
959 "T%u:\n", object->type);
961 for (j = 0; j < object->instances + 1; j++) {
962 u16 size = object->size + 1;
963 u16 addr = object->start_address + j * size;
965 error = __mxt_read_reg(data->client, addr, size, obuf);
966 if (error)
967 goto done;
969 count = mxt_show_instance(buf, count, object, j, obuf);
973 done:
974 kfree(obuf);
975 return error ?: count;
978 static int mxt_load_fw(struct device *dev, const char *fn)
980 struct mxt_data *data = dev_get_drvdata(dev);
981 struct i2c_client *client = data->client;
982 const struct firmware *fw = NULL;
983 unsigned int frame_size;
984 unsigned int pos = 0;
985 int ret;
987 ret = request_firmware(&fw, fn, dev);
988 if (ret) {
989 dev_err(dev, "Unable to open firmware %s\n", fn);
990 return ret;
993 /* Change to the bootloader mode */
994 mxt_write_object(data, MXT_GEN_COMMAND_T6,
995 MXT_COMMAND_RESET, MXT_BOOT_VALUE);
996 msleep(MXT_RESET_TIME);
998 /* Change to slave address of bootloader */
999 if (client->addr == MXT_APP_LOW)
1000 client->addr = MXT_BOOT_LOW;
1001 else
1002 client->addr = MXT_BOOT_HIGH;
1004 ret = mxt_check_bootloader(client, MXT_WAITING_BOOTLOAD_CMD);
1005 if (ret)
1006 goto out;
1008 /* Unlock bootloader */
1009 mxt_unlock_bootloader(client);
1011 while (pos < fw->size) {
1012 ret = mxt_check_bootloader(client,
1013 MXT_WAITING_FRAME_DATA);
1014 if (ret)
1015 goto out;
1017 frame_size = ((*(fw->data + pos) << 8) | *(fw->data + pos + 1));
1019 /* We should add 2 at frame size as the the firmware data is not
1020 * included the CRC bytes.
1022 frame_size += 2;
1024 /* Write one frame to device */
1025 mxt_fw_write(client, fw->data + pos, frame_size);
1027 ret = mxt_check_bootloader(client,
1028 MXT_FRAME_CRC_PASS);
1029 if (ret)
1030 goto out;
1032 pos += frame_size;
1034 dev_dbg(dev, "Updated %d bytes / %zd bytes\n", pos, fw->size);
1037 out:
1038 release_firmware(fw);
1040 /* Change to slave address of application */
1041 if (client->addr == MXT_BOOT_LOW)
1042 client->addr = MXT_APP_LOW;
1043 else
1044 client->addr = MXT_APP_HIGH;
1046 return ret;
1049 static ssize_t mxt_update_fw_store(struct device *dev,
1050 struct device_attribute *attr,
1051 const char *buf, size_t count)
1053 struct mxt_data *data = dev_get_drvdata(dev);
1054 int error;
1056 disable_irq(data->irq);
1058 error = mxt_load_fw(dev, MXT_FW_NAME);
1059 if (error) {
1060 dev_err(dev, "The firmware update failed(%d)\n", error);
1061 count = error;
1062 } else {
1063 dev_dbg(dev, "The firmware update succeeded\n");
1065 /* Wait for reset */
1066 msleep(MXT_FWRESET_TIME);
1068 mxt_free_object_table(data);
1070 mxt_initialize(data);
1073 enable_irq(data->irq);
1075 error = mxt_make_highchg(data);
1076 if (error)
1077 return error;
1079 return count;
1082 static DEVICE_ATTR(fw_version, S_IRUGO, mxt_fw_version_show, NULL);
1083 static DEVICE_ATTR(hw_version, S_IRUGO, mxt_hw_version_show, NULL);
1084 static DEVICE_ATTR(object, S_IRUGO, mxt_object_show, NULL);
1085 static DEVICE_ATTR(update_fw, S_IWUSR, NULL, mxt_update_fw_store);
1087 static struct attribute *mxt_attrs[] = {
1088 &dev_attr_fw_version.attr,
1089 &dev_attr_hw_version.attr,
1090 &dev_attr_object.attr,
1091 &dev_attr_update_fw.attr,
1092 NULL
1095 static const struct attribute_group mxt_attr_group = {
1096 .attrs = mxt_attrs,
1099 static void mxt_start(struct mxt_data *data)
1101 /* Touch enable */
1102 mxt_write_object(data,
1103 MXT_TOUCH_MULTI_T9, MXT_TOUCH_CTRL, 0x83);
1106 static void mxt_stop(struct mxt_data *data)
1108 /* Touch disable */
1109 mxt_write_object(data,
1110 MXT_TOUCH_MULTI_T9, MXT_TOUCH_CTRL, 0);
1113 static int mxt_input_open(struct input_dev *dev)
1115 struct mxt_data *data = input_get_drvdata(dev);
1117 mxt_start(data);
1119 return 0;
1122 static void mxt_input_close(struct input_dev *dev)
1124 struct mxt_data *data = input_get_drvdata(dev);
1126 mxt_stop(data);
1129 static int mxt_probe(struct i2c_client *client,
1130 const struct i2c_device_id *id)
1132 const struct mxt_platform_data *pdata = dev_get_platdata(&client->dev);
1133 struct mxt_data *data;
1134 struct input_dev *input_dev;
1135 int error;
1136 unsigned int num_mt_slots;
1138 if (!pdata)
1139 return -EINVAL;
1141 data = kzalloc(sizeof(struct mxt_data), GFP_KERNEL);
1142 input_dev = input_allocate_device();
1143 if (!data || !input_dev) {
1144 dev_err(&client->dev, "Failed to allocate memory\n");
1145 error = -ENOMEM;
1146 goto err_free_mem;
1149 data->is_tp = pdata && pdata->is_tp;
1151 input_dev->name = (data->is_tp) ? "Atmel maXTouch Touchpad" :
1152 "Atmel maXTouch Touchscreen";
1153 snprintf(data->phys, sizeof(data->phys), "i2c-%u-%04x/input0",
1154 client->adapter->nr, client->addr);
1156 input_dev->phys = data->phys;
1158 input_dev->id.bustype = BUS_I2C;
1159 input_dev->dev.parent = &client->dev;
1160 input_dev->open = mxt_input_open;
1161 input_dev->close = mxt_input_close;
1163 data->client = client;
1164 data->input_dev = input_dev;
1165 data->pdata = pdata;
1166 data->irq = client->irq;
1168 mxt_calc_resolution(data);
1170 error = mxt_initialize(data);
1171 if (error)
1172 goto err_free_mem;
1174 __set_bit(EV_ABS, input_dev->evbit);
1175 __set_bit(EV_KEY, input_dev->evbit);
1176 __set_bit(BTN_TOUCH, input_dev->keybit);
1178 if (data->is_tp) {
1179 int i;
1180 __set_bit(INPUT_PROP_POINTER, input_dev->propbit);
1181 __set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit);
1183 for (i = 0; i < MXT_NUM_GPIO; i++)
1184 if (pdata->key_map[i] != KEY_RESERVED)
1185 __set_bit(pdata->key_map[i], input_dev->keybit);
1187 __set_bit(BTN_TOOL_FINGER, input_dev->keybit);
1188 __set_bit(BTN_TOOL_DOUBLETAP, input_dev->keybit);
1189 __set_bit(BTN_TOOL_TRIPLETAP, input_dev->keybit);
1190 __set_bit(BTN_TOOL_QUADTAP, input_dev->keybit);
1191 __set_bit(BTN_TOOL_QUINTTAP, input_dev->keybit);
1193 input_abs_set_res(input_dev, ABS_X, MXT_PIXELS_PER_MM);
1194 input_abs_set_res(input_dev, ABS_Y, MXT_PIXELS_PER_MM);
1195 input_abs_set_res(input_dev, ABS_MT_POSITION_X,
1196 MXT_PIXELS_PER_MM);
1197 input_abs_set_res(input_dev, ABS_MT_POSITION_Y,
1198 MXT_PIXELS_PER_MM);
1201 /* For single touch */
1202 input_set_abs_params(input_dev, ABS_X,
1203 0, data->max_x, 0, 0);
1204 input_set_abs_params(input_dev, ABS_Y,
1205 0, data->max_y, 0, 0);
1206 input_set_abs_params(input_dev, ABS_PRESSURE,
1207 0, 255, 0, 0);
1209 /* For multi touch */
1210 num_mt_slots = data->T9_reportid_max - data->T9_reportid_min + 1;
1211 error = input_mt_init_slots(input_dev, num_mt_slots, 0);
1212 if (error)
1213 goto err_free_object;
1214 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR,
1215 0, MXT_MAX_AREA, 0, 0);
1216 input_set_abs_params(input_dev, ABS_MT_POSITION_X,
1217 0, data->max_x, 0, 0);
1218 input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
1219 0, data->max_y, 0, 0);
1220 input_set_abs_params(input_dev, ABS_MT_PRESSURE,
1221 0, 255, 0, 0);
1223 input_set_drvdata(input_dev, data);
1224 i2c_set_clientdata(client, data);
1226 error = request_threaded_irq(client->irq, NULL, mxt_interrupt,
1227 pdata->irqflags | IRQF_ONESHOT,
1228 client->name, data);
1229 if (error) {
1230 dev_err(&client->dev, "Failed to register interrupt\n");
1231 goto err_free_object;
1234 error = mxt_make_highchg(data);
1235 if (error)
1236 goto err_free_irq;
1238 error = input_register_device(input_dev);
1239 if (error)
1240 goto err_free_irq;
1242 error = sysfs_create_group(&client->dev.kobj, &mxt_attr_group);
1243 if (error)
1244 goto err_unregister_device;
1246 return 0;
1248 err_unregister_device:
1249 input_unregister_device(input_dev);
1250 input_dev = NULL;
1251 err_free_irq:
1252 free_irq(client->irq, data);
1253 err_free_object:
1254 kfree(data->object_table);
1255 err_free_mem:
1256 input_free_device(input_dev);
1257 kfree(data);
1258 return error;
1261 static int mxt_remove(struct i2c_client *client)
1263 struct mxt_data *data = i2c_get_clientdata(client);
1265 sysfs_remove_group(&client->dev.kobj, &mxt_attr_group);
1266 free_irq(data->irq, data);
1267 input_unregister_device(data->input_dev);
1268 kfree(data->object_table);
1269 kfree(data);
1271 return 0;
1274 #ifdef CONFIG_PM_SLEEP
1275 static int mxt_suspend(struct device *dev)
1277 struct i2c_client *client = to_i2c_client(dev);
1278 struct mxt_data *data = i2c_get_clientdata(client);
1279 struct input_dev *input_dev = data->input_dev;
1281 mutex_lock(&input_dev->mutex);
1283 if (input_dev->users)
1284 mxt_stop(data);
1286 mutex_unlock(&input_dev->mutex);
1288 return 0;
1291 static int mxt_resume(struct device *dev)
1293 struct i2c_client *client = to_i2c_client(dev);
1294 struct mxt_data *data = i2c_get_clientdata(client);
1295 struct input_dev *input_dev = data->input_dev;
1297 /* Soft reset */
1298 mxt_write_object(data, MXT_GEN_COMMAND_T6,
1299 MXT_COMMAND_RESET, 1);
1301 msleep(MXT_RESET_TIME);
1303 mutex_lock(&input_dev->mutex);
1305 if (input_dev->users)
1306 mxt_start(data);
1308 mutex_unlock(&input_dev->mutex);
1310 return 0;
1312 #endif
1314 static SIMPLE_DEV_PM_OPS(mxt_pm_ops, mxt_suspend, mxt_resume);
1316 static const struct i2c_device_id mxt_id[] = {
1317 { "qt602240_ts", 0 },
1318 { "atmel_mxt_ts", 0 },
1319 { "atmel_mxt_tp", 0 },
1320 { "mXT224", 0 },
1323 MODULE_DEVICE_TABLE(i2c, mxt_id);
1325 static struct i2c_driver mxt_driver = {
1326 .driver = {
1327 .name = "atmel_mxt_ts",
1328 .owner = THIS_MODULE,
1329 .pm = &mxt_pm_ops,
1331 .probe = mxt_probe,
1332 .remove = mxt_remove,
1333 .id_table = mxt_id,
1336 module_i2c_driver(mxt_driver);
1338 /* Module information */
1339 MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
1340 MODULE_DESCRIPTION("Atmel maXTouch Touchscreen driver");
1341 MODULE_LICENSE("GPL");