24 #include <linux/device.h>
27 #include <linux/input.h>
30 #include <linux/slab.h>
32 #include <linux/i2c.h>
36 #include <linux/module.h>
39 #define AD7879_REG_ZEROS 0
40 #define AD7879_REG_CTRL1 1
41 #define AD7879_REG_CTRL2 2
42 #define AD7879_REG_CTRL3 3
43 #define AD7879_REG_AUX1HIGH 4
44 #define AD7879_REG_AUX1LOW 5
45 #define AD7879_REG_TEMP1HIGH 6
46 #define AD7879_REG_TEMP1LOW 7
47 #define AD7879_REG_XPLUS 8
48 #define AD7879_REG_YPLUS 9
49 #define AD7879_REG_Z1 10
50 #define AD7879_REG_Z2 11
51 #define AD7879_REG_AUXVBAT 12
52 #define AD7879_REG_TEMP 13
53 #define AD7879_REG_REVID 14
56 #define AD7879_TMR(x) ((x & 0xFF) << 0)
57 #define AD7879_ACQ(x) ((x & 0x3) << 8)
58 #define AD7879_MODE_NOC (0 << 10)
59 #define AD7879_MODE_SCC (1 << 10)
60 #define AD7879_MODE_SEQ0 (2 << 10)
61 #define AD7879_MODE_SEQ1 (3 << 10)
62 #define AD7879_MODE_INT (1 << 15)
65 #define AD7879_FCD(x) ((x & 0x3) << 0)
66 #define AD7879_RESET (1 << 4)
67 #define AD7879_MFS(x) ((x & 0x3) << 5)
68 #define AD7879_AVG(x) ((x & 0x3) << 7)
69 #define AD7879_SER (1 << 9)
70 #define AD7879_DFR (0 << 9)
71 #define AD7879_GPIOPOL (1 << 10)
72 #define AD7879_GPIODIR (1 << 11)
73 #define AD7879_GPIO_DATA (1 << 12)
74 #define AD7879_GPIO_EN (1 << 13)
75 #define AD7879_PM(x) ((x & 0x3) << 14)
76 #define AD7879_PM_SHUTDOWN (0)
77 #define AD7879_PM_DYN (1)
78 #define AD7879_PM_FULLON (2)
81 #define AD7879_TEMPMASK_BIT (1<<15)
82 #define AD7879_AUXVBATMASK_BIT (1<<14)
83 #define AD7879_INTMODE_BIT (1<<13)
84 #define AD7879_GPIOALERTMASK_BIT (1<<12)
85 #define AD7879_AUXLOW_BIT (1<<11)
86 #define AD7879_AUXHIGH_BIT (1<<10)
87 #define AD7879_TEMPLOW_BIT (1<<9)
88 #define AD7879_TEMPHIGH_BIT (1<<8)
89 #define AD7879_YPLUS_BIT (1<<7)
90 #define AD7879_XPLUS_BIT (1<<6)
91 #define AD7879_Z1_BIT (1<<5)
92 #define AD7879_Z2_BIT (1<<4)
93 #define AD7879_AUX_BIT (1<<3)
94 #define AD7879_VBAT_BIT (1<<2)
95 #define AD7879_TEMP_BIT (1<<1)
105 #define MAX_12BIT ((1<<12)-1)
106 #define TS_PEN_UP_TIMEOUT msecs_to_jiffies(50)
114 #ifdef CONFIG_GPIOLIB
141 return ts->
bops->read(ts->
dev, reg);
146 return ts->
bops->multi_read(ts->
dev, first_reg, count, buf);
151 return ts->
bops->write(ts->
dev, reg, val);
154 static int ad7879_report(
struct ad7879 *ts)
156 struct input_dev *input_dev = ts->
input;
184 Rt = (Rt + 2047) >> 12;
199 if (timer_pending(&ts->
timer)) {
201 input_report_key(input_dev,
BTN_TOUCH, 1);
202 input_report_abs(input_dev,
ABS_X, ts->
x);
203 input_report_abs(input_dev,
ABS_Y, ts->
y);
205 input_sync(input_dev);
218 static void ad7879_ts_event_release(
struct ad7879 *ts)
220 struct input_dev *input_dev = ts->
input;
223 input_report_key(input_dev,
BTN_TOUCH, 0);
224 input_sync(input_dev);
227 static void ad7879_timer(
unsigned long handle)
229 struct ad7879 *ts = (
void *)handle;
231 ad7879_ts_event_release(ts);
240 if (!ad7879_report(ts))
246 static void __ad7879_enable(
struct ad7879 *ts)
255 static void __ad7879_disable(
struct ad7879 *ts)
262 ad7879_ts_event_release(ts);
268 static int ad7879_open(
struct input_dev *
input)
270 struct ad7879 *ts = input_get_drvdata(input);
279 static void ad7879_close(
struct input_dev* input)
281 struct ad7879 *ts = input_get_drvdata(input);
285 __ad7879_disable(ts);
288 #ifdef CONFIG_PM_SLEEP
289 static int ad7879_suspend(
struct device *
dev)
296 __ad7879_disable(ts);
305 static int ad7879_resume(
struct device *dev)
336 __ad7879_disable(ts);
355 const char *buf,
size_t count)
365 ad7879_toggle(ts, val);
370 static DEVICE_ATTR(disable, 0664, ad7879_disable_show, ad7879_disable_store);
372 static struct attribute *ad7879_attributes[] = {
373 &dev_attr_disable.attr,
378 .attrs = ad7879_attributes,
381 #ifdef CONFIG_GPIOLIB
382 static int ad7879_gpio_direction_input(
struct gpio_chip *
chip,
396 static int ad7879_gpio_direction_output(
struct gpio_chip *
chip,
416 static int ad7879_gpio_get_value(
struct gpio_chip *chip,
unsigned gpio)
428 static void ad7879_gpio_set_value(
struct gpio_chip *chip,
429 unsigned gpio,
int value)
443 static int ad7879_gpio_add(
struct ad7879 *ts,
451 ts->gc.direction_input = ad7879_gpio_direction_input;
452 ts->gc.direction_output = ad7879_gpio_direction_output;
453 ts->gc.get = ad7879_gpio_get_value;
454 ts->gc.set = ad7879_gpio_set_value;
455 ts->gc.can_sleep = 1;
458 ts->gc.label =
"AD7879-GPIO";
464 dev_err(ts->
dev,
"failed to register gpio %d\n",
471 static void ad7879_gpio_remove(
struct ad7879 *ts)
479 dev_err(ts->
dev,
"failed to remove gpio %d\n",
484 static inline int ad7879_gpio_add(
struct ad7879 *ts,
490 static inline void ad7879_gpio_remove(
struct ad7879 *ts)
500 struct input_dev *input_dev;
511 dev_err(dev,
"no platform data?\n");
517 input_dev = input_allocate_device();
518 if (!ts || !input_dev) {
525 ts->
input = input_dev;
538 ts->median = pdata->
median;
540 snprintf(ts->phys,
sizeof(ts->phys),
"%s/input0", dev_name(dev));
542 input_dev->name =
"AD7879 Touchscreen";
543 input_dev->phys = ts->phys;
544 input_dev->dev.parent =
dev;
545 input_dev->id.bustype = bops->
bustype;
547 input_dev->open = ad7879_open;
548 input_dev->close = ad7879_close;
550 input_set_drvdata(input_dev, ts);
560 input_set_abs_params(input_dev,
ABS_X,
564 input_set_abs_params(input_dev,
ABS_Y,
573 dev_err(dev,
"Failed to write %s\n", input_dev->
name);
578 input_dev->id.product = (revid & 0xff);
579 input_dev->id.version = revid >> 8;
580 if (input_dev->id.product != devid) {
581 dev_err(dev,
"Failed to probe %s (%x vs %x)\n",
582 input_dev->
name, devid, revid);
608 dev_err(dev,
"irq %d busy?\n", ts->irq);
612 __ad7879_disable(ts);
618 err = ad7879_gpio_add(ts, pdata);
620 goto err_remove_attr;
622 err = input_register_device(input_dev);
624 goto err_remove_gpio;
629 ad7879_gpio_remove(ts);
635 input_free_device(input_dev);
644 ad7879_gpio_remove(ts);
647 input_unregister_device(ts->
input);