1 // SPDX-License-Identifier: GPL-2.0
2 /*
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.
11 */
12
13 #include <linux/delay.h>
14 #include <linux/gpio/consumer.h>
15 #include <linux/i2c.h>
16 #include <linux/input.h>
17 #include <linux/input/mt.h>
18 #include <linux/input/touchscreen.h>
19 #include <linux/interrupt.h>
20 #include <linux/module.h>
21 #include <linux/of.h>
22 #include <linux/pm_qos.h>
23 #include <linux/slab.h>
24 #include <linux/types.h>
25 #include <linux/input/touch-overlay.h>
26
27 #define ST1232_TS_NAME "st1232-ts"
28 #define ST1633_TS_NAME "st1633-ts"
29
30 #define REG_STATUS 0x01 /* Device Status | Error Code */
31
32 #define STATUS_NORMAL 0x00
33 #define STATUS_INIT 0x01
34 #define STATUS_ERROR 0x02
35 #define STATUS_AUTO_TUNING 0x03
36 #define STATUS_IDLE 0x04
37 #define STATUS_POWER_DOWN 0x05
38
39 #define ERROR_NONE 0x00
40 #define ERROR_INVALID_ADDRESS 0x10
41 #define ERROR_INVALID_VALUE 0x20
42 #define ERROR_INVALID_PLATFORM 0x30
43
44 #define REG_XY_RESOLUTION 0x04
45 #define REG_XY_COORDINATES 0x12
46 #define ST_TS_MAX_FINGERS 10
47
48 struct st_chip_info {
49 bool have_z;
50 u16 max_area;
51 u16 max_fingers;
52 };
53
54 struct st1232_ts_data {
55 struct i2c_client *client;
56 struct input_dev *input_dev;
57 struct touchscreen_properties prop;
58 struct dev_pm_qos_request low_latency_req;
59 struct gpio_desc *reset_gpio;
60 const struct st_chip_info *chip_info;
61 struct list_head touch_overlay_list;
62 int read_buf_len;
63 u8 *read_buf;
64 };
65
st1232_ts_read_data(struct st1232_ts_data * ts,u8 reg,unsigned int n)66 static int st1232_ts_read_data(struct st1232_ts_data *ts, u8 reg,
67 unsigned int n)
68 {
69 struct i2c_client *client = ts->client;
70 struct i2c_msg msg[] = {
71 {
72 .addr = client->addr,
73 .len = sizeof(reg),
74 .buf = ®,
75 },
76 {
77 .addr = client->addr,
78 .flags = I2C_M_RD | I2C_M_DMA_SAFE,
79 .len = n,
80 .buf = ts->read_buf,
81 }
82 };
83 int ret;
84
85 ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
86 if (ret != ARRAY_SIZE(msg))
87 return ret < 0 ? ret : -EIO;
88
89 return 0;
90 }
91
st1232_ts_wait_ready(struct st1232_ts_data * ts)92 static int st1232_ts_wait_ready(struct st1232_ts_data *ts)
93 {
94 unsigned int retries;
95 int error;
96
97 for (retries = 100; retries; retries--) {
98 error = st1232_ts_read_data(ts, REG_STATUS, 1);
99 if (!error) {
100 switch (ts->read_buf[0]) {
101 case STATUS_NORMAL | ERROR_NONE:
102 case STATUS_IDLE | ERROR_NONE:
103 return 0;
104 }
105 }
106
107 usleep_range(1000, 2000);
108 }
109
110 return -ENXIO;
111 }
112
st1232_ts_read_resolution(struct st1232_ts_data * ts,u16 * max_x,u16 * max_y)113 static int st1232_ts_read_resolution(struct st1232_ts_data *ts, u16 *max_x,
114 u16 *max_y)
115 {
116 u8 *buf;
117 int error;
118
119 /* select resolution register */
120 error = st1232_ts_read_data(ts, REG_XY_RESOLUTION, 3);
121 if (error)
122 return error;
123
124 buf = ts->read_buf;
125
126 *max_x = (((buf[0] & 0x0070) << 4) | buf[1]) - 1;
127 *max_y = (((buf[0] & 0x0007) << 8) | buf[2]) - 1;
128
129 return 0;
130 }
131
st1232_ts_parse_and_report(struct st1232_ts_data * ts)132 static int st1232_ts_parse_and_report(struct st1232_ts_data *ts)
133 {
134 struct input_dev *input = ts->input_dev;
135 struct input_mt_pos pos[ST_TS_MAX_FINGERS];
136 u8 z[ST_TS_MAX_FINGERS];
137 int slots[ST_TS_MAX_FINGERS];
138 int n_contacts = 0;
139 int i;
140
141 for (i = 0; i < ts->chip_info->max_fingers; i++) {
142 u8 *buf = &ts->read_buf[i * 4];
143
144 if (buf[0] & BIT(7)) {
145 unsigned int x = ((buf[0] & 0x70) << 4) | buf[1];
146 unsigned int y = ((buf[0] & 0x07) << 8) | buf[2];
147
148 touchscreen_set_mt_pos(&pos[n_contacts],
149 &ts->prop, x, y);
150
151 /* st1232 includes a z-axis / touch strength */
152 if (ts->chip_info->have_z)
153 z[n_contacts] = ts->read_buf[i + 6];
154
155 n_contacts++;
156 }
157 }
158
159 input_mt_assign_slots(input, slots, pos, n_contacts, 0);
160 for (i = 0; i < n_contacts; i++) {
161 if (touch_overlay_process_contact(&ts->touch_overlay_list,
162 input, &pos[i], slots[i]))
163 continue;
164
165 input_mt_slot(input, slots[i]);
166 input_mt_report_slot_state(input, MT_TOOL_FINGER, true);
167 input_report_abs(input, ABS_MT_POSITION_X, pos[i].x);
168 input_report_abs(input, ABS_MT_POSITION_Y, pos[i].y);
169 if (ts->chip_info->have_z)
170 input_report_abs(input, ABS_MT_TOUCH_MAJOR, z[i]);
171 }
172
173 touch_overlay_sync_frame(&ts->touch_overlay_list, input);
174 input_mt_sync_frame(input);
175 input_sync(input);
176
177 return n_contacts;
178 }
179
st1232_ts_irq_handler(int irq,void * dev_id)180 static irqreturn_t st1232_ts_irq_handler(int irq, void *dev_id)
181 {
182 struct st1232_ts_data *ts = dev_id;
183 int count;
184 int error;
185
186 error = st1232_ts_read_data(ts, REG_XY_COORDINATES, ts->read_buf_len);
187 if (error)
188 goto out;
189
190 count = st1232_ts_parse_and_report(ts);
191 if (!count) {
192 if (ts->low_latency_req.dev) {
193 dev_pm_qos_remove_request(&ts->low_latency_req);
194 ts->low_latency_req.dev = NULL;
195 }
196 } else if (!ts->low_latency_req.dev) {
197 /* First contact, request 100 us latency. */
198 dev_pm_qos_add_ancestor_request(&ts->client->dev,
199 &ts->low_latency_req,
200 DEV_PM_QOS_RESUME_LATENCY, 100);
201 }
202
203 out:
204 return IRQ_HANDLED;
205 }
206
st1232_ts_power(struct st1232_ts_data * ts,bool poweron)207 static void st1232_ts_power(struct st1232_ts_data *ts, bool poweron)
208 {
209 if (ts->reset_gpio)
210 gpiod_set_value_cansleep(ts->reset_gpio, !poweron);
211 }
212
st1232_ts_power_off(void * data)213 static void st1232_ts_power_off(void *data)
214 {
215 st1232_ts_power(data, false);
216 }
217
218 static const struct st_chip_info st1232_chip_info = {
219 .have_z = true,
220 .max_area = 0xff,
221 .max_fingers = 2,
222 };
223
224 static const struct st_chip_info st1633_chip_info = {
225 .have_z = false,
226 .max_area = 0x00,
227 .max_fingers = 5,
228 };
229
st1232_ts_probe(struct i2c_client * client)230 static int st1232_ts_probe(struct i2c_client *client)
231 {
232 const struct i2c_device_id *id = i2c_client_get_device_id(client);
233 const struct st_chip_info *match;
234 struct st1232_ts_data *ts;
235 struct input_dev *input_dev;
236 u16 max_x, max_y;
237 int error;
238
239 match = device_get_match_data(&client->dev);
240 if (!match && id)
241 match = (const void *)id->driver_data;
242 if (!match) {
243 dev_err(&client->dev, "unknown device model\n");
244 return -ENODEV;
245 }
246
247 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
248 dev_err(&client->dev, "need I2C_FUNC_I2C\n");
249 return -EIO;
250 }
251
252 if (!client->irq) {
253 dev_err(&client->dev, "no IRQ?\n");
254 return -EINVAL;
255 }
256
257 ts = devm_kzalloc(&client->dev, sizeof(*ts), GFP_KERNEL);
258 if (!ts)
259 return -ENOMEM;
260
261 ts->chip_info = match;
262
263 /* allocate a buffer according to the number of registers to read */
264 ts->read_buf_len = ts->chip_info->max_fingers * 4;
265 ts->read_buf = devm_kzalloc(&client->dev, ts->read_buf_len, GFP_KERNEL);
266 if (!ts->read_buf)
267 return -ENOMEM;
268
269 input_dev = devm_input_allocate_device(&client->dev);
270 if (!input_dev)
271 return -ENOMEM;
272
273 ts->client = client;
274 ts->input_dev = input_dev;
275
276 ts->reset_gpio = devm_gpiod_get_optional(&client->dev, NULL,
277 GPIOD_OUT_HIGH);
278 if (IS_ERR(ts->reset_gpio)) {
279 error = PTR_ERR(ts->reset_gpio);
280 dev_err(&client->dev, "Unable to request GPIO pin: %d.\n",
281 error);
282 return error;
283 }
284
285 st1232_ts_power(ts, true);
286
287 error = devm_add_action_or_reset(&client->dev, st1232_ts_power_off, ts);
288 if (error) {
289 dev_err(&client->dev,
290 "Failed to install power off action: %d\n", error);
291 return error;
292 }
293
294 input_dev->name = "st1232-touchscreen";
295 input_dev->id.bustype = BUS_I2C;
296
297 /* Wait until device is ready */
298 error = st1232_ts_wait_ready(ts);
299 if (error)
300 return error;
301
302 if (ts->chip_info->have_z)
303 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR, 0,
304 ts->chip_info->max_area, 0, 0);
305
306 /* map overlay objects if defined in the device tree */
307 INIT_LIST_HEAD(&ts->touch_overlay_list);
308 error = touch_overlay_map(&ts->touch_overlay_list, input_dev);
309 if (error)
310 return error;
311
312 if (touch_overlay_mapped_touchscreen(&ts->touch_overlay_list)) {
313 /* Read resolution from the overlay touchscreen if defined */
314 touch_overlay_get_touchscreen_abs(&ts->touch_overlay_list,
315 &max_x, &max_y);
316 } else {
317 /* Read resolution from the chip */
318 error = st1232_ts_read_resolution(ts, &max_x, &max_y);
319 if (error) {
320 dev_err(&client->dev,
321 "Failed to read resolution: %d\n", error);
322 return error;
323 }
324 }
325
326 input_set_abs_params(input_dev, ABS_MT_POSITION_X,
327 0, max_x, 0, 0);
328 input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
329 0, max_y, 0, 0);
330
331 touchscreen_parse_properties(input_dev, true, &ts->prop);
332
333 error = input_mt_init_slots(input_dev, ts->chip_info->max_fingers,
334 INPUT_MT_DIRECT | INPUT_MT_TRACK |
335 INPUT_MT_DROP_UNUSED);
336 if (error) {
337 dev_err(&client->dev, "failed to initialize MT slots\n");
338 return error;
339 }
340
341 error = devm_request_threaded_irq(&client->dev, client->irq,
342 NULL, st1232_ts_irq_handler,
343 IRQF_ONESHOT,
344 client->name, ts);
345 if (error) {
346 dev_err(&client->dev, "Failed to register interrupt\n");
347 return error;
348 }
349
350 error = input_register_device(ts->input_dev);
351 if (error) {
352 dev_err(&client->dev, "Unable to register %s input device\n",
353 input_dev->name);
354 return error;
355 }
356
357 i2c_set_clientdata(client, ts);
358
359 return 0;
360 }
361
st1232_ts_suspend(struct device * dev)362 static int st1232_ts_suspend(struct device *dev)
363 {
364 struct i2c_client *client = to_i2c_client(dev);
365 struct st1232_ts_data *ts = i2c_get_clientdata(client);
366
367 disable_irq(client->irq);
368
369 if (!device_may_wakeup(&client->dev))
370 st1232_ts_power(ts, false);
371
372 return 0;
373 }
374
st1232_ts_resume(struct device * dev)375 static int st1232_ts_resume(struct device *dev)
376 {
377 struct i2c_client *client = to_i2c_client(dev);
378 struct st1232_ts_data *ts = i2c_get_clientdata(client);
379
380 if (!device_may_wakeup(&client->dev))
381 st1232_ts_power(ts, true);
382
383 enable_irq(client->irq);
384
385 return 0;
386 }
387
388 static DEFINE_SIMPLE_DEV_PM_OPS(st1232_ts_pm_ops,
389 st1232_ts_suspend, st1232_ts_resume);
390
391 static const struct i2c_device_id st1232_ts_id[] = {
392 { ST1232_TS_NAME, (unsigned long)&st1232_chip_info },
393 { ST1633_TS_NAME, (unsigned long)&st1633_chip_info },
394 { }
395 };
396 MODULE_DEVICE_TABLE(i2c, st1232_ts_id);
397
398 static const struct of_device_id st1232_ts_dt_ids[] = {
399 { .compatible = "sitronix,st1232", .data = &st1232_chip_info },
400 { .compatible = "sitronix,st1633", .data = &st1633_chip_info },
401 { }
402 };
403 MODULE_DEVICE_TABLE(of, st1232_ts_dt_ids);
404
405 static struct i2c_driver st1232_ts_driver = {
406 .probe = st1232_ts_probe,
407 .id_table = st1232_ts_id,
408 .driver = {
409 .name = ST1232_TS_NAME,
410 .of_match_table = st1232_ts_dt_ids,
411 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
412 .pm = pm_sleep_ptr(&st1232_ts_pm_ops),
413 },
414 };
415
416 module_i2c_driver(st1232_ts_driver);
417
418 MODULE_AUTHOR("Tony SIM <chinyeow.sim.xt@renesas.com>");
419 MODULE_AUTHOR("Martin Kepplinger <martin.kepplinger@ginzinger.com>");
420 MODULE_DESCRIPTION("SITRONIX ST1232 Touchscreen Controller Driver");
421 MODULE_LICENSE("GPL v2");
422