1
2
3
4
5
6
7
8
9
10
11
12
13
14#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/sched.h>
17#include <linux/interrupt.h>
18#include <linux/init.h>
19#include <linux/device.h>
20#include <linux/of.h>
21#include <linux/platform_device.h>
22#include <linux/input.h>
23#include <linux/slab.h>
24#include <linux/delay.h>
25#include <linux/i2c.h>
26#include <linux/workqueue.h>
27
28#include <linux/mfd/stmpe.h>
29
30
31
32
33#define STMPE_REG_INT_STA 0x0B
34#define STMPE_REG_ADC_CTRL1 0x20
35#define STMPE_REG_ADC_CTRL2 0x21
36#define STMPE_REG_TSC_CTRL 0x40
37#define STMPE_REG_TSC_CFG 0x41
38#define STMPE_REG_FIFO_TH 0x4A
39#define STMPE_REG_FIFO_STA 0x4B
40#define STMPE_REG_FIFO_SIZE 0x4C
41#define STMPE_REG_TSC_DATA_XYZ 0x52
42#define STMPE_REG_TSC_FRACTION_Z 0x56
43#define STMPE_REG_TSC_I_DRIVE 0x58
44
45#define OP_MOD_XYZ 0
46
47#define STMPE_TSC_CTRL_TSC_EN (1<<0)
48
49#define STMPE_FIFO_STA_RESET (1<<0)
50
51#define STMPE_IRQ_TOUCH_DET 0
52
53#define SAMPLE_TIME(x) ((x & 0xf) << 4)
54#define MOD_12B(x) ((x & 0x1) << 3)
55#define REF_SEL(x) ((x & 0x1) << 1)
56#define ADC_FREQ(x) (x & 0x3)
57#define AVE_CTRL(x) ((x & 0x3) << 6)
58#define DET_DELAY(x) ((x & 0x7) << 3)
59#define SETTLING(x) (x & 0x7)
60#define FRACTION_Z(x) (x & 0x7)
61#define I_DRIVE(x) (x & 0x1)
62#define OP_MODE(x) ((x & 0x7) << 1)
63
64#define STMPE_TS_NAME "stmpe-ts"
65#define XY_MASK 0xfff
66
67struct stmpe_touch {
68 struct stmpe *stmpe;
69 struct input_dev *idev;
70 struct delayed_work work;
71 struct device *dev;
72 u8 sample_time;
73 u8 mod_12b;
74 u8 ref_sel;
75 u8 adc_freq;
76 u8 ave_ctrl;
77 u8 touch_det_delay;
78 u8 settling;
79 u8 fraction_z;
80 u8 i_drive;
81};
82
83static int __stmpe_reset_fifo(struct stmpe *stmpe)
84{
85 int ret;
86
87 ret = stmpe_set_bits(stmpe, STMPE_REG_FIFO_STA,
88 STMPE_FIFO_STA_RESET, STMPE_FIFO_STA_RESET);
89 if (ret)
90 return ret;
91
92 return stmpe_set_bits(stmpe, STMPE_REG_FIFO_STA,
93 STMPE_FIFO_STA_RESET, 0);
94}
95
96static void stmpe_work(struct work_struct *work)
97{
98 int int_sta;
99 u32 timeout = 40;
100
101 struct stmpe_touch *ts =
102 container_of(work, struct stmpe_touch, work.work);
103
104 int_sta = stmpe_reg_read(ts->stmpe, STMPE_REG_INT_STA);
105
106
107
108
109
110
111
112
113 while ((int_sta & (1 << STMPE_IRQ_TOUCH_DET)) && (timeout > 0)) {
114 timeout--;
115 int_sta = stmpe_reg_read(ts->stmpe, STMPE_REG_INT_STA);
116 udelay(100);
117 }
118
119
120 __stmpe_reset_fifo(ts->stmpe);
121
122 input_report_abs(ts->idev, ABS_PRESSURE, 0);
123 input_report_key(ts->idev, BTN_TOUCH, 0);
124 input_sync(ts->idev);
125}
126
127static irqreturn_t stmpe_ts_handler(int irq, void *data)
128{
129 u8 data_set[4];
130 int x, y, z;
131 struct stmpe_touch *ts = data;
132
133
134
135
136
137 cancel_delayed_work_sync(&ts->work);
138
139
140
141
142
143
144
145 stmpe_set_bits(ts->stmpe, STMPE_REG_TSC_CTRL,
146 STMPE_TSC_CTRL_TSC_EN, 0);
147
148 stmpe_block_read(ts->stmpe, STMPE_REG_TSC_DATA_XYZ, 4, data_set);
149
150 x = (data_set[0] << 4) | (data_set[1] >> 4);
151 y = ((data_set[1] & 0xf) << 8) | data_set[2];
152 z = data_set[3];
153
154 input_report_abs(ts->idev, ABS_X, x);
155 input_report_abs(ts->idev, ABS_Y, y);
156 input_report_abs(ts->idev, ABS_PRESSURE, z);
157 input_report_key(ts->idev, BTN_TOUCH, 1);
158 input_sync(ts->idev);
159
160
161 __stmpe_reset_fifo(ts->stmpe);
162
163
164 stmpe_set_bits(ts->stmpe, STMPE_REG_TSC_CTRL,
165 STMPE_TSC_CTRL_TSC_EN, STMPE_TSC_CTRL_TSC_EN);
166
167
168 schedule_delayed_work(&ts->work, HZ / 50);
169
170 return IRQ_HANDLED;
171}
172
173static int stmpe_init_hw(struct stmpe_touch *ts)
174{
175 int ret;
176 u8 adc_ctrl1, adc_ctrl1_mask, tsc_cfg, tsc_cfg_mask;
177 struct stmpe *stmpe = ts->stmpe;
178 struct device *dev = ts->dev;
179
180 ret = stmpe_enable(stmpe, STMPE_BLOCK_TOUCHSCREEN | STMPE_BLOCK_ADC);
181 if (ret) {
182 dev_err(dev, "Could not enable clock for ADC and TS\n");
183 return ret;
184 }
185
186 adc_ctrl1 = SAMPLE_TIME(ts->sample_time) | MOD_12B(ts->mod_12b) |
187 REF_SEL(ts->ref_sel);
188 adc_ctrl1_mask = SAMPLE_TIME(0xff) | MOD_12B(0xff) | REF_SEL(0xff);
189
190 ret = stmpe_set_bits(stmpe, STMPE_REG_ADC_CTRL1,
191 adc_ctrl1_mask, adc_ctrl1);
192 if (ret) {
193 dev_err(dev, "Could not setup ADC\n");
194 return ret;
195 }
196
197 ret = stmpe_set_bits(stmpe, STMPE_REG_ADC_CTRL2,
198 ADC_FREQ(0xff), ADC_FREQ(ts->adc_freq));
199 if (ret) {
200 dev_err(dev, "Could not setup ADC\n");
201 return ret;
202 }
203
204 tsc_cfg = AVE_CTRL(ts->ave_ctrl) | DET_DELAY(ts->touch_det_delay) |
205 SETTLING(ts->settling);
206 tsc_cfg_mask = AVE_CTRL(0xff) | DET_DELAY(0xff) | SETTLING(0xff);
207
208 ret = stmpe_set_bits(stmpe, STMPE_REG_TSC_CFG, tsc_cfg_mask, tsc_cfg);
209 if (ret) {
210 dev_err(dev, "Could not config touch\n");
211 return ret;
212 }
213
214 ret = stmpe_set_bits(stmpe, STMPE_REG_TSC_FRACTION_Z,
215 FRACTION_Z(0xff), FRACTION_Z(ts->fraction_z));
216 if (ret) {
217 dev_err(dev, "Could not config touch\n");
218 return ret;
219 }
220
221 ret = stmpe_set_bits(stmpe, STMPE_REG_TSC_I_DRIVE,
222 I_DRIVE(0xff), I_DRIVE(ts->i_drive));
223 if (ret) {
224 dev_err(dev, "Could not config touch\n");
225 return ret;
226 }
227
228
229 ret = stmpe_reg_write(stmpe, STMPE_REG_FIFO_TH, 1);
230 if (ret) {
231 dev_err(dev, "Could not set FIFO\n");
232 return ret;
233 }
234
235 ret = stmpe_set_bits(stmpe, STMPE_REG_TSC_CTRL,
236 OP_MODE(0xff), OP_MODE(OP_MOD_XYZ));
237 if (ret) {
238 dev_err(dev, "Could not set mode\n");
239 return ret;
240 }
241
242 return 0;
243}
244
245static int stmpe_ts_open(struct input_dev *dev)
246{
247 struct stmpe_touch *ts = input_get_drvdata(dev);
248 int ret = 0;
249
250 ret = __stmpe_reset_fifo(ts->stmpe);
251 if (ret)
252 return ret;
253
254 return stmpe_set_bits(ts->stmpe, STMPE_REG_TSC_CTRL,
255 STMPE_TSC_CTRL_TSC_EN, STMPE_TSC_CTRL_TSC_EN);
256}
257
258static void stmpe_ts_close(struct input_dev *dev)
259{
260 struct stmpe_touch *ts = input_get_drvdata(dev);
261
262 cancel_delayed_work_sync(&ts->work);
263
264 stmpe_set_bits(ts->stmpe, STMPE_REG_TSC_CTRL,
265 STMPE_TSC_CTRL_TSC_EN, 0);
266}
267
268static void stmpe_ts_get_platform_info(struct platform_device *pdev,
269 struct stmpe_touch *ts)
270{
271 struct stmpe *stmpe = dev_get_drvdata(pdev->dev.parent);
272 struct device_node *np = pdev->dev.of_node;
273 struct stmpe_ts_platform_data *ts_pdata = NULL;
274
275 ts->stmpe = stmpe;
276
277 if (stmpe->pdata && stmpe->pdata->ts) {
278 ts_pdata = stmpe->pdata->ts;
279
280 ts->sample_time = ts_pdata->sample_time;
281 ts->mod_12b = ts_pdata->mod_12b;
282 ts->ref_sel = ts_pdata->ref_sel;
283 ts->adc_freq = ts_pdata->adc_freq;
284 ts->ave_ctrl = ts_pdata->ave_ctrl;
285 ts->touch_det_delay = ts_pdata->touch_det_delay;
286 ts->settling = ts_pdata->settling;
287 ts->fraction_z = ts_pdata->fraction_z;
288 ts->i_drive = ts_pdata->i_drive;
289 } else if (np) {
290 u32 val;
291
292 if (!of_property_read_u32(np, "st,sample-time", &val))
293 ts->sample_time = val;
294 if (!of_property_read_u32(np, "st,mod-12b", &val))
295 ts->mod_12b = val;
296 if (!of_property_read_u32(np, "st,ref-sel", &val))
297 ts->ref_sel = val;
298 if (!of_property_read_u32(np, "st,adc-freq", &val))
299 ts->adc_freq = val;
300 if (!of_property_read_u32(np, "st,ave-ctrl", &val))
301 ts->ave_ctrl = val;
302 if (!of_property_read_u32(np, "st,touch-det-delay", &val))
303 ts->touch_det_delay = val;
304 if (!of_property_read_u32(np, "st,settling", &val))
305 ts->settling = val;
306 if (!of_property_read_u32(np, "st,fraction-z", &val))
307 ts->fraction_z = val;
308 if (!of_property_read_u32(np, "st,i-drive", &val))
309 ts->i_drive = val;
310 }
311}
312
313static int stmpe_input_probe(struct platform_device *pdev)
314{
315 struct stmpe_touch *ts;
316 struct input_dev *idev;
317 int error;
318 int ts_irq;
319
320 ts_irq = platform_get_irq_byname(pdev, "FIFO_TH");
321 if (ts_irq < 0)
322 return ts_irq;
323
324 ts = devm_kzalloc(&pdev->dev, sizeof(*ts), GFP_KERNEL);
325 if (!ts)
326 return -ENOMEM;
327
328 idev = devm_input_allocate_device(&pdev->dev);
329 if (!idev)
330 return -ENOMEM;
331
332 platform_set_drvdata(pdev, ts);
333 ts->idev = idev;
334 ts->dev = &pdev->dev;
335
336 stmpe_ts_get_platform_info(pdev, ts);
337
338 INIT_DELAYED_WORK(&ts->work, stmpe_work);
339
340 error = devm_request_threaded_irq(&pdev->dev, ts_irq,
341 NULL, stmpe_ts_handler,
342 IRQF_ONESHOT, STMPE_TS_NAME, ts);
343 if (error) {
344 dev_err(&pdev->dev, "Failed to request IRQ %d\n", ts_irq);
345 return error;
346 }
347
348 error = stmpe_init_hw(ts);
349 if (error)
350 return error;
351
352 idev->name = STMPE_TS_NAME;
353 idev->phys = STMPE_TS_NAME"/input0";
354 idev->id.bustype = BUS_I2C;
355 idev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
356 idev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
357
358 idev->open = stmpe_ts_open;
359 idev->close = stmpe_ts_close;
360
361 input_set_drvdata(idev, ts);
362
363 input_set_abs_params(idev, ABS_X, 0, XY_MASK, 0, 0);
364 input_set_abs_params(idev, ABS_Y, 0, XY_MASK, 0, 0);
365 input_set_abs_params(idev, ABS_PRESSURE, 0x0, 0xff, 0, 0);
366
367 error = input_register_device(idev);
368 if (error) {
369 dev_err(&pdev->dev, "Could not register input device\n");
370 return error;
371 }
372
373 return 0;
374}
375
376static int stmpe_ts_remove(struct platform_device *pdev)
377{
378 struct stmpe_touch *ts = platform_get_drvdata(pdev);
379
380 stmpe_disable(ts->stmpe, STMPE_BLOCK_TOUCHSCREEN);
381
382 return 0;
383}
384
385static struct platform_driver stmpe_ts_driver = {
386 .driver = {
387 .name = STMPE_TS_NAME,
388 .owner = THIS_MODULE,
389 },
390 .probe = stmpe_input_probe,
391 .remove = stmpe_ts_remove,
392};
393module_platform_driver(stmpe_ts_driver);
394
395MODULE_AUTHOR("Luotao Fu <l.fu@pengutronix.de>");
396MODULE_DESCRIPTION("STMPEXXX touchscreen driver");
397MODULE_LICENSE("GPL");
398MODULE_ALIAS("platform:" STMPE_TS_NAME);
399