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/gpio.h>
28 #include <linux/gpio/consumer.h>
30 #include <linux/of_device.h>
31 #include <linux/platform_data/pixcir_i2c_ts.h>
33 #define PIXCIR_MAX_SLOTS 5 /* Max fingers supported by driver */
35 struct pixcir_i2c_ts_data {
36 struct i2c_client *client;
37 struct input_dev *input;
38 struct gpio_desc *gpio_attb;
39 struct gpio_desc *gpio_reset;
40 const struct pixcir_ts_platform_data *pdata;
42 int max_fingers; /* Max fingers supported in this instance */
51 struct pixcir_report_data {
53 struct pixcir_touch touches[PIXCIR_MAX_SLOTS];
56 static void pixcir_ts_parse(struct pixcir_i2c_ts_data *tsdata,
57 struct pixcir_report_data *report)
59 u8 rdbuf[2 + PIXCIR_MAX_SLOTS * 5];
65 const struct pixcir_i2c_chip_data *chip = &tsdata->pdata->chip;
67 memset(report, 0, sizeof(struct pixcir_report_data));
69 i = chip->has_hw_ids ? 1 : 0;
70 readsize = 2 + tsdata->max_fingers * (4 + i);
71 if (readsize > sizeof(rdbuf))
72 readsize = sizeof(rdbuf);
74 ret = i2c_master_send(tsdata->client, wrbuf, sizeof(wrbuf));
75 if (ret != sizeof(wrbuf)) {
76 dev_err(&tsdata->client->dev,
77 "%s: i2c_master_send failed(), ret=%d\n",
82 ret = i2c_master_recv(tsdata->client, rdbuf, readsize);
83 if (ret != sizeof(rdbuf)) {
84 dev_err(&tsdata->client->dev,
85 "%s: i2c_master_recv failed(), ret=%d\n",
90 touch = rdbuf[0] & 0x7;
91 if (touch > tsdata->max_fingers)
92 touch = tsdata->max_fingers;
94 report->num_touches = touch;
97 for (i = 0; i < touch; i++) {
98 report->touches[i].x = (bufptr[1] << 8) | bufptr[0];
99 report->touches[i].y = (bufptr[3] << 8) | bufptr[2];
101 if (chip->has_hw_ids) {
102 report->touches[i].id = bufptr[4];
110 static void pixcir_ts_report(struct pixcir_i2c_ts_data *ts,
111 struct pixcir_report_data *report)
113 struct input_mt_pos pos[PIXCIR_MAX_SLOTS];
114 int slots[PIXCIR_MAX_SLOTS];
115 struct pixcir_touch *touch;
117 struct device *dev = &ts->client->dev;
118 const struct pixcir_i2c_chip_data *chip = &ts->pdata->chip;
120 n = report->num_touches;
121 if (n > PIXCIR_MAX_SLOTS)
122 n = PIXCIR_MAX_SLOTS;
124 if (!chip->has_hw_ids) {
125 for (i = 0; i < n; i++) {
126 touch = &report->touches[i];
131 input_mt_assign_slots(ts->input, slots, pos, n, 0);
134 for (i = 0; i < n; i++) {
135 touch = &report->touches[i];
137 if (chip->has_hw_ids) {
138 slot = input_mt_get_slot_by_key(ts->input, touch->id);
140 dev_dbg(dev, "no free slot for id 0x%x\n",
148 input_mt_slot(ts->input, slot);
149 input_mt_report_slot_state(ts->input,
150 MT_TOOL_FINGER, true);
152 input_event(ts->input, EV_ABS, ABS_MT_POSITION_X, touch->x);
153 input_event(ts->input, EV_ABS, ABS_MT_POSITION_Y, touch->y);
155 dev_dbg(dev, "%d: slot %d, x %d, y %d\n",
156 i, slot, touch->x, touch->y);
159 input_mt_sync_frame(ts->input);
160 input_sync(ts->input);
163 static irqreturn_t pixcir_ts_isr(int irq, void *dev_id)
165 struct pixcir_i2c_ts_data *tsdata = dev_id;
166 struct pixcir_report_data report;
168 while (tsdata->running) {
170 pixcir_ts_parse(tsdata, &report);
173 pixcir_ts_report(tsdata, &report);
175 if (gpiod_get_value_cansleep(tsdata->gpio_attb)) {
176 if (report.num_touches) {
178 * Last report with no finger up?
181 input_mt_sync_frame(tsdata->input);
182 input_sync(tsdata->input);
193 static void pixcir_reset(struct pixcir_i2c_ts_data *tsdata)
195 if (!IS_ERR_OR_NULL(tsdata->gpio_reset)) {
196 gpiod_set_value_cansleep(tsdata->gpio_reset, 1);
197 ndelay(100); /* datasheet section 1.2.3 says 80ns min. */
198 gpiod_set_value_cansleep(tsdata->gpio_reset, 0);
199 /* wait for controller ready. 100ms guess. */
204 static int pixcir_set_power_mode(struct pixcir_i2c_ts_data *ts,
205 enum pixcir_power_mode mode)
207 struct device *dev = &ts->client->dev;
210 ret = i2c_smbus_read_byte_data(ts->client, PIXCIR_REG_POWER_MODE);
212 dev_err(dev, "%s: can't read reg 0x%x : %d\n",
213 __func__, PIXCIR_REG_POWER_MODE, ret);
217 ret &= ~PIXCIR_POWER_MODE_MASK;
220 /* Always AUTO_IDLE */
221 ret |= PIXCIR_POWER_ALLOW_IDLE;
223 ret = i2c_smbus_write_byte_data(ts->client, PIXCIR_REG_POWER_MODE, ret);
225 dev_err(dev, "%s: can't write reg 0x%x : %d\n",
226 __func__, PIXCIR_REG_POWER_MODE, ret);
234 * Set the interrupt mode for the device i.e. ATTB line behaviour
236 * @polarity : 1 for active high, 0 for active low.
238 static int pixcir_set_int_mode(struct pixcir_i2c_ts_data *ts,
239 enum pixcir_int_mode mode, bool polarity)
241 struct device *dev = &ts->client->dev;
244 ret = i2c_smbus_read_byte_data(ts->client, PIXCIR_REG_INT_MODE);
246 dev_err(dev, "%s: can't read reg 0x%x : %d\n",
247 __func__, PIXCIR_REG_INT_MODE, ret);
251 ret &= ~PIXCIR_INT_MODE_MASK;
255 ret |= PIXCIR_INT_POL_HIGH;
257 ret &= ~PIXCIR_INT_POL_HIGH;
259 ret = i2c_smbus_write_byte_data(ts->client, PIXCIR_REG_INT_MODE, ret);
261 dev_err(dev, "%s: can't write reg 0x%x : %d\n",
262 __func__, PIXCIR_REG_INT_MODE, ret);
270 * Enable/disable interrupt generation
272 static int pixcir_int_enable(struct pixcir_i2c_ts_data *ts, bool enable)
274 struct device *dev = &ts->client->dev;
277 ret = i2c_smbus_read_byte_data(ts->client, PIXCIR_REG_INT_MODE);
279 dev_err(dev, "%s: can't read reg 0x%x : %d\n",
280 __func__, PIXCIR_REG_INT_MODE, ret);
285 ret |= PIXCIR_INT_ENABLE;
287 ret &= ~PIXCIR_INT_ENABLE;
289 ret = i2c_smbus_write_byte_data(ts->client, PIXCIR_REG_INT_MODE, ret);
291 dev_err(dev, "%s: can't write reg 0x%x : %d\n",
292 __func__, PIXCIR_REG_INT_MODE, ret);
299 static int pixcir_start(struct pixcir_i2c_ts_data *ts)
301 struct device *dev = &ts->client->dev;
304 /* LEVEL_TOUCH interrupt with active low polarity */
305 error = pixcir_set_int_mode(ts, PIXCIR_INT_LEVEL_TOUCH, 0);
307 dev_err(dev, "Failed to set interrupt mode: %d\n", error);
312 mb(); /* Update status before IRQ can fire */
314 /* enable interrupt generation */
315 error = pixcir_int_enable(ts, true);
317 dev_err(dev, "Failed to enable interrupt generation: %d\n",
325 static int pixcir_stop(struct pixcir_i2c_ts_data *ts)
329 /* Disable interrupt generation */
330 error = pixcir_int_enable(ts, false);
332 dev_err(&ts->client->dev,
333 "Failed to disable interrupt generation: %d\n",
338 /* Exit ISR if running, no more report parsing */
340 mb(); /* update status before we synchronize irq */
342 /* Wait till running ISR is complete */
343 synchronize_irq(ts->client->irq);
348 static int pixcir_input_open(struct input_dev *dev)
350 struct pixcir_i2c_ts_data *ts = input_get_drvdata(dev);
352 return pixcir_start(ts);
355 static void pixcir_input_close(struct input_dev *dev)
357 struct pixcir_i2c_ts_data *ts = input_get_drvdata(dev);
362 static int __maybe_unused pixcir_i2c_ts_suspend(struct device *dev)
364 struct i2c_client *client = to_i2c_client(dev);
365 struct pixcir_i2c_ts_data *ts = i2c_get_clientdata(client);
366 struct input_dev *input = ts->input;
369 mutex_lock(&input->mutex);
371 if (device_may_wakeup(&client->dev)) {
373 ret = pixcir_start(ts);
375 dev_err(dev, "Failed to start\n");
380 enable_irq_wake(client->irq);
381 } else if (input->users) {
382 ret = pixcir_stop(ts);
386 mutex_unlock(&input->mutex);
391 static int __maybe_unused pixcir_i2c_ts_resume(struct device *dev)
393 struct i2c_client *client = to_i2c_client(dev);
394 struct pixcir_i2c_ts_data *ts = i2c_get_clientdata(client);
395 struct input_dev *input = ts->input;
398 mutex_lock(&input->mutex);
400 if (device_may_wakeup(&client->dev)) {
401 disable_irq_wake(client->irq);
404 ret = pixcir_stop(ts);
406 dev_err(dev, "Failed to stop\n");
410 } else if (input->users) {
411 ret = pixcir_start(ts);
415 mutex_unlock(&input->mutex);
420 static SIMPLE_DEV_PM_OPS(pixcir_dev_pm_ops,
421 pixcir_i2c_ts_suspend, pixcir_i2c_ts_resume);
424 static const struct of_device_id pixcir_of_match[];
426 static struct pixcir_ts_platform_data *pixcir_parse_dt(struct device *dev)
428 struct pixcir_ts_platform_data *pdata;
429 struct device_node *np = dev->of_node;
430 const struct of_device_id *match;
432 match = of_match_device(of_match_ptr(pixcir_of_match), dev);
434 return ERR_PTR(-EINVAL);
436 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
438 return ERR_PTR(-ENOMEM);
440 pdata->chip = *(const struct pixcir_i2c_chip_data *)match->data;
442 if (of_property_read_u32(np, "touchscreen-size-x", &pdata->x_max)) {
443 dev_err(dev, "Failed to get touchscreen-size-x property\n");
444 return ERR_PTR(-EINVAL);
448 if (of_property_read_u32(np, "touchscreen-size-y", &pdata->y_max)) {
449 dev_err(dev, "Failed to get touchscreen-size-y property\n");
450 return ERR_PTR(-EINVAL);
454 dev_dbg(dev, "%s: x %d, y %d\n", __func__,
455 pdata->x_max + 1, pdata->y_max + 1);
460 static struct pixcir_ts_platform_data *pixcir_parse_dt(struct device *dev)
462 return ERR_PTR(-EINVAL);
466 static int pixcir_i2c_ts_probe(struct i2c_client *client,
467 const struct i2c_device_id *id)
469 const struct pixcir_ts_platform_data *pdata =
470 dev_get_platdata(&client->dev);
471 struct device *dev = &client->dev;
472 struct device_node *np = dev->of_node;
473 struct pixcir_i2c_ts_data *tsdata;
474 struct input_dev *input;
478 pdata = pixcir_parse_dt(dev);
480 return PTR_ERR(pdata);
484 dev_err(&client->dev, "platform data not defined\n");
488 if (!pdata->chip.max_fingers) {
489 dev_err(dev, "Invalid max_fingers in pdata\n");
493 tsdata = devm_kzalloc(dev, sizeof(*tsdata), GFP_KERNEL);
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;
505 tsdata->pdata = pdata;
507 input->name = client->name;
508 input->id.bustype = BUS_I2C;
509 input->open = pixcir_input_open;
510 input->close = pixcir_input_close;
511 input->dev.parent = &client->dev;
513 __set_bit(EV_KEY, input->evbit);
514 __set_bit(EV_ABS, input->evbit);
515 __set_bit(BTN_TOUCH, input->keybit);
516 input_set_abs_params(input, ABS_X, 0, pdata->x_max, 0, 0);
517 input_set_abs_params(input, ABS_Y, 0, pdata->y_max, 0, 0);
518 input_set_abs_params(input, ABS_MT_POSITION_X, 0, pdata->x_max, 0, 0);
519 input_set_abs_params(input, ABS_MT_POSITION_Y, 0, pdata->y_max, 0, 0);
521 tsdata->max_fingers = tsdata->pdata->chip.max_fingers;
522 if (tsdata->max_fingers > PIXCIR_MAX_SLOTS) {
523 tsdata->max_fingers = PIXCIR_MAX_SLOTS;
524 dev_info(dev, "Limiting maximum fingers to %d\n",
525 tsdata->max_fingers);
528 error = input_mt_init_slots(input, tsdata->max_fingers,
529 INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
531 dev_err(dev, "Error initializing Multi-Touch slots\n");
535 input_set_drvdata(input, tsdata);
537 tsdata->gpio_attb = devm_gpiod_get(dev, "attb", GPIOD_IN);
538 if (IS_ERR(tsdata->gpio_attb)) {
539 error = PTR_ERR(tsdata->gpio_attb);
540 dev_err(dev, "Failed to request ATTB gpio: %d\n", error);
544 tsdata->gpio_reset = devm_gpiod_get_optional(dev, "reset",
546 if (IS_ERR(tsdata->gpio_reset)) {
547 error = PTR_ERR(tsdata->gpio_reset);
548 dev_err(dev, "Failed to request RESET gpio: %d\n", error);
552 error = devm_request_threaded_irq(dev, client->irq, NULL, pixcir_ts_isr,
553 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
554 client->name, tsdata);
556 dev_err(dev, "failed to request irq %d\n", client->irq);
560 pixcir_reset(tsdata);
562 /* Always be in IDLE mode to save power, device supports auto wake */
563 error = pixcir_set_power_mode(tsdata, PIXCIR_POWER_IDLE);
565 dev_err(dev, "Failed to set IDLE mode\n");
569 /* Stop device till opened */
570 error = pixcir_stop(tsdata);
574 error = input_register_device(input);
578 i2c_set_clientdata(client, tsdata);
579 device_init_wakeup(&client->dev, 1);
584 static int pixcir_i2c_ts_remove(struct i2c_client *client)
586 device_init_wakeup(&client->dev, 0);
591 static const struct i2c_device_id pixcir_i2c_ts_id[] = {
593 { "pixcir_tangoc", 0 },
596 MODULE_DEVICE_TABLE(i2c, pixcir_i2c_ts_id);
599 static const struct pixcir_i2c_chip_data pixcir_ts_data = {
601 /* no hw id support */
604 static const struct pixcir_i2c_chip_data pixcir_tangoc_data = {
609 static const struct of_device_id pixcir_of_match[] = {
610 { .compatible = "pixcir,pixcir_ts", .data = &pixcir_ts_data },
611 { .compatible = "pixcir,pixcir_tangoc", .data = &pixcir_tangoc_data },
614 MODULE_DEVICE_TABLE(of, pixcir_of_match);
617 static struct i2c_driver pixcir_i2c_ts_driver = {
619 .owner = THIS_MODULE,
621 .pm = &pixcir_dev_pm_ops,
622 .of_match_table = of_match_ptr(pixcir_of_match),
624 .probe = pixcir_i2c_ts_probe,
625 .remove = pixcir_i2c_ts_remove,
626 .id_table = pixcir_i2c_ts_id,
629 module_i2c_driver(pixcir_i2c_ts_driver);
631 MODULE_AUTHOR("Jianchun Bian <jcbian@pixcir.com.cn>, Dequan Meng <dqmeng@pixcir.com.cn>");
632 MODULE_DESCRIPTION("Pixcir I2C Touchscreen Driver");
633 MODULE_LICENSE("GPL");