2 * Touchscreen driver for the tps6507x chip.
4 * Copyright (c) 2009 RidgeRun (todd.fischer@ridgerun.com)
8 * Using code from tsc2007, MtekVision Co., Ltd.
10 * For licencing details see kernel-base/COPYING
12 * TPS65070, TPS65073, TPS650731, and TPS650732 support
13 * 10 bit touch screen interface.
16 #include <linux/module.h>
17 #include <linux/workqueue.h>
18 #include <linux/slab.h>
19 #include <linux/input.h>
20 #include <linux/platform_device.h>
21 #include <linux/mfd/tps6507x.h>
22 #include <linux/input/tps6507x-ts.h>
23 #include <linux/delay.h>
25 #define TSC_DEFAULT_POLL_PERIOD 30 /* ms */
26 #define TPS_DEFAULT_MIN_PRESSURE 0x30
27 #define MAX_10BIT ((1 << 10) - 1)
29 #define TPS6507X_ADCONFIG_CONVERT_TS (TPS6507X_ADCONFIG_AD_ENABLE | \
30 TPS6507X_ADCONFIG_START_CONVERSION | \
31 TPS6507X_ADCONFIG_INPUT_REAL_TSC)
32 #define TPS6507X_ADCONFIG_POWER_DOWN_TS (TPS6507X_ADCONFIG_INPUT_REAL_TSC)
41 struct input_dev *input_dev;
44 struct delayed_work work;
45 unsigned polling; /* polling is active */
47 struct tps6507x_dev *mfd;
51 void (*clear_penirq)(void);
52 unsigned long poll_period; /* ms */
54 int vref; /* non-zero to leave vref on */
57 static int tps6507x_read_u8(struct tps6507x_ts *tsc, u8 reg, u8 *data)
61 err = tsc->mfd->read_dev(tsc->mfd, reg, 1, data);
69 static int tps6507x_write_u8(struct tps6507x_ts *tsc, u8 reg, u8 data)
71 return tsc->mfd->write_dev(tsc->mfd, reg, 1, &data);
74 static s32 tps6507x_adc_conversion(struct tps6507x_ts *tsc,
75 u8 tsc_mode, u16 *value)
81 /* Route input signal to A/D converter */
83 ret = tps6507x_write_u8(tsc, TPS6507X_REG_TSCMODE, tsc_mode);
85 dev_err(tsc->dev, "TSC mode read failed\n");
89 /* Start A/D conversion */
91 ret = tps6507x_write_u8(tsc, TPS6507X_REG_ADCONFIG,
92 TPS6507X_ADCONFIG_CONVERT_TS);
94 dev_err(tsc->dev, "ADC config write failed\n");
99 ret = tps6507x_read_u8(tsc, TPS6507X_REG_ADCONFIG,
102 dev_err(tsc->dev, "ADC config read failed\n");
105 } while (adc_status & TPS6507X_ADCONFIG_START_CONVERSION);
107 ret = tps6507x_read_u8(tsc, TPS6507X_REG_ADRESULT_2, &result);
109 dev_err(tsc->dev, "ADC result 2 read failed\n");
113 *value = (result & TPS6507X_REG_ADRESULT_2_MASK) << 8;
115 ret = tps6507x_read_u8(tsc, TPS6507X_REG_ADRESULT_1, &result);
117 dev_err(tsc->dev, "ADC result 1 read failed\n");
123 dev_dbg(tsc->dev, "TSC channel %d = 0x%X\n", tsc_mode, *value);
129 /* Need to call tps6507x_adc_standby() after using A/D converter for the
130 * touch screen interrupt to work properly.
133 static s32 tps6507x_adc_standby(struct tps6507x_ts *tsc)
139 ret = tps6507x_write_u8(tsc, TPS6507X_REG_ADCONFIG,
140 TPS6507X_ADCONFIG_INPUT_TSC);
144 ret = tps6507x_write_u8(tsc, TPS6507X_REG_TSCMODE,
145 TPS6507X_TSCMODE_STANDBY);
149 ret = tps6507x_read_u8(tsc, TPS6507X_REG_INT, &val);
153 while (val & TPS6507X_REG_TSC_INT) {
155 ret = tps6507x_read_u8(tsc, TPS6507X_REG_INT, &val);
164 static void tps6507x_ts_handler(struct work_struct *work)
166 struct tps6507x_ts *tsc = container_of(work,
167 struct tps6507x_ts, work.work);
168 struct input_dev *input_dev = tsc->input_dev;
174 ret = tps6507x_adc_conversion(tsc, TPS6507X_TSCMODE_PRESSURE,
179 pendown = tsc->tc.pressure > tsc->min_pressure;
181 if (unlikely(!pendown && tsc->pendown)) {
182 dev_dbg(tsc->dev, "UP\n");
183 input_report_key(input_dev, BTN_TOUCH, 0);
184 input_report_abs(input_dev, ABS_PRESSURE, 0);
185 input_sync(input_dev);
192 dev_dbg(tsc->dev, "DOWN\n");
193 input_report_key(input_dev, BTN_TOUCH, 1);
195 dev_dbg(tsc->dev, "still down\n");
197 ret = tps6507x_adc_conversion(tsc, TPS6507X_TSCMODE_X_POSITION,
202 ret = tps6507x_adc_conversion(tsc, TPS6507X_TSCMODE_Y_POSITION,
207 input_report_abs(input_dev, ABS_X, tsc->tc.x);
208 input_report_abs(input_dev, ABS_Y, tsc->tc.y);
209 input_report_abs(input_dev, ABS_PRESSURE, tsc->tc.pressure);
210 input_sync(input_dev);
216 /* always poll if not using interrupts */
220 schd = schedule_delayed_work(&tsc->work,
221 msecs_to_jiffies(tsc->poll_period));
226 dev_err(tsc->dev, "re-schedule failed");
231 ret = tps6507x_adc_standby(tsc);
234 static int tps6507x_ts_probe(struct platform_device *pdev)
237 struct tps6507x_ts *tsc;
238 struct tps6507x_dev *tps6507x_dev = dev_get_drvdata(pdev->dev.parent);
239 struct touchscreen_init_data *init_data;
240 struct input_dev *input_dev;
241 struct tps6507x_board *tps_board;
245 * tps_board points to pmic related constants
246 * coming from the board-evm file.
249 tps_board = (struct tps6507x_board *)tps6507x_dev->dev->platform_data;
252 dev_err(tps6507x_dev->dev,
253 "Could not find tps6507x platform data\n");
258 * init_data points to array of regulator_init structures
259 * coming from the board-evm file.
262 init_data = tps_board->tps6507x_ts_init_data;
264 tsc = kzalloc(sizeof(struct tps6507x_ts), GFP_KERNEL);
266 dev_err(tps6507x_dev->dev, "failed to allocate driver data\n");
271 tps6507x_dev->ts = tsc;
272 tsc->mfd = tps6507x_dev;
273 tsc->dev = tps6507x_dev->dev;
274 input_dev = input_allocate_device();
276 dev_err(tsc->dev, "Failed to allocate input device.\n");
281 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
282 input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
284 input_set_abs_params(input_dev, ABS_X, 0, MAX_10BIT, 0, 0);
285 input_set_abs_params(input_dev, ABS_Y, 0, MAX_10BIT, 0, 0);
286 input_set_abs_params(input_dev, ABS_PRESSURE, 0, MAX_10BIT, 0, 0);
288 input_dev->name = "TPS6507x Touchscreen";
289 input_dev->id.bustype = BUS_I2C;
290 input_dev->dev.parent = tsc->dev;
292 snprintf(tsc->phys, sizeof(tsc->phys),
293 "%s/input0", dev_name(tsc->dev));
294 input_dev->phys = tsc->phys;
296 dev_dbg(tsc->dev, "device: %s\n", input_dev->phys);
298 input_set_drvdata(input_dev, tsc);
300 tsc->input_dev = input_dev;
302 INIT_DELAYED_WORK(&tsc->work, tps6507x_ts_handler);
305 tsc->poll_period = init_data->poll_period;
306 tsc->vref = init_data->vref;
307 tsc->min_pressure = init_data->min_pressure;
308 input_dev->id.vendor = init_data->vendor;
309 input_dev->id.product = init_data->product;
310 input_dev->id.version = init_data->version;
312 tsc->poll_period = TSC_DEFAULT_POLL_PERIOD;
313 tsc->min_pressure = TPS_DEFAULT_MIN_PRESSURE;
316 error = tps6507x_adc_standby(tsc);
320 error = input_register_device(input_dev);
324 schd = schedule_delayed_work(&tsc->work,
325 msecs_to_jiffies(tsc->poll_period));
331 dev_err(tsc->dev, "schedule failed");
334 platform_set_drvdata(pdev, tps6507x_dev);
339 cancel_delayed_work_sync(&tsc->work);
340 input_free_device(input_dev);
343 tps6507x_dev->ts = NULL;
348 static int tps6507x_ts_remove(struct platform_device *pdev)
350 struct tps6507x_dev *tps6507x_dev = platform_get_drvdata(pdev);
351 struct tps6507x_ts *tsc = tps6507x_dev->ts;
352 struct input_dev *input_dev = tsc->input_dev;
354 cancel_delayed_work_sync(&tsc->work);
356 input_unregister_device(input_dev);
358 tps6507x_dev->ts = NULL;
364 static struct platform_driver tps6507x_ts_driver = {
366 .name = "tps6507x-ts",
367 .owner = THIS_MODULE,
369 .probe = tps6507x_ts_probe,
370 .remove = tps6507x_ts_remove,
372 module_platform_driver(tps6507x_ts_driver);
374 MODULE_AUTHOR("Todd Fischer <todd.fischer@ridgerun.com>");
375 MODULE_DESCRIPTION("TPS6507x - TouchScreen driver");
376 MODULE_LICENSE("GPL v2");
377 MODULE_ALIAS("platform:tps6507x-ts");