Lines Matching +full:spi +full:- +full:tx +full:- +full:delay +full:- +full:us

4  * Copyright (C) 2006-2010 Nokia Corporation
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include <linux/delay.h>
31 #include <linux/spi/spi.h>
32 #include <linux/spi/tsc2005.h>
41 * 5) tsc2005_irq_thread() queues up an spi transfer to fetch the x, y, z1, z2
113 struct spi_device *spi; member
151 u8 tx = TSC2005_CMD | TSC2005_CMD_12BIT | cmd; in tsc2005_cmd() local
153 .tx_buf = &tx, in tsc2005_cmd()
163 error = spi_sync(ts->spi, &msg); in tsc2005_cmd()
165 dev_err(&ts->spi->dev, "%s: failed, command: %x, error: %d\n", in tsc2005_cmd()
175 u32 tx = ((reg | TSC2005_REG_PND0) << 16) | value; in tsc2005_write() local
177 .tx_buf = &tx, in tsc2005_write()
187 error = spi_sync(ts->spi, &msg); in tsc2005_write()
189 dev_err(&ts->spi->dev, in tsc2005_write()
202 rd->spi_tx = (reg | TSC2005_REG_READ) << 16; in tsc2005_setup_read()
203 rd->spi_xfer.tx_buf = &rd->spi_tx; in tsc2005_setup_read()
204 rd->spi_xfer.rx_buf = &rd->spi_rx; in tsc2005_setup_read()
205 rd->spi_xfer.len = 4; in tsc2005_setup_read()
206 rd->spi_xfer.bits_per_word = 24; in tsc2005_setup_read()
207 rd->spi_xfer.cs_change = !last; in tsc2005_setup_read()
221 error = spi_sync(ts->spi, &msg); in tsc2005_read()
233 input_report_abs(ts->idev, ABS_X, x); in tsc2005_update_pen_state()
234 input_report_abs(ts->idev, ABS_Y, y); in tsc2005_update_pen_state()
235 input_report_abs(ts->idev, ABS_PRESSURE, pressure); in tsc2005_update_pen_state()
236 if (!ts->pen_down) { in tsc2005_update_pen_state()
237 input_report_key(ts->idev, BTN_TOUCH, !!pressure); in tsc2005_update_pen_state()
238 ts->pen_down = true; in tsc2005_update_pen_state()
241 input_report_abs(ts->idev, ABS_PRESSURE, 0); in tsc2005_update_pen_state()
242 if (ts->pen_down) { in tsc2005_update_pen_state()
243 input_report_key(ts->idev, BTN_TOUCH, 0); in tsc2005_update_pen_state()
244 ts->pen_down = false; in tsc2005_update_pen_state()
247 input_sync(ts->idev); in tsc2005_update_pen_state()
248 dev_dbg(&ts->spi->dev, "point(%4d,%4d), pressure (%4d)\n", x, y, in tsc2005_update_pen_state()
262 error = spi_sync(ts->spi, &ts->spi_read_msg); in tsc2005_irq_thread()
266 x = ts->spi_x.spi_rx; in tsc2005_irq_thread()
267 y = ts->spi_y.spi_rx; in tsc2005_irq_thread()
268 z1 = ts->spi_z1.spi_rx; in tsc2005_irq_thread()
269 z2 = ts->spi_z2.spi_rx; in tsc2005_irq_thread()
281 * the value before pen-up - that implies SPI fed us stale data in tsc2005_irq_thread()
283 if (!ts->pen_down && in tsc2005_irq_thread()
284 ts->in_x == x && ts->in_y == y && in tsc2005_irq_thread()
285 ts->in_z1 == z1 && ts->in_z2 == z2) { in tsc2005_irq_thread()
293 ts->in_x = x; in tsc2005_irq_thread()
294 ts->in_y = y; in tsc2005_irq_thread()
295 ts->in_z1 = z1; in tsc2005_irq_thread()
296 ts->in_z2 = z2; in tsc2005_irq_thread()
299 pressure = x * (z2 - z1) / z1; in tsc2005_irq_thread()
300 pressure = pressure * ts->x_plate_ohm / 4096; in tsc2005_irq_thread()
304 spin_lock_irqsave(&ts->lock, flags); in tsc2005_irq_thread()
307 mod_timer(&ts->penup_timer, in tsc2005_irq_thread()
310 spin_unlock_irqrestore(&ts->lock, flags); in tsc2005_irq_thread()
312 ts->last_valid_interrupt = jiffies; in tsc2005_irq_thread()
322 spin_lock_irqsave(&ts->lock, flags); in tsc2005_penup_timer()
324 spin_unlock_irqrestore(&ts->lock, flags); in tsc2005_penup_timer()
340 /* must be called with ts->mutex held */
345 disable_irq(ts->spi->irq); in __tsc2005_disable()
346 del_timer_sync(&ts->penup_timer); in __tsc2005_disable()
348 cancel_delayed_work_sync(&ts->esd_work); in __tsc2005_disable()
350 enable_irq(ts->spi->irq); in __tsc2005_disable()
353 /* must be called with ts->mutex held */
358 if (ts->esd_timeout && ts->set_reset) { in __tsc2005_enable()
359 ts->last_valid_interrupt = jiffies; in __tsc2005_enable()
360 schedule_delayed_work(&ts->esd_work, in __tsc2005_enable()
362 msecs_to_jiffies(ts->esd_timeout))); in __tsc2005_enable()
371 struct spi_device *spi = to_spi_device(dev); in tsc2005_selftest_show() local
372 struct tsc2005 *ts = spi_get_drvdata(spi); in tsc2005_selftest_show()
379 mutex_lock(&ts->mutex); in tsc2005_selftest_show()
393 temp_high_test = (temp_high_orig - 1) & MAX_12BIT; in tsc2005_selftest_show()
417 ts->set_reset(false); in tsc2005_selftest_show()
418 usleep_range(100, 500); /* only 10us required */ in tsc2005_selftest_show()
419 ts->set_reset(true); in tsc2005_selftest_show()
441 mutex_unlock(&ts->mutex); in tsc2005_selftest_show()
457 struct spi_device *spi = to_spi_device(dev); in tsc2005_attr_is_visible() local
458 struct tsc2005 *ts = spi_get_drvdata(spi); in tsc2005_attr_is_visible()
459 umode_t mode = attr->mode; in tsc2005_attr_is_visible()
462 if (!ts->set_reset) in tsc2005_attr_is_visible()
480 if (!mutex_trylock(&ts->mutex)) { in tsc2005_esd_work()
489 if (time_is_after_jiffies(ts->last_valid_interrupt + in tsc2005_esd_work()
490 msecs_to_jiffies(ts->esd_timeout))) in tsc2005_esd_work()
502 * then we should reset the controller as if from power-up and start in tsc2005_esd_work()
505 dev_info(&ts->spi->dev, "TSC2005 not responding - resetting\n"); in tsc2005_esd_work()
507 disable_irq(ts->spi->irq); in tsc2005_esd_work()
508 del_timer_sync(&ts->penup_timer); in tsc2005_esd_work()
512 ts->set_reset(false); in tsc2005_esd_work()
513 usleep_range(100, 500); /* only 10us required */ in tsc2005_esd_work()
514 ts->set_reset(true); in tsc2005_esd_work()
516 enable_irq(ts->spi->irq); in tsc2005_esd_work()
520 mutex_unlock(&ts->mutex); in tsc2005_esd_work()
522 /* re-arm the watchdog */ in tsc2005_esd_work()
523 schedule_delayed_work(&ts->esd_work, in tsc2005_esd_work()
525 msecs_to_jiffies(ts->esd_timeout))); in tsc2005_esd_work()
532 mutex_lock(&ts->mutex); in tsc2005_open()
534 if (!ts->suspended) in tsc2005_open()
537 ts->opened = true; in tsc2005_open()
539 mutex_unlock(&ts->mutex); in tsc2005_open()
548 mutex_lock(&ts->mutex); in tsc2005_close()
550 if (!ts->suspended) in tsc2005_close()
553 ts->opened = false; in tsc2005_close()
555 mutex_unlock(&ts->mutex); in tsc2005_close()
560 tsc2005_setup_read(&ts->spi_x, TSC2005_REG_X, false); in tsc2005_setup_spi_xfer()
561 tsc2005_setup_read(&ts->spi_y, TSC2005_REG_Y, false); in tsc2005_setup_spi_xfer()
562 tsc2005_setup_read(&ts->spi_z1, TSC2005_REG_Z1, false); in tsc2005_setup_spi_xfer()
563 tsc2005_setup_read(&ts->spi_z2, TSC2005_REG_Z2, true); in tsc2005_setup_spi_xfer()
565 spi_message_init(&ts->spi_read_msg); in tsc2005_setup_spi_xfer()
566 spi_message_add_tail(&ts->spi_x.spi_xfer, &ts->spi_read_msg); in tsc2005_setup_spi_xfer()
567 spi_message_add_tail(&ts->spi_y.spi_xfer, &ts->spi_read_msg); in tsc2005_setup_spi_xfer()
568 spi_message_add_tail(&ts->spi_z1.spi_xfer, &ts->spi_read_msg); in tsc2005_setup_spi_xfer()
569 spi_message_add_tail(&ts->spi_z2.spi_xfer, &ts->spi_read_msg); in tsc2005_setup_spi_xfer()
572 static int __devinit tsc2005_probe(struct spi_device *spi) in tsc2005_probe() argument
574 const struct tsc2005_platform_data *pdata = spi->dev.platform_data; in tsc2005_probe()
582 dev_dbg(&spi->dev, "no platform data\n"); in tsc2005_probe()
583 return -ENODEV; in tsc2005_probe()
586 fudge_x = pdata->ts_x_fudge ? : 4; in tsc2005_probe()
587 fudge_y = pdata->ts_y_fudge ? : 8; in tsc2005_probe()
588 fudge_p = pdata->ts_pressure_fudge ? : 2; in tsc2005_probe()
589 max_x = pdata->ts_x_max ? : MAX_12BIT; in tsc2005_probe()
590 max_y = pdata->ts_y_max ? : MAX_12BIT; in tsc2005_probe()
591 max_p = pdata->ts_pressure_max ? : MAX_12BIT; in tsc2005_probe()
593 if (spi->irq <= 0) { in tsc2005_probe()
594 dev_dbg(&spi->dev, "no irq\n"); in tsc2005_probe()
595 return -ENODEV; in tsc2005_probe()
598 spi->mode = SPI_MODE_0; in tsc2005_probe()
599 spi->bits_per_word = 8; in tsc2005_probe()
600 if (!spi->max_speed_hz) in tsc2005_probe()
601 spi->max_speed_hz = TSC2005_SPI_MAX_SPEED_HZ; in tsc2005_probe()
603 error = spi_setup(spi); in tsc2005_probe()
610 error = -ENOMEM; in tsc2005_probe()
614 ts->spi = spi; in tsc2005_probe()
615 ts->idev = input_dev; in tsc2005_probe()
617 ts->x_plate_ohm = pdata->ts_x_plate_ohm ? : 280; in tsc2005_probe()
618 ts->esd_timeout = pdata->esd_timeout_ms; in tsc2005_probe()
619 ts->set_reset = pdata->set_reset; in tsc2005_probe()
621 mutex_init(&ts->mutex); in tsc2005_probe()
623 spin_lock_init(&ts->lock); in tsc2005_probe()
624 setup_timer(&ts->penup_timer, tsc2005_penup_timer, (unsigned long)ts); in tsc2005_probe()
626 INIT_DELAYED_WORK(&ts->esd_work, tsc2005_esd_work); in tsc2005_probe()
630 snprintf(ts->phys, sizeof(ts->phys), in tsc2005_probe()
631 "%s/input-ts", dev_name(&spi->dev)); in tsc2005_probe()
633 input_dev->name = "TSC2005 touchscreen"; in tsc2005_probe()
634 input_dev->phys = ts->phys; in tsc2005_probe()
635 input_dev->id.bustype = BUS_SPI; in tsc2005_probe()
636 input_dev->dev.parent = &spi->dev; in tsc2005_probe()
637 input_dev->evbit[0] = BIT(EV_ABS) | BIT(EV_KEY); in tsc2005_probe()
638 input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH); in tsc2005_probe()
644 input_dev->open = tsc2005_open; in tsc2005_probe()
645 input_dev->close = tsc2005_close; in tsc2005_probe()
652 error = request_threaded_irq(spi->irq, NULL, tsc2005_irq_thread, in tsc2005_probe()
655 dev_err(&spi->dev, "Failed to request irq, err: %d\n", error); in tsc2005_probe()
659 spi_set_drvdata(spi, ts); in tsc2005_probe()
660 error = sysfs_create_group(&spi->dev.kobj, &tsc2005_attr_group); in tsc2005_probe()
662 dev_err(&spi->dev, in tsc2005_probe()
667 error = input_register_device(ts->idev); in tsc2005_probe()
669 dev_err(&spi->dev, in tsc2005_probe()
674 irq_set_irq_wake(spi->irq, 1); in tsc2005_probe()
678 sysfs_remove_group(&spi->dev.kobj, &tsc2005_attr_group); in tsc2005_probe()
680 spi_set_drvdata(spi, NULL); in tsc2005_probe()
681 free_irq(spi->irq, ts); in tsc2005_probe()
688 static int __devexit tsc2005_remove(struct spi_device *spi) in tsc2005_remove() argument
690 struct tsc2005 *ts = spi_get_drvdata(spi); in tsc2005_remove()
692 sysfs_remove_group(&ts->spi->dev.kobj, &tsc2005_attr_group); in tsc2005_remove()
694 free_irq(ts->spi->irq, ts); in tsc2005_remove()
695 input_unregister_device(ts->idev); in tsc2005_remove()
698 spi_set_drvdata(spi, NULL); in tsc2005_remove()
705 struct spi_device *spi = to_spi_device(dev); in tsc2005_suspend() local
706 struct tsc2005 *ts = spi_get_drvdata(spi); in tsc2005_suspend()
708 mutex_lock(&ts->mutex); in tsc2005_suspend()
710 if (!ts->suspended && ts->opened) in tsc2005_suspend()
713 ts->suspended = true; in tsc2005_suspend()
715 mutex_unlock(&ts->mutex); in tsc2005_suspend()
722 struct spi_device *spi = to_spi_device(dev); in tsc2005_resume() local
723 struct tsc2005 *ts = spi_get_drvdata(spi); in tsc2005_resume()
725 mutex_lock(&ts->mutex); in tsc2005_resume()
727 if (ts->suspended && ts->opened) in tsc2005_resume()
730 ts->suspended = false; in tsc2005_resume()
732 mutex_unlock(&ts->mutex); in tsc2005_resume()