Michael Hennerich | b4be468 | 2009-03-09 20:12:52 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 Michael Hennerich, Analog Devices Inc. |
| 3 | * |
| 4 | * Description: AD7879 based touchscreen, and GPIO driver (I2C/SPI Interface) |
| 5 | * |
| 6 | * Bugs: Enter bugs at http://blackfin.uclinux.org/ |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, see the file COPYING, or write |
| 20 | * to the Free Software Foundation, Inc., |
| 21 | * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 22 | * |
| 23 | * History: |
| 24 | * Copyright (c) 2005 David Brownell |
| 25 | * Copyright (c) 2006 Nokia Corporation |
| 26 | * Various changes: Imre Deak <imre.deak@nokia.com> |
| 27 | * |
| 28 | * Using code from: |
| 29 | * - corgi_ts.c |
| 30 | * Copyright (C) 2004-2005 Richard Purdie |
| 31 | * - omap_ts.[hc], ads7846.h, ts_osk.c |
| 32 | * Copyright (C) 2002 MontaVista Software |
| 33 | * Copyright (C) 2004 Texas Instruments |
| 34 | * Copyright (C) 2005 Dirk Behme |
| 35 | * - ad7877.c |
| 36 | * Copyright (C) 2006-2008 Analog Devices Inc. |
| 37 | */ |
| 38 | |
| 39 | #include <linux/device.h> |
| 40 | #include <linux/init.h> |
| 41 | #include <linux/delay.h> |
| 42 | #include <linux/input.h> |
| 43 | #include <linux/interrupt.h> |
| 44 | #include <linux/irq.h> |
| 45 | #include <linux/slab.h> |
| 46 | #include <linux/workqueue.h> |
| 47 | #include <linux/spi/spi.h> |
| 48 | #include <linux/i2c.h> |
| 49 | |
| 50 | #include <linux/spi/ad7879.h> |
| 51 | |
| 52 | #define AD7879_REG_ZEROS 0 |
| 53 | #define AD7879_REG_CTRL1 1 |
| 54 | #define AD7879_REG_CTRL2 2 |
| 55 | #define AD7879_REG_CTRL3 3 |
| 56 | #define AD7879_REG_AUX1HIGH 4 |
| 57 | #define AD7879_REG_AUX1LOW 5 |
| 58 | #define AD7879_REG_TEMP1HIGH 6 |
| 59 | #define AD7879_REG_TEMP1LOW 7 |
| 60 | #define AD7879_REG_XPLUS 8 |
| 61 | #define AD7879_REG_YPLUS 9 |
| 62 | #define AD7879_REG_Z1 10 |
| 63 | #define AD7879_REG_Z2 11 |
| 64 | #define AD7879_REG_AUXVBAT 12 |
| 65 | #define AD7879_REG_TEMP 13 |
| 66 | #define AD7879_REG_REVID 14 |
| 67 | |
| 68 | /* Control REG 1 */ |
| 69 | #define AD7879_TMR(x) ((x & 0xFF) << 0) |
| 70 | #define AD7879_ACQ(x) ((x & 0x3) << 8) |
| 71 | #define AD7879_MODE_NOC (0 << 10) /* Do not convert */ |
| 72 | #define AD7879_MODE_SCC (1 << 10) /* Single channel conversion */ |
| 73 | #define AD7879_MODE_SEQ0 (2 << 10) /* Sequence 0 in Slave Mode */ |
| 74 | #define AD7879_MODE_SEQ1 (3 << 10) /* Sequence 1 in Master Mode */ |
| 75 | #define AD7879_MODE_INT (1 << 15) /* PENIRQ disabled INT enabled */ |
| 76 | |
| 77 | /* Control REG 2 */ |
| 78 | #define AD7879_FCD(x) ((x & 0x3) << 0) |
| 79 | #define AD7879_RESET (1 << 4) |
| 80 | #define AD7879_MFS(x) ((x & 0x3) << 5) |
| 81 | #define AD7879_AVG(x) ((x & 0x3) << 7) |
| 82 | #define AD7879_SER (1 << 9) /* non-differential */ |
| 83 | #define AD7879_DFR (0 << 9) /* differential */ |
| 84 | #define AD7879_GPIOPOL (1 << 10) |
| 85 | #define AD7879_GPIODIR (1 << 11) |
| 86 | #define AD7879_GPIO_DATA (1 << 12) |
| 87 | #define AD7879_GPIO_EN (1 << 13) |
| 88 | #define AD7879_PM(x) ((x & 0x3) << 14) |
| 89 | #define AD7879_PM_SHUTDOWN (0) |
| 90 | #define AD7879_PM_DYN (1) |
| 91 | #define AD7879_PM_FULLON (2) |
| 92 | |
| 93 | /* Control REG 3 */ |
| 94 | #define AD7879_TEMPMASK_BIT (1<<15) |
| 95 | #define AD7879_AUXVBATMASK_BIT (1<<14) |
| 96 | #define AD7879_INTMODE_BIT (1<<13) |
| 97 | #define AD7879_GPIOALERTMASK_BIT (1<<12) |
| 98 | #define AD7879_AUXLOW_BIT (1<<11) |
| 99 | #define AD7879_AUXHIGH_BIT (1<<10) |
| 100 | #define AD7879_TEMPLOW_BIT (1<<9) |
| 101 | #define AD7879_TEMPHIGH_BIT (1<<8) |
| 102 | #define AD7879_YPLUS_BIT (1<<7) |
| 103 | #define AD7879_XPLUS_BIT (1<<6) |
| 104 | #define AD7879_Z1_BIT (1<<5) |
| 105 | #define AD7879_Z2_BIT (1<<4) |
| 106 | #define AD7879_AUX_BIT (1<<3) |
| 107 | #define AD7879_VBAT_BIT (1<<2) |
| 108 | #define AD7879_TEMP_BIT (1<<1) |
| 109 | |
| 110 | enum { |
| 111 | AD7879_SEQ_XPOS = 0, |
| 112 | AD7879_SEQ_YPOS = 1, |
| 113 | AD7879_SEQ_Z1 = 2, |
| 114 | AD7879_SEQ_Z2 = 3, |
| 115 | AD7879_NR_SENSE = 4, |
| 116 | }; |
| 117 | |
| 118 | #define MAX_12BIT ((1<<12)-1) |
| 119 | #define TS_PEN_UP_TIMEOUT msecs_to_jiffies(50) |
| 120 | |
| 121 | #if defined(CONFIG_TOUCHSCREEN_AD7879_SPI) || defined(CONFIG_TOUCHSCREEN_AD7879_SPI_MODULE) |
| 122 | #define AD7879_DEVID 0x7A |
| 123 | typedef struct spi_device bus_device; |
| 124 | #elif defined(CONFIG_TOUCHSCREEN_AD7879_I2C) || defined(CONFIG_TOUCHSCREEN_AD7879_I2C_MODULE) |
| 125 | #define AD7879_DEVID 0x79 |
| 126 | typedef struct i2c_client bus_device; |
| 127 | #endif |
| 128 | |
| 129 | struct ad7879 { |
| 130 | bus_device *bus; |
| 131 | struct input_dev *input; |
| 132 | struct work_struct work; |
| 133 | struct timer_list timer; |
| 134 | |
| 135 | struct mutex mutex; |
| 136 | unsigned disabled:1; /* P: mutex */ |
| 137 | |
| 138 | #if defined(CONFIG_TOUCHSCREEN_AD7879_SPI) || defined(CONFIG_TOUCHSCREEN_AD7879_SPI_MODULE) |
| 139 | struct spi_message msg; |
| 140 | struct spi_transfer xfer[AD7879_NR_SENSE + 1]; |
| 141 | u16 cmd; |
| 142 | #endif |
| 143 | u16 conversion_data[AD7879_NR_SENSE]; |
| 144 | char phys[32]; |
| 145 | u8 first_conversion_delay; |
| 146 | u8 acquisition_time; |
| 147 | u8 averaging; |
| 148 | u8 pen_down_acc_interval; |
| 149 | u8 median; |
| 150 | u16 x_plate_ohms; |
| 151 | u16 pressure_max; |
| 152 | u16 gpio_init; |
| 153 | u16 cmd_crtl1; |
| 154 | u16 cmd_crtl2; |
| 155 | u16 cmd_crtl3; |
| 156 | unsigned gpio:1; |
| 157 | }; |
| 158 | |
| 159 | static int ad7879_read(bus_device *, u8); |
| 160 | static int ad7879_write(bus_device *, u8, u16); |
| 161 | static void ad7879_collect(struct ad7879 *); |
| 162 | |
| 163 | static void ad7879_report(struct ad7879 *ts) |
| 164 | { |
| 165 | struct input_dev *input_dev = ts->input; |
| 166 | unsigned Rt; |
| 167 | u16 x, y, z1, z2; |
| 168 | |
| 169 | x = ts->conversion_data[AD7879_SEQ_XPOS] & MAX_12BIT; |
| 170 | y = ts->conversion_data[AD7879_SEQ_YPOS] & MAX_12BIT; |
| 171 | z1 = ts->conversion_data[AD7879_SEQ_Z1] & MAX_12BIT; |
| 172 | z2 = ts->conversion_data[AD7879_SEQ_Z2] & MAX_12BIT; |
| 173 | |
| 174 | /* |
| 175 | * The samples processed here are already preprocessed by the AD7879. |
| 176 | * The preprocessing function consists of a median and an averaging filter. |
| 177 | * The combination of these two techniques provides a robust solution, |
| 178 | * discarding the spurious noise in the signal and keeping only the data of interest. |
| 179 | * The size of both filters is programmable. (dev.platform_data, see linux/spi/ad7879.h) |
| 180 | * Other user-programmable conversion controls include variable acquisition time, |
| 181 | * and first conversion delay. Up to 16 averages can be taken per conversion. |
| 182 | */ |
| 183 | |
| 184 | if (likely(x && z1)) { |
| 185 | /* compute touch pressure resistance using equation #1 */ |
| 186 | Rt = (z2 - z1) * x * ts->x_plate_ohms; |
| 187 | Rt /= z1; |
| 188 | Rt = (Rt + 2047) >> 12; |
| 189 | |
| 190 | input_report_abs(input_dev, ABS_X, x); |
| 191 | input_report_abs(input_dev, ABS_Y, y); |
| 192 | input_report_abs(input_dev, ABS_PRESSURE, Rt); |
| 193 | input_sync(input_dev); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | static void ad7879_work(struct work_struct *work) |
| 198 | { |
| 199 | struct ad7879 *ts = container_of(work, struct ad7879, work); |
| 200 | |
| 201 | /* use keventd context to read the result registers */ |
| 202 | ad7879_collect(ts); |
| 203 | ad7879_report(ts); |
| 204 | mod_timer(&ts->timer, jiffies + TS_PEN_UP_TIMEOUT); |
| 205 | } |
| 206 | |
| 207 | static void ad7879_ts_event_release(struct ad7879 *ts) |
| 208 | { |
| 209 | struct input_dev *input_dev = ts->input; |
| 210 | |
| 211 | input_report_abs(input_dev, ABS_PRESSURE, 0); |
| 212 | input_sync(input_dev); |
| 213 | } |
| 214 | |
| 215 | static void ad7879_timer(unsigned long handle) |
| 216 | { |
| 217 | struct ad7879 *ts = (void *)handle; |
| 218 | |
| 219 | ad7879_ts_event_release(ts); |
| 220 | } |
| 221 | |
| 222 | static irqreturn_t ad7879_irq(int irq, void *handle) |
| 223 | { |
| 224 | struct ad7879 *ts = handle; |
| 225 | |
| 226 | /* The repeated conversion sequencer controlled by TMR kicked off too fast. |
| 227 | * We ignore the last and process the sample sequence currently in the queue. |
| 228 | * It can't be older than 9.4ms |
| 229 | */ |
| 230 | |
| 231 | if (!work_pending(&ts->work)) |
| 232 | schedule_work(&ts->work); |
| 233 | |
| 234 | return IRQ_HANDLED; |
| 235 | } |
| 236 | |
| 237 | static void ad7879_setup(struct ad7879 *ts) |
| 238 | { |
| 239 | ts->cmd_crtl3 = AD7879_YPLUS_BIT | |
| 240 | AD7879_XPLUS_BIT | |
| 241 | AD7879_Z2_BIT | |
| 242 | AD7879_Z1_BIT | |
| 243 | AD7879_TEMPMASK_BIT | |
| 244 | AD7879_AUXVBATMASK_BIT | |
| 245 | AD7879_GPIOALERTMASK_BIT; |
| 246 | |
| 247 | ts->cmd_crtl2 = AD7879_PM(AD7879_PM_DYN) | AD7879_DFR | |
| 248 | AD7879_AVG(ts->averaging) | |
| 249 | AD7879_MFS(ts->median) | |
| 250 | AD7879_FCD(ts->first_conversion_delay) | |
| 251 | ts->gpio_init; |
| 252 | |
| 253 | ts->cmd_crtl1 = AD7879_MODE_INT | AD7879_MODE_SEQ1 | |
| 254 | AD7879_ACQ(ts->acquisition_time) | |
| 255 | AD7879_TMR(ts->pen_down_acc_interval); |
| 256 | |
| 257 | ad7879_write(ts->bus, AD7879_REG_CTRL2, ts->cmd_crtl2); |
| 258 | ad7879_write(ts->bus, AD7879_REG_CTRL3, ts->cmd_crtl3); |
| 259 | ad7879_write(ts->bus, AD7879_REG_CTRL1, ts->cmd_crtl1); |
| 260 | } |
| 261 | |
| 262 | static void ad7879_disable(struct ad7879 *ts) |
| 263 | { |
| 264 | mutex_lock(&ts->mutex); |
| 265 | |
| 266 | if (!ts->disabled) { |
| 267 | |
| 268 | ts->disabled = 1; |
| 269 | disable_irq(ts->bus->irq); |
| 270 | |
| 271 | cancel_work_sync(&ts->work); |
| 272 | |
| 273 | if (del_timer_sync(&ts->timer)) |
| 274 | ad7879_ts_event_release(ts); |
| 275 | |
| 276 | ad7879_write(ts->bus, AD7879_REG_CTRL2, |
| 277 | AD7879_PM(AD7879_PM_SHUTDOWN)); |
| 278 | } |
| 279 | |
| 280 | mutex_unlock(&ts->mutex); |
| 281 | } |
| 282 | |
| 283 | static void ad7879_enable(struct ad7879 *ts) |
| 284 | { |
| 285 | mutex_lock(&ts->mutex); |
| 286 | |
| 287 | if (ts->disabled) { |
| 288 | ad7879_setup(ts); |
| 289 | ts->disabled = 0; |
| 290 | enable_irq(ts->bus->irq); |
| 291 | } |
| 292 | |
| 293 | mutex_unlock(&ts->mutex); |
| 294 | } |
| 295 | |
| 296 | static ssize_t ad7879_disable_show(struct device *dev, |
| 297 | struct device_attribute *attr, char *buf) |
| 298 | { |
| 299 | struct ad7879 *ts = dev_get_drvdata(dev); |
| 300 | |
| 301 | return sprintf(buf, "%u\n", ts->disabled); |
| 302 | } |
| 303 | |
| 304 | static ssize_t ad7879_disable_store(struct device *dev, |
| 305 | struct device_attribute *attr, |
| 306 | const char *buf, size_t count) |
| 307 | { |
| 308 | struct ad7879 *ts = dev_get_drvdata(dev); |
| 309 | unsigned long val; |
| 310 | int error; |
| 311 | |
| 312 | error = strict_strtoul(buf, 10, &val); |
| 313 | if (error) |
| 314 | return error; |
| 315 | |
| 316 | if (val) |
| 317 | ad7879_disable(ts); |
| 318 | else |
| 319 | ad7879_enable(ts); |
| 320 | |
| 321 | return count; |
| 322 | } |
| 323 | |
| 324 | static DEVICE_ATTR(disable, 0664, ad7879_disable_show, ad7879_disable_store); |
| 325 | |
| 326 | static ssize_t ad7879_gpio_show(struct device *dev, |
| 327 | struct device_attribute *attr, char *buf) |
| 328 | { |
| 329 | struct ad7879 *ts = dev_get_drvdata(dev); |
| 330 | |
| 331 | return sprintf(buf, "%u\n", ts->gpio); |
| 332 | } |
| 333 | |
| 334 | static ssize_t ad7879_gpio_store(struct device *dev, |
| 335 | struct device_attribute *attr, |
| 336 | const char *buf, size_t count) |
| 337 | { |
| 338 | struct ad7879 *ts = dev_get_drvdata(dev); |
| 339 | unsigned long val; |
| 340 | int error; |
| 341 | |
| 342 | error = strict_strtoul(buf, 10, &val); |
| 343 | if (error) |
| 344 | return error; |
| 345 | |
| 346 | mutex_lock(&ts->mutex); |
| 347 | ts->gpio = !!val; |
| 348 | error = ad7879_write(ts->bus, AD7879_REG_CTRL2, |
| 349 | ts->gpio ? |
| 350 | ts->cmd_crtl2 & ~AD7879_GPIO_DATA : |
| 351 | ts->cmd_crtl2 | AD7879_GPIO_DATA); |
| 352 | mutex_unlock(&ts->mutex); |
| 353 | |
| 354 | return error ? : count; |
| 355 | } |
| 356 | |
| 357 | static DEVICE_ATTR(gpio, 0664, ad7879_gpio_show, ad7879_gpio_store); |
| 358 | |
| 359 | static struct attribute *ad7879_attributes[] = { |
| 360 | &dev_attr_disable.attr, |
| 361 | &dev_attr_gpio.attr, |
| 362 | NULL |
| 363 | }; |
| 364 | |
| 365 | static const struct attribute_group ad7879_attr_group = { |
| 366 | .attrs = ad7879_attributes, |
| 367 | }; |
| 368 | |
| 369 | static int __devinit ad7879_construct(bus_device *bus, struct ad7879 *ts) |
| 370 | { |
| 371 | struct input_dev *input_dev; |
| 372 | struct ad7879_platform_data *pdata = bus->dev.platform_data; |
| 373 | int err; |
| 374 | u16 revid; |
| 375 | |
| 376 | if (!bus->irq) { |
| 377 | dev_err(&bus->dev, "no IRQ?\n"); |
| 378 | return -ENODEV; |
| 379 | } |
| 380 | |
| 381 | if (!pdata) { |
| 382 | dev_err(&bus->dev, "no platform data?\n"); |
| 383 | return -ENODEV; |
| 384 | } |
| 385 | |
| 386 | input_dev = input_allocate_device(); |
| 387 | if (!input_dev) |
| 388 | return -ENOMEM; |
| 389 | |
| 390 | ts->input = input_dev; |
| 391 | |
| 392 | setup_timer(&ts->timer, ad7879_timer, (unsigned long) ts); |
| 393 | INIT_WORK(&ts->work, ad7879_work); |
| 394 | mutex_init(&ts->mutex); |
| 395 | |
| 396 | ts->x_plate_ohms = pdata->x_plate_ohms ? : 400; |
| 397 | ts->pressure_max = pdata->pressure_max ? : ~0; |
| 398 | |
| 399 | ts->first_conversion_delay = pdata->first_conversion_delay; |
| 400 | ts->acquisition_time = pdata->acquisition_time; |
| 401 | ts->averaging = pdata->averaging; |
| 402 | ts->pen_down_acc_interval = pdata->pen_down_acc_interval; |
| 403 | ts->median = pdata->median; |
| 404 | |
| 405 | if (pdata->gpio_output) |
| 406 | ts->gpio_init = AD7879_GPIO_EN | |
| 407 | (pdata->gpio_default ? 0 : AD7879_GPIO_DATA); |
| 408 | else |
| 409 | ts->gpio_init = AD7879_GPIO_EN | AD7879_GPIODIR; |
| 410 | |
| 411 | snprintf(ts->phys, sizeof(ts->phys), "%s/input0", dev_name(&bus->dev)); |
| 412 | |
| 413 | input_dev->name = "AD7879 Touchscreen"; |
| 414 | input_dev->phys = ts->phys; |
| 415 | input_dev->dev.parent = &bus->dev; |
| 416 | |
| 417 | __set_bit(EV_ABS, input_dev->evbit); |
| 418 | __set_bit(ABS_X, input_dev->absbit); |
| 419 | __set_bit(ABS_Y, input_dev->absbit); |
| 420 | __set_bit(ABS_PRESSURE, input_dev->absbit); |
| 421 | |
| 422 | input_set_abs_params(input_dev, ABS_X, |
| 423 | pdata->x_min ? : 0, |
| 424 | pdata->x_max ? : MAX_12BIT, |
| 425 | 0, 0); |
| 426 | input_set_abs_params(input_dev, ABS_Y, |
| 427 | pdata->y_min ? : 0, |
| 428 | pdata->y_max ? : MAX_12BIT, |
| 429 | 0, 0); |
| 430 | input_set_abs_params(input_dev, ABS_PRESSURE, |
| 431 | pdata->pressure_min, pdata->pressure_max, 0, 0); |
| 432 | |
| 433 | err = ad7879_write(bus, AD7879_REG_CTRL2, AD7879_RESET); |
| 434 | |
| 435 | if (err < 0) { |
| 436 | dev_err(&bus->dev, "Failed to write %s\n", input_dev->name); |
| 437 | goto err_free_mem; |
| 438 | } |
| 439 | |
| 440 | revid = ad7879_read(bus, AD7879_REG_REVID); |
| 441 | |
| 442 | if ((revid & 0xFF) != AD7879_DEVID) { |
| 443 | dev_err(&bus->dev, "Failed to probe %s\n", input_dev->name); |
| 444 | err = -ENODEV; |
| 445 | goto err_free_mem; |
| 446 | } |
| 447 | |
| 448 | ad7879_setup(ts); |
| 449 | |
| 450 | err = request_irq(bus->irq, ad7879_irq, |
Michael Hennerich | 0bc69ce | 2009-04-14 10:38:36 -0700 | [diff] [blame^] | 451 | IRQF_TRIGGER_FALLING, bus->dev.driver->name, ts); |
Michael Hennerich | b4be468 | 2009-03-09 20:12:52 -0700 | [diff] [blame] | 452 | |
| 453 | if (err) { |
| 454 | dev_err(&bus->dev, "irq %d busy?\n", bus->irq); |
| 455 | goto err_free_mem; |
| 456 | } |
| 457 | |
| 458 | err = sysfs_create_group(&bus->dev.kobj, &ad7879_attr_group); |
| 459 | if (err) |
| 460 | goto err_free_irq; |
| 461 | |
| 462 | err = input_register_device(input_dev); |
| 463 | if (err) |
| 464 | goto err_remove_attr; |
| 465 | |
| 466 | dev_info(&bus->dev, "Rev.%d touchscreen, irq %d\n", |
| 467 | revid >> 8, bus->irq); |
| 468 | |
| 469 | return 0; |
| 470 | |
| 471 | err_remove_attr: |
| 472 | sysfs_remove_group(&bus->dev.kobj, &ad7879_attr_group); |
| 473 | err_free_irq: |
| 474 | free_irq(bus->irq, ts); |
| 475 | err_free_mem: |
| 476 | input_free_device(input_dev); |
| 477 | |
| 478 | return err; |
| 479 | } |
| 480 | |
| 481 | static int __devexit ad7879_destroy(bus_device *bus, struct ad7879 *ts) |
| 482 | { |
| 483 | ad7879_disable(ts); |
| 484 | sysfs_remove_group(&ts->bus->dev.kobj, &ad7879_attr_group); |
| 485 | free_irq(ts->bus->irq, ts); |
| 486 | input_unregister_device(ts->input); |
| 487 | dev_dbg(&bus->dev, "unregistered touchscreen\n"); |
| 488 | |
| 489 | return 0; |
| 490 | } |
| 491 | |
| 492 | #ifdef CONFIG_PM |
| 493 | static int ad7879_suspend(bus_device *bus, pm_message_t message) |
| 494 | { |
| 495 | struct ad7879 *ts = dev_get_drvdata(&bus->dev); |
| 496 | |
| 497 | ad7879_disable(ts); |
| 498 | |
| 499 | return 0; |
| 500 | } |
| 501 | |
| 502 | static int ad7879_resume(bus_device *bus) |
| 503 | { |
| 504 | struct ad7879 *ts = dev_get_drvdata(&bus->dev); |
| 505 | |
| 506 | ad7879_enable(ts); |
| 507 | |
| 508 | return 0; |
| 509 | } |
| 510 | #else |
| 511 | #define ad7879_suspend NULL |
| 512 | #define ad7879_resume NULL |
| 513 | #endif |
| 514 | |
| 515 | #if defined(CONFIG_TOUCHSCREEN_AD7879_SPI) || defined(CONFIG_TOUCHSCREEN_AD7879_SPI_MODULE) |
| 516 | #define MAX_SPI_FREQ_HZ 5000000 |
| 517 | #define AD7879_CMD_MAGIC 0xE000 |
| 518 | #define AD7879_CMD_READ (1 << 10) |
| 519 | #define AD7879_WRITECMD(reg) (AD7879_CMD_MAGIC | (reg & 0xF)) |
| 520 | #define AD7879_READCMD(reg) (AD7879_CMD_MAGIC | AD7879_CMD_READ | (reg & 0xF)) |
| 521 | |
| 522 | struct ser_req { |
| 523 | u16 command; |
| 524 | u16 data; |
| 525 | struct spi_message msg; |
| 526 | struct spi_transfer xfer[2]; |
| 527 | }; |
| 528 | |
| 529 | /* |
| 530 | * ad7879_read/write are only used for initial setup and for sysfs controls. |
| 531 | * The main traffic is done in ad7879_collect(). |
| 532 | */ |
| 533 | |
| 534 | static int ad7879_read(struct spi_device *spi, u8 reg) |
| 535 | { |
| 536 | struct ser_req *req; |
| 537 | int status, ret; |
| 538 | |
| 539 | req = kzalloc(sizeof *req, GFP_KERNEL); |
| 540 | if (!req) |
| 541 | return -ENOMEM; |
| 542 | |
| 543 | spi_message_init(&req->msg); |
| 544 | |
| 545 | req->command = (u16) AD7879_READCMD(reg); |
| 546 | req->xfer[0].tx_buf = &req->command; |
| 547 | req->xfer[0].len = 2; |
| 548 | |
| 549 | req->xfer[1].rx_buf = &req->data; |
| 550 | req->xfer[1].len = 2; |
| 551 | |
| 552 | spi_message_add_tail(&req->xfer[0], &req->msg); |
| 553 | spi_message_add_tail(&req->xfer[1], &req->msg); |
| 554 | |
| 555 | status = spi_sync(spi, &req->msg); |
| 556 | ret = status ? : req->data; |
| 557 | |
| 558 | kfree(req); |
| 559 | |
| 560 | return ret; |
| 561 | } |
| 562 | |
| 563 | static int ad7879_write(struct spi_device *spi, u8 reg, u16 val) |
| 564 | { |
| 565 | struct ser_req *req; |
| 566 | int status; |
| 567 | |
| 568 | req = kzalloc(sizeof *req, GFP_KERNEL); |
| 569 | if (!req) |
| 570 | return -ENOMEM; |
| 571 | |
| 572 | spi_message_init(&req->msg); |
| 573 | |
| 574 | req->command = (u16) AD7879_WRITECMD(reg); |
| 575 | req->xfer[0].tx_buf = &req->command; |
| 576 | req->xfer[0].len = 2; |
| 577 | |
| 578 | req->data = val; |
| 579 | req->xfer[1].tx_buf = &req->data; |
| 580 | req->xfer[1].len = 2; |
| 581 | |
| 582 | spi_message_add_tail(&req->xfer[0], &req->msg); |
| 583 | spi_message_add_tail(&req->xfer[1], &req->msg); |
| 584 | |
| 585 | status = spi_sync(spi, &req->msg); |
| 586 | |
| 587 | kfree(req); |
| 588 | |
| 589 | return status; |
| 590 | } |
| 591 | |
| 592 | static void ad7879_collect(struct ad7879 *ts) |
| 593 | { |
| 594 | int status = spi_sync(ts->bus, &ts->msg); |
| 595 | |
| 596 | if (status) |
| 597 | dev_err(&ts->bus->dev, "spi_sync --> %d\n", status); |
| 598 | } |
| 599 | |
| 600 | static void ad7879_setup_ts_def_msg(struct ad7879 *ts) |
| 601 | { |
| 602 | struct spi_message *m; |
| 603 | int i; |
| 604 | |
| 605 | ts->cmd = (u16) AD7879_READCMD(AD7879_REG_XPLUS); |
| 606 | |
| 607 | m = &ts->msg; |
| 608 | spi_message_init(m); |
| 609 | ts->xfer[0].tx_buf = &ts->cmd; |
| 610 | ts->xfer[0].len = 2; |
| 611 | |
| 612 | spi_message_add_tail(&ts->xfer[0], m); |
| 613 | |
| 614 | for (i = 0; i < AD7879_NR_SENSE; i++) { |
| 615 | ts->xfer[i + 1].rx_buf = &ts->conversion_data[i]; |
| 616 | ts->xfer[i + 1].len = 2; |
| 617 | spi_message_add_tail(&ts->xfer[i + 1], m); |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | static int __devinit ad7879_probe(struct spi_device *spi) |
| 622 | { |
| 623 | struct ad7879 *ts; |
| 624 | int error; |
| 625 | |
| 626 | /* don't exceed max specified SPI CLK frequency */ |
| 627 | if (spi->max_speed_hz > MAX_SPI_FREQ_HZ) { |
| 628 | dev_err(&spi->dev, "SPI CLK %d Hz?\n", spi->max_speed_hz); |
| 629 | return -EINVAL; |
| 630 | } |
| 631 | |
| 632 | ts = kzalloc(sizeof(struct ad7879), GFP_KERNEL); |
| 633 | if (!ts) |
| 634 | return -ENOMEM; |
| 635 | |
| 636 | dev_set_drvdata(&spi->dev, ts); |
| 637 | ts->bus = spi; |
| 638 | |
| 639 | ad7879_setup_ts_def_msg(ts); |
| 640 | |
| 641 | error = ad7879_construct(spi, ts); |
| 642 | if (error) { |
| 643 | dev_set_drvdata(&spi->dev, NULL); |
| 644 | kfree(ts); |
| 645 | } |
| 646 | |
| 647 | return 0; |
| 648 | } |
| 649 | |
| 650 | static int __devexit ad7879_remove(struct spi_device *spi) |
| 651 | { |
| 652 | struct ad7879 *ts = dev_get_drvdata(&spi->dev); |
| 653 | |
| 654 | ad7879_destroy(spi, ts); |
| 655 | dev_set_drvdata(&spi->dev, NULL); |
| 656 | kfree(ts); |
| 657 | |
| 658 | return 0; |
| 659 | } |
| 660 | |
| 661 | static struct spi_driver ad7879_driver = { |
| 662 | .driver = { |
| 663 | .name = "ad7879", |
| 664 | .bus = &spi_bus_type, |
| 665 | .owner = THIS_MODULE, |
| 666 | }, |
| 667 | .probe = ad7879_probe, |
| 668 | .remove = __devexit_p(ad7879_remove), |
| 669 | .suspend = ad7879_suspend, |
| 670 | .resume = ad7879_resume, |
| 671 | }; |
| 672 | |
| 673 | static int __init ad7879_init(void) |
| 674 | { |
| 675 | return spi_register_driver(&ad7879_driver); |
| 676 | } |
| 677 | module_init(ad7879_init); |
| 678 | |
| 679 | static void __exit ad7879_exit(void) |
| 680 | { |
| 681 | spi_unregister_driver(&ad7879_driver); |
| 682 | } |
| 683 | module_exit(ad7879_exit); |
| 684 | |
| 685 | #elif defined(CONFIG_TOUCHSCREEN_AD7879_I2C) || defined(CONFIG_TOUCHSCREEN_AD7879_I2C_MODULE) |
| 686 | |
| 687 | /* All registers are word-sized. |
| 688 | * AD7879 uses a high-byte first convention. |
| 689 | */ |
| 690 | static int ad7879_read(struct i2c_client *client, u8 reg) |
| 691 | { |
| 692 | return swab16(i2c_smbus_read_word_data(client, reg)); |
| 693 | } |
| 694 | |
| 695 | static int ad7879_write(struct i2c_client *client, u8 reg, u16 val) |
| 696 | { |
| 697 | return i2c_smbus_write_word_data(client, reg, swab16(val)); |
| 698 | } |
| 699 | |
| 700 | static void ad7879_collect(struct ad7879 *ts) |
| 701 | { |
| 702 | int i; |
| 703 | |
| 704 | for (i = 0; i < AD7879_NR_SENSE; i++) |
| 705 | ts->conversion_data[i] = ad7879_read(ts->bus, |
| 706 | AD7879_REG_XPLUS + i); |
| 707 | } |
| 708 | |
| 709 | static int __devinit ad7879_probe(struct i2c_client *client, |
| 710 | const struct i2c_device_id *id) |
| 711 | { |
| 712 | struct ad7879 *ts; |
| 713 | int error; |
| 714 | |
| 715 | if (!i2c_check_functionality(client->adapter, |
| 716 | I2C_FUNC_SMBUS_WORD_DATA)) { |
| 717 | dev_err(&client->dev, "SMBUS Word Data not Supported\n"); |
| 718 | return -EIO; |
| 719 | } |
| 720 | |
| 721 | ts = kzalloc(sizeof(struct ad7879), GFP_KERNEL); |
| 722 | if (!ts) |
| 723 | return -ENOMEM; |
| 724 | |
| 725 | i2c_set_clientdata(client, ts); |
| 726 | ts->bus = client; |
| 727 | |
| 728 | error = ad7879_construct(client, ts); |
| 729 | if (error) { |
| 730 | i2c_set_clientdata(client, NULL); |
| 731 | kfree(ts); |
| 732 | } |
| 733 | |
| 734 | return 0; |
| 735 | } |
| 736 | |
| 737 | static int __devexit ad7879_remove(struct i2c_client *client) |
| 738 | { |
| 739 | struct ad7879 *ts = dev_get_drvdata(&client->dev); |
| 740 | |
| 741 | ad7879_destroy(client, ts); |
| 742 | i2c_set_clientdata(client, NULL); |
| 743 | kfree(ts); |
| 744 | |
| 745 | return 0; |
| 746 | } |
| 747 | |
| 748 | static const struct i2c_device_id ad7879_id[] = { |
| 749 | { "ad7879", 0 }, |
| 750 | { } |
| 751 | }; |
| 752 | MODULE_DEVICE_TABLE(i2c, ad7879_id); |
| 753 | |
| 754 | static struct i2c_driver ad7879_driver = { |
| 755 | .driver = { |
| 756 | .name = "ad7879", |
| 757 | .owner = THIS_MODULE, |
| 758 | }, |
| 759 | .probe = ad7879_probe, |
| 760 | .remove = __devexit_p(ad7879_remove), |
| 761 | .suspend = ad7879_suspend, |
| 762 | .resume = ad7879_resume, |
| 763 | .id_table = ad7879_id, |
| 764 | }; |
| 765 | |
| 766 | static int __init ad7879_init(void) |
| 767 | { |
| 768 | return i2c_add_driver(&ad7879_driver); |
| 769 | } |
| 770 | module_init(ad7879_init); |
| 771 | |
| 772 | static void __exit ad7879_exit(void) |
| 773 | { |
| 774 | i2c_del_driver(&ad7879_driver); |
| 775 | } |
| 776 | module_exit(ad7879_exit); |
| 777 | #endif |
| 778 | |
| 779 | MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>"); |
| 780 | MODULE_DESCRIPTION("AD7879(-1) touchscreen Driver"); |
| 781 | MODULE_LICENSE("GPL"); |