Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Driver for Motorola PCAP2 touchscreen as found in the EZX phone platform.
0004  *
0005  *  Copyright (C) 2006 Harald Welte <laforge@openezx.org>
0006  *  Copyright (C) 2009 Daniel Ribeiro <drwyrm@gmail.com>
0007  */
0008 
0009 #include <linux/module.h>
0010 #include <linux/fs.h>
0011 #include <linux/string.h>
0012 #include <linux/slab.h>
0013 #include <linux/pm.h>
0014 #include <linux/timer.h>
0015 #include <linux/interrupt.h>
0016 #include <linux/platform_device.h>
0017 #include <linux/input.h>
0018 #include <linux/mfd/ezx-pcap.h>
0019 
0020 struct pcap_ts {
0021     struct pcap_chip *pcap;
0022     struct input_dev *input;
0023     struct delayed_work work;
0024     u16 x, y;
0025     u16 pressure;
0026     u8 read_state;
0027 };
0028 
0029 #define SAMPLE_DELAY    20 /* msecs */
0030 
0031 #define X_AXIS_MIN  0
0032 #define X_AXIS_MAX  1023
0033 #define Y_AXIS_MAX  X_AXIS_MAX
0034 #define Y_AXIS_MIN  X_AXIS_MIN
0035 #define PRESSURE_MAX    X_AXIS_MAX
0036 #define PRESSURE_MIN    X_AXIS_MIN
0037 
0038 static void pcap_ts_read_xy(void *data, u16 res[2])
0039 {
0040     struct pcap_ts *pcap_ts = data;
0041 
0042     switch (pcap_ts->read_state) {
0043     case PCAP_ADC_TS_M_PRESSURE:
0044         /* pressure reading is unreliable */
0045         if (res[0] > PRESSURE_MIN && res[0] < PRESSURE_MAX)
0046             pcap_ts->pressure = res[0];
0047         pcap_ts->read_state = PCAP_ADC_TS_M_XY;
0048         schedule_delayed_work(&pcap_ts->work, 0);
0049         break;
0050     case PCAP_ADC_TS_M_XY:
0051         pcap_ts->y = res[0];
0052         pcap_ts->x = res[1];
0053         if (pcap_ts->x <= X_AXIS_MIN || pcap_ts->x >= X_AXIS_MAX ||
0054             pcap_ts->y <= Y_AXIS_MIN || pcap_ts->y >= Y_AXIS_MAX) {
0055             /* pen has been released */
0056             input_report_abs(pcap_ts->input, ABS_PRESSURE, 0);
0057             input_report_key(pcap_ts->input, BTN_TOUCH, 0);
0058 
0059             pcap_ts->read_state = PCAP_ADC_TS_M_STANDBY;
0060             schedule_delayed_work(&pcap_ts->work, 0);
0061         } else {
0062             /* pen is touching the screen */
0063             input_report_abs(pcap_ts->input, ABS_X, pcap_ts->x);
0064             input_report_abs(pcap_ts->input, ABS_Y, pcap_ts->y);
0065             input_report_key(pcap_ts->input, BTN_TOUCH, 1);
0066             input_report_abs(pcap_ts->input, ABS_PRESSURE,
0067                         pcap_ts->pressure);
0068 
0069             /* switch back to pressure read mode */
0070             pcap_ts->read_state = PCAP_ADC_TS_M_PRESSURE;
0071             schedule_delayed_work(&pcap_ts->work,
0072                     msecs_to_jiffies(SAMPLE_DELAY));
0073         }
0074         input_sync(pcap_ts->input);
0075         break;
0076     default:
0077         dev_warn(&pcap_ts->input->dev,
0078                 "pcap_ts: Warning, unhandled read_state %d\n",
0079                 pcap_ts->read_state);
0080         break;
0081     }
0082 }
0083 
0084 static void pcap_ts_work(struct work_struct *work)
0085 {
0086     struct delayed_work *dw = to_delayed_work(work);
0087     struct pcap_ts *pcap_ts = container_of(dw, struct pcap_ts, work);
0088     u8 ch[2];
0089 
0090     pcap_set_ts_bits(pcap_ts->pcap,
0091             pcap_ts->read_state << PCAP_ADC_TS_M_SHIFT);
0092 
0093     if (pcap_ts->read_state == PCAP_ADC_TS_M_STANDBY)
0094         return;
0095 
0096     /* start adc conversion */
0097     ch[0] = PCAP_ADC_CH_TS_X1;
0098     ch[1] = PCAP_ADC_CH_TS_Y1;
0099     pcap_adc_async(pcap_ts->pcap, PCAP_ADC_BANK_1, 0, ch,
0100                         pcap_ts_read_xy, pcap_ts);
0101 }
0102 
0103 static irqreturn_t pcap_ts_event_touch(int pirq, void *data)
0104 {
0105     struct pcap_ts *pcap_ts = data;
0106 
0107     if (pcap_ts->read_state == PCAP_ADC_TS_M_STANDBY) {
0108         pcap_ts->read_state = PCAP_ADC_TS_M_PRESSURE;
0109         schedule_delayed_work(&pcap_ts->work, 0);
0110     }
0111     return IRQ_HANDLED;
0112 }
0113 
0114 static int pcap_ts_open(struct input_dev *dev)
0115 {
0116     struct pcap_ts *pcap_ts = input_get_drvdata(dev);
0117 
0118     pcap_ts->read_state = PCAP_ADC_TS_M_STANDBY;
0119     schedule_delayed_work(&pcap_ts->work, 0);
0120 
0121     return 0;
0122 }
0123 
0124 static void pcap_ts_close(struct input_dev *dev)
0125 {
0126     struct pcap_ts *pcap_ts = input_get_drvdata(dev);
0127 
0128     cancel_delayed_work_sync(&pcap_ts->work);
0129 
0130     pcap_ts->read_state = PCAP_ADC_TS_M_NONTS;
0131     pcap_set_ts_bits(pcap_ts->pcap,
0132                 pcap_ts->read_state << PCAP_ADC_TS_M_SHIFT);
0133 }
0134 
0135 static int pcap_ts_probe(struct platform_device *pdev)
0136 {
0137     struct input_dev *input_dev;
0138     struct pcap_ts *pcap_ts;
0139     int err = -ENOMEM;
0140 
0141     pcap_ts = kzalloc(sizeof(*pcap_ts), GFP_KERNEL);
0142     if (!pcap_ts)
0143         return err;
0144 
0145     pcap_ts->pcap = dev_get_drvdata(pdev->dev.parent);
0146     platform_set_drvdata(pdev, pcap_ts);
0147 
0148     input_dev = input_allocate_device();
0149     if (!input_dev)
0150         goto fail;
0151 
0152     INIT_DELAYED_WORK(&pcap_ts->work, pcap_ts_work);
0153 
0154     pcap_ts->read_state = PCAP_ADC_TS_M_NONTS;
0155     pcap_set_ts_bits(pcap_ts->pcap,
0156                 pcap_ts->read_state << PCAP_ADC_TS_M_SHIFT);
0157 
0158     pcap_ts->input = input_dev;
0159     input_set_drvdata(input_dev, pcap_ts);
0160 
0161     input_dev->name = "pcap-touchscreen";
0162     input_dev->phys = "pcap_ts/input0";
0163     input_dev->id.bustype = BUS_HOST;
0164     input_dev->id.vendor = 0x0001;
0165     input_dev->id.product = 0x0002;
0166     input_dev->id.version = 0x0100;
0167     input_dev->dev.parent = &pdev->dev;
0168     input_dev->open = pcap_ts_open;
0169     input_dev->close = pcap_ts_close;
0170 
0171     input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
0172     input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
0173     input_set_abs_params(input_dev, ABS_X, X_AXIS_MIN, X_AXIS_MAX, 0, 0);
0174     input_set_abs_params(input_dev, ABS_Y, Y_AXIS_MIN, Y_AXIS_MAX, 0, 0);
0175     input_set_abs_params(input_dev, ABS_PRESSURE, PRESSURE_MIN,
0176                  PRESSURE_MAX, 0, 0);
0177 
0178     err = input_register_device(pcap_ts->input);
0179     if (err)
0180         goto fail_allocate;
0181 
0182     err = request_irq(pcap_to_irq(pcap_ts->pcap, PCAP_IRQ_TS),
0183             pcap_ts_event_touch, 0, "Touch Screen", pcap_ts);
0184     if (err)
0185         goto fail_register;
0186 
0187     return 0;
0188 
0189 fail_register:
0190     input_unregister_device(input_dev);
0191     goto fail;
0192 fail_allocate:
0193     input_free_device(input_dev);
0194 fail:
0195     kfree(pcap_ts);
0196 
0197     return err;
0198 }
0199 
0200 static int pcap_ts_remove(struct platform_device *pdev)
0201 {
0202     struct pcap_ts *pcap_ts = platform_get_drvdata(pdev);
0203 
0204     free_irq(pcap_to_irq(pcap_ts->pcap, PCAP_IRQ_TS), pcap_ts);
0205     cancel_delayed_work_sync(&pcap_ts->work);
0206 
0207     input_unregister_device(pcap_ts->input);
0208 
0209     kfree(pcap_ts);
0210 
0211     return 0;
0212 }
0213 
0214 #ifdef CONFIG_PM
0215 static int pcap_ts_suspend(struct device *dev)
0216 {
0217     struct pcap_ts *pcap_ts = dev_get_drvdata(dev);
0218 
0219     pcap_set_ts_bits(pcap_ts->pcap, PCAP_ADC_TS_REF_LOWPWR);
0220     return 0;
0221 }
0222 
0223 static int pcap_ts_resume(struct device *dev)
0224 {
0225     struct pcap_ts *pcap_ts = dev_get_drvdata(dev);
0226 
0227     pcap_set_ts_bits(pcap_ts->pcap,
0228                 pcap_ts->read_state << PCAP_ADC_TS_M_SHIFT);
0229     return 0;
0230 }
0231 
0232 static const struct dev_pm_ops pcap_ts_pm_ops = {
0233     .suspend    = pcap_ts_suspend,
0234     .resume     = pcap_ts_resume,
0235 };
0236 #define PCAP_TS_PM_OPS (&pcap_ts_pm_ops)
0237 #else
0238 #define PCAP_TS_PM_OPS NULL
0239 #endif
0240 
0241 static struct platform_driver pcap_ts_driver = {
0242     .probe      = pcap_ts_probe,
0243     .remove     = pcap_ts_remove,
0244     .driver     = {
0245         .name   = "pcap-ts",
0246         .pm = PCAP_TS_PM_OPS,
0247     },
0248 };
0249 module_platform_driver(pcap_ts_driver);
0250 
0251 MODULE_DESCRIPTION("Motorola PCAP2 touchscreen driver");
0252 MODULE_AUTHOR("Daniel Ribeiro / Harald Welte");
0253 MODULE_LICENSE("GPL");
0254 MODULE_ALIAS("platform:pcap_ts");