Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
tnetv107x-ts.c
Go to the documentation of this file.
1 /*
2  * Texas Instruments TNETV107X Touchscreen Driver
3  *
4  * Copyright (C) 2010 Texas Instruments
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation version 2.
9  *
10  * This program is distributed "as is" WITHOUT ANY WARRANTY of any
11  * kind, whether express or implied; without even the implied warranty
12  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  */
15 
16 #include <linux/module.h>
17 #include <linux/kernel.h>
18 #include <linux/err.h>
19 #include <linux/errno.h>
20 #include <linux/input.h>
21 #include <linux/platform_device.h>
22 #include <linux/interrupt.h>
23 #include <linux/slab.h>
24 #include <linux/delay.h>
25 #include <linux/ctype.h>
26 #include <linux/io.h>
27 #include <linux/clk.h>
28 
29 #include <mach/tnetv107x.h>
30 
31 #define TSC_PENUP_POLL (HZ / 5)
32 #define IDLE_TIMEOUT 100 /* msec */
33 
34 /*
35  * The first and last samples of a touch interval are usually garbage and need
36  * to be filtered out with these devices. The following definitions control
37  * the number of samples skipped.
38  */
39 #define TSC_HEAD_SKIP 1
40 #define TSC_TAIL_SKIP 1
41 #define TSC_SKIP (TSC_HEAD_SKIP + TSC_TAIL_SKIP + 1)
42 #define TSC_SAMPLES (TSC_SKIP + 1)
43 
44 /* Register Offsets */
45 struct tsc_regs {
52  u32 chval[4];
53 };
54 
55 /* TSC Mode Configuration Register (tscm) bits */
56 #define WMODE BIT(0)
57 #define TSKIND BIT(1)
58 #define ZMEASURE_EN BIT(2)
59 #define IDLE BIT(3)
60 #define TSC_EN BIT(4)
61 #define STOP BIT(5)
62 #define ONE_SHOT BIT(6)
63 #define SINGLE BIT(7)
64 #define AVG BIT(8)
65 #define AVGNUM(x) (((x) & 0x03) << 9)
66 #define PVSTC(x) (((x) & 0x07) << 11)
67 #define PON BIT(14)
68 #define PONBG BIT(15)
69 #define AFERST BIT(16)
70 
71 /* ADC DATA Capture Register bits */
72 #define DATA_VALID BIT(16)
73 
74 /* Register Access Macros */
75 #define tsc_read(ts, reg) __raw_readl(&(ts)->regs->reg)
76 #define tsc_write(ts, reg, val) __raw_writel(val, &(ts)->regs->reg);
77 #define tsc_set_bits(ts, reg, val) \
78  tsc_write(ts, reg, tsc_read(ts, reg) | (val))
79 #define tsc_clr_bits(ts, reg, val) \
80  tsc_write(ts, reg, tsc_read(ts, reg) & ~(val))
81 
82 struct sample {
83  int x, y, p;
84 };
85 
86 struct tsc_data {
88  struct resource *res;
90  struct timer_list timer;
92  struct clk *clk;
93  struct device *dev;
96  int tsc_irq;
97 };
98 
99 static int tsc_read_sample(struct tsc_data *ts, struct sample* sample)
100 {
101  int x, y, z1, z2, t, p = 0;
102  u32 val;
103 
104  val = tsc_read(ts, chval[0]);
105  if (val & DATA_VALID)
106  x = val & 0xffff;
107  else
108  return -EINVAL;
109 
110  y = tsc_read(ts, chval[1]) & 0xffff;
111  z1 = tsc_read(ts, chval[2]) & 0xffff;
112  z2 = tsc_read(ts, chval[3]) & 0xffff;
113 
114  if (z1) {
115  t = ((600 * x) * (z2 - z1));
116  p = t / (u32) (z1 << 12);
117  if (p < 0)
118  p = 0;
119  }
120 
121  sample->x = x;
122  sample->y = y;
123  sample->p = p;
124 
125  return 0;
126 }
127 
128 static void tsc_poll(unsigned long data)
129 {
130  struct tsc_data *ts = (struct tsc_data *)data;
131  unsigned long flags;
132  int i, val, x, y, p;
133 
134  spin_lock_irqsave(&ts->lock, flags);
135 
136  if (ts->sample_count >= TSC_SKIP) {
137  input_report_abs(ts->input_dev, ABS_PRESSURE, 0);
138  input_report_key(ts->input_dev, BTN_TOUCH, 0);
139  input_sync(ts->input_dev);
140  } else if (ts->sample_count > 0) {
141  /*
142  * A touch event lasted less than our skip count. Salvage and
143  * report anyway.
144  */
145  for (i = 0, val = 0; i < ts->sample_count; i++)
146  val += ts->samples[i].x;
147  x = val / ts->sample_count;
148 
149  for (i = 0, val = 0; i < ts->sample_count; i++)
150  val += ts->samples[i].y;
151  y = val / ts->sample_count;
152 
153  for (i = 0, val = 0; i < ts->sample_count; i++)
154  val += ts->samples[i].p;
155  p = val / ts->sample_count;
156 
157  input_report_abs(ts->input_dev, ABS_X, x);
158  input_report_abs(ts->input_dev, ABS_Y, y);
159  input_report_abs(ts->input_dev, ABS_PRESSURE, p);
160  input_report_key(ts->input_dev, BTN_TOUCH, 1);
161  input_sync(ts->input_dev);
162  }
163 
164  ts->sample_count = 0;
165 
166  spin_unlock_irqrestore(&ts->lock, flags);
167 }
168 
169 static irqreturn_t tsc_irq(int irq, void *dev_id)
170 {
171  struct tsc_data *ts = (struct tsc_data *)dev_id;
172  struct sample *sample;
173  int index;
174 
175  spin_lock(&ts->lock);
176 
177  index = ts->sample_count % TSC_SAMPLES;
178  sample = &ts->samples[index];
179  if (tsc_read_sample(ts, sample) < 0)
180  goto out;
181 
182  if (++ts->sample_count >= TSC_SKIP) {
183  index = (ts->sample_count - TSC_TAIL_SKIP - 1) % TSC_SAMPLES;
184  sample = &ts->samples[index];
185 
186  input_report_abs(ts->input_dev, ABS_X, sample->x);
187  input_report_abs(ts->input_dev, ABS_Y, sample->y);
188  input_report_abs(ts->input_dev, ABS_PRESSURE, sample->p);
189  if (ts->sample_count == TSC_SKIP)
190  input_report_key(ts->input_dev, BTN_TOUCH, 1);
191  input_sync(ts->input_dev);
192  }
193  mod_timer(&ts->timer, jiffies + TSC_PENUP_POLL);
194 out:
195  spin_unlock(&ts->lock);
196  return IRQ_HANDLED;
197 }
198 
199 static int tsc_start(struct input_dev *dev)
200 {
201  struct tsc_data *ts = input_get_drvdata(dev);
202  unsigned long timeout = jiffies + msecs_to_jiffies(IDLE_TIMEOUT);
203  u32 val;
204 
205  clk_enable(ts->clk);
206 
207  /* Go to idle mode, before any initialization */
208  while (time_after(timeout, jiffies)) {
209  if (tsc_read(ts, tscm) & IDLE)
210  break;
211  }
212 
213  if (time_before(timeout, jiffies)) {
214  dev_warn(ts->dev, "timeout waiting for idle\n");
215  clk_disable(ts->clk);
216  return -EIO;
217  }
218 
219  /* Configure TSC Control register*/
220  val = (PONBG | PON | PVSTC(4) | ONE_SHOT | ZMEASURE_EN);
221  tsc_write(ts, tscm, val);
222 
223  /* Bring TSC out of reset: Clear AFE reset bit */
224  val &= ~(AFERST);
225  tsc_write(ts, tscm, val);
226 
227  /* Configure all pins for hardware control*/
228  tsc_write(ts, bwcm, 0);
229 
230  /* Finally enable the TSC */
231  tsc_set_bits(ts, tscm, TSC_EN);
232 
233  return 0;
234 }
235 
236 static void tsc_stop(struct input_dev *dev)
237 {
238  struct tsc_data *ts = input_get_drvdata(dev);
239 
240  tsc_clr_bits(ts, tscm, TSC_EN);
242  del_timer_sync(&ts->timer);
243  clk_disable(ts->clk);
244 }
245 
246 static int __devinit tsc_probe(struct platform_device *pdev)
247 {
248  struct device *dev = &pdev->dev;
249  struct tsc_data *ts;
250  int error = 0;
251  u32 rev = 0;
252 
253  ts = kzalloc(sizeof(struct tsc_data), GFP_KERNEL);
254  if (!ts) {
255  dev_err(dev, "cannot allocate device info\n");
256  return -ENOMEM;
257  }
258 
259  ts->dev = dev;
260  spin_lock_init(&ts->lock);
261  setup_timer(&ts->timer, tsc_poll, (unsigned long)ts);
262  platform_set_drvdata(pdev, ts);
263 
264  ts->tsc_irq = platform_get_irq(pdev, 0);
265  if (ts->tsc_irq < 0) {
266  dev_err(dev, "cannot determine device interrupt\n");
267  error = -ENODEV;
268  goto error_res;
269  }
270 
271  ts->res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
272  if (!ts->res) {
273  dev_err(dev, "cannot determine register area\n");
274  error = -ENODEV;
275  goto error_res;
276  }
277 
278  if (!request_mem_region(ts->res->start, resource_size(ts->res),
279  pdev->name)) {
280  dev_err(dev, "cannot claim register memory\n");
281  ts->res = NULL;
282  error = -EINVAL;
283  goto error_res;
284  }
285 
286  ts->regs = ioremap(ts->res->start, resource_size(ts->res));
287  if (!ts->regs) {
288  dev_err(dev, "cannot map register memory\n");
289  error = -ENOMEM;
290  goto error_map;
291  }
292 
293  ts->clk = clk_get(dev, NULL);
294  if (IS_ERR(ts->clk)) {
295  dev_err(dev, "cannot claim device clock\n");
296  error = PTR_ERR(ts->clk);
297  goto error_clk;
298  }
299 
301  dev_name(dev), ts);
302  if (error < 0) {
303  dev_err(ts->dev, "Could not allocate ts irq\n");
304  goto error_irq;
305  }
306 
307  ts->input_dev = input_allocate_device();
308  if (!ts->input_dev) {
309  dev_err(dev, "cannot allocate input device\n");
310  error = -ENOMEM;
311  goto error_input;
312  }
313  input_set_drvdata(ts->input_dev, ts);
314 
315  ts->input_dev->name = pdev->name;
316  ts->input_dev->id.bustype = BUS_HOST;
317  ts->input_dev->dev.parent = &pdev->dev;
318  ts->input_dev->open = tsc_start;
319  ts->input_dev->close = tsc_stop;
320 
321  clk_enable(ts->clk);
322  rev = tsc_read(ts, rev);
323  ts->input_dev->id.product = ((rev >> 8) & 0x07);
324  ts->input_dev->id.version = ((rev >> 16) & 0xfff);
325  clk_disable(ts->clk);
326 
327  __set_bit(EV_KEY, ts->input_dev->evbit);
328  __set_bit(EV_ABS, ts->input_dev->evbit);
329  __set_bit(BTN_TOUCH, ts->input_dev->keybit);
330 
331  input_set_abs_params(ts->input_dev, ABS_X, 0, 0xffff, 5, 0);
332  input_set_abs_params(ts->input_dev, ABS_Y, 0, 0xffff, 5, 0);
333  input_set_abs_params(ts->input_dev, ABS_PRESSURE, 0, 4095, 128, 0);
334 
335  error = input_register_device(ts->input_dev);
336  if (error < 0) {
337  dev_err(dev, "failed input device registration\n");
338  goto error_reg;
339  }
340 
341  return 0;
342 
343 error_reg:
344  input_free_device(ts->input_dev);
345 error_input:
346  free_irq(ts->tsc_irq, ts);
347 error_irq:
348  clk_put(ts->clk);
349 error_clk:
350  iounmap(ts->regs);
351 error_map:
352  release_mem_region(ts->res->start, resource_size(ts->res));
353 error_res:
354  platform_set_drvdata(pdev, NULL);
355  kfree(ts);
356 
357  return error;
358 }
359 
360 static int __devexit tsc_remove(struct platform_device *pdev)
361 {
362  struct tsc_data *ts = platform_get_drvdata(pdev);
363 
364  input_unregister_device(ts->input_dev);
365  free_irq(ts->tsc_irq, ts);
366  clk_put(ts->clk);
367  iounmap(ts->regs);
368  release_mem_region(ts->res->start, resource_size(ts->res));
369  platform_set_drvdata(pdev, NULL);
370  kfree(ts);
371 
372  return 0;
373 }
374 
375 static struct platform_driver tsc_driver = {
376  .probe = tsc_probe,
377  .remove = __devexit_p(tsc_remove),
378  .driver.name = "tnetv107x-ts",
379  .driver.owner = THIS_MODULE,
380 };
381 module_platform_driver(tsc_driver);
382 
383 MODULE_AUTHOR("Cyril Chemparathy");
384 MODULE_DESCRIPTION("TNETV107X Touchscreen Driver");
385 MODULE_ALIAS("platform:tnetv107x-ts");
386 MODULE_LICENSE("GPL");