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.h>*/
31 #include <linux/of_device.h>
32 #include <linux/platform_data/pixcir_i2c_ts.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 	const struct pixcir_i2c_chip_data *chip;
42 	int max_fingers;	/* Max fingers supported in this instance */
43 	bool running;
44 };
45 
46 struct pixcir_touch {
47 	int x;
48 	int y;
49 	int id;
50 };
51 
52 struct pixcir_report_data {
53 	int num_touches;
54 	struct pixcir_touch touches[PIXCIR_MAX_SLOTS];
55 };
56 
57 static void pixcir_ts_parse(struct pixcir_i2c_ts_data *tsdata,
58 			    struct pixcir_report_data *report)
59 {
60 	u8 rdbuf[2 + PIXCIR_MAX_SLOTS * 5];
61 	u8 wrbuf[1] = { 0 };
62 	u8 *bufptr;
63 	u8 touch;
64 	int ret, i;
65 	int readsize;
66 	const struct pixcir_i2c_chip_data *chip = tsdata->chip;
67 
68 	memset(report, 0, sizeof(struct pixcir_report_data));
69 
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 
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 
83 	ret = i2c_master_recv(tsdata->client, rdbuf, readsize);
84 	if (ret != readsize) {
85 		dev_err(&tsdata->client->dev,
86 			"%s: i2c_master_recv failed(), ret=%d\n",
87 			__func__, ret);
88 		return;
89 	}
90 
91 	touch = rdbuf[0] & 0x7;
92 	if (touch > tsdata->max_fingers)
93 		touch = tsdata->max_fingers;
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 
102 		if (chip->has_hw_ids) {
103 			report->touches[i].id = bufptr[4];
104 			bufptr = bufptr + 5;
105 		} else {
106 			bufptr = bufptr + 4;
107 		}
108 	}
109 }
110 
111 static 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;
119 	const struct pixcir_i2c_chip_data *chip = ts->chip;
120 
121 	n = report->num_touches;
122 	if (n > PIXCIR_MAX_SLOTS)
123 		n = PIXCIR_MAX_SLOTS;
124 
125 	if (!ts->chip->has_hw_ids) {
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 		}
131 
132 		input_mt_assign_slots(ts->input, slots, pos, n, 0);
133 	}
134 
135 	for (i = 0; i < n; i++) {
136 		touch = &report->touches[i];
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 		}
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);
162 }
163 
164 static irqreturn_t pixcir_ts_isr(int irq, void *dev_id)
165 {
166 	struct pixcir_i2c_ts_data *tsdata = dev_id;
167 	struct pixcir_report_data report;
168 
169 	while (tsdata->running) {
170 		/* parse packet */
171 		pixcir_ts_parse(tsdata, &report);
172 
173 		/* report it */
174 		pixcir_ts_report(tsdata, &report);
175 
176 		if (gpiod_get_value_cansleep(tsdata->gpio_attb)) {
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 			}
185 			break;
186 		}
187 
188 		msleep(20);
189 	}
190 
191 	return IRQ_HANDLED;
192 }
193 
194 static 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 
205 static 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  */
239 static 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  */
273 static 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 
300 static 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 
326 static 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 
349 static 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 
356 static 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 
363 static int __maybe_unused pixcir_i2c_ts_suspend(struct device *dev)
364 {
365 	struct i2c_client *client = to_i2c_client(dev);
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 		}
380 
381 		enable_irq_wake(client->irq);
382 	} else if (input->users) {
383 		ret = pixcir_stop(ts);
384 	}
385 
386 unlock:
387 	mutex_unlock(&input->mutex);
388 
389 	return ret;
390 }
391 
392 static int __maybe_unused pixcir_i2c_ts_resume(struct device *dev)
393 {
394 	struct i2c_client *client = to_i2c_client(dev);
395 	struct pixcir_i2c_ts_data *ts = i2c_get_clientdata(client);
396 	struct input_dev *input = ts->input;
397 	int ret = 0;
398 
399 	mutex_lock(&input->mutex);
400 
401 	if (device_may_wakeup(&client->dev)) {
402 		disable_irq_wake(client->irq);
403 
404 		if (!input->users) {
405 			ret = pixcir_stop(ts);
406 			if (ret) {
407 				dev_err(dev, "Failed to stop\n");
408 				goto unlock;
409 			}
410 		}
411 	} else if (input->users) {
412 		ret = pixcir_start(ts);
413 	}
414 
415 unlock:
416 	mutex_unlock(&input->mutex);
417 
418 	return ret;
419 }
420 
421 static SIMPLE_DEV_PM_OPS(pixcir_dev_pm_ops,
422 			 pixcir_i2c_ts_suspend, pixcir_i2c_ts_resume);
423 
424 #ifdef CONFIG_OF
425 static const struct of_device_id pixcir_of_match[];
426 
427 static int pixcir_parse_dt(struct device *dev,
428 			   struct pixcir_i2c_ts_data *tsdata)
429 {
430 	const struct of_device_id *match;
431 
432 	match = of_match_device(of_match_ptr(pixcir_of_match), dev);
433 	if (!match)
434 		return -EINVAL;
435 
436 	tsdata->chip = (const struct pixcir_i2c_chip_data *)match->data;
437 	if (!tsdata->chip)
438 		return -EINVAL;
439 
440 	return 0;
441 }
442 #else
443 static int pixcir_parse_dt(struct device *dev,
444 			   struct pixcir_i2c_ts_data *tsdata)
445 {
446 	return -EINVAL;
447 }
448 #endif
449 
450 static int pixcir_i2c_ts_probe(struct i2c_client *client,
451 			       const struct i2c_device_id *id)
452 {
453 	const struct pixcir_ts_platform_data *pdata =
454 			dev_get_platdata(&client->dev);
455 	struct device *dev = &client->dev;
456 	struct pixcir_i2c_ts_data *tsdata;
457 	struct input_dev *input;
458 	int error;
459 
460 	tsdata = devm_kzalloc(dev, sizeof(*tsdata), GFP_KERNEL);
461 	if (!tsdata)
462 		return -ENOMEM;
463 
464 	if (pdata) {
465 		tsdata->chip = &pdata->chip;
466 	} else if (dev->of_node) {
467 		error = pixcir_parse_dt(dev, tsdata);
468 		if (error)
469 			return error;
470 	} else {
471 		dev_err(&client->dev, "platform data not defined\n");
472 		return -EINVAL;
473 	}
474 
475 	if (!tsdata->chip->max_fingers) {
476 		dev_err(dev, "Invalid max_fingers in chip data\n");
477 		return -EINVAL;
478 	}
479 
480 	input = devm_input_allocate_device(dev);
481 	if (!input) {
482 		dev_err(dev, "Failed to allocate input device\n");
483 		return -ENOMEM;
484 	}
485 
486 	tsdata->client = client;
487 	tsdata->input = input;
488 
489 	input->name = client->name;
490 	input->id.bustype = BUS_I2C;
491 	input->open = pixcir_input_open;
492 	input->close = pixcir_input_close;
493 	input->dev.parent = &client->dev;
494 
495 	if (pdata) {
496 		input_set_abs_params(input, ABS_MT_POSITION_X, 0, pdata->x_max, 0, 0);
497 		input_set_abs_params(input, ABS_MT_POSITION_Y, 0, pdata->y_max, 0, 0);
498 	} else {
499 		input_set_capability(input, EV_ABS, ABS_MT_POSITION_X);
500 		input_set_capability(input, EV_ABS, ABS_MT_POSITION_Y);
501 		touchscreen_parse_properties(input, true);
502 		if (!input_abs_get_max(input, ABS_MT_POSITION_X) ||
503 		    !input_abs_get_max(input, ABS_MT_POSITION_Y)) {
504 			dev_err(dev, "Touchscreen size is not specified\n");
505 			return -EINVAL;
506 		}
507 	}
508 
509 	tsdata->max_fingers = tsdata->chip->max_fingers;
510 	if (tsdata->max_fingers > PIXCIR_MAX_SLOTS) {
511 		tsdata->max_fingers = PIXCIR_MAX_SLOTS;
512 		dev_info(dev, "Limiting maximum fingers to %d\n",
513 			 tsdata->max_fingers);
514 	}
515 
516 	error = input_mt_init_slots(input, tsdata->max_fingers,
517 				    INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
518 	if (error) {
519 		dev_err(dev, "Error initializing Multi-Touch slots\n");
520 		return error;
521 	}
522 
523 	input_set_drvdata(input, tsdata);
524 
525 	tsdata->gpio_attb = devm_gpiod_get(dev, "attb", GPIOD_IN);
526 	if (IS_ERR(tsdata->gpio_attb)) {
527 		error = PTR_ERR(tsdata->gpio_attb);
528 		dev_err(dev, "Failed to request ATTB gpio: %d\n", error);
529 		return error;
530 	}
531 
532 	tsdata->gpio_reset = devm_gpiod_get_optional(dev, "reset",
533 						     GPIOD_OUT_LOW);
534 	if (IS_ERR(tsdata->gpio_reset)) {
535 		error = PTR_ERR(tsdata->gpio_reset);
536 		dev_err(dev, "Failed to request RESET gpio: %d\n", error);
537 		return error;
538 	}
539 
540 	error = devm_request_threaded_irq(dev, client->irq, NULL, pixcir_ts_isr,
541 					  IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
542 					  client->name, tsdata);
543 	if (error) {
544 		dev_err(dev, "failed to request irq %d\n", client->irq);
545 		return error;
546 	}
547 
548 	pixcir_reset(tsdata);
549 
550 	/* Always be in IDLE mode to save power, device supports auto wake */
551 	error = pixcir_set_power_mode(tsdata, PIXCIR_POWER_IDLE);
552 	if (error) {
553 		dev_err(dev, "Failed to set IDLE mode\n");
554 		return error;
555 	}
556 
557 	/* Stop device till opened */
558 	error = pixcir_stop(tsdata);
559 	if (error)
560 		return error;
561 
562 	error = input_register_device(input);
563 	if (error)
564 		return error;
565 
566 	i2c_set_clientdata(client, tsdata);
567 	device_init_wakeup(&client->dev, 1);
568 
569 	return 0;
570 }
571 
572 static int pixcir_i2c_ts_remove(struct i2c_client *client)
573 {
574 	device_init_wakeup(&client->dev, 0);
575 
576 	return 0;
577 }
578 
579 static const struct i2c_device_id pixcir_i2c_ts_id[] = {
580 	{ "pixcir_ts", 0 },
581 	{ "pixcir_tangoc", 0 },
582 	{ }
583 };
584 MODULE_DEVICE_TABLE(i2c, pixcir_i2c_ts_id);
585 
586 #ifdef CONFIG_OF
587 static const struct pixcir_i2c_chip_data pixcir_ts_data = {
588 	.max_fingers = 2,
589 	/* no hw id support */
590 };
591 
592 static const struct pixcir_i2c_chip_data pixcir_tangoc_data = {
593 	.max_fingers = 5,
594 	.has_hw_ids = true,
595 };
596 
597 static const struct of_device_id pixcir_of_match[] = {
598 	{ .compatible = "pixcir,pixcir_ts", .data = &pixcir_ts_data },
599 	{ .compatible = "pixcir,pixcir_tangoc", .data = &pixcir_tangoc_data },
600 	{ }
601 };
602 MODULE_DEVICE_TABLE(of, pixcir_of_match);
603 #endif
604 
605 static struct i2c_driver pixcir_i2c_ts_driver = {
606 	.driver = {
607 		.name	= "pixcir_ts",
608 		.pm	= &pixcir_dev_pm_ops,
609 		.of_match_table = of_match_ptr(pixcir_of_match),
610 	},
611 	.probe		= pixcir_i2c_ts_probe,
612 	.remove		= pixcir_i2c_ts_remove,
613 	.id_table	= pixcir_i2c_ts_id,
614 };
615 
616 module_i2c_driver(pixcir_i2c_ts_driver);
617 
618 MODULE_AUTHOR("Jianchun Bian <jcbian@pixcir.com.cn>, Dequan Meng <dqmeng@pixcir.com.cn>");
619 MODULE_DESCRIPTION("Pixcir I2C Touchscreen Driver");
620 MODULE_LICENSE("GPL");
621