Loading...
1/*
2 * drivers/input/touchscreen/tps6507x_ts.c
3 *
4 * Touchscreen driver for the tps6507x chip.
5 *
6 * Copyright (c) 2009 RidgeRun (todd.fischer@ridgerun.com)
7 *
8 * Credits:
9 *
10 * Using code from tsc2007, MtekVision Co., Ltd.
11 *
12 * For licencing details see kernel-base/COPYING
13 *
14 * TPS65070, TPS65073, TPS650731, and TPS650732 support
15 * 10 bit touch screen interface.
16 */
17
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>
26
27#define TSC_DEFAULT_POLL_PERIOD 30 /* ms */
28#define TPS_DEFAULT_MIN_PRESSURE 0x30
29#define MAX_10BIT ((1 << 10) - 1)
30
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)
35
36struct ts_event {
37 u16 x;
38 u16 y;
39 u16 pressure;
40};
41
42struct tps6507x_ts {
43 struct input_dev *input_dev;
44 struct device *dev;
45 char phys[32];
46 struct delayed_work work;
47 unsigned polling; /* polling is active */
48 struct ts_event tc;
49 struct tps6507x_dev *mfd;
50 u16 model;
51 unsigned pendown;
52 int irq;
53 void (*clear_penirq)(void);
54 unsigned long poll_period; /* ms */
55 u16 min_pressure;
56 int vref; /* non-zero to leave vref on */
57};
58
59static int tps6507x_read_u8(struct tps6507x_ts *tsc, u8 reg, u8 *data)
60{
61 int err;
62
63 err = tsc->mfd->read_dev(tsc->mfd, reg, 1, data);
64
65 if (err)
66 return err;
67
68 return 0;
69}
70
71static int tps6507x_write_u8(struct tps6507x_ts *tsc, u8 reg, u8 data)
72{
73 return tsc->mfd->write_dev(tsc->mfd, reg, 1, &data);
74}
75
76static s32 tps6507x_adc_conversion(struct tps6507x_ts *tsc,
77 u8 tsc_mode, u16 *value)
78{
79 s32 ret;
80 u8 adc_status;
81 u8 result;
82
83 /* Route input signal to A/D converter */
84
85 ret = tps6507x_write_u8(tsc, TPS6507X_REG_TSCMODE, tsc_mode);
86 if (ret) {
87 dev_err(tsc->dev, "TSC mode read failed\n");
88 goto err;
89 }
90
91 /* Start A/D conversion */
92
93 ret = tps6507x_write_u8(tsc, TPS6507X_REG_ADCONFIG,
94 TPS6507X_ADCONFIG_CONVERT_TS);
95 if (ret) {
96 dev_err(tsc->dev, "ADC config write failed\n");
97 return ret;
98 }
99
100 do {
101 ret = tps6507x_read_u8(tsc, TPS6507X_REG_ADCONFIG,
102 &adc_status);
103 if (ret) {
104 dev_err(tsc->dev, "ADC config read failed\n");
105 goto err;
106 }
107 } while (adc_status & TPS6507X_ADCONFIG_START_CONVERSION);
108
109 ret = tps6507x_read_u8(tsc, TPS6507X_REG_ADRESULT_2, &result);
110 if (ret) {
111 dev_err(tsc->dev, "ADC result 2 read failed\n");
112 goto err;
113 }
114
115 *value = (result & TPS6507X_REG_ADRESULT_2_MASK) << 8;
116
117 ret = tps6507x_read_u8(tsc, TPS6507X_REG_ADRESULT_1, &result);
118 if (ret) {
119 dev_err(tsc->dev, "ADC result 1 read failed\n");
120 goto err;
121 }
122
123 *value |= result;
124
125 dev_dbg(tsc->dev, "TSC channel %d = 0x%X\n", tsc_mode, *value);
126
127err:
128 return ret;
129}
130
131/* Need to call tps6507x_adc_standby() after using A/D converter for the
132 * touch screen interrupt to work properly.
133 */
134
135static s32 tps6507x_adc_standby(struct tps6507x_ts *tsc)
136{
137 s32 ret;
138 s32 loops = 0;
139 u8 val;
140
141 ret = tps6507x_write_u8(tsc, TPS6507X_REG_ADCONFIG,
142 TPS6507X_ADCONFIG_INPUT_TSC);
143 if (ret)
144 return ret;
145
146 ret = tps6507x_write_u8(tsc, TPS6507X_REG_TSCMODE,
147 TPS6507X_TSCMODE_STANDBY);
148 if (ret)
149 return ret;
150
151 ret = tps6507x_read_u8(tsc, TPS6507X_REG_INT, &val);
152 if (ret)
153 return ret;
154
155 while (val & TPS6507X_REG_TSC_INT) {
156 mdelay(10);
157 ret = tps6507x_read_u8(tsc, TPS6507X_REG_INT, &val);
158 if (ret)
159 return ret;
160 loops++;
161 }
162
163 return ret;
164}
165
166static void tps6507x_ts_handler(struct work_struct *work)
167{
168 struct tps6507x_ts *tsc = container_of(work,
169 struct tps6507x_ts, work.work);
170 struct input_dev *input_dev = tsc->input_dev;
171 int pendown;
172 int schd;
173 int poll = 0;
174 s32 ret;
175
176 ret = tps6507x_adc_conversion(tsc, TPS6507X_TSCMODE_PRESSURE,
177 &tsc->tc.pressure);
178 if (ret)
179 goto done;
180
181 pendown = tsc->tc.pressure > tsc->min_pressure;
182
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);
188 tsc->pendown = 0;
189 }
190
191 if (pendown) {
192
193 if (!tsc->pendown) {
194 dev_dbg(tsc->dev, "DOWN\n");
195 input_report_key(input_dev, BTN_TOUCH, 1);
196 } else
197 dev_dbg(tsc->dev, "still down\n");
198
199 ret = tps6507x_adc_conversion(tsc, TPS6507X_TSCMODE_X_POSITION,
200 &tsc->tc.x);
201 if (ret)
202 goto done;
203
204 ret = tps6507x_adc_conversion(tsc, TPS6507X_TSCMODE_Y_POSITION,
205 &tsc->tc.y);
206 if (ret)
207 goto done;
208
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);
213 tsc->pendown = 1;
214 poll = 1;
215 }
216
217done:
218 /* always poll if not using interrupts */
219 poll = 1;
220
221 if (poll) {
222 schd = schedule_delayed_work(&tsc->work,
223 msecs_to_jiffies(tsc->poll_period));
224 if (schd)
225 tsc->polling = 1;
226 else {
227 tsc->polling = 0;
228 dev_err(tsc->dev, "re-schedule failed");
229 }
230 } else
231 tsc->polling = 0;
232
233 ret = tps6507x_adc_standby(tsc);
234}
235
236static int tps6507x_ts_probe(struct platform_device *pdev)
237{
238 int error;
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;
244 int schd;
245
246 /**
247 * tps_board points to pmic related constants
248 * coming from the board-evm file.
249 */
250
251 tps_board = (struct tps6507x_board *)tps6507x_dev->dev->platform_data;
252
253 if (!tps_board) {
254 dev_err(tps6507x_dev->dev,
255 "Could not find tps6507x platform data\n");
256 return -EIO;
257 }
258
259 /**
260 * init_data points to array of regulator_init structures
261 * coming from the board-evm file.
262 */
263
264 init_data = tps_board->tps6507x_ts_init_data;
265
266 tsc = kzalloc(sizeof(struct tps6507x_ts), GFP_KERNEL);
267 if (!tsc) {
268 dev_err(tps6507x_dev->dev, "failed to allocate driver data\n");
269 error = -ENOMEM;
270 goto err0;
271 }
272
273 tps6507x_dev->ts = tsc;
274 tsc->mfd = tps6507x_dev;
275 tsc->dev = tps6507x_dev->dev;
276 input_dev = input_allocate_device();
277 if (!input_dev) {
278 dev_err(tsc->dev, "Failed to allocate input device.\n");
279 error = -ENOMEM;
280 goto err1;
281 }
282
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);
285
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);
289
290 input_dev->name = "TPS6507x Touchscreen";
291 input_dev->id.bustype = BUS_I2C;
292 input_dev->dev.parent = tsc->dev;
293
294 snprintf(tsc->phys, sizeof(tsc->phys),
295 "%s/input0", dev_name(tsc->dev));
296 input_dev->phys = tsc->phys;
297
298 dev_dbg(tsc->dev, "device: %s\n", input_dev->phys);
299
300 input_set_drvdata(input_dev, tsc);
301
302 tsc->input_dev = input_dev;
303
304 INIT_DELAYED_WORK(&tsc->work, tps6507x_ts_handler);
305
306 if (init_data) {
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;
313 } else {
314 tsc->poll_period = TSC_DEFAULT_POLL_PERIOD;
315 tsc->min_pressure = TPS_DEFAULT_MIN_PRESSURE;
316 }
317
318 error = tps6507x_adc_standby(tsc);
319 if (error)
320 goto err2;
321
322 error = input_register_device(input_dev);
323 if (error)
324 goto err2;
325
326 schd = schedule_delayed_work(&tsc->work,
327 msecs_to_jiffies(tsc->poll_period));
328
329 if (schd)
330 tsc->polling = 1;
331 else {
332 tsc->polling = 0;
333 dev_err(tsc->dev, "schedule failed");
334 goto err2;
335 }
336 platform_set_drvdata(pdev, tps6507x_dev);
337
338 return 0;
339
340err2:
341 cancel_delayed_work_sync(&tsc->work);
342 input_free_device(input_dev);
343err1:
344 kfree(tsc);
345 tps6507x_dev->ts = NULL;
346err0:
347 return error;
348}
349
350static int __devexit tps6507x_ts_remove(struct platform_device *pdev)
351{
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;
355
356 cancel_delayed_work_sync(&tsc->work);
357
358 input_unregister_device(input_dev);
359
360 tps6507x_dev->ts = NULL;
361 kfree(tsc);
362
363 return 0;
364}
365
366static struct platform_driver tps6507x_ts_driver = {
367 .driver = {
368 .name = "tps6507x-ts",
369 .owner = THIS_MODULE,
370 },
371 .probe = tps6507x_ts_probe,
372 .remove = __devexit_p(tps6507x_ts_remove),
373};
374
375static int __init tps6507x_ts_init(void)
376{
377 return platform_driver_register(&tps6507x_ts_driver);
378}
379module_init(tps6507x_ts_init);
380
381static void __exit tps6507x_ts_exit(void)
382{
383 platform_driver_unregister(&tps6507x_ts_driver);
384}
385module_exit(tps6507x_ts_exit);
386
387MODULE_AUTHOR("Todd Fischer <todd.fischer@ridgerun.com>");
388MODULE_DESCRIPTION("TPS6507x - TouchScreen driver");
389MODULE_LICENSE("GPL v2");
390MODULE_ALIAS("platform:tps6507x-tsc");
1/*
2 * Touchscreen driver for the tps6507x chip.
3 *
4 * Copyright (c) 2009 RidgeRun (todd.fischer@ridgerun.com)
5 *
6 * Credits:
7 *
8 * Using code from tsc2007, MtekVision Co., Ltd.
9 *
10 * For licencing details see kernel-base/COPYING
11 *
12 * TPS65070, TPS65073, TPS650731, and TPS650732 support
13 * 10 bit touch screen interface.
14 */
15
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>
24
25#define TSC_DEFAULT_POLL_PERIOD 30 /* ms */
26#define TPS_DEFAULT_MIN_PRESSURE 0x30
27#define MAX_10BIT ((1 << 10) - 1)
28
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)
33
34struct ts_event {
35 u16 x;
36 u16 y;
37 u16 pressure;
38};
39
40struct tps6507x_ts {
41 struct device *dev;
42 struct input_dev *input;
43 struct tps6507x_dev *mfd;
44 char phys[32];
45 struct ts_event tc;
46 u16 min_pressure;
47 bool pendown;
48};
49
50static int tps6507x_read_u8(struct tps6507x_ts *tsc, u8 reg, u8 *data)
51{
52 return tsc->mfd->read_dev(tsc->mfd, reg, 1, data);
53}
54
55static int tps6507x_write_u8(struct tps6507x_ts *tsc, u8 reg, u8 data)
56{
57 return tsc->mfd->write_dev(tsc->mfd, reg, 1, &data);
58}
59
60static s32 tps6507x_adc_conversion(struct tps6507x_ts *tsc,
61 u8 tsc_mode, u16 *value)
62{
63 s32 ret;
64 u8 adc_status;
65 u8 result;
66
67 /* Route input signal to A/D converter */
68
69 ret = tps6507x_write_u8(tsc, TPS6507X_REG_TSCMODE, tsc_mode);
70 if (ret) {
71 dev_err(tsc->dev, "TSC mode read failed\n");
72 goto err;
73 }
74
75 /* Start A/D conversion */
76
77 ret = tps6507x_write_u8(tsc, TPS6507X_REG_ADCONFIG,
78 TPS6507X_ADCONFIG_CONVERT_TS);
79 if (ret) {
80 dev_err(tsc->dev, "ADC config write failed\n");
81 return ret;
82 }
83
84 do {
85 ret = tps6507x_read_u8(tsc, TPS6507X_REG_ADCONFIG,
86 &adc_status);
87 if (ret) {
88 dev_err(tsc->dev, "ADC config read failed\n");
89 goto err;
90 }
91 } while (adc_status & TPS6507X_ADCONFIG_START_CONVERSION);
92
93 ret = tps6507x_read_u8(tsc, TPS6507X_REG_ADRESULT_2, &result);
94 if (ret) {
95 dev_err(tsc->dev, "ADC result 2 read failed\n");
96 goto err;
97 }
98
99 *value = (result & TPS6507X_REG_ADRESULT_2_MASK) << 8;
100
101 ret = tps6507x_read_u8(tsc, TPS6507X_REG_ADRESULT_1, &result);
102 if (ret) {
103 dev_err(tsc->dev, "ADC result 1 read failed\n");
104 goto err;
105 }
106
107 *value |= result;
108
109 dev_dbg(tsc->dev, "TSC channel %d = 0x%X\n", tsc_mode, *value);
110
111err:
112 return ret;
113}
114
115/* Need to call tps6507x_adc_standby() after using A/D converter for the
116 * touch screen interrupt to work properly.
117 */
118
119static s32 tps6507x_adc_standby(struct tps6507x_ts *tsc)
120{
121 s32 ret;
122 u8 val;
123
124 ret = tps6507x_write_u8(tsc, TPS6507X_REG_ADCONFIG,
125 TPS6507X_ADCONFIG_INPUT_TSC);
126 if (ret)
127 return ret;
128
129 ret = tps6507x_write_u8(tsc, TPS6507X_REG_TSCMODE,
130 TPS6507X_TSCMODE_STANDBY);
131 if (ret)
132 return ret;
133
134 ret = tps6507x_read_u8(tsc, TPS6507X_REG_INT, &val);
135 if (ret)
136 return ret;
137
138 while (val & TPS6507X_REG_TSC_INT) {
139 mdelay(10);
140 ret = tps6507x_read_u8(tsc, TPS6507X_REG_INT, &val);
141 if (ret)
142 return ret;
143 }
144
145 return ret;
146}
147
148static void tps6507x_ts_poll(struct input_dev *input_dev)
149{
150 struct tps6507x_ts *tsc = input_get_drvdata(input_dev);
151 bool pendown;
152 s32 ret;
153
154 ret = tps6507x_adc_conversion(tsc, TPS6507X_TSCMODE_PRESSURE,
155 &tsc->tc.pressure);
156 if (ret)
157 goto done;
158
159 pendown = tsc->tc.pressure > tsc->min_pressure;
160
161 if (unlikely(!pendown && tsc->pendown)) {
162 dev_dbg(tsc->dev, "UP\n");
163 input_report_key(input_dev, BTN_TOUCH, 0);
164 input_report_abs(input_dev, ABS_PRESSURE, 0);
165 input_sync(input_dev);
166 tsc->pendown = false;
167 }
168
169 if (pendown) {
170
171 if (!tsc->pendown) {
172 dev_dbg(tsc->dev, "DOWN\n");
173 input_report_key(input_dev, BTN_TOUCH, 1);
174 } else
175 dev_dbg(tsc->dev, "still down\n");
176
177 ret = tps6507x_adc_conversion(tsc, TPS6507X_TSCMODE_X_POSITION,
178 &tsc->tc.x);
179 if (ret)
180 goto done;
181
182 ret = tps6507x_adc_conversion(tsc, TPS6507X_TSCMODE_Y_POSITION,
183 &tsc->tc.y);
184 if (ret)
185 goto done;
186
187 input_report_abs(input_dev, ABS_X, tsc->tc.x);
188 input_report_abs(input_dev, ABS_Y, tsc->tc.y);
189 input_report_abs(input_dev, ABS_PRESSURE, tsc->tc.pressure);
190 input_sync(input_dev);
191 tsc->pendown = true;
192 }
193
194done:
195 tps6507x_adc_standby(tsc);
196}
197
198static int tps6507x_ts_probe(struct platform_device *pdev)
199{
200 struct tps6507x_dev *tps6507x_dev = dev_get_drvdata(pdev->dev.parent);
201 const struct tps6507x_board *tps_board;
202 const struct touchscreen_init_data *init_data;
203 struct tps6507x_ts *tsc;
204 struct input_dev *input_dev;
205 int error;
206
207 /*
208 * tps_board points to pmic related constants
209 * coming from the board-evm file.
210 */
211 tps_board = dev_get_platdata(tps6507x_dev->dev);
212 if (!tps_board) {
213 dev_err(tps6507x_dev->dev,
214 "Could not find tps6507x platform data\n");
215 return -ENODEV;
216 }
217
218 /*
219 * init_data points to array of regulator_init structures
220 * coming from the board-evm file.
221 */
222 init_data = tps_board->tps6507x_ts_init_data;
223
224 tsc = devm_kzalloc(&pdev->dev, sizeof(struct tps6507x_ts), GFP_KERNEL);
225 if (!tsc) {
226 dev_err(tps6507x_dev->dev, "failed to allocate driver data\n");
227 return -ENOMEM;
228 }
229
230 tsc->mfd = tps6507x_dev;
231 tsc->dev = tps6507x_dev->dev;
232 tsc->min_pressure = init_data ?
233 init_data->min_pressure : TPS_DEFAULT_MIN_PRESSURE;
234
235 snprintf(tsc->phys, sizeof(tsc->phys),
236 "%s/input0", dev_name(tsc->dev));
237
238 input_dev = devm_input_allocate_device(&pdev->dev);
239 if (!input_dev) {
240 dev_err(tsc->dev, "Failed to allocate polled input device.\n");
241 return -ENOMEM;
242 }
243
244 tsc->input = input_dev;
245 input_set_drvdata(input_dev, tsc);
246
247 input_set_capability(input_dev, EV_KEY, BTN_TOUCH);
248 input_set_abs_params(input_dev, ABS_X, 0, MAX_10BIT, 0, 0);
249 input_set_abs_params(input_dev, ABS_Y, 0, MAX_10BIT, 0, 0);
250 input_set_abs_params(input_dev, ABS_PRESSURE, 0, MAX_10BIT, 0, 0);
251
252 input_dev->name = "TPS6507x Touchscreen";
253 input_dev->phys = tsc->phys;
254 input_dev->dev.parent = tsc->dev;
255 input_dev->id.bustype = BUS_I2C;
256 if (init_data) {
257 input_dev->id.vendor = init_data->vendor;
258 input_dev->id.product = init_data->product;
259 input_dev->id.version = init_data->version;
260 }
261
262 error = tps6507x_adc_standby(tsc);
263 if (error)
264 return error;
265
266 error = input_setup_polling(input_dev, tps6507x_ts_poll);
267 if (error)
268 return error;
269
270 input_set_poll_interval(input_dev,
271 init_data ? init_data->poll_period :
272 TSC_DEFAULT_POLL_PERIOD);
273
274 error = input_register_device(input_dev);
275 if (error)
276 return error;
277
278 return 0;
279}
280
281static struct platform_driver tps6507x_ts_driver = {
282 .driver = {
283 .name = "tps6507x-ts",
284 },
285 .probe = tps6507x_ts_probe,
286};
287module_platform_driver(tps6507x_ts_driver);
288
289MODULE_AUTHOR("Todd Fischer <todd.fischer@ridgerun.com>");
290MODULE_DESCRIPTION("TPS6507x - TouchScreen driver");
291MODULE_LICENSE("GPL v2");
292MODULE_ALIAS("platform:tps6507x-ts");