blob: defe5dd3627ce98d84104ef3ffea64214ee173fc [file] [log] [blame]
Magnus Damm885c3162008-05-07 11:15:02 -04001/*
2 * Touch Screen driver for Renesas MIGO-R Platform
3 *
4 * Copyright (c) 2008 Magnus Damm
5 * Copyright (c) 2007 Ujjwal Pande <ujjwal@kenati.com>,
6 * Kenati Technologies Pvt Ltd.
7 *
8 * This file is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This file is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22#include <linux/module.h>
23#include <linux/kernel.h>
24#include <linux/input.h>
25#include <linux/interrupt.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Magnus Damm885c3162008-05-07 11:15:02 -040027#include <asm/io.h>
28#include <linux/i2c.h>
29#include <linux/timer.h>
30
31#define EVENT_PENDOWN 1
32#define EVENT_REPEAT 2
33#define EVENT_PENUP 3
34
35struct migor_ts_priv {
36 struct i2c_client *client;
37 struct input_dev *input;
38 struct delayed_work work;
39 int irq;
40};
41
42static const u_int8_t migor_ts_ena_seq[17] = { 0x33, 0x22, 0x11,
43 0x01, 0x06, 0x07, };
44static const u_int8_t migor_ts_dis_seq[17] = { };
45
46static void migor_ts_poscheck(struct work_struct *work)
47{
48 struct migor_ts_priv *priv = container_of(work,
49 struct migor_ts_priv,
50 work.work);
51 unsigned short xpos, ypos;
52 unsigned char event;
53 u_int8_t buf[16];
54
55 memset(buf, 0, sizeof(buf));
56
57 /* Set Index 0 */
58 buf[0] = 0;
59 if (i2c_master_send(priv->client, buf, 1) != 1) {
60 dev_err(&priv->client->dev, "Unable to write i2c index\n");
61 goto out;
62 }
63
64 /* Now do Page Read */
65 if (i2c_master_recv(priv->client, buf, sizeof(buf)) != sizeof(buf)) {
66 dev_err(&priv->client->dev, "Unable to read i2c page\n");
67 goto out;
68 }
69
70 ypos = ((buf[9] & 0x03) << 8 | buf[8]);
71 xpos = ((buf[11] & 0x03) << 8 | buf[10]);
72 event = buf[12];
73
74 if (event == EVENT_PENDOWN || event == EVENT_REPEAT) {
75 input_report_key(priv->input, BTN_TOUCH, 1);
76 input_report_abs(priv->input, ABS_X, ypos); /*X-Y swap*/
77 input_report_abs(priv->input, ABS_Y, xpos);
78 input_sync(priv->input);
79 } else if (event == EVENT_PENUP) {
80 input_report_key(priv->input, BTN_TOUCH, 0);
81 input_sync(priv->input);
82 }
83 out:
84 enable_irq(priv->irq);
85}
86
87static irqreturn_t migor_ts_isr(int irq, void *dev_id)
88{
89 struct migor_ts_priv *priv = dev_id;
90
91 /* the touch screen controller chip is hooked up to the cpu
92 * using i2c and a single interrupt line. the interrupt line
93 * is pulled low whenever someone taps the screen. to deassert
94 * the interrupt line we need to acknowledge the interrupt by
95 * communicating with the controller over the slow i2c bus.
96 *
97 * we can't acknowledge from interrupt context since the i2c
98 * bus controller may sleep, so we just disable the interrupt
99 * here and handle the acknowledge using delayed work.
100 */
101
102 disable_irq_nosync(irq);
103 schedule_delayed_work(&priv->work, HZ / 20);
104
105 return IRQ_HANDLED;
106}
107
108
109static int migor_ts_open(struct input_dev *dev)
110{
111 struct migor_ts_priv *priv = input_get_drvdata(dev);
112 struct i2c_client *client = priv->client;
113 int count;
114
115 /* enable controller */
116 count = i2c_master_send(client, migor_ts_ena_seq,
117 sizeof(migor_ts_ena_seq));
118 if (count != sizeof(migor_ts_ena_seq)) {
119 dev_err(&client->dev, "Unable to enable touchscreen.\n");
120 return -ENXIO;
121 }
122
123 return 0;
124}
125
126static void migor_ts_close(struct input_dev *dev)
127{
128 struct migor_ts_priv *priv = input_get_drvdata(dev);
129 struct i2c_client *client = priv->client;
130
131 disable_irq(priv->irq);
132
133 /* cancel pending work and wait for migor_ts_poscheck() to finish */
134 if (cancel_delayed_work_sync(&priv->work)) {
135 /*
136 * if migor_ts_poscheck was canceled we need to enable IRQ
137 * here to balance disable done in migor_ts_isr.
138 */
139 enable_irq(priv->irq);
140 }
141
142 /* disable controller */
143 i2c_master_send(client, migor_ts_dis_seq, sizeof(migor_ts_dis_seq));
144
145 enable_irq(priv->irq);
146}
147
148static int migor_ts_probe(struct i2c_client *client,
149 const struct i2c_device_id *idp)
150{
151 struct migor_ts_priv *priv;
152 struct input_dev *input;
153 int error;
154
155 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
156 if (!priv) {
157 dev_err(&client->dev, "failed to allocate driver data\n");
158 error = -ENOMEM;
159 goto err0;
160 }
161
162 dev_set_drvdata(&client->dev, priv);
163
164 input = input_allocate_device();
165 if (!input) {
166 dev_err(&client->dev, "Failed to allocate input device.\n");
167 error = -ENOMEM;
168 goto err1;
169 }
170
171 input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
172 input->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
173
174 input_set_abs_params(input, ABS_X, 95, 955, 0, 0);
175 input_set_abs_params(input, ABS_Y, 85, 935, 0, 0);
176
Magnus Dammb6ce9ad2008-08-11 14:53:49 +0900177 input->name = client->name;
Magnus Damm885c3162008-05-07 11:15:02 -0400178 input->id.bustype = BUS_I2C;
179 input->dev.parent = &client->dev;
180
181 input->open = migor_ts_open;
182 input->close = migor_ts_close;
183
184 input_set_drvdata(input, priv);
185
186 priv->client = client;
187 priv->input = input;
188 INIT_DELAYED_WORK(&priv->work, migor_ts_poscheck);
189 priv->irq = client->irq;
190
191 error = input_register_device(input);
192 if (error)
193 goto err1;
194
195 error = request_irq(priv->irq, migor_ts_isr, IRQF_TRIGGER_LOW,
Magnus Dammb6ce9ad2008-08-11 14:53:49 +0900196 client->name, priv);
Magnus Damm885c3162008-05-07 11:15:02 -0400197 if (error) {
198 dev_err(&client->dev, "Unable to request touchscreen IRQ.\n");
199 goto err2;
200 }
201
Magnus Damm0935ade2009-04-01 14:49:41 +0000202 device_init_wakeup(&client->dev, 1);
Magnus Damm885c3162008-05-07 11:15:02 -0400203 return 0;
204
205 err2:
206 input_unregister_device(input);
207 input = NULL; /* so we dont try to free it below */
208 err1:
209 input_free_device(input);
210 kfree(priv);
211 err0:
212 dev_set_drvdata(&client->dev, NULL);
213 return error;
214}
215
216static int migor_ts_remove(struct i2c_client *client)
217{
218 struct migor_ts_priv *priv = dev_get_drvdata(&client->dev);
219
220 free_irq(priv->irq, priv);
221 input_unregister_device(priv->input);
222 kfree(priv);
223
224 dev_set_drvdata(&client->dev, NULL);
225
226 return 0;
227}
228
Magnus Damm0935ade2009-04-01 14:49:41 +0000229static int migor_ts_suspend(struct i2c_client *client, pm_message_t mesg)
230{
231 struct migor_ts_priv *priv = dev_get_drvdata(&client->dev);
232
233 if (device_may_wakeup(&client->dev))
234 enable_irq_wake(priv->irq);
235
236 return 0;
237}
238
239static int migor_ts_resume(struct i2c_client *client)
240{
241 struct migor_ts_priv *priv = dev_get_drvdata(&client->dev);
242
243 if (device_may_wakeup(&client->dev))
244 disable_irq_wake(priv->irq);
245
246 return 0;
247}
248
Magnus Dammb6ce9ad2008-08-11 14:53:49 +0900249static const struct i2c_device_id migor_ts_id[] = {
250 { "migor_ts", 0 },
251 { }
252};
253MODULE_DEVICE_TABLE(i2c, migor_ts);
254
Magnus Damm885c3162008-05-07 11:15:02 -0400255static struct i2c_driver migor_ts_driver = {
256 .driver = {
257 .name = "migor_ts",
258 },
259 .probe = migor_ts_probe,
260 .remove = migor_ts_remove,
Magnus Damm0935ade2009-04-01 14:49:41 +0000261 .suspend = migor_ts_suspend,
262 .resume = migor_ts_resume,
Magnus Dammb6ce9ad2008-08-11 14:53:49 +0900263 .id_table = migor_ts_id,
Magnus Damm885c3162008-05-07 11:15:02 -0400264};
265
266static int __init migor_ts_init(void)
267{
268 return i2c_add_driver(&migor_ts_driver);
269}
270
271static void __exit migor_ts_exit(void)
272{
273 i2c_del_driver(&migor_ts_driver);
274}
275
276MODULE_DESCRIPTION("MigoR Touchscreen driver");
277MODULE_AUTHOR("Magnus Damm <damm@opensource.se>");
278MODULE_LICENSE("GPL");
279
280module_init(migor_ts_init);
281module_exit(migor_ts_exit);