2 * Driver for Semtech SX8654 I2C touchscreen controller.
4 * Copyright (c) 2015 Armadeus Systems
5 * Sébastien Szymanski <sebastien.szymanski@armadeus.com>
9 * Copyright (c) 2013 U-MoBo Srl
10 * Pierluigi Passaro <p.passaro@u-mobo.com>
12 * Copyright (c) 2009 Wayne Roberts
14 * Copyright (c) 2008 Kwangwoo Lee
16 * Copyright (c) 2005 David Brownell
17 * Copyright (c) 2006 Nokia Corporation
19 * Copyright (C) 2004-2005 Richard Purdie
20 * - omap_ts.[hc], ads7846.h, ts_osk.c
21 * Copyright (C) 2002 MontaVista Software
22 * Copyright (C) 2004 Texas Instruments
23 * Copyright (C) 2005 Dirk Behme
25 * This program is free software; you can redistribute it and/or modify
26 * it under the terms of the GNU General Public License version 2 as
27 * published by the Free Software Foundation.
30 #include <linux/bitops.h>
31 #include <linux/delay.h>
32 #include <linux/gpio/consumer.h>
33 #include <linux/i2c.h>
34 #include <linux/input.h>
35 #include <linux/input/touchscreen.h>
36 #include <linux/interrupt.h>
37 #include <linux/irq.h>
38 #include <linux/module.h>
41 /* register addresses */
42 #define I2C_REG_TOUCH0 0x00
43 #define I2C_REG_TOUCH1 0x01
44 #define I2C_REG_CHANMASK 0x04
45 #define I2C_REG_IRQMASK 0x22
46 #define I2C_REG_IRQSRC 0x23
47 #define I2C_REG_SOFTRESET 0x3f
49 #define I2C_REG_SX8650_STAT 0x05
50 #define SX8650_STAT_CONVIRQ BIT(7)
53 #define CMD_READ_REGISTER 0x40
54 #define CMD_PENTRG 0xe0
56 /* value for I2C_REG_SOFTRESET */
57 #define SOFTRESET_VALUE 0xde
59 /* bits for I2C_REG_IRQSRC */
60 #define IRQ_PENTOUCH_TOUCHCONVDONE BIT(3)
61 #define IRQ_PENRELEASE BIT(2)
63 /* bits for RegTouch1 */
65 #define RPDNT_100K 0x00
68 /* bits for I2C_REG_CHANMASK */
72 /* coordinates rate: higher nibble of CTRL0 register */
73 #define RATE_MANUAL 0x00
74 #define RATE_5000CPS 0xf0
76 /* power delay: lower nibble of CTRL0 register */
77 #define POWDLY_1_1MS 0x0b
79 /* for sx8650, as we have no pen release IRQ there: timeout in ns following the
80 * last PENIRQ after which we assume the pen is lifted.
82 #define SX8650_PENIRQ_TIMEOUT msecs_to_jiffies(10)
84 #define MAX_12BIT ((1 << 12) - 1)
85 #define MAX_I2C_READ_LEN 10 /* see datasheet section 5.1.5 */
87 /* channel definition */
94 bool has_irq_penrelease
;
100 struct input_dev
*input
;
101 struct i2c_client
*client
;
102 struct gpio_desc
*gpio_reset
;
104 spinlock_t lock
; /* for input reporting from irq/timer */
105 struct timer_list timer
;
107 struct touchscreen_properties props
;
109 const struct sx865x_data
*data
;
112 static inline void sx865x_penrelease(struct sx8654
*ts
)
114 struct input_dev
*input_dev
= ts
->input
;
116 input_report_key(input_dev
, BTN_TOUCH
, 0);
117 input_sync(input_dev
);
120 static void sx865x_penrelease_timer_handler(struct timer_list
*t
)
122 struct sx8654
*ts
= from_timer(ts
, t
, timer
);
125 spin_lock_irqsave(&ts
->lock
, flags
);
126 sx865x_penrelease(ts
);
127 spin_unlock_irqrestore(&ts
->lock
, flags
);
128 dev_dbg(&ts
->client
->dev
, "penrelease by timer\n");
131 static irqreturn_t
sx8650_irq(int irq
, void *handle
)
133 struct sx8654
*ts
= handle
;
134 struct device
*dev
= &ts
->client
->dev
;
141 __be16 data
[MAX_I2C_READ_LEN
/ sizeof(__be16
)];
142 u8 nchan
= hweight32(ts
->data
->chan_mask
);
143 u8 readlen
= nchan
* sizeof(*data
);
145 stat
= i2c_smbus_read_byte_data(ts
->client
, CMD_READ_REGISTER
146 | I2C_REG_SX8650_STAT
);
148 if (!(stat
& SX8650_STAT_CONVIRQ
)) {
149 dev_dbg(dev
, "%s ignore stat [0x%02x]", __func__
, stat
);
153 len
= i2c_master_recv(ts
->client
, (u8
*)data
, readlen
);
154 if (len
!= readlen
) {
155 dev_dbg(dev
, "ignore short recv (%d)\n", len
);
159 spin_lock_irqsave(&ts
->lock
, flags
);
163 for (i
= 0; i
< nchan
; i
++) {
164 chdata
= be16_to_cpu(data
[i
]);
166 if (unlikely(chdata
== 0xFFFF)) {
167 dev_dbg(dev
, "invalid qualified data @ %d\n", i
);
169 } else if (unlikely(chdata
& 0x8000)) {
170 dev_warn(dev
, "hibit @ %d [0x%04x]\n", i
, chdata
);
176 x
= chdata
& MAX_12BIT
;
178 y
= chdata
& MAX_12BIT
;
180 dev_warn(dev
, "unknown channel %d [0x%04x]\n", ch
,
184 touchscreen_report_pos(ts
->input
, &ts
->props
, x
, y
, false);
185 input_report_key(ts
->input
, BTN_TOUCH
, 1);
186 input_sync(ts
->input
);
187 dev_dbg(dev
, "point(%4d,%4d)\n", x
, y
);
189 mod_timer(&ts
->timer
, jiffies
+ SX8650_PENIRQ_TIMEOUT
);
190 spin_unlock_irqrestore(&ts
->lock
, flags
);
195 static irqreturn_t
sx8654_irq(int irq
, void *handle
)
197 struct sx8654
*sx8654
= handle
;
203 irqsrc
= i2c_smbus_read_byte_data(sx8654
->client
,
204 CMD_READ_REGISTER
| I2C_REG_IRQSRC
);
205 dev_dbg(&sx8654
->client
->dev
, "irqsrc = 0x%x", irqsrc
);
210 if (irqsrc
& IRQ_PENRELEASE
) {
211 dev_dbg(&sx8654
->client
->dev
, "pen release interrupt");
213 input_report_key(sx8654
->input
, BTN_TOUCH
, 0);
214 input_sync(sx8654
->input
);
217 if (irqsrc
& IRQ_PENTOUCH_TOUCHCONVDONE
) {
218 dev_dbg(&sx8654
->client
->dev
, "pen touch interrupt");
220 retval
= i2c_master_recv(sx8654
->client
, data
, sizeof(data
));
221 if (retval
!= sizeof(data
))
225 if (unlikely(data
[0] & 0x80 || data
[2] & 0x80))
228 x
= ((data
[0] & 0xf) << 8) | (data
[1]);
229 y
= ((data
[2] & 0xf) << 8) | (data
[3]);
231 touchscreen_report_pos(sx8654
->input
, &sx8654
->props
, x
, y
,
233 input_report_key(sx8654
->input
, BTN_TOUCH
, 1);
234 input_sync(sx8654
->input
);
236 dev_dbg(&sx8654
->client
->dev
, "point(%4d,%4d)\n", x
, y
);
243 static int sx8654_reset(struct sx8654
*ts
)
247 if (ts
->gpio_reset
) {
248 gpiod_set_value_cansleep(ts
->gpio_reset
, 1);
249 udelay(2); /* Tpulse > 1µs */
250 gpiod_set_value_cansleep(ts
->gpio_reset
, 0);
252 dev_dbg(&ts
->client
->dev
, "NRST unavailable, try softreset\n");
253 err
= i2c_smbus_write_byte_data(ts
->client
, I2C_REG_SOFTRESET
,
262 static int sx8654_open(struct input_dev
*dev
)
264 struct sx8654
*sx8654
= input_get_drvdata(dev
);
265 struct i2c_client
*client
= sx8654
->client
;
268 /* enable pen trigger mode */
269 error
= i2c_smbus_write_byte_data(client
, I2C_REG_TOUCH0
,
270 RATE_5000CPS
| POWDLY_1_1MS
);
272 dev_err(&client
->dev
, "writing to I2C_REG_TOUCH0 failed");
276 error
= i2c_smbus_write_byte(client
, CMD_PENTRG
);
278 dev_err(&client
->dev
, "writing command CMD_PENTRG failed");
282 enable_irq(client
->irq
);
287 static void sx8654_close(struct input_dev
*dev
)
289 struct sx8654
*sx8654
= input_get_drvdata(dev
);
290 struct i2c_client
*client
= sx8654
->client
;
293 disable_irq(client
->irq
);
295 if (!sx8654
->data
->has_irq_penrelease
)
296 del_timer_sync(&sx8654
->timer
);
298 /* enable manual mode mode */
299 error
= i2c_smbus_write_byte(client
, sx8654
->data
->cmd_manual
);
301 dev_err(&client
->dev
, "writing command CMD_MANUAL failed");
305 error
= i2c_smbus_write_byte_data(client
, I2C_REG_TOUCH0
, RATE_MANUAL
);
307 dev_err(&client
->dev
, "writing to I2C_REG_TOUCH0 failed");
312 static int sx8654_probe(struct i2c_client
*client
,
313 const struct i2c_device_id
*id
)
315 struct sx8654
*sx8654
;
316 struct input_dev
*input
;
319 if (!i2c_check_functionality(client
->adapter
,
320 I2C_FUNC_SMBUS_READ_WORD_DATA
))
323 sx8654
= devm_kzalloc(&client
->dev
, sizeof(*sx8654
), GFP_KERNEL
);
327 sx8654
->gpio_reset
= devm_gpiod_get_optional(&client
->dev
, "reset",
329 if (IS_ERR(sx8654
->gpio_reset
)) {
330 error
= PTR_ERR(sx8654
->gpio_reset
);
331 if (error
!= -EPROBE_DEFER
)
332 dev_err(&client
->dev
, "unable to get reset-gpio: %d\n",
336 dev_dbg(&client
->dev
, "got GPIO reset pin\n");
338 sx8654
->data
= device_get_match_data(&client
->dev
);
340 sx8654
->data
= (const struct sx865x_data
*)id
->driver_data
;
342 dev_err(&client
->dev
, "invalid or missing device data\n");
346 if (!sx8654
->data
->has_irq_penrelease
) {
347 dev_dbg(&client
->dev
, "use timer for penrelease\n");
348 timer_setup(&sx8654
->timer
, sx865x_penrelease_timer_handler
, 0);
349 spin_lock_init(&sx8654
->lock
);
352 input
= devm_input_allocate_device(&client
->dev
);
356 input
->name
= "SX8654 I2C Touchscreen";
357 input
->id
.bustype
= BUS_I2C
;
358 input
->dev
.parent
= &client
->dev
;
359 input
->open
= sx8654_open
;
360 input
->close
= sx8654_close
;
362 __set_bit(INPUT_PROP_DIRECT
, input
->propbit
);
363 input_set_capability(input
, EV_KEY
, BTN_TOUCH
);
364 input_set_abs_params(input
, ABS_X
, 0, MAX_12BIT
, 0, 0);
365 input_set_abs_params(input
, ABS_Y
, 0, MAX_12BIT
, 0, 0);
367 touchscreen_parse_properties(input
, false, &sx8654
->props
);
369 sx8654
->client
= client
;
370 sx8654
->input
= input
;
372 input_set_drvdata(sx8654
->input
, sx8654
);
374 error
= sx8654_reset(sx8654
);
376 dev_err(&client
->dev
, "reset failed");
380 error
= i2c_smbus_write_byte_data(client
, I2C_REG_CHANMASK
,
381 sx8654
->data
->chan_mask
);
383 dev_err(&client
->dev
, "writing to I2C_REG_CHANMASK failed");
387 if (sx8654
->data
->has_reg_irqmask
) {
388 error
= i2c_smbus_write_byte_data(client
, I2C_REG_IRQMASK
,
389 IRQ_PENTOUCH_TOUCHCONVDONE
|
392 dev_err(&client
->dev
, "writing I2C_REG_IRQMASK failed");
397 error
= i2c_smbus_write_byte_data(client
, I2C_REG_TOUCH1
,
398 CONDIRQ
| RPDNT_100K
| FILT_7SA
);
400 dev_err(&client
->dev
, "writing to I2C_REG_TOUCH1 failed");
404 error
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
405 NULL
, sx8654
->data
->irqh
,
407 client
->name
, sx8654
);
409 dev_err(&client
->dev
,
410 "Failed to enable IRQ %d, error: %d\n",
415 /* Disable the IRQ, we'll enable it in sx8654_open() */
416 disable_irq(client
->irq
);
418 error
= input_register_device(sx8654
->input
);
425 static const struct sx865x_data sx8650_data
= {
427 .has_irq_penrelease
= false,
428 .has_reg_irqmask
= false,
429 .chan_mask
= (CONV_X
| CONV_Y
),
433 static const struct sx865x_data sx8654_data
= {
435 .has_irq_penrelease
= true,
436 .has_reg_irqmask
= true,
437 .chan_mask
= (CONV_X
| CONV_Y
),
442 static const struct of_device_id sx8654_of_match
[] = {
444 .compatible
= "semtech,sx8650",
445 .data
= &sx8650_data
,
447 .compatible
= "semtech,sx8654",
448 .data
= &sx8654_data
,
450 .compatible
= "semtech,sx8655",
451 .data
= &sx8654_data
,
453 .compatible
= "semtech,sx8656",
454 .data
= &sx8654_data
,
458 MODULE_DEVICE_TABLE(of
, sx8654_of_match
);
461 static const struct i2c_device_id sx8654_id_table
[] = {
462 { .name
= "semtech_sx8650", .driver_data
= (long)&sx8650_data
},
463 { .name
= "semtech_sx8654", .driver_data
= (long)&sx8654_data
},
464 { .name
= "semtech_sx8655", .driver_data
= (long)&sx8654_data
},
465 { .name
= "semtech_sx8656", .driver_data
= (long)&sx8654_data
},
468 MODULE_DEVICE_TABLE(i2c
, sx8654_id_table
);
470 static struct i2c_driver sx8654_driver
= {
473 .of_match_table
= of_match_ptr(sx8654_of_match
),
475 .id_table
= sx8654_id_table
,
476 .probe
= sx8654_probe
,
478 module_i2c_driver(sx8654_driver
);
480 MODULE_AUTHOR("Sébastien Szymanski <sebastien.szymanski@armadeus.com>");
481 MODULE_DESCRIPTION("Semtech SX8654 I2C Touchscreen Driver");
482 MODULE_LICENSE("GPL");