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/module.h>
11 #include <linux/kernel.h>
12 #include <linux/input.h>
13 #include <linux/slab.h>
14 #include <linux/interrupt.h>
16 #include <linux/i2c.h>
17 #include <linux/gpio.h>
18 #include <linux/input/cy8ctmg110_pdata.h>
19 #include <asm/byteorder.h>
21 #define CY8CTMG110_DRIVER_NAME "cy8ctmg110"
23 /* Touch coordinates */
24 #define CY8CTMG110_X_MIN 0
25 #define CY8CTMG110_Y_MIN 0
26 #define CY8CTMG110_X_MAX 759
27 #define CY8CTMG110_Y_MAX 465
30 /* cy8ctmg110 register definitions */
31 #define CY8CTMG110_TOUCH_WAKEUP_TIME 0
32 #define CY8CTMG110_TOUCH_SLEEP_TIME 2
33 #define CY8CTMG110_TOUCH_X1 3
34 #define CY8CTMG110_TOUCH_Y1 5
35 #define CY8CTMG110_TOUCH_X2 7
36 #define CY8CTMG110_TOUCH_Y2 9
37 #define CY8CTMG110_FINGERS 11
38 #define CY8CTMG110_GESTURE 12
39 #define CY8CTMG110_REG_MAX 13
43 * The touch driver structure.
46 struct input_dev *input;
48 struct i2c_client *client;
53 * cy8ctmg110_power is the routine that is called when touch hardware
54 * will powered off or on.
56 static void cy8ctmg110_power(struct cy8ctmg110 *ts, bool poweron)
59 gpio_direction_output(ts->reset_pin, 1 - poweron);
62 static int cy8ctmg110_write_regs(struct cy8ctmg110 *tsc, unsigned char reg,
63 unsigned char len, unsigned char *value)
65 struct i2c_client *client = tsc->client;
67 unsigned char i2c_data[6];
72 memcpy(i2c_data + 1, value, len);
74 ret = i2c_master_send(client, i2c_data, len + 1);
76 dev_err(&client->dev, "i2c write data cmd failed\n");
77 return ret < 0 ? ret : -EIO;
83 static int cy8ctmg110_read_regs(struct cy8ctmg110 *tsc,
84 unsigned char *data, unsigned char len, unsigned char cmd)
86 struct i2c_client *client = tsc->client;
88 struct i2c_msg msg[2] = {
89 /* first write slave position to i2c devices */
95 /* Second read data from position */
104 ret = i2c_transfer(client->adapter, msg, 2);
111 static int cy8ctmg110_touch_pos(struct cy8ctmg110 *tsc)
113 struct input_dev *input = tsc->input;
114 unsigned char reg_p[CY8CTMG110_REG_MAX];
116 memset(reg_p, 0, CY8CTMG110_REG_MAX);
118 /* Reading coordinates */
119 if (cy8ctmg110_read_regs(tsc, reg_p, 9, CY8CTMG110_TOUCH_X1) != 0)
122 /* Number of touch */
124 input_report_key(input, BTN_TOUCH, 0);
126 input_report_key(input, BTN_TOUCH, 1);
127 input_report_abs(input, ABS_X,
128 be16_to_cpup((__be16 *)(reg_p + 0)));
129 input_report_abs(input, ABS_Y,
130 be16_to_cpup((__be16 *)(reg_p + 2)));
138 static int cy8ctmg110_set_sleepmode(struct cy8ctmg110 *ts, bool sleep)
140 unsigned char reg_p[3];
152 return cy8ctmg110_write_regs(ts, CY8CTMG110_TOUCH_WAKEUP_TIME, 3, reg_p);
155 static irqreturn_t cy8ctmg110_irq_thread(int irq, void *dev_id)
157 struct cy8ctmg110 *tsc = dev_id;
159 cy8ctmg110_touch_pos(tsc);
164 static void cy8ctmg110_shut_off(void *_ts)
166 struct cy8ctmg110 *ts = _ts;
168 cy8ctmg110_set_sleepmode(ts, true);
169 cy8ctmg110_power(ts, false);
172 static int cy8ctmg110_probe(struct i2c_client *client,
173 const struct i2c_device_id *id)
175 const struct cy8ctmg110_pdata *pdata = dev_get_platdata(&client->dev);
176 struct cy8ctmg110 *ts;
177 struct input_dev *input_dev;
180 /* No pdata no way forward */
182 dev_err(&client->dev, "no pdata\n");
186 if (!i2c_check_functionality(client->adapter,
187 I2C_FUNC_SMBUS_READ_WORD_DATA))
190 ts = devm_kzalloc(&client->dev, sizeof(*ts), GFP_KERNEL);
194 input_dev = devm_input_allocate_device(&client->dev);
199 ts->input = input_dev;
200 ts->reset_pin = pdata->reset_pin;
202 snprintf(ts->phys, sizeof(ts->phys),
203 "%s/input0", dev_name(&client->dev));
205 input_dev->name = CY8CTMG110_DRIVER_NAME " Touchscreen";
206 input_dev->phys = ts->phys;
207 input_dev->id.bustype = BUS_I2C;
209 input_set_capability(input_dev, EV_KEY, BTN_TOUCH);
210 input_set_abs_params(input_dev, ABS_X,
211 CY8CTMG110_X_MIN, CY8CTMG110_X_MAX, 4, 0);
212 input_set_abs_params(input_dev, ABS_Y,
213 CY8CTMG110_Y_MIN, CY8CTMG110_Y_MAX, 4, 0);
216 err = devm_gpio_request(&client->dev, ts->reset_pin, NULL);
218 dev_err(&client->dev,
219 "Unable to request GPIO pin %d.\n",
225 cy8ctmg110_power(ts, true);
226 cy8ctmg110_set_sleepmode(ts, false);
228 err = devm_add_action_or_reset(&client->dev, cy8ctmg110_shut_off, ts);
232 err = devm_request_threaded_irq(&client->dev, client->irq,
233 NULL, cy8ctmg110_irq_thread,
234 IRQF_ONESHOT, "touch_reset_key", ts);
236 dev_err(&client->dev,
237 "irq %d busy? error %d\n", client->irq, err);
241 err = input_register_device(input_dev);
245 i2c_set_clientdata(client, ts);
250 static int __maybe_unused cy8ctmg110_suspend(struct device *dev)
252 struct i2c_client *client = to_i2c_client(dev);
253 struct cy8ctmg110 *ts = i2c_get_clientdata(client);
255 if (!device_may_wakeup(&client->dev)) {
256 cy8ctmg110_set_sleepmode(ts, true);
257 cy8ctmg110_power(ts, false);
263 static int __maybe_unused cy8ctmg110_resume(struct device *dev)
265 struct i2c_client *client = to_i2c_client(dev);
266 struct cy8ctmg110 *ts = i2c_get_clientdata(client);
268 if (!device_may_wakeup(&client->dev)) {
269 cy8ctmg110_power(ts, true);
270 cy8ctmg110_set_sleepmode(ts, false);
276 static SIMPLE_DEV_PM_OPS(cy8ctmg110_pm, cy8ctmg110_suspend, cy8ctmg110_resume);
278 static const struct i2c_device_id cy8ctmg110_idtable[] = {
279 { CY8CTMG110_DRIVER_NAME, 1 },
283 MODULE_DEVICE_TABLE(i2c, cy8ctmg110_idtable);
285 static struct i2c_driver cy8ctmg110_driver = {
287 .name = CY8CTMG110_DRIVER_NAME,
288 .pm = &cy8ctmg110_pm,
290 .id_table = cy8ctmg110_idtable,
291 .probe = cy8ctmg110_probe,
294 module_i2c_driver(cy8ctmg110_driver);
296 MODULE_AUTHOR("Samuli Konttila <samuli.konttila@aavamobile.com>");
297 MODULE_DESCRIPTION("cy8ctmg110 TouchScreen Driver");
298 MODULE_LICENSE("GPL v2");