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