]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/input/touchscreen/ad7879.c
Merge branch 'perf-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[net-next-2.6.git] / drivers / input / touchscreen / ad7879.c
CommitLineData
b4be468c 1/*
36fb2527 2 * Copyright (C) 2008-2009 Michael Hennerich, Analog Devices Inc.
b4be468c 3 *
36fb2527
MH
4 * Description: AD7879/AD7889 based touchscreen, and GPIO driver
5 * (I2C/SPI Interface)
b4be468c
MH
6 *
7 * Bugs: Enter bugs at http://blackfin.uclinux.org/
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 *
24 * History:
25 * Copyright (c) 2005 David Brownell
26 * Copyright (c) 2006 Nokia Corporation
27 * Various changes: Imre Deak <imre.deak@nokia.com>
28 *
29 * Using code from:
30 * - corgi_ts.c
31 * Copyright (C) 2004-2005 Richard Purdie
32 * - omap_ts.[hc], ads7846.h, ts_osk.c
33 * Copyright (C) 2002 MontaVista Software
34 * Copyright (C) 2004 Texas Instruments
35 * Copyright (C) 2005 Dirk Behme
36 * - ad7877.c
37 * Copyright (C) 2006-2008 Analog Devices Inc.
38 */
39
40#include <linux/device.h>
41#include <linux/init.h>
42#include <linux/delay.h>
43#include <linux/input.h>
44#include <linux/interrupt.h>
45#include <linux/irq.h>
46#include <linux/slab.h>
47#include <linux/workqueue.h>
48#include <linux/spi/spi.h>
49#include <linux/i2c.h>
ec51b7f5 50#include <linux/gpio.h>
b4be468c
MH
51
52#include <linux/spi/ad7879.h>
53
54#define AD7879_REG_ZEROS 0
55#define AD7879_REG_CTRL1 1
56#define AD7879_REG_CTRL2 2
57#define AD7879_REG_CTRL3 3
58#define AD7879_REG_AUX1HIGH 4
59#define AD7879_REG_AUX1LOW 5
60#define AD7879_REG_TEMP1HIGH 6
61#define AD7879_REG_TEMP1LOW 7
62#define AD7879_REG_XPLUS 8
63#define AD7879_REG_YPLUS 9
64#define AD7879_REG_Z1 10
65#define AD7879_REG_Z2 11
66#define AD7879_REG_AUXVBAT 12
67#define AD7879_REG_TEMP 13
68#define AD7879_REG_REVID 14
69
70/* Control REG 1 */
71#define AD7879_TMR(x) ((x & 0xFF) << 0)
72#define AD7879_ACQ(x) ((x & 0x3) << 8)
73#define AD7879_MODE_NOC (0 << 10) /* Do not convert */
74#define AD7879_MODE_SCC (1 << 10) /* Single channel conversion */
75#define AD7879_MODE_SEQ0 (2 << 10) /* Sequence 0 in Slave Mode */
76#define AD7879_MODE_SEQ1 (3 << 10) /* Sequence 1 in Master Mode */
77#define AD7879_MODE_INT (1 << 15) /* PENIRQ disabled INT enabled */
78
79/* Control REG 2 */
80#define AD7879_FCD(x) ((x & 0x3) << 0)
81#define AD7879_RESET (1 << 4)
82#define AD7879_MFS(x) ((x & 0x3) << 5)
83#define AD7879_AVG(x) ((x & 0x3) << 7)
84#define AD7879_SER (1 << 9) /* non-differential */
85#define AD7879_DFR (0 << 9) /* differential */
86#define AD7879_GPIOPOL (1 << 10)
87#define AD7879_GPIODIR (1 << 11)
88#define AD7879_GPIO_DATA (1 << 12)
89#define AD7879_GPIO_EN (1 << 13)
90#define AD7879_PM(x) ((x & 0x3) << 14)
91#define AD7879_PM_SHUTDOWN (0)
92#define AD7879_PM_DYN (1)
93#define AD7879_PM_FULLON (2)
94
95/* Control REG 3 */
96#define AD7879_TEMPMASK_BIT (1<<15)
97#define AD7879_AUXVBATMASK_BIT (1<<14)
98#define AD7879_INTMODE_BIT (1<<13)
99#define AD7879_GPIOALERTMASK_BIT (1<<12)
100#define AD7879_AUXLOW_BIT (1<<11)
101#define AD7879_AUXHIGH_BIT (1<<10)
102#define AD7879_TEMPLOW_BIT (1<<9)
103#define AD7879_TEMPHIGH_BIT (1<<8)
104#define AD7879_YPLUS_BIT (1<<7)
105#define AD7879_XPLUS_BIT (1<<6)
106#define AD7879_Z1_BIT (1<<5)
107#define AD7879_Z2_BIT (1<<4)
108#define AD7879_AUX_BIT (1<<3)
109#define AD7879_VBAT_BIT (1<<2)
110#define AD7879_TEMP_BIT (1<<1)
111
112enum {
113 AD7879_SEQ_XPOS = 0,
114 AD7879_SEQ_YPOS = 1,
115 AD7879_SEQ_Z1 = 2,
116 AD7879_SEQ_Z2 = 3,
117 AD7879_NR_SENSE = 4,
118};
119
120#define MAX_12BIT ((1<<12)-1)
121#define TS_PEN_UP_TIMEOUT msecs_to_jiffies(50)
122
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 {
132 bus_device *bus;
133 struct input_dev *input;
134 struct work_struct work;
135 struct timer_list timer;
ec51b7f5
MH
136#ifdef CONFIG_GPIOLIB
137 struct gpio_chip gc;
138#endif
b4be468c
MH
139 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
147 u16 conversion_data[AD7879_NR_SENSE];
148 char phys[32];
149 u8 first_conversion_delay;
150 u8 acquisition_time;
151 u8 averaging;
152 u8 pen_down_acc_interval;
153 u8 median;
154 u16 x_plate_ohms;
155 u16 pressure_max;
b4be468c
MH
156 u16 cmd_crtl1;
157 u16 cmd_crtl2;
158 u16 cmd_crtl3;
b4be468c
MH
159};
160
161static int ad7879_read(bus_device *, u8);
162static int ad7879_write(bus_device *, u8, u16);
163static void ad7879_collect(struct ad7879 *);
164
165static void ad7879_report(struct ad7879 *ts)
166{
167 struct input_dev *input_dev = ts->input;
168 unsigned Rt;
169 u16 x, y, z1, z2;
170
171 x = ts->conversion_data[AD7879_SEQ_XPOS] & MAX_12BIT;
172 y = ts->conversion_data[AD7879_SEQ_YPOS] & MAX_12BIT;
173 z1 = ts->conversion_data[AD7879_SEQ_Z1] & MAX_12BIT;
174 z2 = ts->conversion_data[AD7879_SEQ_Z2] & MAX_12BIT;
175
176 /*
177 * The samples processed here are already preprocessed by the AD7879.
178 * The preprocessing function consists of a median and an averaging filter.
179 * The combination of these two techniques provides a robust solution,
180 * discarding the spurious noise in the signal and keeping only the data of interest.
181 * The size of both filters is programmable. (dev.platform_data, see linux/spi/ad7879.h)
182 * Other user-programmable conversion controls include variable acquisition time,
183 * and first conversion delay. Up to 16 averages can be taken per conversion.
184 */
185
186 if (likely(x && z1)) {
187 /* compute touch pressure resistance using equation #1 */
188 Rt = (z2 - z1) * x * ts->x_plate_ohms;
189 Rt /= z1;
190 Rt = (Rt + 2047) >> 12;
191
192 input_report_abs(input_dev, ABS_X, x);
193 input_report_abs(input_dev, ABS_Y, y);
194 input_report_abs(input_dev, ABS_PRESSURE, Rt);
195 input_sync(input_dev);
196 }
197}
198
199static void ad7879_work(struct work_struct *work)
200{
201 struct ad7879 *ts = container_of(work, struct ad7879, work);
202
203 /* use keventd context to read the result registers */
204 ad7879_collect(ts);
205 ad7879_report(ts);
206 mod_timer(&ts->timer, jiffies + TS_PEN_UP_TIMEOUT);
207}
208
209static void ad7879_ts_event_release(struct ad7879 *ts)
210{
211 struct input_dev *input_dev = ts->input;
212
213 input_report_abs(input_dev, ABS_PRESSURE, 0);
214 input_sync(input_dev);
215}
216
217static void ad7879_timer(unsigned long handle)
218{
219 struct ad7879 *ts = (void *)handle;
220
221 ad7879_ts_event_release(ts);
222}
223
224static irqreturn_t ad7879_irq(int irq, void *handle)
225{
226 struct ad7879 *ts = handle;
227
228 /* The repeated conversion sequencer controlled by TMR kicked off too fast.
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
233 if (!work_pending(&ts->work))
234 schedule_work(&ts->work);
235
236 return IRQ_HANDLED;
237}
238
239static void ad7879_setup(struct ad7879 *ts)
240{
b4be468c
MH
241 ad7879_write(ts->bus, AD7879_REG_CTRL2, ts->cmd_crtl2);
242 ad7879_write(ts->bus, AD7879_REG_CTRL3, ts->cmd_crtl3);
243 ad7879_write(ts->bus, AD7879_REG_CTRL1, ts->cmd_crtl1);
244}
245
246static void ad7879_disable(struct ad7879 *ts)
247{
248 mutex_lock(&ts->mutex);
249
250 if (!ts->disabled) {
251
252 ts->disabled = 1;
253 disable_irq(ts->bus->irq);
254
255 cancel_work_sync(&ts->work);
256
257 if (del_timer_sync(&ts->timer))
258 ad7879_ts_event_release(ts);
259
260 ad7879_write(ts->bus, AD7879_REG_CTRL2,
261 AD7879_PM(AD7879_PM_SHUTDOWN));
262 }
263
264 mutex_unlock(&ts->mutex);
265}
266
267static void ad7879_enable(struct ad7879 *ts)
268{
269 mutex_lock(&ts->mutex);
270
271 if (ts->disabled) {
272 ad7879_setup(ts);
273 ts->disabled = 0;
274 enable_irq(ts->bus->irq);
275 }
276
277 mutex_unlock(&ts->mutex);
278}
279
280static ssize_t ad7879_disable_show(struct device *dev,
281 struct device_attribute *attr, char *buf)
282{
283 struct ad7879 *ts = dev_get_drvdata(dev);
284
285 return sprintf(buf, "%u\n", ts->disabled);
286}
287
288static ssize_t ad7879_disable_store(struct device *dev,
289 struct device_attribute *attr,
290 const char *buf, size_t count)
291{
292 struct ad7879 *ts = dev_get_drvdata(dev);
293 unsigned long val;
294 int error;
295
296 error = strict_strtoul(buf, 10, &val);
297 if (error)
298 return error;
299
300 if (val)
301 ad7879_disable(ts);
302 else
303 ad7879_enable(ts);
304
305 return count;
306}
307
308static DEVICE_ATTR(disable, 0664, ad7879_disable_show, ad7879_disable_store);
309
ec51b7f5
MH
310static struct attribute *ad7879_attributes[] = {
311 &dev_attr_disable.attr,
312 NULL
313};
314
315static const struct attribute_group ad7879_attr_group = {
316 .attrs = ad7879_attributes,
317};
318
319#ifdef CONFIG_GPIOLIB
320static int ad7879_gpio_direction_input(struct gpio_chip *chip,
321 unsigned gpio)
b4be468c 322{
ec51b7f5
MH
323 struct ad7879 *ts = container_of(chip, struct ad7879, gc);
324 int err;
b4be468c 325
ec51b7f5
MH
326 mutex_lock(&ts->mutex);
327 ts->cmd_crtl2 |= AD7879_GPIO_EN | AD7879_GPIODIR | AD7879_GPIOPOL;
328 err = ad7879_write(ts->bus, AD7879_REG_CTRL2, ts->cmd_crtl2);
329 mutex_unlock(&ts->mutex);
330
331 return err;
b4be468c
MH
332}
333
ec51b7f5
MH
334static int ad7879_gpio_direction_output(struct gpio_chip *chip,
335 unsigned gpio, int level)
b4be468c 336{
ec51b7f5
MH
337 struct ad7879 *ts = container_of(chip, struct ad7879, gc);
338 int err;
b4be468c 339
ec51b7f5
MH
340 mutex_lock(&ts->mutex);
341 ts->cmd_crtl2 &= ~AD7879_GPIODIR;
342 ts->cmd_crtl2 |= AD7879_GPIO_EN | AD7879_GPIOPOL;
343 if (level)
344 ts->cmd_crtl2 |= AD7879_GPIO_DATA;
345 else
346 ts->cmd_crtl2 &= ~AD7879_GPIO_DATA;
347
348 err = ad7879_write(ts->bus, AD7879_REG_CTRL2, ts->cmd_crtl2);
349 mutex_unlock(&ts->mutex);
350
351 return err;
352}
353
354static int ad7879_gpio_get_value(struct gpio_chip *chip, unsigned gpio)
355{
356 struct ad7879 *ts = container_of(chip, struct ad7879, gc);
357 u16 val;
b4be468c
MH
358
359 mutex_lock(&ts->mutex);
ec51b7f5 360 val = ad7879_read(ts->bus, AD7879_REG_CTRL2);
b4be468c
MH
361 mutex_unlock(&ts->mutex);
362
ec51b7f5 363 return !!(val & AD7879_GPIO_DATA);
b4be468c
MH
364}
365
ec51b7f5
MH
366static void ad7879_gpio_set_value(struct gpio_chip *chip,
367 unsigned gpio, int value)
368{
369 struct ad7879 *ts = container_of(chip, struct ad7879, gc);
b4be468c 370
ec51b7f5
MH
371 mutex_lock(&ts->mutex);
372 if (value)
373 ts->cmd_crtl2 |= AD7879_GPIO_DATA;
374 else
375 ts->cmd_crtl2 &= ~AD7879_GPIO_DATA;
b4be468c 376
ec51b7f5
MH
377 ad7879_write(ts->bus, AD7879_REG_CTRL2, ts->cmd_crtl2);
378 mutex_unlock(&ts->mutex);
379}
380
381static int __devinit ad7879_gpio_add(struct device *dev)
382{
383 struct ad7879 *ts = dev_get_drvdata(dev);
384 struct ad7879_platform_data *pdata = dev->platform_data;
385 int ret = 0;
386
387 if (pdata->gpio_export) {
388 ts->gc.direction_input = ad7879_gpio_direction_input;
389 ts->gc.direction_output = ad7879_gpio_direction_output;
390 ts->gc.get = ad7879_gpio_get_value;
391 ts->gc.set = ad7879_gpio_set_value;
392 ts->gc.can_sleep = 1;
393 ts->gc.base = pdata->gpio_base;
394 ts->gc.ngpio = 1;
395 ts->gc.label = "AD7879-GPIO";
396 ts->gc.owner = THIS_MODULE;
397 ts->gc.dev = dev;
398
399 ret = gpiochip_add(&ts->gc);
400 if (ret)
401 dev_err(dev, "failed to register gpio %d\n",
402 ts->gc.base);
403 }
404
405 return ret;
406}
407
408/*
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{
415 struct ad7879 *ts = dev_get_drvdata(dev);
416 struct ad7879_platform_data *pdata = dev->platform_data;
417 int ret;
418
419 if (pdata->gpio_export) {
420 ret = gpiochip_remove(&ts->gc);
421 if (ret)
422 dev_err(dev, "failed to remove gpio %d\n",
423 ts->gc.base);
424 }
425}
426#else
427static inline int ad7879_gpio_add(struct device *dev)
428{
429 return 0;
430}
431
432static inline void ad7879_gpio_remove(struct device *dev)
433{
434}
435#endif
b4be468c
MH
436
437static int __devinit ad7879_construct(bus_device *bus, struct ad7879 *ts)
438{
439 struct input_dev *input_dev;
440 struct ad7879_platform_data *pdata = bus->dev.platform_data;
441 int err;
442 u16 revid;
443
444 if (!bus->irq) {
445 dev_err(&bus->dev, "no IRQ?\n");
446 return -ENODEV;
447 }
448
449 if (!pdata) {
450 dev_err(&bus->dev, "no platform data?\n");
451 return -ENODEV;
452 }
453
454 input_dev = input_allocate_device();
455 if (!input_dev)
456 return -ENOMEM;
457
458 ts->input = input_dev;
459
460 setup_timer(&ts->timer, ad7879_timer, (unsigned long) ts);
461 INIT_WORK(&ts->work, ad7879_work);
462 mutex_init(&ts->mutex);
463
464 ts->x_plate_ohms = pdata->x_plate_ohms ? : 400;
465 ts->pressure_max = pdata->pressure_max ? : ~0;
466
467 ts->first_conversion_delay = pdata->first_conversion_delay;
468 ts->acquisition_time = pdata->acquisition_time;
469 ts->averaging = pdata->averaging;
470 ts->pen_down_acc_interval = pdata->pen_down_acc_interval;
471 ts->median = pdata->median;
472
b4be468c
MH
473 snprintf(ts->phys, sizeof(ts->phys), "%s/input0", dev_name(&bus->dev));
474
475 input_dev->name = "AD7879 Touchscreen";
476 input_dev->phys = ts->phys;
477 input_dev->dev.parent = &bus->dev;
478
479 __set_bit(EV_ABS, input_dev->evbit);
480 __set_bit(ABS_X, input_dev->absbit);
481 __set_bit(ABS_Y, input_dev->absbit);
482 __set_bit(ABS_PRESSURE, input_dev->absbit);
483
484 input_set_abs_params(input_dev, ABS_X,
485 pdata->x_min ? : 0,
486 pdata->x_max ? : MAX_12BIT,
487 0, 0);
488 input_set_abs_params(input_dev, ABS_Y,
489 pdata->y_min ? : 0,
490 pdata->y_max ? : MAX_12BIT,
491 0, 0);
492 input_set_abs_params(input_dev, ABS_PRESSURE,
493 pdata->pressure_min, pdata->pressure_max, 0, 0);
494
495 err = ad7879_write(bus, AD7879_REG_CTRL2, AD7879_RESET);
496
497 if (err < 0) {
498 dev_err(&bus->dev, "Failed to write %s\n", input_dev->name);
499 goto err_free_mem;
500 }
501
502 revid = ad7879_read(bus, AD7879_REG_REVID);
503
504 if ((revid & 0xFF) != AD7879_DEVID) {
505 dev_err(&bus->dev, "Failed to probe %s\n", input_dev->name);
506 err = -ENODEV;
507 goto err_free_mem;
508 }
509
ec51b7f5
MH
510 ts->cmd_crtl3 = AD7879_YPLUS_BIT |
511 AD7879_XPLUS_BIT |
512 AD7879_Z2_BIT |
513 AD7879_Z1_BIT |
514 AD7879_TEMPMASK_BIT |
515 AD7879_AUXVBATMASK_BIT |
516 AD7879_GPIOALERTMASK_BIT;
517
518 ts->cmd_crtl2 = AD7879_PM(AD7879_PM_DYN) | AD7879_DFR |
519 AD7879_AVG(ts->averaging) |
520 AD7879_MFS(ts->median) |
521 AD7879_FCD(ts->first_conversion_delay);
522
523 ts->cmd_crtl1 = AD7879_MODE_INT | AD7879_MODE_SEQ1 |
524 AD7879_ACQ(ts->acquisition_time) |
525 AD7879_TMR(ts->pen_down_acc_interval);
526
b4be468c
MH
527 ad7879_setup(ts);
528
529 err = request_irq(bus->irq, ad7879_irq,
0bc69ce6 530 IRQF_TRIGGER_FALLING, bus->dev.driver->name, ts);
b4be468c
MH
531
532 if (err) {
533 dev_err(&bus->dev, "irq %d busy?\n", bus->irq);
534 goto err_free_mem;
535 }
536
537 err = sysfs_create_group(&bus->dev.kobj, &ad7879_attr_group);
538 if (err)
539 goto err_free_irq;
540
ec51b7f5 541 err = ad7879_gpio_add(&bus->dev);
b4be468c
MH
542 if (err)
543 goto err_remove_attr;
544
ec51b7f5
MH
545 err = input_register_device(input_dev);
546 if (err)
547 goto err_remove_gpio;
548
b4be468c
MH
549 dev_info(&bus->dev, "Rev.%d touchscreen, irq %d\n",
550 revid >> 8, bus->irq);
551
552 return 0;
553
ec51b7f5
MH
554err_remove_gpio:
555 ad7879_gpio_remove(&bus->dev);
b4be468c
MH
556err_remove_attr:
557 sysfs_remove_group(&bus->dev.kobj, &ad7879_attr_group);
558err_free_irq:
559 free_irq(bus->irq, ts);
560err_free_mem:
561 input_free_device(input_dev);
562
563 return err;
564}
565
566static int __devexit ad7879_destroy(bus_device *bus, struct ad7879 *ts)
567{
ec51b7f5 568 ad7879_gpio_remove(&bus->dev);
b4be468c
MH
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
4fa5757a 733 return error;
b4be468c
MH
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);
743
744 return 0;
745}
746
747static struct spi_driver ad7879_driver = {
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{
778 return swab16(i2c_smbus_read_word_data(client, reg));
779}
780
781static int ad7879_write(struct i2c_client *client, u8 reg, u16 val)
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);
fbae3fb1 815 if (error)
b4be468c 816 kfree(ts);
b4be468c 817
4fa5757a 818 return error;
b4be468c
MH
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);
b4be468c
MH
826 kfree(ts);
827
828 return 0;
829}
830
831static const struct i2c_device_id ad7879_id[] = {
832 { "ad7879", 0 },
36fb2527 833 { "ad7889", 0 },
b4be468c
MH
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}
860module_exit(ad7879_exit);
861#endif
862
863MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
864MODULE_DESCRIPTION("AD7879(-1) touchscreen Driver");
865MODULE_LICENSE("GPL");
e0626e38 866MODULE_ALIAS("spi:ad7879");