1*d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
29bcc00b9SEric Miao /*
39bcc00b9SEric Miao * Touchscreen driver for Dialog Semiconductor DA9034
49bcc00b9SEric Miao *
59bcc00b9SEric Miao * Copyright (C) 2006-2008 Marvell International Ltd.
69bcc00b9SEric Miao * Fengwei Yin <fengwei.yin@marvell.com>
793ff27c6SEric Miao * Bin Yang <bin.yang@marvell.com>
89bcc00b9SEric Miao * Eric Miao <eric.miao@marvell.com>
99bcc00b9SEric Miao */
109bcc00b9SEric Miao
119bcc00b9SEric Miao #include <linux/module.h>
129bcc00b9SEric Miao #include <linux/kernel.h>
139bcc00b9SEric Miao #include <linux/delay.h>
149bcc00b9SEric Miao #include <linux/platform_device.h>
159bcc00b9SEric Miao #include <linux/input.h>
1630aafdbaSAlan Cox #include <linux/workqueue.h>
179bcc00b9SEric Miao #include <linux/mfd/da903x.h>
185a0e3ad6STejun Heo #include <linux/slab.h>
199bcc00b9SEric Miao
209bcc00b9SEric Miao #define DA9034_MANUAL_CTRL 0x50
219bcc00b9SEric Miao #define DA9034_LDO_ADC_EN (1 << 4)
229bcc00b9SEric Miao
239bcc00b9SEric Miao #define DA9034_AUTO_CTRL1 0x51
249bcc00b9SEric Miao
259bcc00b9SEric Miao #define DA9034_AUTO_CTRL2 0x52
269bcc00b9SEric Miao #define DA9034_AUTO_TSI_EN (1 << 3)
279bcc00b9SEric Miao #define DA9034_PEN_DETECT (1 << 4)
289bcc00b9SEric Miao
299bcc00b9SEric Miao #define DA9034_TSI_CTRL1 0x53
309bcc00b9SEric Miao #define DA9034_TSI_CTRL2 0x54
319bcc00b9SEric Miao #define DA9034_TSI_X_MSB 0x6c
329bcc00b9SEric Miao #define DA9034_TSI_Y_MSB 0x6d
339bcc00b9SEric Miao #define DA9034_TSI_XY_LSB 0x6e
349bcc00b9SEric Miao
359bcc00b9SEric Miao enum {
369bcc00b9SEric Miao STATE_IDLE, /* wait for pendown */
379bcc00b9SEric Miao STATE_BUSY, /* TSI busy sampling */
389bcc00b9SEric Miao STATE_STOP, /* sample available */
399bcc00b9SEric Miao STATE_WAIT, /* Wait to start next sample */
409bcc00b9SEric Miao };
419bcc00b9SEric Miao
429bcc00b9SEric Miao enum {
439bcc00b9SEric Miao EVENT_PEN_DOWN,
449bcc00b9SEric Miao EVENT_PEN_UP,
459bcc00b9SEric Miao EVENT_TSI_READY,
469bcc00b9SEric Miao EVENT_TIMEDOUT,
479bcc00b9SEric Miao };
489bcc00b9SEric Miao
499bcc00b9SEric Miao struct da9034_touch {
509bcc00b9SEric Miao struct device *da9034_dev;
519bcc00b9SEric Miao struct input_dev *input_dev;
529bcc00b9SEric Miao
539bcc00b9SEric Miao struct delayed_work tsi_work;
549bcc00b9SEric Miao struct notifier_block notifier;
559bcc00b9SEric Miao
569bcc00b9SEric Miao int state;
579bcc00b9SEric Miao
589bcc00b9SEric Miao int interval_ms;
599bcc00b9SEric Miao int x_inverted;
609bcc00b9SEric Miao int y_inverted;
619bcc00b9SEric Miao
629bcc00b9SEric Miao int last_x;
639bcc00b9SEric Miao int last_y;
649bcc00b9SEric Miao };
659bcc00b9SEric Miao
is_pen_down(struct da9034_touch * touch)669bcc00b9SEric Miao static inline int is_pen_down(struct da9034_touch *touch)
679bcc00b9SEric Miao {
689bcc00b9SEric Miao return da903x_query_status(touch->da9034_dev, DA9034_STATUS_PEN_DOWN);
699bcc00b9SEric Miao }
709bcc00b9SEric Miao
detect_pen_down(struct da9034_touch * touch,int on)719bcc00b9SEric Miao static inline int detect_pen_down(struct da9034_touch *touch, int on)
729bcc00b9SEric Miao {
739bcc00b9SEric Miao if (on)
749bcc00b9SEric Miao return da903x_set_bits(touch->da9034_dev,
759bcc00b9SEric Miao DA9034_AUTO_CTRL2, DA9034_PEN_DETECT);
769bcc00b9SEric Miao else
779bcc00b9SEric Miao return da903x_clr_bits(touch->da9034_dev,
789bcc00b9SEric Miao DA9034_AUTO_CTRL2, DA9034_PEN_DETECT);
799bcc00b9SEric Miao }
809bcc00b9SEric Miao
read_tsi(struct da9034_touch * touch)819bcc00b9SEric Miao static int read_tsi(struct da9034_touch *touch)
829bcc00b9SEric Miao {
839bcc00b9SEric Miao uint8_t _x, _y, _v;
849bcc00b9SEric Miao int ret;
859bcc00b9SEric Miao
869bcc00b9SEric Miao ret = da903x_read(touch->da9034_dev, DA9034_TSI_X_MSB, &_x);
879bcc00b9SEric Miao if (ret)
889bcc00b9SEric Miao return ret;
899bcc00b9SEric Miao
909bcc00b9SEric Miao ret = da903x_read(touch->da9034_dev, DA9034_TSI_Y_MSB, &_y);
919bcc00b9SEric Miao if (ret)
929bcc00b9SEric Miao return ret;
939bcc00b9SEric Miao
949bcc00b9SEric Miao ret = da903x_read(touch->da9034_dev, DA9034_TSI_XY_LSB, &_v);
959bcc00b9SEric Miao if (ret)
969bcc00b9SEric Miao return ret;
979bcc00b9SEric Miao
989bcc00b9SEric Miao touch->last_x = ((_x << 2) & 0x3fc) | (_v & 0x3);
999bcc00b9SEric Miao touch->last_y = ((_y << 2) & 0x3fc) | ((_v & 0xc) >> 2);
1009bcc00b9SEric Miao
1019bcc00b9SEric Miao return 0;
1029bcc00b9SEric Miao }
1039bcc00b9SEric Miao
start_tsi(struct da9034_touch * touch)1049bcc00b9SEric Miao static inline int start_tsi(struct da9034_touch *touch)
1059bcc00b9SEric Miao {
1069bcc00b9SEric Miao return da903x_set_bits(touch->da9034_dev,
1079bcc00b9SEric Miao DA9034_AUTO_CTRL2, DA9034_AUTO_TSI_EN);
1089bcc00b9SEric Miao }
1099bcc00b9SEric Miao
stop_tsi(struct da9034_touch * touch)1109bcc00b9SEric Miao static inline int stop_tsi(struct da9034_touch *touch)
1119bcc00b9SEric Miao {
1129bcc00b9SEric Miao return da903x_clr_bits(touch->da9034_dev,
1139bcc00b9SEric Miao DA9034_AUTO_CTRL2, DA9034_AUTO_TSI_EN);
1149bcc00b9SEric Miao }
1159bcc00b9SEric Miao
report_pen_down(struct da9034_touch * touch)1169bcc00b9SEric Miao static inline void report_pen_down(struct da9034_touch *touch)
1179bcc00b9SEric Miao {
1189bcc00b9SEric Miao int x = touch->last_x;
1199bcc00b9SEric Miao int y = touch->last_y;
1209bcc00b9SEric Miao
1219bcc00b9SEric Miao x &= 0xfff;
1229bcc00b9SEric Miao if (touch->x_inverted)
1239bcc00b9SEric Miao x = 1024 - x;
1249bcc00b9SEric Miao y &= 0xfff;
1259bcc00b9SEric Miao if (touch->y_inverted)
1269bcc00b9SEric Miao y = 1024 - y;
1279bcc00b9SEric Miao
1289bcc00b9SEric Miao input_report_abs(touch->input_dev, ABS_X, x);
1299bcc00b9SEric Miao input_report_abs(touch->input_dev, ABS_Y, y);
1309bcc00b9SEric Miao input_report_key(touch->input_dev, BTN_TOUCH, 1);
1319bcc00b9SEric Miao
1329bcc00b9SEric Miao input_sync(touch->input_dev);
1339bcc00b9SEric Miao }
1349bcc00b9SEric Miao
report_pen_up(struct da9034_touch * touch)1359bcc00b9SEric Miao static inline void report_pen_up(struct da9034_touch *touch)
1369bcc00b9SEric Miao {
1379bcc00b9SEric Miao input_report_key(touch->input_dev, BTN_TOUCH, 0);
1389bcc00b9SEric Miao input_sync(touch->input_dev);
1399bcc00b9SEric Miao }
1409bcc00b9SEric Miao
da9034_event_handler(struct da9034_touch * touch,int event)1419bcc00b9SEric Miao static void da9034_event_handler(struct da9034_touch *touch, int event)
1429bcc00b9SEric Miao {
1439bcc00b9SEric Miao int err;
1449bcc00b9SEric Miao
1459bcc00b9SEric Miao switch (touch->state) {
1469bcc00b9SEric Miao case STATE_IDLE:
1479bcc00b9SEric Miao if (event != EVENT_PEN_DOWN)
1489bcc00b9SEric Miao break;
1499bcc00b9SEric Miao
1509bcc00b9SEric Miao /* Enable auto measurement of the TSI, this will
1519bcc00b9SEric Miao * automatically disable pen down detection
1529bcc00b9SEric Miao */
1539bcc00b9SEric Miao err = start_tsi(touch);
1549bcc00b9SEric Miao if (err)
1559bcc00b9SEric Miao goto err_reset;
1569bcc00b9SEric Miao
1579bcc00b9SEric Miao touch->state = STATE_BUSY;
1589bcc00b9SEric Miao break;
1599bcc00b9SEric Miao
1609bcc00b9SEric Miao case STATE_BUSY:
1619bcc00b9SEric Miao if (event != EVENT_TSI_READY)
1629bcc00b9SEric Miao break;
1639bcc00b9SEric Miao
1649bcc00b9SEric Miao err = read_tsi(touch);
1659bcc00b9SEric Miao if (err)
1669bcc00b9SEric Miao goto err_reset;
1679bcc00b9SEric Miao
1689bcc00b9SEric Miao /* Disable auto measurement of the TSI, so that
1699bcc00b9SEric Miao * pen down status will be available
1709bcc00b9SEric Miao */
1719bcc00b9SEric Miao err = stop_tsi(touch);
1729bcc00b9SEric Miao if (err)
1739bcc00b9SEric Miao goto err_reset;
1749bcc00b9SEric Miao
1759bcc00b9SEric Miao touch->state = STATE_STOP;
1767f6d5ff2SEric Miao
1777f6d5ff2SEric Miao /* FIXME: PEN_{UP/DOWN} events are expected to be
1787f6d5ff2SEric Miao * available by stopping TSI, but this is found not
1797f6d5ff2SEric Miao * always true, delay and simulate such an event
1807f6d5ff2SEric Miao * here is more reliable
1817f6d5ff2SEric Miao */
1827f6d5ff2SEric Miao mdelay(1);
1837f6d5ff2SEric Miao da9034_event_handler(touch,
1847f6d5ff2SEric Miao is_pen_down(touch) ? EVENT_PEN_DOWN :
1857f6d5ff2SEric Miao EVENT_PEN_UP);
1869bcc00b9SEric Miao break;
1879bcc00b9SEric Miao
1889bcc00b9SEric Miao case STATE_STOP:
1899bcc00b9SEric Miao if (event == EVENT_PEN_DOWN) {
1909bcc00b9SEric Miao report_pen_down(touch);
1919bcc00b9SEric Miao schedule_delayed_work(&touch->tsi_work,
1929bcc00b9SEric Miao msecs_to_jiffies(touch->interval_ms));
1939bcc00b9SEric Miao touch->state = STATE_WAIT;
1949bcc00b9SEric Miao }
1959bcc00b9SEric Miao
1969bcc00b9SEric Miao if (event == EVENT_PEN_UP) {
1979bcc00b9SEric Miao report_pen_up(touch);
1989bcc00b9SEric Miao touch->state = STATE_IDLE;
1999bcc00b9SEric Miao }
2009bcc00b9SEric Miao break;
2019bcc00b9SEric Miao
2029bcc00b9SEric Miao case STATE_WAIT:
2039bcc00b9SEric Miao if (event != EVENT_TIMEDOUT)
2049bcc00b9SEric Miao break;
2059bcc00b9SEric Miao
2069bcc00b9SEric Miao if (is_pen_down(touch)) {
2079bcc00b9SEric Miao start_tsi(touch);
2089bcc00b9SEric Miao touch->state = STATE_BUSY;
2097f6d5ff2SEric Miao } else {
2107f6d5ff2SEric Miao report_pen_up(touch);
2119bcc00b9SEric Miao touch->state = STATE_IDLE;
2127f6d5ff2SEric Miao }
2139bcc00b9SEric Miao break;
2149bcc00b9SEric Miao }
2159bcc00b9SEric Miao return;
2169bcc00b9SEric Miao
2179bcc00b9SEric Miao err_reset:
2189bcc00b9SEric Miao touch->state = STATE_IDLE;
2199bcc00b9SEric Miao stop_tsi(touch);
2209bcc00b9SEric Miao detect_pen_down(touch, 1);
2219bcc00b9SEric Miao }
2229bcc00b9SEric Miao
da9034_tsi_work(struct work_struct * work)2239bcc00b9SEric Miao static void da9034_tsi_work(struct work_struct *work)
2249bcc00b9SEric Miao {
2259bcc00b9SEric Miao struct da9034_touch *touch =
2269bcc00b9SEric Miao container_of(work, struct da9034_touch, tsi_work.work);
2279bcc00b9SEric Miao
2289bcc00b9SEric Miao da9034_event_handler(touch, EVENT_TIMEDOUT);
2299bcc00b9SEric Miao }
2309bcc00b9SEric Miao
da9034_touch_notifier(struct notifier_block * nb,unsigned long event,void * data)2319bcc00b9SEric Miao static int da9034_touch_notifier(struct notifier_block *nb,
2329bcc00b9SEric Miao unsigned long event, void *data)
2339bcc00b9SEric Miao {
2349bcc00b9SEric Miao struct da9034_touch *touch =
2359bcc00b9SEric Miao container_of(nb, struct da9034_touch, notifier);
2369bcc00b9SEric Miao
2379bcc00b9SEric Miao if (event & DA9034_EVENT_TSI_READY)
2389bcc00b9SEric Miao da9034_event_handler(touch, EVENT_TSI_READY);
2399bcc00b9SEric Miao
2407f6d5ff2SEric Miao if ((event & DA9034_EVENT_PEN_DOWN) && touch->state == STATE_IDLE)
2417f6d5ff2SEric Miao da9034_event_handler(touch, EVENT_PEN_DOWN);
2427f6d5ff2SEric Miao
2439bcc00b9SEric Miao return 0;
2449bcc00b9SEric Miao }
2459bcc00b9SEric Miao
da9034_touch_open(struct input_dev * dev)2469bcc00b9SEric Miao static int da9034_touch_open(struct input_dev *dev)
2479bcc00b9SEric Miao {
2489bcc00b9SEric Miao struct da9034_touch *touch = input_get_drvdata(dev);
2499bcc00b9SEric Miao int ret;
2509bcc00b9SEric Miao
2519bcc00b9SEric Miao ret = da903x_register_notifier(touch->da9034_dev, &touch->notifier,
2529bcc00b9SEric Miao DA9034_EVENT_PEN_DOWN | DA9034_EVENT_TSI_READY);
2539bcc00b9SEric Miao if (ret)
2549bcc00b9SEric Miao return -EBUSY;
2559bcc00b9SEric Miao
2569bcc00b9SEric Miao /* Enable ADC LDO */
2579bcc00b9SEric Miao ret = da903x_set_bits(touch->da9034_dev,
2589bcc00b9SEric Miao DA9034_MANUAL_CTRL, DA9034_LDO_ADC_EN);
2599bcc00b9SEric Miao if (ret)
2609bcc00b9SEric Miao return ret;
2619bcc00b9SEric Miao
2629bcc00b9SEric Miao /* TSI_DELAY: 3 slots, TSI_SKIP: 3 slots */
2639bcc00b9SEric Miao ret = da903x_write(touch->da9034_dev, DA9034_TSI_CTRL1, 0x1b);
2649bcc00b9SEric Miao if (ret)
2659bcc00b9SEric Miao return ret;
2669bcc00b9SEric Miao
2679bcc00b9SEric Miao ret = da903x_write(touch->da9034_dev, DA9034_TSI_CTRL2, 0x00);
2689bcc00b9SEric Miao if (ret)
2699bcc00b9SEric Miao return ret;
2709bcc00b9SEric Miao
2719bcc00b9SEric Miao touch->state = STATE_IDLE;
2729bcc00b9SEric Miao detect_pen_down(touch, 1);
2739bcc00b9SEric Miao
2749bcc00b9SEric Miao return 0;
2759bcc00b9SEric Miao }
2769bcc00b9SEric Miao
da9034_touch_close(struct input_dev * dev)2779bcc00b9SEric Miao static void da9034_touch_close(struct input_dev *dev)
2789bcc00b9SEric Miao {
2799bcc00b9SEric Miao struct da9034_touch *touch = input_get_drvdata(dev);
2809bcc00b9SEric Miao
2819bcc00b9SEric Miao da903x_unregister_notifier(touch->da9034_dev, &touch->notifier,
2829bcc00b9SEric Miao DA9034_EVENT_PEN_DOWN | DA9034_EVENT_TSI_READY);
2839bcc00b9SEric Miao
2849bcc00b9SEric Miao cancel_delayed_work_sync(&touch->tsi_work);
2859bcc00b9SEric Miao
2869bcc00b9SEric Miao touch->state = STATE_IDLE;
2879bcc00b9SEric Miao stop_tsi(touch);
2889bcc00b9SEric Miao detect_pen_down(touch, 0);
2899bcc00b9SEric Miao
2909bcc00b9SEric Miao /* Disable ADC LDO */
2919bcc00b9SEric Miao da903x_clr_bits(touch->da9034_dev,
2929bcc00b9SEric Miao DA9034_MANUAL_CTRL, DA9034_LDO_ADC_EN);
2939bcc00b9SEric Miao }
2949bcc00b9SEric Miao
2959bcc00b9SEric Miao
da9034_touch_probe(struct platform_device * pdev)2965298cc4cSBill Pemberton static int da9034_touch_probe(struct platform_device *pdev)
2979bcc00b9SEric Miao {
298c838cb3dSJingoo Han struct da9034_touch_pdata *pdata = dev_get_platdata(&pdev->dev);
2999bcc00b9SEric Miao struct da9034_touch *touch;
3009bcc00b9SEric Miao struct input_dev *input_dev;
3014f8edc3cSHimangi Saraogi int error;
3029bcc00b9SEric Miao
3034f8edc3cSHimangi Saraogi touch = devm_kzalloc(&pdev->dev, sizeof(struct da9034_touch),
3044f8edc3cSHimangi Saraogi GFP_KERNEL);
3054f8edc3cSHimangi Saraogi if (!touch) {
3069bcc00b9SEric Miao dev_err(&pdev->dev, "failed to allocate driver data\n");
3079bcc00b9SEric Miao return -ENOMEM;
3089bcc00b9SEric Miao }
3099bcc00b9SEric Miao
3109bcc00b9SEric Miao touch->da9034_dev = pdev->dev.parent;
3119bcc00b9SEric Miao
3129bcc00b9SEric Miao if (pdata) {
3139bcc00b9SEric Miao touch->interval_ms = pdata->interval_ms;
3149bcc00b9SEric Miao touch->x_inverted = pdata->x_inverted;
3159bcc00b9SEric Miao touch->y_inverted = pdata->y_inverted;
3164f8edc3cSHimangi Saraogi } else {
3179bcc00b9SEric Miao /* fallback into default */
3189bcc00b9SEric Miao touch->interval_ms = 10;
3194f8edc3cSHimangi Saraogi }
3209bcc00b9SEric Miao
3219bcc00b9SEric Miao INIT_DELAYED_WORK(&touch->tsi_work, da9034_tsi_work);
3229bcc00b9SEric Miao touch->notifier.notifier_call = da9034_touch_notifier;
3239bcc00b9SEric Miao
3244f8edc3cSHimangi Saraogi input_dev = devm_input_allocate_device(&pdev->dev);
3259bcc00b9SEric Miao if (!input_dev) {
3269bcc00b9SEric Miao dev_err(&pdev->dev, "failed to allocate input device\n");
3274f8edc3cSHimangi Saraogi return -ENOMEM;
3289bcc00b9SEric Miao }
3299bcc00b9SEric Miao
3309bcc00b9SEric Miao input_dev->name = pdev->name;
3319bcc00b9SEric Miao input_dev->open = da9034_touch_open;
3329bcc00b9SEric Miao input_dev->close = da9034_touch_close;
3339bcc00b9SEric Miao input_dev->dev.parent = &pdev->dev;
3349bcc00b9SEric Miao
3359bcc00b9SEric Miao __set_bit(EV_ABS, input_dev->evbit);
3369bcc00b9SEric Miao __set_bit(ABS_X, input_dev->absbit);
3379bcc00b9SEric Miao __set_bit(ABS_Y, input_dev->absbit);
3389bcc00b9SEric Miao input_set_abs_params(input_dev, ABS_X, 0, 1023, 0, 0);
3399bcc00b9SEric Miao input_set_abs_params(input_dev, ABS_Y, 0, 1023, 0, 0);
3409bcc00b9SEric Miao
3419bcc00b9SEric Miao __set_bit(EV_KEY, input_dev->evbit);
3429bcc00b9SEric Miao __set_bit(BTN_TOUCH, input_dev->keybit);
3439bcc00b9SEric Miao
3449bcc00b9SEric Miao touch->input_dev = input_dev;
3459bcc00b9SEric Miao input_set_drvdata(input_dev, touch);
3469bcc00b9SEric Miao
3474f8edc3cSHimangi Saraogi error = input_register_device(input_dev);
3484f8edc3cSHimangi Saraogi if (error)
3494f8edc3cSHimangi Saraogi return error;
3509bcc00b9SEric Miao
3519bcc00b9SEric Miao return 0;
3529bcc00b9SEric Miao }
3539bcc00b9SEric Miao
3549bcc00b9SEric Miao static struct platform_driver da9034_touch_driver = {
3559bcc00b9SEric Miao .driver = {
3569bcc00b9SEric Miao .name = "da9034-touch",
3579bcc00b9SEric Miao },
3589bcc00b9SEric Miao .probe = da9034_touch_probe,
3599bcc00b9SEric Miao };
360cdcc96e2SJJ Ding module_platform_driver(da9034_touch_driver);
3619bcc00b9SEric Miao
3629bcc00b9SEric Miao MODULE_DESCRIPTION("Touchscreen driver for Dialog Semiconductor DA9034");
36393ff27c6SEric Miao MODULE_AUTHOR("Eric Miao <eric.miao@marvell.com>, Bin Yang <bin.yang@marvell.com>");
3649bcc00b9SEric Miao MODULE_LICENSE("GPL");
3659bcc00b9SEric Miao MODULE_ALIAS("platform:da9034-touch");
366