Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Microchip AR1020 and AR1021 driver for I2C
0004  *
0005  * Author: Christian Gmeiner <christian.gmeiner@gmail.com>
0006  */
0007 
0008 #include <linux/bitops.h>
0009 #include <linux/module.h>
0010 #include <linux/input.h>
0011 #include <linux/of.h>
0012 #include <linux/i2c.h>
0013 #include <linux/irq.h>
0014 #include <linux/interrupt.h>
0015 
0016 #define AR1021_TOUCH_PKG_SIZE   5
0017 
0018 #define AR1021_MAX_X    4095
0019 #define AR1021_MAX_Y    4095
0020 
0021 #define AR1021_CMD  0x55
0022 
0023 #define AR1021_CMD_ENABLE_TOUCH     0x12
0024 
0025 struct ar1021_i2c {
0026     struct i2c_client *client;
0027     struct input_dev *input;
0028     u8 data[AR1021_TOUCH_PKG_SIZE];
0029 };
0030 
0031 static irqreturn_t ar1021_i2c_irq(int irq, void *dev_id)
0032 {
0033     struct ar1021_i2c *ar1021 = dev_id;
0034     struct input_dev *input = ar1021->input;
0035     u8 *data = ar1021->data;
0036     unsigned int x, y, button;
0037     int retval;
0038 
0039     retval = i2c_master_recv(ar1021->client,
0040                  ar1021->data, sizeof(ar1021->data));
0041     if (retval != sizeof(ar1021->data))
0042         goto out;
0043 
0044     /* sync bit set ? */
0045     if (!(data[0] & BIT(7)))
0046         goto out;
0047 
0048     button = data[0] & BIT(0);
0049     x = ((data[2] & 0x1f) << 7) | (data[1] & 0x7f);
0050     y = ((data[4] & 0x1f) << 7) | (data[3] & 0x7f);
0051 
0052     input_report_abs(input, ABS_X, x);
0053     input_report_abs(input, ABS_Y, y);
0054     input_report_key(input, BTN_TOUCH, button);
0055     input_sync(input);
0056 
0057 out:
0058     return IRQ_HANDLED;
0059 }
0060 
0061 static int ar1021_i2c_open(struct input_dev *dev)
0062 {
0063     static const u8 cmd_enable_touch[] = {
0064         AR1021_CMD,
0065         0x01, /* number of bytes after this */
0066         AR1021_CMD_ENABLE_TOUCH
0067     };
0068     struct ar1021_i2c *ar1021 = input_get_drvdata(dev);
0069     struct i2c_client *client = ar1021->client;
0070     int error;
0071 
0072     error = i2c_master_send(ar1021->client, cmd_enable_touch,
0073                 sizeof(cmd_enable_touch));
0074     if (error < 0)
0075         return error;
0076 
0077     enable_irq(client->irq);
0078 
0079     return 0;
0080 }
0081 
0082 static void ar1021_i2c_close(struct input_dev *dev)
0083 {
0084     struct ar1021_i2c *ar1021 = input_get_drvdata(dev);
0085     struct i2c_client *client = ar1021->client;
0086 
0087     disable_irq(client->irq);
0088 }
0089 
0090 static int ar1021_i2c_probe(struct i2c_client *client,
0091                 const struct i2c_device_id *id)
0092 {
0093     struct ar1021_i2c *ar1021;
0094     struct input_dev *input;
0095     int error;
0096 
0097     if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
0098         dev_err(&client->dev, "i2c_check_functionality error\n");
0099         return -ENXIO;
0100     }
0101 
0102     ar1021 = devm_kzalloc(&client->dev, sizeof(*ar1021), GFP_KERNEL);
0103     if (!ar1021)
0104         return -ENOMEM;
0105 
0106     input = devm_input_allocate_device(&client->dev);
0107     if (!input)
0108         return -ENOMEM;
0109 
0110     ar1021->client = client;
0111     ar1021->input = input;
0112 
0113     input->name = "ar1021 I2C Touchscreen";
0114     input->id.bustype = BUS_I2C;
0115     input->dev.parent = &client->dev;
0116     input->open = ar1021_i2c_open;
0117     input->close = ar1021_i2c_close;
0118 
0119     __set_bit(INPUT_PROP_DIRECT, input->propbit);
0120     input_set_capability(input, EV_KEY, BTN_TOUCH);
0121     input_set_abs_params(input, ABS_X, 0, AR1021_MAX_X, 0, 0);
0122     input_set_abs_params(input, ABS_Y, 0, AR1021_MAX_Y, 0, 0);
0123 
0124     input_set_drvdata(input, ar1021);
0125 
0126     error = devm_request_threaded_irq(&client->dev, client->irq,
0127                       NULL, ar1021_i2c_irq,
0128                       IRQF_ONESHOT | IRQF_NO_AUTOEN,
0129                       "ar1021_i2c", ar1021);
0130     if (error) {
0131         dev_err(&client->dev,
0132             "Failed to enable IRQ, error: %d\n", error);
0133         return error;
0134     }
0135 
0136     error = input_register_device(ar1021->input);
0137     if (error) {
0138         dev_err(&client->dev,
0139             "Failed to register input device, error: %d\n", error);
0140         return error;
0141     }
0142 
0143     return 0;
0144 }
0145 
0146 static int __maybe_unused ar1021_i2c_suspend(struct device *dev)
0147 {
0148     struct i2c_client *client = to_i2c_client(dev);
0149 
0150     disable_irq(client->irq);
0151 
0152     return 0;
0153 }
0154 
0155 static int __maybe_unused ar1021_i2c_resume(struct device *dev)
0156 {
0157     struct i2c_client *client = to_i2c_client(dev);
0158 
0159     enable_irq(client->irq);
0160 
0161     return 0;
0162 }
0163 
0164 static SIMPLE_DEV_PM_OPS(ar1021_i2c_pm, ar1021_i2c_suspend, ar1021_i2c_resume);
0165 
0166 static const struct i2c_device_id ar1021_i2c_id[] = {
0167     { "ar1021", 0 },
0168     { },
0169 };
0170 MODULE_DEVICE_TABLE(i2c, ar1021_i2c_id);
0171 
0172 static const struct of_device_id ar1021_i2c_of_match[] = {
0173     { .compatible = "microchip,ar1021-i2c", },
0174     { }
0175 };
0176 MODULE_DEVICE_TABLE(of, ar1021_i2c_of_match);
0177 
0178 static struct i2c_driver ar1021_i2c_driver = {
0179     .driver = {
0180         .name   = "ar1021_i2c",
0181         .pm = &ar1021_i2c_pm,
0182         .of_match_table = ar1021_i2c_of_match,
0183     },
0184 
0185     .probe      = ar1021_i2c_probe,
0186     .id_table   = ar1021_i2c_id,
0187 };
0188 module_i2c_driver(ar1021_i2c_driver);
0189 
0190 MODULE_AUTHOR("Christian Gmeiner <christian.gmeiner@gmail.com>");
0191 MODULE_DESCRIPTION("Microchip AR1020 and AR1021 I2C Driver");
0192 MODULE_LICENSE("GPL");