1 // SPDX-License-Identifier: GPL-2.0-only
3 * Driver for cypress touch screen controller
5 * Copyright (c) 2009 Aava Mobile
7 * Some cleanups by Alan Cox <alan@linux.intel.com>
10 #include <linux/i2c.h>
11 #include <linux/input.h>
12 #include <linux/interrupt.h>
13 #include <linux/gpio/consumer.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/slab.h>
17 #include <asm/byteorder.h>
19 #define CY8CTMG110_DRIVER_NAME "cy8ctmg110"
21 /* Touch coordinates */
22 #define CY8CTMG110_X_MIN 0
23 #define CY8CTMG110_Y_MIN 0
24 #define CY8CTMG110_X_MAX 759
25 #define CY8CTMG110_Y_MAX 465
28 /* cy8ctmg110 register definitions */
29 #define CY8CTMG110_TOUCH_WAKEUP_TIME 0
30 #define CY8CTMG110_TOUCH_SLEEP_TIME 2
31 #define CY8CTMG110_TOUCH_X1 3
32 #define CY8CTMG110_TOUCH_Y1 5
33 #define CY8CTMG110_TOUCH_X2 7
34 #define CY8CTMG110_TOUCH_Y2 9
35 #define CY8CTMG110_FINGERS 11
36 #define CY8CTMG110_GESTURE 12
37 #define CY8CTMG110_REG_MAX 13
41 * The touch driver structure.
44 struct input_dev
*input
;
46 struct i2c_client
*client
;
47 struct gpio_desc
*reset_gpio
;
51 * cy8ctmg110_power is the routine that is called when touch hardware
52 * is being powered off or on. When powering on this routine de-asserts
53 * the RESET line, when powering off reset line is asserted.
55 static void cy8ctmg110_power(struct cy8ctmg110
*ts
, bool poweron
)
58 gpiod_set_value_cansleep(ts
->reset_gpio
, !poweron
);
61 static int cy8ctmg110_write_regs(struct cy8ctmg110
*tsc
, unsigned char reg
,
62 unsigned char len
, unsigned char *value
)
64 struct i2c_client
*client
= tsc
->client
;
66 unsigned char i2c_data
[6];
71 memcpy(i2c_data
+ 1, value
, len
);
73 ret
= i2c_master_send(client
, i2c_data
, len
+ 1);
75 dev_err(&client
->dev
, "i2c write data cmd failed\n");
76 return ret
< 0 ? ret
: -EIO
;
82 static int cy8ctmg110_read_regs(struct cy8ctmg110
*tsc
,
83 unsigned char *data
, unsigned char len
, unsigned char cmd
)
85 struct i2c_client
*client
= tsc
->client
;
87 struct i2c_msg msg
[2] = {
88 /* first write slave position to i2c devices */
94 /* Second read data from position */
103 ret
= i2c_transfer(client
->adapter
, msg
, 2);
110 static int cy8ctmg110_touch_pos(struct cy8ctmg110
*tsc
)
112 struct input_dev
*input
= tsc
->input
;
113 unsigned char reg_p
[CY8CTMG110_REG_MAX
];
115 memset(reg_p
, 0, CY8CTMG110_REG_MAX
);
117 /* Reading coordinates */
118 if (cy8ctmg110_read_regs(tsc
, reg_p
, 9, CY8CTMG110_TOUCH_X1
) != 0)
121 /* Number of touch */
123 input_report_key(input
, BTN_TOUCH
, 0);
125 input_report_key(input
, BTN_TOUCH
, 1);
126 input_report_abs(input
, ABS_X
,
127 be16_to_cpup((__be16
*)(reg_p
+ 0)));
128 input_report_abs(input
, ABS_Y
,
129 be16_to_cpup((__be16
*)(reg_p
+ 2)));
137 static int cy8ctmg110_set_sleepmode(struct cy8ctmg110
*ts
, bool sleep
)
139 unsigned char reg_p
[3];
151 return cy8ctmg110_write_regs(ts
, CY8CTMG110_TOUCH_WAKEUP_TIME
, 3, reg_p
);
154 static irqreturn_t
cy8ctmg110_irq_thread(int irq
, void *dev_id
)
156 struct cy8ctmg110
*tsc
= dev_id
;
158 cy8ctmg110_touch_pos(tsc
);
163 static void cy8ctmg110_shut_off(void *_ts
)
165 struct cy8ctmg110
*ts
= _ts
;
167 cy8ctmg110_set_sleepmode(ts
, true);
168 cy8ctmg110_power(ts
, false);
171 static int cy8ctmg110_probe(struct i2c_client
*client
)
173 struct cy8ctmg110
*ts
;
174 struct input_dev
*input_dev
;
177 if (!i2c_check_functionality(client
->adapter
,
178 I2C_FUNC_SMBUS_READ_WORD_DATA
))
181 ts
= devm_kzalloc(&client
->dev
, sizeof(*ts
), GFP_KERNEL
);
185 input_dev
= devm_input_allocate_device(&client
->dev
);
190 ts
->input
= input_dev
;
192 snprintf(ts
->phys
, sizeof(ts
->phys
),
193 "%s/input0", dev_name(&client
->dev
));
195 input_dev
->name
= CY8CTMG110_DRIVER_NAME
" Touchscreen";
196 input_dev
->phys
= ts
->phys
;
197 input_dev
->id
.bustype
= BUS_I2C
;
199 input_set_capability(input_dev
, EV_KEY
, BTN_TOUCH
);
200 input_set_abs_params(input_dev
, ABS_X
,
201 CY8CTMG110_X_MIN
, CY8CTMG110_X_MAX
, 4, 0);
202 input_set_abs_params(input_dev
, ABS_Y
,
203 CY8CTMG110_Y_MIN
, CY8CTMG110_Y_MAX
, 4, 0);
205 /* Request and assert reset line */
206 ts
->reset_gpio
= devm_gpiod_get_optional(&client
->dev
, NULL
,
208 if (IS_ERR(ts
->reset_gpio
)) {
209 err
= PTR_ERR(ts
->reset_gpio
);
210 dev_err(&client
->dev
,
211 "Unable to request reset GPIO: %d\n", err
);
215 cy8ctmg110_power(ts
, true);
216 cy8ctmg110_set_sleepmode(ts
, false);
218 err
= devm_add_action_or_reset(&client
->dev
, cy8ctmg110_shut_off
, ts
);
222 err
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
223 NULL
, cy8ctmg110_irq_thread
,
224 IRQF_ONESHOT
, "touch_reset_key", ts
);
226 dev_err(&client
->dev
,
227 "irq %d busy? error %d\n", client
->irq
, err
);
231 err
= input_register_device(input_dev
);
235 i2c_set_clientdata(client
, ts
);
240 static int cy8ctmg110_suspend(struct device
*dev
)
242 struct i2c_client
*client
= to_i2c_client(dev
);
243 struct cy8ctmg110
*ts
= i2c_get_clientdata(client
);
245 if (!device_may_wakeup(&client
->dev
)) {
246 cy8ctmg110_set_sleepmode(ts
, true);
247 cy8ctmg110_power(ts
, false);
253 static int cy8ctmg110_resume(struct device
*dev
)
255 struct i2c_client
*client
= to_i2c_client(dev
);
256 struct cy8ctmg110
*ts
= i2c_get_clientdata(client
);
258 if (!device_may_wakeup(&client
->dev
)) {
259 cy8ctmg110_power(ts
, true);
260 cy8ctmg110_set_sleepmode(ts
, false);
266 static DEFINE_SIMPLE_DEV_PM_OPS(cy8ctmg110_pm
,
267 cy8ctmg110_suspend
, cy8ctmg110_resume
);
269 static const struct i2c_device_id cy8ctmg110_idtable
[] = {
270 { CY8CTMG110_DRIVER_NAME
, 1 },
274 MODULE_DEVICE_TABLE(i2c
, cy8ctmg110_idtable
);
276 static struct i2c_driver cy8ctmg110_driver
= {
278 .name
= CY8CTMG110_DRIVER_NAME
,
279 .pm
= pm_sleep_ptr(&cy8ctmg110_pm
),
281 .id_table
= cy8ctmg110_idtable
,
282 .probe
= cy8ctmg110_probe
,
285 module_i2c_driver(cy8ctmg110_driver
);
287 MODULE_AUTHOR("Samuli Konttila <samuli.konttila@aavamobile.com>");
288 MODULE_DESCRIPTION("cy8ctmg110 TouchScreen Driver");
289 MODULE_LICENSE("GPL v2");