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/pixcir_ts.h>
27 #include <linux/gpio.h>
29 struct pixcir_i2c_ts_data
{
30 struct i2c_client
*client
;
31 struct input_dev
*input
;
32 const struct pixcir_ts_platform_data
*chip
;
36 static void pixcir_ts_poscheck(struct pixcir_i2c_ts_data
*data
)
38 struct pixcir_i2c_ts_data
*tsdata
= data
;
39 u8 rdbuf
[10], wrbuf
[1] = { 0 };
43 ret
= i2c_master_send(tsdata
->client
, wrbuf
, sizeof(wrbuf
));
44 if (ret
!= sizeof(wrbuf
)) {
45 dev_err(&tsdata
->client
->dev
,
46 "%s: i2c_master_send failed(), ret=%d\n",
51 ret
= i2c_master_recv(tsdata
->client
, rdbuf
, sizeof(rdbuf
));
52 if (ret
!= sizeof(rdbuf
)) {
53 dev_err(&tsdata
->client
->dev
,
54 "%s: i2c_master_recv failed(), ret=%d\n",
61 u16 posx1
= (rdbuf
[3] << 8) | rdbuf
[2];
62 u16 posy1
= (rdbuf
[5] << 8) | rdbuf
[4];
63 u16 posx2
= (rdbuf
[7] << 8) | rdbuf
[6];
64 u16 posy2
= (rdbuf
[9] << 8) | rdbuf
[8];
66 input_report_key(tsdata
->input
, BTN_TOUCH
, 1);
67 input_report_abs(tsdata
->input
, ABS_X
, posx1
);
68 input_report_abs(tsdata
->input
, ABS_Y
, posy1
);
70 input_report_abs(tsdata
->input
, ABS_MT_POSITION_X
, posx1
);
71 input_report_abs(tsdata
->input
, ABS_MT_POSITION_Y
, posy1
);
72 input_mt_sync(tsdata
->input
);
75 input_report_abs(tsdata
->input
,
76 ABS_MT_POSITION_X
, posx2
);
77 input_report_abs(tsdata
->input
,
78 ABS_MT_POSITION_Y
, posy2
);
79 input_mt_sync(tsdata
->input
);
82 input_report_key(tsdata
->input
, BTN_TOUCH
, 0);
85 input_sync(tsdata
->input
);
88 static irqreturn_t
pixcir_ts_isr(int irq
, void *dev_id
)
90 struct pixcir_i2c_ts_data
*tsdata
= dev_id
;
91 const struct pixcir_ts_platform_data
*pdata
= tsdata
->chip
;
93 while (tsdata
->running
) {
94 pixcir_ts_poscheck(tsdata
);
96 if (gpio_get_value(pdata
->gpio_attb
))
105 static int pixcir_set_power_mode(struct pixcir_i2c_ts_data
*ts
,
106 enum pixcir_power_mode mode
)
108 struct device
*dev
= &ts
->client
->dev
;
111 ret
= i2c_smbus_read_byte_data(ts
->client
, PIXCIR_REG_POWER_MODE
);
113 dev_err(dev
, "%s: can't read reg 0x%x : %d\n",
114 __func__
, PIXCIR_REG_POWER_MODE
, ret
);
118 ret
&= ~PIXCIR_POWER_MODE_MASK
;
121 /* Always AUTO_IDLE */
122 ret
|= PIXCIR_POWER_ALLOW_IDLE
;
124 ret
= i2c_smbus_write_byte_data(ts
->client
, PIXCIR_REG_POWER_MODE
, ret
);
126 dev_err(dev
, "%s: can't write reg 0x%x : %d\n",
127 __func__
, PIXCIR_REG_POWER_MODE
, ret
);
135 * Set the interrupt mode for the device i.e. ATTB line behaviour
137 * @polarity : 1 for active high, 0 for active low.
139 static int pixcir_set_int_mode(struct pixcir_i2c_ts_data
*ts
,
140 enum pixcir_int_mode mode
, bool polarity
)
142 struct device
*dev
= &ts
->client
->dev
;
145 ret
= i2c_smbus_read_byte_data(ts
->client
, PIXCIR_REG_INT_MODE
);
147 dev_err(dev
, "%s: can't read reg 0x%x : %d\n",
148 __func__
, PIXCIR_REG_INT_MODE
, ret
);
152 ret
&= ~PIXCIR_INT_MODE_MASK
;
156 ret
|= PIXCIR_INT_POL_HIGH
;
158 ret
&= ~PIXCIR_INT_POL_HIGH
;
160 ret
= i2c_smbus_write_byte_data(ts
->client
, PIXCIR_REG_INT_MODE
, ret
);
162 dev_err(dev
, "%s: can't write reg 0x%x : %d\n",
163 __func__
, PIXCIR_REG_INT_MODE
, ret
);
171 * Enable/disable interrupt generation
173 static int pixcir_int_enable(struct pixcir_i2c_ts_data
*ts
, bool enable
)
175 struct device
*dev
= &ts
->client
->dev
;
178 ret
= i2c_smbus_read_byte_data(ts
->client
, PIXCIR_REG_INT_MODE
);
180 dev_err(dev
, "%s: can't read reg 0x%x : %d\n",
181 __func__
, PIXCIR_REG_INT_MODE
, ret
);
186 ret
|= PIXCIR_INT_ENABLE
;
188 ret
&= ~PIXCIR_INT_ENABLE
;
190 ret
= i2c_smbus_write_byte_data(ts
->client
, PIXCIR_REG_INT_MODE
, ret
);
192 dev_err(dev
, "%s: can't write reg 0x%x : %d\n",
193 __func__
, PIXCIR_REG_INT_MODE
, ret
);
200 static int pixcir_start(struct pixcir_i2c_ts_data
*ts
)
202 struct device
*dev
= &ts
->client
->dev
;
205 /* LEVEL_TOUCH interrupt with active low polarity */
206 error
= pixcir_set_int_mode(ts
, PIXCIR_INT_LEVEL_TOUCH
, 0);
208 dev_err(dev
, "Failed to set interrupt mode: %d\n", error
);
213 mb(); /* Update status before IRQ can fire */
215 /* enable interrupt generation */
216 error
= pixcir_int_enable(ts
, true);
218 dev_err(dev
, "Failed to enable interrupt generation: %d\n",
226 static int pixcir_stop(struct pixcir_i2c_ts_data
*ts
)
230 /* Disable interrupt generation */
231 error
= pixcir_int_enable(ts
, false);
233 dev_err(&ts
->client
->dev
,
234 "Failed to disable interrupt generation: %d\n",
239 /* Exit ISR if running, no more report parsing */
241 mb(); /* update status before we synchronize irq */
243 /* Wait till running ISR is complete */
244 synchronize_irq(ts
->client
->irq
);
249 static int pixcir_input_open(struct input_dev
*dev
)
251 struct pixcir_i2c_ts_data
*ts
= input_get_drvdata(dev
);
253 return pixcir_start(ts
);
256 static void pixcir_input_close(struct input_dev
*dev
)
258 struct pixcir_i2c_ts_data
*ts
= input_get_drvdata(dev
);
263 #ifdef CONFIG_PM_SLEEP
264 static int pixcir_i2c_ts_suspend(struct device
*dev
)
266 struct i2c_client
*client
= to_i2c_client(dev
);
267 struct pixcir_i2c_ts_data
*ts
= i2c_get_clientdata(client
);
268 struct input_dev
*input
= ts
->input
;
271 mutex_lock(&input
->mutex
);
273 if (device_may_wakeup(&client
->dev
)) {
275 ret
= pixcir_start(ts
);
277 dev_err(dev
, "Failed to start\n");
282 enable_irq_wake(client
->irq
);
283 } else if (input
->users
) {
284 ret
= pixcir_stop(ts
);
288 mutex_unlock(&input
->mutex
);
293 static int pixcir_i2c_ts_resume(struct device
*dev
)
295 struct i2c_client
*client
= to_i2c_client(dev
);
296 struct pixcir_i2c_ts_data
*ts
= i2c_get_clientdata(client
);
297 struct input_dev
*input
= ts
->input
;
300 mutex_lock(&input
->mutex
);
302 if (device_may_wakeup(&client
->dev
)) {
303 disable_irq_wake(client
->irq
);
306 ret
= pixcir_stop(ts
);
308 dev_err(dev
, "Failed to stop\n");
312 } else if (input
->users
) {
313 ret
= pixcir_start(ts
);
317 mutex_unlock(&input
->mutex
);
323 static SIMPLE_DEV_PM_OPS(pixcir_dev_pm_ops
,
324 pixcir_i2c_ts_suspend
, pixcir_i2c_ts_resume
);
326 static int pixcir_i2c_ts_probe(struct i2c_client
*client
,
327 const struct i2c_device_id
*id
)
329 const struct pixcir_ts_platform_data
*pdata
=
330 dev_get_platdata(&client
->dev
);
331 struct device
*dev
= &client
->dev
;
332 struct pixcir_i2c_ts_data
*tsdata
;
333 struct input_dev
*input
;
337 dev_err(&client
->dev
, "platform data not defined\n");
341 if (!gpio_is_valid(pdata
->gpio_attb
)) {
342 dev_err(dev
, "Invalid gpio_attb in pdata\n");
346 tsdata
= devm_kzalloc(dev
, sizeof(*tsdata
), GFP_KERNEL
);
350 input
= devm_input_allocate_device(dev
);
352 dev_err(dev
, "Failed to allocate input device\n");
356 tsdata
->client
= client
;
357 tsdata
->input
= input
;
358 tsdata
->chip
= pdata
;
360 input
->name
= client
->name
;
361 input
->id
.bustype
= BUS_I2C
;
362 input
->open
= pixcir_input_open
;
363 input
->close
= pixcir_input_close
;
364 input
->dev
.parent
= &client
->dev
;
366 __set_bit(EV_KEY
, input
->evbit
);
367 __set_bit(EV_ABS
, input
->evbit
);
368 __set_bit(BTN_TOUCH
, input
->keybit
);
369 input_set_abs_params(input
, ABS_X
, 0, pdata
->x_max
, 0, 0);
370 input_set_abs_params(input
, ABS_Y
, 0, pdata
->y_max
, 0, 0);
371 input_set_abs_params(input
, ABS_MT_POSITION_X
, 0, pdata
->x_max
, 0, 0);
372 input_set_abs_params(input
, ABS_MT_POSITION_Y
, 0, pdata
->y_max
, 0, 0);
374 input_set_drvdata(input
, tsdata
);
376 error
= devm_gpio_request_one(dev
, pdata
->gpio_attb
,
377 GPIOF_DIR_IN
, "pixcir_i2c_attb");
379 dev_err(dev
, "Failed to request ATTB gpio\n");
383 error
= devm_request_threaded_irq(dev
, client
->irq
, NULL
, pixcir_ts_isr
,
384 IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
385 client
->name
, tsdata
);
387 dev_err(dev
, "failed to request irq %d\n", client
->irq
);
391 /* Always be in IDLE mode to save power, device supports auto wake */
392 error
= pixcir_set_power_mode(tsdata
, PIXCIR_POWER_IDLE
);
394 dev_err(dev
, "Failed to set IDLE mode\n");
398 /* Stop device till opened */
399 error
= pixcir_stop(tsdata
);
403 error
= input_register_device(input
);
407 i2c_set_clientdata(client
, tsdata
);
408 device_init_wakeup(&client
->dev
, 1);
413 static int pixcir_i2c_ts_remove(struct i2c_client
*client
)
415 device_init_wakeup(&client
->dev
, 0);
420 static const struct i2c_device_id pixcir_i2c_ts_id
[] = {
424 MODULE_DEVICE_TABLE(i2c
, pixcir_i2c_ts_id
);
426 static struct i2c_driver pixcir_i2c_ts_driver
= {
428 .owner
= THIS_MODULE
,
430 .pm
= &pixcir_dev_pm_ops
,
432 .probe
= pixcir_i2c_ts_probe
,
433 .remove
= pixcir_i2c_ts_remove
,
434 .id_table
= pixcir_i2c_ts_id
,
437 module_i2c_driver(pixcir_i2c_ts_driver
);
439 MODULE_AUTHOR("Jianchun Bian <jcbian@pixcir.com.cn>, Dequan Meng <dqmeng@pixcir.com.cn>");
440 MODULE_DESCRIPTION("Pixcir I2C Touchscreen Driver");
441 MODULE_LICENSE("GPL");