2 * Microchip AR1021 driver for I2C
4 * Author: Christian Gmeiner <christian.gmeiner@gmail.com>
6 * License: GPLv2 as published by the FSF.
9 #include <linux/module.h>
10 #include <linux/input.h>
12 #include <linux/i2c.h>
13 #include <linux/irq.h>
14 #include <linux/interrupt.h>
16 #define AR1021_TOCUH_PKG_SIZE 5
18 #define AR1021_MAX_X 4095
19 #define AR1021_MAX_Y 4095
22 struct i2c_client *client;
23 struct input_dev *input;
24 u8 data[AR1021_TOCUH_PKG_SIZE];
27 static irqreturn_t ar1021_i2c_irq(int irq, void *dev_id)
29 struct ar1021_i2c *ar1021 = dev_id;
30 struct input_dev *input = ar1021->input;
31 u8 *data = ar1021->data;
32 unsigned int x, y, button;
35 retval = i2c_master_recv(ar1021->client,
36 ar1021->data, sizeof(ar1021->data));
37 if (retval != sizeof(ar1021->data))
41 if ((data[0] & 0x80) == 0)
44 button = data[0] & BIT(0);
45 x = ((data[2] & 0x1f) << 7) | (data[1] & 0x7f);
46 y = ((data[4] & 0x1f) << 7) | (data[3] & 0x7f);
48 input_report_abs(input, ABS_X, x);
49 input_report_abs(input, ABS_Y, y);
50 input_report_key(input, BTN_TOUCH, button);
57 static int ar1021_i2c_open(struct input_dev *dev)
59 struct ar1021_i2c *ar1021 = input_get_drvdata(dev);
60 struct i2c_client *client = ar1021->client;
62 enable_irq(client->irq);
67 static void ar1021_i2c_close(struct input_dev *dev)
69 struct ar1021_i2c *ar1021 = input_get_drvdata(dev);
70 struct i2c_client *client = ar1021->client;
72 disable_irq(client->irq);
75 static int ar1021_i2c_probe(struct i2c_client *client,
76 const struct i2c_device_id *id)
78 struct ar1021_i2c *ar1021;
79 struct input_dev *input;
82 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
83 dev_err(&client->dev, "i2c_check_functionality error\n");
87 ar1021 = devm_kzalloc(&client->dev, sizeof(*ar1021), GFP_KERNEL);
91 input = devm_input_allocate_device(&client->dev);
95 ar1021->client = client;
96 ar1021->input = input;
98 input->name = "ar1021 I2C Touchscreen";
99 input->id.bustype = BUS_I2C;
100 input->dev.parent = &client->dev;
101 input->open = ar1021_i2c_open;
102 input->close = ar1021_i2c_close;
104 input_set_capability(input, EV_KEY, BTN_TOUCH);
105 input_set_abs_params(input, ABS_X, 0, AR1021_MAX_X, 0, 0);
106 input_set_abs_params(input, ABS_Y, 0, AR1021_MAX_Y, 0, 0);
108 input_set_drvdata(input, ar1021);
110 error = devm_request_threaded_irq(&client->dev, client->irq,
111 NULL, ar1021_i2c_irq,
112 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
113 "ar1021_i2c", ar1021);
115 dev_err(&client->dev,
116 "Failed to enable IRQ, error: %d\n", error);
120 /* Disable the IRQ, we'll enable it in ar1021_i2c_open() */
121 disable_irq(client->irq);
123 error = input_register_device(ar1021->input);
125 dev_err(&client->dev,
126 "Failed to register input device, error: %d\n", error);
130 i2c_set_clientdata(client, ar1021);
134 static int __maybe_unused ar1021_i2c_suspend(struct device *dev)
136 struct i2c_client *client = to_i2c_client(dev);
138 disable_irq(client->irq);
143 static int __maybe_unused ar1021_i2c_resume(struct device *dev)
145 struct i2c_client *client = to_i2c_client(dev);
147 enable_irq(client->irq);
152 static SIMPLE_DEV_PM_OPS(ar1021_i2c_pm, ar1021_i2c_suspend, ar1021_i2c_resume);
154 static const struct i2c_device_id ar1021_i2c_id[] = {
158 MODULE_DEVICE_TABLE(i2c, ar1021_i2c_id);
160 static const struct of_device_id ar1021_i2c_of_match[] = {
161 { .compatible = "microchip,ar1021-i2c", },
164 MODULE_DEVICE_TABLE(of, ar1021_i2c_of_match);
166 static struct i2c_driver ar1021_i2c_driver = {
168 .name = "ar1021_i2c",
169 .pm = &ar1021_i2c_pm,
170 .of_match_table = ar1021_i2c_of_match,
173 .probe = ar1021_i2c_probe,
174 .id_table = ar1021_i2c_id,
176 module_i2c_driver(ar1021_i2c_driver);
178 MODULE_AUTHOR("Christian Gmeiner <christian.gmeiner@gmail.com>");
179 MODULE_DESCRIPTION("Microchip AR1021 I2C Driver");
180 MODULE_LICENSE("GPL");