1 /*
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>
26 #include <asm/io.h>
27 #include <linux/i2c.h>
28 #include <linux/timer.h>
29 
30 #define EVENT_PENDOWN 1
31 #define EVENT_REPEAT  2
32 #define EVENT_PENUP   3
33 
34 struct migor_ts_priv {
35 	struct i2c_client *client;
36 	struct input_dev *input;
37 	struct delayed_work work;
38 	int irq;
39 };
40 
41 static const u_int8_t migor_ts_ena_seq[17] = { 0x33, 0x22, 0x11,
42 					       0x01, 0x06, 0x07, };
43 static const u_int8_t migor_ts_dis_seq[17] = { };
44 
45 static void migor_ts_poscheck(struct work_struct *work)
46 {
47 	struct migor_ts_priv *priv = container_of(work,
48 						  struct migor_ts_priv,
49 						  work.work);
50 	unsigned short xpos, ypos;
51 	unsigned char event;
52 	u_int8_t buf[16];
53 
54 	memset(buf, 0, sizeof(buf));
55 
56 	/* Set Index 0 */
57 	buf[0] = 0;
58 	if (i2c_master_send(priv->client, buf, 1) != 1) {
59 		dev_err(&priv->client->dev, "Unable to write i2c index\n");
60 		goto out;
61 	}
62 
63 	/* Now do Page Read */
64 	if (i2c_master_recv(priv->client, buf, sizeof(buf)) != sizeof(buf)) {
65 		dev_err(&priv->client->dev, "Unable to read i2c page\n");
66 		goto out;
67 	}
68 
69 	ypos = ((buf[9] & 0x03) << 8 | buf[8]);
70 	xpos = ((buf[11] & 0x03) << 8 | buf[10]);
71 	event = buf[12];
72 
73 	if (event == EVENT_PENDOWN || event == EVENT_REPEAT) {
74 		input_report_key(priv->input, BTN_TOUCH, 1);
75 		input_report_abs(priv->input, ABS_X, ypos); /*X-Y swap*/
76 		input_report_abs(priv->input, ABS_Y, xpos);
77 		input_sync(priv->input);
78 	} else if (event == EVENT_PENUP) {
79 		input_report_key(priv->input, BTN_TOUCH, 0);
80 		input_sync(priv->input);
81 	}
82  out:
83 	enable_irq(priv->irq);
84 }
85 
86 static irqreturn_t migor_ts_isr(int irq, void *dev_id)
87 {
88 	struct migor_ts_priv *priv = dev_id;
89 
90 	/* the touch screen controller chip is hooked up to the cpu
91 	 * using i2c and a single interrupt line. the interrupt line
92 	 * is pulled low whenever someone taps the screen. to deassert
93 	 * the interrupt line we need to acknowledge the interrupt by
94 	 * communicating with the controller over the slow i2c bus.
95 	 *
96 	 * we can't acknowledge from interrupt context since the i2c
97 	 * bus controller may sleep, so we just disable the interrupt
98 	 * here and handle the acknowledge using delayed work.
99 	 */
100 
101 	disable_irq_nosync(irq);
102 	schedule_delayed_work(&priv->work, HZ / 20);
103 
104 	return IRQ_HANDLED;
105 }
106 
107 
108 static int migor_ts_open(struct input_dev *dev)
109 {
110 	struct migor_ts_priv *priv = input_get_drvdata(dev);
111 	struct i2c_client *client = priv->client;
112 	int count;
113 
114 	/* enable controller */
115 	count = i2c_master_send(client, migor_ts_ena_seq,
116 				sizeof(migor_ts_ena_seq));
117 	if (count != sizeof(migor_ts_ena_seq)) {
118 		dev_err(&client->dev, "Unable to enable touchscreen.\n");
119 		return -ENXIO;
120 	}
121 
122 	return 0;
123 }
124 
125 static void migor_ts_close(struct input_dev *dev)
126 {
127 	struct migor_ts_priv *priv = input_get_drvdata(dev);
128 	struct i2c_client *client = priv->client;
129 
130 	disable_irq(priv->irq);
131 
132 	/* cancel pending work and wait for migor_ts_poscheck() to finish */
133 	if (cancel_delayed_work_sync(&priv->work)) {
134 		/*
135 		 * if migor_ts_poscheck was canceled we need to enable IRQ
136 		 * here to balance disable done in migor_ts_isr.
137 		 */
138 		enable_irq(priv->irq);
139 	}
140 
141 	/* disable controller */
142 	i2c_master_send(client, migor_ts_dis_seq, sizeof(migor_ts_dis_seq));
143 
144 	enable_irq(priv->irq);
145 }
146 
147 static int migor_ts_probe(struct i2c_client *client,
148 			  const struct i2c_device_id *idp)
149 {
150 	struct migor_ts_priv *priv;
151 	struct input_dev *input;
152 	int error;
153 
154 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
155 	if (!priv) {
156 		dev_err(&client->dev, "failed to allocate driver data\n");
157 		error = -ENOMEM;
158 		goto err0;
159 	}
160 
161 	dev_set_drvdata(&client->dev, priv);
162 
163 	input = input_allocate_device();
164 	if (!input) {
165 		dev_err(&client->dev, "Failed to allocate input device.\n");
166 		error = -ENOMEM;
167 		goto err1;
168 	}
169 
170 	input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
171 	input->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
172 
173 	input_set_abs_params(input, ABS_X, 95, 955, 0, 0);
174 	input_set_abs_params(input, ABS_Y, 85, 935, 0, 0);
175 
176 	input->name = client->name;
177 	input->id.bustype = BUS_I2C;
178 	input->dev.parent = &client->dev;
179 
180 	input->open = migor_ts_open;
181 	input->close = migor_ts_close;
182 
183 	input_set_drvdata(input, priv);
184 
185 	priv->client = client;
186 	priv->input = input;
187 	INIT_DELAYED_WORK(&priv->work, migor_ts_poscheck);
188 	priv->irq = client->irq;
189 
190 	error = input_register_device(input);
191 	if (error)
192 		goto err1;
193 
194 	error = request_irq(priv->irq, migor_ts_isr, IRQF_TRIGGER_LOW,
195 			    client->name, priv);
196 	if (error) {
197 		dev_err(&client->dev, "Unable to request touchscreen IRQ.\n");
198 		goto err2;
199 	}
200 
201 	device_init_wakeup(&client->dev, 1);
202 	return 0;
203 
204  err2:
205 	input_unregister_device(input);
206 	input = NULL; /* so we dont try to free it below */
207  err1:
208 	input_free_device(input);
209 	kfree(priv);
210  err0:
211 	dev_set_drvdata(&client->dev, NULL);
212 	return error;
213 }
214 
215 static int migor_ts_remove(struct i2c_client *client)
216 {
217 	struct migor_ts_priv *priv = dev_get_drvdata(&client->dev);
218 
219 	free_irq(priv->irq, priv);
220 	input_unregister_device(priv->input);
221 	kfree(priv);
222 
223 	dev_set_drvdata(&client->dev, NULL);
224 
225 	return 0;
226 }
227 
228 static int migor_ts_suspend(struct i2c_client *client, pm_message_t mesg)
229 {
230 	struct migor_ts_priv *priv = dev_get_drvdata(&client->dev);
231 
232 	if (device_may_wakeup(&client->dev))
233 		enable_irq_wake(priv->irq);
234 
235 	return 0;
236 }
237 
238 static int migor_ts_resume(struct i2c_client *client)
239 {
240 	struct migor_ts_priv *priv = dev_get_drvdata(&client->dev);
241 
242 	if (device_may_wakeup(&client->dev))
243 		disable_irq_wake(priv->irq);
244 
245 	return 0;
246 }
247 
248 static const struct i2c_device_id migor_ts_id[] = {
249 	{ "migor_ts", 0 },
250 	{ }
251 };
252 MODULE_DEVICE_TABLE(i2c, migor_ts);
253 
254 static struct i2c_driver migor_ts_driver = {
255 	.driver = {
256 		.name = "migor_ts",
257 	},
258 	.probe = migor_ts_probe,
259 	.remove = migor_ts_remove,
260 	.suspend = migor_ts_suspend,
261 	.resume = migor_ts_resume,
262 	.id_table = migor_ts_id,
263 };
264 
265 static int __init migor_ts_init(void)
266 {
267 	return i2c_add_driver(&migor_ts_driver);
268 }
269 
270 static void __exit migor_ts_exit(void)
271 {
272 	i2c_del_driver(&migor_ts_driver);
273 }
274 
275 MODULE_DESCRIPTION("MigoR Touchscreen driver");
276 MODULE_AUTHOR("Magnus Damm <damm@opensource.se>");
277 MODULE_LICENSE("GPL");
278 
279 module_init(migor_ts_init);
280 module_exit(migor_ts_exit);
281