aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/input/touchscreen/ad7879.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/input/touchscreen/ad7879.c')
-rw-r--r--drivers/input/touchscreen/ad7879.c625
1 files changed, 186 insertions, 439 deletions
diff --git a/drivers/input/touchscreen/ad7879.c b/drivers/input/touchscreen/ad7879.c
index 4b32fb4704cd..ba6f0bd1e762 100644
--- a/drivers/input/touchscreen/ad7879.c
+++ b/drivers/input/touchscreen/ad7879.c
@@ -1,25 +1,9 @@
1/* 1/*
2 * Copyright (C) 2008-2009 Michael Hennerich, Analog Devices Inc. 2 * AD7879/AD7889 based touchscreen and GPIO driver
3 * 3 *
4 * Description: AD7879/AD7889 based touchscreen, and GPIO driver 4 * Copyright (C) 2008-2010 Michael Hennerich, Analog Devices Inc.
5 * (I2C/SPI Interface)
6 * 5 *
7 * Bugs: Enter bugs at http://blackfin.uclinux.org/ 6 * Licensed under the GPL-2 or later.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, see the file COPYING, or write
21 * to the Free Software Foundation, Inc.,
22 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 * 7 *
24 * History: 8 * History:
25 * Copyright (c) 2005 David Brownell 9 * Copyright (c) 2005 David Brownell
@@ -44,12 +28,12 @@
44#include <linux/interrupt.h> 28#include <linux/interrupt.h>
45#include <linux/irq.h> 29#include <linux/irq.h>
46#include <linux/slab.h> 30#include <linux/slab.h>
47#include <linux/workqueue.h>
48#include <linux/spi/spi.h> 31#include <linux/spi/spi.h>
49#include <linux/i2c.h> 32#include <linux/i2c.h>
50#include <linux/gpio.h> 33#include <linux/gpio.h>
51 34
52#include <linux/spi/ad7879.h> 35#include <linux/spi/ad7879.h>
36#include "ad7879.h"
53 37
54#define AD7879_REG_ZEROS 0 38#define AD7879_REG_ZEROS 0
55#define AD7879_REG_CTRL1 1 39#define AD7879_REG_CTRL1 1
@@ -120,30 +104,19 @@ enum {
120#define MAX_12BIT ((1<<12)-1) 104#define MAX_12BIT ((1<<12)-1)
121#define TS_PEN_UP_TIMEOUT msecs_to_jiffies(50) 105#define TS_PEN_UP_TIMEOUT msecs_to_jiffies(50)
122 106
123#if defined(CONFIG_TOUCHSCREEN_AD7879_SPI) || defined(CONFIG_TOUCHSCREEN_AD7879_SPI_MODULE)
124#define AD7879_DEVID 0x7A
125typedef struct spi_device bus_device;
126#elif defined(CONFIG_TOUCHSCREEN_AD7879_I2C) || defined(CONFIG_TOUCHSCREEN_AD7879_I2C_MODULE)
127#define AD7879_DEVID 0x79
128typedef struct i2c_client bus_device;
129#endif
130
131struct ad7879 { 107struct ad7879 {
132 bus_device *bus; 108 const struct ad7879_bus_ops *bops;
109
110 struct device *dev;
133 struct input_dev *input; 111 struct input_dev *input;
134 struct work_struct work;
135 struct timer_list timer; 112 struct timer_list timer;
136#ifdef CONFIG_GPIOLIB 113#ifdef CONFIG_GPIOLIB
137 struct gpio_chip gc; 114 struct gpio_chip gc;
138#endif
139 struct mutex mutex; 115 struct mutex mutex;
140 unsigned disabled:1; /* P: mutex */
141
142#if defined(CONFIG_TOUCHSCREEN_AD7879_SPI) || defined(CONFIG_TOUCHSCREEN_AD7879_SPI_MODULE)
143 struct spi_message msg;
144 struct spi_transfer xfer[AD7879_NR_SENSE + 1];
145 u16 cmd;
146#endif 116#endif
117 unsigned int irq;
118 bool disabled; /* P: input->mutex */
119 bool suspended; /* P: input->mutex */
147 u16 conversion_data[AD7879_NR_SENSE]; 120 u16 conversion_data[AD7879_NR_SENSE];
148 char phys[32]; 121 char phys[32];
149 u8 first_conversion_delay; 122 u8 first_conversion_delay;
@@ -158,11 +131,22 @@ struct ad7879 {
158 u16 cmd_crtl3; 131 u16 cmd_crtl3;
159}; 132};
160 133
161static int ad7879_read(bus_device *, u8); 134static int ad7879_read(struct ad7879 *ts, u8 reg)
162static int ad7879_write(bus_device *, u8, u16); 135{
163static void ad7879_collect(struct ad7879 *); 136 return ts->bops->read(ts->dev, reg);
137}
138
139static int ad7879_multi_read(struct ad7879 *ts, u8 first_reg, u8 count, u16 *buf)
140{
141 return ts->bops->multi_read(ts->dev, first_reg, count, buf);
142}
164 143
165static void ad7879_report(struct ad7879 *ts) 144static int ad7879_write(struct ad7879 *ts, u8 reg, u16 val)
145{
146 return ts->bops->write(ts->dev, reg, val);
147}
148
149static int ad7879_report(struct ad7879 *ts)
166{ 150{
167 struct input_dev *input_dev = ts->input; 151 struct input_dev *input_dev = ts->input;
168 unsigned Rt; 152 unsigned Rt;
@@ -175,12 +159,14 @@ static void ad7879_report(struct ad7879 *ts)
175 159
176 /* 160 /*
177 * The samples processed here are already preprocessed by the AD7879. 161 * The samples processed here are already preprocessed by the AD7879.
178 * The preprocessing function consists of a median and an averaging filter. 162 * The preprocessing function consists of a median and an averaging
179 * The combination of these two techniques provides a robust solution, 163 * filter. The combination of these two techniques provides a robust
180 * discarding the spurious noise in the signal and keeping only the data of interest. 164 * solution, discarding the spurious noise in the signal and keeping
181 * The size of both filters is programmable. (dev.platform_data, see linux/spi/ad7879.h) 165 * only the data of interest. The size of both filters is
182 * Other user-programmable conversion controls include variable acquisition time, 166 * programmable. (dev.platform_data, see linux/spi/ad7879.h) Other
183 * and first conversion delay. Up to 16 averages can be taken per conversion. 167 * user-programmable conversion controls include variable acquisition
168 * time, and first conversion delay. Up to 16 averages can be taken
169 * per conversion.
184 */ 170 */
185 171
186 if (likely(x && z1)) { 172 if (likely(x && z1)) {
@@ -189,21 +175,17 @@ static void ad7879_report(struct ad7879 *ts)
189 Rt /= z1; 175 Rt /= z1;
190 Rt = (Rt + 2047) >> 12; 176 Rt = (Rt + 2047) >> 12;
191 177
178 if (!timer_pending(&ts->timer))
179 input_report_key(input_dev, BTN_TOUCH, 1);
180
192 input_report_abs(input_dev, ABS_X, x); 181 input_report_abs(input_dev, ABS_X, x);
193 input_report_abs(input_dev, ABS_Y, y); 182 input_report_abs(input_dev, ABS_Y, y);
194 input_report_abs(input_dev, ABS_PRESSURE, Rt); 183 input_report_abs(input_dev, ABS_PRESSURE, Rt);
195 input_sync(input_dev); 184 input_sync(input_dev);
185 return 0;
196 } 186 }
197}
198
199static void ad7879_work(struct work_struct *work)
200{
201 struct ad7879 *ts = container_of(work, struct ad7879, work);
202 187
203 /* use keventd context to read the result registers */ 188 return -EINVAL;
204 ad7879_collect(ts);
205 ad7879_report(ts);
206 mod_timer(&ts->timer, jiffies + TS_PEN_UP_TIMEOUT);
207} 189}
208 190
209static void ad7879_ts_event_release(struct ad7879 *ts) 191static void ad7879_ts_event_release(struct ad7879 *ts)
@@ -211,6 +193,7 @@ static void ad7879_ts_event_release(struct ad7879 *ts)
211 struct input_dev *input_dev = ts->input; 193 struct input_dev *input_dev = ts->input;
212 194
213 input_report_abs(input_dev, ABS_PRESSURE, 0); 195 input_report_abs(input_dev, ABS_PRESSURE, 0);
196 input_report_key(input_dev, BTN_TOUCH, 0);
214 input_sync(input_dev); 197 input_sync(input_dev);
215} 198}
216 199
@@ -225,56 +208,98 @@ static irqreturn_t ad7879_irq(int irq, void *handle)
225{ 208{
226 struct ad7879 *ts = handle; 209 struct ad7879 *ts = handle;
227 210
228 /* The repeated conversion sequencer controlled by TMR kicked off too fast. 211 ad7879_multi_read(ts, AD7879_REG_XPLUS, AD7879_NR_SENSE, ts->conversion_data);
229 * We ignore the last and process the sample sequence currently in the queue.
230 * It can't be older than 9.4ms
231 */
232 212
233 if (!work_pending(&ts->work)) 213 if (!ad7879_report(ts))
234 schedule_work(&ts->work); 214 mod_timer(&ts->timer, jiffies + TS_PEN_UP_TIMEOUT);
235 215
236 return IRQ_HANDLED; 216 return IRQ_HANDLED;
237} 217}
238 218
239static void ad7879_setup(struct ad7879 *ts) 219static void __ad7879_enable(struct ad7879 *ts)
240{ 220{
241 ad7879_write(ts->bus, AD7879_REG_CTRL2, ts->cmd_crtl2); 221 ad7879_write(ts, AD7879_REG_CTRL2, ts->cmd_crtl2);
242 ad7879_write(ts->bus, AD7879_REG_CTRL3, ts->cmd_crtl3); 222 ad7879_write(ts, AD7879_REG_CTRL3, ts->cmd_crtl3);
243 ad7879_write(ts->bus, AD7879_REG_CTRL1, ts->cmd_crtl1); 223 ad7879_write(ts, AD7879_REG_CTRL1, ts->cmd_crtl1);
224
225 enable_irq(ts->irq);
244} 226}
245 227
246static void ad7879_disable(struct ad7879 *ts) 228static void __ad7879_disable(struct ad7879 *ts)
247{ 229{
248 mutex_lock(&ts->mutex); 230 disable_irq(ts->irq);
231
232 if (del_timer_sync(&ts->timer))
233 ad7879_ts_event_release(ts);
234
235 ad7879_write(ts, AD7879_REG_CTRL2, AD7879_PM(AD7879_PM_SHUTDOWN));
236}
249 237
250 if (!ts->disabled) {
251 238
252 ts->disabled = 1; 239static int ad7879_open(struct input_dev *input)
253 disable_irq(ts->bus->irq); 240{
241 struct ad7879 *ts = input_get_drvdata(input);
254 242
255 cancel_work_sync(&ts->work); 243 /* protected by input->mutex */
244 if (!ts->disabled && !ts->suspended)
245 __ad7879_enable(ts);
256 246
257 if (del_timer_sync(&ts->timer)) 247 return 0;
258 ad7879_ts_event_release(ts); 248}
259 249
260 ad7879_write(ts->bus, AD7879_REG_CTRL2, 250static void ad7879_close(struct input_dev* input)
261 AD7879_PM(AD7879_PM_SHUTDOWN)); 251{
262 } 252 struct ad7879 *ts = input_get_drvdata(input);
263 253
264 mutex_unlock(&ts->mutex); 254 /* protected by input->mutex */
255 if (!ts->disabled && !ts->suspended)
256 __ad7879_disable(ts);
265} 257}
266 258
267static void ad7879_enable(struct ad7879 *ts) 259void ad7879_suspend(struct ad7879 *ts)
268{ 260{
269 mutex_lock(&ts->mutex); 261 mutex_lock(&ts->input->mutex);
262
263 if (!ts->suspended && !ts->disabled && ts->input->users)
264 __ad7879_disable(ts);
265
266 ts->suspended = true;
270 267
271 if (ts->disabled) { 268 mutex_unlock(&ts->input->mutex);
272 ad7879_setup(ts); 269}
273 ts->disabled = 0; 270EXPORT_SYMBOL(ad7879_suspend);
274 enable_irq(ts->bus->irq); 271
272void ad7879_resume(struct ad7879 *ts)
273{
274 mutex_lock(&ts->input->mutex);
275
276 if (ts->suspended && !ts->disabled && ts->input->users)
277 __ad7879_enable(ts);
278
279 ts->suspended = false;
280
281 mutex_unlock(&ts->input->mutex);
282}
283EXPORT_SYMBOL(ad7879_resume);
284
285static void ad7879_toggle(struct ad7879 *ts, bool disable)
286{
287 mutex_lock(&ts->input->mutex);
288
289 if (!ts->suspended && ts->input->users != 0) {
290
291 if (disable) {
292 if (ts->disabled)
293 __ad7879_enable(ts);
294 } else {
295 if (!ts->disabled)
296 __ad7879_disable(ts);
297 }
275 } 298 }
276 299
277 mutex_unlock(&ts->mutex); 300 ts->disabled = disable;
301
302 mutex_unlock(&ts->input->mutex);
278} 303}
279 304
280static ssize_t ad7879_disable_show(struct device *dev, 305static ssize_t ad7879_disable_show(struct device *dev,
@@ -297,10 +322,7 @@ static ssize_t ad7879_disable_store(struct device *dev,
297 if (error) 322 if (error)
298 return error; 323 return error;
299 324
300 if (val) 325 ad7879_toggle(ts, val);
301 ad7879_disable(ts);
302 else
303 ad7879_enable(ts);
304 326
305 return count; 327 return count;
306} 328}
@@ -325,7 +347,7 @@ static int ad7879_gpio_direction_input(struct gpio_chip *chip,
325 347
326 mutex_lock(&ts->mutex); 348 mutex_lock(&ts->mutex);
327 ts->cmd_crtl2 |= AD7879_GPIO_EN | AD7879_GPIODIR | AD7879_GPIOPOL; 349 ts->cmd_crtl2 |= AD7879_GPIO_EN | AD7879_GPIODIR | AD7879_GPIOPOL;
328 err = ad7879_write(ts->bus, AD7879_REG_CTRL2, ts->cmd_crtl2); 350 err = ad7879_write(ts, AD7879_REG_CTRL2, ts->cmd_crtl2);
329 mutex_unlock(&ts->mutex); 351 mutex_unlock(&ts->mutex);
330 352
331 return err; 353 return err;
@@ -345,7 +367,7 @@ static int ad7879_gpio_direction_output(struct gpio_chip *chip,
345 else 367 else
346 ts->cmd_crtl2 &= ~AD7879_GPIO_DATA; 368 ts->cmd_crtl2 &= ~AD7879_GPIO_DATA;
347 369
348 err = ad7879_write(ts->bus, AD7879_REG_CTRL2, ts->cmd_crtl2); 370 err = ad7879_write(ts, AD7879_REG_CTRL2, ts->cmd_crtl2);
349 mutex_unlock(&ts->mutex); 371 mutex_unlock(&ts->mutex);
350 372
351 return err; 373 return err;
@@ -357,7 +379,7 @@ static int ad7879_gpio_get_value(struct gpio_chip *chip, unsigned gpio)
357 u16 val; 379 u16 val;
358 380
359 mutex_lock(&ts->mutex); 381 mutex_lock(&ts->mutex);
360 val = ad7879_read(ts->bus, AD7879_REG_CTRL2); 382 val = ad7879_read(ts, AD7879_REG_CTRL2);
361 mutex_unlock(&ts->mutex); 383 mutex_unlock(&ts->mutex);
362 384
363 return !!(val & AD7879_GPIO_DATA); 385 return !!(val & AD7879_GPIO_DATA);
@@ -374,16 +396,17 @@ static void ad7879_gpio_set_value(struct gpio_chip *chip,
374 else 396 else
375 ts->cmd_crtl2 &= ~AD7879_GPIO_DATA; 397 ts->cmd_crtl2 &= ~AD7879_GPIO_DATA;
376 398
377 ad7879_write(ts->bus, AD7879_REG_CTRL2, ts->cmd_crtl2); 399 ad7879_write(ts, AD7879_REG_CTRL2, ts->cmd_crtl2);
378 mutex_unlock(&ts->mutex); 400 mutex_unlock(&ts->mutex);
379} 401}
380 402
381static int __devinit ad7879_gpio_add(struct device *dev) 403static int ad7879_gpio_add(struct ad7879 *ts,
404 const struct ad7879_platform_data *pdata)
382{ 405{
383 struct ad7879 *ts = dev_get_drvdata(dev);
384 struct ad7879_platform_data *pdata = dev->platform_data;
385 int ret = 0; 406 int ret = 0;
386 407
408 mutex_init(&ts->mutex);
409
387 if (pdata->gpio_export) { 410 if (pdata->gpio_export) {
388 ts->gc.direction_input = ad7879_gpio_direction_input; 411 ts->gc.direction_input = ad7879_gpio_direction_input;
389 ts->gc.direction_output = ad7879_gpio_direction_output; 412 ts->gc.direction_output = ad7879_gpio_direction_output;
@@ -394,72 +417,75 @@ static int __devinit ad7879_gpio_add(struct device *dev)
394 ts->gc.ngpio = 1; 417 ts->gc.ngpio = 1;
395 ts->gc.label = "AD7879-GPIO"; 418 ts->gc.label = "AD7879-GPIO";
396 ts->gc.owner = THIS_MODULE; 419 ts->gc.owner = THIS_MODULE;
397 ts->gc.dev = dev; 420 ts->gc.dev = ts->dev;
398 421
399 ret = gpiochip_add(&ts->gc); 422 ret = gpiochip_add(&ts->gc);
400 if (ret) 423 if (ret)
401 dev_err(dev, "failed to register gpio %d\n", 424 dev_err(ts->dev, "failed to register gpio %d\n",
402 ts->gc.base); 425 ts->gc.base);
403 } 426 }
404 427
405 return ret; 428 return ret;
406} 429}
407 430
408/* 431static void ad7879_gpio_remove(struct ad7879 *ts)
409 * We mark ad7879_gpio_remove inline so there is a chance the code
410 * gets discarded when not needed. We can't do __devinit/__devexit
411 * markup since it is used in both probe and remove methods.
412 */
413static inline void ad7879_gpio_remove(struct device *dev)
414{ 432{
415 struct ad7879 *ts = dev_get_drvdata(dev); 433 const struct ad7879_platform_data *pdata = ts->dev->platform_data;
416 struct ad7879_platform_data *pdata = dev->platform_data;
417 int ret; 434 int ret;
418 435
419 if (pdata->gpio_export) { 436 if (pdata->gpio_export) {
420 ret = gpiochip_remove(&ts->gc); 437 ret = gpiochip_remove(&ts->gc);
421 if (ret) 438 if (ret)
422 dev_err(dev, "failed to remove gpio %d\n", 439 dev_err(ts->dev, "failed to remove gpio %d\n",
423 ts->gc.base); 440 ts->gc.base);
424 } 441 }
425} 442}
426#else 443#else
427static inline int ad7879_gpio_add(struct device *dev) 444static inline int ad7879_gpio_add(struct ad7879 *ts,
445 const struct ad7879_platform_data *pdata)
428{ 446{
429 return 0; 447 return 0;
430} 448}
431 449
432static inline void ad7879_gpio_remove(struct device *dev) 450static inline void ad7879_gpio_remove(struct ad7879 *ts)
433{ 451{
434} 452}
435#endif 453#endif
436 454
437static int __devinit ad7879_construct(bus_device *bus, struct ad7879 *ts) 455struct ad7879 *ad7879_probe(struct device *dev, u8 devid, unsigned int irq,
456 const struct ad7879_bus_ops *bops)
438{ 457{
458 struct ad7879_platform_data *pdata = dev->platform_data;
459 struct ad7879 *ts;
439 struct input_dev *input_dev; 460 struct input_dev *input_dev;
440 struct ad7879_platform_data *pdata = bus->dev.platform_data;
441 int err; 461 int err;
442 u16 revid; 462 u16 revid;
443 463
444 if (!bus->irq) { 464 if (!irq) {
445 dev_err(&bus->dev, "no IRQ?\n"); 465 dev_err(dev, "no IRQ?\n");
446 return -ENODEV; 466 err = -EINVAL;
467 goto err_out;
447 } 468 }
448 469
449 if (!pdata) { 470 if (!pdata) {
450 dev_err(&bus->dev, "no platform data?\n"); 471 dev_err(dev, "no platform data?\n");
451 return -ENODEV; 472 err = -EINVAL;
473 goto err_out;
452 } 474 }
453 475
476 ts = kzalloc(sizeof(*ts), GFP_KERNEL);
454 input_dev = input_allocate_device(); 477 input_dev = input_allocate_device();
455 if (!input_dev) 478 if (!ts || !input_dev) {
456 return -ENOMEM; 479 err = -ENOMEM;
480 goto err_free_mem;
481 }
457 482
483 ts->bops = bops;
484 ts->dev = dev;
458 ts->input = input_dev; 485 ts->input = input_dev;
486 ts->irq = irq;
459 487
460 setup_timer(&ts->timer, ad7879_timer, (unsigned long) ts); 488 setup_timer(&ts->timer, ad7879_timer, (unsigned long) ts);
461 INIT_WORK(&ts->work, ad7879_work);
462 mutex_init(&ts->mutex);
463 489
464 ts->x_plate_ohms = pdata->x_plate_ohms ? : 400; 490 ts->x_plate_ohms = pdata->x_plate_ohms ? : 400;
465 ts->pressure_max = pdata->pressure_max ? : ~0; 491 ts->pressure_max = pdata->pressure_max ? : ~0;
@@ -470,17 +496,26 @@ static int __devinit ad7879_construct(bus_device *bus, struct ad7879 *ts)
470 ts->pen_down_acc_interval = pdata->pen_down_acc_interval; 496 ts->pen_down_acc_interval = pdata->pen_down_acc_interval;
471 ts->median = pdata->median; 497 ts->median = pdata->median;
472 498
473 snprintf(ts->phys, sizeof(ts->phys), "%s/input0", dev_name(&bus->dev)); 499 snprintf(ts->phys, sizeof(ts->phys), "%s/input0", dev_name(dev));
474 500
475 input_dev->name = "AD7879 Touchscreen"; 501 input_dev->name = "AD7879 Touchscreen";
476 input_dev->phys = ts->phys; 502 input_dev->phys = ts->phys;
477 input_dev->dev.parent = &bus->dev; 503 input_dev->dev.parent = dev;
504 input_dev->id.bustype = bops->bustype;
505
506 input_dev->open = ad7879_open;
507 input_dev->close = ad7879_close;
508
509 input_set_drvdata(input_dev, ts);
478 510
479 __set_bit(EV_ABS, input_dev->evbit); 511 __set_bit(EV_ABS, input_dev->evbit);
480 __set_bit(ABS_X, input_dev->absbit); 512 __set_bit(ABS_X, input_dev->absbit);
481 __set_bit(ABS_Y, input_dev->absbit); 513 __set_bit(ABS_Y, input_dev->absbit);
482 __set_bit(ABS_PRESSURE, input_dev->absbit); 514 __set_bit(ABS_PRESSURE, input_dev->absbit);
483 515
516 __set_bit(EV_KEY, input_dev->evbit);
517 __set_bit(BTN_TOUCH, input_dev->keybit);
518
484 input_set_abs_params(input_dev, ABS_X, 519 input_set_abs_params(input_dev, ABS_X,
485 pdata->x_min ? : 0, 520 pdata->x_min ? : 0,
486 pdata->x_max ? : MAX_12BIT, 521 pdata->x_max ? : MAX_12BIT,
@@ -492,17 +527,18 @@ static int __devinit ad7879_construct(bus_device *bus, struct ad7879 *ts)
492 input_set_abs_params(input_dev, ABS_PRESSURE, 527 input_set_abs_params(input_dev, ABS_PRESSURE,
493 pdata->pressure_min, pdata->pressure_max, 0, 0); 528 pdata->pressure_min, pdata->pressure_max, 0, 0);
494 529
495 err = ad7879_write(bus, AD7879_REG_CTRL2, AD7879_RESET); 530 err = ad7879_write(ts, AD7879_REG_CTRL2, AD7879_RESET);
496
497 if (err < 0) { 531 if (err < 0) {
498 dev_err(&bus->dev, "Failed to write %s\n", input_dev->name); 532 dev_err(dev, "Failed to write %s\n", input_dev->name);
499 goto err_free_mem; 533 goto err_free_mem;
500 } 534 }
501 535
502 revid = ad7879_read(bus, AD7879_REG_REVID); 536 revid = ad7879_read(ts, AD7879_REG_REVID);
503 537 input_dev->id.product = (revid & 0xff);
504 if ((revid & 0xFF) != AD7879_DEVID) { 538 input_dev->id.version = revid >> 8;
505 dev_err(&bus->dev, "Failed to probe %s\n", input_dev->name); 539 if (input_dev->id.product != devid) {
540 dev_err(dev, "Failed to probe %s (%x vs %x)\n",
541 input_dev->name, devid, revid);
506 err = -ENODEV; 542 err = -ENODEV;
507 goto err_free_mem; 543 goto err_free_mem;
508 } 544 }
@@ -524,21 +560,21 @@ static int __devinit ad7879_construct(bus_device *bus, struct ad7879 *ts)
524 AD7879_ACQ(ts->acquisition_time) | 560 AD7879_ACQ(ts->acquisition_time) |
525 AD7879_TMR(ts->pen_down_acc_interval); 561 AD7879_TMR(ts->pen_down_acc_interval);
526 562
527 ad7879_setup(ts); 563 err = request_threaded_irq(ts->irq, NULL, ad7879_irq,
528 564 IRQF_TRIGGER_FALLING,
529 err = request_irq(bus->irq, ad7879_irq, 565 dev_name(dev), ts);
530 IRQF_TRIGGER_FALLING, bus->dev.driver->name, ts);
531
532 if (err) { 566 if (err) {
533 dev_err(&bus->dev, "irq %d busy?\n", bus->irq); 567 dev_err(dev, "irq %d busy?\n", ts->irq);
534 goto err_free_mem; 568 goto err_free_mem;
535 } 569 }
536 570
537 err = sysfs_create_group(&bus->dev.kobj, &ad7879_attr_group); 571 __ad7879_disable(ts);
572
573 err = sysfs_create_group(&dev->kobj, &ad7879_attr_group);
538 if (err) 574 if (err)
539 goto err_free_irq; 575 goto err_free_irq;
540 576
541 err = ad7879_gpio_add(&bus->dev); 577 err = ad7879_gpio_add(ts, pdata);
542 if (err) 578 if (err)
543 goto err_remove_attr; 579 goto err_remove_attr;
544 580
@@ -546,321 +582,32 @@ static int __devinit ad7879_construct(bus_device *bus, struct ad7879 *ts)
546 if (err) 582 if (err)
547 goto err_remove_gpio; 583 goto err_remove_gpio;
548 584
549 dev_info(&bus->dev, "Rev.%d touchscreen, irq %d\n", 585 return ts;
550 revid >> 8, bus->irq);
551
552 return 0;
553 586
554err_remove_gpio: 587err_remove_gpio:
555 ad7879_gpio_remove(&bus->dev); 588 ad7879_gpio_remove(ts);
556err_remove_attr: 589err_remove_attr:
557 sysfs_remove_group(&bus->dev.kobj, &ad7879_attr_group); 590 sysfs_remove_group(&dev->kobj, &ad7879_attr_group);
558err_free_irq: 591err_free_irq:
559 free_irq(bus->irq, ts); 592 free_irq(ts->irq, ts);
560err_free_mem: 593err_free_mem:
561 input_free_device(input_dev); 594 input_free_device(input_dev);
562
563 return err;
564}
565
566static int __devexit ad7879_destroy(bus_device *bus, struct ad7879 *ts)
567{
568 ad7879_gpio_remove(&bus->dev);
569 ad7879_disable(ts);
570 sysfs_remove_group(&ts->bus->dev.kobj, &ad7879_attr_group);
571 free_irq(ts->bus->irq, ts);
572 input_unregister_device(ts->input);
573 dev_dbg(&bus->dev, "unregistered touchscreen\n");
574
575 return 0;
576}
577
578#ifdef CONFIG_PM
579static int ad7879_suspend(bus_device *bus, pm_message_t message)
580{
581 struct ad7879 *ts = dev_get_drvdata(&bus->dev);
582
583 ad7879_disable(ts);
584
585 return 0;
586}
587
588static int ad7879_resume(bus_device *bus)
589{
590 struct ad7879 *ts = dev_get_drvdata(&bus->dev);
591
592 ad7879_enable(ts);
593
594 return 0;
595}
596#else
597#define ad7879_suspend NULL
598#define ad7879_resume NULL
599#endif
600
601#if defined(CONFIG_TOUCHSCREEN_AD7879_SPI) || defined(CONFIG_TOUCHSCREEN_AD7879_SPI_MODULE)
602#define MAX_SPI_FREQ_HZ 5000000
603#define AD7879_CMD_MAGIC 0xE000
604#define AD7879_CMD_READ (1 << 10)
605#define AD7879_WRITECMD(reg) (AD7879_CMD_MAGIC | (reg & 0xF))
606#define AD7879_READCMD(reg) (AD7879_CMD_MAGIC | AD7879_CMD_READ | (reg & 0xF))
607
608struct ser_req {
609 u16 command;
610 u16 data;
611 struct spi_message msg;
612 struct spi_transfer xfer[2];
613};
614
615/*
616 * ad7879_read/write are only used for initial setup and for sysfs controls.
617 * The main traffic is done in ad7879_collect().
618 */
619
620static int ad7879_read(struct spi_device *spi, u8 reg)
621{
622 struct ser_req *req;
623 int status, ret;
624
625 req = kzalloc(sizeof *req, GFP_KERNEL);
626 if (!req)
627 return -ENOMEM;
628
629 spi_message_init(&req->msg);
630
631 req->command = (u16) AD7879_READCMD(reg);
632 req->xfer[0].tx_buf = &req->command;
633 req->xfer[0].len = 2;
634
635 req->xfer[1].rx_buf = &req->data;
636 req->xfer[1].len = 2;
637
638 spi_message_add_tail(&req->xfer[0], &req->msg);
639 spi_message_add_tail(&req->xfer[1], &req->msg);
640
641 status = spi_sync(spi, &req->msg);
642 ret = status ? : req->data;
643
644 kfree(req);
645
646 return ret;
647}
648
649static int ad7879_write(struct spi_device *spi, u8 reg, u16 val)
650{
651 struct ser_req *req;
652 int status;
653
654 req = kzalloc(sizeof *req, GFP_KERNEL);
655 if (!req)
656 return -ENOMEM;
657
658 spi_message_init(&req->msg);
659
660 req->command = (u16) AD7879_WRITECMD(reg);
661 req->xfer[0].tx_buf = &req->command;
662 req->xfer[0].len = 2;
663
664 req->data = val;
665 req->xfer[1].tx_buf = &req->data;
666 req->xfer[1].len = 2;
667
668 spi_message_add_tail(&req->xfer[0], &req->msg);
669 spi_message_add_tail(&req->xfer[1], &req->msg);
670
671 status = spi_sync(spi, &req->msg);
672
673 kfree(req);
674
675 return status;
676}
677
678static void ad7879_collect(struct ad7879 *ts)
679{
680 int status = spi_sync(ts->bus, &ts->msg);
681
682 if (status)
683 dev_err(&ts->bus->dev, "spi_sync --> %d\n", status);
684}
685
686static void ad7879_setup_ts_def_msg(struct ad7879 *ts)
687{
688 struct spi_message *m;
689 int i;
690
691 ts->cmd = (u16) AD7879_READCMD(AD7879_REG_XPLUS);
692
693 m = &ts->msg;
694 spi_message_init(m);
695 ts->xfer[0].tx_buf = &ts->cmd;
696 ts->xfer[0].len = 2;
697
698 spi_message_add_tail(&ts->xfer[0], m);
699
700 for (i = 0; i < AD7879_NR_SENSE; i++) {
701 ts->xfer[i + 1].rx_buf = &ts->conversion_data[i];
702 ts->xfer[i + 1].len = 2;
703 spi_message_add_tail(&ts->xfer[i + 1], m);
704 }
705}
706
707static int __devinit ad7879_probe(struct spi_device *spi)
708{
709 struct ad7879 *ts;
710 int error;
711
712 /* don't exceed max specified SPI CLK frequency */
713 if (spi->max_speed_hz > MAX_SPI_FREQ_HZ) {
714 dev_err(&spi->dev, "SPI CLK %d Hz?\n", spi->max_speed_hz);
715 return -EINVAL;
716 }
717
718 ts = kzalloc(sizeof(struct ad7879), GFP_KERNEL);
719 if (!ts)
720 return -ENOMEM;
721
722 dev_set_drvdata(&spi->dev, ts);
723 ts->bus = spi;
724
725 ad7879_setup_ts_def_msg(ts);
726
727 error = ad7879_construct(spi, ts);
728 if (error) {
729 dev_set_drvdata(&spi->dev, NULL);
730 kfree(ts);
731 }
732
733 return error;
734}
735
736static int __devexit ad7879_remove(struct spi_device *spi)
737{
738 struct ad7879 *ts = dev_get_drvdata(&spi->dev);
739
740 ad7879_destroy(spi, ts);
741 dev_set_drvdata(&spi->dev, NULL);
742 kfree(ts); 595 kfree(ts);
743 596err_out:
744 return 0; 597 return ERR_PTR(err);
745} 598}
599EXPORT_SYMBOL(ad7879_probe);
746 600
747static struct spi_driver ad7879_driver = { 601void ad7879_remove(struct ad7879 *ts)
748 .driver = {
749 .name = "ad7879",
750 .bus = &spi_bus_type,
751 .owner = THIS_MODULE,
752 },
753 .probe = ad7879_probe,
754 .remove = __devexit_p(ad7879_remove),
755 .suspend = ad7879_suspend,
756 .resume = ad7879_resume,
757};
758
759static int __init ad7879_init(void)
760{
761 return spi_register_driver(&ad7879_driver);
762}
763module_init(ad7879_init);
764
765static void __exit ad7879_exit(void)
766{
767 spi_unregister_driver(&ad7879_driver);
768}
769module_exit(ad7879_exit);
770
771#elif defined(CONFIG_TOUCHSCREEN_AD7879_I2C) || defined(CONFIG_TOUCHSCREEN_AD7879_I2C_MODULE)
772
773/* All registers are word-sized.
774 * AD7879 uses a high-byte first convention.
775 */
776static int ad7879_read(struct i2c_client *client, u8 reg)
777{ 602{
778 return swab16(i2c_smbus_read_word_data(client, reg)); 603 ad7879_gpio_remove(ts);
779} 604 sysfs_remove_group(&ts->dev->kobj, &ad7879_attr_group);
780 605 free_irq(ts->irq, ts);
781static int ad7879_write(struct i2c_client *client, u8 reg, u16 val) 606 input_unregister_device(ts->input);
782{
783 return i2c_smbus_write_word_data(client, reg, swab16(val));
784}
785
786static void ad7879_collect(struct ad7879 *ts)
787{
788 int i;
789
790 for (i = 0; i < AD7879_NR_SENSE; i++)
791 ts->conversion_data[i] = ad7879_read(ts->bus,
792 AD7879_REG_XPLUS + i);
793}
794
795static int __devinit ad7879_probe(struct i2c_client *client,
796 const struct i2c_device_id *id)
797{
798 struct ad7879 *ts;
799 int error;
800
801 if (!i2c_check_functionality(client->adapter,
802 I2C_FUNC_SMBUS_WORD_DATA)) {
803 dev_err(&client->dev, "SMBUS Word Data not Supported\n");
804 return -EIO;
805 }
806
807 ts = kzalloc(sizeof(struct ad7879), GFP_KERNEL);
808 if (!ts)
809 return -ENOMEM;
810
811 i2c_set_clientdata(client, ts);
812 ts->bus = client;
813
814 error = ad7879_construct(client, ts);
815 if (error)
816 kfree(ts);
817
818 return error;
819}
820
821static int __devexit ad7879_remove(struct i2c_client *client)
822{
823 struct ad7879 *ts = dev_get_drvdata(&client->dev);
824
825 ad7879_destroy(client, ts);
826 kfree(ts); 607 kfree(ts);
827
828 return 0;
829}
830
831static const struct i2c_device_id ad7879_id[] = {
832 { "ad7879", 0 },
833 { "ad7889", 0 },
834 { }
835};
836MODULE_DEVICE_TABLE(i2c, ad7879_id);
837
838static struct i2c_driver ad7879_driver = {
839 .driver = {
840 .name = "ad7879",
841 .owner = THIS_MODULE,
842 },
843 .probe = ad7879_probe,
844 .remove = __devexit_p(ad7879_remove),
845 .suspend = ad7879_suspend,
846 .resume = ad7879_resume,
847 .id_table = ad7879_id,
848};
849
850static int __init ad7879_init(void)
851{
852 return i2c_add_driver(&ad7879_driver);
853}
854module_init(ad7879_init);
855
856static void __exit ad7879_exit(void)
857{
858 i2c_del_driver(&ad7879_driver);
859} 608}
860module_exit(ad7879_exit); 609EXPORT_SYMBOL(ad7879_remove);
861#endif
862 610
863MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>"); 611MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
864MODULE_DESCRIPTION("AD7879(-1) touchscreen Driver"); 612MODULE_DESCRIPTION("AD7879(-1) touchscreen Driver");
865MODULE_LICENSE("GPL"); 613MODULE_LICENSE("GPL");
866MODULE_ALIAS("spi:ad7879");