stmpe-ts.c revision 037db524a2015607031c70a7935153120601b908
1/*
2 * STMicroelectronics STMPE811 Touchscreen Driver
3 *
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>
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/* Register layouts and functionalities are identical on all stmpexxx variants
31 * with touchscreen controller
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	 * touch_det sometimes get desasserted or just get stuck. This appears
108	 * to be a silicon bug, We still have to clearify this with the
109	 * manufacture. As a workaround We release the key anyway if the
110	 * touch_det keeps coming in after 4ms, while the FIFO contains no value
111	 * during the whole time.
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	/* reset the FIFO before we report release event */
120	__stmpe_reset_fifo(ts->stmpe);
121
122	input_report_abs(ts->idev, ABS_PRESSURE, 0);
123	input_sync(ts->idev);
124}
125
126static irqreturn_t stmpe_ts_handler(int irq, void *data)
127{
128	u8 data_set[4];
129	int x, y, z;
130	struct stmpe_touch *ts = data;
131
132	/*
133	 * Cancel scheduled polling for release if we have new value
134	 * available. Wait if the polling is already running.
135	 */
136	cancel_delayed_work_sync(&ts->work);
137
138	/*
139	 * The FIFO sometimes just crashes and stops generating interrupts. This
140	 * appears to be a silicon bug. We still have to clearify this with
141	 * the manufacture. As a workaround we disable the TSC while we are
142	 * collecting data and flush the FIFO after reading
143	 */
144	stmpe_set_bits(ts->stmpe, STMPE_REG_TSC_CTRL,
145				STMPE_TSC_CTRL_TSC_EN, 0);
146
147	stmpe_block_read(ts->stmpe, STMPE_REG_TSC_DATA_XYZ, 4, data_set);
148
149	x = (data_set[0] << 4) | (data_set[1] >> 4);
150	y = ((data_set[1] & 0xf) << 8) | data_set[2];
151	z = data_set[3];
152
153	input_report_abs(ts->idev, ABS_X, x);
154	input_report_abs(ts->idev, ABS_Y, y);
155	input_report_abs(ts->idev, ABS_PRESSURE, z);
156	input_sync(ts->idev);
157
158       /* flush the FIFO after we have read out our values. */
159	__stmpe_reset_fifo(ts->stmpe);
160
161	/* reenable the tsc */
162	stmpe_set_bits(ts->stmpe, STMPE_REG_TSC_CTRL,
163			STMPE_TSC_CTRL_TSC_EN, STMPE_TSC_CTRL_TSC_EN);
164
165	/* start polling for touch_det to detect release */
166	schedule_delayed_work(&ts->work, HZ / 50);
167
168	return IRQ_HANDLED;
169}
170
171static int __devinit stmpe_init_hw(struct stmpe_touch *ts)
172{
173	int ret;
174	u8 adc_ctrl1, adc_ctrl1_mask, tsc_cfg, tsc_cfg_mask;
175	struct stmpe *stmpe = ts->stmpe;
176	struct device *dev = ts->dev;
177
178	ret = stmpe_enable(stmpe, STMPE_BLOCK_TOUCHSCREEN | STMPE_BLOCK_ADC);
179	if (ret) {
180		dev_err(dev, "Could not enable clock for ADC and TS\n");
181		return ret;
182	}
183
184	adc_ctrl1 = SAMPLE_TIME(ts->sample_time) | MOD_12B(ts->mod_12b) |
185		REF_SEL(ts->ref_sel);
186	adc_ctrl1_mask = SAMPLE_TIME(0xff) | MOD_12B(0xff) | REF_SEL(0xff);
187
188	ret = stmpe_set_bits(stmpe, STMPE_REG_ADC_CTRL1,
189			adc_ctrl1_mask, adc_ctrl1);
190	if (ret) {
191		dev_err(dev, "Could not setup ADC\n");
192		return ret;
193	}
194
195	ret = stmpe_set_bits(stmpe, STMPE_REG_ADC_CTRL2,
196			ADC_FREQ(0xff), ADC_FREQ(ts->adc_freq));
197	if (ret) {
198		dev_err(dev, "Could not setup ADC\n");
199		return ret;
200	}
201
202	tsc_cfg = AVE_CTRL(ts->ave_ctrl) | DET_DELAY(ts->touch_det_delay) |
203			SETTLING(ts->settling);
204	tsc_cfg_mask = AVE_CTRL(0xff) | DET_DELAY(0xff) | SETTLING(0xff);
205
206	ret = stmpe_set_bits(stmpe, STMPE_REG_TSC_CFG, tsc_cfg_mask, tsc_cfg);
207	if (ret) {
208		dev_err(dev, "Could not config touch\n");
209		return ret;
210	}
211
212	ret = stmpe_set_bits(stmpe, STMPE_REG_TSC_FRACTION_Z,
213			FRACTION_Z(0xff), FRACTION_Z(ts->fraction_z));
214	if (ret) {
215		dev_err(dev, "Could not config touch\n");
216		return ret;
217	}
218
219	ret = stmpe_set_bits(stmpe, STMPE_REG_TSC_I_DRIVE,
220			I_DRIVE(0xff), I_DRIVE(ts->i_drive));
221	if (ret) {
222		dev_err(dev, "Could not config touch\n");
223		return ret;
224	}
225
226	/* set FIFO to 1 for single point reading */
227	ret = stmpe_reg_write(stmpe, STMPE_REG_FIFO_TH, 1);
228	if (ret) {
229		dev_err(dev, "Could not set FIFO\n");
230		return ret;
231	}
232
233	ret = stmpe_set_bits(stmpe, STMPE_REG_TSC_CTRL,
234			OP_MODE(0xff), OP_MODE(OP_MOD_XYZ));
235	if (ret) {
236		dev_err(dev, "Could not set mode\n");
237		return ret;
238	}
239
240	return 0;
241}
242
243static int stmpe_ts_open(struct input_dev *dev)
244{
245	struct stmpe_touch *ts = input_get_drvdata(dev);
246	int ret = 0;
247
248	ret = __stmpe_reset_fifo(ts->stmpe);
249	if (ret)
250		return ret;
251
252	return stmpe_set_bits(ts->stmpe, STMPE_REG_TSC_CTRL,
253			STMPE_TSC_CTRL_TSC_EN, STMPE_TSC_CTRL_TSC_EN);
254}
255
256static void stmpe_ts_close(struct input_dev *dev)
257{
258	struct stmpe_touch *ts = input_get_drvdata(dev);
259
260	cancel_delayed_work_sync(&ts->work);
261
262	stmpe_set_bits(ts->stmpe, STMPE_REG_TSC_CTRL,
263			STMPE_TSC_CTRL_TSC_EN, 0);
264}
265
266static void stmpe_ts_get_platform_info(struct platform_device *pdev,
267					struct stmpe_touch *ts)
268{
269	struct stmpe *stmpe = dev_get_drvdata(pdev->dev.parent);
270	struct device_node *np = pdev->dev.of_node;
271	struct stmpe_ts_platform_data *ts_pdata = NULL;
272
273	ts->stmpe = stmpe;
274
275	if (stmpe->pdata && stmpe->pdata->ts) {
276		ts_pdata = stmpe->pdata->ts;
277
278		ts->sample_time = ts_pdata->sample_time;
279		ts->mod_12b = ts_pdata->mod_12b;
280		ts->ref_sel = ts_pdata->ref_sel;
281		ts->adc_freq = ts_pdata->adc_freq;
282		ts->ave_ctrl = ts_pdata->ave_ctrl;
283		ts->touch_det_delay = ts_pdata->touch_det_delay;
284		ts->settling = ts_pdata->settling;
285		ts->fraction_z = ts_pdata->fraction_z;
286		ts->i_drive = ts_pdata->i_drive;
287	} else if (np) {
288		u32 val;
289
290		if (!of_property_read_u32(np, "st,sample-time", &val))
291			ts->sample_time = val;
292		if (!of_property_read_u32(np, "st,mod-12b", &val))
293			ts->mod_12b = val;
294		if (!of_property_read_u32(np, "st,ref-sel", &val))
295			ts->ref_sel = val;
296		if (!of_property_read_u32(np, "st,adc-freq", &val))
297			ts->adc_freq = val;
298		if (!of_property_read_u32(np, "st,ave-ctrl", &val))
299			ts->ave_ctrl = val;
300		if (!of_property_read_u32(np, "st,touch-det-delay", &val))
301			ts->touch_det_delay = val;
302		if (!of_property_read_u32(np, "st,settling", &val))
303			ts->settling = val;
304		if (!of_property_read_u32(np, "st,fraction-z", &val))
305			ts->fraction_z = val;
306		if (!of_property_read_u32(np, "st,i-drive", &val))
307			ts->i_drive = val;
308	}
309}
310
311static int __devinit stmpe_input_probe(struct platform_device *pdev)
312{
313	struct stmpe_touch *ts;
314	struct input_dev *idev;
315	int error;
316	int ts_irq;
317
318	ts_irq = platform_get_irq_byname(pdev, "FIFO_TH");
319	if (ts_irq < 0)
320		return ts_irq;
321
322	ts = devm_kzalloc(&pdev->dev, sizeof(*ts), GFP_KERNEL);
323	if (!ts)
324		return -ENOMEM;
325
326	idev = devm_input_allocate_device(&pdev->dev);
327	if (!idev)
328		return -ENOMEM;
329
330	platform_set_drvdata(pdev, ts);
331	ts->idev = idev;
332	ts->dev = &pdev->dev;
333
334	stmpe_ts_get_platform_info(pdev, ts);
335
336	INIT_DELAYED_WORK(&ts->work, stmpe_work);
337
338	error = devm_request_threaded_irq(&pdev->dev, ts_irq,
339					  NULL, stmpe_ts_handler,
340					  IRQF_ONESHOT, STMPE_TS_NAME, ts);
341	if (error) {
342		dev_err(&pdev->dev, "Failed to request IRQ %d\n", ts_irq);
343		return error;
344	}
345
346	error = stmpe_init_hw(ts);
347	if (error)
348		return error;
349
350	idev->name = STMPE_TS_NAME;
351	idev->phys = STMPE_TS_NAME"/input0";
352	idev->id.bustype = BUS_I2C;
353	idev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
354	idev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
355
356	idev->open = stmpe_ts_open;
357	idev->close = stmpe_ts_close;
358
359	input_set_drvdata(idev, ts);
360
361	input_set_abs_params(idev, ABS_X, 0, XY_MASK, 0, 0);
362	input_set_abs_params(idev, ABS_Y, 0, XY_MASK, 0, 0);
363	input_set_abs_params(idev, ABS_PRESSURE, 0x0, 0xff, 0, 0);
364
365	error = input_register_device(idev);
366	if (error) {
367		dev_err(&pdev->dev, "Could not register input device\n");
368		return error;
369	}
370
371	return 0;
372}
373
374static int __devexit stmpe_ts_remove(struct platform_device *pdev)
375{
376	struct stmpe_touch *ts = platform_get_drvdata(pdev);
377
378	stmpe_disable(ts->stmpe, STMPE_BLOCK_TOUCHSCREEN);
379
380	return 0;
381}
382
383static struct platform_driver stmpe_ts_driver = {
384	.driver = {
385		   .name = STMPE_TS_NAME,
386		   .owner = THIS_MODULE,
387		   },
388	.probe = stmpe_input_probe,
389	.remove = __devexit_p(stmpe_ts_remove),
390};
391module_platform_driver(stmpe_ts_driver);
392
393MODULE_AUTHOR("Luotao Fu <l.fu@pengutronix.de>");
394MODULE_DESCRIPTION("STMPEXXX touchscreen driver");
395MODULE_LICENSE("GPL");
396MODULE_ALIAS("platform:" STMPE_TS_NAME);
397