]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/input/touchscreen/pixcir_i2c_ts.c
Merge tag 'vfio-v4.4-rc1' of git://github.com/awilliam/linux-vfio
[mirror_ubuntu-zesty-kernel.git] / drivers / input / touchscreen / pixcir_i2c_ts.c
CommitLineData
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>
d48259a0 27#include <linux/input/touchscreen.h>
0dfc8d41 28#include <linux/gpio.h>
cb4a5f06 29#include <linux/gpio/consumer.h>
d48259a0 30/*#include <linux/of.h>*/
a4054596 31#include <linux/of_device.h>
28a74c05 32#include <linux/platform_data/pixcir_i2c_ts.h>
36a281e2 33
36874c7e 34#define PIXCIR_MAX_SLOTS 5 /* Max fingers supported by driver */
62e65b7e 35
36a281e2
JB
36struct pixcir_i2c_ts_data {
37 struct i2c_client *client;
38 struct input_dev *input;
cb4a5f06 39 struct gpio_desc *gpio_attb;
40929167 40 struct gpio_desc *gpio_reset;
d48259a0 41 const struct pixcir_i2c_chip_data *chip;
36874c7e 42 int max_fingers; /* Max fingers supported in this instance */
d48259a0 43 bool running;
36a281e2
JB
44};
45
62e65b7e
RQ
46struct pixcir_touch {
47 int x;
48 int y;
36874c7e 49 int id;
62e65b7e
RQ
50};
51
52struct pixcir_report_data {
53 int num_touches;
54 struct pixcir_touch touches[PIXCIR_MAX_SLOTS];
55};
56
57static void pixcir_ts_parse(struct pixcir_i2c_ts_data *tsdata,
58 struct pixcir_report_data *report)
36a281e2 59{
36874c7e
RQ
60 u8 rdbuf[2 + PIXCIR_MAX_SLOTS * 5];
61 u8 wrbuf[1] = { 0 };
62e65b7e 62 u8 *bufptr;
36a281e2 63 u8 touch;
62e65b7e 64 int ret, i;
36874c7e 65 int readsize;
d48259a0 66 const struct pixcir_i2c_chip_data *chip = tsdata->chip;
62e65b7e
RQ
67
68 memset(report, 0, sizeof(struct pixcir_report_data));
36a281e2 69
36874c7e
RQ
70 i = chip->has_hw_ids ? 1 : 0;
71 readsize = 2 + tsdata->max_fingers * (4 + i);
72 if (readsize > sizeof(rdbuf))
73 readsize = sizeof(rdbuf);
74
36a281e2
JB
75 ret = i2c_master_send(tsdata->client, wrbuf, sizeof(wrbuf));
76 if (ret != sizeof(wrbuf)) {
77 dev_err(&tsdata->client->dev,
78 "%s: i2c_master_send failed(), ret=%d\n",
79 __func__, ret);
80 return;
81 }
82
36874c7e 83 ret = i2c_master_recv(tsdata->client, rdbuf, readsize);
469d7d22 84 if (ret != readsize) {
36a281e2
JB
85 dev_err(&tsdata->client->dev,
86 "%s: i2c_master_recv failed(), ret=%d\n",
87 __func__, ret);
88 return;
89 }
90
62e65b7e 91 touch = rdbuf[0] & 0x7;
36874c7e
RQ
92 if (touch > tsdata->max_fingers)
93 touch = tsdata->max_fingers;
62e65b7e
RQ
94
95 report->num_touches = touch;
96 bufptr = &rdbuf[2];
97
98 for (i = 0; i < touch; i++) {
99 report->touches[i].x = (bufptr[1] << 8) | bufptr[0];
100 report->touches[i].y = (bufptr[3] << 8) | bufptr[2];
101
36874c7e
RQ
102 if (chip->has_hw_ids) {
103 report->touches[i].id = bufptr[4];
104 bufptr = bufptr + 5;
105 } else {
106 bufptr = bufptr + 4;
107 }
36a281e2 108 }
62e65b7e
RQ
109}
110
111static void pixcir_ts_report(struct pixcir_i2c_ts_data *ts,
112 struct pixcir_report_data *report)
113{
114 struct input_mt_pos pos[PIXCIR_MAX_SLOTS];
115 int slots[PIXCIR_MAX_SLOTS];
116 struct pixcir_touch *touch;
117 int n, i, slot;
118 struct device *dev = &ts->client->dev;
d48259a0 119 const struct pixcir_i2c_chip_data *chip = ts->chip;
62e65b7e
RQ
120
121 n = report->num_touches;
122 if (n > PIXCIR_MAX_SLOTS)
123 n = PIXCIR_MAX_SLOTS;
36a281e2 124
d48259a0 125 if (!ts->chip->has_hw_ids) {
36874c7e
RQ
126 for (i = 0; i < n; i++) {
127 touch = &report->touches[i];
128 pos[i].x = touch->x;
129 pos[i].y = touch->y;
130 }
62e65b7e 131
448c7f38 132 input_mt_assign_slots(ts->input, slots, pos, n, 0);
36874c7e 133 }
62e65b7e
RQ
134
135 for (i = 0; i < n; i++) {
136 touch = &report->touches[i];
36874c7e
RQ
137
138 if (chip->has_hw_ids) {
139 slot = input_mt_get_slot_by_key(ts->input, touch->id);
140 if (slot < 0) {
141 dev_dbg(dev, "no free slot for id 0x%x\n",
142 touch->id);
143 continue;
144 }
145 } else {
146 slot = slots[i];
147 }
62e65b7e
RQ
148
149 input_mt_slot(ts->input, slot);
150 input_mt_report_slot_state(ts->input,
151 MT_TOOL_FINGER, true);
152
153 input_event(ts->input, EV_ABS, ABS_MT_POSITION_X, touch->x);
154 input_event(ts->input, EV_ABS, ABS_MT_POSITION_Y, touch->y);
155
156 dev_dbg(dev, "%d: slot %d, x %d, y %d\n",
157 i, slot, touch->x, touch->y);
158 }
159
160 input_mt_sync_frame(ts->input);
161 input_sync(ts->input);
36a281e2
JB
162}
163
164static irqreturn_t pixcir_ts_isr(int irq, void *dev_id)
165{
166 struct pixcir_i2c_ts_data *tsdata = dev_id;
62e65b7e 167 struct pixcir_report_data report;
36a281e2 168
3b36fbb0 169 while (tsdata->running) {
62e65b7e
RQ
170 /* parse packet */
171 pixcir_ts_parse(tsdata, &report);
172
173 /* report it */
174 pixcir_ts_report(tsdata, &report);
175
127520ca 176 if (gpiod_get_value_cansleep(tsdata->gpio_attb)) {
62e65b7e
RQ
177 if (report.num_touches) {
178 /*
179 * Last report with no finger up?
180 * Do it now then.
181 */
182 input_mt_sync_frame(tsdata->input);
183 input_sync(tsdata->input);
184 }
36a281e2 185 break;
62e65b7e 186 }
36a281e2
JB
187
188 msleep(20);
189 }
190
191 return IRQ_HANDLED;
192}
193
40929167
RQ
194static void pixcir_reset(struct pixcir_i2c_ts_data *tsdata)
195{
196 if (!IS_ERR_OR_NULL(tsdata->gpio_reset)) {
197 gpiod_set_value_cansleep(tsdata->gpio_reset, 1);
198 ndelay(100); /* datasheet section 1.2.3 says 80ns min. */
199 gpiod_set_value_cansleep(tsdata->gpio_reset, 0);
200 /* wait for controller ready. 100ms guess. */
201 msleep(100);
202 }
203}
204
3b36fbb0
RQ
205static int pixcir_set_power_mode(struct pixcir_i2c_ts_data *ts,
206 enum pixcir_power_mode mode)
207{
208 struct device *dev = &ts->client->dev;
209 int ret;
210
211 ret = i2c_smbus_read_byte_data(ts->client, PIXCIR_REG_POWER_MODE);
212 if (ret < 0) {
213 dev_err(dev, "%s: can't read reg 0x%x : %d\n",
214 __func__, PIXCIR_REG_POWER_MODE, ret);
215 return ret;
216 }
217
218 ret &= ~PIXCIR_POWER_MODE_MASK;
219 ret |= mode;
220
221 /* Always AUTO_IDLE */
222 ret |= PIXCIR_POWER_ALLOW_IDLE;
223
224 ret = i2c_smbus_write_byte_data(ts->client, PIXCIR_REG_POWER_MODE, ret);
225 if (ret < 0) {
226 dev_err(dev, "%s: can't write reg 0x%x : %d\n",
227 __func__, PIXCIR_REG_POWER_MODE, ret);
228 return ret;
229 }
230
231 return 0;
232}
233
234/*
235 * Set the interrupt mode for the device i.e. ATTB line behaviour
236 *
237 * @polarity : 1 for active high, 0 for active low.
238 */
239static int pixcir_set_int_mode(struct pixcir_i2c_ts_data *ts,
240 enum pixcir_int_mode mode, bool polarity)
241{
242 struct device *dev = &ts->client->dev;
243 int ret;
244
245 ret = i2c_smbus_read_byte_data(ts->client, PIXCIR_REG_INT_MODE);
246 if (ret < 0) {
247 dev_err(dev, "%s: can't read reg 0x%x : %d\n",
248 __func__, PIXCIR_REG_INT_MODE, ret);
249 return ret;
250 }
251
252 ret &= ~PIXCIR_INT_MODE_MASK;
253 ret |= mode;
254
255 if (polarity)
256 ret |= PIXCIR_INT_POL_HIGH;
257 else
258 ret &= ~PIXCIR_INT_POL_HIGH;
259
260 ret = i2c_smbus_write_byte_data(ts->client, PIXCIR_REG_INT_MODE, ret);
261 if (ret < 0) {
262 dev_err(dev, "%s: can't write reg 0x%x : %d\n",
263 __func__, PIXCIR_REG_INT_MODE, ret);
264 return ret;
265 }
266
267 return 0;
268}
269
270/*
271 * Enable/disable interrupt generation
272 */
273static int pixcir_int_enable(struct pixcir_i2c_ts_data *ts, bool enable)
274{
275 struct device *dev = &ts->client->dev;
276 int ret;
277
278 ret = i2c_smbus_read_byte_data(ts->client, PIXCIR_REG_INT_MODE);
279 if (ret < 0) {
280 dev_err(dev, "%s: can't read reg 0x%x : %d\n",
281 __func__, PIXCIR_REG_INT_MODE, ret);
282 return ret;
283 }
284
285 if (enable)
286 ret |= PIXCIR_INT_ENABLE;
287 else
288 ret &= ~PIXCIR_INT_ENABLE;
289
290 ret = i2c_smbus_write_byte_data(ts->client, PIXCIR_REG_INT_MODE, ret);
291 if (ret < 0) {
292 dev_err(dev, "%s: can't write reg 0x%x : %d\n",
293 __func__, PIXCIR_REG_INT_MODE, ret);
294 return ret;
295 }
296
297 return 0;
298}
299
300static int pixcir_start(struct pixcir_i2c_ts_data *ts)
301{
302 struct device *dev = &ts->client->dev;
303 int error;
304
305 /* LEVEL_TOUCH interrupt with active low polarity */
306 error = pixcir_set_int_mode(ts, PIXCIR_INT_LEVEL_TOUCH, 0);
307 if (error) {
308 dev_err(dev, "Failed to set interrupt mode: %d\n", error);
309 return error;
310 }
311
312 ts->running = true;
313 mb(); /* Update status before IRQ can fire */
314
315 /* enable interrupt generation */
316 error = pixcir_int_enable(ts, true);
317 if (error) {
318 dev_err(dev, "Failed to enable interrupt generation: %d\n",
319 error);
320 return error;
321 }
322
323 return 0;
324}
325
326static int pixcir_stop(struct pixcir_i2c_ts_data *ts)
327{
328 int error;
329
330 /* Disable interrupt generation */
331 error = pixcir_int_enable(ts, false);
332 if (error) {
333 dev_err(&ts->client->dev,
334 "Failed to disable interrupt generation: %d\n",
335 error);
336 return error;
337 }
338
339 /* Exit ISR if running, no more report parsing */
340 ts->running = false;
341 mb(); /* update status before we synchronize irq */
342
343 /* Wait till running ISR is complete */
344 synchronize_irq(ts->client->irq);
345
346 return 0;
347}
348
349static int pixcir_input_open(struct input_dev *dev)
350{
351 struct pixcir_i2c_ts_data *ts = input_get_drvdata(dev);
352
353 return pixcir_start(ts);
354}
355
356static void pixcir_input_close(struct input_dev *dev)
357{
358 struct pixcir_i2c_ts_data *ts = input_get_drvdata(dev);
359
360 pixcir_stop(ts);
361}
362
02b6a58b 363static int __maybe_unused pixcir_i2c_ts_suspend(struct device *dev)
36a281e2
JB
364{
365 struct i2c_client *client = to_i2c_client(dev);
7cdcb8d1
RQ
366 struct pixcir_i2c_ts_data *ts = i2c_get_clientdata(client);
367 struct input_dev *input = ts->input;
368 int ret = 0;
369
370 mutex_lock(&input->mutex);
371
372 if (device_may_wakeup(&client->dev)) {
373 if (!input->users) {
374 ret = pixcir_start(ts);
375 if (ret) {
376 dev_err(dev, "Failed to start\n");
377 goto unlock;
378 }
379 }
7cdcb8d1
RQ
380 } else if (input->users) {
381 ret = pixcir_stop(ts);
382 }
36a281e2 383
7cdcb8d1
RQ
384unlock:
385 mutex_unlock(&input->mutex);
386
387 return ret;
36a281e2
JB
388}
389
02b6a58b 390static int __maybe_unused pixcir_i2c_ts_resume(struct device *dev)
36a281e2
JB
391{
392 struct i2c_client *client = to_i2c_client(dev);
7cdcb8d1
RQ
393 struct pixcir_i2c_ts_data *ts = i2c_get_clientdata(client);
394 struct input_dev *input = ts->input;
395 int ret = 0;
396
397 mutex_lock(&input->mutex);
36a281e2 398
7cdcb8d1 399 if (device_may_wakeup(&client->dev)) {
36a281e2 400
7cdcb8d1
RQ
401 if (!input->users) {
402 ret = pixcir_stop(ts);
403 if (ret) {
404 dev_err(dev, "Failed to stop\n");
405 goto unlock;
406 }
407 }
408 } else if (input->users) {
409 ret = pixcir_start(ts);
410 }
411
412unlock:
413 mutex_unlock(&input->mutex);
414
415 return ret;
36a281e2 416}
36a281e2
JB
417
418static SIMPLE_DEV_PM_OPS(pixcir_dev_pm_ops,
419 pixcir_i2c_ts_suspend, pixcir_i2c_ts_resume);
420
a4054596
RQ
421#ifdef CONFIG_OF
422static const struct of_device_id pixcir_of_match[];
423
d48259a0
DT
424static int pixcir_parse_dt(struct device *dev,
425 struct pixcir_i2c_ts_data *tsdata)
a4054596 426{
a4054596
RQ
427 const struct of_device_id *match;
428
429 match = of_match_device(of_match_ptr(pixcir_of_match), dev);
430 if (!match)
d48259a0 431 return -EINVAL;
a4054596 432
d48259a0
DT
433 tsdata->chip = (const struct pixcir_i2c_chip_data *)match->data;
434 if (!tsdata->chip)
435 return -EINVAL;
a4054596 436
d48259a0 437 return 0;
a4054596
RQ
438}
439#else
d48259a0
DT
440static int pixcir_parse_dt(struct device *dev,
441 struct pixcir_i2c_ts_data *tsdata)
a4054596 442{
d48259a0 443 return -EINVAL;
a4054596
RQ
444}
445#endif
446
5298cc4c 447static int pixcir_i2c_ts_probe(struct i2c_client *client,
d48259a0 448 const struct i2c_device_id *id)
36a281e2 449{
c838cb3d
JH
450 const struct pixcir_ts_platform_data *pdata =
451 dev_get_platdata(&client->dev);
e9d4718d 452 struct device *dev = &client->dev;
36a281e2
JB
453 struct pixcir_i2c_ts_data *tsdata;
454 struct input_dev *input;
455 int error;
456
d48259a0
DT
457 tsdata = devm_kzalloc(dev, sizeof(*tsdata), GFP_KERNEL);
458 if (!tsdata)
459 return -ENOMEM;
a4054596 460
d48259a0
DT
461 if (pdata) {
462 tsdata->chip = &pdata->chip;
463 } else if (dev->of_node) {
464 error = pixcir_parse_dt(dev, tsdata);
465 if (error)
466 return error;
467 } else {
36a281e2
JB
468 dev_err(&client->dev, "platform data not defined\n");
469 return -EINVAL;
470 }
471
d48259a0
DT
472 if (!tsdata->chip->max_fingers) {
473 dev_err(dev, "Invalid max_fingers in chip data\n");
36874c7e
RQ
474 return -EINVAL;
475 }
476
e9d4718d
RQ
477 input = devm_input_allocate_device(dev);
478 if (!input) {
479 dev_err(dev, "Failed to allocate input device\n");
480 return -ENOMEM;
36a281e2
JB
481 }
482
483 tsdata->client = client;
484 tsdata->input = input;
36a281e2
JB
485
486 input->name = client->name;
487 input->id.bustype = BUS_I2C;
3b36fbb0
RQ
488 input->open = pixcir_input_open;
489 input->close = pixcir_input_close;
36a281e2
JB
490 input->dev.parent = &client->dev;
491
d48259a0
DT
492 if (pdata) {
493 input_set_abs_params(input, ABS_MT_POSITION_X, 0, pdata->x_max, 0, 0);
494 input_set_abs_params(input, ABS_MT_POSITION_Y, 0, pdata->y_max, 0, 0);
495 } else {
496 input_set_capability(input, EV_ABS, ABS_MT_POSITION_X);
497 input_set_capability(input, EV_ABS, ABS_MT_POSITION_Y);
498 touchscreen_parse_properties(input, true);
499 if (!input_abs_get_max(input, ABS_MT_POSITION_X) ||
500 !input_abs_get_max(input, ABS_MT_POSITION_Y)) {
501 dev_err(dev, "Touchscreen size is not specified\n");
502 return -EINVAL;
503 }
504 }
36a281e2 505
d48259a0 506 tsdata->max_fingers = tsdata->chip->max_fingers;
36874c7e
RQ
507 if (tsdata->max_fingers > PIXCIR_MAX_SLOTS) {
508 tsdata->max_fingers = PIXCIR_MAX_SLOTS;
509 dev_info(dev, "Limiting maximum fingers to %d\n",
510 tsdata->max_fingers);
511 }
512
513 error = input_mt_init_slots(input, tsdata->max_fingers,
62e65b7e
RQ
514 INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
515 if (error) {
516 dev_err(dev, "Error initializing Multi-Touch slots\n");
517 return error;
518 }
519
36a281e2
JB
520 input_set_drvdata(input, tsdata);
521
cb4a5f06
DT
522 tsdata->gpio_attb = devm_gpiod_get(dev, "attb", GPIOD_IN);
523 if (IS_ERR(tsdata->gpio_attb)) {
524 error = PTR_ERR(tsdata->gpio_attb);
525 dev_err(dev, "Failed to request ATTB gpio: %d\n", error);
0dfc8d41
RQ
526 return error;
527 }
528
40929167
RQ
529 tsdata->gpio_reset = devm_gpiod_get_optional(dev, "reset",
530 GPIOD_OUT_LOW);
531 if (IS_ERR(tsdata->gpio_reset)) {
532 error = PTR_ERR(tsdata->gpio_reset);
533 dev_err(dev, "Failed to request RESET gpio: %d\n", error);
534 return error;
535 }
536
e9d4718d
RQ
537 error = devm_request_threaded_irq(dev, client->irq, NULL, pixcir_ts_isr,
538 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
539 client->name, tsdata);
36a281e2 540 if (error) {
e9d4718d
RQ
541 dev_err(dev, "failed to request irq %d\n", client->irq);
542 return error;
36a281e2
JB
543 }
544
40929167
RQ
545 pixcir_reset(tsdata);
546
3b36fbb0
RQ
547 /* Always be in IDLE mode to save power, device supports auto wake */
548 error = pixcir_set_power_mode(tsdata, PIXCIR_POWER_IDLE);
549 if (error) {
550 dev_err(dev, "Failed to set IDLE mode\n");
551 return error;
552 }
553
554 /* Stop device till opened */
555 error = pixcir_stop(tsdata);
556 if (error)
557 return error;
558
36a281e2
JB
559 error = input_register_device(input);
560 if (error)
e9d4718d 561 return error;
36a281e2 562
7cdcb8d1 563 i2c_set_clientdata(client, tsdata);
36a281e2 564
36a281e2
JB
565 return 0;
566}
567
568static const struct i2c_device_id pixcir_i2c_ts_id[] = {
569 { "pixcir_ts", 0 },
a4054596 570 { "pixcir_tangoc", 0 },
36a281e2
JB
571 { }
572};
573MODULE_DEVICE_TABLE(i2c, pixcir_i2c_ts_id);
574
a4054596
RQ
575#ifdef CONFIG_OF
576static const struct pixcir_i2c_chip_data pixcir_ts_data = {
577 .max_fingers = 2,
578 /* no hw id support */
579};
580
581static const struct pixcir_i2c_chip_data pixcir_tangoc_data = {
582 .max_fingers = 5,
583 .has_hw_ids = true,
584};
585
586static const struct of_device_id pixcir_of_match[] = {
587 { .compatible = "pixcir,pixcir_ts", .data = &pixcir_ts_data },
588 { .compatible = "pixcir,pixcir_tangoc", .data = &pixcir_tangoc_data },
589 { }
590};
591MODULE_DEVICE_TABLE(of, pixcir_of_match);
592#endif
593
36a281e2
JB
594static struct i2c_driver pixcir_i2c_ts_driver = {
595 .driver = {
36a281e2
JB
596 .name = "pixcir_ts",
597 .pm = &pixcir_dev_pm_ops,
a4054596 598 .of_match_table = of_match_ptr(pixcir_of_match),
36a281e2
JB
599 },
600 .probe = pixcir_i2c_ts_probe,
36a281e2
JB
601 .id_table = pixcir_i2c_ts_id,
602};
603
4a533835 604module_i2c_driver(pixcir_i2c_ts_driver);
36a281e2
JB
605
606MODULE_AUTHOR("Jianchun Bian <jcbian@pixcir.com.cn>, Dequan Meng <dqmeng@pixcir.com.cn>");
607MODULE_DESCRIPTION("Pixcir I2C Touchscreen Driver");
608MODULE_LICENSE("GPL");