blob: 8a3ecef3a2c324a2335a481f40f2ad5495759938 [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/* Source for:
2 * Cypress TrueTouch(TM) Standard Product I2C touchscreen driver.
3 * drivers/input/touchscreen/cyttsp-i2c.c
4 *
5 * Copyright (C) 2009, 2010 Cypress Semiconductor, Inc.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * version 2, and only version 2, as published by the
10 * Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 *
21 * Cypress reserves the right to make changes without further notice
22 * to the materials described herein. Cypress does not assume any
23 * liability arising out of the application described herein.
24 *
25 * Contact Cypress Semiconductor at www.cypress.com
26 *
27 */
28
29#include <linux/delay.h>
30#include <linux/init.h>
31#include <linux/module.h>
32#include <linux/i2c.h>
33#include <linux/input.h>
34#include <linux/slab.h>
35#include <linux/gpio.h>
36#include <linux/irq.h>
37#include <linux/interrupt.h>
38#include <linux/timer.h>
39#include <linux/workqueue.h>
40#include <linux/byteorder/generic.h>
41#include <linux/bitops.h>
42#include <linux/pm_runtime.h>
43#include <linux/firmware.h>
44#include <linux/mutex.h>
45#include <linux/regulator/consumer.h>
46#ifdef CONFIG_HAS_EARLYSUSPEND
47#include <linux/earlysuspend.h>
48#endif /* CONFIG_HAS_EARLYSUSPEND */
49
50#define CY_DECLARE_GLOBALS
51
52#include <linux/cyttsp.h>
53
54uint32_t cyttsp_tsdebug1 = 0xff;
55module_param_named(tsdebug1, cyttsp_tsdebug1, uint, 0664);
56
57#define FW_FNAME_LEN 40
58
59/* CY TTSP I2C Driver private data */
60struct cyttsp {
61 struct i2c_client *client;
62 struct input_dev *input;
63 struct work_struct work;
64 struct timer_list timer;
65 struct mutex mutex;
66 char phys[32];
67 struct cyttsp_platform_data *platform_data;
68 u8 num_prv_st_tch;
Amy Malocheedd5fd72011-06-22 18:50:21 -070069 u16 fw_start_addr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070070 u16 act_trk[CY_NUM_TRK_ID];
71 u16 prv_st_tch[CY_NUM_ST_TCH_ID];
72 u16 prv_mt_tch[CY_NUM_MT_TCH_ID];
73 u16 prv_mt_pos[CY_NUM_TRK_ID][2];
74 atomic_t irq_enabled;
75 bool cyttsp_update_fw;
76 bool cyttsp_fwloader_mode;
77 bool is_suspended;
78 struct regulator **vdd;
79 char fw_fname[FW_FNAME_LEN];
80#ifdef CONFIG_HAS_EARLYSUSPEND
81 struct early_suspend early_suspend;
82#endif /* CONFIG_HAS_EARLYSUSPEND */
83};
84static u8 irq_cnt; /* comparison counter with register valuw */
85static u32 irq_cnt_total; /* total interrupts */
86static u32 irq_err_cnt; /* count number of touch interrupts with err */
87#define CY_IRQ_CNT_MASK 0x000000FF /* mapped for sizeof count in reg */
88#define CY_IRQ_CNT_REG 0x00 /* tt_undef[0]=reg 0x1B - Gen3 only */
89
90#ifdef CONFIG_HAS_EARLYSUSPEND
91static void cyttsp_early_suspend(struct early_suspend *handler);
92static void cyttsp_late_resume(struct early_suspend *handler);
93#endif /* CONFIG_HAS_EARLYSUSPEND */
94
95static struct workqueue_struct *cyttsp_ts_wq;
96
97
98/* ****************************************************************************
99 * Prototypes for static functions
100 * ************************************************************************** */
101static void cyttsp_xy_worker(struct work_struct *work);
102static irqreturn_t cyttsp_irq(int irq, void *handle);
103static int cyttsp_inlist(u16 prev_track[],
104 u8 cur_trk_id, u8 *prev_loc, u8 num_touches);
105static int cyttsp_next_avail_inlist(u16 cur_trk[],
106 u8 *new_loc, u8 num_touches);
107static int cyttsp_putbl(struct cyttsp *ts, int show,
108 int show_status, int show_version, int show_cid);
109static int __devinit cyttsp_probe(struct i2c_client *client,
110 const struct i2c_device_id *id);
111static int __devexit cyttsp_remove(struct i2c_client *client);
112static int cyttsp_resume(struct device *dev);
113static int cyttsp_suspend(struct device *dev);
114
115/* Static variables */
116static struct cyttsp_gen3_xydata_t g_xy_data;
117static struct cyttsp_bootloader_data_t g_bl_data;
118static struct cyttsp_sysinfo_data_t g_sysinfo_data;
119static const struct i2c_device_id cyttsp_id[] = {
120 { CY_I2C_NAME, 0 }, { }
121};
122static u8 bl_cmd[] = {
123 CY_BL_FILE0, CY_BL_CMD, CY_BL_EXIT,
124 CY_BL_KEY0, CY_BL_KEY1, CY_BL_KEY2,
125 CY_BL_KEY3, CY_BL_KEY4, CY_BL_KEY5,
126 CY_BL_KEY6, CY_BL_KEY7};
127
128MODULE_DEVICE_TABLE(i2c, cyttsp_id);
129
130static const struct dev_pm_ops cyttsp_pm_ops = {
131 .suspend = cyttsp_suspend,
132 .resume = cyttsp_resume,
133};
134
135static struct i2c_driver cyttsp_driver = {
136 .driver = {
137 .name = CY_I2C_NAME,
138 .owner = THIS_MODULE,
139 .pm = &cyttsp_pm_ops,
140 },
141 .probe = cyttsp_probe,
142 .remove = __devexit_p(cyttsp_remove),
143 .id_table = cyttsp_id,
144};
145
146MODULE_LICENSE("GPL");
147MODULE_DESCRIPTION("Cypress TrueTouch(R) Standard touchscreen driver");
148MODULE_AUTHOR("Cypress");
149
150static ssize_t cyttsp_irq_status(struct device *dev,
151 struct device_attribute *attr, char *buf)
152{
153 struct i2c_client *client = container_of(dev, struct i2c_client, dev);
154 struct cyttsp *ts = i2c_get_clientdata(client);
155 return sprintf(buf, "%u\n", atomic_read(&ts->irq_enabled));
156}
157
158static ssize_t cyttsp_irq_enable(struct device *dev,
159 struct device_attribute *attr,
160 const char *buf, size_t size)
161{
162 struct i2c_client *client = container_of(dev, struct i2c_client, dev);
163 struct cyttsp *ts = i2c_get_clientdata(client);
164 int err = 0;
165 unsigned long value;
166
167 if (size > 2)
168 return -EINVAL;
169
170 err = strict_strtoul(buf, 10, &value);
171 if (err != 0)
172 return err;
173
174 switch (value) {
175 case 0:
176 if (atomic_cmpxchg(&ts->irq_enabled, 1, 0)) {
177 pr_info("touch irq disabled!\n");
178 disable_irq_nosync(ts->client->irq);
179 }
180 err = size;
181 break;
182 case 1:
183 if (!atomic_cmpxchg(&ts->irq_enabled, 0, 1)) {
184 pr_info("touch irq enabled!\n");
185 enable_irq(ts->client->irq);
186 }
187 err = size;
188 break;
189 default:
190 pr_info("cyttsp_irq_enable failed -> irq_enabled = %d\n",
191 atomic_read(&ts->irq_enabled));
192 err = -EINVAL;
193 break;
194 }
195
196 return err;
197}
198
199static DEVICE_ATTR(irq_enable, 0777, cyttsp_irq_status, cyttsp_irq_enable);
200
201static ssize_t cyttsp_fw_show(struct device *dev,
202 struct device_attribute *attr, char *buf)
203{
204 return sprintf(buf, "%d.%d.%d\n", g_bl_data.appid_lo,
205 g_bl_data.appver_hi, g_bl_data.appver_lo);
206}
207
208static DEVICE_ATTR(cyttsp_fw_ver, 0777, cyttsp_fw_show, NULL);
209
210/* firmware flashing block */
211#define BLK_SIZE 16
212#define DATA_REC_LEN 64
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700213#define BLK_SEED 0xff
214#define RECAL_REG 0x1b
215
216enum bl_commands {
217 BL_CMD_WRBLK = 0x39,
218 BL_CMD_INIT = 0x38,
219 BL_CMD_TERMINATE = 0x3b,
220};
221/* TODO: Add key as part of platform data */
222#define KEY_CS (0 + 1 + 2 + 3 + 4 + 5 + 6 + 7)
223#define KEY {0, 1, 2, 3, 4, 5, 6, 7}
224
225static const char _key[] = KEY;
226#define KEY_LEN sizeof(_key)
227
228static int rec_cnt;
229struct fw_record {
230 u8 seed;
231 u8 cmd;
232 u8 key[KEY_LEN];
233 u8 blk_hi;
234 u8 blk_lo;
235 u8 data[DATA_REC_LEN];
236 u8 data_cs;
237 u8 rec_cs;
238};
239#define fw_rec_size (sizeof(struct fw_record))
240
241struct cmd_record {
242 u8 reg;
243 u8 seed;
244 u8 cmd;
245 u8 key[KEY_LEN];
246};
247#define cmd_rec_size (sizeof(struct cmd_record))
248
249static struct fw_record data_record = {
250 .seed = BLK_SEED,
251 .cmd = BL_CMD_WRBLK,
252 .key = KEY,
253};
254
255static const struct cmd_record terminate_rec = {
256 .reg = 0,
257 .seed = BLK_SEED,
258 .cmd = BL_CMD_TERMINATE,
259 .key = KEY,
260};
261static const struct cmd_record initiate_rec = {
262 .reg = 0,
263 .seed = BLK_SEED,
264 .cmd = BL_CMD_INIT,
265 .key = KEY,
266};
267
268#define BL_REC1_ADDR 0x0780
269#define BL_REC2_ADDR 0x07c0
270
271#define ID_INFO_REC ":40078000"
272#define ID_INFO_OFFSET_IN_REC 77
273
274#define REC_START_CHR ':'
275#define REC_LEN_OFFSET 1
276#define REC_ADDR_HI_OFFSET 3
277#define REC_ADDR_LO_OFFSET 5
278#define REC_TYPE_OFFSET 7
279#define REC_DATA_OFFSET 9
280#define REC_LINE_SIZE 141
281
282static int cyttsp_soft_reset(struct cyttsp *ts)
283{
284 int retval = 0, tries = 0;
285 u8 host_reg = CY_SOFT_RESET_MODE;
286
287 do {
288 retval = i2c_smbus_write_i2c_block_data(ts->client,
289 CY_REG_BASE, sizeof(host_reg), &host_reg);
290 if (retval < 0)
291 msleep(20);
292 } while (tries++ < 10 && (retval < 0));
293
294 if (retval < 0) {
295 pr_err("%s: failed\n", __func__);
296 return retval;
297 }
298
299 tries = 0;
300 do {
301 msleep(20);
302 cyttsp_putbl(ts, 1, true, true, false);
303 } while (g_bl_data.bl_status != 0x10 &&
304 g_bl_data.bl_status != 0x11 &&
305 tries++ < 100);
306
307 if (g_bl_data.bl_status != 0x11 && g_bl_data.bl_status != 0x10)
308 return -EINVAL;
309
310 return 0;
311}
312
313static void cyttsp_exit_bl_mode(struct cyttsp *ts)
314{
315 int retval, tries = 0;
316
317 do {
318 retval = i2c_smbus_write_i2c_block_data(ts->client,
319 CY_REG_BASE, sizeof(bl_cmd), bl_cmd);
320 if (retval < 0)
321 msleep(20);
322 } while (tries++ < 10 && (retval < 0));
323}
324
325static void cyttsp_set_sysinfo_mode(struct cyttsp *ts)
326{
327 int retval, tries = 0;
328 u8 host_reg = CY_SYSINFO_MODE;
329
330 do {
331 retval = i2c_smbus_write_i2c_block_data(ts->client,
332 CY_REG_BASE, sizeof(host_reg), &host_reg);
333 if (retval < 0)
334 msleep(20);
335 } while (tries++ < 10 && (retval < 0));
336
337 /* wait for TTSP Device to complete switch to SysInfo mode */
338 if (!(retval < 0)) {
339 retval = i2c_smbus_read_i2c_block_data(ts->client,
340 CY_REG_BASE,
341 sizeof(struct cyttsp_sysinfo_data_t),
342 (u8 *)&g_sysinfo_data);
343 } else
344 pr_err("%s: failed\n", __func__);
345}
346
347static void cyttsp_set_opmode(struct cyttsp *ts)
348{
349 int retval, tries = 0;
350 u8 host_reg = CY_OP_MODE;
351
352 do {
353 retval = i2c_smbus_write_i2c_block_data(ts->client,
354 CY_REG_BASE, sizeof(host_reg), &host_reg);
355 if (retval < 0)
356 msleep(20);
357 } while (tries++ < 10 && (retval < 0));
358}
359
360static int str2uc(char *str, u8 *val)
361{
362 char substr[3];
363 unsigned long ulval;
364 int rc;
365
366 if (!str && strlen(str) < 2)
367 return -EINVAL;
368
369 substr[0] = str[0];
370 substr[1] = str[1];
371 substr[2] = '\0';
372
373 rc = strict_strtoul(substr, 16, &ulval);
374 if (rc != 0)
375 return rc;
376
377 *val = (u8) ulval;
378
379 return 0;
380}
381
382static int flash_block(struct cyttsp *ts, u8 *blk, int len)
383{
384 int retval, i, tries = 0;
385 char buf[(2 * (BLK_SIZE + 1)) + 1];
386 char *p = buf;
387
388 for (i = 0; i < len; i++, p += 2)
389 sprintf(p, "%02x", blk[i]);
390 pr_debug("%s: size %d, pos %ld payload %s\n",
391 __func__, len, (long)0, buf);
392
393 do {
394 retval = i2c_smbus_write_i2c_block_data(ts->client,
395 CY_REG_BASE, len, blk);
396 if (retval < 0)
397 msleep(20);
398 } while (tries++ < 20 && (retval < 0));
399
400 if (retval < 0) {
401 pr_err("%s: failed\n", __func__);
402 return retval;
403 }
404
405 return 0;
406}
407
408static int flash_command(struct cyttsp *ts, const struct cmd_record *record)
409{
410 return flash_block(ts, (u8 *)record, cmd_rec_size);
411}
412
413static void init_data_record(struct fw_record *rec, unsigned short addr)
414{
415 addr >>= 6;
416 rec->blk_hi = (addr >> 8) & 0xff;
417 rec->blk_lo = addr & 0xff;
418 rec->rec_cs = rec->blk_hi + rec->blk_lo +
419 (unsigned char)(BLK_SEED + BL_CMD_WRBLK + KEY_CS);
420 rec->data_cs = 0;
421}
422
Amy Malocheedd5fd72011-06-22 18:50:21 -0700423static int check_record(struct cyttsp *ts, u8 *rec)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700424{
425 int rc;
426 u16 addr;
427 u8 r_len, type, hi_off, lo_off;
428
429 rc = str2uc(rec + REC_LEN_OFFSET, &r_len);
430 if (rc < 0)
431 return rc;
432
433 rc = str2uc(rec + REC_TYPE_OFFSET, &type);
434 if (rc < 0)
435 return rc;
436
437 if (*rec != REC_START_CHR || r_len != DATA_REC_LEN || type != 0)
438 return -EINVAL;
439
440 rc = str2uc(rec + REC_ADDR_HI_OFFSET, &hi_off);
441 if (rc < 0)
442 return rc;
443
444 rc = str2uc(rec + REC_ADDR_LO_OFFSET, &lo_off);
445 if (rc < 0)
446 return rc;
447
448 addr = (hi_off << 8) | lo_off;
449
Amy Malocheedd5fd72011-06-22 18:50:21 -0700450 if (addr >= ts->fw_start_addr || addr == BL_REC1_ADDR
451 || addr == BL_REC2_ADDR)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700452 return 0;
453
454 return -EINVAL;
455}
456
457static struct fw_record *prepare_record(u8 *rec)
458{
459 int i, rc;
460 u16 addr;
461 u8 hi_off, lo_off;
462 u8 *p;
463
464 rc = str2uc(rec + REC_ADDR_HI_OFFSET, &hi_off);
465 if (rc < 0)
466 return ERR_PTR((long) rc);
467
468 rc = str2uc(rec + REC_ADDR_LO_OFFSET, &lo_off);
469 if (rc < 0)
470 return ERR_PTR((long) rc);
471
472 addr = (hi_off << 8) | lo_off;
473
474 init_data_record(&data_record, addr);
475 p = rec + REC_DATA_OFFSET;
476 for (i = 0; i < DATA_REC_LEN; i++) {
477 rc = str2uc(p, &data_record.data[i]);
478 if (rc < 0)
479 return ERR_PTR((long) rc);
480 data_record.data_cs += data_record.data[i];
481 data_record.rec_cs += data_record.data[i];
482 p += 2;
483 }
484 data_record.rec_cs += data_record.data_cs;
485
486 return &data_record;
487}
488
489static int flash_record(struct cyttsp *ts, const struct fw_record *record)
490{
491 int len = fw_rec_size;
492 int blk_len, rc;
493 u8 *rec = (u8 *)record;
494 u8 data[BLK_SIZE + 1];
495 u8 blk_offset;
496
497 for (blk_offset = 0; len; len -= blk_len) {
498 data[0] = blk_offset;
499 blk_len = len > BLK_SIZE ? BLK_SIZE : len;
500 memcpy(data + 1, rec, blk_len);
501 rec += blk_len;
502 rc = flash_block(ts, data, blk_len + 1);
503 if (rc < 0)
504 return rc;
505 blk_offset += blk_len;
506 }
507 return 0;
508}
509
510static int flash_data_rec(struct cyttsp *ts, u8 *buf)
511{
512 struct fw_record *rec;
513 int rc, tries;
514
515 if (!buf)
516 return -EINVAL;
517
Amy Malocheedd5fd72011-06-22 18:50:21 -0700518 rc = check_record(ts, buf);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700519
520 if (rc < 0) {
521 pr_debug("%s: record ignored %s", __func__, buf);
522 return 0;
523 }
524
525 rec = prepare_record(buf);
526 if (IS_ERR_OR_NULL(rec))
527 return PTR_ERR(rec);
528
529 rc = flash_record(ts, rec);
530 if (rc < 0)
531 return rc;
532
533 tries = 0;
534 do {
535 if (rec_cnt%2)
536 msleep(20);
537 cyttsp_putbl(ts, 4, true, false, false);
538 } while (g_bl_data.bl_status != 0x10 &&
539 g_bl_data.bl_status != 0x11 &&
540 tries++ < 100);
541 rec_cnt++;
542 return rc;
543}
544
545static int cyttspfw_flash_firmware(struct cyttsp *ts, const u8 *data,
546 int data_len)
547{
548 u8 *buf;
549 int i, j;
550 int rc, tries = 0;
551
552 /* initiate bootload: this will erase all the existing data */
553 rc = flash_command(ts, &initiate_rec);
554 if (rc < 0)
555 return rc;
556
557 do {
558 msleep(100);
559 cyttsp_putbl(ts, 4, true, false, false);
560 } while (g_bl_data.bl_status != 0x10 &&
561 g_bl_data.bl_status != 0x11 &&
562 tries++ < 100);
563
564 buf = kzalloc(REC_LINE_SIZE + 1, GFP_KERNEL);
565 if (!buf) {
566 pr_err("%s: no memory\n", __func__);
567 return -ENOMEM;
568 }
569
570 rec_cnt = 0;
571 /* flash data records */
572 for (i = 0, j = 0; i < data_len; i++, j++) {
573 if ((data[i] == REC_START_CHR) && j) {
574 buf[j] = 0;
575 rc = flash_data_rec(ts, buf);
576 if (rc < 0)
577 return rc;
578 j = 0;
579 }
580 buf[j] = data[i];
581 }
582
583 /* flash last data record */
584 if (j) {
585 buf[j] = 0;
586 rc = flash_data_rec(ts, buf);
587 if (rc < 0)
588 return rc;
589 }
590
591 kfree(buf);
592
593 /* termiate bootload */
594 tries = 0;
595 rc = flash_command(ts, &terminate_rec);
596 do {
597 msleep(100);
598 cyttsp_putbl(ts, 4, true, false, false);
599 } while (g_bl_data.bl_status != 0x10 &&
600 g_bl_data.bl_status != 0x11 &&
601 tries++ < 100);
602
603 return rc;
604}
605
606static int get_hex_fw_ver(u8 *p, u8 *ttspver_hi, u8 *ttspver_lo,
607 u8 *appid_hi, u8 *appid_lo, u8 *appver_hi,
608 u8 *appver_lo, u8 *cid_0, u8 *cid_1, u8 *cid_2)
609{
610 int rc;
611
612 p = p + ID_INFO_OFFSET_IN_REC;
613 rc = str2uc(p, ttspver_hi);
614 if (rc < 0)
615 return rc;
616 p += 2;
617 rc = str2uc(p, ttspver_lo);
618 if (rc < 0)
619 return rc;
620 p += 2;
621 rc = str2uc(p, appid_hi);
622 if (rc < 0)
623 return rc;
624 p += 2;
625 rc = str2uc(p, appid_lo);
626 if (rc < 0)
627 return rc;
628 p += 2;
629 rc = str2uc(p, appver_hi);
630 if (rc < 0)
631 return rc;
632 p += 2;
633 rc = str2uc(p, appver_lo);
634 if (rc < 0)
635 return rc;
636 p += 2;
637 rc = str2uc(p, cid_0);
638 if (rc < 0)
639 return rc;
640 p += 2;
641 rc = str2uc(p, cid_1);
642 if (rc < 0)
643 return rc;
644 p += 2;
645 rc = str2uc(p, cid_2);
646 if (rc < 0)
647 return rc;
648
649 return 0;
650}
651
652static void cyttspfw_flash_start(struct cyttsp *ts, const u8 *data,
653 int data_len, u8 *buf, bool force)
654{
655 int rc;
656 u8 ttspver_hi = 0, ttspver_lo = 0, fw_upgrade = 0;
657 u8 appid_hi = 0, appid_lo = 0;
658 u8 appver_hi = 0, appver_lo = 0;
659 u8 cid_0 = 0, cid_1 = 0, cid_2 = 0;
660 char *p = buf;
661
662 /* get hex firmware version */
663 rc = get_hex_fw_ver(p, &ttspver_hi, &ttspver_lo,
664 &appid_hi, &appid_lo, &appver_hi,
665 &appver_lo, &cid_0, &cid_1, &cid_2);
666
667 if (rc < 0) {
668 pr_err("%s: unable to get hex firmware version\n", __func__);
669 return;
670 }
671
672 /* disable interrupts before flashing */
673 if (ts->client->irq == 0)
674 del_timer(&ts->timer);
675 else
676 disable_irq(ts->client->irq);
677
678 rc = cancel_work_sync(&ts->work);
679
680 if (rc && ts->client->irq)
681 enable_irq(ts->client->irq);
682
683 /* enter bootloader idle mode */
684 rc = cyttsp_soft_reset(ts);
685
686 if (rc < 0) {
687 pr_err("%s: try entering into idle mode"
688 " second time\n", __func__);
689 msleep(1000);
690 rc = cyttsp_soft_reset(ts);
691 }
692
693 if (rc < 0) {
694 pr_err("%s: try again later\n", __func__);
695 return;
696 }
697
698
699 pr_info("Current firmware: %d.%d.%d", g_bl_data.appid_lo,
700 g_bl_data.appver_hi, g_bl_data.appver_lo);
701 pr_info("New firmware: %d.%d.%d", appid_lo, appver_hi, appver_lo);
702
703 if (force)
704 fw_upgrade = 1;
705 else
706 if ((appid_hi == g_bl_data.appid_hi) &&
707 (appid_lo == g_bl_data.appid_lo)) {
708 if (appver_hi > g_bl_data.appver_hi) {
709 fw_upgrade = 1;
710 } else if ((appver_hi == g_bl_data.appver_hi) &&
711 (appver_lo > g_bl_data.appver_lo)) {
712 fw_upgrade = 1;
713 } else {
714 fw_upgrade = 0;
715 pr_info("%s: Firmware version "
716 "lesser/equal to existing firmware, "
717 "upgrade not needed\n", __func__);
718 }
719 } else {
720 fw_upgrade = 0;
721 pr_info("%s: Firware versions do not match, "
722 "cannot upgrade\n", __func__);
723 }
724
725 if (fw_upgrade) {
726 pr_info("%s: Starting firmware upgrade\n", __func__);
727 rc = cyttspfw_flash_firmware(ts, data, data_len);
728 if (rc < 0)
729 pr_err("%s: firmware upgrade failed\n", __func__);
730 else
731 pr_info("%s: firmware upgrade success\n", __func__);
732 }
733
734 /* enter bootloader idle mode */
735 cyttsp_soft_reset(ts);
736 /* exit bootloader mode */
737 cyttsp_exit_bl_mode(ts);
738 msleep(100);
739 /* set sysinfo details */
740 cyttsp_set_sysinfo_mode(ts);
741 /* enter application mode */
742 cyttsp_set_opmode(ts);
743
744 /* enable interrupts */
745 if (ts->client->irq == 0)
746 mod_timer(&ts->timer, jiffies + TOUCHSCREEN_TIMEOUT);
747 else
748 enable_irq(ts->client->irq);
749}
750
751static void cyttspfw_upgrade_start(struct cyttsp *ts, const u8 *data,
752 int data_len, bool force)
753{
754 int i, j;
755 u8 *buf;
756
757 buf = kzalloc(REC_LINE_SIZE + 1, GFP_KERNEL);
758 if (!buf) {
759 pr_err("%s: no memory\n", __func__);
760 return;
761 }
762
763 for (i = 0, j = 0; i < data_len; i++, j++) {
764 if ((data[i] == REC_START_CHR) && j) {
765 buf[j] = 0;
766 j = 0;
767 if (!strncmp(buf, ID_INFO_REC, strlen(ID_INFO_REC))) {
768 cyttspfw_flash_start(ts, data, data_len,
769 buf, force);
770 break;
771 }
772 }
773 buf[j] = data[i];
774 }
775
776 /* check in the last record of firmware */
777 if (j) {
778 buf[j] = 0;
779 if (!strncmp(buf, ID_INFO_REC, strlen(ID_INFO_REC))) {
780 cyttspfw_flash_start(ts, data, data_len,
781 buf, force);
782 }
783 }
784
785 kfree(buf);
786}
787
788static void cyttspfw_upgrade(struct device *dev, bool force)
789{
790 struct cyttsp *ts = dev_get_drvdata(dev);
791 const struct firmware *cyttsp_fw;
792 int retval = 0;
793
794 if (ts->is_suspended == true) {
795 pr_err("%s: in suspend state, resume it\n", __func__);
796 retval = cyttsp_resume(dev);
797 if (retval < 0) {
798 pr_err("%s: unable to resume\n", __func__);
799 return;
800 }
801 }
802
803 retval = request_firmware(&cyttsp_fw, ts->fw_fname, dev);
804 if (retval < 0) {
805 pr_err("%s: %s request failed(%d)\n", __func__,
806 ts->fw_fname, retval);
807 } else {
808 /* check and start upgrade */
809 cyttspfw_upgrade_start(ts, cyttsp_fw->data,
810 cyttsp_fw->size, force);
811 release_firmware(cyttsp_fw);
812 }
813}
814
815static ssize_t cyttsp_update_fw_show(struct device *dev,
816 struct device_attribute *attr, char *buf)
817{
818 struct cyttsp *ts = dev_get_drvdata(dev);
819 return snprintf(buf, 2, "%d\n", ts->cyttsp_fwloader_mode);
820}
821
822static ssize_t cyttsp_force_update_fw_store(struct device *dev,
823 struct device_attribute *attr,
824 const char *buf, size_t size)
825{
826 struct cyttsp *ts = dev_get_drvdata(dev);
827 unsigned long val;
828 int rc;
829
830 if (size > 2)
831 return -EINVAL;
832
833 rc = strict_strtoul(buf, 10, &val);
834 if (rc != 0)
835 return rc;
836
837 mutex_lock(&ts->mutex);
838 if (!ts->cyttsp_fwloader_mode && val) {
839 ts->cyttsp_fwloader_mode = 1;
840 cyttspfw_upgrade(dev, true);
841 ts->cyttsp_fwloader_mode = 0;
842 }
843 mutex_unlock(&ts->mutex);
844 return size;
845}
846
847static DEVICE_ATTR(cyttsp_force_update_fw, 0777, cyttsp_update_fw_show,
848 cyttsp_force_update_fw_store);
849
850static ssize_t cyttsp_update_fw_store(struct device *dev,
851 struct device_attribute *attr,
852 const char *buf, size_t size)
853{
854 struct cyttsp *ts = dev_get_drvdata(dev);
855 unsigned long val;
856 int rc;
857
858 if (size > 2)
859 return -EINVAL;
860
861 rc = strict_strtoul(buf, 10, &val);
862 if (rc != 0)
863 return rc;
864
865 mutex_lock(&ts->mutex);
866 if (!ts->cyttsp_fwloader_mode && val) {
867 ts->cyttsp_fwloader_mode = 1;
868 cyttspfw_upgrade(dev, false);
869 ts->cyttsp_fwloader_mode = 0;
870 }
871 mutex_unlock(&ts->mutex);
872
873 return size;
874}
875
876static DEVICE_ATTR(cyttsp_update_fw, 0777, cyttsp_update_fw_show,
877 cyttsp_update_fw_store);
878
879static ssize_t cyttsp_fw_name_show(struct device *dev,
880 struct device_attribute *attr, char *buf)
881{
882 struct cyttsp *ts = dev_get_drvdata(dev);
883 return snprintf(buf, FW_FNAME_LEN - 1, "%s\n", ts->fw_fname);
884}
885
886static ssize_t cyttsp_fw_name_store(struct device *dev,
887 struct device_attribute *attr,
888 const char *buf, size_t size)
889{
890 struct cyttsp *ts = dev_get_drvdata(dev);
891
892 if (size > FW_FNAME_LEN - 1)
893 return -EINVAL;
894
895 strncpy(ts->fw_fname, buf, size);
896 if (ts->fw_fname[size-1] == '\n')
897 ts->fw_fname[size-1] = 0;
898
899 return size;
900}
901
902static DEVICE_ATTR(cyttsp_fw_name, 0777, cyttsp_fw_name_show,
903 cyttsp_fw_name_store);
904
905/* The cyttsp_xy_worker function reads the XY coordinates and sends them to
906 * the input layer. It is scheduled from the interrupt (or timer).
907 */
908void cyttsp_xy_worker(struct work_struct *work)
909{
910 struct cyttsp *ts = container_of(work, struct cyttsp, work);
911 u8 id, tilt, rev_x, rev_y;
912 u8 i, loc;
913 u8 prv_tch; /* number of previous touches */
914 u8 cur_tch; /* number of current touches */
915 u16 tmp_trk[CY_NUM_MT_TCH_ID];
916 u16 snd_trk[CY_NUM_MT_TCH_ID];
917 u16 cur_trk[CY_NUM_TRK_ID];
918 u16 cur_st_tch[CY_NUM_ST_TCH_ID];
919 u16 cur_mt_tch[CY_NUM_MT_TCH_ID];
920 /* if NOT CY_USE_TRACKING_ID then
921 * only uses CY_NUM_MT_TCH_ID positions */
922 u16 cur_mt_pos[CY_NUM_TRK_ID][2];
923 /* if NOT CY_USE_TRACKING_ID then
924 * only uses CY_NUM_MT_TCH_ID positions */
925 u8 cur_mt_z[CY_NUM_TRK_ID];
926 u8 curr_tool_width;
927 u16 st_x1, st_y1;
928 u8 st_z1;
929 u16 st_x2, st_y2;
930 u8 st_z2;
931 s32 retval;
932
933 cyttsp_xdebug("TTSP worker start 1:\n");
934
935 /* get event data from CYTTSP device */
936 i = CY_NUM_RETRY;
937 do {
938 retval = i2c_smbus_read_i2c_block_data(ts->client,
939 CY_REG_BASE,
940 sizeof(struct cyttsp_gen3_xydata_t), (u8 *)&g_xy_data);
941 } while ((retval < CY_OK) && --i);
942
943 if (retval < CY_OK) {
944 /* return immediately on
945 * failure to read device on the i2c bus */
946 goto exit_xy_worker;
947 }
948
949 cyttsp_xdebug("TTSP worker start 2:\n");
950
951 /* compare own irq counter with the device irq counter */
952 if (ts->client->irq) {
953 u8 host_reg;
954 u8 cur_cnt;
955 if (ts->platform_data->use_hndshk) {
956
957 host_reg = g_xy_data.hst_mode & CY_HNDSHK_BIT ?
958 g_xy_data.hst_mode & ~CY_HNDSHK_BIT :
959 g_xy_data.hst_mode | CY_HNDSHK_BIT;
960 retval = i2c_smbus_write_i2c_block_data(ts->client,
961 CY_REG_BASE, sizeof(host_reg), &host_reg);
962 }
963 cur_cnt = g_xy_data.tt_undef[CY_IRQ_CNT_REG];
964 irq_cnt_total++;
965 irq_cnt++;
966 if (irq_cnt != cur_cnt) {
967 irq_err_cnt++;
968 cyttsp_debug("i_c_ER: dv=%d fw=%d hm=%02X t=%lu te=%lu\n", \
969 irq_cnt, \
970 cur_cnt, g_xy_data.hst_mode, \
971 (unsigned long)irq_cnt_total, \
972 (unsigned long)irq_err_cnt);
973 } else {
974 cyttsp_debug("i_c_ok: dv=%d fw=%d hm=%02X t=%lu te=%lu\n", \
975 irq_cnt, \
976 cur_cnt, g_xy_data.hst_mode, \
977 (unsigned long)irq_cnt_total, \
978 (unsigned long)irq_err_cnt);
979 }
980 irq_cnt = cur_cnt;
981 }
982
983 /* Get the current num touches and return if there are no touches */
984 if ((GET_BOOTLOADERMODE(g_xy_data.tt_mode) == 1) ||
985 (GET_HSTMODE(g_xy_data.hst_mode) != CY_OK)) {
986 u8 host_reg, tries;
987 /* the TTSP device has suffered spurious reset or mode switch */
988 cyttsp_debug( \
989 "Spurious err opmode (tt_mode=%02X hst_mode=%02X)\n", \
990 g_xy_data.tt_mode, g_xy_data.hst_mode);
991 cyttsp_debug("Reset TTSP Device; Terminating active tracks\n");
992 /* terminate all active tracks */
993 cur_tch = CY_NTCH;
994 /* reset TTSP part and take it back out of Bootloader mode */
995 /* reset TTSP Device back to bootloader mode */
996 host_reg = CY_SOFT_RESET_MODE;
997 retval = i2c_smbus_write_i2c_block_data(ts->client, CY_REG_BASE,
998 sizeof(host_reg), &host_reg);
999 /* wait for TTSP Device to complete reset back to bootloader */
1000 tries = 0;
1001 do {
1002 mdelay(1);
1003 cyttsp_putbl(ts, 1, false, false, false);
1004 } while (g_bl_data.bl_status != 0x10 &&
1005 g_bl_data.bl_status != 0x11 &&
1006 tries++ < 100);
1007 retval = cyttsp_putbl(ts, 1, true, true, true);
1008 /* switch back to operational mode */
1009 /* take TTSP device out of bootloader mode;
1010 * switch back to TrueTouch operational mode */
1011 if (!(retval < CY_OK)) {
1012 int tries;
1013 retval = i2c_smbus_write_i2c_block_data(ts->client,
1014 CY_REG_BASE,
1015 sizeof(bl_cmd), bl_cmd);
1016 /* wait for TTSP Device to complete
1017 * switch to Operational mode */
1018 tries = 0;
1019 do {
1020 mdelay(100);
1021 cyttsp_putbl(ts, 2, false, false, false);
1022 } while (GET_BOOTLOADERMODE(g_bl_data.bl_status) &&
1023 tries++ < 100);
1024 cyttsp_putbl(ts, 2, true, false, false);
1025 }
1026 goto exit_xy_worker;
1027 } else {
1028 cur_tch = GET_NUM_TOUCHES(g_xy_data.tt_stat);
1029 if (IS_LARGE_AREA(g_xy_data.tt_stat)) {
1030 /* terminate all active tracks */
1031 cur_tch = CY_NTCH;
1032 cyttsp_debug("Large obj detect (tt_stat=0x%02X). Terminate act trks\n", \
1033 g_xy_data.tt_stat);
1034 } else if (cur_tch > CY_NUM_MT_TCH_ID) {
1035 /* if the number of fingers on the touch surface
1036 * is more than the maximum then
1037 * there will be no new track information
1038 * even for the original touches.
1039 * Therefore, terminate all active tracks.
1040 */
1041 cur_tch = CY_NTCH;
1042 cyttsp_debug("Num touch err (tt_stat=0x%02X). Terminate act trks\n", \
1043 g_xy_data.tt_stat);
1044 }
1045 }
1046
1047 /* set tool size */
1048 curr_tool_width = CY_SMALL_TOOL_WIDTH;
1049
1050 /* translate Gen2 interface data into comparable Gen3 data */
1051 if (ts->platform_data->gen == CY_GEN2) {
1052 struct cyttsp_gen2_xydata_t *pxy_gen2_data;
1053 pxy_gen2_data = (struct cyttsp_gen2_xydata_t *)(&g_xy_data);
1054
1055 /* use test data? */
1056 cyttsp_testdat(&g_xy_data, &tt_gen2_testray, \
1057 sizeof(struct cyttsp_gen3_xydata_t));
1058
1059 if (pxy_gen2_data->evnt_idx == CY_GEN2_NOTOUCH) {
1060 cur_tch = 0;
1061 } else if (cur_tch == CY_GEN2_GHOST) {
1062 cur_tch = 0;
1063 } else if (cur_tch == CY_GEN2_2TOUCH) {
1064 /* stuff artificial track ID1 and ID2 */
1065 g_xy_data.touch12_id = 0x12;
1066 g_xy_data.z1 = CY_MAXZ;
1067 g_xy_data.z2 = CY_MAXZ;
1068 cur_tch--; /* 2 touches */
1069 } else if (cur_tch == CY_GEN2_1TOUCH) {
1070 /* stuff artificial track ID1 and ID2 */
1071 g_xy_data.touch12_id = 0x12;
1072 g_xy_data.z1 = CY_MAXZ;
1073 g_xy_data.z2 = CY_NTCH;
1074 if (pxy_gen2_data->evnt_idx == CY_GEN2_TOUCH2) {
1075 /* push touch 2 data into touch1
1076 * (first finger up; second finger down) */
1077 /* stuff artificial track ID1 for touch2 info */
1078 g_xy_data.touch12_id = 0x20;
1079 /* stuff touch 1 with touch 2 coordinate data */
1080 g_xy_data.x1 = g_xy_data.x2;
1081 g_xy_data.y1 = g_xy_data.y2;
1082 }
1083 } else {
1084 cur_tch = 0;
1085 }
1086 } else {
1087 /* use test data? */
1088 cyttsp_testdat(&g_xy_data, &tt_gen3_testray, \
1089 sizeof(struct cyttsp_gen3_xydata_t));
1090 }
1091
1092
1093
1094 /* clear current active track ID array and count previous touches */
1095 for (id = 0, prv_tch = CY_NTCH;
1096 id < CY_NUM_TRK_ID; id++) {
1097 cur_trk[id] = CY_NTCH;
1098 prv_tch += ts->act_trk[id];
1099 }
1100
1101 /* send no events if no previous touches and no new touches */
1102 if ((prv_tch == CY_NTCH) &&
1103 ((cur_tch == CY_NTCH) ||
1104 (cur_tch > CY_NUM_MT_TCH_ID))) {
1105 goto exit_xy_worker;
1106 }
1107
1108 cyttsp_debug("prev=%d curr=%d\n", prv_tch, cur_tch);
1109
1110 for (id = 0; id < CY_NUM_ST_TCH_ID; id++) {
1111 /* clear current single touches array */
1112 cur_st_tch[id] = CY_IGNR_TCH;
1113 }
1114
1115 /* clear single touch positions */
1116 st_x1 = CY_NTCH;
1117 st_y1 = CY_NTCH;
1118 st_z1 = CY_NTCH;
1119 st_x2 = CY_NTCH;
1120 st_y2 = CY_NTCH;
1121 st_z2 = CY_NTCH;
1122
1123 for (id = 0; id < CY_NUM_MT_TCH_ID; id++) {
1124 /* clear current multi-touches array and
1125 * multi-touch positions/z */
1126 cur_mt_tch[id] = CY_IGNR_TCH;
1127 }
1128
1129 if (ts->platform_data->use_trk_id) {
1130 for (id = 0; id < CY_NUM_MT_TCH_ID; id++) {
1131 cur_mt_pos[id][CY_XPOS] = 0;
1132 cur_mt_pos[id][CY_YPOS] = 0;
1133 cur_mt_z[id] = 0;
1134 }
1135 } else {
1136 for (id = 0; id < CY_NUM_TRK_ID; id++) {
1137 cur_mt_pos[id][CY_XPOS] = 0;
1138 cur_mt_pos[id][CY_YPOS] = 0;
1139 cur_mt_z[id] = 0;
1140 }
1141 }
1142
1143 /* Determine if display is tilted */
1144 if (FLIP_DATA(ts->platform_data->flags))
1145 tilt = true;
1146 else
1147 tilt = false;
1148
1149 /* Check for switch in origin */
1150 if (REVERSE_X(ts->platform_data->flags))
1151 rev_x = true;
1152 else
1153 rev_x = false;
1154
1155 if (REVERSE_Y(ts->platform_data->flags))
1156 rev_y = true;
1157 else
1158 rev_y = false;
1159
1160 if (cur_tch) {
1161 struct cyttsp_gen2_xydata_t *pxy_gen2_data;
1162 struct cyttsp_gen3_xydata_t *pxy_gen3_data;
1163 switch (ts->platform_data->gen) {
1164 case CY_GEN2: {
1165 pxy_gen2_data =
1166 (struct cyttsp_gen2_xydata_t *)(&g_xy_data);
1167 cyttsp_xdebug("TTSP Gen2 report:\n");
1168 cyttsp_xdebug("%02X %02X %02X\n", \
1169 pxy_gen2_data->hst_mode, \
1170 pxy_gen2_data->tt_mode, \
1171 pxy_gen2_data->tt_stat);
1172 cyttsp_xdebug("%04X %04X %02X %02X\n", \
1173 pxy_gen2_data->x1, \
1174 pxy_gen2_data->y1, \
1175 pxy_gen2_data->z1, \
1176 pxy_gen2_data->evnt_idx);
1177 cyttsp_xdebug("%04X %04X %02X\n", \
1178 pxy_gen2_data->x2, \
1179 pxy_gen2_data->y2, \
1180 pxy_gen2_data->tt_undef1);
1181 cyttsp_xdebug("%02X %02X %02X\n", \
1182 pxy_gen2_data->gest_cnt, \
1183 pxy_gen2_data->gest_id, \
1184 pxy_gen2_data->gest_set);
1185 break;
1186 }
1187 case CY_GEN3:
1188 default: {
1189 pxy_gen3_data =
1190 (struct cyttsp_gen3_xydata_t *)(&g_xy_data);
1191 cyttsp_xdebug("TTSP Gen3 report:\n");
1192 cyttsp_xdebug("%02X %02X %02X\n", \
1193 pxy_gen3_data->hst_mode,
1194 pxy_gen3_data->tt_mode,
1195 pxy_gen3_data->tt_stat);
1196 cyttsp_xdebug("%04X %04X %02X %02X", \
1197 pxy_gen3_data->x1,
1198 pxy_gen3_data->y1,
1199 pxy_gen3_data->z1, \
1200 pxy_gen3_data->touch12_id);
1201 cyttsp_xdebug("%04X %04X %02X\n", \
1202 pxy_gen3_data->x2, \
1203 pxy_gen3_data->y2, \
1204 pxy_gen3_data->z2);
1205 cyttsp_xdebug("%02X %02X %02X\n", \
1206 pxy_gen3_data->gest_cnt, \
1207 pxy_gen3_data->gest_id, \
1208 pxy_gen3_data->gest_set);
1209 cyttsp_xdebug("%04X %04X %02X %02X\n", \
1210 pxy_gen3_data->x3, \
1211 pxy_gen3_data->y3, \
1212 pxy_gen3_data->z3, \
1213 pxy_gen3_data->touch34_id);
1214 cyttsp_xdebug("%04X %04X %02X\n", \
1215 pxy_gen3_data->x4, \
1216 pxy_gen3_data->y4, \
1217 pxy_gen3_data->z4);
1218 break;
1219 }
1220 }
1221 }
1222
1223 /* process the touches */
1224 switch (cur_tch) {
1225 case 4: {
1226 g_xy_data.x4 = be16_to_cpu(g_xy_data.x4);
1227 g_xy_data.y4 = be16_to_cpu(g_xy_data.y4);
1228 if (tilt)
1229 FLIP_XY(g_xy_data.x4, g_xy_data.y4);
1230
1231 if (rev_x) {
1232 g_xy_data.x4 = INVERT_X(g_xy_data.x4,
1233 ts->platform_data->panel_maxx);
1234 if (g_xy_data.x4 < 0)
1235 pr_debug("X value is negative. Please configure"
1236 " maxx in platform data structure\n");
1237 }
1238 if (rev_y) {
1239 g_xy_data.y4 = INVERT_X(g_xy_data.y4,
1240 ts->platform_data->panel_maxy);
1241 if (g_xy_data.y4 < 0)
1242 pr_debug("Y value is negative. Please configure"
1243 " maxy in platform data structure\n");
1244
1245 }
1246 id = GET_TOUCH4_ID(g_xy_data.touch34_id);
1247 if (ts->platform_data->use_trk_id) {
1248 cur_mt_pos[CY_MT_TCH4_IDX][CY_XPOS] =
1249 g_xy_data.x4;
1250 cur_mt_pos[CY_MT_TCH4_IDX][CY_YPOS] =
1251 g_xy_data.y4;
1252 cur_mt_z[CY_MT_TCH4_IDX] = g_xy_data.z4;
1253 } else {
1254 cur_mt_pos[id][CY_XPOS] = g_xy_data.x4;
1255 cur_mt_pos[id][CY_YPOS] = g_xy_data.y4;
1256 cur_mt_z[id] = g_xy_data.z4;
1257 }
1258 cur_mt_tch[CY_MT_TCH4_IDX] = id;
1259 cur_trk[id] = CY_TCH;
1260 if (ts->prv_st_tch[CY_ST_FNGR1_IDX] <
1261 CY_NUM_TRK_ID) {
1262 if (ts->prv_st_tch[CY_ST_FNGR1_IDX] == id) {
1263 st_x1 = g_xy_data.x4;
1264 st_y1 = g_xy_data.y4;
1265 st_z1 = g_xy_data.z4;
1266 cur_st_tch[CY_ST_FNGR1_IDX] = id;
1267 } else if (ts->prv_st_tch[CY_ST_FNGR2_IDX] == id) {
1268 st_x2 = g_xy_data.x4;
1269 st_y2 = g_xy_data.y4;
1270 st_z2 = g_xy_data.z4;
1271 cur_st_tch[CY_ST_FNGR2_IDX] = id;
1272 }
1273 }
1274 cyttsp_xdebug("4th XYZ:% 3d,% 3d,% 3d ID:% 2d\n\n", \
1275 g_xy_data.x4, g_xy_data.y4, g_xy_data.z4, \
1276 (g_xy_data.touch34_id & 0x0F));
1277 /* do not break */
1278 }
1279 case 3: {
1280 g_xy_data.x3 = be16_to_cpu(g_xy_data.x3);
1281 g_xy_data.y3 = be16_to_cpu(g_xy_data.y3);
1282 if (tilt)
1283 FLIP_XY(g_xy_data.x3, g_xy_data.y3);
1284
1285 if (rev_x) {
1286 g_xy_data.x3 = INVERT_X(g_xy_data.x3,
1287 ts->platform_data->panel_maxx);
1288 if (g_xy_data.x3 < 0)
1289 pr_debug("X value is negative. Please configure"
1290 " maxx in platform data structure\n");
1291
1292 }
1293 if (rev_y) {
1294 g_xy_data.y3 = INVERT_X(g_xy_data.y3,
1295 ts->platform_data->panel_maxy);
1296 if (g_xy_data.y3 < 0)
1297 pr_debug("Y value is negative. Please configure"
1298 " maxy in platform data structure\n");
1299
1300 }
1301 id = GET_TOUCH3_ID(g_xy_data.touch34_id);
1302 if (ts->platform_data->use_trk_id) {
1303 cur_mt_pos[CY_MT_TCH3_IDX][CY_XPOS] =
1304 g_xy_data.x3;
1305 cur_mt_pos[CY_MT_TCH3_IDX][CY_YPOS] =
1306 g_xy_data.y3;
1307 cur_mt_z[CY_MT_TCH3_IDX] = g_xy_data.z3;
1308 } else {
1309 cur_mt_pos[id][CY_XPOS] = g_xy_data.x3;
1310 cur_mt_pos[id][CY_YPOS] = g_xy_data.y3;
1311 cur_mt_z[id] = g_xy_data.z3;
1312 }
1313 cur_mt_tch[CY_MT_TCH3_IDX] = id;
1314 cur_trk[id] = CY_TCH;
1315 if (ts->prv_st_tch[CY_ST_FNGR1_IDX] <
1316 CY_NUM_TRK_ID) {
1317 if (ts->prv_st_tch[CY_ST_FNGR1_IDX] == id) {
1318 st_x1 = g_xy_data.x3;
1319 st_y1 = g_xy_data.y3;
1320 st_z1 = g_xy_data.z3;
1321 cur_st_tch[CY_ST_FNGR1_IDX] = id;
1322 } else if (ts->prv_st_tch[CY_ST_FNGR2_IDX] == id) {
1323 st_x2 = g_xy_data.x3;
1324 st_y2 = g_xy_data.y3;
1325 st_z2 = g_xy_data.z3;
1326 cur_st_tch[CY_ST_FNGR2_IDX] = id;
1327 }
1328 }
1329 cyttsp_xdebug("3rd XYZ:% 3d,% 3d,% 3d ID:% 2d\n", \
1330 g_xy_data.x3, g_xy_data.y3, g_xy_data.z3, \
1331 ((g_xy_data.touch34_id >> 4) & 0x0F));
1332 /* do not break */
1333 }
1334 case 2: {
1335 g_xy_data.x2 = be16_to_cpu(g_xy_data.x2);
1336 g_xy_data.y2 = be16_to_cpu(g_xy_data.y2);
1337 if (tilt)
1338 FLIP_XY(g_xy_data.x2, g_xy_data.y2);
1339
1340 if (rev_x) {
1341 g_xy_data.x2 = INVERT_X(g_xy_data.x2,
1342 ts->platform_data->panel_maxx);
1343 if (g_xy_data.x2 < 0)
1344 pr_debug("X value is negative. Please configure"
1345 " maxx in platform data structure\n");
1346 }
1347 if (rev_y) {
1348 g_xy_data.y2 = INVERT_X(g_xy_data.y2,
1349 ts->platform_data->panel_maxy);
1350 if (g_xy_data.y2 < 0)
1351 pr_debug("Y value is negative. Please configure"
1352 " maxy in platform data structure\n");
1353 }
1354 id = GET_TOUCH2_ID(g_xy_data.touch12_id);
1355 if (ts->platform_data->use_trk_id) {
1356 cur_mt_pos[CY_MT_TCH2_IDX][CY_XPOS] =
1357 g_xy_data.x2;
1358 cur_mt_pos[CY_MT_TCH2_IDX][CY_YPOS] =
1359 g_xy_data.y2;
1360 cur_mt_z[CY_MT_TCH2_IDX] = g_xy_data.z2;
1361 } else {
1362 cur_mt_pos[id][CY_XPOS] = g_xy_data.x2;
1363 cur_mt_pos[id][CY_YPOS] = g_xy_data.y2;
1364 cur_mt_z[id] = g_xy_data.z2;
1365 }
1366 cur_mt_tch[CY_MT_TCH2_IDX] = id;
1367 cur_trk[id] = CY_TCH;
1368 if (ts->prv_st_tch[CY_ST_FNGR1_IDX] <
1369 CY_NUM_TRK_ID) {
1370 if (ts->prv_st_tch[CY_ST_FNGR1_IDX] == id) {
1371 st_x1 = g_xy_data.x2;
1372 st_y1 = g_xy_data.y2;
1373 st_z1 = g_xy_data.z2;
1374 cur_st_tch[CY_ST_FNGR1_IDX] = id;
1375 } else if (ts->prv_st_tch[CY_ST_FNGR2_IDX] == id) {
1376 st_x2 = g_xy_data.x2;
1377 st_y2 = g_xy_data.y2;
1378 st_z2 = g_xy_data.z2;
1379 cur_st_tch[CY_ST_FNGR2_IDX] = id;
1380 }
1381 }
1382 cyttsp_xdebug("2nd XYZ:% 3d,% 3d,% 3d ID:% 2d\n", \
1383 g_xy_data.x2, g_xy_data.y2, g_xy_data.z2, \
1384 (g_xy_data.touch12_id & 0x0F));
1385 /* do not break */
1386 }
1387 case 1: {
1388 g_xy_data.x1 = be16_to_cpu(g_xy_data.x1);
1389 g_xy_data.y1 = be16_to_cpu(g_xy_data.y1);
1390 if (tilt)
1391 FLIP_XY(g_xy_data.x1, g_xy_data.y1);
1392
1393 if (rev_x) {
1394 g_xy_data.x1 = INVERT_X(g_xy_data.x1,
1395 ts->platform_data->panel_maxx);
1396 if (g_xy_data.x1 < 0)
1397 pr_debug("X value is negative. Please configure"
1398 " maxx in platform data structure\n");
1399 }
1400 if (rev_y) {
1401 g_xy_data.y1 = INVERT_X(g_xy_data.y1,
1402 ts->platform_data->panel_maxy);
1403 if (g_xy_data.y1 < 0)
1404 pr_debug("Y value is negative. Please configure"
1405 " maxy in platform data structure");
1406 }
1407 id = GET_TOUCH1_ID(g_xy_data.touch12_id);
1408 if (ts->platform_data->use_trk_id) {
1409 cur_mt_pos[CY_MT_TCH1_IDX][CY_XPOS] =
1410 g_xy_data.x1;
1411 cur_mt_pos[CY_MT_TCH1_IDX][CY_YPOS] =
1412 g_xy_data.y1;
1413 cur_mt_z[CY_MT_TCH1_IDX] = g_xy_data.z1;
1414 } else {
1415 cur_mt_pos[id][CY_XPOS] = g_xy_data.x1;
1416 cur_mt_pos[id][CY_YPOS] = g_xy_data.y1;
1417 cur_mt_z[id] = g_xy_data.z1;
1418 }
1419 cur_mt_tch[CY_MT_TCH1_IDX] = id;
1420 cur_trk[id] = CY_TCH;
1421 if (ts->prv_st_tch[CY_ST_FNGR1_IDX] <
1422 CY_NUM_TRK_ID) {
1423 if (ts->prv_st_tch[CY_ST_FNGR1_IDX] == id) {
1424 st_x1 = g_xy_data.x1;
1425 st_y1 = g_xy_data.y1;
1426 st_z1 = g_xy_data.z1;
1427 cur_st_tch[CY_ST_FNGR1_IDX] = id;
1428 } else if (ts->prv_st_tch[CY_ST_FNGR2_IDX] == id) {
1429 st_x2 = g_xy_data.x1;
1430 st_y2 = g_xy_data.y1;
1431 st_z2 = g_xy_data.z1;
1432 cur_st_tch[CY_ST_FNGR2_IDX] = id;
1433 }
1434 }
1435 cyttsp_xdebug("1st XYZ:% 3d,% 3d,% 3d ID:% 2d\n", \
1436 g_xy_data.x1, g_xy_data.y1, g_xy_data.z1, \
1437 ((g_xy_data.touch12_id >> 4) & 0x0F));
1438 break;
1439 }
1440 case 0:
1441 default:{
1442 break;
1443 }
1444 }
1445
1446 /* handle Single Touch signals */
1447 if (ts->platform_data->use_st) {
1448 cyttsp_xdebug("ST STEP 0 - ST1 ID=%d ST2 ID=%d\n", \
1449 cur_st_tch[CY_ST_FNGR1_IDX], \
1450 cur_st_tch[CY_ST_FNGR2_IDX]);
1451 if (cur_st_tch[CY_ST_FNGR1_IDX] > CY_NUM_TRK_ID) {
1452 /* reassign finger 1 and 2 positions to new tracks */
1453 if (cur_tch > 0) {
1454 /* reassign st finger1 */
1455 if (ts->platform_data->use_trk_id) {
1456 id = CY_MT_TCH1_IDX;
1457 cur_st_tch[CY_ST_FNGR1_IDX] = cur_mt_tch[id];
1458 } else {
1459 id = GET_TOUCH1_ID(g_xy_data.touch12_id);
1460 cur_st_tch[CY_ST_FNGR1_IDX] = id;
1461 }
1462 st_x1 = cur_mt_pos[id][CY_XPOS];
1463 st_y1 = cur_mt_pos[id][CY_YPOS];
1464 st_z1 = cur_mt_z[id];
1465 cyttsp_xdebug("ST STEP 1 - ST1 ID=%3d\n", \
1466 cur_st_tch[CY_ST_FNGR1_IDX]);
1467 if ((cur_tch > 1) &&
1468 (cur_st_tch[CY_ST_FNGR2_IDX] >
1469 CY_NUM_TRK_ID)) {
1470 /* reassign st finger2 */
1471 if (cur_tch > 1) {
1472 if (ts->platform_data->use_trk_id) {
1473 id = CY_MT_TCH2_IDX;
1474 cur_st_tch[CY_ST_FNGR2_IDX] = cur_mt_tch[id];
1475 } else {
1476 id = GET_TOUCH2_ID(g_xy_data.touch12_id);
1477 cur_st_tch[CY_ST_FNGR2_IDX] = id;
1478 }
1479 st_x2 = cur_mt_pos[id][CY_XPOS];
1480 st_y2 = cur_mt_pos[id][CY_YPOS];
1481 st_z2 = cur_mt_z[id];
1482 cyttsp_xdebug("ST STEP 2 - ST2 ID=%3d\n", \
1483 cur_st_tch[CY_ST_FNGR2_IDX]);
1484 }
1485 }
1486 }
1487 } else if (cur_st_tch[CY_ST_FNGR2_IDX] > CY_NUM_TRK_ID) {
1488 if (cur_tch > 1) {
1489 /* reassign st finger2 */
1490 if (ts->platform_data->use_trk_id) {
1491 /* reassign st finger2 */
1492 id = CY_MT_TCH2_IDX;
1493 cur_st_tch[CY_ST_FNGR2_IDX] =
1494 cur_mt_tch[id];
1495 } else {
1496 /* reassign st finger2 */
1497 id = GET_TOUCH2_ID(g_xy_data.touch12_id);
1498 cur_st_tch[CY_ST_FNGR2_IDX] = id;
1499 }
1500 st_x2 = cur_mt_pos[id][CY_XPOS];
1501 st_y2 = cur_mt_pos[id][CY_YPOS];
1502 st_z2 = cur_mt_z[id];
1503 cyttsp_xdebug("ST STEP 3 - ST2 ID=%3d\n", \
1504 cur_st_tch[CY_ST_FNGR2_IDX]);
1505 }
1506 }
1507 /* if the 1st touch is missing and there is a 2nd touch,
1508 * then set the 1st touch to 2nd touch and terminate 2nd touch
1509 */
1510 if ((cur_st_tch[CY_ST_FNGR1_IDX] > CY_NUM_TRK_ID) &&
1511 (cur_st_tch[CY_ST_FNGR2_IDX] < CY_NUM_TRK_ID)) {
1512 st_x1 = st_x2;
1513 st_y1 = st_y2;
1514 st_z1 = st_z2;
1515 cur_st_tch[CY_ST_FNGR1_IDX] =
1516 cur_st_tch[CY_ST_FNGR2_IDX];
1517 cur_st_tch[CY_ST_FNGR2_IDX] =
1518 CY_IGNR_TCH;
1519 }
1520 /* if the 2nd touch ends up equal to the 1st touch,
1521 * then just report a single touch */
1522 if (cur_st_tch[CY_ST_FNGR1_IDX] ==
1523 cur_st_tch[CY_ST_FNGR2_IDX]) {
1524 cur_st_tch[CY_ST_FNGR2_IDX] =
1525 CY_IGNR_TCH;
1526 }
1527 /* set Single Touch current event signals */
1528 if (cur_st_tch[CY_ST_FNGR1_IDX] < CY_NUM_TRK_ID) {
1529 input_report_abs(ts->input,
1530 ABS_X, st_x1);
1531 input_report_abs(ts->input,
1532 ABS_Y, st_y1);
1533 input_report_abs(ts->input,
1534 ABS_PRESSURE, st_z1);
1535 input_report_key(ts->input,
1536 BTN_TOUCH,
1537 CY_TCH);
1538 input_report_abs(ts->input,
1539 ABS_TOOL_WIDTH,
1540 curr_tool_width);
1541 cyttsp_debug("ST->F1:%3d X:%3d Y:%3d Z:%3d\n", \
1542 cur_st_tch[CY_ST_FNGR1_IDX], \
1543 st_x1, st_y1, st_z1);
1544 if (cur_st_tch[CY_ST_FNGR2_IDX] < CY_NUM_TRK_ID) {
1545 input_report_key(ts->input, BTN_2, CY_TCH);
1546 input_report_abs(ts->input, ABS_HAT0X, st_x2);
1547 input_report_abs(ts->input, ABS_HAT0Y, st_y2);
1548 cyttsp_debug("ST->F2:%3d X:%3d Y:%3d Z:%3d\n", \
1549 cur_st_tch[CY_ST_FNGR2_IDX],
1550 st_x2, st_y2, st_z2);
1551 } else {
1552 input_report_key(ts->input,
1553 BTN_2,
1554 CY_NTCH);
1555 }
1556 } else {
1557 input_report_abs(ts->input, ABS_PRESSURE, CY_NTCH);
1558 input_report_key(ts->input, BTN_TOUCH, CY_NTCH);
1559 input_report_key(ts->input, BTN_2, CY_NTCH);
1560 }
1561 /* update platform data for the current single touch info */
1562 ts->prv_st_tch[CY_ST_FNGR1_IDX] = cur_st_tch[CY_ST_FNGR1_IDX];
1563 ts->prv_st_tch[CY_ST_FNGR2_IDX] = cur_st_tch[CY_ST_FNGR2_IDX];
1564
1565 }
1566
1567 /* handle Multi-touch signals */
1568 if (ts->platform_data->use_mt) {
1569 if (ts->platform_data->use_trk_id) {
1570 /* terminate any previous touch where the track
1571 * is missing from the current event */
1572 for (id = 0; id < CY_NUM_TRK_ID; id++) {
1573 if ((ts->act_trk[id] != CY_NTCH) &&
1574 (cur_trk[id] == CY_NTCH)) {
1575 input_report_abs(ts->input,
1576 ABS_MT_TRACKING_ID,
1577 id);
1578 input_report_abs(ts->input,
1579 ABS_MT_TOUCH_MAJOR,
1580 CY_NTCH);
1581 input_report_abs(ts->input,
1582 ABS_MT_WIDTH_MAJOR,
1583 curr_tool_width);
1584 input_report_abs(ts->input,
1585 ABS_MT_POSITION_X,
1586 ts->prv_mt_pos[id][CY_XPOS]);
1587 input_report_abs(ts->input,
1588 ABS_MT_POSITION_Y,
1589 ts->prv_mt_pos[id][CY_YPOS]);
1590 CY_MT_SYNC(ts->input);
1591 ts->act_trk[id] = CY_NTCH;
1592 ts->prv_mt_pos[id][CY_XPOS] = 0;
1593 ts->prv_mt_pos[id][CY_YPOS] = 0;
1594 }
1595 }
1596 /* set Multi-Touch current event signals */
1597 for (id = 0; id < CY_NUM_MT_TCH_ID; id++) {
1598 if (cur_mt_tch[id] < CY_NUM_TRK_ID) {
1599 input_report_abs(ts->input,
1600 ABS_MT_TRACKING_ID,
1601 cur_mt_tch[id]);
1602 input_report_abs(ts->input,
1603 ABS_MT_TOUCH_MAJOR,
1604 cur_mt_z[id]);
1605 input_report_abs(ts->input,
1606 ABS_MT_WIDTH_MAJOR,
1607 curr_tool_width);
1608 input_report_abs(ts->input,
1609 ABS_MT_POSITION_X,
1610 cur_mt_pos[id][CY_XPOS]);
1611 input_report_abs(ts->input,
1612 ABS_MT_POSITION_Y,
1613 cur_mt_pos[id][CY_YPOS]);
1614 CY_MT_SYNC(ts->input);
1615 ts->act_trk[id] = CY_TCH;
1616 ts->prv_mt_pos[id][CY_XPOS] =
1617 cur_mt_pos[id][CY_XPOS];
1618 ts->prv_mt_pos[id][CY_YPOS] =
1619 cur_mt_pos[id][CY_YPOS];
1620 }
1621 }
1622 } else {
1623 /* set temporary track array elements to voids */
1624 for (id = 0; id < CY_NUM_MT_TCH_ID; id++) {
1625 tmp_trk[id] = CY_IGNR_TCH;
1626 snd_trk[id] = CY_IGNR_TCH;
1627 }
1628
1629 /* get what is currently active */
1630 for (i = 0, id = 0;
1631 id < CY_NUM_TRK_ID && i < CY_NUM_MT_TCH_ID;
1632 id++) {
1633 if (cur_trk[id] == CY_TCH) {
1634 /* only incr counter if track found */
1635 tmp_trk[i] = id;
1636 i++;
1637 }
1638 }
1639 cyttsp_xdebug("T1: t0=%d, t1=%d, t2=%d, t3=%d\n", \
1640 tmp_trk[0], tmp_trk[1], tmp_trk[2], \
1641 tmp_trk[3]);
1642 cyttsp_xdebug("T1: p0=%d, p1=%d, p2=%d, p3=%d\n", \
1643 ts->prv_mt_tch[0], ts->prv_mt_tch[1], \
1644 ts->prv_mt_tch[2], ts->prv_mt_tch[3]);
1645
1646 /* pack in still active previous touches */
1647 for (id = 0, prv_tch = 0;
1648 id < CY_NUM_MT_TCH_ID; id++) {
1649 if (tmp_trk[id] < CY_NUM_TRK_ID) {
1650 if (cyttsp_inlist(ts->prv_mt_tch,
1651 tmp_trk[id], &loc,
1652 CY_NUM_MT_TCH_ID)) {
1653 loc &= CY_NUM_MT_TCH_ID - 1;
1654 snd_trk[loc] = tmp_trk[id];
1655 prv_tch++;
1656 cyttsp_xdebug("inlist s[%d]=%d t[%d]=%d l=%d p=%d\n", \
1657 loc, snd_trk[loc], \
1658 id, tmp_trk[id], \
1659 loc, prv_tch);
1660 } else {
1661 cyttsp_xdebug("not inlist s[%d]=%d t[%d]=%d l=%d \n", \
1662 id, snd_trk[id], \
1663 id, tmp_trk[id], \
1664 loc);
1665 }
1666 }
1667 }
1668 cyttsp_xdebug("S1: s0=%d, s1=%d, s2=%d, s3=%d p=%d\n", \
1669 snd_trk[0], snd_trk[1], snd_trk[2], \
1670 snd_trk[3], prv_tch);
1671
1672 /* pack in new touches */
1673 for (id = 0; id < CY_NUM_MT_TCH_ID; id++) {
1674 if (tmp_trk[id] < CY_NUM_TRK_ID) {
1675 if (!cyttsp_inlist(snd_trk, tmp_trk[id], &loc, CY_NUM_MT_TCH_ID)) {
1676 cyttsp_xdebug("not inlist t[%d]=%d l=%d\n", \
1677 id, tmp_trk[id], loc);
1678 if (cyttsp_next_avail_inlist(snd_trk, &loc, CY_NUM_MT_TCH_ID)) {
1679 loc &= CY_NUM_MT_TCH_ID - 1;
1680 snd_trk[loc] = tmp_trk[id];
1681 cyttsp_xdebug("put inlist s[%d]=%d t[%d]=%d\n",
1682 loc, snd_trk[loc], id, tmp_trk[id]);
1683 }
1684 } else {
1685 cyttsp_xdebug("is in list s[%d]=%d t[%d]=%d loc=%d\n", \
1686 id, snd_trk[id], id, tmp_trk[id], loc);
1687 }
1688 }
1689 }
1690 cyttsp_xdebug("S2: s0=%d, s1=%d, s2=%d, s3=%d\n", \
1691 snd_trk[0], snd_trk[1],
1692 snd_trk[2], snd_trk[3]);
1693
1694 /* sync motion event signals for each current touch */
1695 for (id = 0; id < CY_NUM_MT_TCH_ID; id++) {
1696 /* z will either be 0 (NOTOUCH) or
1697 * some pressure (TOUCH) */
1698 cyttsp_xdebug("MT0 prev[%d]=%d temp[%d]=%d send[%d]=%d\n", \
1699 id, ts->prv_mt_tch[id], \
1700 id, tmp_trk[id], \
1701 id, snd_trk[id]);
1702 if (snd_trk[id] < CY_NUM_TRK_ID) {
1703 input_report_abs(ts->input,
1704 ABS_MT_TOUCH_MAJOR,
1705 cur_mt_z[snd_trk[id]]);
1706 input_report_abs(ts->input,
1707 ABS_MT_WIDTH_MAJOR,
1708 curr_tool_width);
1709 input_report_abs(ts->input,
1710 ABS_MT_POSITION_X,
1711 cur_mt_pos[snd_trk[id]][CY_XPOS]);
1712 input_report_abs(ts->input,
1713 ABS_MT_POSITION_Y,
1714 cur_mt_pos[snd_trk[id]][CY_YPOS]);
1715 CY_MT_SYNC(ts->input);
1716 cyttsp_debug("MT1->TID:%2d X:%3d Y:%3d Z:%3d touch-sent\n", \
1717 snd_trk[id], \
1718 cur_mt_pos[snd_trk[id]][CY_XPOS], \
1719 cur_mt_pos[snd_trk[id]][CY_YPOS], \
1720 cur_mt_z[snd_trk[id]]);
1721 } else if (ts->prv_mt_tch[id] < CY_NUM_TRK_ID) {
1722 /* void out this touch */
1723 input_report_abs(ts->input,
1724 ABS_MT_TOUCH_MAJOR,
1725 CY_NTCH);
1726 input_report_abs(ts->input,
1727 ABS_MT_WIDTH_MAJOR,
1728 curr_tool_width);
1729 input_report_abs(ts->input,
1730 ABS_MT_POSITION_X,
1731 ts->prv_mt_pos[ts->prv_mt_tch[id]][CY_XPOS]);
1732 input_report_abs(ts->input,
1733 ABS_MT_POSITION_Y,
1734 ts->prv_mt_pos[ts->prv_mt_tch[id]][CY_YPOS]);
1735 CY_MT_SYNC(ts->input);
1736 cyttsp_debug("MT2->TID:%2d X:%3d Y:%3d Z:%3d lift off-sent\n", \
1737 ts->prv_mt_tch[id], \
1738 ts->prv_mt_pos[ts->prv_mt_tch[id]][CY_XPOS], \
1739 ts->prv_mt_pos[ts->prv_mt_tch[id]][CY_YPOS], \
1740 CY_NTCH);
1741 } else {
1742 /* do not stuff any signals for this
1743 * previously and currently
1744 * void touches */
1745 cyttsp_xdebug("MT3->send[%d]=%d - No touch - NOT sent\n", \
1746 id, snd_trk[id]);
1747 }
1748 }
1749
1750 /* save current posted tracks to
1751 * previous track memory */
1752 for (id = 0; id < CY_NUM_MT_TCH_ID; id++) {
1753 ts->prv_mt_tch[id] = snd_trk[id];
1754 if (snd_trk[id] < CY_NUM_TRK_ID) {
1755 ts->prv_mt_pos[snd_trk[id]][CY_XPOS] =
1756 cur_mt_pos[snd_trk[id]][CY_XPOS];
1757 ts->prv_mt_pos[snd_trk[id]][CY_YPOS] =
1758 cur_mt_pos[snd_trk[id]][CY_YPOS];
1759 cyttsp_xdebug("MT4->TID:%2d X:%3d Y:%3d Z:%3d save for previous\n", \
1760 snd_trk[id], \
1761 ts->prv_mt_pos[snd_trk[id]][CY_XPOS], \
1762 ts->prv_mt_pos[snd_trk[id]][CY_YPOS], \
1763 CY_NTCH);
1764 }
1765 }
1766 for (id = 0; id < CY_NUM_TRK_ID; id++)
1767 ts->act_trk[id] = CY_NTCH;
1768 for (id = 0; id < CY_NUM_MT_TCH_ID; id++) {
1769 if (snd_trk[id] < CY_NUM_TRK_ID)
1770 ts->act_trk[snd_trk[id]] = CY_TCH;
1771 }
1772 }
1773 }
1774
1775 /* handle gestures */
1776 if (ts->platform_data->use_gestures) {
1777 if (g_xy_data.gest_id) {
1778 input_report_key(ts->input,
1779 BTN_3, CY_TCH);
1780 input_report_abs(ts->input,
1781 ABS_HAT1X, g_xy_data.gest_id);
1782 input_report_abs(ts->input,
1783 ABS_HAT2Y, g_xy_data.gest_cnt);
1784 }
1785 }
1786
1787 /* signal the view motion event */
1788 input_sync(ts->input);
1789
1790 for (id = 0; id < CY_NUM_TRK_ID; id++) {
1791 /* update platform data for the current MT information */
1792 ts->act_trk[id] = cur_trk[id];
1793 }
1794
1795exit_xy_worker:
1796 if (cyttsp_disable_touch) {
1797 /* Turn off the touch interrupts */
1798 cyttsp_debug("Not enabling touch\n");
1799 } else {
1800 if (ts->client->irq == 0) {
1801 /* restart event timer */
1802 mod_timer(&ts->timer, jiffies + TOUCHSCREEN_TIMEOUT);
1803 } else {
1804 /* re-enable the interrupt after processing */
1805 enable_irq(ts->client->irq);
1806 }
1807 }
1808 return;
1809}
1810
1811static int cyttsp_inlist(u16 prev_track[], u8 cur_trk_id,
1812 u8 *prev_loc, u8 num_touches)
1813{
1814 u8 id = 0;
1815
1816 *prev_loc = CY_IGNR_TCH;
1817
1818 cyttsp_xdebug("IN p[%d]=%d c=%d n=%d loc=%d\n", \
1819 id, prev_track[id], cur_trk_id, \
1820 num_touches, *prev_loc);
1821 for (id = 0, *prev_loc = CY_IGNR_TCH;
1822 (id < num_touches); id++) {
1823 cyttsp_xdebug("p[%d]=%d c=%d n=%d loc=%d\n", \
1824 id, prev_track[id], cur_trk_id, \
1825 num_touches, *prev_loc);
1826 if (prev_track[id] == cur_trk_id) {
1827 *prev_loc = id;
1828 break;
1829 }
1830 }
1831 cyttsp_xdebug("OUT p[%d]=%d c=%d n=%d loc=%d\n", \
1832 id, prev_track[id], cur_trk_id, num_touches, *prev_loc);
1833
1834 return ((*prev_loc < CY_NUM_TRK_ID) ? true : false);
1835}
1836
1837static int cyttsp_next_avail_inlist(u16 cur_trk[],
1838 u8 *new_loc, u8 num_touches)
1839{
1840 u8 id;
1841
1842 for (id = 0, *new_loc = CY_IGNR_TCH;
1843 (id < num_touches); id++) {
1844 if (cur_trk[id] > CY_NUM_TRK_ID) {
1845 *new_loc = id;
1846 break;
1847 }
1848 }
1849
1850 return ((*new_loc < CY_NUM_TRK_ID) ? true : false);
1851}
1852
1853/* Timer function used as dummy interrupt driver */
1854static void cyttsp_timer(unsigned long handle)
1855{
1856 struct cyttsp *ts = (struct cyttsp *) handle;
1857
1858 cyttsp_xdebug("TTSP Device timer event\n");
1859
1860 /* schedule motion signal handling */
1861 queue_work(cyttsp_ts_wq, &ts->work);
1862
1863 return;
1864}
1865
1866
1867
1868/* ************************************************************************
1869 * ISR function. This function is general, initialized in drivers init
1870 * function
1871 * ************************************************************************ */
1872static irqreturn_t cyttsp_irq(int irq, void *handle)
1873{
1874 struct cyttsp *ts = (struct cyttsp *) handle;
1875
1876 cyttsp_xdebug("%s: Got IRQ\n", CY_I2C_NAME);
1877
1878 /* disable further interrupts until this interrupt is processed */
1879 disable_irq_nosync(ts->client->irq);
1880
1881 /* schedule motion signal handling */
1882 queue_work(cyttsp_ts_wq, &ts->work);
1883 return IRQ_HANDLED;
1884}
1885
1886/* ************************************************************************
1887 * Probe initialization functions
1888 * ************************************************************************ */
1889static int cyttsp_putbl(struct cyttsp *ts, int show,
1890 int show_status, int show_version, int show_cid)
1891{
1892 int retval = CY_OK;
1893
1894 int num_bytes = (show_status * 3) + (show_version * 6) + (show_cid * 3);
1895
1896 if (show_cid)
1897 num_bytes = sizeof(struct cyttsp_bootloader_data_t);
1898 else if (show_version)
1899 num_bytes = sizeof(struct cyttsp_bootloader_data_t) - 3;
1900 else
1901 num_bytes = sizeof(struct cyttsp_bootloader_data_t) - 9;
1902
1903 if (show) {
1904 retval = i2c_smbus_read_i2c_block_data(ts->client,
1905 CY_REG_BASE, num_bytes, (u8 *)&g_bl_data);
1906 if (show_status) {
1907 cyttsp_debug("BL%d: f=%02X s=%02X err=%02X bl=%02X%02X bld=%02X%02X\n", \
1908 show, \
1909 g_bl_data.bl_file, \
1910 g_bl_data.bl_status, \
1911 g_bl_data.bl_error, \
1912 g_bl_data.blver_hi, g_bl_data.blver_lo, \
1913 g_bl_data.bld_blver_hi, g_bl_data.bld_blver_lo);
1914 }
1915 if (show_version) {
1916 cyttsp_debug("BL%d: ttspver=0x%02X%02X appid=0x%02X%02X appver=0x%02X%02X\n", \
1917 show, \
1918 g_bl_data.ttspver_hi, g_bl_data.ttspver_lo, \
1919 g_bl_data.appid_hi, g_bl_data.appid_lo, \
1920 g_bl_data.appver_hi, g_bl_data.appver_lo);
1921 }
1922 if (show_cid) {
1923 cyttsp_debug("BL%d: cid=0x%02X%02X%02X\n", \
1924 show, \
1925 g_bl_data.cid_0, \
1926 g_bl_data.cid_1, \
1927 g_bl_data.cid_2);
1928 }
1929 }
1930
1931 return retval;
1932}
1933
1934#ifdef CY_INCLUDE_LOAD_FILE
1935#define CY_MAX_I2C_LEN 256
1936#define CY_MAX_TRY 10
1937#define CY_BL_PAGE_SIZE 16
1938#define CY_BL_NUM_PAGES 5
1939static int cyttsp_i2c_wr_blk_chunks(struct cyttsp *ts, u8 command,
1940 u8 length, const u8 *values)
1941{
1942 int retval = CY_OK;
1943 int block = 1;
1944
1945 u8 dataray[CY_MAX_I2C_LEN];
1946
1947 /* first page already includes the bl page offset */
1948 retval = i2c_smbus_write_i2c_block_data(ts->client, CY_REG_BASE,
1949 CY_BL_PAGE_SIZE+1, values);
1950 values += CY_BL_PAGE_SIZE+1;
1951 length -= CY_BL_PAGE_SIZE+1;
1952
1953 /* rem blocks require bl page offset stuffing */
1954 while (length &&
1955 (block < CY_BL_NUM_PAGES) &&
1956 !(retval < CY_OK)) {
1957 udelay(43*2); /* TRM * 2 */
1958 dataray[0] = CY_BL_PAGE_SIZE*block;
1959 memcpy(&dataray[1], values,
1960 length >= CY_BL_PAGE_SIZE ?
1961 CY_BL_PAGE_SIZE : length);
1962 retval = i2c_smbus_write_i2c_block_data(ts->client,
1963 CY_REG_BASE,
1964 length >= CY_BL_PAGE_SIZE ?
1965 CY_BL_PAGE_SIZE + 1 : length+1, dataray);
1966 values += CY_BL_PAGE_SIZE;
1967 length = length >= CY_BL_PAGE_SIZE ?
1968 length - CY_BL_PAGE_SIZE : 0;
1969 block++;
1970 }
1971
1972 return retval;
1973}
1974
1975static int cyttsp_bootload_app(struct cyttsp *ts)
1976{
1977 int retval = CY_OK;
1978 int i, tries;
1979 u8 host_reg;
1980
1981 cyttsp_debug("load new firmware \n");
1982 /* reset TTSP Device back to bootloader mode */
1983 host_reg = CY_SOFT_RESET_MODE;
1984 retval = i2c_smbus_write_i2c_block_data(ts->client, CY_REG_BASE,
1985 sizeof(host_reg), &host_reg);
1986 /* wait for TTSP Device to complete reset back to bootloader */
1987 tries = 0;
1988 do {
1989 mdelay(1);
1990 cyttsp_putbl(ts, 3, false, false, false);
1991 } while (g_bl_data.bl_status != 0x10 &&
1992 g_bl_data.bl_status != 0x11 &&
1993 tries++ < 100);
1994 cyttsp_debug("load file - tver=0x%02X%02X a_id=0x%02X%02X aver=0x%02X%02X\n", \
1995 cyttsp_fw_tts_verh, cyttsp_fw_tts_verl, \
1996 cyttsp_fw_app_idh, cyttsp_fw_app_idl, \
1997 cyttsp_fw_app_verh, cyttsp_fw_app_verl);
1998
1999 /* download new TTSP Application to the Bootloader */
2000 if (!(retval < CY_OK)) {
2001 i = 0;
2002 /* send bootload initiation command */
2003 if (cyttsp_fw[i].Command == CY_BL_INIT_LOAD) {
2004 g_bl_data.bl_file = 0;
2005 g_bl_data.bl_status = 0;
2006 g_bl_data.bl_error = 0;
2007 retval = i2c_smbus_write_i2c_block_data(ts->client,
2008 CY_REG_BASE,
2009 cyttsp_fw[i].Length, cyttsp_fw[i].Block);
2010 /* delay to allow bl to get ready for block writes */
2011 i++;
2012 tries = 0;
2013 do {
2014 mdelay(100);
2015 cyttsp_putbl(ts, 4, false, false, false);
2016 } while (g_bl_data.bl_status != 0x10 &&
2017 g_bl_data.bl_status != 0x11 &&
2018 tries++ < 100);
2019 cyttsp_debug("wait init f=%02X, s=%02X, e=%02X t=%d\n", \
2020 g_bl_data.bl_file, g_bl_data.bl_status, \
2021 g_bl_data.bl_error, tries);
2022 /* send bootload firmware load blocks */
2023 if (!(retval < CY_OK)) {
2024 while (cyttsp_fw[i].Command == CY_BL_WRITE_BLK) {
2025 retval = cyttsp_i2c_wr_blk_chunks(ts,
2026 CY_REG_BASE,
2027 cyttsp_fw[i].Length,
2028 cyttsp_fw[i].Block);
2029 cyttsp_xdebug("BL DNLD Rec=% 3d Len=% 3d Addr=%04X\n", \
2030 cyttsp_fw[i].Record, \
2031 cyttsp_fw[i].Length, \
2032 cyttsp_fw[i].Address);
2033 i++;
2034 if (retval < CY_OK) {
2035 cyttsp_debug("BL fail Rec=%3d retval=%d\n", \
2036 cyttsp_fw[i-1].Record, \
2037 retval);
2038 break;
2039 } else {
2040 tries = 0;
2041 cyttsp_putbl(ts, 5, false, false, false);
2042 while (!((g_bl_data.bl_status == 0x10) &&
2043 (g_bl_data.bl_error == 0x20)) &&
2044 !((g_bl_data.bl_status == 0x11) &&
2045 (g_bl_data.bl_error == 0x20)) &&
2046 (tries++ < 100)) {
2047 mdelay(1);
2048 cyttsp_putbl(ts, 5, false, false, false);
2049 }
2050 }
2051 }
2052
2053 if (!(retval < CY_OK)) {
2054 while (i < cyttsp_fw_records) {
2055 retval = i2c_smbus_write_i2c_block_data(ts->client, CY_REG_BASE,
2056 cyttsp_fw[i].Length,
2057 cyttsp_fw[i].Block);
2058 i++;
2059 tries = 0;
2060 do {
2061 mdelay(100);
2062 cyttsp_putbl(ts, 6, true, false, false);
2063 } while (g_bl_data.bl_status != 0x10 &&
2064 g_bl_data.bl_status != 0x11 &&
2065 tries++ < 100);
2066 cyttsp_debug("wait term f=%02X, s=%02X, e=%02X t=%d\n", \
2067 g_bl_data.bl_file, \
2068 g_bl_data.bl_status, \
2069 g_bl_data.bl_error, \
2070 tries);
2071 if (retval < CY_OK)
2072 break;
2073 }
2074 }
2075 }
2076 }
2077 }
2078
2079 /* reset TTSP Device back to bootloader mode */
2080 host_reg = CY_SOFT_RESET_MODE;
2081 retval = i2c_smbus_write_i2c_block_data(ts->client, CY_REG_BASE,
2082 sizeof(host_reg), &host_reg);
2083 /* wait for TTSP Device to complete reset back to bootloader */
2084 tries = 0;
2085 do {
2086 mdelay(1);
2087 cyttsp_putbl(ts, 3, false, false, false);
2088 } while (g_bl_data.bl_status != 0x10 &&
2089 g_bl_data.bl_status != 0x11 &&
2090 tries++ < 100);
2091
2092 /* set arg2 to non-0 to activate */
2093 retval = cyttsp_putbl(ts, 8, true, true, true);
2094
2095 return retval;
2096}
2097#else
2098static int cyttsp_bootload_app(struct cyttsp *ts)
2099{
2100 cyttsp_debug("no-load new firmware \n");
2101 return CY_OK;
2102}
2103#endif /* CY_INCLUDE_LOAD_FILE */
2104
2105
2106static int cyttsp_power_on(struct cyttsp *ts)
2107{
2108 int retval = CY_OK;
2109 u8 host_reg;
2110 int tries;
2111
2112 cyttsp_debug("Power up \n");
2113
2114 /* check if the TTSP device has a bootloader installed */
2115 host_reg = CY_SOFT_RESET_MODE;
2116 retval = i2c_smbus_write_i2c_block_data(ts->client, CY_REG_BASE,
2117 sizeof(host_reg), &host_reg);
2118 tries = 0;
2119 do {
2120 mdelay(1);
2121
2122 /* set arg2 to non-0 to activate */
2123 retval = cyttsp_putbl(ts, 1, true, true, true);
2124 cyttsp_info("BL%d: f=%02X s=%02X err=%02X bl=%02X%02X bld=%02X%02X R=%d\n", \
2125 101, \
2126 g_bl_data.bl_file, g_bl_data.bl_status, \
2127 g_bl_data.bl_error, \
2128 g_bl_data.blver_hi, g_bl_data.blver_lo, \
2129 g_bl_data.bld_blver_hi, g_bl_data.bld_blver_lo,
2130 retval);
2131 cyttsp_info("BL%d: tver=%02X%02X a_id=%02X%02X aver=%02X%02X\n", \
2132 102, \
2133 g_bl_data.ttspver_hi, g_bl_data.ttspver_lo, \
2134 g_bl_data.appid_hi, g_bl_data.appid_lo, \
2135 g_bl_data.appver_hi, g_bl_data.appver_lo);
2136 cyttsp_info("BL%d: c_id=%02X%02X%02X\n", \
2137 103, \
2138 g_bl_data.cid_0, g_bl_data.cid_1, g_bl_data.cid_2);
2139 } while (!(retval < CY_OK) &&
2140 !GET_BOOTLOADERMODE(g_bl_data.bl_status) &&
2141 !(g_bl_data.bl_file == CY_OP_MODE + CY_LOW_PWR_MODE) &&
2142 tries++ < 100);
2143
2144 /* is bootloader missing? */
2145 if (!(retval < CY_OK)) {
2146 cyttsp_xdebug("Ret=%d Check if bootloader is missing...\n", \
2147 retval);
2148 if (!GET_BOOTLOADERMODE(g_bl_data.bl_status)) {
2149 /* skip all bl and sys info and go to op mode */
2150 if (!(retval < CY_OK)) {
2151 cyttsp_xdebug("Bl is missing (ret=%d)\n", \
2152 retval);
2153 host_reg = CY_OP_MODE/* + CY_LOW_PWR_MODE*/;
2154 retval = i2c_smbus_write_i2c_block_data(ts->client, CY_REG_BASE,
2155 sizeof(host_reg), &host_reg);
2156 /* wait for TTSP Device to complete switch to
2157 * Operational mode */
2158 mdelay(1000);
2159 goto bypass;
2160 }
2161 }
2162 }
2163
2164
2165 /* take TTSP out of bootloader mode; go to TrueTouch operational mode */
2166 if (!(retval < CY_OK)) {
2167 cyttsp_xdebug1("exit bootloader; go operational\n");
2168 retval = i2c_smbus_write_i2c_block_data(ts->client,
2169 CY_REG_BASE, sizeof(bl_cmd), bl_cmd);
2170 tries = 0;
2171 do {
2172 mdelay(100);
2173 cyttsp_putbl(ts, 4, true, false, false);
2174 cyttsp_info("BL%d: f=%02X s=%02X err=%02X bl=%02X%02X bld=%02X%02X\n", \
2175 104, \
2176 g_bl_data.bl_file, g_bl_data.bl_status, \
2177 g_bl_data.bl_error, \
2178 g_bl_data.blver_hi, g_bl_data.blver_lo, \
2179 g_bl_data.bld_blver_hi, g_bl_data.bld_blver_lo);
2180 } while (GET_BOOTLOADERMODE(g_bl_data.bl_status) &&
2181 tries++ < 100);
2182 }
2183
2184
2185
2186 if (!(retval < CY_OK) &&
2187 cyttsp_app_load()) {
2188 if (CY_DIFF(g_bl_data.ttspver_hi, cyttsp_tts_verh()) ||
2189 CY_DIFF(g_bl_data.ttspver_lo, cyttsp_tts_verl()) ||
2190 CY_DIFF(g_bl_data.appid_hi, cyttsp_app_idh()) ||
2191 CY_DIFF(g_bl_data.appid_lo, cyttsp_app_idl()) ||
2192 CY_DIFF(g_bl_data.appver_hi, cyttsp_app_verh()) ||
2193 CY_DIFF(g_bl_data.appver_lo, cyttsp_app_verl()) ||
2194 CY_DIFF(g_bl_data.cid_0, cyttsp_cid_0()) ||
2195 CY_DIFF(g_bl_data.cid_1, cyttsp_cid_1()) ||
2196 CY_DIFF(g_bl_data.cid_2, cyttsp_cid_2()) ||
2197 cyttsp_force_fw_load()) {
2198 cyttsp_debug("blttsp=0x%02X%02X flttsp=0x%02X%02X force=%d\n", \
2199 g_bl_data.ttspver_hi, g_bl_data.ttspver_lo, \
2200 cyttsp_tts_verh(), cyttsp_tts_verl(), \
2201 cyttsp_force_fw_load());
2202 cyttsp_debug("blappid=0x%02X%02X flappid=0x%02X%02X\n", \
2203 g_bl_data.appid_hi, g_bl_data.appid_lo, \
2204 cyttsp_app_idh(), cyttsp_app_idl());
2205 cyttsp_debug("blappver=0x%02X%02X flappver=0x%02X%02X\n", \
2206 g_bl_data.appver_hi, g_bl_data.appver_lo, \
2207 cyttsp_app_verh(), cyttsp_app_verl());
2208 cyttsp_debug("blcid=0x%02X%02X%02X flcid=0x%02X%02X%02X\n", \
2209 g_bl_data.cid_0, \
2210 g_bl_data.cid_1, \
2211 g_bl_data.cid_2, \
2212 cyttsp_cid_0(), \
2213 cyttsp_cid_1(), \
2214 cyttsp_cid_2());
2215 /* enter bootloader to load new app into TTSP Device */
2216 retval = cyttsp_bootload_app(ts);
2217 /* take TTSP device out of bootloader mode;
2218 * switch back to TrueTouch operational mode */
2219 if (!(retval < CY_OK)) {
2220 retval = i2c_smbus_write_i2c_block_data(ts->client,
2221 CY_REG_BASE,
2222 sizeof(bl_cmd), bl_cmd);
2223 /* wait for TTSP Device to complete
2224 * switch to Operational mode */
2225 tries = 0;
2226 do {
2227 mdelay(100);
2228 cyttsp_putbl(ts, 9, false, false, false);
2229 } while (GET_BOOTLOADERMODE(g_bl_data.bl_status) &&
2230 tries++ < 100);
2231 cyttsp_putbl(ts, 9, true, false, false);
2232 }
2233 }
2234 }
2235
2236bypass:
2237 /* switch to System Information mode to read versions
2238 * and set interval registers */
2239 if (!(retval < CY_OK)) {
2240 cyttsp_debug("switch to sysinfo mode \n");
2241 host_reg = CY_SYSINFO_MODE;
2242 retval = i2c_smbus_write_i2c_block_data(ts->client,
2243 CY_REG_BASE, sizeof(host_reg), &host_reg);
2244 /* wait for TTSP Device to complete switch to SysInfo mode */
2245 mdelay(100);
2246 if (!(retval < CY_OK)) {
2247 retval = i2c_smbus_read_i2c_block_data(ts->client,
2248 CY_REG_BASE,
2249 sizeof(struct cyttsp_sysinfo_data_t),
2250 (u8 *)&g_sysinfo_data);
2251 cyttsp_debug("SI2: hst_mode=0x%02X mfg_cmd=0x%02X mfg_stat=0x%02X\n", \
2252 g_sysinfo_data.hst_mode, \
2253 g_sysinfo_data.mfg_cmd, \
2254 g_sysinfo_data.mfg_stat);
2255 cyttsp_debug("SI2: bl_ver=0x%02X%02X\n", \
2256 g_sysinfo_data.bl_verh, \
2257 g_sysinfo_data.bl_verl);
2258 cyttsp_debug("SI2: sysinfo act_int=0x%02X tch_tmout=0x%02X lp_int=0x%02X\n", \
2259 g_sysinfo_data.act_intrvl, \
2260 g_sysinfo_data.tch_tmout, \
2261 g_sysinfo_data.lp_intrvl);
2262 cyttsp_info("SI%d: tver=%02X%02X a_id=%02X%02X aver=%02X%02X\n", \
2263 102, \
2264 g_sysinfo_data.tts_verh, \
2265 g_sysinfo_data.tts_verl, \
2266 g_sysinfo_data.app_idh, \
2267 g_sysinfo_data.app_idl, \
2268 g_sysinfo_data.app_verh, \
2269 g_sysinfo_data.app_verl);
2270 cyttsp_info("SI%d: c_id=%02X%02X%02X\n", \
2271 103, \
2272 g_sysinfo_data.cid[0], \
2273 g_sysinfo_data.cid[1], \
2274 g_sysinfo_data.cid[2]);
2275 if (!(retval < CY_OK) &&
2276 (CY_DIFF(ts->platform_data->act_intrvl,
2277 CY_ACT_INTRVL_DFLT) ||
2278 CY_DIFF(ts->platform_data->tch_tmout,
2279 CY_TCH_TMOUT_DFLT) ||
2280 CY_DIFF(ts->platform_data->lp_intrvl,
2281 CY_LP_INTRVL_DFLT))) {
2282 if (!(retval < CY_OK)) {
2283 u8 intrvl_ray[sizeof(ts->platform_data->act_intrvl) +
2284 sizeof(ts->platform_data->tch_tmout) +
2285 sizeof(ts->platform_data->lp_intrvl)];
2286 u8 i = 0;
2287
2288 intrvl_ray[i++] =
2289 ts->platform_data->act_intrvl;
2290 intrvl_ray[i++] =
2291 ts->platform_data->tch_tmout;
2292 intrvl_ray[i++] =
2293 ts->platform_data->lp_intrvl;
2294
2295 cyttsp_debug("SI2: platinfo act_intrvl=0x%02X tch_tmout=0x%02X lp_intrvl=0x%02X\n", \
2296 ts->platform_data->act_intrvl, \
2297 ts->platform_data->tch_tmout, \
2298 ts->platform_data->lp_intrvl);
2299 /* set intrvl registers */
2300 retval = i2c_smbus_write_i2c_block_data(
2301 ts->client,
2302 CY_REG_ACT_INTRVL,
2303 sizeof(intrvl_ray), intrvl_ray);
2304 mdelay(CY_DLY_SYSINFO);
2305 }
2306 }
2307 }
2308 /* switch back to Operational mode */
2309 cyttsp_debug("switch back to operational mode \n");
2310 if (!(retval < CY_OK)) {
2311 host_reg = CY_OP_MODE/* + CY_LOW_PWR_MODE*/;
2312 retval = i2c_smbus_write_i2c_block_data(ts->client,
2313 CY_REG_BASE,
2314 sizeof(host_reg), &host_reg);
2315 /* wait for TTSP Device to complete
2316 * switch to Operational mode */
2317 mdelay(100);
2318 }
2319 }
2320 /* init gesture setup;
2321 * this is required even if not using gestures
2322 * in order to set the active distance */
2323 if (!(retval < CY_OK)) {
2324 u8 gesture_setup;
2325 cyttsp_debug("init gesture setup \n");
2326 gesture_setup = ts->platform_data->gest_set;
2327 retval = i2c_smbus_write_i2c_block_data(ts->client,
2328 CY_REG_GEST_SET,
2329 sizeof(gesture_setup), &gesture_setup);
2330 mdelay(CY_DLY_DFLT);
2331 }
2332
2333 if (!(retval < CY_OK))
2334 ts->platform_data->power_state = CY_ACTIVE_STATE;
2335 else
2336 ts->platform_data->power_state = CY_IDLE_STATE;
2337
2338 cyttsp_debug("Retval=%d Power state is %s\n", \
2339 retval, \
2340 ts->platform_data->power_state == CY_ACTIVE_STATE ? \
2341 "ACTIVE" : "IDLE");
2342
2343 return retval;
2344}
2345
2346static int cyttsp_power_device(struct cyttsp *ts, bool on)
2347{
2348 int rc = 0, i;
2349 const struct cyttsp_regulator *reg_info =
2350 ts->platform_data->regulator_info;
2351 u8 num_reg = ts->platform_data->num_regulators;
2352
2353 if (!reg_info) {
2354 pr_err("regulator pdata not specified\n");
2355 return -EINVAL;
2356 }
2357
2358 if (on == false) /* Turn off the regulators */
2359 goto ts_reg_disable;
2360
2361 ts->vdd = kzalloc(num_reg * sizeof(struct regulator *), GFP_KERNEL);
2362 if (!ts->vdd) {
2363 pr_err("unable to allocate memory\n");
2364 return -ENOMEM;
2365 }
2366
2367 for (i = 0; i < num_reg; i++) {
2368 ts->vdd[i] = regulator_get(&ts->client->dev, reg_info[i].name);
2369 if (IS_ERR(ts->vdd[i])) {
2370 rc = PTR_ERR(ts->vdd[i]);
2371 pr_err("%s:regulator get failed rc=%d\n",
2372 __func__, rc);
2373 goto error_vdd;
2374 }
2375
2376 if (regulator_count_voltages(ts->vdd[i]) > 0) {
2377 rc = regulator_set_voltage(ts->vdd[i],
2378 reg_info[i].min_uV, reg_info[i].max_uV);
2379 if (rc) {
2380 pr_err("%s: regulator_set_voltage"
2381 "failed rc =%d\n", __func__, rc);
2382 regulator_put(ts->vdd[i]);
2383 goto error_vdd;
2384 }
2385 }
2386
2387 rc = regulator_set_optimum_mode(ts->vdd[i],
2388 reg_info[i].load_uA);
2389 if (rc < 0) {
2390 pr_err("%s: regulator_set_optimum_mode failed rc=%d\n",
2391 __func__, rc);
2392
2393 regulator_set_voltage(ts->vdd[i], 0,
2394 reg_info[i].max_uV);
2395 regulator_put(ts->vdd[i]);
2396 goto error_vdd;
2397 }
2398
2399 rc = regulator_enable(ts->vdd[i]);
2400 if (rc) {
2401 pr_err("%s: regulator_enable failed rc =%d\n",
2402 __func__, rc);
2403 regulator_set_optimum_mode(ts->vdd[i], 0);
2404 regulator_set_voltage(ts->vdd[i], 0,
2405 reg_info[i].max_uV);
2406 regulator_put(ts->vdd[i]);
2407 goto error_vdd;
2408 }
2409 }
2410
2411 return rc;
2412
2413ts_reg_disable:
2414 i = ts->platform_data->num_regulators;
2415error_vdd:
2416 while (--i >= 0) {
2417 if (regulator_count_voltages(ts->vdd[i]) > 0)
2418 regulator_set_voltage(ts->vdd[i], 0,
2419 reg_info[i].max_uV);
2420 regulator_set_optimum_mode(ts->vdd[i], 0);
2421 regulator_disable(ts->vdd[i]);
2422 regulator_put(ts->vdd[i]);
2423 }
2424 kfree(ts->vdd);
2425 return rc;
2426}
2427
2428/* cyttsp_initialize: Driver Initialization. This function takes
2429 * care of the following tasks:
2430 * 1. Create and register an input device with input layer
2431 * 2. Take CYTTSP device out of bootloader mode; go operational
2432 * 3. Start any timers/Work queues. */
2433static int cyttsp_initialize(struct i2c_client *client, struct cyttsp *ts)
2434{
2435 struct input_dev *input_device;
2436 int error = 0;
2437 int retval = CY_OK;
2438 u8 id;
2439
2440 /* Create the input device and register it. */
2441 input_device = input_allocate_device();
2442 if (!input_device) {
2443 error = -ENOMEM;
2444 cyttsp_xdebug1("err input allocate device\n");
2445 goto error_free_device;
2446 }
2447
2448 if (!client) {
2449 error = ~ENODEV;
2450 cyttsp_xdebug1("err client is Null\n");
2451 goto error_free_device;
2452 }
2453
2454 if (!ts) {
2455 error = ~ENODEV;
2456 cyttsp_xdebug1("err context is Null\n");
2457 goto error_free_device;
2458 }
2459
2460 ts->input = input_device;
2461 input_device->name = CY_I2C_NAME;
2462 input_device->phys = ts->phys;
2463 input_device->dev.parent = &client->dev;
2464
2465 /* init the touch structures */
2466 ts->num_prv_st_tch = CY_NTCH;
2467 for (id = 0; id < CY_NUM_TRK_ID; id++) {
2468 ts->act_trk[id] = CY_NTCH;
2469 ts->prv_mt_pos[id][CY_XPOS] = 0;
2470 ts->prv_mt_pos[id][CY_YPOS] = 0;
2471 }
2472
2473 for (id = 0; id < CY_NUM_MT_TCH_ID; id++)
2474 ts->prv_mt_tch[id] = CY_IGNR_TCH;
2475
2476 for (id = 0; id < CY_NUM_ST_TCH_ID; id++)
2477 ts->prv_st_tch[id] = CY_IGNR_TCH;
2478
2479 set_bit(EV_SYN, input_device->evbit);
2480 set_bit(EV_KEY, input_device->evbit);
2481 set_bit(EV_ABS, input_device->evbit);
2482 set_bit(BTN_TOUCH, input_device->keybit);
2483 set_bit(BTN_2, input_device->keybit);
2484 if (ts->platform_data->use_gestures)
2485 set_bit(BTN_3, input_device->keybit);
2486
2487 input_set_abs_params(input_device, ABS_X, ts->platform_data->disp_minx,
2488 ts->platform_data->disp_maxx, 0, 0);
2489 input_set_abs_params(input_device, ABS_Y, ts->platform_data->disp_miny,
2490 ts->platform_data->disp_maxy, 0, 0);
2491 input_set_abs_params(input_device,
2492 ABS_TOOL_WIDTH, 0, CY_LARGE_TOOL_WIDTH, 0 , 0);
2493 input_set_abs_params(input_device,
2494 ABS_PRESSURE, 0, CY_MAXZ, 0, 0);
2495 input_set_abs_params(input_device,
2496 ABS_HAT0X, 0, ts->platform_data->panel_maxx, 0, 0);
2497 input_set_abs_params(input_device,
2498 ABS_HAT0Y, 0, ts->platform_data->panel_maxy, 0, 0);
2499 if (ts->platform_data->use_gestures) {
2500 input_set_abs_params(input_device,
2501 ABS_HAT1X, 0, CY_MAXZ, 0, 0);
2502 input_set_abs_params(input_device,
2503 ABS_HAT1Y, 0, CY_MAXZ, 0, 0);
2504 }
2505 if (ts->platform_data->use_mt) {
2506 input_set_abs_params(input_device, ABS_MT_POSITION_X,
2507 ts->platform_data->disp_minx,
2508 ts->platform_data->disp_maxx, 0, 0);
2509 input_set_abs_params(input_device, ABS_MT_POSITION_Y,
2510 ts->platform_data->disp_miny,
2511 ts->platform_data->disp_maxy, 0, 0);
2512 input_set_abs_params(input_device,
2513 ABS_MT_TOUCH_MAJOR, 0, CY_MAXZ, 0, 0);
2514 input_set_abs_params(input_device,
2515 ABS_MT_WIDTH_MAJOR, 0, CY_LARGE_TOOL_WIDTH, 0, 0);
2516 if (ts->platform_data->use_trk_id) {
2517 input_set_abs_params(input_device,
2518 ABS_MT_TRACKING_ID, 0, CY_NUM_TRK_ID, 0, 0);
2519 }
2520 }
2521
2522 /* set dummy key to make driver work with virtual keys */
2523 input_set_capability(input_device, EV_KEY, KEY_PROG1);
2524
2525 cyttsp_info("%s: Register input device\n", CY_I2C_NAME);
2526 error = input_register_device(input_device);
2527 if (error) {
2528 cyttsp_alert("%s: Failed to register input device\n", \
2529 CY_I2C_NAME);
2530 retval = error;
2531 goto error_free_device;
2532 }
2533
2534 /* Prepare our worker structure prior to setting up the timer/ISR */
2535 INIT_WORK(&ts->work, cyttsp_xy_worker);
2536
2537 if (gpio_is_valid(ts->platform_data->resout_gpio)) {
2538 /* configure touchscreen reset out gpio */
2539 retval = gpio_request(ts->platform_data->resout_gpio,
2540 "cyttsp_resout_gpio");
2541 if (retval) {
2542 pr_err("%s: unable to request reset gpio %d\n",
2543 __func__, ts->platform_data->resout_gpio);
2544 goto error_free_device;
2545 }
2546
2547 retval = gpio_direction_output(
2548 ts->platform_data->resout_gpio, 1);
2549 if (retval) {
2550 pr_err("%s: unable to set direction for gpio %d\n",
2551 __func__, ts->platform_data->resout_gpio);
2552 goto error_resout_gpio_dir;
2553 }
2554 }
2555
2556 if (gpio_is_valid(ts->platform_data->sleep_gpio)) {
2557 /* configure touchscreen reset out gpio */
2558 retval = gpio_request(ts->platform_data->sleep_gpio,
2559 "cy8c_sleep_gpio");
2560 if (retval) {
2561 pr_err("%s: unable to request sleep gpio %d\n",
2562 __func__, ts->platform_data->sleep_gpio);
2563 goto error_sleep_gpio_req;
2564 }
2565
2566 retval = gpio_direction_output(
2567 ts->platform_data->sleep_gpio, 0);
2568 if (retval) {
2569 pr_err("%s: unable to set direction for gpio %d\n",
2570 __func__, ts->platform_data->resout_gpio);
2571 goto error_sleep_gpio_dir;
2572 }
2573 }
2574
2575 if (gpio_is_valid(ts->platform_data->irq_gpio)) {
2576 /* configure touchscreen irq gpio */
2577 retval = gpio_request(ts->platform_data->irq_gpio,
2578 "ts_irq_gpio");
2579 if (retval) {
2580 pr_err("%s: unable to request gpio [%d]\n", __func__,
2581 ts->platform_data->irq_gpio);
2582 goto error_irq_gpio_req;
2583 }
2584 retval = gpio_direction_input(ts->platform_data->irq_gpio);
2585 if (retval) {
2586 pr_err("%s: unable to set_direction for gpio [%d]\n",
2587 __func__, ts->platform_data->irq_gpio);
2588 goto error_irq_gpio_dir;
2589 }
2590 }
2591
2592 if (ts->platform_data->regulator_info) {
2593 retval = cyttsp_power_device(ts, true);
2594 if (retval) {
2595 pr_err("%s: Unable to power device %d\n",
2596 __func__, retval);
2597 goto error_irq_gpio_dir;
2598 }
2599 }
2600
2601 /* Power on the chip and make sure that I/Os are set as specified
2602 * in the platform */
2603 if (ts->platform_data->init) {
2604 retval = ts->platform_data->init(client);
2605 if (retval) {
2606 pr_err("%s: ts init failed\n", __func__);
2607 goto error_power_device;
2608 }
2609 }
2610
2611 msleep(100);
2612
2613 /* check this device active by reading first byte/register */
2614 retval = i2c_smbus_read_byte_data(ts->client, 0x01);
2615 if (retval < 0) {
2616 pr_err("%s: i2c sanity check failed\n", __func__);
2617 goto error_power_device;
2618 }
2619
2620 retval = cyttsp_power_on(ts);
2621 if (retval < 0) {
2622 pr_err("%s: cyttsp_power_on failed\n", __func__);
2623 goto error_power_device;
2624 }
2625
2626 /* Timer or Interrupt setup */
2627 if (ts->client->irq == 0) {
2628 cyttsp_info("Setting up timer\n");
2629 setup_timer(&ts->timer, cyttsp_timer, (unsigned long) ts);
2630 mod_timer(&ts->timer, jiffies + TOUCHSCREEN_TIMEOUT);
2631 } else {
2632 cyttsp_info("Setting up interrupt\n");
2633 /* request_irq() will also call enable_irq() */
2634 error = request_irq(client->irq, cyttsp_irq,
2635 IRQF_TRIGGER_FALLING,
2636 client->dev.driver->name, ts);
2637 if (error) {
2638 cyttsp_alert("error: could not request irq\n");
2639 retval = error;
2640 goto error_power_device;
2641 }
2642 }
2643
2644 irq_cnt = 0;
2645 irq_cnt_total = 0;
2646 irq_err_cnt = 0;
2647
2648 atomic_set(&ts->irq_enabled, 1);
2649 retval = device_create_file(&ts->client->dev, &dev_attr_irq_enable);
2650 if (retval < CY_OK) {
2651 cyttsp_alert("File device creation failed: %d\n", retval);
2652 retval = -ENODEV;
2653 goto error_free_irq;
2654 }
2655
2656 retval = device_create_file(&client->dev, &dev_attr_cyttsp_fw_ver);
2657 if (retval) {
2658 cyttsp_alert("sysfs entry for firmware version failed\n");
2659 goto error_rm_dev_file_irq_en;
2660 }
2661
2662 ts->cyttsp_fwloader_mode = 0;
2663 retval = device_create_file(&client->dev, &dev_attr_cyttsp_update_fw);
2664 if (retval) {
2665 cyttsp_alert("sysfs entry for firmware update failed\n");
2666 goto error_rm_dev_file_fw_ver;
2667 }
2668
2669 retval = device_create_file(&client->dev,
2670 &dev_attr_cyttsp_force_update_fw);
2671 if (retval) {
2672 cyttsp_alert("sysfs entry for force firmware update failed\n");
2673 goto error_rm_dev_file_update_fw;
2674 }
2675 if (ts->platform_data->correct_fw_ver) {
2676 if (g_bl_data.appid_lo != ts->platform_data->correct_fw_ver)
2677 printk(KERN_INFO "Please update touchscreen firmware\n");
2678 }
2679
2680 retval = device_create_file(&client->dev,
2681 &dev_attr_cyttsp_fw_name);
2682 if (retval) {
2683 cyttsp_alert("sysfs entry for file name selection failed\n");
2684 goto error_rm_dev_file_fupdate_fw;
2685 }
2686
2687 cyttsp_info("%s: Successful registration\n", CY_I2C_NAME);
2688
2689 goto success;
2690
2691error_rm_dev_file_fupdate_fw:
2692 device_remove_file(&client->dev, &dev_attr_cyttsp_force_update_fw);
2693error_rm_dev_file_update_fw:
2694 device_remove_file(&client->dev, &dev_attr_cyttsp_update_fw);
2695error_rm_dev_file_fw_ver:
2696 device_remove_file(&client->dev, &dev_attr_cyttsp_fw_ver);
2697error_rm_dev_file_irq_en:
2698 device_remove_file(&client->dev, &dev_attr_irq_enable);
2699error_free_irq:
2700 if (ts->client->irq)
2701 free_irq(client->irq, ts);
2702error_power_device:
2703 if (ts->platform_data->regulator_info)
2704 cyttsp_power_device(ts, false);
2705error_irq_gpio_dir:
2706 if (gpio_is_valid(ts->platform_data->irq_gpio))
2707 gpio_free(ts->platform_data->irq_gpio);
2708error_irq_gpio_req:
2709 if (gpio_is_valid(ts->platform_data->sleep_gpio))
2710 gpio_direction_output(ts->platform_data->sleep_gpio, 1);
2711error_sleep_gpio_dir:
2712 if (gpio_is_valid(ts->platform_data->sleep_gpio))
2713 gpio_free(ts->platform_data->sleep_gpio);
2714error_sleep_gpio_req:
2715 if (gpio_is_valid(ts->platform_data->resout_gpio))
2716 gpio_direction_output(ts->platform_data->resout_gpio, 0);
2717error_resout_gpio_dir:
2718 if (gpio_is_valid(ts->platform_data->resout_gpio))
2719 gpio_free(ts->platform_data->resout_gpio);
2720error_free_device:
2721 if (input_device)
2722 input_free_device(input_device);
2723
2724success:
2725 return retval;
2726}
2727
2728/* I2C driver probe function */
2729static int __devinit cyttsp_probe(struct i2c_client *client,
2730 const struct i2c_device_id *id)
2731{
2732 struct cyttsp *ts;
2733 int error;
2734 int retval = CY_OK;
2735
2736 cyttsp_info("Start Probe 1.2\n");
2737
2738 /* allocate and clear memory */
2739 ts = kzalloc(sizeof(struct cyttsp), GFP_KERNEL);
2740 if (ts == NULL) {
2741 cyttsp_xdebug1("err kzalloc for cyttsp\n");
2742 retval = -ENOMEM;
2743 }
2744
2745 /* Enable runtime PM ops, start in ACTIVE mode */
2746 error = pm_runtime_set_active(&client->dev);
2747 if (error < 0)
2748 dev_dbg(&client->dev, "unable to set runtime pm state\n");
2749 pm_runtime_enable(&client->dev);
2750
2751 if (!(retval < CY_OK)) {
2752 /* register driver_data */
2753 ts->client = client;
2754 ts->platform_data = client->dev.platform_data;
2755
2756 if (ts->platform_data->fw_fname)
2757 strncpy(ts->fw_fname, ts->platform_data->fw_fname,
2758 FW_FNAME_LEN - 1);
2759 else
2760 strncpy(ts->fw_fname, "cyttsp.hex", FW_FNAME_LEN - 1);
2761
Amy Malocheedd5fd72011-06-22 18:50:21 -07002762 if (ts->platform_data->gen == CY_GEN3) {
2763 ts->fw_start_addr = 0x0b00;
2764 } else if (ts->platform_data->gen == CY_GEN2) {
2765 ts->fw_start_addr = 0x0880;
2766 } else {
2767 pr_err("%s: unsupported cypress chip\n", __func__);
2768 kfree(ts);
2769 return -EINVAL;
2770 }
2771
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002772 i2c_set_clientdata(client, ts);
2773
2774 error = cyttsp_initialize(client, ts);
2775 if (error) {
2776 cyttsp_xdebug1("err cyttsp_initialize\n");
2777 if (ts != NULL) {
2778 /* deallocate memory */
2779 kfree(ts);
2780 }
2781/*
2782 i2c_del_driver(&cyttsp_driver);
2783*/
2784 retval = -ENODEV;
2785 } else
2786 cyttsp_openlog();
2787 }
2788
2789#ifdef CONFIG_HAS_EARLYSUSPEND
2790 if (!(retval < CY_OK)) {
2791 ts->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1;
2792 ts->early_suspend.suspend = cyttsp_early_suspend;
2793 ts->early_suspend.resume = cyttsp_late_resume;
2794 register_early_suspend(&ts->early_suspend);
2795 }
2796#endif /* CONFIG_HAS_EARLYSUSPEND */
2797 device_init_wakeup(&client->dev, ts->platform_data->wakeup);
2798 mutex_init(&ts->mutex);
2799
2800 cyttsp_info("Start Probe %s\n", \
2801 (retval < CY_OK) ? "FAIL" : "PASS");
2802
2803 return retval;
2804}
2805
2806/* Function to manage power-on resume */
2807static int cyttsp_resume(struct device *dev)
2808{
2809 struct cyttsp *ts = dev_get_drvdata(dev);
2810 int retval = CY_OK;
2811
2812 cyttsp_debug("Wake Up\n");
2813
2814 if (ts->is_suspended == false) {
2815 pr_err("%s: in wakeup state\n", __func__);
2816 return 0;
2817 }
2818
2819 if (device_may_wakeup(dev)) {
2820 if (ts->client->irq)
2821 disable_irq_wake(ts->client->irq);
2822 return 0;
2823 }
2824
2825 /* re-enable the interrupt prior to wake device */
2826 if (ts->client->irq)
2827 enable_irq(ts->client->irq);
2828
2829 if (ts->platform_data->use_sleep &&
2830 (ts->platform_data->power_state != CY_ACTIVE_STATE)) {
2831 if (ts->platform_data->resume)
2832 retval = ts->platform_data->resume(ts->client);
2833 if (!(retval < CY_OK)) {
2834 /* take TTSP device out of bootloader mode;
2835 * switch back to TrueTouch operational mode */
2836 if (!(retval < CY_OK)) {
2837 int tries;
2838 retval = i2c_smbus_write_i2c_block_data(ts->client,
2839 CY_REG_BASE,
2840 sizeof(bl_cmd), bl_cmd);
2841 /* wait for TTSP Device to complete
2842 * switch to Operational mode */
2843 tries = 0;
2844 do {
2845 mdelay(100);
2846 cyttsp_putbl(ts, 16, false, false, false);
2847 } while (GET_BOOTLOADERMODE(g_bl_data.bl_status) &&
2848 tries++ < 100);
2849 cyttsp_putbl(ts, 16, true, false, false);
2850 }
2851 }
2852 }
2853
2854 if (!(retval < CY_OK) &&
2855 (GET_HSTMODE(g_bl_data.bl_file) == CY_OK)) {
2856 ts->platform_data->power_state = CY_ACTIVE_STATE;
2857
2858 /* re-enable the timer after resuming */
2859 if (ts->client->irq == 0)
2860 mod_timer(&ts->timer, jiffies + TOUCHSCREEN_TIMEOUT);
2861 } else
2862 retval = -ENODEV;
2863
2864 ts->is_suspended = false;
2865 cyttsp_debug("Wake Up %s\n", \
2866 (retval < CY_OK) ? "FAIL" : "PASS");
2867
2868 return retval;
2869}
2870
2871
2872/* Function to manage low power suspend */
2873static int cyttsp_suspend(struct device *dev)
2874{
2875 struct cyttsp *ts = dev_get_drvdata(dev);
2876 u8 sleep_mode = CY_OK;
2877 int retval = CY_OK;
2878
2879 cyttsp_debug("Enter Sleep\n");
2880
2881 if (ts->is_suspended == true) {
2882 pr_err("%s: in sleep state\n", __func__);
2883 return 0;
2884 }
2885
2886 mutex_lock(&ts->mutex);
2887 if (ts->cyttsp_fwloader_mode) {
2888 pr_err("%s:firmware upgrade mode:"
2889 "suspend not allowed\n", __func__);
2890 mutex_unlock(&ts->mutex);
2891 return -EBUSY;
2892 }
2893 mutex_unlock(&ts->mutex);
2894
2895 if (device_may_wakeup(dev)) {
2896 if (ts->client->irq)
2897 enable_irq_wake(ts->client->irq);
2898 return 0;
2899 }
2900
2901 /* disable worker */
2902 if (ts->client->irq == 0)
2903 del_timer(&ts->timer);
2904 else
2905 disable_irq_nosync(ts->client->irq);
2906 retval = cancel_work_sync(&ts->work);
2907
2908 if (retval)
2909 enable_irq(ts->client->irq);
2910
2911 if (!(retval < CY_OK)) {
2912 if (ts->platform_data->use_sleep &&
2913 (ts->platform_data->power_state == CY_ACTIVE_STATE)) {
2914 if (ts->platform_data->use_sleep & CY_USE_DEEP_SLEEP_SEL)
2915 sleep_mode = CY_DEEP_SLEEP_MODE;
2916 else
2917 sleep_mode = CY_LOW_PWR_MODE;
2918
2919 retval = i2c_smbus_write_i2c_block_data(ts->client,
2920 CY_REG_BASE,
2921 sizeof(sleep_mode), &sleep_mode);
2922 }
2923 }
2924
2925 if (!(retval < CY_OK)) {
2926 if (sleep_mode == CY_DEEP_SLEEP_MODE)
2927 ts->platform_data->power_state = CY_SLEEP_STATE;
2928 else if (sleep_mode == CY_LOW_PWR_MODE)
2929 ts->platform_data->power_state = CY_LOW_PWR_STATE;
2930 }
2931
2932 ts->is_suspended = true;
2933 cyttsp_debug("Sleep Power state is %s\n", \
2934 (ts->platform_data->power_state == CY_ACTIVE_STATE) ? \
2935 "ACTIVE" : \
2936 ((ts->platform_data->power_state == CY_SLEEP_STATE) ? \
2937 "SLEEP" : "LOW POWER"));
2938
2939 return retval;
2940}
2941
2942/* registered in driver struct */
2943static int __devexit cyttsp_remove(struct i2c_client *client)
2944{
2945 /* clientdata registered on probe */
2946 struct cyttsp *ts = i2c_get_clientdata(client);
2947 int err;
2948
2949 cyttsp_alert("Unregister\n");
2950
2951 pm_runtime_set_suspended(&client->dev);
2952 pm_runtime_disable(&client->dev);
2953
2954 device_init_wakeup(&client->dev, 0);
2955 device_remove_file(&ts->client->dev, &dev_attr_irq_enable);
2956 device_remove_file(&client->dev, &dev_attr_cyttsp_fw_ver);
2957 device_remove_file(&client->dev, &dev_attr_cyttsp_update_fw);
2958 device_remove_file(&client->dev, &dev_attr_cyttsp_force_update_fw);
2959 device_remove_file(&client->dev, &dev_attr_cyttsp_fw_name);
2960
2961 /* Start cleaning up by removing any delayed work and the timer */
2962 if (cancel_delayed_work((struct delayed_work *)&ts->work) < CY_OK)
2963 cyttsp_alert("error: could not remove work from workqueue\n");
2964
2965 /* free up timer or irq */
2966 if (ts->client->irq == 0) {
2967 err = del_timer(&ts->timer);
2968 if (err < CY_OK)
2969 cyttsp_alert("error: failed to delete timer\n");
2970 } else
2971 free_irq(client->irq, ts);
2972
2973 if (ts->platform_data->regulator_info)
2974 cyttsp_power_device(ts, false);
2975
2976#ifdef CONFIG_HAS_EARLYSUSPEND
2977 unregister_early_suspend(&ts->early_suspend);
2978#endif /* CONFIG_HAS_EARLYSUSPEND */
2979
2980 mutex_destroy(&ts->mutex);
2981
2982 if (gpio_is_valid(ts->platform_data->sleep_gpio)) {
2983 gpio_direction_output(ts->platform_data->sleep_gpio, 1);
2984 gpio_free(ts->platform_data->sleep_gpio);
2985 }
2986
2987 if (gpio_is_valid(ts->platform_data->resout_gpio)) {
2988 gpio_direction_output(ts->platform_data->resout_gpio, 0);
2989 gpio_free(ts->platform_data->resout_gpio);
2990 }
2991
2992 if (gpio_is_valid(ts->platform_data->irq_gpio))
2993 gpio_free(ts->platform_data->irq_gpio);
2994
2995 /* housekeeping */
2996 if (ts != NULL)
2997 kfree(ts);
2998
2999 cyttsp_alert("Leaving\n");
3000
3001 return 0;
3002}
3003
3004#ifdef CONFIG_HAS_EARLYSUSPEND
3005static void cyttsp_early_suspend(struct early_suspend *handler)
3006{
3007 struct cyttsp *ts;
3008
3009 ts = container_of(handler, struct cyttsp, early_suspend);
3010 cyttsp_suspend(&ts->client->dev);
3011}
3012
3013static void cyttsp_late_resume(struct early_suspend *handler)
3014{
3015 struct cyttsp *ts;
3016
3017 ts = container_of(handler, struct cyttsp, early_suspend);
3018 cyttsp_resume(&ts->client->dev);
3019}
3020#endif /* CONFIG_HAS_EARLYSUSPEND */
3021
3022static int cyttsp_init(void)
3023{
3024 int ret;
3025
3026 cyttsp_info("Cypress TrueTouch(R) Standard Product\n");
3027 cyttsp_info("I2C Touchscreen Driver (Built %s @ %s)\n", \
3028 __DATE__, __TIME__);
3029
3030 cyttsp_ts_wq = create_singlethread_workqueue("cyttsp_ts_wq");
3031 if (cyttsp_ts_wq == NULL) {
3032 cyttsp_debug("No memory for cyttsp_ts_wq\n");
3033 return -ENOMEM;
3034 }
3035
3036 ret = i2c_add_driver(&cyttsp_driver);
3037
3038 return ret;
3039}
3040
3041static void cyttsp_exit(void)
3042{
3043 if (cyttsp_ts_wq)
3044 destroy_workqueue(cyttsp_ts_wq);
3045 return i2c_del_driver(&cyttsp_driver);
3046}
3047
3048module_init(cyttsp_init);
3049module_exit(cyttsp_exit);
3050MODULE_FIRMWARE("cyttsp.fw");
3051