2 * Driver for Pixcir I2C touchscreen controllers.
4 * Copyright (C) 2010-2011 Pixcir, Inc.
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.
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.
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
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>
34 #define PIXCIR_MAX_SLOTS 5 /* Max fingers supported by driver */
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 int max_fingers; /* Max fingers supported in this instance */
54 struct pixcir_report_data {
56 struct pixcir_touch touches[PIXCIR_MAX_SLOTS];
59 static void pixcir_ts_parse(struct pixcir_i2c_ts_data *tsdata,
60 struct pixcir_report_data *report)
62 u8 rdbuf[2 + PIXCIR_MAX_SLOTS * 5];
68 const struct pixcir_i2c_chip_data *chip = tsdata->chip;
70 memset(report, 0, sizeof(struct pixcir_report_data));
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);
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",
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",
93 touch = rdbuf[0] & 0x7;
94 if (touch > tsdata->max_fingers)
95 touch = tsdata->max_fingers;
97 report->num_touches = touch;
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];
104 if (chip->has_hw_ids) {
105 report->touches[i].id = bufptr[4];
113 static void pixcir_ts_report(struct pixcir_i2c_ts_data *ts,
114 struct pixcir_report_data *report)
116 struct input_mt_pos pos[PIXCIR_MAX_SLOTS];
117 int slots[PIXCIR_MAX_SLOTS];
118 struct pixcir_touch *touch;
120 struct device *dev = &ts->client->dev;
121 const struct pixcir_i2c_chip_data *chip = ts->chip;
123 n = report->num_touches;
124 if (n > PIXCIR_MAX_SLOTS)
125 n = PIXCIR_MAX_SLOTS;
127 if (!ts->chip->has_hw_ids) {
128 for (i = 0; i < n; i++) {
129 touch = &report->touches[i];
134 input_mt_assign_slots(ts->input, slots, pos, n, 0);
137 for (i = 0; i < n; i++) {
138 touch = &report->touches[i];
140 if (chip->has_hw_ids) {
141 slot = input_mt_get_slot_by_key(ts->input, touch->id);
143 dev_dbg(dev, "no free slot for id 0x%x\n",
151 input_mt_slot(ts->input, slot);
152 input_mt_report_slot_state(ts->input,
153 MT_TOOL_FINGER, true);
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);
158 dev_dbg(dev, "%d: slot %d, x %d, y %d\n",
159 i, slot, touch->x, touch->y);
162 input_mt_sync_frame(ts->input);
163 input_sync(ts->input);
166 static irqreturn_t pixcir_ts_isr(int irq, void *dev_id)
168 struct pixcir_i2c_ts_data *tsdata = dev_id;
169 struct pixcir_report_data report;
171 while (tsdata->running) {
173 pixcir_ts_parse(tsdata, &report);
176 pixcir_ts_report(tsdata, &report);
178 if (gpiod_get_value_cansleep(tsdata->gpio_attb)) {
179 if (report.num_touches) {
181 * Last report with no finger up?
184 input_mt_sync_frame(tsdata->input);
185 input_sync(tsdata->input);
196 static void pixcir_reset(struct pixcir_i2c_ts_data *tsdata)
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. */
207 static int pixcir_set_power_mode(struct pixcir_i2c_ts_data *ts,
208 enum pixcir_power_mode mode)
210 struct device *dev = &ts->client->dev;
213 if (mode == PIXCIR_POWER_ACTIVE || mode == PIXCIR_POWER_IDLE) {
215 gpiod_set_value_cansleep(ts->gpio_wake, 1);
218 ret = i2c_smbus_read_byte_data(ts->client, PIXCIR_REG_POWER_MODE);
220 dev_err(dev, "%s: can't read reg 0x%x : %d\n",
221 __func__, PIXCIR_REG_POWER_MODE, ret);
225 ret &= ~PIXCIR_POWER_MODE_MASK;
228 /* Always AUTO_IDLE */
229 ret |= PIXCIR_POWER_ALLOW_IDLE;
231 ret = i2c_smbus_write_byte_data(ts->client, PIXCIR_REG_POWER_MODE, ret);
233 dev_err(dev, "%s: can't write reg 0x%x : %d\n",
234 __func__, PIXCIR_REG_POWER_MODE, ret);
238 if (mode == PIXCIR_POWER_HALT) {
240 gpiod_set_value_cansleep(ts->gpio_wake, 0);
247 * Set the interrupt mode for the device i.e. ATTB line behaviour
249 * @polarity : 1 for active high, 0 for active low.
251 static int pixcir_set_int_mode(struct pixcir_i2c_ts_data *ts,
252 enum pixcir_int_mode mode, bool polarity)
254 struct device *dev = &ts->client->dev;
257 ret = i2c_smbus_read_byte_data(ts->client, PIXCIR_REG_INT_MODE);
259 dev_err(dev, "%s: can't read reg 0x%x : %d\n",
260 __func__, PIXCIR_REG_INT_MODE, ret);
264 ret &= ~PIXCIR_INT_MODE_MASK;
268 ret |= PIXCIR_INT_POL_HIGH;
270 ret &= ~PIXCIR_INT_POL_HIGH;
272 ret = i2c_smbus_write_byte_data(ts->client, PIXCIR_REG_INT_MODE, ret);
274 dev_err(dev, "%s: can't write reg 0x%x : %d\n",
275 __func__, PIXCIR_REG_INT_MODE, ret);
283 * Enable/disable interrupt generation
285 static int pixcir_int_enable(struct pixcir_i2c_ts_data *ts, bool enable)
287 struct device *dev = &ts->client->dev;
290 ret = i2c_smbus_read_byte_data(ts->client, PIXCIR_REG_INT_MODE);
292 dev_err(dev, "%s: can't read reg 0x%x : %d\n",
293 __func__, PIXCIR_REG_INT_MODE, ret);
298 ret |= PIXCIR_INT_ENABLE;
300 ret &= ~PIXCIR_INT_ENABLE;
302 ret = i2c_smbus_write_byte_data(ts->client, PIXCIR_REG_INT_MODE, ret);
304 dev_err(dev, "%s: can't write reg 0x%x : %d\n",
305 __func__, PIXCIR_REG_INT_MODE, ret);
312 static int pixcir_start(struct pixcir_i2c_ts_data *ts)
314 struct device *dev = &ts->client->dev;
317 if (ts->gpio_enable) {
318 gpiod_set_value_cansleep(ts->gpio_enable, 1);
322 /* LEVEL_TOUCH interrupt with active low polarity */
323 error = pixcir_set_int_mode(ts, PIXCIR_INT_LEVEL_TOUCH, 0);
325 dev_err(dev, "Failed to set interrupt mode: %d\n", error);
330 mb(); /* Update status before IRQ can fire */
332 /* enable interrupt generation */
333 error = pixcir_int_enable(ts, true);
335 dev_err(dev, "Failed to enable interrupt generation: %d\n",
343 static int pixcir_stop(struct pixcir_i2c_ts_data *ts)
347 /* Disable interrupt generation */
348 error = pixcir_int_enable(ts, false);
350 dev_err(&ts->client->dev,
351 "Failed to disable interrupt generation: %d\n",
356 /* Exit ISR if running, no more report parsing */
358 mb(); /* update status before we synchronize irq */
360 /* Wait till running ISR is complete */
361 synchronize_irq(ts->client->irq);
364 gpiod_set_value_cansleep(ts->gpio_enable, 0);
369 static int pixcir_input_open(struct input_dev *dev)
371 struct pixcir_i2c_ts_data *ts = input_get_drvdata(dev);
373 return pixcir_start(ts);
376 static void pixcir_input_close(struct input_dev *dev)
378 struct pixcir_i2c_ts_data *ts = input_get_drvdata(dev);
383 static int __maybe_unused pixcir_i2c_ts_suspend(struct device *dev)
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;
390 mutex_lock(&input->mutex);
392 if (device_may_wakeup(&client->dev)) {
394 ret = pixcir_start(ts);
396 dev_err(dev, "Failed to start\n");
400 } else if (input->users) {
401 ret = pixcir_stop(ts);
405 mutex_unlock(&input->mutex);
410 static int __maybe_unused pixcir_i2c_ts_resume(struct device *dev)
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;
417 mutex_lock(&input->mutex);
419 if (device_may_wakeup(&client->dev)) {
422 ret = pixcir_stop(ts);
424 dev_err(dev, "Failed to stop\n");
428 } else if (input->users) {
429 ret = pixcir_start(ts);
433 mutex_unlock(&input->mutex);
438 static SIMPLE_DEV_PM_OPS(pixcir_dev_pm_ops,
439 pixcir_i2c_ts_suspend, pixcir_i2c_ts_resume);
442 static const struct of_device_id pixcir_of_match[];
444 static int pixcir_parse_dt(struct device *dev,
445 struct pixcir_i2c_ts_data *tsdata)
447 const struct of_device_id *match;
449 match = of_match_device(of_match_ptr(pixcir_of_match), dev);
453 tsdata->chip = (const struct pixcir_i2c_chip_data *)match->data;
460 static int pixcir_parse_dt(struct device *dev,
461 struct pixcir_i2c_ts_data *tsdata)
467 static int pixcir_i2c_ts_probe(struct i2c_client *client,
468 const struct i2c_device_id *id)
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;
477 tsdata = devm_kzalloc(dev, sizeof(*tsdata), GFP_KERNEL);
482 tsdata->chip = &pdata->chip;
483 } else if (dev->of_node) {
484 error = pixcir_parse_dt(dev, tsdata);
488 dev_err(&client->dev, "platform data not defined\n");
492 if (!tsdata->chip->max_fingers) {
493 dev_err(dev, "Invalid max_fingers in chip data\n");
497 input = devm_input_allocate_device(dev);
499 dev_err(dev, "Failed to allocate input device\n");
503 tsdata->client = client;
504 tsdata->input = input;
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;
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);
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");
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);
533 error = input_mt_init_slots(input, tsdata->max_fingers,
534 INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
536 dev_err(dev, "Error initializing Multi-Touch slots\n");
540 input_set_drvdata(input, tsdata);
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);
549 tsdata->gpio_reset = devm_gpiod_get_optional(dev, "reset",
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);
557 tsdata->gpio_wake = devm_gpiod_get_optional(dev, "wake",
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);
566 tsdata->gpio_enable = devm_gpiod_get_optional(dev, "enable",
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);
575 if (tsdata->gpio_enable)
578 error = devm_request_threaded_irq(dev, client->irq, NULL, pixcir_ts_isr,
579 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
580 client->name, tsdata);
582 dev_err(dev, "failed to request irq %d\n", client->irq);
586 pixcir_reset(tsdata);
588 /* Always be in IDLE mode to save power, device supports auto wake */
589 error = pixcir_set_power_mode(tsdata, PIXCIR_POWER_IDLE);
591 dev_err(dev, "Failed to set IDLE mode\n");
595 /* Stop device till opened */
596 error = pixcir_stop(tsdata);
600 error = input_register_device(input);
604 i2c_set_clientdata(client, tsdata);
609 static const struct i2c_device_id pixcir_i2c_ts_id[] = {
611 { "pixcir_tangoc", 0 },
614 MODULE_DEVICE_TABLE(i2c, pixcir_i2c_ts_id);
617 static const struct pixcir_i2c_chip_data pixcir_ts_data = {
619 /* no hw id support */
622 static const struct pixcir_i2c_chip_data pixcir_tangoc_data = {
627 static 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 },
632 MODULE_DEVICE_TABLE(of, pixcir_of_match);
635 static struct i2c_driver pixcir_i2c_ts_driver = {
638 .pm = &pixcir_dev_pm_ops,
639 .of_match_table = of_match_ptr(pixcir_of_match),
641 .probe = pixcir_i2c_ts_probe,
642 .id_table = pixcir_i2c_ts_id,
645 module_i2c_driver(pixcir_i2c_ts_driver);
647 MODULE_AUTHOR("Jianchun Bian <jcbian@pixcir.com.cn>, Dequan Meng <dqmeng@pixcir.com.cn>");
648 MODULE_DESCRIPTION("Pixcir I2C Touchscreen Driver");
649 MODULE_LICENSE("GPL");