Linux-libre 3.10.48-gnu
[librecmc/linux-libre.git] / drivers / input / touchscreen / cyttsp_core.c
1 /*
2  * Core Source for:
3  * Cypress TrueTouch(TM) Standard Product (TTSP) touchscreen drivers.
4  * For use with Cypress Txx3xx parts.
5  * Supported parts include:
6  * CY8CTST341
7  * CY8CTMA340
8  *
9  * Copyright (C) 2009, 2010, 2011 Cypress Semiconductor, Inc.
10  * Copyright (C) 2012 Javier Martinez Canillas <javier@dowhile0.org>
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * version 2, and only version 2, as published by the
15  * Free Software Foundation.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License along
23  * with this program; if not, write to the Free Software Foundation, Inc.,
24  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25  *
26  * Contact Cypress Semiconductor at www.cypress.com <kev@cypress.com>
27  *
28  */
29
30 #include <linux/delay.h>
31 #include <linux/input.h>
32 #include <linux/input/mt.h>
33 #include <linux/gpio.h>
34 #include <linux/interrupt.h>
35 #include <linux/slab.h>
36
37 #include "cyttsp_core.h"
38
39 /* Bootloader number of command keys */
40 #define CY_NUM_BL_KEYS          8
41
42 /* helpers */
43 #define GET_NUM_TOUCHES(x)              ((x) & 0x0F)
44 #define IS_LARGE_AREA(x)                (((x) & 0x10) >> 4)
45 #define IS_BAD_PKT(x)                   ((x) & 0x20)
46 #define IS_VALID_APP(x)                 ((x) & 0x01)
47 #define IS_OPERATIONAL_ERR(x)           ((x) & 0x3F)
48 #define GET_HSTMODE(reg)                (((reg) & 0x70) >> 4)
49 #define GET_BOOTLOADERMODE(reg)         (((reg) & 0x10) >> 4)
50
51 #define CY_REG_BASE                     0x00
52 #define CY_REG_ACT_DIST                 0x1E
53 #define CY_REG_ACT_INTRVL               0x1D
54 #define CY_REG_TCH_TMOUT                (CY_REG_ACT_INTRVL + 1)
55 #define CY_REG_LP_INTRVL                (CY_REG_TCH_TMOUT + 1)
56 #define CY_MAXZ                         255
57 #define CY_DELAY_DFLT                   20 /* ms */
58 #define CY_DELAY_MAX                    500
59 #define CY_ACT_DIST_DFLT                0xF8
60 #define CY_HNDSHK_BIT                   0x80
61 /* device mode bits */
62 #define CY_OPERATE_MODE                 0x00
63 #define CY_SYSINFO_MODE                 0x10
64 /* power mode select bits */
65 #define CY_SOFT_RESET_MODE              0x01 /* return to Bootloader mode */
66 #define CY_DEEP_SLEEP_MODE              0x02
67 #define CY_LOW_POWER_MODE               0x04
68
69 /* Slots management */
70 #define CY_MAX_FINGER                   4
71 #define CY_MAX_ID                       16
72
73 static const u8 bl_command[] = {
74         0x00,                   /* file offset */
75         0xFF,                   /* command */
76         0xA5,                   /* exit bootloader command */
77         0, 1, 2, 3, 4, 5, 6, 7  /* default keys */
78 };
79
80 static int ttsp_read_block_data(struct cyttsp *ts, u8 command,
81                                 u8 length, void *buf)
82 {
83         int error;
84         int tries;
85
86         for (tries = 0; tries < CY_NUM_RETRY; tries++) {
87                 error = ts->bus_ops->read(ts, command, length, buf);
88                 if (!error)
89                         return 0;
90
91                 msleep(CY_DELAY_DFLT);
92         }
93
94         return -EIO;
95 }
96
97 static int ttsp_write_block_data(struct cyttsp *ts, u8 command,
98                                  u8 length, void *buf)
99 {
100         int error;
101         int tries;
102
103         for (tries = 0; tries < CY_NUM_RETRY; tries++) {
104                 error = ts->bus_ops->write(ts, command, length, buf);
105                 if (!error)
106                         return 0;
107
108                 msleep(CY_DELAY_DFLT);
109         }
110
111         return -EIO;
112 }
113
114 static int ttsp_send_command(struct cyttsp *ts, u8 cmd)
115 {
116         return ttsp_write_block_data(ts, CY_REG_BASE, sizeof(cmd), &cmd);
117 }
118
119 static int cyttsp_handshake(struct cyttsp *ts)
120 {
121         if (ts->pdata->use_hndshk)
122                 return ttsp_send_command(ts,
123                                 ts->xy_data.hst_mode ^ CY_HNDSHK_BIT);
124
125         return 0;
126 }
127
128 static int cyttsp_load_bl_regs(struct cyttsp *ts)
129 {
130         memset(&ts->bl_data, 0, sizeof(ts->bl_data));
131         ts->bl_data.bl_status = 0x10;
132
133         return ttsp_read_block_data(ts, CY_REG_BASE,
134                                     sizeof(ts->bl_data), &ts->bl_data);
135 }
136
137 static int cyttsp_exit_bl_mode(struct cyttsp *ts)
138 {
139         int error;
140         u8 bl_cmd[sizeof(bl_command)];
141
142         memcpy(bl_cmd, bl_command, sizeof(bl_command));
143         if (ts->pdata->bl_keys)
144                 memcpy(&bl_cmd[sizeof(bl_command) - CY_NUM_BL_KEYS],
145                         ts->pdata->bl_keys, CY_NUM_BL_KEYS);
146
147         error = ttsp_write_block_data(ts, CY_REG_BASE,
148                                       sizeof(bl_cmd), bl_cmd);
149         if (error)
150                 return error;
151
152         /* wait for TTSP Device to complete the operation */
153         msleep(CY_DELAY_DFLT);
154
155         error = cyttsp_load_bl_regs(ts);
156         if (error)
157                 return error;
158
159         if (GET_BOOTLOADERMODE(ts->bl_data.bl_status))
160                 return -EIO;
161
162         return 0;
163 }
164
165 static int cyttsp_set_operational_mode(struct cyttsp *ts)
166 {
167         int error;
168
169         error = ttsp_send_command(ts, CY_OPERATE_MODE);
170         if (error)
171                 return error;
172
173         /* wait for TTSP Device to complete switch to Operational mode */
174         error = ttsp_read_block_data(ts, CY_REG_BASE,
175                                      sizeof(ts->xy_data), &ts->xy_data);
176         if (error)
177                 return error;
178
179         error = cyttsp_handshake(ts);
180         if (error)
181                 return error;
182
183         return ts->xy_data.act_dist == CY_ACT_DIST_DFLT ? -EIO : 0;
184 }
185
186 static int cyttsp_set_sysinfo_mode(struct cyttsp *ts)
187 {
188         int error;
189
190         memset(&ts->sysinfo_data, 0, sizeof(ts->sysinfo_data));
191
192         /* switch to sysinfo mode */
193         error = ttsp_send_command(ts, CY_SYSINFO_MODE);
194         if (error)
195                 return error;
196
197         /* read sysinfo registers */
198         msleep(CY_DELAY_DFLT);
199         error = ttsp_read_block_data(ts, CY_REG_BASE, sizeof(ts->sysinfo_data),
200                                       &ts->sysinfo_data);
201         if (error)
202                 return error;
203
204         error = cyttsp_handshake(ts);
205         if (error)
206                 return error;
207
208         if (!ts->sysinfo_data.tts_verh && !ts->sysinfo_data.tts_verl)
209                 return -EIO;
210
211         return 0;
212 }
213
214 static int cyttsp_set_sysinfo_regs(struct cyttsp *ts)
215 {
216         int retval = 0;
217
218         if (ts->pdata->act_intrvl != CY_ACT_INTRVL_DFLT ||
219             ts->pdata->tch_tmout != CY_TCH_TMOUT_DFLT ||
220             ts->pdata->lp_intrvl != CY_LP_INTRVL_DFLT) {
221
222                 u8 intrvl_ray[] = {
223                         ts->pdata->act_intrvl,
224                         ts->pdata->tch_tmout,
225                         ts->pdata->lp_intrvl
226                 };
227
228                 /* set intrvl registers */
229                 retval = ttsp_write_block_data(ts, CY_REG_ACT_INTRVL,
230                                         sizeof(intrvl_ray), intrvl_ray);
231                 msleep(CY_DELAY_DFLT);
232         }
233
234         return retval;
235 }
236
237 static int cyttsp_soft_reset(struct cyttsp *ts)
238 {
239         unsigned long timeout;
240         int retval;
241
242         /* wait for interrupt to set ready completion */
243         INIT_COMPLETION(ts->bl_ready);
244         ts->state = CY_BL_STATE;
245
246         enable_irq(ts->irq);
247
248         retval = ttsp_send_command(ts, CY_SOFT_RESET_MODE);
249         if (retval)
250                 goto out;
251
252         timeout = wait_for_completion_timeout(&ts->bl_ready,
253                         msecs_to_jiffies(CY_DELAY_DFLT * CY_DELAY_MAX));
254         retval = timeout ? 0 : -EIO;
255
256 out:
257         ts->state = CY_IDLE_STATE;
258         disable_irq(ts->irq);
259         return retval;
260 }
261
262 static int cyttsp_act_dist_setup(struct cyttsp *ts)
263 {
264         u8 act_dist_setup = ts->pdata->act_dist;
265
266         /* Init gesture; active distance setup */
267         return ttsp_write_block_data(ts, CY_REG_ACT_DIST,
268                                 sizeof(act_dist_setup), &act_dist_setup);
269 }
270
271 static void cyttsp_extract_track_ids(struct cyttsp_xydata *xy_data, int *ids)
272 {
273         ids[0] = xy_data->touch12_id >> 4;
274         ids[1] = xy_data->touch12_id & 0xF;
275         ids[2] = xy_data->touch34_id >> 4;
276         ids[3] = xy_data->touch34_id & 0xF;
277 }
278
279 static const struct cyttsp_tch *cyttsp_get_tch(struct cyttsp_xydata *xy_data,
280                                                int idx)
281 {
282         switch (idx) {
283         case 0:
284                 return &xy_data->tch1;
285         case 1:
286                 return &xy_data->tch2;
287         case 2:
288                 return &xy_data->tch3;
289         case 3:
290                 return &xy_data->tch4;
291         default:
292                 return NULL;
293         }
294 }
295
296 static void cyttsp_report_tchdata(struct cyttsp *ts)
297 {
298         struct cyttsp_xydata *xy_data = &ts->xy_data;
299         struct input_dev *input = ts->input;
300         int num_tch = GET_NUM_TOUCHES(xy_data->tt_stat);
301         const struct cyttsp_tch *tch;
302         int ids[CY_MAX_ID];
303         int i;
304         DECLARE_BITMAP(used, CY_MAX_ID);
305
306         if (IS_LARGE_AREA(xy_data->tt_stat) == 1) {
307                 /* terminate all active tracks */
308                 num_tch = 0;
309                 dev_dbg(ts->dev, "%s: Large area detected\n", __func__);
310         } else if (num_tch > CY_MAX_FINGER) {
311                 /* terminate all active tracks */
312                 num_tch = 0;
313                 dev_dbg(ts->dev, "%s: Num touch error detected\n", __func__);
314         } else if (IS_BAD_PKT(xy_data->tt_mode)) {
315                 /* terminate all active tracks */
316                 num_tch = 0;
317                 dev_dbg(ts->dev, "%s: Invalid buffer detected\n", __func__);
318         }
319
320         cyttsp_extract_track_ids(xy_data, ids);
321
322         bitmap_zero(used, CY_MAX_ID);
323
324         for (i = 0; i < num_tch; i++) {
325                 tch = cyttsp_get_tch(xy_data, i);
326
327                 input_mt_slot(input, ids[i]);
328                 input_mt_report_slot_state(input, MT_TOOL_FINGER, true);
329                 input_report_abs(input, ABS_MT_POSITION_X, be16_to_cpu(tch->x));
330                 input_report_abs(input, ABS_MT_POSITION_Y, be16_to_cpu(tch->y));
331                 input_report_abs(input, ABS_MT_TOUCH_MAJOR, tch->z);
332
333                 __set_bit(ids[i], used);
334         }
335
336         for (i = 0; i < CY_MAX_ID; i++) {
337                 if (test_bit(i, used))
338                         continue;
339
340                 input_mt_slot(input, i);
341                 input_mt_report_slot_state(input, MT_TOOL_FINGER, false);
342         }
343
344         input_sync(input);
345 }
346
347 static irqreturn_t cyttsp_irq(int irq, void *handle)
348 {
349         struct cyttsp *ts = handle;
350         int error;
351
352         if (unlikely(ts->state == CY_BL_STATE)) {
353                 complete(&ts->bl_ready);
354                 goto out;
355         }
356
357         /* Get touch data from CYTTSP device */
358         error = ttsp_read_block_data(ts, CY_REG_BASE,
359                                  sizeof(struct cyttsp_xydata), &ts->xy_data);
360         if (error)
361                 goto out;
362
363         /* provide flow control handshake */
364         error = cyttsp_handshake(ts);
365         if (error)
366                 goto out;
367
368         if (unlikely(ts->state == CY_IDLE_STATE))
369                 goto out;
370
371         if (GET_BOOTLOADERMODE(ts->xy_data.tt_mode)) {
372                 /*
373                  * TTSP device has reset back to bootloader mode.
374                  * Restore to operational mode.
375                  */
376                 error = cyttsp_exit_bl_mode(ts);
377                 if (error) {
378                         dev_err(ts->dev,
379                                 "Could not return to operational mode, err: %d\n",
380                                 error);
381                         ts->state = CY_IDLE_STATE;
382                 }
383         } else {
384                 cyttsp_report_tchdata(ts);
385         }
386
387 out:
388         return IRQ_HANDLED;
389 }
390
391 static int cyttsp_power_on(struct cyttsp *ts)
392 {
393         int error;
394
395         error = cyttsp_soft_reset(ts);
396         if (error)
397                 return error;
398
399         error = cyttsp_load_bl_regs(ts);
400         if (error)
401                 return error;
402
403         if (GET_BOOTLOADERMODE(ts->bl_data.bl_status) &&
404             IS_VALID_APP(ts->bl_data.bl_status)) {
405                 error = cyttsp_exit_bl_mode(ts);
406                 if (error)
407                         return error;
408         }
409
410         if (GET_HSTMODE(ts->bl_data.bl_file) != CY_OPERATE_MODE ||
411             IS_OPERATIONAL_ERR(ts->bl_data.bl_status)) {
412                 return -ENODEV;
413         }
414
415         error = cyttsp_set_sysinfo_mode(ts);
416         if (error)
417                 return error;
418
419         error = cyttsp_set_sysinfo_regs(ts);
420         if (error)
421                 return error;
422
423         error = cyttsp_set_operational_mode(ts);
424         if (error)
425                 return error;
426
427         /* init active distance */
428         error = cyttsp_act_dist_setup(ts);
429         if (error)
430                 return error;
431
432         ts->state = CY_ACTIVE_STATE;
433
434         return 0;
435 }
436
437 static int cyttsp_enable(struct cyttsp *ts)
438 {
439         int error;
440
441         /*
442          * The device firmware can wake on an I2C or SPI memory slave
443          * address match. So just reading a register is sufficient to
444          * wake up the device. The first read attempt will fail but it
445          * will wake it up making the second read attempt successful.
446          */
447         error = ttsp_read_block_data(ts, CY_REG_BASE,
448                                      sizeof(ts->xy_data), &ts->xy_data);
449         if (error)
450                 return error;
451
452         if (GET_HSTMODE(ts->xy_data.hst_mode))
453                 return -EIO;
454
455         enable_irq(ts->irq);
456
457         return 0;
458 }
459
460 static int cyttsp_disable(struct cyttsp *ts)
461 {
462         int error;
463
464         error = ttsp_send_command(ts, CY_LOW_POWER_MODE);
465         if (error)
466                 return error;
467
468         disable_irq(ts->irq);
469
470         return 0;
471 }
472
473 #ifdef CONFIG_PM_SLEEP
474 static int cyttsp_suspend(struct device *dev)
475 {
476         struct cyttsp *ts = dev_get_drvdata(dev);
477         int retval = 0;
478
479         mutex_lock(&ts->input->mutex);
480
481         if (ts->input->users) {
482                 retval = cyttsp_disable(ts);
483                 if (retval == 0)
484                         ts->suspended = true;
485         }
486
487         mutex_unlock(&ts->input->mutex);
488
489         return retval;
490 }
491
492 static int cyttsp_resume(struct device *dev)
493 {
494         struct cyttsp *ts = dev_get_drvdata(dev);
495
496         mutex_lock(&ts->input->mutex);
497
498         if (ts->input->users)
499                 cyttsp_enable(ts);
500
501         ts->suspended = false;
502
503         mutex_unlock(&ts->input->mutex);
504
505         return 0;
506 }
507
508 #endif
509
510 SIMPLE_DEV_PM_OPS(cyttsp_pm_ops, cyttsp_suspend, cyttsp_resume);
511 EXPORT_SYMBOL_GPL(cyttsp_pm_ops);
512
513 static int cyttsp_open(struct input_dev *dev)
514 {
515         struct cyttsp *ts = input_get_drvdata(dev);
516         int retval = 0;
517
518         if (!ts->suspended)
519                 retval = cyttsp_enable(ts);
520
521         return retval;
522 }
523
524 static void cyttsp_close(struct input_dev *dev)
525 {
526         struct cyttsp *ts = input_get_drvdata(dev);
527
528         if (!ts->suspended)
529                 cyttsp_disable(ts);
530 }
531
532 struct cyttsp *cyttsp_probe(const struct cyttsp_bus_ops *bus_ops,
533                             struct device *dev, int irq, size_t xfer_buf_size)
534 {
535         const struct cyttsp_platform_data *pdata = dev->platform_data;
536         struct cyttsp *ts;
537         struct input_dev *input_dev;
538         int error;
539
540         if (!pdata || !pdata->name || irq <= 0) {
541                 error = -EINVAL;
542                 goto err_out;
543         }
544
545         ts = kzalloc(sizeof(*ts) + xfer_buf_size, GFP_KERNEL);
546         input_dev = input_allocate_device();
547         if (!ts || !input_dev) {
548                 error = -ENOMEM;
549                 goto err_free_mem;
550         }
551
552         ts->dev = dev;
553         ts->input = input_dev;
554         ts->pdata = dev->platform_data;
555         ts->bus_ops = bus_ops;
556         ts->irq = irq;
557
558         init_completion(&ts->bl_ready);
559         snprintf(ts->phys, sizeof(ts->phys), "%s/input0", dev_name(dev));
560
561         if (pdata->init) {
562                 error = pdata->init();
563                 if (error) {
564                         dev_err(ts->dev, "platform init failed, err: %d\n",
565                                 error);
566                         goto err_free_mem;
567                 }
568         }
569
570         input_dev->name = pdata->name;
571         input_dev->phys = ts->phys;
572         input_dev->id.bustype = bus_ops->bustype;
573         input_dev->dev.parent = ts->dev;
574
575         input_dev->open = cyttsp_open;
576         input_dev->close = cyttsp_close;
577
578         input_set_drvdata(input_dev, ts);
579
580         __set_bit(EV_ABS, input_dev->evbit);
581         input_set_abs_params(input_dev, ABS_MT_POSITION_X,
582                              0, pdata->maxx, 0, 0);
583         input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
584                              0, pdata->maxy, 0, 0);
585         input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR,
586                              0, CY_MAXZ, 0, 0);
587
588         input_mt_init_slots(input_dev, CY_MAX_ID, 0);
589
590         error = request_threaded_irq(ts->irq, NULL, cyttsp_irq,
591                                      IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
592                                      pdata->name, ts);
593         if (error) {
594                 dev_err(ts->dev, "failed to request IRQ %d, err: %d\n",
595                         ts->irq, error);
596                 goto err_platform_exit;
597         }
598
599         disable_irq(ts->irq);
600
601         error = cyttsp_power_on(ts);
602         if (error)
603                 goto err_free_irq;
604
605         error = input_register_device(input_dev);
606         if (error) {
607                 dev_err(ts->dev, "failed to register input device: %d\n",
608                         error);
609                 goto err_free_irq;
610         }
611
612         return ts;
613
614 err_free_irq:
615         free_irq(ts->irq, ts);
616 err_platform_exit:
617         if (pdata->exit)
618                 pdata->exit();
619 err_free_mem:
620         input_free_device(input_dev);
621         kfree(ts);
622 err_out:
623         return ERR_PTR(error);
624 }
625 EXPORT_SYMBOL_GPL(cyttsp_probe);
626
627 void cyttsp_remove(struct cyttsp *ts)
628 {
629         free_irq(ts->irq, ts);
630         input_unregister_device(ts->input);
631         if (ts->pdata->exit)
632                 ts->pdata->exit();
633         kfree(ts);
634 }
635 EXPORT_SYMBOL_GPL(cyttsp_remove);
636
637 MODULE_LICENSE("GPL");
638 MODULE_DESCRIPTION("Cypress TrueTouch(R) Standard touchscreen driver core");
639 MODULE_AUTHOR("Cypress");