1 // SPDX-License-Identifier: GPL-2.0-only
4 * Cypress TrueTouch(TM) Standard Product (TTSP) touchscreen drivers.
5 * For use with Cypress Txx3xx parts.
6 * Supported parts include:
10 * Copyright (C) 2009, 2010, 2011 Cypress Semiconductor, Inc.
11 * Copyright (C) 2012 Javier Martinez Canillas <javier@dowhile0.org>
13 * Contact Cypress Semiconductor at www.cypress.com <kev@cypress.com>
16 #include <linux/delay.h>
17 #include <linux/input.h>
18 #include <linux/input/mt.h>
19 #include <linux/input/touchscreen.h>
20 #include <linux/interrupt.h>
21 #include <linux/slab.h>
22 #include <linux/property.h>
23 #include <linux/gpio/consumer.h>
24 #include <linux/regulator/consumer.h>
26 #include "cyttsp_core.h"
28 /* Bootloader number of command keys */
29 #define CY_NUM_BL_KEYS 8
32 #define GET_NUM_TOUCHES(x) ((x) & 0x0F)
33 #define IS_LARGE_AREA(x) (((x) & 0x10) >> 4)
34 #define IS_BAD_PKT(x) ((x) & 0x20)
35 #define IS_VALID_APP(x) ((x) & 0x01)
36 #define IS_OPERATIONAL_ERR(x) ((x) & 0x3F)
37 #define GET_HSTMODE(reg) (((reg) & 0x70) >> 4)
38 #define GET_BOOTLOADERMODE(reg) (((reg) & 0x10) >> 4)
40 #define CY_REG_BASE 0x00
41 #define CY_REG_ACT_DIST 0x1E
42 #define CY_REG_ACT_INTRVL 0x1D
43 #define CY_REG_TCH_TMOUT (CY_REG_ACT_INTRVL + 1)
44 #define CY_REG_LP_INTRVL (CY_REG_TCH_TMOUT + 1)
46 #define CY_DELAY_DFLT 20 /* ms */
47 #define CY_DELAY_MAX 500
48 /* Active distance in pixels for a gesture to be reported */
49 #define CY_ACT_DIST_DFLT 0xF8 /* pixels */
50 #define CY_ACT_DIST_MASK 0x0F
51 /* Active Power state scanning/processing refresh interval */
52 #define CY_ACT_INTRVL_DFLT 0x00 /* ms */
53 /* Low Power state scanning/processing refresh interval */
54 #define CY_LP_INTRVL_DFLT 0x0A /* ms */
55 /* touch timeout for the Active power */
56 #define CY_TCH_TMOUT_DFLT 0xFF /* ms */
57 #define CY_HNDSHK_BIT 0x80
58 /* device mode bits */
59 #define CY_OPERATE_MODE 0x00
60 #define CY_SYSINFO_MODE 0x10
61 /* power mode select bits */
62 #define CY_SOFT_RESET_MODE 0x01 /* return to Bootloader mode */
63 #define CY_DEEP_SLEEP_MODE 0x02
64 #define CY_LOW_POWER_MODE 0x04
66 /* Slots management */
67 #define CY_MAX_FINGER 4
70 static const u8 bl_command
[] = {
71 0x00, /* file offset */
73 0xA5, /* exit bootloader command */
74 0, 1, 2, 3, 4, 5, 6, 7 /* default keys */
77 static int ttsp_read_block_data(struct cyttsp
*ts
, u8 command
,
83 for (tries
= 0; tries
< CY_NUM_RETRY
; tries
++) {
84 error
= ts
->bus_ops
->read(ts
->dev
, ts
->xfer_buf
, command
,
89 msleep(CY_DELAY_DFLT
);
95 static int ttsp_write_block_data(struct cyttsp
*ts
, u8 command
,
101 for (tries
= 0; tries
< CY_NUM_RETRY
; tries
++) {
102 error
= ts
->bus_ops
->write(ts
->dev
, ts
->xfer_buf
, command
,
107 msleep(CY_DELAY_DFLT
);
113 static int ttsp_send_command(struct cyttsp
*ts
, u8 cmd
)
115 return ttsp_write_block_data(ts
, CY_REG_BASE
, sizeof(cmd
), &cmd
);
118 static int cyttsp_handshake(struct cyttsp
*ts
)
121 return ttsp_send_command(ts
,
122 ts
->xy_data
.hst_mode
^ CY_HNDSHK_BIT
);
127 static int cyttsp_load_bl_regs(struct cyttsp
*ts
)
129 memset(&ts
->bl_data
, 0, sizeof(ts
->bl_data
));
130 ts
->bl_data
.bl_status
= 0x10;
132 return ttsp_read_block_data(ts
, CY_REG_BASE
,
133 sizeof(ts
->bl_data
), &ts
->bl_data
);
136 static int cyttsp_exit_bl_mode(struct cyttsp
*ts
)
139 u8 bl_cmd
[sizeof(bl_command
)];
141 memcpy(bl_cmd
, bl_command
, sizeof(bl_command
));
143 memcpy(&bl_cmd
[sizeof(bl_command
) - CY_NUM_BL_KEYS
],
144 ts
->bl_keys
, CY_NUM_BL_KEYS
);
146 error
= ttsp_write_block_data(ts
, CY_REG_BASE
,
147 sizeof(bl_cmd
), bl_cmd
);
151 /* wait for TTSP Device to complete the operation */
152 msleep(CY_DELAY_DFLT
);
154 error
= cyttsp_load_bl_regs(ts
);
158 if (GET_BOOTLOADERMODE(ts
->bl_data
.bl_status
))
164 static int cyttsp_set_operational_mode(struct cyttsp
*ts
)
168 error
= ttsp_send_command(ts
, CY_OPERATE_MODE
);
172 /* wait for TTSP Device to complete switch to Operational mode */
173 error
= ttsp_read_block_data(ts
, CY_REG_BASE
,
174 sizeof(ts
->xy_data
), &ts
->xy_data
);
178 error
= cyttsp_handshake(ts
);
182 return ts
->xy_data
.act_dist
== CY_ACT_DIST_DFLT
? -EIO
: 0;
185 static int cyttsp_set_sysinfo_mode(struct cyttsp
*ts
)
189 memset(&ts
->sysinfo_data
, 0, sizeof(ts
->sysinfo_data
));
191 /* switch to sysinfo mode */
192 error
= ttsp_send_command(ts
, CY_SYSINFO_MODE
);
196 /* read sysinfo registers */
197 msleep(CY_DELAY_DFLT
);
198 error
= ttsp_read_block_data(ts
, CY_REG_BASE
, sizeof(ts
->sysinfo_data
),
203 error
= cyttsp_handshake(ts
);
207 if (!ts
->sysinfo_data
.tts_verh
&& !ts
->sysinfo_data
.tts_verl
)
213 static int cyttsp_set_sysinfo_regs(struct cyttsp
*ts
)
217 if (ts
->act_intrvl
!= CY_ACT_INTRVL_DFLT
||
218 ts
->tch_tmout
!= CY_TCH_TMOUT_DFLT
||
219 ts
->lp_intrvl
!= CY_LP_INTRVL_DFLT
) {
227 /* set intrvl registers */
228 retval
= ttsp_write_block_data(ts
, CY_REG_ACT_INTRVL
,
229 sizeof(intrvl_ray
), intrvl_ray
);
230 msleep(CY_DELAY_DFLT
);
236 static void cyttsp_hard_reset(struct cyttsp
*ts
)
238 if (ts
->reset_gpio
) {
240 * According to the CY8CTMA340 datasheet page 21, the external
241 * reset pulse width should be >= 1 ms. The datasheet does not
242 * specify how long we have to wait after reset but a vendor
243 * tree specifies 5 ms here.
245 gpiod_set_value_cansleep(ts
->reset_gpio
, 1);
246 usleep_range(1000, 2000);
247 gpiod_set_value_cansleep(ts
->reset_gpio
, 0);
248 usleep_range(5000, 6000);
252 static int cyttsp_soft_reset(struct cyttsp
*ts
)
256 /* wait for interrupt to set ready completion */
257 reinit_completion(&ts
->bl_ready
);
258 ts
->state
= CY_BL_STATE
;
262 retval
= ttsp_send_command(ts
, CY_SOFT_RESET_MODE
);
264 dev_err(ts
->dev
, "failed to send soft reset\n");
268 if (!wait_for_completion_timeout(&ts
->bl_ready
,
269 msecs_to_jiffies(CY_DELAY_DFLT
* CY_DELAY_MAX
))) {
270 dev_err(ts
->dev
, "timeout waiting for soft reset\n");
275 ts
->state
= CY_IDLE_STATE
;
276 disable_irq(ts
->irq
);
280 static int cyttsp_act_dist_setup(struct cyttsp
*ts
)
282 u8 act_dist_setup
= ts
->act_dist
;
284 /* Init gesture; active distance setup */
285 return ttsp_write_block_data(ts
, CY_REG_ACT_DIST
,
286 sizeof(act_dist_setup
), &act_dist_setup
);
289 static void cyttsp_extract_track_ids(struct cyttsp_xydata
*xy_data
, int *ids
)
291 ids
[0] = xy_data
->touch12_id
>> 4;
292 ids
[1] = xy_data
->touch12_id
& 0xF;
293 ids
[2] = xy_data
->touch34_id
>> 4;
294 ids
[3] = xy_data
->touch34_id
& 0xF;
297 static const struct cyttsp_tch
*cyttsp_get_tch(struct cyttsp_xydata
*xy_data
,
302 return &xy_data
->tch1
;
304 return &xy_data
->tch2
;
306 return &xy_data
->tch3
;
308 return &xy_data
->tch4
;
314 static void cyttsp_report_tchdata(struct cyttsp
*ts
)
316 struct cyttsp_xydata
*xy_data
= &ts
->xy_data
;
317 struct input_dev
*input
= ts
->input
;
318 int num_tch
= GET_NUM_TOUCHES(xy_data
->tt_stat
);
319 const struct cyttsp_tch
*tch
;
322 DECLARE_BITMAP(used
, CY_MAX_ID
);
324 if (IS_LARGE_AREA(xy_data
->tt_stat
) == 1) {
325 /* terminate all active tracks */
327 dev_dbg(ts
->dev
, "%s: Large area detected\n", __func__
);
328 } else if (num_tch
> CY_MAX_FINGER
) {
329 /* terminate all active tracks */
331 dev_dbg(ts
->dev
, "%s: Num touch error detected\n", __func__
);
332 } else if (IS_BAD_PKT(xy_data
->tt_mode
)) {
333 /* terminate all active tracks */
335 dev_dbg(ts
->dev
, "%s: Invalid buffer detected\n", __func__
);
338 cyttsp_extract_track_ids(xy_data
, ids
);
340 bitmap_zero(used
, CY_MAX_ID
);
342 for (i
= 0; i
< num_tch
; i
++) {
343 tch
= cyttsp_get_tch(xy_data
, i
);
345 input_mt_slot(input
, ids
[i
]);
346 input_mt_report_slot_state(input
, MT_TOOL_FINGER
, true);
347 input_report_abs(input
, ABS_MT_POSITION_X
, be16_to_cpu(tch
->x
));
348 input_report_abs(input
, ABS_MT_POSITION_Y
, be16_to_cpu(tch
->y
));
349 input_report_abs(input
, ABS_MT_TOUCH_MAJOR
, tch
->z
);
351 __set_bit(ids
[i
], used
);
354 for (i
= 0; i
< CY_MAX_ID
; i
++) {
355 if (test_bit(i
, used
))
358 input_mt_slot(input
, i
);
359 input_mt_report_slot_inactive(input
);
365 static irqreturn_t
cyttsp_irq(int irq
, void *handle
)
367 struct cyttsp
*ts
= handle
;
370 if (unlikely(ts
->state
== CY_BL_STATE
)) {
371 complete(&ts
->bl_ready
);
375 /* Get touch data from CYTTSP device */
376 error
= ttsp_read_block_data(ts
, CY_REG_BASE
,
377 sizeof(struct cyttsp_xydata
), &ts
->xy_data
);
381 /* provide flow control handshake */
382 error
= cyttsp_handshake(ts
);
386 if (unlikely(ts
->state
== CY_IDLE_STATE
))
389 if (GET_BOOTLOADERMODE(ts
->xy_data
.tt_mode
)) {
391 * TTSP device has reset back to bootloader mode.
392 * Restore to operational mode.
394 error
= cyttsp_exit_bl_mode(ts
);
397 "Could not return to operational mode, err: %d\n",
399 ts
->state
= CY_IDLE_STATE
;
402 cyttsp_report_tchdata(ts
);
409 static int cyttsp_power_on(struct cyttsp
*ts
)
413 error
= cyttsp_soft_reset(ts
);
417 error
= cyttsp_load_bl_regs(ts
);
421 if (GET_BOOTLOADERMODE(ts
->bl_data
.bl_status
) &&
422 IS_VALID_APP(ts
->bl_data
.bl_status
)) {
423 error
= cyttsp_exit_bl_mode(ts
);
425 dev_err(ts
->dev
, "failed to exit bootloader mode\n");
430 if (GET_HSTMODE(ts
->bl_data
.bl_file
) != CY_OPERATE_MODE
||
431 IS_OPERATIONAL_ERR(ts
->bl_data
.bl_status
)) {
435 error
= cyttsp_set_sysinfo_mode(ts
);
439 error
= cyttsp_set_sysinfo_regs(ts
);
443 error
= cyttsp_set_operational_mode(ts
);
447 /* init active distance */
448 error
= cyttsp_act_dist_setup(ts
);
452 ts
->state
= CY_ACTIVE_STATE
;
457 static int cyttsp_enable(struct cyttsp
*ts
)
462 * The device firmware can wake on an I2C or SPI memory slave
463 * address match. So just reading a register is sufficient to
464 * wake up the device. The first read attempt will fail but it
465 * will wake it up making the second read attempt successful.
467 error
= ttsp_read_block_data(ts
, CY_REG_BASE
,
468 sizeof(ts
->xy_data
), &ts
->xy_data
);
472 if (GET_HSTMODE(ts
->xy_data
.hst_mode
))
480 static int cyttsp_disable(struct cyttsp
*ts
)
484 error
= ttsp_send_command(ts
, CY_LOW_POWER_MODE
);
488 disable_irq(ts
->irq
);
493 static int cyttsp_suspend(struct device
*dev
)
495 struct cyttsp
*ts
= dev_get_drvdata(dev
);
498 mutex_lock(&ts
->input
->mutex
);
500 if (input_device_enabled(ts
->input
)) {
501 retval
= cyttsp_disable(ts
);
503 ts
->suspended
= true;
506 mutex_unlock(&ts
->input
->mutex
);
511 static int cyttsp_resume(struct device
*dev
)
513 struct cyttsp
*ts
= dev_get_drvdata(dev
);
515 mutex_lock(&ts
->input
->mutex
);
517 if (input_device_enabled(ts
->input
))
520 ts
->suspended
= false;
522 mutex_unlock(&ts
->input
->mutex
);
527 EXPORT_GPL_SIMPLE_DEV_PM_OPS(cyttsp_pm_ops
, cyttsp_suspend
, cyttsp_resume
);
529 static int cyttsp_open(struct input_dev
*dev
)
531 struct cyttsp
*ts
= input_get_drvdata(dev
);
535 retval
= cyttsp_enable(ts
);
540 static void cyttsp_close(struct input_dev
*dev
)
542 struct cyttsp
*ts
= input_get_drvdata(dev
);
548 static int cyttsp_parse_properties(struct cyttsp
*ts
)
550 struct device
*dev
= ts
->dev
;
554 ts
->bl_keys
= devm_kzalloc(dev
, CY_NUM_BL_KEYS
, GFP_KERNEL
);
558 /* Set some default values */
559 ts
->use_hndshk
= false;
560 ts
->act_dist
= CY_ACT_DIST_DFLT
;
561 ts
->act_intrvl
= CY_ACT_INTRVL_DFLT
;
562 ts
->tch_tmout
= CY_TCH_TMOUT_DFLT
;
563 ts
->lp_intrvl
= CY_LP_INTRVL_DFLT
;
565 ret
= device_property_read_u8_array(dev
, "bootloader-key",
566 ts
->bl_keys
, CY_NUM_BL_KEYS
);
569 "bootloader-key property could not be retrieved\n");
573 ts
->use_hndshk
= device_property_present(dev
, "use-handshake");
575 if (!device_property_read_u32(dev
, "active-distance", &dt_value
)) {
577 dev_err(dev
, "active-distance (%u) must be [0-15]\n",
581 ts
->act_dist
&= ~CY_ACT_DIST_MASK
;
582 ts
->act_dist
|= dt_value
;
585 if (!device_property_read_u32(dev
, "active-interval-ms", &dt_value
)) {
586 if (dt_value
> 255) {
587 dev_err(dev
, "active-interval-ms (%u) must be [0-255]\n",
591 ts
->act_intrvl
= dt_value
;
594 if (!device_property_read_u32(dev
, "lowpower-interval-ms", &dt_value
)) {
595 if (dt_value
> 2550) {
596 dev_err(dev
, "lowpower-interval-ms (%u) must be [0-2550]\n",
600 /* Register value is expressed in 0.01s / bit */
601 ts
->lp_intrvl
= dt_value
/ 10;
604 if (!device_property_read_u32(dev
, "touch-timeout-ms", &dt_value
)) {
605 if (dt_value
> 2550) {
606 dev_err(dev
, "touch-timeout-ms (%u) must be [0-2550]\n",
610 /* Register value is expressed in 0.01s / bit */
611 ts
->tch_tmout
= dt_value
/ 10;
617 struct cyttsp
*cyttsp_probe(const struct cyttsp_bus_ops
*bus_ops
,
618 struct device
*dev
, int irq
, size_t xfer_buf_size
)
621 * VCPIN is the analog voltage supply
622 * VDD is the digital voltage supply
624 static const char * const supplies
[] = { "vcpin", "vdd" };
626 struct input_dev
*input_dev
;
629 ts
= devm_kzalloc(dev
, sizeof(*ts
) + xfer_buf_size
, GFP_KERNEL
);
631 return ERR_PTR(-ENOMEM
);
633 input_dev
= devm_input_allocate_device(dev
);
635 return ERR_PTR(-ENOMEM
);
638 ts
->input
= input_dev
;
639 ts
->bus_ops
= bus_ops
;
642 error
= devm_regulator_bulk_get_enable(dev
, ARRAY_SIZE(supplies
),
645 dev_err(dev
, "Failed to enable regulators: %d\n", error
);
646 return ERR_PTR(error
);
649 ts
->reset_gpio
= devm_gpiod_get_optional(dev
, "reset", GPIOD_OUT_LOW
);
650 if (IS_ERR(ts
->reset_gpio
)) {
651 error
= PTR_ERR(ts
->reset_gpio
);
652 dev_err(dev
, "Failed to request reset gpio, error %d\n", error
);
653 return ERR_PTR(error
);
656 error
= cyttsp_parse_properties(ts
);
658 return ERR_PTR(error
);
660 init_completion(&ts
->bl_ready
);
662 input_dev
->name
= "Cypress TTSP TouchScreen";
663 input_dev
->id
.bustype
= bus_ops
->bustype
;
664 input_dev
->dev
.parent
= ts
->dev
;
666 input_dev
->open
= cyttsp_open
;
667 input_dev
->close
= cyttsp_close
;
669 input_set_drvdata(input_dev
, ts
);
671 input_set_capability(input_dev
, EV_ABS
, ABS_MT_POSITION_X
);
672 input_set_capability(input_dev
, EV_ABS
, ABS_MT_POSITION_Y
);
673 /* One byte for width 0..255 so this is the limit */
674 input_set_abs_params(input_dev
, ABS_MT_TOUCH_MAJOR
, 0, 255, 0, 0);
676 touchscreen_parse_properties(input_dev
, true, NULL
);
678 error
= input_mt_init_slots(input_dev
, CY_MAX_ID
, INPUT_MT_DIRECT
);
680 dev_err(dev
, "Unable to init MT slots.\n");
681 return ERR_PTR(error
);
684 error
= devm_request_threaded_irq(dev
, ts
->irq
, NULL
, cyttsp_irq
,
685 IRQF_ONESHOT
| IRQF_NO_AUTOEN
,
688 dev_err(ts
->dev
, "failed to request IRQ %d, err: %d\n",
690 return ERR_PTR(error
);
693 cyttsp_hard_reset(ts
);
695 error
= cyttsp_power_on(ts
);
697 return ERR_PTR(error
);
699 error
= input_register_device(input_dev
);
701 dev_err(ts
->dev
, "failed to register input device: %d\n",
703 return ERR_PTR(error
);
708 EXPORT_SYMBOL_GPL(cyttsp_probe
);
710 MODULE_LICENSE("GPL");
711 MODULE_DESCRIPTION("Cypress TrueTouch(R) Standard touchscreen driver core");
712 MODULE_AUTHOR("Cypress");