2 * drivers/input/touchscreen/tps6507x_ts.c
4 * Touchscreen driver for the tps6507x chip.
6 * Copyright (c) 2009 RidgeRun (todd.fischer@ridgerun.com)
10 * Using code from tsc2007, MtekVision Co., Ltd.
12 * For licencing details see kernel-base/COPYING
14 * TPS65070, TPS65073, TPS650731, and TPS650732 support
15 * 10 bit touch screen interface.
18 #include <linux/module.h>
19 #include <linux/workqueue.h>
20 #include <linux/slab.h>
21 #include <linux/input.h>
22 #include <linux/platform_device.h>
23 #include <linux/mfd/tps6507x.h>
24 #include <linux/input/tps6507x-ts.h>
25 #include <linux/delay.h>
27 #define TSC_DEFAULT_POLL_PERIOD 30 /* ms */
28 #define TPS_DEFAULT_MIN_PRESSURE 0x30
29 #define MAX_10BIT ((1 << 10) - 1)
31 #define TPS6507X_ADCONFIG_CONVERT_TS (TPS6507X_ADCONFIG_AD_ENABLE | \
32 TPS6507X_ADCONFIG_START_CONVERSION | \
33 TPS6507X_ADCONFIG_INPUT_REAL_TSC)
34 #define TPS6507X_ADCONFIG_POWER_DOWN_TS (TPS6507X_ADCONFIG_INPUT_REAL_TSC)
43 struct input_dev
*input_dev
;
46 struct delayed_work work
;
47 unsigned polling
; /* polling is active */
49 struct tps6507x_dev
*mfd
;
53 void (*clear_penirq
)(void);
54 unsigned long poll_period
; /* ms */
56 int vref
; /* non-zero to leave vref on */
59 static int tps6507x_read_u8(struct tps6507x_ts
*tsc
, u8 reg
, u8
*data
)
63 err
= tsc
->mfd
->read_dev(tsc
->mfd
, reg
, 1, data
);
71 static int tps6507x_write_u8(struct tps6507x_ts
*tsc
, u8 reg
, u8 data
)
73 return tsc
->mfd
->write_dev(tsc
->mfd
, reg
, 1, &data
);
76 static s32
tps6507x_adc_conversion(struct tps6507x_ts
*tsc
,
77 u8 tsc_mode
, u16
*value
)
83 /* Route input signal to A/D converter */
85 ret
= tps6507x_write_u8(tsc
, TPS6507X_REG_TSCMODE
, tsc_mode
);
87 dev_err(tsc
->dev
, "TSC mode read failed\n");
91 /* Start A/D conversion */
93 ret
= tps6507x_write_u8(tsc
, TPS6507X_REG_ADCONFIG
,
94 TPS6507X_ADCONFIG_CONVERT_TS
);
96 dev_err(tsc
->dev
, "ADC config write failed\n");
101 ret
= tps6507x_read_u8(tsc
, TPS6507X_REG_ADCONFIG
,
104 dev_err(tsc
->dev
, "ADC config read failed\n");
107 } while (adc_status
& TPS6507X_ADCONFIG_START_CONVERSION
);
109 ret
= tps6507x_read_u8(tsc
, TPS6507X_REG_ADRESULT_2
, &result
);
111 dev_err(tsc
->dev
, "ADC result 2 read failed\n");
115 *value
= (result
& TPS6507X_REG_ADRESULT_2_MASK
) << 8;
117 ret
= tps6507x_read_u8(tsc
, TPS6507X_REG_ADRESULT_1
, &result
);
119 dev_err(tsc
->dev
, "ADC result 1 read failed\n");
125 dev_dbg(tsc
->dev
, "TSC channel %d = 0x%X\n", tsc_mode
, *value
);
131 /* Need to call tps6507x_adc_standby() after using A/D converter for the
132 * touch screen interrupt to work properly.
135 static s32
tps6507x_adc_standby(struct tps6507x_ts
*tsc
)
141 ret
= tps6507x_write_u8(tsc
, TPS6507X_REG_ADCONFIG
,
142 TPS6507X_ADCONFIG_INPUT_TSC
);
146 ret
= tps6507x_write_u8(tsc
, TPS6507X_REG_TSCMODE
,
147 TPS6507X_TSCMODE_STANDBY
);
151 ret
= tps6507x_read_u8(tsc
, TPS6507X_REG_INT
, &val
);
155 while (val
& TPS6507X_REG_TSC_INT
) {
157 ret
= tps6507x_read_u8(tsc
, TPS6507X_REG_INT
, &val
);
166 static void tps6507x_ts_handler(struct work_struct
*work
)
168 struct tps6507x_ts
*tsc
= container_of(work
,
169 struct tps6507x_ts
, work
.work
);
170 struct input_dev
*input_dev
= tsc
->input_dev
;
176 ret
= tps6507x_adc_conversion(tsc
, TPS6507X_TSCMODE_PRESSURE
,
181 pendown
= tsc
->tc
.pressure
> tsc
->min_pressure
;
183 if (unlikely(!pendown
&& tsc
->pendown
)) {
184 dev_dbg(tsc
->dev
, "UP\n");
185 input_report_key(input_dev
, BTN_TOUCH
, 0);
186 input_report_abs(input_dev
, ABS_PRESSURE
, 0);
187 input_sync(input_dev
);
194 dev_dbg(tsc
->dev
, "DOWN\n");
195 input_report_key(input_dev
, BTN_TOUCH
, 1);
197 dev_dbg(tsc
->dev
, "still down\n");
199 ret
= tps6507x_adc_conversion(tsc
, TPS6507X_TSCMODE_X_POSITION
,
204 ret
= tps6507x_adc_conversion(tsc
, TPS6507X_TSCMODE_Y_POSITION
,
209 input_report_abs(input_dev
, ABS_X
, tsc
->tc
.x
);
210 input_report_abs(input_dev
, ABS_Y
, tsc
->tc
.y
);
211 input_report_abs(input_dev
, ABS_PRESSURE
, tsc
->tc
.pressure
);
212 input_sync(input_dev
);
218 /* always poll if not using interrupts */
222 schd
= schedule_delayed_work(&tsc
->work
,
223 msecs_to_jiffies(tsc
->poll_period
));
228 dev_err(tsc
->dev
, "re-schedule failed");
233 ret
= tps6507x_adc_standby(tsc
);
236 static int tps6507x_ts_probe(struct platform_device
*pdev
)
239 struct tps6507x_ts
*tsc
;
240 struct tps6507x_dev
*tps6507x_dev
= dev_get_drvdata(pdev
->dev
.parent
);
241 struct touchscreen_init_data
*init_data
;
242 struct input_dev
*input_dev
;
243 struct tps6507x_board
*tps_board
;
247 * tps_board points to pmic related constants
248 * coming from the board-evm file.
251 tps_board
= (struct tps6507x_board
*)tps6507x_dev
->dev
->platform_data
;
254 dev_err(tps6507x_dev
->dev
,
255 "Could not find tps6507x platform data\n");
260 * init_data points to array of regulator_init structures
261 * coming from the board-evm file.
264 init_data
= tps_board
->tps6507x_ts_init_data
;
266 tsc
= kzalloc(sizeof(struct tps6507x_ts
), GFP_KERNEL
);
268 dev_err(tps6507x_dev
->dev
, "failed to allocate driver data\n");
273 tps6507x_dev
->ts
= tsc
;
274 tsc
->mfd
= tps6507x_dev
;
275 tsc
->dev
= tps6507x_dev
->dev
;
276 input_dev
= input_allocate_device();
278 dev_err(tsc
->dev
, "Failed to allocate input device.\n");
283 input_dev
->evbit
[0] = BIT_MASK(EV_KEY
) | BIT_MASK(EV_ABS
);
284 input_dev
->keybit
[BIT_WORD(BTN_TOUCH
)] = BIT_MASK(BTN_TOUCH
);
286 input_set_abs_params(input_dev
, ABS_X
, 0, MAX_10BIT
, 0, 0);
287 input_set_abs_params(input_dev
, ABS_Y
, 0, MAX_10BIT
, 0, 0);
288 input_set_abs_params(input_dev
, ABS_PRESSURE
, 0, MAX_10BIT
, 0, 0);
290 input_dev
->name
= "TPS6507x Touchscreen";
291 input_dev
->id
.bustype
= BUS_I2C
;
292 input_dev
->dev
.parent
= tsc
->dev
;
294 snprintf(tsc
->phys
, sizeof(tsc
->phys
),
295 "%s/input0", dev_name(tsc
->dev
));
296 input_dev
->phys
= tsc
->phys
;
298 dev_dbg(tsc
->dev
, "device: %s\n", input_dev
->phys
);
300 input_set_drvdata(input_dev
, tsc
);
302 tsc
->input_dev
= input_dev
;
304 INIT_DELAYED_WORK(&tsc
->work
, tps6507x_ts_handler
);
307 tsc
->poll_period
= init_data
->poll_period
;
308 tsc
->vref
= init_data
->vref
;
309 tsc
->min_pressure
= init_data
->min_pressure
;
310 input_dev
->id
.vendor
= init_data
->vendor
;
311 input_dev
->id
.product
= init_data
->product
;
312 input_dev
->id
.version
= init_data
->version
;
314 tsc
->poll_period
= TSC_DEFAULT_POLL_PERIOD
;
315 tsc
->min_pressure
= TPS_DEFAULT_MIN_PRESSURE
;
318 error
= tps6507x_adc_standby(tsc
);
322 error
= input_register_device(input_dev
);
326 schd
= schedule_delayed_work(&tsc
->work
,
327 msecs_to_jiffies(tsc
->poll_period
));
333 dev_err(tsc
->dev
, "schedule failed");
336 platform_set_drvdata(pdev
, tps6507x_dev
);
341 cancel_delayed_work_sync(&tsc
->work
);
342 input_free_device(input_dev
);
345 tps6507x_dev
->ts
= NULL
;
350 static int __devexit
tps6507x_ts_remove(struct platform_device
*pdev
)
352 struct tps6507x_dev
*tps6507x_dev
= platform_get_drvdata(pdev
);
353 struct tps6507x_ts
*tsc
= tps6507x_dev
->ts
;
354 struct input_dev
*input_dev
= tsc
->input_dev
;
356 cancel_delayed_work_sync(&tsc
->work
);
358 input_unregister_device(input_dev
);
360 tps6507x_dev
->ts
= NULL
;
366 static struct platform_driver tps6507x_ts_driver
= {
368 .name
= "tps6507x-ts",
369 .owner
= THIS_MODULE
,
371 .probe
= tps6507x_ts_probe
,
372 .remove
= __devexit_p(tps6507x_ts_remove
),
375 static int __init
tps6507x_ts_init(void)
377 return platform_driver_register(&tps6507x_ts_driver
);
379 module_init(tps6507x_ts_init
);
381 static void __exit
tps6507x_ts_exit(void)
383 platform_driver_unregister(&tps6507x_ts_driver
);
385 module_exit(tps6507x_ts_exit
);
387 MODULE_AUTHOR("Todd Fischer <todd.fischer@ridgerun.com>");
388 MODULE_DESCRIPTION("TPS6507x - TouchScreen driver");
389 MODULE_LICENSE("GPL v2");
390 MODULE_ALIAS("platform:tps6507x-tsc");