]>
Commit | Line | Data |
---|---|---|
36a281e2 JB |
1 | /* |
2 | * Driver for Pixcir I2C touchscreen controllers. | |
3 | * | |
4 | * Copyright (C) 2010-2011 Pixcir, Inc. | |
5 | * | |
6 | * This software is licensed under the terms of the GNU General Public | |
7 | * License version 2, as published by the Free Software Foundation, and | |
8 | * may be copied, distributed, and modified under those terms. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public | |
16 | * License along with this library; if not, write to the Free Software | |
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 | */ | |
19 | ||
20 | #include <linux/delay.h> | |
21 | #include <linux/module.h> | |
22 | #include <linux/interrupt.h> | |
23 | #include <linux/slab.h> | |
24 | #include <linux/i2c.h> | |
25 | #include <linux/input.h> | |
62e65b7e | 26 | #include <linux/input/mt.h> |
36a281e2 | 27 | #include <linux/input/pixcir_ts.h> |
0dfc8d41 | 28 | #include <linux/gpio.h> |
36a281e2 | 29 | |
62e65b7e RQ |
30 | #define PIXCIR_MAX_SLOTS 2 |
31 | ||
36a281e2 JB |
32 | struct pixcir_i2c_ts_data { |
33 | struct i2c_client *client; | |
34 | struct input_dev *input; | |
35 | const struct pixcir_ts_platform_data *chip; | |
3b36fbb0 | 36 | bool running; |
36a281e2 JB |
37 | }; |
38 | ||
62e65b7e RQ |
39 | struct pixcir_touch { |
40 | int x; | |
41 | int y; | |
42 | }; | |
43 | ||
44 | struct pixcir_report_data { | |
45 | int num_touches; | |
46 | struct pixcir_touch touches[PIXCIR_MAX_SLOTS]; | |
47 | }; | |
48 | ||
49 | static void pixcir_ts_parse(struct pixcir_i2c_ts_data *tsdata, | |
50 | struct pixcir_report_data *report) | |
36a281e2 | 51 | { |
36a281e2 | 52 | u8 rdbuf[10], wrbuf[1] = { 0 }; |
62e65b7e | 53 | u8 *bufptr; |
36a281e2 | 54 | u8 touch; |
62e65b7e RQ |
55 | int ret, i; |
56 | ||
57 | memset(report, 0, sizeof(struct pixcir_report_data)); | |
36a281e2 JB |
58 | |
59 | ret = i2c_master_send(tsdata->client, wrbuf, sizeof(wrbuf)); | |
60 | if (ret != sizeof(wrbuf)) { | |
61 | dev_err(&tsdata->client->dev, | |
62 | "%s: i2c_master_send failed(), ret=%d\n", | |
63 | __func__, ret); | |
64 | return; | |
65 | } | |
66 | ||
67 | ret = i2c_master_recv(tsdata->client, rdbuf, sizeof(rdbuf)); | |
68 | if (ret != sizeof(rdbuf)) { | |
69 | dev_err(&tsdata->client->dev, | |
70 | "%s: i2c_master_recv failed(), ret=%d\n", | |
71 | __func__, ret); | |
72 | return; | |
73 | } | |
74 | ||
62e65b7e RQ |
75 | touch = rdbuf[0] & 0x7; |
76 | if (touch > PIXCIR_MAX_SLOTS) | |
77 | touch = PIXCIR_MAX_SLOTS; | |
78 | ||
79 | report->num_touches = touch; | |
80 | bufptr = &rdbuf[2]; | |
81 | ||
82 | for (i = 0; i < touch; i++) { | |
83 | report->touches[i].x = (bufptr[1] << 8) | bufptr[0]; | |
84 | report->touches[i].y = (bufptr[3] << 8) | bufptr[2]; | |
85 | ||
86 | bufptr = bufptr + 4; | |
36a281e2 | 87 | } |
62e65b7e RQ |
88 | } |
89 | ||
90 | static void pixcir_ts_report(struct pixcir_i2c_ts_data *ts, | |
91 | struct pixcir_report_data *report) | |
92 | { | |
93 | struct input_mt_pos pos[PIXCIR_MAX_SLOTS]; | |
94 | int slots[PIXCIR_MAX_SLOTS]; | |
95 | struct pixcir_touch *touch; | |
96 | int n, i, slot; | |
97 | struct device *dev = &ts->client->dev; | |
98 | ||
99 | n = report->num_touches; | |
100 | if (n > PIXCIR_MAX_SLOTS) | |
101 | n = PIXCIR_MAX_SLOTS; | |
36a281e2 | 102 | |
62e65b7e RQ |
103 | for (i = 0; i < n; i++) { |
104 | touch = &report->touches[i]; | |
105 | pos[i].x = touch->x; | |
106 | pos[i].y = touch->y; | |
107 | } | |
108 | ||
109 | input_mt_assign_slots(ts->input, slots, pos, n); | |
110 | ||
111 | for (i = 0; i < n; i++) { | |
112 | touch = &report->touches[i]; | |
113 | slot = slots[i]; | |
114 | ||
115 | input_mt_slot(ts->input, slot); | |
116 | input_mt_report_slot_state(ts->input, | |
117 | MT_TOOL_FINGER, true); | |
118 | ||
119 | input_event(ts->input, EV_ABS, ABS_MT_POSITION_X, touch->x); | |
120 | input_event(ts->input, EV_ABS, ABS_MT_POSITION_Y, touch->y); | |
121 | ||
122 | dev_dbg(dev, "%d: slot %d, x %d, y %d\n", | |
123 | i, slot, touch->x, touch->y); | |
124 | } | |
125 | ||
126 | input_mt_sync_frame(ts->input); | |
127 | input_sync(ts->input); | |
36a281e2 JB |
128 | } |
129 | ||
130 | static irqreturn_t pixcir_ts_isr(int irq, void *dev_id) | |
131 | { | |
132 | struct pixcir_i2c_ts_data *tsdata = dev_id; | |
0dfc8d41 | 133 | const struct pixcir_ts_platform_data *pdata = tsdata->chip; |
62e65b7e | 134 | struct pixcir_report_data report; |
36a281e2 | 135 | |
3b36fbb0 | 136 | while (tsdata->running) { |
62e65b7e RQ |
137 | /* parse packet */ |
138 | pixcir_ts_parse(tsdata, &report); | |
139 | ||
140 | /* report it */ | |
141 | pixcir_ts_report(tsdata, &report); | |
142 | ||
143 | if (gpio_get_value(pdata->gpio_attb)) { | |
144 | if (report.num_touches) { | |
145 | /* | |
146 | * Last report with no finger up? | |
147 | * Do it now then. | |
148 | */ | |
149 | input_mt_sync_frame(tsdata->input); | |
150 | input_sync(tsdata->input); | |
151 | } | |
36a281e2 | 152 | break; |
62e65b7e | 153 | } |
36a281e2 JB |
154 | |
155 | msleep(20); | |
156 | } | |
157 | ||
158 | return IRQ_HANDLED; | |
159 | } | |
160 | ||
3b36fbb0 RQ |
161 | static int pixcir_set_power_mode(struct pixcir_i2c_ts_data *ts, |
162 | enum pixcir_power_mode mode) | |
163 | { | |
164 | struct device *dev = &ts->client->dev; | |
165 | int ret; | |
166 | ||
167 | ret = i2c_smbus_read_byte_data(ts->client, PIXCIR_REG_POWER_MODE); | |
168 | if (ret < 0) { | |
169 | dev_err(dev, "%s: can't read reg 0x%x : %d\n", | |
170 | __func__, PIXCIR_REG_POWER_MODE, ret); | |
171 | return ret; | |
172 | } | |
173 | ||
174 | ret &= ~PIXCIR_POWER_MODE_MASK; | |
175 | ret |= mode; | |
176 | ||
177 | /* Always AUTO_IDLE */ | |
178 | ret |= PIXCIR_POWER_ALLOW_IDLE; | |
179 | ||
180 | ret = i2c_smbus_write_byte_data(ts->client, PIXCIR_REG_POWER_MODE, ret); | |
181 | if (ret < 0) { | |
182 | dev_err(dev, "%s: can't write reg 0x%x : %d\n", | |
183 | __func__, PIXCIR_REG_POWER_MODE, ret); | |
184 | return ret; | |
185 | } | |
186 | ||
187 | return 0; | |
188 | } | |
189 | ||
190 | /* | |
191 | * Set the interrupt mode for the device i.e. ATTB line behaviour | |
192 | * | |
193 | * @polarity : 1 for active high, 0 for active low. | |
194 | */ | |
195 | static int pixcir_set_int_mode(struct pixcir_i2c_ts_data *ts, | |
196 | enum pixcir_int_mode mode, bool polarity) | |
197 | { | |
198 | struct device *dev = &ts->client->dev; | |
199 | int ret; | |
200 | ||
201 | ret = i2c_smbus_read_byte_data(ts->client, PIXCIR_REG_INT_MODE); | |
202 | if (ret < 0) { | |
203 | dev_err(dev, "%s: can't read reg 0x%x : %d\n", | |
204 | __func__, PIXCIR_REG_INT_MODE, ret); | |
205 | return ret; | |
206 | } | |
207 | ||
208 | ret &= ~PIXCIR_INT_MODE_MASK; | |
209 | ret |= mode; | |
210 | ||
211 | if (polarity) | |
212 | ret |= PIXCIR_INT_POL_HIGH; | |
213 | else | |
214 | ret &= ~PIXCIR_INT_POL_HIGH; | |
215 | ||
216 | ret = i2c_smbus_write_byte_data(ts->client, PIXCIR_REG_INT_MODE, ret); | |
217 | if (ret < 0) { | |
218 | dev_err(dev, "%s: can't write reg 0x%x : %d\n", | |
219 | __func__, PIXCIR_REG_INT_MODE, ret); | |
220 | return ret; | |
221 | } | |
222 | ||
223 | return 0; | |
224 | } | |
225 | ||
226 | /* | |
227 | * Enable/disable interrupt generation | |
228 | */ | |
229 | static int pixcir_int_enable(struct pixcir_i2c_ts_data *ts, bool enable) | |
230 | { | |
231 | struct device *dev = &ts->client->dev; | |
232 | int ret; | |
233 | ||
234 | ret = i2c_smbus_read_byte_data(ts->client, PIXCIR_REG_INT_MODE); | |
235 | if (ret < 0) { | |
236 | dev_err(dev, "%s: can't read reg 0x%x : %d\n", | |
237 | __func__, PIXCIR_REG_INT_MODE, ret); | |
238 | return ret; | |
239 | } | |
240 | ||
241 | if (enable) | |
242 | ret |= PIXCIR_INT_ENABLE; | |
243 | else | |
244 | ret &= ~PIXCIR_INT_ENABLE; | |
245 | ||
246 | ret = i2c_smbus_write_byte_data(ts->client, PIXCIR_REG_INT_MODE, ret); | |
247 | if (ret < 0) { | |
248 | dev_err(dev, "%s: can't write reg 0x%x : %d\n", | |
249 | __func__, PIXCIR_REG_INT_MODE, ret); | |
250 | return ret; | |
251 | } | |
252 | ||
253 | return 0; | |
254 | } | |
255 | ||
256 | static int pixcir_start(struct pixcir_i2c_ts_data *ts) | |
257 | { | |
258 | struct device *dev = &ts->client->dev; | |
259 | int error; | |
260 | ||
261 | /* LEVEL_TOUCH interrupt with active low polarity */ | |
262 | error = pixcir_set_int_mode(ts, PIXCIR_INT_LEVEL_TOUCH, 0); | |
263 | if (error) { | |
264 | dev_err(dev, "Failed to set interrupt mode: %d\n", error); | |
265 | return error; | |
266 | } | |
267 | ||
268 | ts->running = true; | |
269 | mb(); /* Update status before IRQ can fire */ | |
270 | ||
271 | /* enable interrupt generation */ | |
272 | error = pixcir_int_enable(ts, true); | |
273 | if (error) { | |
274 | dev_err(dev, "Failed to enable interrupt generation: %d\n", | |
275 | error); | |
276 | return error; | |
277 | } | |
278 | ||
279 | return 0; | |
280 | } | |
281 | ||
282 | static int pixcir_stop(struct pixcir_i2c_ts_data *ts) | |
283 | { | |
284 | int error; | |
285 | ||
286 | /* Disable interrupt generation */ | |
287 | error = pixcir_int_enable(ts, false); | |
288 | if (error) { | |
289 | dev_err(&ts->client->dev, | |
290 | "Failed to disable interrupt generation: %d\n", | |
291 | error); | |
292 | return error; | |
293 | } | |
294 | ||
295 | /* Exit ISR if running, no more report parsing */ | |
296 | ts->running = false; | |
297 | mb(); /* update status before we synchronize irq */ | |
298 | ||
299 | /* Wait till running ISR is complete */ | |
300 | synchronize_irq(ts->client->irq); | |
301 | ||
302 | return 0; | |
303 | } | |
304 | ||
305 | static int pixcir_input_open(struct input_dev *dev) | |
306 | { | |
307 | struct pixcir_i2c_ts_data *ts = input_get_drvdata(dev); | |
308 | ||
309 | return pixcir_start(ts); | |
310 | } | |
311 | ||
312 | static void pixcir_input_close(struct input_dev *dev) | |
313 | { | |
314 | struct pixcir_i2c_ts_data *ts = input_get_drvdata(dev); | |
315 | ||
316 | pixcir_stop(ts); | |
317 | } | |
318 | ||
36a281e2 JB |
319 | #ifdef CONFIG_PM_SLEEP |
320 | static int pixcir_i2c_ts_suspend(struct device *dev) | |
321 | { | |
322 | struct i2c_client *client = to_i2c_client(dev); | |
7cdcb8d1 RQ |
323 | struct pixcir_i2c_ts_data *ts = i2c_get_clientdata(client); |
324 | struct input_dev *input = ts->input; | |
325 | int ret = 0; | |
326 | ||
327 | mutex_lock(&input->mutex); | |
328 | ||
329 | if (device_may_wakeup(&client->dev)) { | |
330 | if (!input->users) { | |
331 | ret = pixcir_start(ts); | |
332 | if (ret) { | |
333 | dev_err(dev, "Failed to start\n"); | |
334 | goto unlock; | |
335 | } | |
336 | } | |
36a281e2 | 337 | |
36a281e2 | 338 | enable_irq_wake(client->irq); |
7cdcb8d1 RQ |
339 | } else if (input->users) { |
340 | ret = pixcir_stop(ts); | |
341 | } | |
36a281e2 | 342 | |
7cdcb8d1 RQ |
343 | unlock: |
344 | mutex_unlock(&input->mutex); | |
345 | ||
346 | return ret; | |
36a281e2 JB |
347 | } |
348 | ||
349 | static int pixcir_i2c_ts_resume(struct device *dev) | |
350 | { | |
351 | struct i2c_client *client = to_i2c_client(dev); | |
7cdcb8d1 RQ |
352 | struct pixcir_i2c_ts_data *ts = i2c_get_clientdata(client); |
353 | struct input_dev *input = ts->input; | |
354 | int ret = 0; | |
355 | ||
356 | mutex_lock(&input->mutex); | |
36a281e2 | 357 | |
7cdcb8d1 | 358 | if (device_may_wakeup(&client->dev)) { |
36a281e2 JB |
359 | disable_irq_wake(client->irq); |
360 | ||
7cdcb8d1 RQ |
361 | if (!input->users) { |
362 | ret = pixcir_stop(ts); | |
363 | if (ret) { | |
364 | dev_err(dev, "Failed to stop\n"); | |
365 | goto unlock; | |
366 | } | |
367 | } | |
368 | } else if (input->users) { | |
369 | ret = pixcir_start(ts); | |
370 | } | |
371 | ||
372 | unlock: | |
373 | mutex_unlock(&input->mutex); | |
374 | ||
375 | return ret; | |
36a281e2 JB |
376 | } |
377 | #endif | |
378 | ||
379 | static SIMPLE_DEV_PM_OPS(pixcir_dev_pm_ops, | |
380 | pixcir_i2c_ts_suspend, pixcir_i2c_ts_resume); | |
381 | ||
5298cc4c | 382 | static int pixcir_i2c_ts_probe(struct i2c_client *client, |
36a281e2 JB |
383 | const struct i2c_device_id *id) |
384 | { | |
c838cb3d JH |
385 | const struct pixcir_ts_platform_data *pdata = |
386 | dev_get_platdata(&client->dev); | |
e9d4718d | 387 | struct device *dev = &client->dev; |
36a281e2 JB |
388 | struct pixcir_i2c_ts_data *tsdata; |
389 | struct input_dev *input; | |
390 | int error; | |
391 | ||
392 | if (!pdata) { | |
393 | dev_err(&client->dev, "platform data not defined\n"); | |
394 | return -EINVAL; | |
395 | } | |
396 | ||
0dfc8d41 RQ |
397 | if (!gpio_is_valid(pdata->gpio_attb)) { |
398 | dev_err(dev, "Invalid gpio_attb in pdata\n"); | |
399 | return -EINVAL; | |
400 | } | |
401 | ||
e9d4718d RQ |
402 | tsdata = devm_kzalloc(dev, sizeof(*tsdata), GFP_KERNEL); |
403 | if (!tsdata) | |
404 | return -ENOMEM; | |
405 | ||
406 | input = devm_input_allocate_device(dev); | |
407 | if (!input) { | |
408 | dev_err(dev, "Failed to allocate input device\n"); | |
409 | return -ENOMEM; | |
36a281e2 JB |
410 | } |
411 | ||
412 | tsdata->client = client; | |
413 | tsdata->input = input; | |
414 | tsdata->chip = pdata; | |
415 | ||
416 | input->name = client->name; | |
417 | input->id.bustype = BUS_I2C; | |
3b36fbb0 RQ |
418 | input->open = pixcir_input_open; |
419 | input->close = pixcir_input_close; | |
36a281e2 JB |
420 | input->dev.parent = &client->dev; |
421 | ||
422 | __set_bit(EV_KEY, input->evbit); | |
423 | __set_bit(EV_ABS, input->evbit); | |
424 | __set_bit(BTN_TOUCH, input->keybit); | |
425 | input_set_abs_params(input, ABS_X, 0, pdata->x_max, 0, 0); | |
426 | input_set_abs_params(input, ABS_Y, 0, pdata->y_max, 0, 0); | |
427 | input_set_abs_params(input, ABS_MT_POSITION_X, 0, pdata->x_max, 0, 0); | |
428 | input_set_abs_params(input, ABS_MT_POSITION_Y, 0, pdata->y_max, 0, 0); | |
429 | ||
62e65b7e RQ |
430 | error = input_mt_init_slots(input, PIXCIR_MAX_SLOTS, |
431 | INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED); | |
432 | if (error) { | |
433 | dev_err(dev, "Error initializing Multi-Touch slots\n"); | |
434 | return error; | |
435 | } | |
436 | ||
36a281e2 JB |
437 | input_set_drvdata(input, tsdata); |
438 | ||
0dfc8d41 RQ |
439 | error = devm_gpio_request_one(dev, pdata->gpio_attb, |
440 | GPIOF_DIR_IN, "pixcir_i2c_attb"); | |
441 | if (error) { | |
442 | dev_err(dev, "Failed to request ATTB gpio\n"); | |
443 | return error; | |
444 | } | |
445 | ||
e9d4718d RQ |
446 | error = devm_request_threaded_irq(dev, client->irq, NULL, pixcir_ts_isr, |
447 | IRQF_TRIGGER_FALLING | IRQF_ONESHOT, | |
448 | client->name, tsdata); | |
36a281e2 | 449 | if (error) { |
e9d4718d RQ |
450 | dev_err(dev, "failed to request irq %d\n", client->irq); |
451 | return error; | |
36a281e2 JB |
452 | } |
453 | ||
3b36fbb0 RQ |
454 | /* Always be in IDLE mode to save power, device supports auto wake */ |
455 | error = pixcir_set_power_mode(tsdata, PIXCIR_POWER_IDLE); | |
456 | if (error) { | |
457 | dev_err(dev, "Failed to set IDLE mode\n"); | |
458 | return error; | |
459 | } | |
460 | ||
461 | /* Stop device till opened */ | |
462 | error = pixcir_stop(tsdata); | |
463 | if (error) | |
464 | return error; | |
465 | ||
36a281e2 JB |
466 | error = input_register_device(input); |
467 | if (error) | |
e9d4718d | 468 | return error; |
36a281e2 | 469 | |
7cdcb8d1 | 470 | i2c_set_clientdata(client, tsdata); |
36a281e2 JB |
471 | device_init_wakeup(&client->dev, 1); |
472 | ||
473 | return 0; | |
36a281e2 JB |
474 | } |
475 | ||
e2619cf7 | 476 | static int pixcir_i2c_ts_remove(struct i2c_client *client) |
36a281e2 | 477 | { |
36a281e2 JB |
478 | device_init_wakeup(&client->dev, 0); |
479 | ||
36a281e2 JB |
480 | return 0; |
481 | } | |
482 | ||
483 | static const struct i2c_device_id pixcir_i2c_ts_id[] = { | |
484 | { "pixcir_ts", 0 }, | |
485 | { } | |
486 | }; | |
487 | MODULE_DEVICE_TABLE(i2c, pixcir_i2c_ts_id); | |
488 | ||
489 | static struct i2c_driver pixcir_i2c_ts_driver = { | |
490 | .driver = { | |
491 | .owner = THIS_MODULE, | |
492 | .name = "pixcir_ts", | |
493 | .pm = &pixcir_dev_pm_ops, | |
494 | }, | |
495 | .probe = pixcir_i2c_ts_probe, | |
1cb0aa88 | 496 | .remove = pixcir_i2c_ts_remove, |
36a281e2 JB |
497 | .id_table = pixcir_i2c_ts_id, |
498 | }; | |
499 | ||
4a533835 | 500 | module_i2c_driver(pixcir_i2c_ts_driver); |
36a281e2 JB |
501 | |
502 | MODULE_AUTHOR("Jianchun Bian <jcbian@pixcir.com.cn>, Dequan Meng <dqmeng@pixcir.com.cn>"); | |
503 | MODULE_DESCRIPTION("Pixcir I2C Touchscreen Driver"); | |
504 | MODULE_LICENSE("GPL"); |