Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Touch Screen driver for EETI's I2C connected touch screen panels
0004  *   Copyright (c) 2009,2018 Daniel Mack <daniel@zonque.org>
0005  *
0006  * See EETI's software guide for the protocol specification:
0007  *   http://home.eeti.com.tw/documentation.html
0008  *
0009  * Based on migor_ts.c
0010  *   Copyright (c) 2008 Magnus Damm
0011  *   Copyright (c) 2007 Ujjwal Pande <ujjwal@kenati.com>
0012  */
0013 
0014 #include <linux/module.h>
0015 #include <linux/kernel.h>
0016 #include <linux/input.h>
0017 #include <linux/input/touchscreen.h>
0018 #include <linux/interrupt.h>
0019 #include <linux/i2c.h>
0020 #include <linux/timer.h>
0021 #include <linux/gpio/consumer.h>
0022 #include <linux/of.h>
0023 #include <linux/slab.h>
0024 #include <asm/unaligned.h>
0025 
0026 struct eeti_ts {
0027     struct i2c_client *client;
0028     struct input_dev *input;
0029     struct gpio_desc *attn_gpio;
0030     struct touchscreen_properties props;
0031     struct mutex mutex;
0032     bool running;
0033 };
0034 
0035 #define EETI_TS_BITDEPTH    (11)
0036 #define EETI_MAXVAL     ((1 << (EETI_TS_BITDEPTH + 1)) - 1)
0037 
0038 #define REPORT_BIT_PRESSED  BIT(0)
0039 #define REPORT_BIT_AD0      BIT(1)
0040 #define REPORT_BIT_AD1      BIT(2)
0041 #define REPORT_BIT_HAS_PRESSURE BIT(6)
0042 #define REPORT_RES_BITS(v)  (((v) >> 1) + EETI_TS_BITDEPTH)
0043 
0044 static void eeti_ts_report_event(struct eeti_ts *eeti, u8 *buf)
0045 {
0046     unsigned int res;
0047     u16 x, y;
0048 
0049     res = REPORT_RES_BITS(buf[0] & (REPORT_BIT_AD0 | REPORT_BIT_AD1));
0050 
0051     x = get_unaligned_be16(&buf[1]);
0052     y = get_unaligned_be16(&buf[3]);
0053 
0054     /* fix the range to 11 bits */
0055     x >>= res - EETI_TS_BITDEPTH;
0056     y >>= res - EETI_TS_BITDEPTH;
0057 
0058     if (buf[0] & REPORT_BIT_HAS_PRESSURE)
0059         input_report_abs(eeti->input, ABS_PRESSURE, buf[5]);
0060 
0061     touchscreen_report_pos(eeti->input, &eeti->props, x, y, false);
0062     input_report_key(eeti->input, BTN_TOUCH, buf[0] & REPORT_BIT_PRESSED);
0063     input_sync(eeti->input);
0064 }
0065 
0066 static int eeti_ts_read(struct eeti_ts *eeti)
0067 {
0068     int len, error;
0069     char buf[6];
0070 
0071     len = i2c_master_recv(eeti->client, buf, sizeof(buf));
0072     if (len != sizeof(buf)) {
0073         error = len < 0 ? len : -EIO;
0074         dev_err(&eeti->client->dev,
0075             "failed to read touchscreen data: %d\n",
0076             error);
0077         return error;
0078     }
0079 
0080     /* Motion packet */
0081     if (buf[0] & 0x80)
0082         eeti_ts_report_event(eeti, buf);
0083 
0084     return 0;
0085 }
0086 
0087 static irqreturn_t eeti_ts_isr(int irq, void *dev_id)
0088 {
0089     struct eeti_ts *eeti = dev_id;
0090     int error;
0091 
0092     mutex_lock(&eeti->mutex);
0093 
0094     do {
0095         /*
0096          * If we have attention GPIO, trust it. Otherwise we'll read
0097          * once and exit. We assume that in this case we are using
0098          * level triggered interrupt and it will get raised again
0099          * if/when there is more data.
0100          */
0101         if (eeti->attn_gpio &&
0102             !gpiod_get_value_cansleep(eeti->attn_gpio)) {
0103             break;
0104         }
0105 
0106         error = eeti_ts_read(eeti);
0107         if (error)
0108             break;
0109 
0110     } while (eeti->running && eeti->attn_gpio);
0111 
0112     mutex_unlock(&eeti->mutex);
0113     return IRQ_HANDLED;
0114 }
0115 
0116 static void eeti_ts_start(struct eeti_ts *eeti)
0117 {
0118     mutex_lock(&eeti->mutex);
0119 
0120     eeti->running = true;
0121     enable_irq(eeti->client->irq);
0122 
0123     /*
0124      * Kick the controller in case we are using edge interrupt and
0125      * we missed our edge while interrupt was disabled. We expect
0126      * the attention GPIO to be wired in this case.
0127      */
0128     if (eeti->attn_gpio && gpiod_get_value_cansleep(eeti->attn_gpio))
0129         eeti_ts_read(eeti);
0130 
0131     mutex_unlock(&eeti->mutex);
0132 }
0133 
0134 static void eeti_ts_stop(struct eeti_ts *eeti)
0135 {
0136     /*
0137      * Not locking here, just setting a flag and expect that the
0138      * interrupt thread will notice the flag eventually.
0139      */
0140     eeti->running = false;
0141     wmb();
0142     disable_irq(eeti->client->irq);
0143 }
0144 
0145 static int eeti_ts_open(struct input_dev *dev)
0146 {
0147     struct eeti_ts *eeti = input_get_drvdata(dev);
0148 
0149     eeti_ts_start(eeti);
0150 
0151     return 0;
0152 }
0153 
0154 static void eeti_ts_close(struct input_dev *dev)
0155 {
0156     struct eeti_ts *eeti = input_get_drvdata(dev);
0157 
0158     eeti_ts_stop(eeti);
0159 }
0160 
0161 static int eeti_ts_probe(struct i2c_client *client,
0162              const struct i2c_device_id *idp)
0163 {
0164     struct device *dev = &client->dev;
0165     struct eeti_ts *eeti;
0166     struct input_dev *input;
0167     int error;
0168 
0169     /*
0170      * In contrast to what's described in the datasheet, there seems
0171      * to be no way of probing the presence of that device using I2C
0172      * commands. So we need to blindly believe it is there, and wait
0173      * for interrupts to occur.
0174      */
0175 
0176     eeti = devm_kzalloc(dev, sizeof(*eeti), GFP_KERNEL);
0177     if (!eeti) {
0178         dev_err(dev, "failed to allocate driver data\n");
0179         return -ENOMEM;
0180     }
0181 
0182     mutex_init(&eeti->mutex);
0183 
0184     input = devm_input_allocate_device(dev);
0185     if (!input) {
0186         dev_err(dev, "Failed to allocate input device.\n");
0187         return -ENOMEM;
0188     }
0189 
0190     input_set_capability(input, EV_KEY, BTN_TOUCH);
0191 
0192     input_set_abs_params(input, ABS_X, 0, EETI_MAXVAL, 0, 0);
0193     input_set_abs_params(input, ABS_Y, 0, EETI_MAXVAL, 0, 0);
0194     input_set_abs_params(input, ABS_PRESSURE, 0, 0xff, 0, 0);
0195 
0196     touchscreen_parse_properties(input, false, &eeti->props);
0197 
0198     input->name = client->name;
0199     input->id.bustype = BUS_I2C;
0200     input->open = eeti_ts_open;
0201     input->close = eeti_ts_close;
0202 
0203     eeti->client = client;
0204     eeti->input = input;
0205 
0206     eeti->attn_gpio = devm_gpiod_get_optional(dev, "attn", GPIOD_IN);
0207     if (IS_ERR(eeti->attn_gpio))
0208         return PTR_ERR(eeti->attn_gpio);
0209 
0210     i2c_set_clientdata(client, eeti);
0211     input_set_drvdata(input, eeti);
0212 
0213     error = devm_request_threaded_irq(dev, client->irq,
0214                       NULL, eeti_ts_isr,
0215                       IRQF_ONESHOT,
0216                       client->name, eeti);
0217     if (error) {
0218         dev_err(dev, "Unable to request touchscreen IRQ: %d\n",
0219             error);
0220         return error;
0221     }
0222 
0223     /*
0224      * Disable the device for now. It will be enabled once the
0225      * input device is opened.
0226      */
0227     eeti_ts_stop(eeti);
0228 
0229     error = input_register_device(input);
0230     if (error)
0231         return error;
0232 
0233     return 0;
0234 }
0235 
0236 static int __maybe_unused eeti_ts_suspend(struct device *dev)
0237 {
0238     struct i2c_client *client = to_i2c_client(dev);
0239     struct eeti_ts *eeti = i2c_get_clientdata(client);
0240     struct input_dev *input_dev = eeti->input;
0241 
0242     mutex_lock(&input_dev->mutex);
0243 
0244     if (input_device_enabled(input_dev))
0245         eeti_ts_stop(eeti);
0246 
0247     mutex_unlock(&input_dev->mutex);
0248 
0249     if (device_may_wakeup(&client->dev))
0250         enable_irq_wake(client->irq);
0251 
0252     return 0;
0253 }
0254 
0255 static int __maybe_unused eeti_ts_resume(struct device *dev)
0256 {
0257     struct i2c_client *client = to_i2c_client(dev);
0258     struct eeti_ts *eeti = i2c_get_clientdata(client);
0259     struct input_dev *input_dev = eeti->input;
0260 
0261     if (device_may_wakeup(&client->dev))
0262         disable_irq_wake(client->irq);
0263 
0264     mutex_lock(&input_dev->mutex);
0265 
0266     if (input_device_enabled(input_dev))
0267         eeti_ts_start(eeti);
0268 
0269     mutex_unlock(&input_dev->mutex);
0270 
0271     return 0;
0272 }
0273 
0274 static SIMPLE_DEV_PM_OPS(eeti_ts_pm, eeti_ts_suspend, eeti_ts_resume);
0275 
0276 static const struct i2c_device_id eeti_ts_id[] = {
0277     { "eeti_ts", 0 },
0278     { }
0279 };
0280 MODULE_DEVICE_TABLE(i2c, eeti_ts_id);
0281 
0282 #ifdef CONFIG_OF
0283 static const struct of_device_id of_eeti_ts_match[] = {
0284     { .compatible = "eeti,exc3000-i2c", },
0285     { }
0286 };
0287 #endif
0288 
0289 static struct i2c_driver eeti_ts_driver = {
0290     .driver = {
0291         .name = "eeti_ts",
0292         .pm = &eeti_ts_pm,
0293         .of_match_table = of_match_ptr(of_eeti_ts_match),
0294     },
0295     .probe = eeti_ts_probe,
0296     .id_table = eeti_ts_id,
0297 };
0298 
0299 module_i2c_driver(eeti_ts_driver);
0300 
0301 MODULE_DESCRIPTION("EETI Touchscreen driver");
0302 MODULE_AUTHOR("Daniel Mack <daniel@zonque.org>");
0303 MODULE_LICENSE("GPL");