blob: 11ff32c6802506a8deedfe460010c9974a046de6 [file] [log] [blame]
Kuninori Morimoto4aeba642018-11-12 11:30:06 -08001// SPDX-License-Identifier: GPL-2.0
Tony SIM56a8bd62010-12-15 23:39:25 -08002/*
3 * ST1232 Touchscreen Controller Driver
4 *
5 * Copyright (C) 2010 Renesas Solutions Corp.
6 * Tony SIM <chinyeow.sim.xt@renesas.com>
7 *
8 * Using code from:
9 * - android.git.kernel.org: projects/kernel/common.git: synaptics_i2c_rmi.c
10 * Copyright (C) 2007 Google, Inc.
Tony SIM56a8bd62010-12-15 23:39:25 -080011 */
12
13#include <linux/delay.h>
Bastian Hechte6a90812013-04-15 09:31:00 -070014#include <linux/gpio.h>
Tony SIM56a8bd62010-12-15 23:39:25 -080015#include <linux/i2c.h>
16#include <linux/input.h>
17#include <linux/interrupt.h>
18#include <linux/module.h>
Sachin Kamat4d6e4822013-10-06 00:56:40 -070019#include <linux/of.h>
Bastian Hechte6a90812013-04-15 09:31:00 -070020#include <linux/of_gpio.h>
Rafael J. Wysocki9ee27ff2011-12-23 01:24:25 +010021#include <linux/pm_qos.h>
Tony SIM56a8bd62010-12-15 23:39:25 -080022#include <linux/slab.h>
23#include <linux/types.h>
24
25#define ST1232_TS_NAME "st1232-ts"
26
27#define MIN_X 0x00
28#define MIN_Y 0x00
29#define MAX_X 0x31f /* (800 - 1) */
30#define MAX_Y 0x1df /* (480 - 1) */
31#define MAX_AREA 0xff
32#define MAX_FINGERS 2
33
34struct st1232_ts_finger {
35 u16 x;
36 u16 y;
37 u8 t;
38 bool is_valid;
39};
40
41struct st1232_ts_data {
42 struct i2c_client *client;
43 struct input_dev *input_dev;
44 struct st1232_ts_finger finger[MAX_FINGERS];
Rafael J. Wysocki9ee27ff2011-12-23 01:24:25 +010045 struct dev_pm_qos_request low_latency_req;
Bastian Hechte6a90812013-04-15 09:31:00 -070046 int reset_gpio;
Tony SIM56a8bd62010-12-15 23:39:25 -080047};
48
49static int st1232_ts_read_data(struct st1232_ts_data *ts)
50{
51 struct st1232_ts_finger *finger = ts->finger;
52 struct i2c_client *client = ts->client;
53 struct i2c_msg msg[2];
54 int error;
55 u8 start_reg;
56 u8 buf[10];
57
58 /* read touchscreen data from ST1232 */
59 msg[0].addr = client->addr;
60 msg[0].flags = 0;
61 msg[0].len = 1;
62 msg[0].buf = &start_reg;
63 start_reg = 0x10;
64
65 msg[1].addr = ts->client->addr;
66 msg[1].flags = I2C_M_RD;
67 msg[1].len = sizeof(buf);
68 msg[1].buf = buf;
69
70 error = i2c_transfer(client->adapter, msg, 2);
71 if (error < 0)
72 return error;
73
74 /* get "valid" bits */
75 finger[0].is_valid = buf[2] >> 7;
76 finger[1].is_valid = buf[5] >> 7;
77
78 /* get xy coordinate */
79 if (finger[0].is_valid) {
80 finger[0].x = ((buf[2] & 0x0070) << 4) | buf[3];
81 finger[0].y = ((buf[2] & 0x0007) << 8) | buf[4];
82 finger[0].t = buf[8];
83 }
84
85 if (finger[1].is_valid) {
86 finger[1].x = ((buf[5] & 0x0070) << 4) | buf[6];
87 finger[1].y = ((buf[5] & 0x0007) << 8) | buf[7];
88 finger[1].t = buf[9];
89 }
90
91 return 0;
92}
93
94static irqreturn_t st1232_ts_irq_handler(int irq, void *dev_id)
95{
96 struct st1232_ts_data *ts = dev_id;
97 struct st1232_ts_finger *finger = ts->finger;
98 struct input_dev *input_dev = ts->input_dev;
99 int count = 0;
100 int i, ret;
101
102 ret = st1232_ts_read_data(ts);
103 if (ret < 0)
104 goto end;
105
106 /* multi touch protocol */
107 for (i = 0; i < MAX_FINGERS; i++) {
108 if (!finger[i].is_valid)
109 continue;
110
111 input_report_abs(input_dev, ABS_MT_TOUCH_MAJOR, finger[i].t);
112 input_report_abs(input_dev, ABS_MT_POSITION_X, finger[i].x);
113 input_report_abs(input_dev, ABS_MT_POSITION_Y, finger[i].y);
114 input_mt_sync(input_dev);
115 count++;
116 }
117
118 /* SYN_MT_REPORT only if no contact */
Rafael J. Wysocki9ee27ff2011-12-23 01:24:25 +0100119 if (!count) {
Tony SIM56a8bd62010-12-15 23:39:25 -0800120 input_mt_sync(input_dev);
Rafael J. Wysocki9ee27ff2011-12-23 01:24:25 +0100121 if (ts->low_latency_req.dev) {
122 dev_pm_qos_remove_request(&ts->low_latency_req);
123 ts->low_latency_req.dev = NULL;
124 }
125 } else if (!ts->low_latency_req.dev) {
126 /* First contact, request 100 us latency. */
127 dev_pm_qos_add_ancestor_request(&ts->client->dev,
Rafael J. Wysocki71d821f2014-02-11 00:36:00 +0100128 &ts->low_latency_req,
129 DEV_PM_QOS_RESUME_LATENCY, 100);
Rafael J. Wysocki9ee27ff2011-12-23 01:24:25 +0100130 }
Tony SIM56a8bd62010-12-15 23:39:25 -0800131
132 /* SYN_REPORT */
133 input_sync(input_dev);
134
135end:
136 return IRQ_HANDLED;
137}
138
Bastian Hechte6a90812013-04-15 09:31:00 -0700139static void st1232_ts_power(struct st1232_ts_data *ts, bool poweron)
140{
141 if (gpio_is_valid(ts->reset_gpio))
142 gpio_direction_output(ts->reset_gpio, poweron);
143}
144
Bill Pemberton5298cc42012-11-23 21:38:25 -0800145static int st1232_ts_probe(struct i2c_client *client,
Geert Uytterhoeven4a1a57d2017-10-31 09:47:28 -0700146 const struct i2c_device_id *id)
Tony SIM56a8bd62010-12-15 23:39:25 -0800147{
148 struct st1232_ts_data *ts;
149 struct input_dev *input_dev;
150 int error;
151
152 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
153 dev_err(&client->dev, "need I2C_FUNC_I2C\n");
154 return -EIO;
155 }
156
157 if (!client->irq) {
158 dev_err(&client->dev, "no IRQ?\n");
159 return -EINVAL;
160 }
161
Laurent Pinchart95b24d22013-04-15 09:23:00 -0700162 ts = devm_kzalloc(&client->dev, sizeof(*ts), GFP_KERNEL);
163 if (!ts)
164 return -ENOMEM;
Tony SIM56a8bd62010-12-15 23:39:25 -0800165
Laurent Pinchart95b24d22013-04-15 09:23:00 -0700166 input_dev = devm_input_allocate_device(&client->dev);
167 if (!input_dev)
168 return -ENOMEM;
Tony SIM56a8bd62010-12-15 23:39:25 -0800169
170 ts->client = client;
171 ts->input_dev = input_dev;
172
Geert Uytterhoeven4a1a57d2017-10-31 09:47:28 -0700173 ts->reset_gpio = of_get_gpio(client->dev.of_node, 0);
Bastian Hechte6a90812013-04-15 09:31:00 -0700174 if (gpio_is_valid(ts->reset_gpio)) {
175 error = devm_gpio_request(&client->dev, ts->reset_gpio, NULL);
176 if (error) {
177 dev_err(&client->dev,
178 "Unable to request GPIO pin %d.\n",
179 ts->reset_gpio);
180 return error;
181 }
182 }
183
184 st1232_ts_power(ts, true);
185
Tony SIM56a8bd62010-12-15 23:39:25 -0800186 input_dev->name = "st1232-touchscreen";
187 input_dev->id.bustype = BUS_I2C;
188 input_dev->dev.parent = &client->dev;
189
Martin Kepplinger20bbb312018-10-05 11:44:45 -0700190 __set_bit(INPUT_PROP_DIRECT, input_dev->propbit);
Tony SIM56a8bd62010-12-15 23:39:25 -0800191 __set_bit(EV_SYN, input_dev->evbit);
192 __set_bit(EV_KEY, input_dev->evbit);
193 __set_bit(EV_ABS, input_dev->evbit);
194
195 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR, 0, MAX_AREA, 0, 0);
196 input_set_abs_params(input_dev, ABS_MT_POSITION_X, MIN_X, MAX_X, 0, 0);
197 input_set_abs_params(input_dev, ABS_MT_POSITION_Y, MIN_Y, MAX_Y, 0, 0);
198
Laurent Pinchart95b24d22013-04-15 09:23:00 -0700199 error = devm_request_threaded_irq(&client->dev, client->irq,
200 NULL, st1232_ts_irq_handler,
201 IRQF_ONESHOT,
202 client->name, ts);
Tony SIM56a8bd62010-12-15 23:39:25 -0800203 if (error) {
204 dev_err(&client->dev, "Failed to register interrupt\n");
Laurent Pinchart95b24d22013-04-15 09:23:00 -0700205 return error;
Tony SIM56a8bd62010-12-15 23:39:25 -0800206 }
207
208 error = input_register_device(ts->input_dev);
209 if (error) {
210 dev_err(&client->dev, "Unable to register %s input device\n",
211 input_dev->name);
Laurent Pinchart95b24d22013-04-15 09:23:00 -0700212 return error;
Tony SIM56a8bd62010-12-15 23:39:25 -0800213 }
214
215 i2c_set_clientdata(client, ts);
216 device_init_wakeup(&client->dev, 1);
217
218 return 0;
Tony SIM56a8bd62010-12-15 23:39:25 -0800219}
220
Bill Pembertone2619cf2012-11-23 21:50:47 -0800221static int st1232_ts_remove(struct i2c_client *client)
Tony SIM56a8bd62010-12-15 23:39:25 -0800222{
Bastian Hechte6a90812013-04-15 09:31:00 -0700223 struct st1232_ts_data *ts = i2c_get_clientdata(client);
224
Bastian Hechte6a90812013-04-15 09:31:00 -0700225 st1232_ts_power(ts, false);
Tony SIM56a8bd62010-12-15 23:39:25 -0800226
227 return 0;
228}
229
Jingoo Han02b6a582014-11-02 00:04:14 -0700230static int __maybe_unused st1232_ts_suspend(struct device *dev)
Tony SIM56a8bd62010-12-15 23:39:25 -0800231{
232 struct i2c_client *client = to_i2c_client(dev);
Bastian Hechte6a90812013-04-15 09:31:00 -0700233 struct st1232_ts_data *ts = i2c_get_clientdata(client);
Tony SIM56a8bd62010-12-15 23:39:25 -0800234
Bastian Hechte6a90812013-04-15 09:31:00 -0700235 if (device_may_wakeup(&client->dev)) {
Tony SIM56a8bd62010-12-15 23:39:25 -0800236 enable_irq_wake(client->irq);
Bastian Hechte6a90812013-04-15 09:31:00 -0700237 } else {
Tony SIM56a8bd62010-12-15 23:39:25 -0800238 disable_irq(client->irq);
Bastian Hechte6a90812013-04-15 09:31:00 -0700239 st1232_ts_power(ts, false);
240 }
Tony SIM56a8bd62010-12-15 23:39:25 -0800241
242 return 0;
243}
244
Jingoo Han02b6a582014-11-02 00:04:14 -0700245static int __maybe_unused st1232_ts_resume(struct device *dev)
Tony SIM56a8bd62010-12-15 23:39:25 -0800246{
247 struct i2c_client *client = to_i2c_client(dev);
Bastian Hechte6a90812013-04-15 09:31:00 -0700248 struct st1232_ts_data *ts = i2c_get_clientdata(client);
Tony SIM56a8bd62010-12-15 23:39:25 -0800249
Bastian Hechte6a90812013-04-15 09:31:00 -0700250 if (device_may_wakeup(&client->dev)) {
Tony SIM56a8bd62010-12-15 23:39:25 -0800251 disable_irq_wake(client->irq);
Bastian Hechte6a90812013-04-15 09:31:00 -0700252 } else {
253 st1232_ts_power(ts, true);
Tony SIM56a8bd62010-12-15 23:39:25 -0800254 enable_irq(client->irq);
Bastian Hechte6a90812013-04-15 09:31:00 -0700255 }
Tony SIM56a8bd62010-12-15 23:39:25 -0800256
257 return 0;
258}
259
Dmitry Torokhovb3571402012-04-03 11:30:28 -0700260static SIMPLE_DEV_PM_OPS(st1232_ts_pm_ops,
261 st1232_ts_suspend, st1232_ts_resume);
262
Tony SIM56a8bd62010-12-15 23:39:25 -0800263static const struct i2c_device_id st1232_ts_id[] = {
264 { ST1232_TS_NAME, 0 },
265 { }
266};
267MODULE_DEVICE_TABLE(i2c, st1232_ts_id);
268
Bill Pemberton78f50c22012-11-23 21:31:00 -0800269static const struct of_device_id st1232_ts_dt_ids[] = {
Magnus Damme6293d22012-04-03 11:30:28 -0700270 { .compatible = "sitronix,st1232", },
271 { }
272};
273MODULE_DEVICE_TABLE(of, st1232_ts_dt_ids);
Magnus Damme6293d22012-04-03 11:30:28 -0700274
Tony SIM56a8bd62010-12-15 23:39:25 -0800275static struct i2c_driver st1232_ts_driver = {
276 .probe = st1232_ts_probe,
Bill Pemberton1cb0aa82012-11-23 21:27:39 -0800277 .remove = st1232_ts_remove,
Tony SIM56a8bd62010-12-15 23:39:25 -0800278 .id_table = st1232_ts_id,
279 .driver = {
280 .name = ST1232_TS_NAME,
Geert Uytterhoeven4a1a57d2017-10-31 09:47:28 -0700281 .of_match_table = st1232_ts_dt_ids,
Tony SIM56a8bd62010-12-15 23:39:25 -0800282 .pm = &st1232_ts_pm_ops,
Tony SIM56a8bd62010-12-15 23:39:25 -0800283 },
284};
285
Axel Lin1b92c1c2012-03-16 23:05:41 -0700286module_i2c_driver(st1232_ts_driver);
Tony SIM56a8bd62010-12-15 23:39:25 -0800287
288MODULE_AUTHOR("Tony SIM <chinyeow.sim.xt@renesas.com>");
289MODULE_DESCRIPTION("SITRONIX ST1232 Touchscreen Controller Driver");
Kuninori Morimoto4aeba642018-11-12 11:30:06 -0800290MODULE_LICENSE("GPL v2");