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/pixcir_ts.h>
28 #include <linux/gpio.h>
30 #include <linux/of_gpio.h>
31 #include <linux/of_device.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 const struct pixcir_ts_platform_data
*pdata
;
40 int max_fingers
; /* Max fingers supported in this instance */
49 struct pixcir_report_data
{
51 struct pixcir_touch touches
[PIXCIR_MAX_SLOTS
];
54 static void pixcir_ts_parse(struct pixcir_i2c_ts_data
*tsdata
,
55 struct pixcir_report_data
*report
)
57 u8 rdbuf
[2 + PIXCIR_MAX_SLOTS
* 5];
63 const struct pixcir_i2c_chip_data
*chip
= &tsdata
->pdata
->chip
;
65 memset(report
, 0, sizeof(struct pixcir_report_data
));
67 i
= chip
->has_hw_ids
? 1 : 0;
68 readsize
= 2 + tsdata
->max_fingers
* (4 + i
);
69 if (readsize
> sizeof(rdbuf
))
70 readsize
= sizeof(rdbuf
);
72 ret
= i2c_master_send(tsdata
->client
, wrbuf
, sizeof(wrbuf
));
73 if (ret
!= sizeof(wrbuf
)) {
74 dev_err(&tsdata
->client
->dev
,
75 "%s: i2c_master_send failed(), ret=%d\n",
80 ret
= i2c_master_recv(tsdata
->client
, rdbuf
, readsize
);
81 if (ret
!= readsize
) {
82 dev_err(&tsdata
->client
->dev
,
83 "%s: i2c_master_recv failed(), ret=%d\n",
88 touch
= rdbuf
[0] & 0x7;
89 if (touch
> tsdata
->max_fingers
)
90 touch
= tsdata
->max_fingers
;
92 report
->num_touches
= touch
;
95 for (i
= 0; i
< touch
; i
++) {
96 report
->touches
[i
].x
= (bufptr
[1] << 8) | bufptr
[0];
97 report
->touches
[i
].y
= (bufptr
[3] << 8) | bufptr
[2];
99 if (chip
->has_hw_ids
) {
100 report
->touches
[i
].id
= bufptr
[4];
108 static void pixcir_ts_report(struct pixcir_i2c_ts_data
*ts
,
109 struct pixcir_report_data
*report
)
111 struct input_mt_pos pos
[PIXCIR_MAX_SLOTS
];
112 int slots
[PIXCIR_MAX_SLOTS
];
113 struct pixcir_touch
*touch
;
115 struct device
*dev
= &ts
->client
->dev
;
116 const struct pixcir_i2c_chip_data
*chip
= &ts
->pdata
->chip
;
118 n
= report
->num_touches
;
119 if (n
> PIXCIR_MAX_SLOTS
)
120 n
= PIXCIR_MAX_SLOTS
;
122 if (!chip
->has_hw_ids
) {
123 for (i
= 0; i
< n
; i
++) {
124 touch
= &report
->touches
[i
];
129 input_mt_assign_slots(ts
->input
, slots
, pos
, n
, 0);
132 for (i
= 0; i
< n
; i
++) {
133 touch
= &report
->touches
[i
];
135 if (chip
->has_hw_ids
) {
136 slot
= input_mt_get_slot_by_key(ts
->input
, touch
->id
);
138 dev_dbg(dev
, "no free slot for id 0x%x\n",
146 input_mt_slot(ts
->input
, slot
);
147 input_mt_report_slot_state(ts
->input
,
148 MT_TOOL_FINGER
, true);
150 input_event(ts
->input
, EV_ABS
, ABS_MT_POSITION_X
, touch
->x
);
151 input_event(ts
->input
, EV_ABS
, ABS_MT_POSITION_Y
, touch
->y
);
153 dev_dbg(dev
, "%d: slot %d, x %d, y %d\n",
154 i
, slot
, touch
->x
, touch
->y
);
157 input_mt_sync_frame(ts
->input
);
158 input_sync(ts
->input
);
161 static irqreturn_t
pixcir_ts_isr(int irq
, void *dev_id
)
163 struct pixcir_i2c_ts_data
*tsdata
= dev_id
;
164 const struct pixcir_ts_platform_data
*pdata
= tsdata
->pdata
;
165 struct pixcir_report_data report
;
167 while (tsdata
->running
) {
169 pixcir_ts_parse(tsdata
, &report
);
172 pixcir_ts_report(tsdata
, &report
);
174 if (gpio_get_value(pdata
->gpio_attb
)) {
175 if (report
.num_touches
) {
177 * Last report with no finger up?
180 input_mt_sync_frame(tsdata
->input
);
181 input_sync(tsdata
->input
);
192 static int pixcir_set_power_mode(struct pixcir_i2c_ts_data
*ts
,
193 enum pixcir_power_mode mode
)
195 struct device
*dev
= &ts
->client
->dev
;
198 ret
= i2c_smbus_read_byte_data(ts
->client
, PIXCIR_REG_POWER_MODE
);
200 dev_err(dev
, "%s: can't read reg 0x%x : %d\n",
201 __func__
, PIXCIR_REG_POWER_MODE
, ret
);
205 ret
&= ~PIXCIR_POWER_MODE_MASK
;
208 /* Always AUTO_IDLE */
209 ret
|= PIXCIR_POWER_ALLOW_IDLE
;
211 ret
= i2c_smbus_write_byte_data(ts
->client
, PIXCIR_REG_POWER_MODE
, ret
);
213 dev_err(dev
, "%s: can't write reg 0x%x : %d\n",
214 __func__
, PIXCIR_REG_POWER_MODE
, ret
);
222 * Set the interrupt mode for the device i.e. ATTB line behaviour
224 * @polarity : 1 for active high, 0 for active low.
226 static int pixcir_set_int_mode(struct pixcir_i2c_ts_data
*ts
,
227 enum pixcir_int_mode mode
, bool polarity
)
229 struct device
*dev
= &ts
->client
->dev
;
232 ret
= i2c_smbus_read_byte_data(ts
->client
, PIXCIR_REG_INT_MODE
);
234 dev_err(dev
, "%s: can't read reg 0x%x : %d\n",
235 __func__
, PIXCIR_REG_INT_MODE
, ret
);
239 ret
&= ~PIXCIR_INT_MODE_MASK
;
243 ret
|= PIXCIR_INT_POL_HIGH
;
245 ret
&= ~PIXCIR_INT_POL_HIGH
;
247 ret
= i2c_smbus_write_byte_data(ts
->client
, PIXCIR_REG_INT_MODE
, ret
);
249 dev_err(dev
, "%s: can't write reg 0x%x : %d\n",
250 __func__
, PIXCIR_REG_INT_MODE
, ret
);
258 * Enable/disable interrupt generation
260 static int pixcir_int_enable(struct pixcir_i2c_ts_data
*ts
, bool enable
)
262 struct device
*dev
= &ts
->client
->dev
;
265 ret
= i2c_smbus_read_byte_data(ts
->client
, PIXCIR_REG_INT_MODE
);
267 dev_err(dev
, "%s: can't read reg 0x%x : %d\n",
268 __func__
, PIXCIR_REG_INT_MODE
, ret
);
273 ret
|= PIXCIR_INT_ENABLE
;
275 ret
&= ~PIXCIR_INT_ENABLE
;
277 ret
= i2c_smbus_write_byte_data(ts
->client
, PIXCIR_REG_INT_MODE
, ret
);
279 dev_err(dev
, "%s: can't write reg 0x%x : %d\n",
280 __func__
, PIXCIR_REG_INT_MODE
, ret
);
287 static int pixcir_start(struct pixcir_i2c_ts_data
*ts
)
289 struct device
*dev
= &ts
->client
->dev
;
292 /* LEVEL_TOUCH interrupt with active low polarity */
293 error
= pixcir_set_int_mode(ts
, PIXCIR_INT_LEVEL_TOUCH
, 0);
295 dev_err(dev
, "Failed to set interrupt mode: %d\n", error
);
300 mb(); /* Update status before IRQ can fire */
302 /* enable interrupt generation */
303 error
= pixcir_int_enable(ts
, true);
305 dev_err(dev
, "Failed to enable interrupt generation: %d\n",
313 static int pixcir_stop(struct pixcir_i2c_ts_data
*ts
)
317 /* Disable interrupt generation */
318 error
= pixcir_int_enable(ts
, false);
320 dev_err(&ts
->client
->dev
,
321 "Failed to disable interrupt generation: %d\n",
326 /* Exit ISR if running, no more report parsing */
328 mb(); /* update status before we synchronize irq */
330 /* Wait till running ISR is complete */
331 synchronize_irq(ts
->client
->irq
);
336 static int pixcir_input_open(struct input_dev
*dev
)
338 struct pixcir_i2c_ts_data
*ts
= input_get_drvdata(dev
);
340 return pixcir_start(ts
);
343 static void pixcir_input_close(struct input_dev
*dev
)
345 struct pixcir_i2c_ts_data
*ts
= input_get_drvdata(dev
);
350 static int __maybe_unused
pixcir_i2c_ts_suspend(struct device
*dev
)
352 struct i2c_client
*client
= to_i2c_client(dev
);
353 struct pixcir_i2c_ts_data
*ts
= i2c_get_clientdata(client
);
354 struct input_dev
*input
= ts
->input
;
357 mutex_lock(&input
->mutex
);
359 if (device_may_wakeup(&client
->dev
)) {
361 ret
= pixcir_start(ts
);
363 dev_err(dev
, "Failed to start\n");
368 enable_irq_wake(client
->irq
);
369 } else if (input
->users
) {
370 ret
= pixcir_stop(ts
);
374 mutex_unlock(&input
->mutex
);
379 static int __maybe_unused
pixcir_i2c_ts_resume(struct device
*dev
)
381 struct i2c_client
*client
= to_i2c_client(dev
);
382 struct pixcir_i2c_ts_data
*ts
= i2c_get_clientdata(client
);
383 struct input_dev
*input
= ts
->input
;
386 mutex_lock(&input
->mutex
);
388 if (device_may_wakeup(&client
->dev
)) {
389 disable_irq_wake(client
->irq
);
392 ret
= pixcir_stop(ts
);
394 dev_err(dev
, "Failed to stop\n");
398 } else if (input
->users
) {
399 ret
= pixcir_start(ts
);
403 mutex_unlock(&input
->mutex
);
408 static SIMPLE_DEV_PM_OPS(pixcir_dev_pm_ops
,
409 pixcir_i2c_ts_suspend
, pixcir_i2c_ts_resume
);
412 static const struct of_device_id pixcir_of_match
[];
414 static struct pixcir_ts_platform_data
*pixcir_parse_dt(struct device
*dev
)
416 struct pixcir_ts_platform_data
*pdata
;
417 struct device_node
*np
= dev
->of_node
;
418 const struct of_device_id
*match
;
420 match
= of_match_device(of_match_ptr(pixcir_of_match
), dev
);
422 return ERR_PTR(-EINVAL
);
424 pdata
= devm_kzalloc(dev
, sizeof(*pdata
), GFP_KERNEL
);
426 return ERR_PTR(-ENOMEM
);
428 pdata
->chip
= *(const struct pixcir_i2c_chip_data
*)match
->data
;
430 pdata
->gpio_attb
= of_get_named_gpio(np
, "attb-gpio", 0);
431 /* gpio_attb validity is checked in probe */
433 if (of_property_read_u32(np
, "touchscreen-size-x", &pdata
->x_max
)) {
434 dev_err(dev
, "Failed to get touchscreen-size-x property\n");
435 return ERR_PTR(-EINVAL
);
439 if (of_property_read_u32(np
, "touchscreen-size-y", &pdata
->y_max
)) {
440 dev_err(dev
, "Failed to get touchscreen-size-y property\n");
441 return ERR_PTR(-EINVAL
);
445 dev_dbg(dev
, "%s: x %d, y %d, gpio %d\n", __func__
,
446 pdata
->x_max
+ 1, pdata
->y_max
+ 1, pdata
->gpio_attb
);
451 static struct pixcir_ts_platform_data
*pixcir_parse_dt(struct device
*dev
)
453 return ERR_PTR(-EINVAL
);
457 static int pixcir_i2c_ts_probe(struct i2c_client
*client
,
458 const struct i2c_device_id
*id
)
460 const struct pixcir_ts_platform_data
*pdata
=
461 dev_get_platdata(&client
->dev
);
462 struct device
*dev
= &client
->dev
;
463 struct device_node
*np
= dev
->of_node
;
464 struct pixcir_i2c_ts_data
*tsdata
;
465 struct input_dev
*input
;
469 pdata
= pixcir_parse_dt(dev
);
471 return PTR_ERR(pdata
);
475 dev_err(&client
->dev
, "platform data not defined\n");
479 if (!gpio_is_valid(pdata
->gpio_attb
)) {
480 dev_err(dev
, "Invalid gpio_attb in pdata\n");
484 if (!pdata
->chip
.max_fingers
) {
485 dev_err(dev
, "Invalid max_fingers in pdata\n");
489 tsdata
= devm_kzalloc(dev
, sizeof(*tsdata
), GFP_KERNEL
);
493 input
= devm_input_allocate_device(dev
);
495 dev_err(dev
, "Failed to allocate input device\n");
499 tsdata
->client
= client
;
500 tsdata
->input
= input
;
501 tsdata
->pdata
= pdata
;
503 input
->name
= client
->name
;
504 input
->id
.bustype
= BUS_I2C
;
505 input
->open
= pixcir_input_open
;
506 input
->close
= pixcir_input_close
;
507 input
->dev
.parent
= &client
->dev
;
509 __set_bit(EV_KEY
, input
->evbit
);
510 __set_bit(EV_ABS
, input
->evbit
);
511 __set_bit(BTN_TOUCH
, input
->keybit
);
512 input_set_abs_params(input
, ABS_X
, 0, pdata
->x_max
, 0, 0);
513 input_set_abs_params(input
, ABS_Y
, 0, pdata
->y_max
, 0, 0);
514 input_set_abs_params(input
, ABS_MT_POSITION_X
, 0, pdata
->x_max
, 0, 0);
515 input_set_abs_params(input
, ABS_MT_POSITION_Y
, 0, pdata
->y_max
, 0, 0);
517 tsdata
->max_fingers
= tsdata
->pdata
->chip
.max_fingers
;
518 if (tsdata
->max_fingers
> PIXCIR_MAX_SLOTS
) {
519 tsdata
->max_fingers
= PIXCIR_MAX_SLOTS
;
520 dev_info(dev
, "Limiting maximum fingers to %d\n",
521 tsdata
->max_fingers
);
524 error
= input_mt_init_slots(input
, tsdata
->max_fingers
,
525 INPUT_MT_DIRECT
| INPUT_MT_DROP_UNUSED
);
527 dev_err(dev
, "Error initializing Multi-Touch slots\n");
531 input_set_drvdata(input
, tsdata
);
533 error
= devm_gpio_request_one(dev
, pdata
->gpio_attb
,
534 GPIOF_DIR_IN
, "pixcir_i2c_attb");
536 dev_err(dev
, "Failed to request ATTB gpio\n");
540 error
= devm_request_threaded_irq(dev
, client
->irq
, NULL
, pixcir_ts_isr
,
541 IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
542 client
->name
, tsdata
);
544 dev_err(dev
, "failed to request irq %d\n", client
->irq
);
548 /* Always be in IDLE mode to save power, device supports auto wake */
549 error
= pixcir_set_power_mode(tsdata
, PIXCIR_POWER_IDLE
);
551 dev_err(dev
, "Failed to set IDLE mode\n");
555 /* Stop device till opened */
556 error
= pixcir_stop(tsdata
);
560 error
= input_register_device(input
);
564 i2c_set_clientdata(client
, tsdata
);
565 device_init_wakeup(&client
->dev
, 1);
570 static int pixcir_i2c_ts_remove(struct i2c_client
*client
)
572 device_init_wakeup(&client
->dev
, 0);
577 static const struct i2c_device_id pixcir_i2c_ts_id
[] = {
579 { "pixcir_tangoc", 0 },
582 MODULE_DEVICE_TABLE(i2c
, pixcir_i2c_ts_id
);
585 static const struct pixcir_i2c_chip_data pixcir_ts_data
= {
587 /* no hw id support */
590 static const struct pixcir_i2c_chip_data pixcir_tangoc_data
= {
595 static const struct of_device_id pixcir_of_match
[] = {
596 { .compatible
= "pixcir,pixcir_ts", .data
= &pixcir_ts_data
},
597 { .compatible
= "pixcir,pixcir_tangoc", .data
= &pixcir_tangoc_data
},
600 MODULE_DEVICE_TABLE(of
, pixcir_of_match
);
603 static struct i2c_driver pixcir_i2c_ts_driver
= {
605 .owner
= THIS_MODULE
,
607 .pm
= &pixcir_dev_pm_ops
,
608 .of_match_table
= of_match_ptr(pixcir_of_match
),
610 .probe
= pixcir_i2c_ts_probe
,
611 .remove
= pixcir_i2c_ts_remove
,
612 .id_table
= pixcir_i2c_ts_id
,
615 module_i2c_driver(pixcir_i2c_ts_driver
);
617 MODULE_AUTHOR("Jianchun Bian <jcbian@pixcir.com.cn>, Dequan Meng <dqmeng@pixcir.com.cn>");
618 MODULE_DESCRIPTION("Pixcir I2C Touchscreen Driver");
619 MODULE_LICENSE("GPL");