Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * Touch Screen driver for Renesas MIGO-R Platform
0004  *
0005  * Copyright (c) 2008 Magnus Damm
0006  * Copyright (c) 2007 Ujjwal Pande <ujjwal@kenati.com>,
0007  *  Kenati Technologies Pvt Ltd.
0008  */
0009 #include <linux/module.h>
0010 #include <linux/kernel.h>
0011 #include <linux/input.h>
0012 #include <linux/interrupt.h>
0013 #include <linux/pm.h>
0014 #include <linux/slab.h>
0015 #include <asm/io.h>
0016 #include <linux/i2c.h>
0017 #include <linux/timer.h>
0018 
0019 #define EVENT_PENDOWN 1
0020 #define EVENT_REPEAT  2
0021 #define EVENT_PENUP   3
0022 
0023 struct migor_ts_priv {
0024     struct i2c_client *client;
0025     struct input_dev *input;
0026     int irq;
0027 };
0028 
0029 static const u_int8_t migor_ts_ena_seq[17] = { 0x33, 0x22, 0x11,
0030                            0x01, 0x06, 0x07, };
0031 static const u_int8_t migor_ts_dis_seq[17] = { };
0032 
0033 static irqreturn_t migor_ts_isr(int irq, void *dev_id)
0034 {
0035     struct migor_ts_priv *priv = dev_id;
0036     unsigned short xpos, ypos;
0037     unsigned char event;
0038     u_int8_t buf[16];
0039 
0040     /*
0041      * The touch screen controller chip is hooked up to the CPU
0042      * using I2C and a single interrupt line. The interrupt line
0043      * is pulled low whenever someone taps the screen. To deassert
0044      * the interrupt line we need to acknowledge the interrupt by
0045      * communicating with the controller over the slow i2c bus.
0046      *
0047      * Since I2C bus controller may sleep we are using threaded
0048      * IRQ here.
0049      */
0050 
0051     memset(buf, 0, sizeof(buf));
0052 
0053     /* Set Index 0 */
0054     buf[0] = 0;
0055     if (i2c_master_send(priv->client, buf, 1) != 1) {
0056         dev_err(&priv->client->dev, "Unable to write i2c index\n");
0057         goto out;
0058     }
0059 
0060     /* Now do Page Read */
0061     if (i2c_master_recv(priv->client, buf, sizeof(buf)) != sizeof(buf)) {
0062         dev_err(&priv->client->dev, "Unable to read i2c page\n");
0063         goto out;
0064     }
0065 
0066     ypos = ((buf[9] & 0x03) << 8 | buf[8]);
0067     xpos = ((buf[11] & 0x03) << 8 | buf[10]);
0068     event = buf[12];
0069 
0070     switch (event) {
0071     case EVENT_PENDOWN:
0072     case EVENT_REPEAT:
0073         input_report_key(priv->input, BTN_TOUCH, 1);
0074         input_report_abs(priv->input, ABS_X, ypos); /*X-Y swap*/
0075         input_report_abs(priv->input, ABS_Y, xpos);
0076         input_sync(priv->input);
0077         break;
0078 
0079     case EVENT_PENUP:
0080         input_report_key(priv->input, BTN_TOUCH, 0);
0081         input_sync(priv->input);
0082         break;
0083     }
0084 
0085  out:
0086     return IRQ_HANDLED;
0087 }
0088 
0089 static int migor_ts_open(struct input_dev *dev)
0090 {
0091     struct migor_ts_priv *priv = input_get_drvdata(dev);
0092     struct i2c_client *client = priv->client;
0093     int count;
0094 
0095     /* enable controller */
0096     count = i2c_master_send(client, migor_ts_ena_seq,
0097                 sizeof(migor_ts_ena_seq));
0098     if (count != sizeof(migor_ts_ena_seq)) {
0099         dev_err(&client->dev, "Unable to enable touchscreen.\n");
0100         return -ENXIO;
0101     }
0102 
0103     return 0;
0104 }
0105 
0106 static void migor_ts_close(struct input_dev *dev)
0107 {
0108     struct migor_ts_priv *priv = input_get_drvdata(dev);
0109     struct i2c_client *client = priv->client;
0110 
0111     disable_irq(priv->irq);
0112 
0113     /* disable controller */
0114     i2c_master_send(client, migor_ts_dis_seq, sizeof(migor_ts_dis_seq));
0115 
0116     enable_irq(priv->irq);
0117 }
0118 
0119 static int migor_ts_probe(struct i2c_client *client,
0120               const struct i2c_device_id *idp)
0121 {
0122     struct migor_ts_priv *priv;
0123     struct input_dev *input;
0124     int error;
0125 
0126     priv = kzalloc(sizeof(*priv), GFP_KERNEL);
0127     input = input_allocate_device();
0128     if (!priv || !input) {
0129         dev_err(&client->dev, "failed to allocate memory\n");
0130         error = -ENOMEM;
0131         goto err_free_mem;
0132     }
0133 
0134     priv->client = client;
0135     priv->input = input;
0136     priv->irq = client->irq;
0137 
0138     input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
0139 
0140     __set_bit(BTN_TOUCH, input->keybit);
0141 
0142     input_set_abs_params(input, ABS_X, 95, 955, 0, 0);
0143     input_set_abs_params(input, ABS_Y, 85, 935, 0, 0);
0144 
0145     input->name = client->name;
0146     input->id.bustype = BUS_I2C;
0147     input->dev.parent = &client->dev;
0148 
0149     input->open = migor_ts_open;
0150     input->close = migor_ts_close;
0151 
0152     input_set_drvdata(input, priv);
0153 
0154     error = request_threaded_irq(priv->irq, NULL, migor_ts_isr,
0155                                      IRQF_TRIGGER_LOW | IRQF_ONESHOT,
0156                                      client->name, priv);
0157     if (error) {
0158         dev_err(&client->dev, "Unable to request touchscreen IRQ.\n");
0159         goto err_free_mem;
0160     }
0161 
0162     error = input_register_device(input);
0163     if (error)
0164         goto err_free_irq;
0165 
0166     i2c_set_clientdata(client, priv);
0167     device_init_wakeup(&client->dev, 1);
0168 
0169     return 0;
0170 
0171  err_free_irq:
0172     free_irq(priv->irq, priv);
0173  err_free_mem:
0174     input_free_device(input);
0175     kfree(priv);
0176     return error;
0177 }
0178 
0179 static int migor_ts_remove(struct i2c_client *client)
0180 {
0181     struct migor_ts_priv *priv = i2c_get_clientdata(client);
0182 
0183     free_irq(priv->irq, priv);
0184     input_unregister_device(priv->input);
0185     kfree(priv);
0186 
0187     dev_set_drvdata(&client->dev, NULL);
0188 
0189     return 0;
0190 }
0191 
0192 static int __maybe_unused migor_ts_suspend(struct device *dev)
0193 {
0194     struct i2c_client *client = to_i2c_client(dev);
0195     struct migor_ts_priv *priv = i2c_get_clientdata(client);
0196 
0197     if (device_may_wakeup(&client->dev))
0198         enable_irq_wake(priv->irq);
0199 
0200     return 0;
0201 }
0202 
0203 static int __maybe_unused migor_ts_resume(struct device *dev)
0204 {
0205     struct i2c_client *client = to_i2c_client(dev);
0206     struct migor_ts_priv *priv = i2c_get_clientdata(client);
0207 
0208     if (device_may_wakeup(&client->dev))
0209         disable_irq_wake(priv->irq);
0210 
0211     return 0;
0212 }
0213 
0214 static SIMPLE_DEV_PM_OPS(migor_ts_pm, migor_ts_suspend, migor_ts_resume);
0215 
0216 static const struct i2c_device_id migor_ts_id[] = {
0217     { "migor_ts", 0 },
0218     { }
0219 };
0220 MODULE_DEVICE_TABLE(i2c, migor_ts_id);
0221 
0222 static struct i2c_driver migor_ts_driver = {
0223     .driver = {
0224         .name = "migor_ts",
0225         .pm = &migor_ts_pm,
0226     },
0227     .probe = migor_ts_probe,
0228     .remove = migor_ts_remove,
0229     .id_table = migor_ts_id,
0230 };
0231 
0232 module_i2c_driver(migor_ts_driver);
0233 
0234 MODULE_DESCRIPTION("MigoR Touchscreen driver");
0235 MODULE_AUTHOR("Magnus Damm <damm@opensource.se>");
0236 MODULE_LICENSE("GPL");