1 // SPDX-License-Identifier: GPL-2.0+
3 * Touch Screen driver for Renesas MIGO-R Platform
5 * Copyright (c) 2008 Magnus Damm
6 * Copyright (c) 2007 Ujjwal Pande <ujjwal@kenati.com>,
7 * Kenati Technologies Pvt Ltd.
9 #include <linux/module.h>
10 #include <linux/kernel.h>
11 #include <linux/input.h>
12 #include <linux/interrupt.h>
14 #include <linux/slab.h>
16 #include <linux/i2c.h>
17 #include <linux/timer.h>
19 #define EVENT_PENDOWN 1
20 #define EVENT_REPEAT 2
23 struct migor_ts_priv
{
24 struct i2c_client
*client
;
25 struct input_dev
*input
;
29 static const u_int8_t migor_ts_ena_seq
[17] = { 0x33, 0x22, 0x11,
31 static const u_int8_t migor_ts_dis_seq
[17] = { };
33 static irqreturn_t
migor_ts_isr(int irq
, void *dev_id
)
35 struct migor_ts_priv
*priv
= dev_id
;
36 unsigned short xpos
, ypos
;
41 * The touch screen controller chip is hooked up to the CPU
42 * using I2C and a single interrupt line. The interrupt line
43 * is pulled low whenever someone taps the screen. To deassert
44 * the interrupt line we need to acknowledge the interrupt by
45 * communicating with the controller over the slow i2c bus.
47 * Since I2C bus controller may sleep we are using threaded
51 memset(buf
, 0, sizeof(buf
));
55 if (i2c_master_send(priv
->client
, buf
, 1) != 1) {
56 dev_err(&priv
->client
->dev
, "Unable to write i2c index\n");
60 /* Now do Page Read */
61 if (i2c_master_recv(priv
->client
, buf
, sizeof(buf
)) != sizeof(buf
)) {
62 dev_err(&priv
->client
->dev
, "Unable to read i2c page\n");
66 ypos
= ((buf
[9] & 0x03) << 8 | buf
[8]);
67 xpos
= ((buf
[11] & 0x03) << 8 | buf
[10]);
73 input_report_key(priv
->input
, BTN_TOUCH
, 1);
74 input_report_abs(priv
->input
, ABS_X
, ypos
); /*X-Y swap*/
75 input_report_abs(priv
->input
, ABS_Y
, xpos
);
76 input_sync(priv
->input
);
80 input_report_key(priv
->input
, BTN_TOUCH
, 0);
81 input_sync(priv
->input
);
89 static int migor_ts_open(struct input_dev
*dev
)
91 struct migor_ts_priv
*priv
= input_get_drvdata(dev
);
92 struct i2c_client
*client
= priv
->client
;
95 /* enable controller */
96 count
= i2c_master_send(client
, migor_ts_ena_seq
,
97 sizeof(migor_ts_ena_seq
));
98 if (count
!= sizeof(migor_ts_ena_seq
)) {
99 dev_err(&client
->dev
, "Unable to enable touchscreen.\n");
106 static void migor_ts_close(struct input_dev
*dev
)
108 struct migor_ts_priv
*priv
= input_get_drvdata(dev
);
109 struct i2c_client
*client
= priv
->client
;
111 disable_irq(priv
->irq
);
113 /* disable controller */
114 i2c_master_send(client
, migor_ts_dis_seq
, sizeof(migor_ts_dis_seq
));
116 enable_irq(priv
->irq
);
119 static int migor_ts_probe(struct i2c_client
*client
,
120 const struct i2c_device_id
*idp
)
122 struct migor_ts_priv
*priv
;
123 struct input_dev
*input
;
126 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
127 input
= input_allocate_device();
128 if (!priv
|| !input
) {
129 dev_err(&client
->dev
, "failed to allocate memory\n");
134 priv
->client
= client
;
136 priv
->irq
= client
->irq
;
138 input
->evbit
[0] = BIT_MASK(EV_KEY
) | BIT_MASK(EV_ABS
);
140 __set_bit(BTN_TOUCH
, input
->keybit
);
142 input_set_abs_params(input
, ABS_X
, 95, 955, 0, 0);
143 input_set_abs_params(input
, ABS_Y
, 85, 935, 0, 0);
145 input
->name
= client
->name
;
146 input
->id
.bustype
= BUS_I2C
;
147 input
->dev
.parent
= &client
->dev
;
149 input
->open
= migor_ts_open
;
150 input
->close
= migor_ts_close
;
152 input_set_drvdata(input
, priv
);
154 error
= request_threaded_irq(priv
->irq
, NULL
, migor_ts_isr
,
155 IRQF_TRIGGER_LOW
| IRQF_ONESHOT
,
158 dev_err(&client
->dev
, "Unable to request touchscreen IRQ.\n");
162 error
= input_register_device(input
);
166 i2c_set_clientdata(client
, priv
);
167 device_init_wakeup(&client
->dev
, 1);
172 free_irq(priv
->irq
, priv
);
174 input_free_device(input
);
179 static int migor_ts_remove(struct i2c_client
*client
)
181 struct migor_ts_priv
*priv
= i2c_get_clientdata(client
);
183 free_irq(priv
->irq
, priv
);
184 input_unregister_device(priv
->input
);
187 dev_set_drvdata(&client
->dev
, NULL
);
192 static int __maybe_unused
migor_ts_suspend(struct device
*dev
)
194 struct i2c_client
*client
= to_i2c_client(dev
);
195 struct migor_ts_priv
*priv
= i2c_get_clientdata(client
);
197 if (device_may_wakeup(&client
->dev
))
198 enable_irq_wake(priv
->irq
);
203 static int __maybe_unused
migor_ts_resume(struct device
*dev
)
205 struct i2c_client
*client
= to_i2c_client(dev
);
206 struct migor_ts_priv
*priv
= i2c_get_clientdata(client
);
208 if (device_may_wakeup(&client
->dev
))
209 disable_irq_wake(priv
->irq
);
214 static SIMPLE_DEV_PM_OPS(migor_ts_pm
, migor_ts_suspend
, migor_ts_resume
);
216 static const struct i2c_device_id migor_ts_id
[] = {
220 MODULE_DEVICE_TABLE(i2c
, migor_ts_id
);
222 static struct i2c_driver migor_ts_driver
= {
227 .probe
= migor_ts_probe
,
228 .remove
= migor_ts_remove
,
229 .id_table
= migor_ts_id
,
232 module_i2c_driver(migor_ts_driver
);
234 MODULE_DESCRIPTION("MigoR Touchscreen driver");
235 MODULE_AUTHOR("Magnus Damm <damm@opensource.se>");
236 MODULE_LICENSE("GPL");