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/input.h>
31 #include <linux/module.h>
33 #include <linux/i2c.h>
34 #include <linux/interrupt.h>
35 #include <linux/irq.h>
37 /* register addresses */
38 #define I2C_REG_TOUCH0 0x00
39 #define I2C_REG_TOUCH1 0x01
40 #define I2C_REG_CHANMASK 0x04
41 #define I2C_REG_IRQMASK 0x22
42 #define I2C_REG_IRQSRC 0x23
43 #define I2C_REG_SOFTRESET 0x3f
46 #define CMD_READ_REGISTER 0x40
47 #define CMD_MANUAL 0xc0
48 #define CMD_PENTRG 0xe0
50 /* value for I2C_REG_SOFTRESET */
51 #define SOFTRESET_VALUE 0xde
53 /* bits for I2C_REG_IRQSRC */
54 #define IRQ_PENTOUCH_TOUCHCONVDONE 0x08
55 #define IRQ_PENRELEASE 0x04
57 /* bits for RegTouch1 */
61 /* bits for I2C_REG_CHANMASK */
65 /* coordinates rate: higher nibble of CTRL0 register */
66 #define RATE_MANUAL 0x00
67 #define RATE_5000CPS 0xf0
69 /* power delay: lower nibble of CTRL0 register */
70 #define POWDLY_1_1MS 0x0b
72 #define MAX_12BIT ((1 << 12) - 1)
75 struct input_dev
*input
;
76 struct i2c_client
*client
;
79 static irqreturn_t
sx8654_irq(int irq
, void *handle
)
81 struct sx8654
*sx8654
= handle
;
87 irqsrc
= i2c_smbus_read_byte_data(sx8654
->client
,
88 CMD_READ_REGISTER
| I2C_REG_IRQSRC
);
89 dev_dbg(&sx8654
->client
->dev
, "irqsrc = 0x%x", irqsrc
);
94 if (irqsrc
& IRQ_PENRELEASE
) {
95 dev_dbg(&sx8654
->client
->dev
, "pen release interrupt");
97 input_report_key(sx8654
->input
, BTN_TOUCH
, 0);
98 input_sync(sx8654
->input
);
101 if (irqsrc
& IRQ_PENTOUCH_TOUCHCONVDONE
) {
102 dev_dbg(&sx8654
->client
->dev
, "pen touch interrupt");
104 retval
= i2c_master_recv(sx8654
->client
, data
, sizeof(data
));
105 if (retval
!= sizeof(data
))
109 if (unlikely(data
[0] & 0x80 || data
[2] & 0x80))
112 x
= ((data
[0] & 0xf) << 8) | (data
[1]);
113 y
= ((data
[2] & 0xf) << 8) | (data
[3]);
115 input_report_abs(sx8654
->input
, ABS_X
, x
);
116 input_report_abs(sx8654
->input
, ABS_Y
, y
);
117 input_report_key(sx8654
->input
, BTN_TOUCH
, 1);
118 input_sync(sx8654
->input
);
120 dev_dbg(&sx8654
->client
->dev
, "point(%4d,%4d)\n", x
, y
);
127 static int sx8654_open(struct input_dev
*dev
)
129 struct sx8654
*sx8654
= input_get_drvdata(dev
);
130 struct i2c_client
*client
= sx8654
->client
;
133 /* enable pen trigger mode */
134 error
= i2c_smbus_write_byte_data(client
, I2C_REG_TOUCH0
,
135 RATE_5000CPS
| POWDLY_1_1MS
);
137 dev_err(&client
->dev
, "writing to I2C_REG_TOUCH0 failed");
141 error
= i2c_smbus_write_byte(client
, CMD_PENTRG
);
143 dev_err(&client
->dev
, "writing command CMD_PENTRG failed");
147 enable_irq(client
->irq
);
152 static void sx8654_close(struct input_dev
*dev
)
154 struct sx8654
*sx8654
= input_get_drvdata(dev
);
155 struct i2c_client
*client
= sx8654
->client
;
158 disable_irq(client
->irq
);
160 /* enable manual mode mode */
161 error
= i2c_smbus_write_byte(client
, CMD_MANUAL
);
163 dev_err(&client
->dev
, "writing command CMD_MANUAL failed");
167 error
= i2c_smbus_write_byte_data(client
, I2C_REG_TOUCH0
, 0);
169 dev_err(&client
->dev
, "writing to I2C_REG_TOUCH0 failed");
174 static int sx8654_probe(struct i2c_client
*client
,
175 const struct i2c_device_id
*id
)
177 struct sx8654
*sx8654
;
178 struct input_dev
*input
;
181 if (!i2c_check_functionality(client
->adapter
,
182 I2C_FUNC_SMBUS_READ_WORD_DATA
))
185 sx8654
= devm_kzalloc(&client
->dev
, sizeof(*sx8654
), GFP_KERNEL
);
189 input
= devm_input_allocate_device(&client
->dev
);
193 input
->name
= "SX8654 I2C Touchscreen";
194 input
->id
.bustype
= BUS_I2C
;
195 input
->dev
.parent
= &client
->dev
;
196 input
->open
= sx8654_open
;
197 input
->close
= sx8654_close
;
199 __set_bit(INPUT_PROP_DIRECT
, input
->propbit
);
200 input_set_capability(input
, EV_KEY
, BTN_TOUCH
);
201 input_set_abs_params(input
, ABS_X
, 0, MAX_12BIT
, 0, 0);
202 input_set_abs_params(input
, ABS_Y
, 0, MAX_12BIT
, 0, 0);
204 sx8654
->client
= client
;
205 sx8654
->input
= input
;
207 input_set_drvdata(sx8654
->input
, sx8654
);
209 error
= i2c_smbus_write_byte_data(client
, I2C_REG_SOFTRESET
,
212 dev_err(&client
->dev
, "writing softreset value failed");
216 error
= i2c_smbus_write_byte_data(client
, I2C_REG_CHANMASK
,
219 dev_err(&client
->dev
, "writing to I2C_REG_CHANMASK failed");
223 error
= i2c_smbus_write_byte_data(client
, I2C_REG_IRQMASK
,
224 IRQ_PENTOUCH_TOUCHCONVDONE
|
227 dev_err(&client
->dev
, "writing to I2C_REG_IRQMASK failed");
231 error
= i2c_smbus_write_byte_data(client
, I2C_REG_TOUCH1
,
234 dev_err(&client
->dev
, "writing to I2C_REG_TOUCH1 failed");
238 error
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
240 IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
241 client
->name
, sx8654
);
243 dev_err(&client
->dev
,
244 "Failed to enable IRQ %d, error: %d\n",
249 /* Disable the IRQ, we'll enable it in sx8654_open() */
250 disable_irq(client
->irq
);
252 error
= input_register_device(sx8654
->input
);
256 i2c_set_clientdata(client
, sx8654
);
261 static const struct of_device_id sx8654_of_match
[] = {
262 { .compatible
= "semtech,sx8654", },
265 MODULE_DEVICE_TABLE(of
, sx8654_of_match
);
268 static const struct i2c_device_id sx8654_id_table
[] = {
269 { "semtech_sx8654", 0 },
272 MODULE_DEVICE_TABLE(i2c
, sx8654_id_table
);
274 static struct i2c_driver sx8654_driver
= {
277 .of_match_table
= of_match_ptr(sx8654_of_match
),
279 .id_table
= sx8654_id_table
,
280 .probe
= sx8654_probe
,
282 module_i2c_driver(sx8654_driver
);
284 MODULE_AUTHOR("Sébastien Szymanski <sebastien.szymanski@armadeus.com>");
285 MODULE_DESCRIPTION("Semtech SX8654 I2C Touchscreen Driver");
286 MODULE_LICENSE("GPL");