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;
46 struct tps6507x_dev *mfd;
48 unsigned long poll_period; /* ms */
52 static int tps6507x_read_u8(struct tps6507x_ts *tsc, u8 reg, u8 *data)
56 err = tsc->mfd->read_dev(tsc->mfd, reg, 1, data);
64 static int tps6507x_write_u8(struct tps6507x_ts *tsc, u8 reg, u8 data)
66 return tsc->mfd->write_dev(tsc->mfd, reg, 1, &data);
69 static s32 tps6507x_adc_conversion(struct tps6507x_ts *tsc,
70 u8 tsc_mode, u16 *value)
76 /* Route input signal to A/D converter */
78 ret = tps6507x_write_u8(tsc, TPS6507X_REG_TSCMODE, tsc_mode);
80 dev_err(tsc->dev, "TSC mode read failed\n");
84 /* Start A/D conversion */
86 ret = tps6507x_write_u8(tsc, TPS6507X_REG_ADCONFIG,
87 TPS6507X_ADCONFIG_CONVERT_TS);
89 dev_err(tsc->dev, "ADC config write failed\n");
94 ret = tps6507x_read_u8(tsc, TPS6507X_REG_ADCONFIG,
97 dev_err(tsc->dev, "ADC config read failed\n");
100 } while (adc_status & TPS6507X_ADCONFIG_START_CONVERSION);
102 ret = tps6507x_read_u8(tsc, TPS6507X_REG_ADRESULT_2, &result);
104 dev_err(tsc->dev, "ADC result 2 read failed\n");
108 *value = (result & TPS6507X_REG_ADRESULT_2_MASK) << 8;
110 ret = tps6507x_read_u8(tsc, TPS6507X_REG_ADRESULT_1, &result);
112 dev_err(tsc->dev, "ADC result 1 read failed\n");
118 dev_dbg(tsc->dev, "TSC channel %d = 0x%X\n", tsc_mode, *value);
124 /* Need to call tps6507x_adc_standby() after using A/D converter for the
125 * touch screen interrupt to work properly.
128 static s32 tps6507x_adc_standby(struct tps6507x_ts *tsc)
134 ret = tps6507x_write_u8(tsc, TPS6507X_REG_ADCONFIG,
135 TPS6507X_ADCONFIG_INPUT_TSC);
139 ret = tps6507x_write_u8(tsc, TPS6507X_REG_TSCMODE,
140 TPS6507X_TSCMODE_STANDBY);
144 ret = tps6507x_read_u8(tsc, TPS6507X_REG_INT, &val);
148 while (val & TPS6507X_REG_TSC_INT) {
150 ret = tps6507x_read_u8(tsc, TPS6507X_REG_INT, &val);
159 static void tps6507x_ts_handler(struct work_struct *work)
161 struct tps6507x_ts *tsc = container_of(work,
162 struct tps6507x_ts, work.work);
163 struct input_dev *input_dev = tsc->input_dev;
168 ret = tps6507x_adc_conversion(tsc, TPS6507X_TSCMODE_PRESSURE,
173 pendown = tsc->tc.pressure > tsc->min_pressure;
175 if (unlikely(!pendown && tsc->pendown)) {
176 dev_dbg(tsc->dev, "UP\n");
177 input_report_key(input_dev, BTN_TOUCH, 0);
178 input_report_abs(input_dev, ABS_PRESSURE, 0);
179 input_sync(input_dev);
180 tsc->pendown = false;
186 dev_dbg(tsc->dev, "DOWN\n");
187 input_report_key(input_dev, BTN_TOUCH, 1);
189 dev_dbg(tsc->dev, "still down\n");
191 ret = tps6507x_adc_conversion(tsc, TPS6507X_TSCMODE_X_POSITION,
196 ret = tps6507x_adc_conversion(tsc, TPS6507X_TSCMODE_Y_POSITION,
201 input_report_abs(input_dev, ABS_X, tsc->tc.x);
202 input_report_abs(input_dev, ABS_Y, tsc->tc.y);
203 input_report_abs(input_dev, ABS_PRESSURE, tsc->tc.pressure);
204 input_sync(input_dev);
209 /* always poll if not using interrupts */
210 schd = schedule_delayed_work(&tsc->work,
211 msecs_to_jiffies(tsc->poll_period));
213 dev_err(tsc->dev, "re-schedule failed");
215 ret = tps6507x_adc_standby(tsc);
218 static int tps6507x_ts_probe(struct platform_device *pdev)
221 struct tps6507x_ts *tsc;
222 struct tps6507x_dev *tps6507x_dev = dev_get_drvdata(pdev->dev.parent);
223 struct touchscreen_init_data *init_data;
224 struct input_dev *input_dev;
225 struct tps6507x_board *tps_board;
229 * tps_board points to pmic related constants
230 * coming from the board-evm file.
233 tps_board = (struct tps6507x_board *)tps6507x_dev->dev->platform_data;
236 dev_err(tps6507x_dev->dev,
237 "Could not find tps6507x platform data\n");
242 * init_data points to array of regulator_init structures
243 * coming from the board-evm file.
246 init_data = tps_board->tps6507x_ts_init_data;
248 tsc = kzalloc(sizeof(struct tps6507x_ts), GFP_KERNEL);
250 dev_err(tps6507x_dev->dev, "failed to allocate driver data\n");
255 tps6507x_dev->ts = tsc;
256 tsc->mfd = tps6507x_dev;
257 tsc->dev = tps6507x_dev->dev;
258 input_dev = input_allocate_device();
260 dev_err(tsc->dev, "Failed to allocate input device.\n");
265 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
266 input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
268 input_set_abs_params(input_dev, ABS_X, 0, MAX_10BIT, 0, 0);
269 input_set_abs_params(input_dev, ABS_Y, 0, MAX_10BIT, 0, 0);
270 input_set_abs_params(input_dev, ABS_PRESSURE, 0, MAX_10BIT, 0, 0);
272 input_dev->name = "TPS6507x Touchscreen";
273 input_dev->id.bustype = BUS_I2C;
274 input_dev->dev.parent = tsc->dev;
276 snprintf(tsc->phys, sizeof(tsc->phys),
277 "%s/input0", dev_name(tsc->dev));
278 input_dev->phys = tsc->phys;
280 dev_dbg(tsc->dev, "device: %s\n", input_dev->phys);
282 input_set_drvdata(input_dev, tsc);
284 tsc->input_dev = input_dev;
286 INIT_DELAYED_WORK(&tsc->work, tps6507x_ts_handler);
289 tsc->poll_period = init_data->poll_period;
290 tsc->min_pressure = init_data->min_pressure;
291 input_dev->id.vendor = init_data->vendor;
292 input_dev->id.product = init_data->product;
293 input_dev->id.version = init_data->version;
295 tsc->poll_period = TSC_DEFAULT_POLL_PERIOD;
296 tsc->min_pressure = TPS_DEFAULT_MIN_PRESSURE;
299 error = tps6507x_adc_standby(tsc);
303 error = input_register_device(input_dev);
307 schd = schedule_delayed_work(&tsc->work,
308 msecs_to_jiffies(tsc->poll_period));
311 dev_err(tsc->dev, "schedule failed");
314 platform_set_drvdata(pdev, tps6507x_dev);
319 cancel_delayed_work_sync(&tsc->work);
320 input_free_device(input_dev);
323 tps6507x_dev->ts = NULL;
328 static int tps6507x_ts_remove(struct platform_device *pdev)
330 struct tps6507x_dev *tps6507x_dev = platform_get_drvdata(pdev);
331 struct tps6507x_ts *tsc = tps6507x_dev->ts;
332 struct input_dev *input_dev = tsc->input_dev;
334 cancel_delayed_work_sync(&tsc->work);
336 input_unregister_device(input_dev);
338 tps6507x_dev->ts = NULL;
344 static struct platform_driver tps6507x_ts_driver = {
346 .name = "tps6507x-ts",
347 .owner = THIS_MODULE,
349 .probe = tps6507x_ts_probe,
350 .remove = tps6507x_ts_remove,
352 module_platform_driver(tps6507x_ts_driver);
354 MODULE_AUTHOR("Todd Fischer <todd.fischer@ridgerun.com>");
355 MODULE_DESCRIPTION("TPS6507x - TouchScreen driver");
356 MODULE_LICENSE("GPL v2");
357 MODULE_ALIAS("platform:tps6507x-ts");