| 1 | |
| 2 | |
| 3 | * Copyright (C) 2006-2008 Michael Hennerich, Analog Devices Inc.
|
| 4 | *
|
| 5 | * Description: AD7877 based touchscreen, sensor (ADCs), DAC and GPIO driver
|
| 6 | * Based on: ads7846.c
|
| 7 | *
|
| 8 | * Bugs: Enter bugs at http://blackfin.uclinux.org/
|
| 9 | *
|
| 10 | * History:
|
| 11 | * Copyright (c) 2005 David Brownell
|
| 12 | * Copyright (c) 2006 Nokia Corporation
|
| 13 | * Various changes: Imre Deak <[email protected]>
|
| 14 | *
|
| 15 | * Using code from:
|
| 16 | * - corgi_ts.c
|
| 17 | * Copyright (C) 2004-2005 Richard Purdie
|
| 18 | * - omap_ts.[hc], ads7846.h, ts_osk.c
|
| 19 | * Copyright (C) 2002 MontaVista Software
|
| 20 | * Copyright (C) 2004 Texas Instruments
|
| 21 | * Copyright (C) 2005 Dirk Behme
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 | #include <linux/device.h>
|
| 26 | #include <linux/delay.h>
|
| 27 | #include <linux/input.h>
|
| 28 | #include <linux/interrupt.h>
|
| 29 | #include <linux/pm.h>
|
| 30 | #include <linux/slab.h>
|
| 31 | #include <linux/spi/spi.h>
|
| 32 | #include <linux/spi/ad7877.h>
|
| 33 | #include <linux/module.h>
|
| 34 | #include <asm/irq.h>
|
| 35 |
|
| 36 | #define TS_PEN_UP_TIMEOUT msecs_to_jiffies(100)
|
| 37 |
|
| 38 | #define MAX_SPI_FREQ_HZ 20000000
|
| 39 | #define MAX_12BIT ((1<<12)-1)
|
| 40 |
|
| 41 | #define AD7877_REG_ZEROS 0
|
| 42 | #define AD7877_REG_CTRL1 1
|
| 43 | #define AD7877_REG_CTRL2 2
|
| 44 | #define AD7877_REG_ALERT 3
|
| 45 | #define AD7877_REG_AUX1HIGH 4
|
| 46 | #define AD7877_REG_AUX1LOW 5
|
| 47 | #define AD7877_REG_BAT1HIGH 6
|
| 48 | #define AD7877_REG_BAT1LOW 7
|
| 49 | #define AD7877_REG_BAT2HIGH 8
|
| 50 | #define AD7877_REG_BAT2LOW 9
|
| 51 | #define AD7877_REG_TEMP1HIGH 10
|
| 52 | #define AD7877_REG_TEMP1LOW 11
|
| 53 | #define AD7877_REG_SEQ0 12
|
| 54 | #define AD7877_REG_SEQ1 13
|
| 55 | #define AD7877_REG_DAC 14
|
| 56 | #define AD7877_REG_NONE1 15
|
| 57 | #define AD7877_REG_EXTWRITE 15
|
| 58 | #define AD7877_REG_XPLUS 16
|
| 59 | #define AD7877_REG_YPLUS 17
|
| 60 | #define AD7877_REG_Z2 18
|
| 61 | #define AD7877_REG_aux1 19
|
| 62 | #define AD7877_REG_aux2 20
|
| 63 | #define AD7877_REG_aux3 21
|
| 64 | #define AD7877_REG_bat1 22
|
| 65 | #define AD7877_REG_bat2 23
|
| 66 | #define AD7877_REG_temp1 24
|
| 67 | #define AD7877_REG_temp2 25
|
| 68 | #define AD7877_REG_Z1 26
|
| 69 | #define AD7877_REG_GPIOCTRL1 27
|
| 70 | #define AD7877_REG_GPIOCTRL2 28
|
| 71 | #define AD7877_REG_GPIODATA 29
|
| 72 | #define AD7877_REG_NONE2 30
|
| 73 | #define AD7877_REG_NONE3 31
|
| 74 |
|
| 75 | #define AD7877_SEQ_YPLUS_BIT (1<<11)
|
| 76 | #define AD7877_SEQ_XPLUS_BIT (1<<10)
|
| 77 | #define AD7877_SEQ_Z2_BIT (1<<9)
|
| 78 | #define AD7877_SEQ_AUX1_BIT (1<<8)
|
| 79 | #define AD7877_SEQ_AUX2_BIT (1<<7)
|
| 80 | #define AD7877_SEQ_AUX3_BIT (1<<6)
|
| 81 | #define AD7877_SEQ_BAT1_BIT (1<<5)
|
| 82 | #define AD7877_SEQ_BAT2_BIT (1<<4)
|
| 83 | #define AD7877_SEQ_TEMP1_BIT (1<<3)
|
| 84 | #define AD7877_SEQ_TEMP2_BIT (1<<2)
|
| 85 | #define AD7877_SEQ_Z1_BIT (1<<1)
|
| 86 |
|
| 87 | enum {
|
| 88 | AD7877_SEQ_YPOS = 0,
|
| 89 | AD7877_SEQ_XPOS = 1,
|
| 90 | AD7877_SEQ_Z2 = 2,
|
| 91 | AD7877_SEQ_AUX1 = 3,
|
| 92 | AD7877_SEQ_AUX2 = 4,
|
| 93 | AD7877_SEQ_AUX3 = 5,
|
| 94 | AD7877_SEQ_BAT1 = 6,
|
| 95 | AD7877_SEQ_BAT2 = 7,
|
| 96 | AD7877_SEQ_TEMP1 = 8,
|
| 97 | AD7877_SEQ_TEMP2 = 9,
|
| 98 | AD7877_SEQ_Z1 = 10,
|
| 99 | AD7877_NR_SENSE = 11,
|
| 100 | };
|
| 101 |
|
| 102 |
|
| 103 | #define AD7877_DAC_CONF 0x1
|
| 104 |
|
| 105 |
|
| 106 | #define AD7877_EXTW_GPIO_3_CONF 0x1C4
|
| 107 | #define AD7877_EXTW_GPIO_DATA 0x200
|
| 108 |
|
| 109 |
|
| 110 | #define AD7877_TMR(x) ((x & 0x3) << 0)
|
| 111 | #define AD7877_REF(x) ((x & 0x1) << 2)
|
| 112 | #define AD7877_POL(x) ((x & 0x1) << 3)
|
| 113 | #define AD7877_FCD(x) ((x & 0x3) << 4)
|
| 114 | #define AD7877_PM(x) ((x & 0x3) << 6)
|
| 115 | #define AD7877_ACQ(x) ((x & 0x3) << 8)
|
| 116 | #define AD7877_AVG(x) ((x & 0x3) << 10)
|
| 117 |
|
| 118 |
|
| 119 | #define AD7877_SER (1 << 11)
|
| 120 | #define AD7877_DFR (0 << 11)
|
| 121 |
|
| 122 | #define AD7877_MODE_NOC (0)
|
| 123 | #define AD7877_MODE_SCC (1)
|
| 124 | #define AD7877_MODE_SEQ0 (2)
|
| 125 | #define AD7877_MODE_SEQ1 (3)
|
| 126 |
|
| 127 | #define AD7877_CHANADD(x) ((x&0xF)<<7)
|
| 128 | #define AD7877_READADD(x) ((x)<<2)
|
| 129 | #define AD7877_WRITEADD(x) ((x)<<12)
|
| 130 |
|
| 131 | #define AD7877_READ_CHAN(x) (AD7877_WRITEADD(AD7877_REG_CTRL1) | AD7877_SER | \
|
| 132 | AD7877_MODE_SCC | AD7877_CHANADD(AD7877_REG_ ## x) | \
|
| 133 | AD7877_READADD(AD7877_REG_ ## x))
|
| 134 |
|
| 135 | #define AD7877_MM_SEQUENCE (AD7877_SEQ_YPLUS_BIT | AD7877_SEQ_XPLUS_BIT | \
|
| 136 | AD7877_SEQ_Z2_BIT | AD7877_SEQ_Z1_BIT)
|
| 137 |
|
| 138 | |
| 139 | * Non-touchscreen sensors only use single-ended conversions.
|
| 140 |
|
| 141 |
|
| 142 | struct ser_req {
|
| 143 | u16 reset;
|
| 144 | u16 ref_on;
|
| 145 | u16 command;
|
| 146 | struct spi_message msg;
|
| 147 | struct spi_transfer xfer[6];
|
| 148 |
|
| 149 | |
| 150 | * DMA (thus cache coherency maintenance) requires the
|
| 151 | * transfer buffers to live in their own cache lines.
|
| 152 |
|
| 153 | u16 sample ____cacheline_aligned;
|
| 154 | };
|
| 155 |
|
| 156 | struct ad7877 {
|
| 157 | struct input_dev *input;
|
| 158 | char phys[32];
|
| 159 |
|
| 160 | struct spi_device *spi;
|
| 161 | u16 model;
|
| 162 | u16 vref_delay_usecs;
|
| 163 | u16 x_plate_ohms;
|
| 164 | u16 pressure_max;
|
| 165 |
|
| 166 | u16 cmd_crtl1;
|
| 167 | u16 cmd_crtl2;
|
| 168 | u16 cmd_dummy;
|
| 169 | u16 dac;
|
| 170 |
|
| 171 | u8 stopacq_polarity;
|
| 172 | u8 first_conversion_delay;
|
| 173 | u8 acquisition_time;
|
| 174 | u8 averaging;
|
| 175 | u8 pen_down_acc_interval;
|
| 176 |
|
| 177 | struct spi_transfer xfer[AD7877_NR_SENSE + 2];
|
| 178 | struct spi_message msg;
|
| 179 |
|
| 180 | struct mutex mutex;
|
| 181 | bool disabled;
|
| 182 | bool gpio3;
|
| 183 | bool gpio4;
|
| 184 |
|
| 185 | spinlock_t lock;
|
| 186 | struct timer_list timer;
|
| 187 |
|
| 188 | |
| 189 | * DMA (thus cache coherency maintenance) requires the
|
| 190 | * transfer buffers to live in their own cache lines.
|
| 191 |
|
| 192 | u16 conversion_data[AD7877_NR_SENSE] ____cacheline_aligned;
|
| 193 | };
|
| 194 |
|
| 195 | static bool gpio3;
|
| 196 | module_param(gpio3, bool, 0);
|
| 197 | MODULE_PARM_DESC(gpio3, "If gpio3 is set to 1 AUX3 acts as GPIO3");
|
| 198 |
|
| 199 | static int ad7877_read(struct spi_device *spi, u16 reg)
|
| 200 | {
|
| 201 | struct ser_req *req;
|
| 202 | int status, ret;
|
| 203 |
|
| 204 | req = kzalloc_obj(*req);
|
| 205 | if (!req)
|
| 206 | return -ENOMEM;
|
| 207 |
|
| 208 | spi_message_init(&req->msg);
|
| 209 |
|
| 210 | req->command = (u16) (AD7877_WRITEADD(AD7877_REG_CTRL1) |
|
| 211 | AD7877_READADD(reg));
|
| 212 | req->xfer[0].tx_buf = &req->command;
|
| 213 | req->xfer[0].len = 2;
|
| 214 | req->xfer[0].cs_change = 1;
|
| 215 |
|
| 216 | req->xfer[1].rx_buf = &req->sample;
|
| 217 | req->xfer[1].len = 2;
|
| 218 |
|
| 219 | spi_message_add_tail(&req->xfer[0], &req->msg);
|
| 220 | spi_message_add_tail(&req->xfer[1], &req->msg);
|
| 221 |
|
| 222 | status = spi_sync(spi, &req->msg);
|
| 223 | ret = status ? : req->sample;
|
| 224 |
|
| 225 | kfree(req);
|
| 226 |
|
| 227 | return ret;
|
| 228 | }
|
| 229 |
|
| 230 | static int ad7877_write(struct spi_device *spi, u16 reg, u16 val)
|
| 231 | {
|
| 232 | struct ser_req *req;
|
| 233 | int status;
|
| 234 |
|
| 235 | req = kzalloc_obj(*req);
|
| 236 | if (!req)
|
| 237 | return -ENOMEM;
|
| 238 |
|
| 239 | spi_message_init(&req->msg);
|
| 240 |
|
| 241 | req->command = (u16) (AD7877_WRITEADD(reg) | (val & MAX_12BIT));
|
| 242 | req->xfer[0].tx_buf = &req->command;
|
| 243 | req->xfer[0].len = 2;
|
| 244 |
|
| 245 | spi_message_add_tail(&req->xfer[0], &req->msg);
|
| 246 |
|
| 247 | status = spi_sync(spi, &req->msg);
|
| 248 |
|
| 249 | kfree(req);
|
| 250 |
|
| 251 | return status;
|
| 252 | }
|
| 253 |
|
| 254 | static int ad7877_read_adc(struct spi_device *spi, unsigned command)
|
| 255 | {
|
| 256 | struct ad7877 *ts = spi_get_drvdata(spi);
|
| 257 | struct ser_req *req;
|
| 258 | int status;
|
| 259 | int sample;
|
| 260 | int i;
|
| 261 |
|
| 262 | req = kzalloc_obj(*req);
|
| 263 | if (!req)
|
| 264 | return -ENOMEM;
|
| 265 |
|
| 266 | spi_message_init(&req->msg);
|
| 267 |
|
| 268 |
|
| 269 | req->ref_on = AD7877_WRITEADD(AD7877_REG_CTRL2) |
|
| 270 | AD7877_POL(ts->stopacq_polarity) |
|
| 271 | AD7877_AVG(0) | AD7877_PM(2) | AD7877_TMR(0) |
|
| 272 | AD7877_ACQ(ts->acquisition_time) | AD7877_FCD(0);
|
| 273 |
|
| 274 | req->reset = AD7877_WRITEADD(AD7877_REG_CTRL1) | AD7877_MODE_NOC;
|
| 275 |
|
| 276 | req->command = (u16) command;
|
| 277 |
|
| 278 | req->xfer[0].tx_buf = &req->reset;
|
| 279 | req->xfer[0].len = 2;
|
| 280 | req->xfer[0].cs_change = 1;
|
| 281 |
|
| 282 | req->xfer[1].tx_buf = &req->ref_on;
|
| 283 | req->xfer[1].len = 2;
|
| 284 | req->xfer[1].delay.value = ts->vref_delay_usecs;
|
| 285 | req->xfer[1].delay.unit = SPI_DELAY_UNIT_USECS;
|
| 286 | req->xfer[1].cs_change = 1;
|
| 287 |
|
| 288 | req->xfer[2].tx_buf = &req->command;
|
| 289 | req->xfer[2].len = 2;
|
| 290 | req->xfer[2].delay.value = ts->vref_delay_usecs;
|
| 291 | req->xfer[2].delay.unit = SPI_DELAY_UNIT_USECS;
|
| 292 | req->xfer[2].cs_change = 1;
|
| 293 |
|
| 294 | req->xfer[3].rx_buf = &req->sample;
|
| 295 | req->xfer[3].len = 2;
|
| 296 | req->xfer[3].cs_change = 1;
|
| 297 |
|
| 298 | req->xfer[4].tx_buf = &ts->cmd_crtl2;
|
| 299 | req->xfer[4].len = 2;
|
| 300 | req->xfer[4].cs_change = 1;
|
| 301 |
|
| 302 | req->xfer[5].tx_buf = &ts->cmd_crtl1;
|
| 303 | req->xfer[5].len = 2;
|
| 304 |
|
| 305 | |
| 306 | * reading touchscreen state; disable penirq while sampling
|
| 307 |
|
| 308 | for (i = 0; i < 6; i++)
|
| 309 | spi_message_add_tail(&req->xfer[i], &req->msg);
|
| 310 |
|
| 311 | status = spi_sync(spi, &req->msg);
|
| 312 | sample = req->sample;
|
| 313 |
|
| 314 | kfree(req);
|
| 315 |
|
| 316 | return status ? : sample;
|
| 317 | }
|
| 318 |
|
| 319 | static int ad7877_process_data(struct ad7877 *ts)
|
| 320 | {
|
| 321 | struct input_dev *input_dev = ts->input;
|
| 322 | unsigned Rt;
|
| 323 | u16 x, y, z1, z2;
|
| 324 |
|
| 325 | x = ts->conversion_data[AD7877_SEQ_XPOS] & MAX_12BIT;
|
| 326 | y = ts->conversion_data[AD7877_SEQ_YPOS] & MAX_12BIT;
|
| 327 | z1 = ts->conversion_data[AD7877_SEQ_Z1] & MAX_12BIT;
|
| 328 | z2 = ts->conversion_data[AD7877_SEQ_Z2] & MAX_12BIT;
|
| 329 |
|
| 330 | |
| 331 | * The samples processed here are already preprocessed by the AD7877.
|
| 332 | * The preprocessing function consists of an averaging filter.
|
| 333 | * The combination of 'first conversion delay' and averaging provides a robust solution,
|
| 334 | * discarding the spurious noise in the signal and keeping only the data of interest.
|
| 335 | * The size of the averaging filter is programmable. (dev.platform_data, see linux/spi/ad7877.h)
|
| 336 | * Other user-programmable conversion controls include variable acquisition time,
|
| 337 | * and first conversion delay. Up to 16 averages can be taken per conversion.
|
| 338 |
|
| 339 |
|
| 340 | if (likely(x && z1)) {
|
| 341 |
|
| 342 | Rt = (z2 - z1) * x * ts->x_plate_ohms;
|
| 343 | Rt /= z1;
|
| 344 | Rt = (Rt + 2047) >> 12;
|
| 345 |
|
| 346 | |
| 347 | * Sample found inconsistent, pressure is beyond
|
| 348 | * the maximum. Don't report it to user space.
|
| 349 |
|
| 350 | if (Rt > ts->pressure_max)
|
| 351 | return -EINVAL;
|
| 352 |
|
| 353 | if (!timer_pending(&ts->timer))
|
| 354 | input_report_key(input_dev, BTN_TOUCH, 1);
|
| 355 |
|
| 356 | input_report_abs(input_dev, ABS_X, x);
|
| 357 | input_report_abs(input_dev, ABS_Y, y);
|
| 358 | input_report_abs(input_dev, ABS_PRESSURE, Rt);
|
| 359 | input_sync(input_dev);
|
| 360 |
|
| 361 | return 0;
|
| 362 | }
|
| 363 |
|
| 364 | return -EINVAL;
|
| 365 | }
|
| 366 |
|
| 367 | static inline void ad7877_ts_event_release(struct ad7877 *ts)
|
| 368 | {
|
| 369 | struct input_dev *input_dev = ts->input;
|
| 370 |
|
| 371 | input_report_abs(input_dev, ABS_PRESSURE, 0);
|
| 372 | input_report_key(input_dev, BTN_TOUCH, 0);
|
| 373 | input_sync(input_dev);
|
| 374 | }
|
| 375 |
|
| 376 | static void ad7877_timer(struct timer_list *t)
|
| 377 | {
|
| 378 | struct ad7877 *ts = timer_container_of(ts, t, timer);
|
| 379 |
|
| 380 | guard(spinlock_irqsave)(&ts->lock);
|
| 381 | ad7877_ts_event_release(ts);
|
| 382 | }
|
| 383 |
|
| 384 | static irqreturn_t ad7877_irq(int irq, void *handle)
|
| 385 | {
|
| 386 | struct ad7877 *ts = handle;
|
| 387 | int error;
|
| 388 |
|
| 389 | error = spi_sync(ts->spi, &ts->msg);
|
| 390 | if (error) {
|
| 391 | dev_err(&ts->spi->dev, "spi_sync --> %d\n", error);
|
| 392 | goto out;
|
| 393 | }
|
| 394 |
|
| 395 | scoped_guard(spinlock_irqsave, &ts->lock) {
|
| 396 | error = ad7877_process_data(ts);
|
| 397 | if (error)
|
| 398 | goto out;
|
| 399 |
|
| 400 | mod_timer(&ts->timer, jiffies + TS_PEN_UP_TIMEOUT);
|
| 401 | }
|
| 402 |
|
| 403 | out:
|
| 404 | return IRQ_HANDLED;
|
| 405 | }
|
| 406 |
|
| 407 | static void ad7877_disable(void *data)
|
| 408 | {
|
| 409 | struct ad7877 *ts = data;
|
| 410 |
|
| 411 | guard(mutex)(&ts->mutex);
|
| 412 |
|
| 413 | if (!ts->disabled) {
|
| 414 | ts->disabled = true;
|
| 415 | disable_irq(ts->spi->irq);
|
| 416 |
|
| 417 | if (timer_delete_sync(&ts->timer))
|
| 418 | ad7877_ts_event_release(ts);
|
| 419 | }
|
| 420 |
|
| 421 | |
| 422 | * We know the chip's in lowpower mode since we always
|
| 423 | * leave it that way after every request
|
| 424 |
|
| 425 | }
|
| 426 |
|
| 427 | static void ad7877_enable(struct ad7877 *ts)
|
| 428 | {
|
| 429 | guard(mutex)(&ts->mutex);
|
| 430 |
|
| 431 | if (ts->disabled) {
|
| 432 | ts->disabled = false;
|
| 433 | enable_irq(ts->spi->irq);
|
| 434 | }
|
| 435 | }
|
| 436 |
|
| 437 | #define SHOW(name) static ssize_t \
|
| 438 | name ## _show(struct device *dev, struct device_attribute *attr, char *buf) \
|
| 439 | { \
|
| 440 | struct ad7877 *ts = dev_get_drvdata(dev); \
|
| 441 | ssize_t v = ad7877_read_adc(ts->spi, \
|
| 442 | AD7877_READ_CHAN(name)); \
|
| 443 | if (v < 0) \
|
| 444 | return v; \
|
| 445 | return sprintf(buf, "%u\n", (unsigned) v); \
|
| 446 | } \
|
| 447 | static DEVICE_ATTR(name, S_IRUGO, name ## _show, NULL);
|
| 448 |
|
| 449 | SHOW(aux1)
|
| 450 | SHOW(aux2)
|
| 451 | SHOW(aux3)
|
| 452 | SHOW(bat1)
|
| 453 | SHOW(bat2)
|
| 454 | SHOW(temp1)
|
| 455 | SHOW(temp2)
|
| 456 |
|
| 457 | static ssize_t ad7877_disable_show(struct device *dev,
|
| 458 | struct device_attribute *attr, char *buf)
|
| 459 | {
|
| 460 | struct ad7877 *ts = dev_get_drvdata(dev);
|
| 461 |
|
| 462 | return sprintf(buf, "%u\n", ts->disabled);
|
| 463 | }
|
| 464 |
|
| 465 | static ssize_t ad7877_disable_store(struct device *dev,
|
| 466 | struct device_attribute *attr,
|
| 467 | const char *buf, size_t count)
|
| 468 | {
|
| 469 | struct ad7877 *ts = dev_get_drvdata(dev);
|
| 470 | unsigned int val;
|
| 471 | int error;
|
| 472 |
|
| 473 | error = kstrtouint(buf, 10, &val);
|
| 474 | if (error)
|
| 475 | return error;
|
| 476 |
|
| 477 | if (val)
|
| 478 | ad7877_disable(ts);
|
| 479 | else
|
| 480 | ad7877_enable(ts);
|
| 481 |
|
| 482 | return count;
|
| 483 | }
|
| 484 |
|
| 485 | static DEVICE_ATTR(disable, 0664, ad7877_disable_show, ad7877_disable_store);
|
| 486 |
|
| 487 | static ssize_t ad7877_dac_show(struct device *dev,
|
| 488 | struct device_attribute *attr, char *buf)
|
| 489 | {
|
| 490 | struct ad7877 *ts = dev_get_drvdata(dev);
|
| 491 |
|
| 492 | return sprintf(buf, "%u\n", ts->dac);
|
| 493 | }
|
| 494 |
|
| 495 | static ssize_t ad7877_dac_store(struct device *dev,
|
| 496 | struct device_attribute *attr,
|
| 497 | const char *buf, size_t count)
|
| 498 | {
|
| 499 | struct ad7877 *ts = dev_get_drvdata(dev);
|
| 500 | unsigned int val;
|
| 501 | int error;
|
| 502 |
|
| 503 | error = kstrtouint(buf, 10, &val);
|
| 504 | if (error)
|
| 505 | return error;
|
| 506 |
|
| 507 | guard(mutex)(&ts->mutex);
|
| 508 | ts->dac = val & 0xFF;
|
| 509 | ad7877_write(ts->spi, AD7877_REG_DAC, (ts->dac << 4) | AD7877_DAC_CONF);
|
| 510 |
|
| 511 | return count;
|
| 512 | }
|
| 513 |
|
| 514 | static DEVICE_ATTR(dac, 0664, ad7877_dac_show, ad7877_dac_store);
|
| 515 |
|
| 516 | static ssize_t ad7877_gpio3_show(struct device *dev,
|
| 517 | struct device_attribute *attr, char *buf)
|
| 518 | {
|
| 519 | struct ad7877 *ts = dev_get_drvdata(dev);
|
| 520 |
|
| 521 | return sprintf(buf, "%u\n", ts->gpio3);
|
| 522 | }
|
| 523 |
|
| 524 | static ssize_t ad7877_gpio3_store(struct device *dev,
|
| 525 | struct device_attribute *attr,
|
| 526 | const char *buf, size_t count)
|
| 527 | {
|
| 528 | struct ad7877 *ts = dev_get_drvdata(dev);
|
| 529 | unsigned int val;
|
| 530 | int error;
|
| 531 |
|
| 532 | error = kstrtouint(buf, 10, &val);
|
| 533 | if (error)
|
| 534 | return error;
|
| 535 |
|
| 536 | guard(mutex)(&ts->mutex);
|
| 537 | ts->gpio3 = !!val;
|
| 538 | ad7877_write(ts->spi, AD7877_REG_EXTWRITE, AD7877_EXTW_GPIO_DATA |
|
| 539 | (ts->gpio4 << 4) | (ts->gpio3 << 5));
|
| 540 |
|
| 541 | return count;
|
| 542 | }
|
| 543 |
|
| 544 | static DEVICE_ATTR(gpio3, 0664, ad7877_gpio3_show, ad7877_gpio3_store);
|
| 545 |
|
| 546 | static ssize_t ad7877_gpio4_show(struct device *dev,
|
| 547 | struct device_attribute *attr, char *buf)
|
| 548 | {
|
| 549 | struct ad7877 *ts = dev_get_drvdata(dev);
|
| 550 |
|
| 551 | return sprintf(buf, "%u\n", ts->gpio4);
|
| 552 | }
|
| 553 |
|
| 554 | static ssize_t ad7877_gpio4_store(struct device *dev,
|
| 555 | struct device_attribute *attr,
|
| 556 | const char *buf, size_t count)
|
| 557 | {
|
| 558 | struct ad7877 *ts = dev_get_drvdata(dev);
|
| 559 | unsigned int val;
|
| 560 | int error;
|
| 561 |
|
| 562 | error = kstrtouint(buf, 10, &val);
|
| 563 | if (error)
|
| 564 | return error;
|
| 565 |
|
| 566 | guard(mutex)(&ts->mutex);
|
| 567 | ts->gpio4 = !!val;
|
| 568 | ad7877_write(ts->spi, AD7877_REG_EXTWRITE, AD7877_EXTW_GPIO_DATA |
|
| 569 | (ts->gpio4 << 4) | (ts->gpio3 << 5));
|
| 570 |
|
| 571 | return count;
|
| 572 | }
|
| 573 |
|
| 574 | static DEVICE_ATTR(gpio4, 0664, ad7877_gpio4_show, ad7877_gpio4_store);
|
| 575 |
|
| 576 | static struct attribute *ad7877_attributes[] = {
|
| 577 | &dev_attr_temp1.attr,
|
| 578 | &dev_attr_temp2.attr,
|
| 579 | &dev_attr_aux1.attr,
|
| 580 | &dev_attr_aux2.attr,
|
| 581 | &dev_attr_aux3.attr,
|
| 582 | &dev_attr_bat1.attr,
|
| 583 | &dev_attr_bat2.attr,
|
| 584 | &dev_attr_disable.attr,
|
| 585 | &dev_attr_dac.attr,
|
| 586 | &dev_attr_gpio3.attr,
|
| 587 | &dev_attr_gpio4.attr,
|
| 588 | NULL
|
| 589 | };
|
| 590 |
|
| 591 | static umode_t ad7877_attr_is_visible(struct kobject *kobj,
|
| 592 | struct attribute *attr, int n)
|
| 593 | {
|
| 594 | umode_t mode = attr->mode;
|
| 595 |
|
| 596 | if (attr == &dev_attr_aux3.attr) {
|
| 597 | if (gpio3)
|
| 598 | mode = 0;
|
| 599 | } else if (attr == &dev_attr_gpio3.attr) {
|
| 600 | if (!gpio3)
|
| 601 | mode = 0;
|
| 602 | }
|
| 603 |
|
| 604 | return mode;
|
| 605 | }
|
| 606 |
|
| 607 | static const struct attribute_group ad7877_group = {
|
| 608 | .is_visible = ad7877_attr_is_visible,
|
| 609 | .attrs = ad7877_attributes,
|
| 610 | };
|
| 611 | __ATTRIBUTE_GROUPS(ad7877);
|
| 612 |
|
| 613 | static void ad7877_setup_ts_def_msg(struct spi_device *spi, struct ad7877 *ts)
|
| 614 | {
|
| 615 | struct spi_message *m;
|
| 616 | int i;
|
| 617 |
|
| 618 | ts->cmd_crtl2 = AD7877_WRITEADD(AD7877_REG_CTRL2) |
|
| 619 | AD7877_POL(ts->stopacq_polarity) |
|
| 620 | AD7877_AVG(ts->averaging) | AD7877_PM(1) |
|
| 621 | AD7877_TMR(ts->pen_down_acc_interval) |
|
| 622 | AD7877_ACQ(ts->acquisition_time) |
|
| 623 | AD7877_FCD(ts->first_conversion_delay);
|
| 624 |
|
| 625 | ad7877_write(spi, AD7877_REG_CTRL2, ts->cmd_crtl2);
|
| 626 |
|
| 627 | ts->cmd_crtl1 = AD7877_WRITEADD(AD7877_REG_CTRL1) |
|
| 628 | AD7877_READADD(AD7877_REG_XPLUS-1) |
|
| 629 | AD7877_MODE_SEQ1 | AD7877_DFR;
|
| 630 |
|
| 631 | ad7877_write(spi, AD7877_REG_CTRL1, ts->cmd_crtl1);
|
| 632 |
|
| 633 | ts->cmd_dummy = 0;
|
| 634 |
|
| 635 | m = &ts->msg;
|
| 636 |
|
| 637 | spi_message_init(m);
|
| 638 |
|
| 639 | m->context = ts;
|
| 640 |
|
| 641 | ts->xfer[0].tx_buf = &ts->cmd_crtl1;
|
| 642 | ts->xfer[0].len = 2;
|
| 643 | ts->xfer[0].cs_change = 1;
|
| 644 |
|
| 645 | spi_message_add_tail(&ts->xfer[0], m);
|
| 646 |
|
| 647 | ts->xfer[1].tx_buf = &ts->cmd_dummy;
|
| 648 | ts->xfer[1].len = 2;
|
| 649 | ts->xfer[1].cs_change = 1;
|
| 650 |
|
| 651 | spi_message_add_tail(&ts->xfer[1], m);
|
| 652 |
|
| 653 | for (i = 0; i < AD7877_NR_SENSE; i++) {
|
| 654 | ts->xfer[i + 2].rx_buf = &ts->conversion_data[AD7877_SEQ_YPOS + i];
|
| 655 | ts->xfer[i + 2].len = 2;
|
| 656 | if (i < (AD7877_NR_SENSE - 1))
|
| 657 | ts->xfer[i + 2].cs_change = 1;
|
| 658 | spi_message_add_tail(&ts->xfer[i + 2], m);
|
| 659 | }
|
| 660 | }
|
| 661 |
|
| 662 | static int ad7877_probe(struct spi_device *spi)
|
| 663 | {
|
| 664 | struct ad7877 *ts;
|
| 665 | struct input_dev *input_dev;
|
| 666 | struct ad7877_platform_data *pdata = dev_get_platdata(&spi->dev);
|
| 667 | int err;
|
| 668 | u16 verify;
|
| 669 |
|
| 670 | if (!spi->irq) {
|
| 671 | dev_dbg(&spi->dev, "no IRQ?\n");
|
| 672 | return -ENODEV;
|
| 673 | }
|
| 674 |
|
| 675 | if (!pdata) {
|
| 676 | dev_dbg(&spi->dev, "no platform data?\n");
|
| 677 | return -ENODEV;
|
| 678 | }
|
| 679 |
|
| 680 |
|
| 681 | if (spi->max_speed_hz > MAX_SPI_FREQ_HZ) {
|
| 682 | dev_dbg(&spi->dev, "SPI CLK %d Hz?\n",spi->max_speed_hz);
|
| 683 | return -EINVAL;
|
| 684 | }
|
| 685 |
|
| 686 | spi->bits_per_word = 16;
|
| 687 | err = spi_setup(spi);
|
| 688 | if (err) {
|
| 689 | dev_dbg(&spi->dev, "spi master doesn't support 16 bits/word\n");
|
| 690 | return err;
|
| 691 | }
|
| 692 |
|
| 693 | ts = devm_kzalloc(&spi->dev, sizeof(struct ad7877), GFP_KERNEL);
|
| 694 | if (!ts)
|
| 695 | return -ENOMEM;
|
| 696 |
|
| 697 | input_dev = devm_input_allocate_device(&spi->dev);
|
| 698 | if (!input_dev)
|
| 699 | return -ENOMEM;
|
| 700 |
|
| 701 | err = devm_add_action_or_reset(&spi->dev, ad7877_disable, ts);
|
| 702 | if (err)
|
| 703 | return err;
|
| 704 |
|
| 705 | spi_set_drvdata(spi, ts);
|
| 706 | ts->spi = spi;
|
| 707 | ts->input = input_dev;
|
| 708 |
|
| 709 | timer_setup(&ts->timer, ad7877_timer, 0);
|
| 710 | mutex_init(&ts->mutex);
|
| 711 | spin_lock_init(&ts->lock);
|
| 712 |
|
| 713 | ts->model = pdata->model ? : 7877;
|
| 714 | ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100;
|
| 715 | ts->x_plate_ohms = pdata->x_plate_ohms ? : 400;
|
| 716 | ts->pressure_max = pdata->pressure_max ? : ~0;
|
| 717 |
|
| 718 | ts->stopacq_polarity = pdata->stopacq_polarity;
|
| 719 | ts->first_conversion_delay = pdata->first_conversion_delay;
|
| 720 | ts->acquisition_time = pdata->acquisition_time;
|
| 721 | ts->averaging = pdata->averaging;
|
| 722 | ts->pen_down_acc_interval = pdata->pen_down_acc_interval;
|
| 723 |
|
| 724 | snprintf(ts->phys, sizeof(ts->phys), "%s/input0", dev_name(&spi->dev));
|
| 725 |
|
| 726 | input_dev->name = "AD7877 Touchscreen";
|
| 727 | input_dev->phys = ts->phys;
|
| 728 | input_dev->dev.parent = &spi->dev;
|
| 729 |
|
| 730 | __set_bit(EV_KEY, input_dev->evbit);
|
| 731 | __set_bit(BTN_TOUCH, input_dev->keybit);
|
| 732 | __set_bit(EV_ABS, input_dev->evbit);
|
| 733 | __set_bit(ABS_X, input_dev->absbit);
|
| 734 | __set_bit(ABS_Y, input_dev->absbit);
|
| 735 | __set_bit(ABS_PRESSURE, input_dev->absbit);
|
| 736 |
|
| 737 | input_set_abs_params(input_dev, ABS_X,
|
| 738 | pdata->x_min ? : 0,
|
| 739 | pdata->x_max ? : MAX_12BIT,
|
| 740 | 0, 0);
|
| 741 | input_set_abs_params(input_dev, ABS_Y,
|
| 742 | pdata->y_min ? : 0,
|
| 743 | pdata->y_max ? : MAX_12BIT,
|
| 744 | 0, 0);
|
| 745 | input_set_abs_params(input_dev, ABS_PRESSURE,
|
| 746 | pdata->pressure_min, pdata->pressure_max, 0, 0);
|
| 747 |
|
| 748 | ad7877_write(spi, AD7877_REG_SEQ1, AD7877_MM_SEQUENCE);
|
| 749 |
|
| 750 | verify = ad7877_read(spi, AD7877_REG_SEQ1);
|
| 751 |
|
| 752 | if (verify != AD7877_MM_SEQUENCE) {
|
| 753 | dev_err(&spi->dev, "%s: Failed to probe %s\n",
|
| 754 | dev_name(&spi->dev), input_dev->name);
|
| 755 | return -ENODEV;
|
| 756 | }
|
| 757 |
|
| 758 | if (gpio3)
|
| 759 | ad7877_write(spi, AD7877_REG_EXTWRITE, AD7877_EXTW_GPIO_3_CONF);
|
| 760 |
|
| 761 | ad7877_setup_ts_def_msg(spi, ts);
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 | err = devm_request_threaded_irq(&spi->dev, spi->irq, NULL, ad7877_irq,
|
| 766 | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
|
| 767 | spi->dev.driver->name, ts);
|
| 768 | if (err) {
|
| 769 | dev_dbg(&spi->dev, "irq %d busy?\n", spi->irq);
|
| 770 | return err;
|
| 771 | }
|
| 772 |
|
| 773 | err = input_register_device(input_dev);
|
| 774 | if (err)
|
| 775 | return err;
|
| 776 |
|
| 777 | return 0;
|
| 778 | }
|
| 779 |
|
| 780 | static int ad7877_suspend(struct device *dev)
|
| 781 | {
|
| 782 | struct ad7877 *ts = dev_get_drvdata(dev);
|
| 783 |
|
| 784 | ad7877_disable(ts);
|
| 785 |
|
| 786 | return 0;
|
| 787 | }
|
| 788 |
|
| 789 | static int ad7877_resume(struct device *dev)
|
| 790 | {
|
| 791 | struct ad7877 *ts = dev_get_drvdata(dev);
|
| 792 |
|
| 793 | ad7877_enable(ts);
|
| 794 |
|
| 795 | return 0;
|
| 796 | }
|
| 797 |
|
| 798 | static DEFINE_SIMPLE_DEV_PM_OPS(ad7877_pm, ad7877_suspend, ad7877_resume);
|
| 799 |
|
| 800 | static struct spi_driver ad7877_driver = {
|
| 801 | .driver = {
|
| 802 | .name = "ad7877",
|
| 803 | .dev_groups = ad7877_groups,
|
| 804 | .pm = pm_sleep_ptr(&ad7877_pm),
|
| 805 | },
|
| 806 | .probe = ad7877_probe,
|
| 807 | };
|
| 808 |
|
| 809 | module_spi_driver(ad7877_driver);
|
| 810 |
|
| 811 | MODULE_AUTHOR("Michael Hennerich <[email protected]>");
|
| 812 | MODULE_DESCRIPTION("AD7877 touchscreen Driver");
|
| 813 | MODULE_LICENSE("GPL");
|
| 814 | MODULE_ALIAS("spi:ad7877");
|