Linux Audio

Check our new training course

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