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 	} else if (input->users) {
381 		ret = pixcir_stop(ts);
382 	}
383 
384 unlock:
385 	mutex_unlock(&input->mutex);
386 
387 	return ret;
388 }
389 
390 static int __maybe_unused pixcir_i2c_ts_resume(struct device *dev)
391 {
392 	struct i2c_client *client = to_i2c_client(dev);
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);
398 
399 	if (device_may_wakeup(&client->dev)) {
400 
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 
412 unlock:
413 	mutex_unlock(&input->mutex);
414 
415 	return ret;
416 }
417 
418 static SIMPLE_DEV_PM_OPS(pixcir_dev_pm_ops,
419 			 pixcir_i2c_ts_suspend, pixcir_i2c_ts_resume);
420 
421 #ifdef CONFIG_OF
422 static const struct of_device_id pixcir_of_match[];
423 
424 static int pixcir_parse_dt(struct device *dev,
425 			   struct pixcir_i2c_ts_data *tsdata)
426 {
427 	const struct of_device_id *match;
428 
429 	match = of_match_device(of_match_ptr(pixcir_of_match), dev);
430 	if (!match)
431 		return -EINVAL;
432 
433 	tsdata->chip = (const struct pixcir_i2c_chip_data *)match->data;
434 	if (!tsdata->chip)
435 		return -EINVAL;
436 
437 	return 0;
438 }
439 #else
440 static int pixcir_parse_dt(struct device *dev,
441 			   struct pixcir_i2c_ts_data *tsdata)
442 {
443 	return -EINVAL;
444 }
445 #endif
446 
447 static int pixcir_i2c_ts_probe(struct i2c_client *client,
448 			       const struct i2c_device_id *id)
449 {
450 	const struct pixcir_ts_platform_data *pdata =
451 			dev_get_platdata(&client->dev);
452 	struct device *dev = &client->dev;
453 	struct pixcir_i2c_ts_data *tsdata;
454 	struct input_dev *input;
455 	int error;
456 
457 	tsdata = devm_kzalloc(dev, sizeof(*tsdata), GFP_KERNEL);
458 	if (!tsdata)
459 		return -ENOMEM;
460 
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 {
468 		dev_err(&client->dev, "platform data not defined\n");
469 		return -EINVAL;
470 	}
471 
472 	if (!tsdata->chip->max_fingers) {
473 		dev_err(dev, "Invalid max_fingers in chip data\n");
474 		return -EINVAL;
475 	}
476 
477 	input = devm_input_allocate_device(dev);
478 	if (!input) {
479 		dev_err(dev, "Failed to allocate input device\n");
480 		return -ENOMEM;
481 	}
482 
483 	tsdata->client = client;
484 	tsdata->input = input;
485 
486 	input->name = client->name;
487 	input->id.bustype = BUS_I2C;
488 	input->open = pixcir_input_open;
489 	input->close = pixcir_input_close;
490 	input->dev.parent = &client->dev;
491 
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 	}
505 
506 	tsdata->max_fingers = tsdata->chip->max_fingers;
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,
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 
520 	input_set_drvdata(input, tsdata);
521 
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);
526 		return error;
527 	}
528 
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 
537 	error = devm_request_threaded_irq(dev, client->irq, NULL, pixcir_ts_isr,
538 					  IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
539 					  client->name, tsdata);
540 	if (error) {
541 		dev_err(dev, "failed to request irq %d\n", client->irq);
542 		return error;
543 	}
544 
545 	pixcir_reset(tsdata);
546 
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 
559 	error = input_register_device(input);
560 	if (error)
561 		return error;
562 
563 	i2c_set_clientdata(client, tsdata);
564 
565 	return 0;
566 }
567 
568 static const struct i2c_device_id pixcir_i2c_ts_id[] = {
569 	{ "pixcir_ts", 0 },
570 	{ "pixcir_tangoc", 0 },
571 	{ }
572 };
573 MODULE_DEVICE_TABLE(i2c, pixcir_i2c_ts_id);
574 
575 #ifdef CONFIG_OF
576 static const struct pixcir_i2c_chip_data pixcir_ts_data = {
577 	.max_fingers = 2,
578 	/* no hw id support */
579 };
580 
581 static const struct pixcir_i2c_chip_data pixcir_tangoc_data = {
582 	.max_fingers = 5,
583 	.has_hw_ids = true,
584 };
585 
586 static 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 };
591 MODULE_DEVICE_TABLE(of, pixcir_of_match);
592 #endif
593 
594 static struct i2c_driver pixcir_i2c_ts_driver = {
595 	.driver = {
596 		.name	= "pixcir_ts",
597 		.pm	= &pixcir_dev_pm_ops,
598 		.of_match_table = of_match_ptr(pixcir_of_match),
599 	},
600 	.probe		= pixcir_i2c_ts_probe,
601 	.id_table	= pixcir_i2c_ts_id,
602 };
603 
604 module_i2c_driver(pixcir_i2c_ts_driver);
605 
606 MODULE_AUTHOR("Jianchun Bian <jcbian@pixcir.com.cn>, Dequan Meng <dqmeng@pixcir.com.cn>");
607 MODULE_DESCRIPTION("Pixcir I2C Touchscreen Driver");
608 MODULE_LICENSE("GPL");
609