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 const struct pixcir_i2c_chip_data
*chip
;
42 int max_fingers
; /* Max fingers supported in this instance */
52 struct pixcir_report_data
{
54 struct pixcir_touch touches
[PIXCIR_MAX_SLOTS
];
57 static void pixcir_ts_parse(struct pixcir_i2c_ts_data
*tsdata
,
58 struct pixcir_report_data
*report
)
60 u8 rdbuf
[2 + PIXCIR_MAX_SLOTS
* 5];
66 const struct pixcir_i2c_chip_data
*chip
= tsdata
->chip
;
68 memset(report
, 0, sizeof(struct pixcir_report_data
));
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
);
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",
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",
91 touch
= rdbuf
[0] & 0x7;
92 if (touch
> tsdata
->max_fingers
)
93 touch
= tsdata
->max_fingers
;
95 report
->num_touches
= touch
;
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];
102 if (chip
->has_hw_ids
) {
103 report
->touches
[i
].id
= bufptr
[4];
111 static void pixcir_ts_report(struct pixcir_i2c_ts_data
*ts
,
112 struct pixcir_report_data
*report
)
114 struct input_mt_pos pos
[PIXCIR_MAX_SLOTS
];
115 int slots
[PIXCIR_MAX_SLOTS
];
116 struct pixcir_touch
*touch
;
118 struct device
*dev
= &ts
->client
->dev
;
119 const struct pixcir_i2c_chip_data
*chip
= ts
->chip
;
121 n
= report
->num_touches
;
122 if (n
> PIXCIR_MAX_SLOTS
)
123 n
= PIXCIR_MAX_SLOTS
;
125 if (!ts
->chip
->has_hw_ids
) {
126 for (i
= 0; i
< n
; i
++) {
127 touch
= &report
->touches
[i
];
132 input_mt_assign_slots(ts
->input
, slots
, pos
, n
, 0);
135 for (i
= 0; i
< n
; i
++) {
136 touch
= &report
->touches
[i
];
138 if (chip
->has_hw_ids
) {
139 slot
= input_mt_get_slot_by_key(ts
->input
, touch
->id
);
141 dev_dbg(dev
, "no free slot for id 0x%x\n",
149 input_mt_slot(ts
->input
, slot
);
150 input_mt_report_slot_state(ts
->input
,
151 MT_TOOL_FINGER
, true);
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
);
156 dev_dbg(dev
, "%d: slot %d, x %d, y %d\n",
157 i
, slot
, touch
->x
, touch
->y
);
160 input_mt_sync_frame(ts
->input
);
161 input_sync(ts
->input
);
164 static irqreturn_t
pixcir_ts_isr(int irq
, void *dev_id
)
166 struct pixcir_i2c_ts_data
*tsdata
= dev_id
;
167 struct pixcir_report_data report
;
169 while (tsdata
->running
) {
171 pixcir_ts_parse(tsdata
, &report
);
174 pixcir_ts_report(tsdata
, &report
);
176 if (gpiod_get_value_cansleep(tsdata
->gpio_attb
)) {
177 if (report
.num_touches
) {
179 * Last report with no finger up?
182 input_mt_sync_frame(tsdata
->input
);
183 input_sync(tsdata
->input
);
194 static void pixcir_reset(struct pixcir_i2c_ts_data
*tsdata
)
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. */
205 static int pixcir_set_power_mode(struct pixcir_i2c_ts_data
*ts
,
206 enum pixcir_power_mode mode
)
208 struct device
*dev
= &ts
->client
->dev
;
211 ret
= i2c_smbus_read_byte_data(ts
->client
, PIXCIR_REG_POWER_MODE
);
213 dev_err(dev
, "%s: can't read reg 0x%x : %d\n",
214 __func__
, PIXCIR_REG_POWER_MODE
, ret
);
218 ret
&= ~PIXCIR_POWER_MODE_MASK
;
221 /* Always AUTO_IDLE */
222 ret
|= PIXCIR_POWER_ALLOW_IDLE
;
224 ret
= i2c_smbus_write_byte_data(ts
->client
, PIXCIR_REG_POWER_MODE
, ret
);
226 dev_err(dev
, "%s: can't write reg 0x%x : %d\n",
227 __func__
, PIXCIR_REG_POWER_MODE
, ret
);
235 * Set the interrupt mode for the device i.e. ATTB line behaviour
237 * @polarity : 1 for active high, 0 for active low.
239 static int pixcir_set_int_mode(struct pixcir_i2c_ts_data
*ts
,
240 enum pixcir_int_mode mode
, bool polarity
)
242 struct device
*dev
= &ts
->client
->dev
;
245 ret
= i2c_smbus_read_byte_data(ts
->client
, PIXCIR_REG_INT_MODE
);
247 dev_err(dev
, "%s: can't read reg 0x%x : %d\n",
248 __func__
, PIXCIR_REG_INT_MODE
, ret
);
252 ret
&= ~PIXCIR_INT_MODE_MASK
;
256 ret
|= PIXCIR_INT_POL_HIGH
;
258 ret
&= ~PIXCIR_INT_POL_HIGH
;
260 ret
= i2c_smbus_write_byte_data(ts
->client
, PIXCIR_REG_INT_MODE
, ret
);
262 dev_err(dev
, "%s: can't write reg 0x%x : %d\n",
263 __func__
, PIXCIR_REG_INT_MODE
, ret
);
271 * Enable/disable interrupt generation
273 static int pixcir_int_enable(struct pixcir_i2c_ts_data
*ts
, bool enable
)
275 struct device
*dev
= &ts
->client
->dev
;
278 ret
= i2c_smbus_read_byte_data(ts
->client
, PIXCIR_REG_INT_MODE
);
280 dev_err(dev
, "%s: can't read reg 0x%x : %d\n",
281 __func__
, PIXCIR_REG_INT_MODE
, ret
);
286 ret
|= PIXCIR_INT_ENABLE
;
288 ret
&= ~PIXCIR_INT_ENABLE
;
290 ret
= i2c_smbus_write_byte_data(ts
->client
, PIXCIR_REG_INT_MODE
, ret
);
292 dev_err(dev
, "%s: can't write reg 0x%x : %d\n",
293 __func__
, PIXCIR_REG_INT_MODE
, ret
);
300 static int pixcir_start(struct pixcir_i2c_ts_data
*ts
)
302 struct device
*dev
= &ts
->client
->dev
;
305 /* LEVEL_TOUCH interrupt with active low polarity */
306 error
= pixcir_set_int_mode(ts
, PIXCIR_INT_LEVEL_TOUCH
, 0);
308 dev_err(dev
, "Failed to set interrupt mode: %d\n", error
);
313 mb(); /* Update status before IRQ can fire */
315 /* enable interrupt generation */
316 error
= pixcir_int_enable(ts
, true);
318 dev_err(dev
, "Failed to enable interrupt generation: %d\n",
326 static int pixcir_stop(struct pixcir_i2c_ts_data
*ts
)
330 /* Disable interrupt generation */
331 error
= pixcir_int_enable(ts
, false);
333 dev_err(&ts
->client
->dev
,
334 "Failed to disable interrupt generation: %d\n",
339 /* Exit ISR if running, no more report parsing */
341 mb(); /* update status before we synchronize irq */
343 /* Wait till running ISR is complete */
344 synchronize_irq(ts
->client
->irq
);
349 static int pixcir_input_open(struct input_dev
*dev
)
351 struct pixcir_i2c_ts_data
*ts
= input_get_drvdata(dev
);
353 return pixcir_start(ts
);
356 static void pixcir_input_close(struct input_dev
*dev
)
358 struct pixcir_i2c_ts_data
*ts
= input_get_drvdata(dev
);
363 static int __maybe_unused
pixcir_i2c_ts_suspend(struct device
*dev
)
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
;
370 mutex_lock(&input
->mutex
);
372 if (device_may_wakeup(&client
->dev
)) {
374 ret
= pixcir_start(ts
);
376 dev_err(dev
, "Failed to start\n");
380 } else if (input
->users
) {
381 ret
= pixcir_stop(ts
);
385 mutex_unlock(&input
->mutex
);
390 static int __maybe_unused
pixcir_i2c_ts_resume(struct device
*dev
)
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
;
397 mutex_lock(&input
->mutex
);
399 if (device_may_wakeup(&client
->dev
)) {
402 ret
= pixcir_stop(ts
);
404 dev_err(dev
, "Failed to stop\n");
408 } else if (input
->users
) {
409 ret
= pixcir_start(ts
);
413 mutex_unlock(&input
->mutex
);
418 static SIMPLE_DEV_PM_OPS(pixcir_dev_pm_ops
,
419 pixcir_i2c_ts_suspend
, pixcir_i2c_ts_resume
);
422 static const struct of_device_id pixcir_of_match
[];
424 static int pixcir_parse_dt(struct device
*dev
,
425 struct pixcir_i2c_ts_data
*tsdata
)
427 const struct of_device_id
*match
;
429 match
= of_match_device(of_match_ptr(pixcir_of_match
), dev
);
433 tsdata
->chip
= (const struct pixcir_i2c_chip_data
*)match
->data
;
440 static int pixcir_parse_dt(struct device
*dev
,
441 struct pixcir_i2c_ts_data
*tsdata
)
447 static int pixcir_i2c_ts_probe(struct i2c_client
*client
,
448 const struct i2c_device_id
*id
)
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
;
457 tsdata
= devm_kzalloc(dev
, sizeof(*tsdata
), GFP_KERNEL
);
462 tsdata
->chip
= &pdata
->chip
;
463 } else if (dev
->of_node
) {
464 error
= pixcir_parse_dt(dev
, tsdata
);
468 dev_err(&client
->dev
, "platform data not defined\n");
472 if (!tsdata
->chip
->max_fingers
) {
473 dev_err(dev
, "Invalid max_fingers in chip data\n");
477 input
= devm_input_allocate_device(dev
);
479 dev_err(dev
, "Failed to allocate input device\n");
483 tsdata
->client
= client
;
484 tsdata
->input
= input
;
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
;
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);
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");
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
);
513 error
= input_mt_init_slots(input
, tsdata
->max_fingers
,
514 INPUT_MT_DIRECT
| INPUT_MT_DROP_UNUSED
);
516 dev_err(dev
, "Error initializing Multi-Touch slots\n");
520 input_set_drvdata(input
, tsdata
);
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
);
529 tsdata
->gpio_reset
= devm_gpiod_get_optional(dev
, "reset",
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
);
537 error
= devm_request_threaded_irq(dev
, client
->irq
, NULL
, pixcir_ts_isr
,
538 IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
539 client
->name
, tsdata
);
541 dev_err(dev
, "failed to request irq %d\n", client
->irq
);
545 pixcir_reset(tsdata
);
547 /* Always be in IDLE mode to save power, device supports auto wake */
548 error
= pixcir_set_power_mode(tsdata
, PIXCIR_POWER_IDLE
);
550 dev_err(dev
, "Failed to set IDLE mode\n");
554 /* Stop device till opened */
555 error
= pixcir_stop(tsdata
);
559 error
= input_register_device(input
);
563 i2c_set_clientdata(client
, tsdata
);
568 static const struct i2c_device_id pixcir_i2c_ts_id
[] = {
570 { "pixcir_tangoc", 0 },
573 MODULE_DEVICE_TABLE(i2c
, pixcir_i2c_ts_id
);
576 static const struct pixcir_i2c_chip_data pixcir_ts_data
= {
578 /* no hw id support */
581 static const struct pixcir_i2c_chip_data pixcir_tangoc_data
= {
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
},
591 MODULE_DEVICE_TABLE(of
, pixcir_of_match
);
594 static struct i2c_driver pixcir_i2c_ts_driver
= {
597 .pm
= &pixcir_dev_pm_ops
,
598 .of_match_table
= of_match_ptr(pixcir_of_match
),
600 .probe
= pixcir_i2c_ts_probe
,
601 .id_table
= pixcir_i2c_ts_id
,
604 module_i2c_driver(pixcir_i2c_ts_driver
);
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");