1 // SPDX-License-Identifier: GPL-2.0-only
3 * Driver for Semtech SX8654 I2C touchscreen controller.
5 * Copyright (c) 2015 Armadeus Systems
6 * Sébastien Szymanski <sebastien.szymanski@armadeus.com>
10 * Copyright (c) 2013 U-MoBo Srl
11 * Pierluigi Passaro <p.passaro@u-mobo.com>
13 * Copyright (c) 2009 Wayne Roberts
15 * Copyright (c) 2008 Kwangwoo Lee
17 * Copyright (c) 2005 David Brownell
18 * Copyright (c) 2006 Nokia Corporation
20 * Copyright (C) 2004-2005 Richard Purdie
21 * - omap_ts.[hc], ads7846.h, ts_osk.c
22 * Copyright (C) 2002 MontaVista Software
23 * Copyright (C) 2004 Texas Instruments
24 * Copyright (C) 2005 Dirk Behme
27 #include <linux/bitops.h>
28 #include <linux/delay.h>
29 #include <linux/gpio/consumer.h>
30 #include <linux/i2c.h>
31 #include <linux/input.h>
32 #include <linux/input/touchscreen.h>
33 #include <linux/interrupt.h>
34 #include <linux/irq.h>
35 #include <linux/module.h>
38 /* register addresses */
39 #define I2C_REG_TOUCH0 0x00
40 #define I2C_REG_TOUCH1 0x01
41 #define I2C_REG_CHANMASK 0x04
42 #define I2C_REG_IRQMASK 0x22
43 #define I2C_REG_IRQSRC 0x23
44 #define I2C_REG_SOFTRESET 0x3f
46 #define I2C_REG_SX8650_STAT 0x05
47 #define SX8650_STAT_CONVIRQ BIT(7)
50 #define CMD_READ_REGISTER 0x40
51 #define CMD_PENTRG 0xe0
53 /* value for I2C_REG_SOFTRESET */
54 #define SOFTRESET_VALUE 0xde
56 /* bits for I2C_REG_IRQSRC */
57 #define IRQ_PENTOUCH_TOUCHCONVDONE BIT(3)
58 #define IRQ_PENRELEASE BIT(2)
60 /* bits for RegTouch1 */
62 #define RPDNT_100K 0x00
65 /* bits for I2C_REG_CHANMASK */
69 /* coordinates rate: higher nibble of CTRL0 register */
70 #define RATE_MANUAL 0x00
71 #define RATE_5000CPS 0xf0
73 /* power delay: lower nibble of CTRL0 register */
74 #define POWDLY_1_1MS 0x0b
76 /* for sx8650, as we have no pen release IRQ there: timeout in ns following the
77 * last PENIRQ after which we assume the pen is lifted.
79 #define SX8650_PENIRQ_TIMEOUT msecs_to_jiffies(10)
81 #define MAX_12BIT ((1 << 12) - 1)
82 #define MAX_I2C_READ_LEN 10 /* see datasheet section 5.1.5 */
84 /* channel definition */
91 bool has_irq_penrelease
;
97 struct input_dev
*input
;
98 struct i2c_client
*client
;
99 struct gpio_desc
*gpio_reset
;
101 spinlock_t lock
; /* for input reporting from irq/timer */
102 struct timer_list timer
;
104 struct touchscreen_properties props
;
106 const struct sx865x_data
*data
;
109 static inline void sx865x_penrelease(struct sx8654
*ts
)
111 struct input_dev
*input_dev
= ts
->input
;
113 input_report_key(input_dev
, BTN_TOUCH
, 0);
114 input_sync(input_dev
);
117 static void sx865x_penrelease_timer_handler(struct timer_list
*t
)
119 struct sx8654
*ts
= from_timer(ts
, t
, timer
);
122 spin_lock_irqsave(&ts
->lock
, flags
);
123 sx865x_penrelease(ts
);
124 spin_unlock_irqrestore(&ts
->lock
, flags
);
125 dev_dbg(&ts
->client
->dev
, "penrelease by timer\n");
128 static irqreturn_t
sx8650_irq(int irq
, void *handle
)
130 struct sx8654
*ts
= handle
;
131 struct device
*dev
= &ts
->client
->dev
;
138 __be16 data
[MAX_I2C_READ_LEN
/ sizeof(__be16
)];
139 u8 nchan
= hweight32(ts
->data
->chan_mask
);
140 u8 readlen
= nchan
* sizeof(*data
);
142 stat
= i2c_smbus_read_byte_data(ts
->client
, CMD_READ_REGISTER
143 | I2C_REG_SX8650_STAT
);
145 if (!(stat
& SX8650_STAT_CONVIRQ
)) {
146 dev_dbg(dev
, "%s ignore stat [0x%02x]", __func__
, stat
);
150 len
= i2c_master_recv(ts
->client
, (u8
*)data
, readlen
);
151 if (len
!= readlen
) {
152 dev_dbg(dev
, "ignore short recv (%d)\n", len
);
156 spin_lock_irqsave(&ts
->lock
, flags
);
160 for (i
= 0; i
< nchan
; i
++) {
161 chdata
= be16_to_cpu(data
[i
]);
163 if (unlikely(chdata
== 0xFFFF)) {
164 dev_dbg(dev
, "invalid qualified data @ %d\n", i
);
166 } else if (unlikely(chdata
& 0x8000)) {
167 dev_warn(dev
, "hibit @ %d [0x%04x]\n", i
, chdata
);
173 x
= chdata
& MAX_12BIT
;
175 y
= chdata
& MAX_12BIT
;
177 dev_warn(dev
, "unknown channel %d [0x%04x]\n", ch
,
181 touchscreen_report_pos(ts
->input
, &ts
->props
, x
, y
, false);
182 input_report_key(ts
->input
, BTN_TOUCH
, 1);
183 input_sync(ts
->input
);
184 dev_dbg(dev
, "point(%4d,%4d)\n", x
, y
);
186 mod_timer(&ts
->timer
, jiffies
+ SX8650_PENIRQ_TIMEOUT
);
187 spin_unlock_irqrestore(&ts
->lock
, flags
);
192 static irqreturn_t
sx8654_irq(int irq
, void *handle
)
194 struct sx8654
*sx8654
= handle
;
200 irqsrc
= i2c_smbus_read_byte_data(sx8654
->client
,
201 CMD_READ_REGISTER
| I2C_REG_IRQSRC
);
202 dev_dbg(&sx8654
->client
->dev
, "irqsrc = 0x%x", irqsrc
);
207 if (irqsrc
& IRQ_PENRELEASE
) {
208 dev_dbg(&sx8654
->client
->dev
, "pen release interrupt");
210 input_report_key(sx8654
->input
, BTN_TOUCH
, 0);
211 input_sync(sx8654
->input
);
214 if (irqsrc
& IRQ_PENTOUCH_TOUCHCONVDONE
) {
215 dev_dbg(&sx8654
->client
->dev
, "pen touch interrupt");
217 retval
= i2c_master_recv(sx8654
->client
, data
, sizeof(data
));
218 if (retval
!= sizeof(data
))
222 if (unlikely(data
[0] & 0x80 || data
[2] & 0x80))
225 x
= ((data
[0] & 0xf) << 8) | (data
[1]);
226 y
= ((data
[2] & 0xf) << 8) | (data
[3]);
228 touchscreen_report_pos(sx8654
->input
, &sx8654
->props
, x
, y
,
230 input_report_key(sx8654
->input
, BTN_TOUCH
, 1);
231 input_sync(sx8654
->input
);
233 dev_dbg(&sx8654
->client
->dev
, "point(%4d,%4d)\n", x
, y
);
240 static int sx8654_reset(struct sx8654
*ts
)
244 if (ts
->gpio_reset
) {
245 gpiod_set_value_cansleep(ts
->gpio_reset
, 1);
246 udelay(2); /* Tpulse > 1µs */
247 gpiod_set_value_cansleep(ts
->gpio_reset
, 0);
249 dev_dbg(&ts
->client
->dev
, "NRST unavailable, try softreset\n");
250 err
= i2c_smbus_write_byte_data(ts
->client
, I2C_REG_SOFTRESET
,
259 static int sx8654_open(struct input_dev
*dev
)
261 struct sx8654
*sx8654
= input_get_drvdata(dev
);
262 struct i2c_client
*client
= sx8654
->client
;
265 /* enable pen trigger mode */
266 error
= i2c_smbus_write_byte_data(client
, I2C_REG_TOUCH0
,
267 RATE_5000CPS
| POWDLY_1_1MS
);
269 dev_err(&client
->dev
, "writing to I2C_REG_TOUCH0 failed");
273 error
= i2c_smbus_write_byte(client
, CMD_PENTRG
);
275 dev_err(&client
->dev
, "writing command CMD_PENTRG failed");
279 enable_irq(client
->irq
);
284 static void sx8654_close(struct input_dev
*dev
)
286 struct sx8654
*sx8654
= input_get_drvdata(dev
);
287 struct i2c_client
*client
= sx8654
->client
;
290 disable_irq(client
->irq
);
292 if (!sx8654
->data
->has_irq_penrelease
)
293 del_timer_sync(&sx8654
->timer
);
295 /* enable manual mode mode */
296 error
= i2c_smbus_write_byte(client
, sx8654
->data
->cmd_manual
);
298 dev_err(&client
->dev
, "writing command CMD_MANUAL failed");
302 error
= i2c_smbus_write_byte_data(client
, I2C_REG_TOUCH0
, RATE_MANUAL
);
304 dev_err(&client
->dev
, "writing to I2C_REG_TOUCH0 failed");
309 static int sx8654_probe(struct i2c_client
*client
,
310 const struct i2c_device_id
*id
)
312 struct sx8654
*sx8654
;
313 struct input_dev
*input
;
316 if (!i2c_check_functionality(client
->adapter
,
317 I2C_FUNC_SMBUS_READ_WORD_DATA
))
320 sx8654
= devm_kzalloc(&client
->dev
, sizeof(*sx8654
), GFP_KERNEL
);
324 sx8654
->gpio_reset
= devm_gpiod_get_optional(&client
->dev
, "reset",
326 if (IS_ERR(sx8654
->gpio_reset
)) {
327 error
= PTR_ERR(sx8654
->gpio_reset
);
328 if (error
!= -EPROBE_DEFER
)
329 dev_err(&client
->dev
, "unable to get reset-gpio: %d\n",
333 dev_dbg(&client
->dev
, "got GPIO reset pin\n");
335 sx8654
->data
= device_get_match_data(&client
->dev
);
337 sx8654
->data
= (const struct sx865x_data
*)id
->driver_data
;
339 dev_err(&client
->dev
, "invalid or missing device data\n");
343 if (!sx8654
->data
->has_irq_penrelease
) {
344 dev_dbg(&client
->dev
, "use timer for penrelease\n");
345 timer_setup(&sx8654
->timer
, sx865x_penrelease_timer_handler
, 0);
346 spin_lock_init(&sx8654
->lock
);
349 input
= devm_input_allocate_device(&client
->dev
);
353 input
->name
= "SX8654 I2C Touchscreen";
354 input
->id
.bustype
= BUS_I2C
;
355 input
->dev
.parent
= &client
->dev
;
356 input
->open
= sx8654_open
;
357 input
->close
= sx8654_close
;
359 __set_bit(INPUT_PROP_DIRECT
, input
->propbit
);
360 input_set_capability(input
, EV_KEY
, BTN_TOUCH
);
361 input_set_abs_params(input
, ABS_X
, 0, MAX_12BIT
, 0, 0);
362 input_set_abs_params(input
, ABS_Y
, 0, MAX_12BIT
, 0, 0);
364 touchscreen_parse_properties(input
, false, &sx8654
->props
);
366 sx8654
->client
= client
;
367 sx8654
->input
= input
;
369 input_set_drvdata(sx8654
->input
, sx8654
);
371 error
= sx8654_reset(sx8654
);
373 dev_err(&client
->dev
, "reset failed");
377 error
= i2c_smbus_write_byte_data(client
, I2C_REG_CHANMASK
,
378 sx8654
->data
->chan_mask
);
380 dev_err(&client
->dev
, "writing to I2C_REG_CHANMASK failed");
384 if (sx8654
->data
->has_reg_irqmask
) {
385 error
= i2c_smbus_write_byte_data(client
, I2C_REG_IRQMASK
,
386 IRQ_PENTOUCH_TOUCHCONVDONE
|
389 dev_err(&client
->dev
, "writing I2C_REG_IRQMASK failed");
394 error
= i2c_smbus_write_byte_data(client
, I2C_REG_TOUCH1
,
395 CONDIRQ
| RPDNT_100K
| FILT_7SA
);
397 dev_err(&client
->dev
, "writing to I2C_REG_TOUCH1 failed");
401 error
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
402 NULL
, sx8654
->data
->irqh
,
404 client
->name
, sx8654
);
406 dev_err(&client
->dev
,
407 "Failed to enable IRQ %d, error: %d\n",
412 /* Disable the IRQ, we'll enable it in sx8654_open() */
413 disable_irq(client
->irq
);
415 error
= input_register_device(sx8654
->input
);
422 static const struct sx865x_data sx8650_data
= {
424 .has_irq_penrelease
= false,
425 .has_reg_irqmask
= false,
426 .chan_mask
= (CONV_X
| CONV_Y
),
430 static const struct sx865x_data sx8654_data
= {
432 .has_irq_penrelease
= true,
433 .has_reg_irqmask
= true,
434 .chan_mask
= (CONV_X
| CONV_Y
),
439 static const struct of_device_id sx8654_of_match
[] = {
441 .compatible
= "semtech,sx8650",
442 .data
= &sx8650_data
,
444 .compatible
= "semtech,sx8654",
445 .data
= &sx8654_data
,
447 .compatible
= "semtech,sx8655",
448 .data
= &sx8654_data
,
450 .compatible
= "semtech,sx8656",
451 .data
= &sx8654_data
,
455 MODULE_DEVICE_TABLE(of
, sx8654_of_match
);
458 static const struct i2c_device_id sx8654_id_table
[] = {
459 { .name
= "semtech_sx8650", .driver_data
= (long)&sx8650_data
},
460 { .name
= "semtech_sx8654", .driver_data
= (long)&sx8654_data
},
461 { .name
= "semtech_sx8655", .driver_data
= (long)&sx8654_data
},
462 { .name
= "semtech_sx8656", .driver_data
= (long)&sx8654_data
},
465 MODULE_DEVICE_TABLE(i2c
, sx8654_id_table
);
467 static struct i2c_driver sx8654_driver
= {
470 .of_match_table
= of_match_ptr(sx8654_of_match
),
472 .id_table
= sx8654_id_table
,
473 .probe
= sx8654_probe
,
475 module_i2c_driver(sx8654_driver
);
477 MODULE_AUTHOR("Sébastien Szymanski <sebastien.szymanski@armadeus.com>");
478 MODULE_DESCRIPTION("Semtech SX8654 I2C Touchscreen Driver");
479 MODULE_LICENSE("GPL");