blob: 2a78e27b4495d6fa121010d56927d07fd85ab046 [file] [log] [blame]
Viresh Kumar2bd942f2012-11-09 23:56:59 -08001/*
2 * STMicroelectronics STMPE811 Touchscreen Driver
Luotao Fuf94add32010-07-02 14:10:29 +02003 *
4 * (C) 2010 Luotao Fu <l.fu@pengutronix.de>
5 * All rights reserved.
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
11 *
12 */
13
14#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/sched.h>
17#include <linux/interrupt.h>
Luotao Fuf94add32010-07-02 14:10:29 +020018#include <linux/device.h>
Vipul Kumar Samar037db522012-11-22 23:42:12 -080019#include <linux/of.h>
Luotao Fuf94add32010-07-02 14:10:29 +020020#include <linux/platform_device.h>
21#include <linux/input.h>
22#include <linux/slab.h>
23#include <linux/delay.h>
24#include <linux/i2c.h>
25#include <linux/workqueue.h>
26
27#include <linux/mfd/stmpe.h>
28
29/* Register layouts and functionalities are identical on all stmpexxx variants
30 * with touchscreen controller
31 */
32#define STMPE_REG_INT_STA 0x0B
33#define STMPE_REG_ADC_CTRL1 0x20
34#define STMPE_REG_ADC_CTRL2 0x21
35#define STMPE_REG_TSC_CTRL 0x40
36#define STMPE_REG_TSC_CFG 0x41
37#define STMPE_REG_FIFO_TH 0x4A
38#define STMPE_REG_FIFO_STA 0x4B
39#define STMPE_REG_FIFO_SIZE 0x4C
40#define STMPE_REG_TSC_DATA_XYZ 0x52
41#define STMPE_REG_TSC_FRACTION_Z 0x56
42#define STMPE_REG_TSC_I_DRIVE 0x58
43
44#define OP_MOD_XYZ 0
45
46#define STMPE_TSC_CTRL_TSC_EN (1<<0)
47
48#define STMPE_FIFO_STA_RESET (1<<0)
49
50#define STMPE_IRQ_TOUCH_DET 0
51
52#define SAMPLE_TIME(x) ((x & 0xf) << 4)
53#define MOD_12B(x) ((x & 0x1) << 3)
54#define REF_SEL(x) ((x & 0x1) << 1)
55#define ADC_FREQ(x) (x & 0x3)
56#define AVE_CTRL(x) ((x & 0x3) << 6)
57#define DET_DELAY(x) ((x & 0x7) << 3)
58#define SETTLING(x) (x & 0x7)
59#define FRACTION_Z(x) (x & 0x7)
60#define I_DRIVE(x) (x & 0x1)
61#define OP_MODE(x) ((x & 0x7) << 1)
62
63#define STMPE_TS_NAME "stmpe-ts"
64#define XY_MASK 0xfff
65
Linus Walleij564a68d2016-02-22 11:37:49 -080066/**
67 * struct stmpe_touch - stmpe811 touch screen controller state
68 * @stmpe: pointer back to STMPE MFD container
69 * @idev: registered input device
70 * @work: a work item used to scan the device
71 * @dev: a pointer back to the MFD cell struct device*
72 * @sample_time: ADC converstion time in number of clock.
73 * (0 -> 36 clocks, 1 -> 44 clocks, 2 -> 56 clocks, 3 -> 64 clocks,
74 * 4 -> 80 clocks, 5 -> 96 clocks, 6 -> 144 clocks),
75 * recommended is 4.
76 * @mod_12b: ADC Bit mode (0 -> 10bit ADC, 1 -> 12bit ADC)
77 * @ref_sel: ADC reference source
78 * (0 -> internal reference, 1 -> external reference)
79 * @adc_freq: ADC Clock speed
80 * (0 -> 1.625 MHz, 1 -> 3.25 MHz, 2 || 3 -> 6.5 MHz)
81 * @ave_ctrl: Sample average control
82 * (0 -> 1 sample, 1 -> 2 samples, 2 -> 4 samples, 3 -> 8 samples)
83 * @touch_det_delay: Touch detect interrupt delay
84 * (0 -> 10 us, 1 -> 50 us, 2 -> 100 us, 3 -> 500 us,
85 * 4-> 1 ms, 5 -> 5 ms, 6 -> 10 ms, 7 -> 50 ms)
86 * recommended is 3
87 * @settling: Panel driver settling time
88 * (0 -> 10 us, 1 -> 100 us, 2 -> 500 us, 3 -> 1 ms,
89 * 4 -> 5 ms, 5 -> 10 ms, 6 for 50 ms, 7 -> 100 ms)
90 * recommended is 2
91 * @fraction_z: Length of the fractional part in z
92 * (fraction_z ([0..7]) = Count of the fractional part)
93 * recommended is 7
94 * @i_drive: current limit value of the touchscreen drivers
95 * (0 -> 20 mA typical 35 mA max, 1 -> 50 mA typical 80 mA max)
96 */
Luotao Fuf94add32010-07-02 14:10:29 +020097struct stmpe_touch {
98 struct stmpe *stmpe;
99 struct input_dev *idev;
100 struct delayed_work work;
101 struct device *dev;
102 u8 sample_time;
103 u8 mod_12b;
104 u8 ref_sel;
105 u8 adc_freq;
106 u8 ave_ctrl;
107 u8 touch_det_delay;
108 u8 settling;
109 u8 fraction_z;
110 u8 i_drive;
111};
112
113static int __stmpe_reset_fifo(struct stmpe *stmpe)
114{
115 int ret;
116
117 ret = stmpe_set_bits(stmpe, STMPE_REG_FIFO_STA,
118 STMPE_FIFO_STA_RESET, STMPE_FIFO_STA_RESET);
119 if (ret)
120 return ret;
121
122 return stmpe_set_bits(stmpe, STMPE_REG_FIFO_STA,
123 STMPE_FIFO_STA_RESET, 0);
124}
125
126static void stmpe_work(struct work_struct *work)
127{
128 int int_sta;
129 u32 timeout = 40;
130
131 struct stmpe_touch *ts =
132 container_of(work, struct stmpe_touch, work.work);
133
134 int_sta = stmpe_reg_read(ts->stmpe, STMPE_REG_INT_STA);
135
136 /*
137 * touch_det sometimes get desasserted or just get stuck. This appears
138 * to be a silicon bug, We still have to clearify this with the
139 * manufacture. As a workaround We release the key anyway if the
140 * touch_det keeps coming in after 4ms, while the FIFO contains no value
141 * during the whole time.
142 */
143 while ((int_sta & (1 << STMPE_IRQ_TOUCH_DET)) && (timeout > 0)) {
144 timeout--;
145 int_sta = stmpe_reg_read(ts->stmpe, STMPE_REG_INT_STA);
146 udelay(100);
147 }
148
149 /* reset the FIFO before we report release event */
150 __stmpe_reset_fifo(ts->stmpe);
151
152 input_report_abs(ts->idev, ABS_PRESSURE, 0);
Vipul Kumar Samar7488b1b2012-12-16 23:10:56 -0800153 input_report_key(ts->idev, BTN_TOUCH, 0);
Luotao Fuf94add32010-07-02 14:10:29 +0200154 input_sync(ts->idev);
155}
156
157static irqreturn_t stmpe_ts_handler(int irq, void *data)
158{
159 u8 data_set[4];
160 int x, y, z;
161 struct stmpe_touch *ts = data;
162
163 /*
164 * Cancel scheduled polling for release if we have new value
165 * available. Wait if the polling is already running.
166 */
167 cancel_delayed_work_sync(&ts->work);
168
169 /*
170 * The FIFO sometimes just crashes and stops generating interrupts. This
171 * appears to be a silicon bug. We still have to clearify this with
172 * the manufacture. As a workaround we disable the TSC while we are
173 * collecting data and flush the FIFO after reading
174 */
175 stmpe_set_bits(ts->stmpe, STMPE_REG_TSC_CTRL,
176 STMPE_TSC_CTRL_TSC_EN, 0);
177
178 stmpe_block_read(ts->stmpe, STMPE_REG_TSC_DATA_XYZ, 4, data_set);
179
180 x = (data_set[0] << 4) | (data_set[1] >> 4);
181 y = ((data_set[1] & 0xf) << 8) | data_set[2];
182 z = data_set[3];
183
184 input_report_abs(ts->idev, ABS_X, x);
185 input_report_abs(ts->idev, ABS_Y, y);
186 input_report_abs(ts->idev, ABS_PRESSURE, z);
Vipul Kumar Samar7488b1b2012-12-16 23:10:56 -0800187 input_report_key(ts->idev, BTN_TOUCH, 1);
Luotao Fuf94add32010-07-02 14:10:29 +0200188 input_sync(ts->idev);
189
190 /* flush the FIFO after we have read out our values. */
191 __stmpe_reset_fifo(ts->stmpe);
192
193 /* reenable the tsc */
194 stmpe_set_bits(ts->stmpe, STMPE_REG_TSC_CTRL,
195 STMPE_TSC_CTRL_TSC_EN, STMPE_TSC_CTRL_TSC_EN);
196
197 /* start polling for touch_det to detect release */
Marek Vasut9ceeb592015-05-07 22:25:49 -0700198 schedule_delayed_work(&ts->work, msecs_to_jiffies(50));
Luotao Fuf94add32010-07-02 14:10:29 +0200199
200 return IRQ_HANDLED;
201}
202
Bill Pemberton5298cc42012-11-23 21:38:25 -0800203static int stmpe_init_hw(struct stmpe_touch *ts)
Luotao Fuf94add32010-07-02 14:10:29 +0200204{
205 int ret;
206 u8 adc_ctrl1, adc_ctrl1_mask, tsc_cfg, tsc_cfg_mask;
207 struct stmpe *stmpe = ts->stmpe;
208 struct device *dev = ts->dev;
209
210 ret = stmpe_enable(stmpe, STMPE_BLOCK_TOUCHSCREEN | STMPE_BLOCK_ADC);
211 if (ret) {
212 dev_err(dev, "Could not enable clock for ADC and TS\n");
213 return ret;
214 }
215
216 adc_ctrl1 = SAMPLE_TIME(ts->sample_time) | MOD_12B(ts->mod_12b) |
217 REF_SEL(ts->ref_sel);
218 adc_ctrl1_mask = SAMPLE_TIME(0xff) | MOD_12B(0xff) | REF_SEL(0xff);
219
220 ret = stmpe_set_bits(stmpe, STMPE_REG_ADC_CTRL1,
221 adc_ctrl1_mask, adc_ctrl1);
222 if (ret) {
223 dev_err(dev, "Could not setup ADC\n");
224 return ret;
225 }
226
227 ret = stmpe_set_bits(stmpe, STMPE_REG_ADC_CTRL2,
228 ADC_FREQ(0xff), ADC_FREQ(ts->adc_freq));
229 if (ret) {
230 dev_err(dev, "Could not setup ADC\n");
231 return ret;
232 }
233
234 tsc_cfg = AVE_CTRL(ts->ave_ctrl) | DET_DELAY(ts->touch_det_delay) |
235 SETTLING(ts->settling);
236 tsc_cfg_mask = AVE_CTRL(0xff) | DET_DELAY(0xff) | SETTLING(0xff);
237
238 ret = stmpe_set_bits(stmpe, STMPE_REG_TSC_CFG, tsc_cfg_mask, tsc_cfg);
239 if (ret) {
240 dev_err(dev, "Could not config touch\n");
241 return ret;
242 }
243
244 ret = stmpe_set_bits(stmpe, STMPE_REG_TSC_FRACTION_Z,
245 FRACTION_Z(0xff), FRACTION_Z(ts->fraction_z));
246 if (ret) {
247 dev_err(dev, "Could not config touch\n");
248 return ret;
249 }
250
251 ret = stmpe_set_bits(stmpe, STMPE_REG_TSC_I_DRIVE,
252 I_DRIVE(0xff), I_DRIVE(ts->i_drive));
253 if (ret) {
254 dev_err(dev, "Could not config touch\n");
255 return ret;
256 }
257
258 /* set FIFO to 1 for single point reading */
259 ret = stmpe_reg_write(stmpe, STMPE_REG_FIFO_TH, 1);
260 if (ret) {
261 dev_err(dev, "Could not set FIFO\n");
262 return ret;
263 }
264
265 ret = stmpe_set_bits(stmpe, STMPE_REG_TSC_CTRL,
266 OP_MODE(0xff), OP_MODE(OP_MOD_XYZ));
267 if (ret) {
268 dev_err(dev, "Could not set mode\n");
269 return ret;
270 }
271
272 return 0;
273}
274
275static int stmpe_ts_open(struct input_dev *dev)
276{
277 struct stmpe_touch *ts = input_get_drvdata(dev);
278 int ret = 0;
279
280 ret = __stmpe_reset_fifo(ts->stmpe);
281 if (ret)
282 return ret;
283
284 return stmpe_set_bits(ts->stmpe, STMPE_REG_TSC_CTRL,
285 STMPE_TSC_CTRL_TSC_EN, STMPE_TSC_CTRL_TSC_EN);
286}
287
288static void stmpe_ts_close(struct input_dev *dev)
289{
290 struct stmpe_touch *ts = input_get_drvdata(dev);
291
292 cancel_delayed_work_sync(&ts->work);
293
294 stmpe_set_bits(ts->stmpe, STMPE_REG_TSC_CTRL,
295 STMPE_TSC_CTRL_TSC_EN, 0);
296}
297
Vipul Kumar Samar037db522012-11-22 23:42:12 -0800298static void stmpe_ts_get_platform_info(struct platform_device *pdev,
299 struct stmpe_touch *ts)
Luotao Fuf94add32010-07-02 14:10:29 +0200300{
Vipul Kumar Samar037db522012-11-22 23:42:12 -0800301 struct device_node *np = pdev->dev.of_node;
Dmitry Torokhove4b88e12015-05-22 13:44:33 -0700302 u32 val;
Vipul Kumar Samar037db522012-11-22 23:42:12 -0800303
Dmitry Torokhove4b88e12015-05-22 13:44:33 -0700304 if (np) {
Vipul Kumar Samar037db522012-11-22 23:42:12 -0800305 if (!of_property_read_u32(np, "st,sample-time", &val))
306 ts->sample_time = val;
307 if (!of_property_read_u32(np, "st,mod-12b", &val))
308 ts->mod_12b = val;
309 if (!of_property_read_u32(np, "st,ref-sel", &val))
310 ts->ref_sel = val;
311 if (!of_property_read_u32(np, "st,adc-freq", &val))
312 ts->adc_freq = val;
313 if (!of_property_read_u32(np, "st,ave-ctrl", &val))
314 ts->ave_ctrl = val;
315 if (!of_property_read_u32(np, "st,touch-det-delay", &val))
316 ts->touch_det_delay = val;
317 if (!of_property_read_u32(np, "st,settling", &val))
318 ts->settling = val;
319 if (!of_property_read_u32(np, "st,fraction-z", &val))
320 ts->fraction_z = val;
321 if (!of_property_read_u32(np, "st,i-drive", &val))
322 ts->i_drive = val;
323 }
324}
325
Bill Pemberton5298cc42012-11-23 21:38:25 -0800326static int stmpe_input_probe(struct platform_device *pdev)
Vipul Kumar Samar037db522012-11-22 23:42:12 -0800327{
Dmitry Torokhove4b88e12015-05-22 13:44:33 -0700328 struct stmpe *stmpe = dev_get_drvdata(pdev->dev.parent);
Luotao Fuf94add32010-07-02 14:10:29 +0200329 struct stmpe_touch *ts;
330 struct input_dev *idev;
Viresh Kumar2bd942f2012-11-09 23:56:59 -0800331 int error;
Kulikov Vasiliy3faeb352010-07-15 22:43:54 +0400332 int ts_irq;
Luotao Fuf94add32010-07-02 14:10:29 +0200333
334 ts_irq = platform_get_irq_byname(pdev, "FIFO_TH");
335 if (ts_irq < 0)
336 return ts_irq;
337
Viresh Kumar2bd942f2012-11-09 23:56:59 -0800338 ts = devm_kzalloc(&pdev->dev, sizeof(*ts), GFP_KERNEL);
339 if (!ts)
340 return -ENOMEM;
Luotao Fuf94add32010-07-02 14:10:29 +0200341
Viresh Kumar2bd942f2012-11-09 23:56:59 -0800342 idev = devm_input_allocate_device(&pdev->dev);
343 if (!idev)
344 return -ENOMEM;
Luotao Fuf94add32010-07-02 14:10:29 +0200345
346 platform_set_drvdata(pdev, ts);
Dmitry Torokhove4b88e12015-05-22 13:44:33 -0700347 ts->stmpe = stmpe;
Luotao Fuf94add32010-07-02 14:10:29 +0200348 ts->idev = idev;
349 ts->dev = &pdev->dev;
350
Vipul Kumar Samar037db522012-11-22 23:42:12 -0800351 stmpe_ts_get_platform_info(pdev, ts);
Luotao Fuf94add32010-07-02 14:10:29 +0200352
353 INIT_DELAYED_WORK(&ts->work, stmpe_work);
354
Viresh Kumar2bd942f2012-11-09 23:56:59 -0800355 error = devm_request_threaded_irq(&pdev->dev, ts_irq,
356 NULL, stmpe_ts_handler,
357 IRQF_ONESHOT, STMPE_TS_NAME, ts);
358 if (error) {
Luotao Fuf94add32010-07-02 14:10:29 +0200359 dev_err(&pdev->dev, "Failed to request IRQ %d\n", ts_irq);
Viresh Kumar2bd942f2012-11-09 23:56:59 -0800360 return error;
Luotao Fuf94add32010-07-02 14:10:29 +0200361 }
362
Viresh Kumar2bd942f2012-11-09 23:56:59 -0800363 error = stmpe_init_hw(ts);
364 if (error)
365 return error;
Luotao Fuf94add32010-07-02 14:10:29 +0200366
367 idev->name = STMPE_TS_NAME;
Vipul Kumar Samarb8d52e22012-11-10 00:08:20 -0800368 idev->phys = STMPE_TS_NAME"/input0";
Luotao Fuf94add32010-07-02 14:10:29 +0200369 idev->id.bustype = BUS_I2C;
Luotao Fuf94add32010-07-02 14:10:29 +0200370
371 idev->open = stmpe_ts_open;
372 idev->close = stmpe_ts_close;
373
374 input_set_drvdata(idev, ts);
375
Dmitry Torokhove4b88e12015-05-22 13:44:33 -0700376 input_set_capability(idev, EV_KEY, BTN_TOUCH);
Luotao Fuf94add32010-07-02 14:10:29 +0200377 input_set_abs_params(idev, ABS_X, 0, XY_MASK, 0, 0);
378 input_set_abs_params(idev, ABS_Y, 0, XY_MASK, 0, 0);
379 input_set_abs_params(idev, ABS_PRESSURE, 0x0, 0xff, 0, 0);
380
Viresh Kumar2bd942f2012-11-09 23:56:59 -0800381 error = input_register_device(idev);
382 if (error) {
Luotao Fuf94add32010-07-02 14:10:29 +0200383 dev_err(&pdev->dev, "Could not register input device\n");
Viresh Kumar2bd942f2012-11-09 23:56:59 -0800384 return error;
Luotao Fuf94add32010-07-02 14:10:29 +0200385 }
386
Viresh Kumar2bd942f2012-11-09 23:56:59 -0800387 return 0;
Luotao Fuf94add32010-07-02 14:10:29 +0200388}
389
Bill Pembertone2619cf2012-11-23 21:50:47 -0800390static int stmpe_ts_remove(struct platform_device *pdev)
Luotao Fuf94add32010-07-02 14:10:29 +0200391{
392 struct stmpe_touch *ts = platform_get_drvdata(pdev);
Luotao Fuf94add32010-07-02 14:10:29 +0200393
394 stmpe_disable(ts->stmpe, STMPE_BLOCK_TOUCHSCREEN);
395
Luotao Fuf94add32010-07-02 14:10:29 +0200396 return 0;
397}
398
399static struct platform_driver stmpe_ts_driver = {
400 .driver = {
Dmitry Torokhovf91a3f02015-05-22 13:09:11 -0700401 .name = STMPE_TS_NAME,
402 },
Luotao Fuf94add32010-07-02 14:10:29 +0200403 .probe = stmpe_input_probe,
Bill Pemberton1cb0aa82012-11-23 21:27:39 -0800404 .remove = stmpe_ts_remove,
Luotao Fuf94add32010-07-02 14:10:29 +0200405};
JJ Dingcdcc96e2011-11-29 11:14:13 -0800406module_platform_driver(stmpe_ts_driver);
Luotao Fuf94add32010-07-02 14:10:29 +0200407
Dmitry Torokhovf91a3f02015-05-22 13:09:11 -0700408static const struct of_device_id stmpe_ts_ids[] = {
409 { .compatible = "st,stmpe-ts", },
410 { },
411};
412MODULE_DEVICE_TABLE(of, stmpe_ts_ids);
Dmitry Torokhovf91a3f02015-05-22 13:09:11 -0700413
Luotao Fuf94add32010-07-02 14:10:29 +0200414MODULE_AUTHOR("Luotao Fu <l.fu@pengutronix.de>");
415MODULE_DESCRIPTION("STMPEXXX touchscreen driver");
416MODULE_LICENSE("GPL");