Linux 4.1.16
[linux/fpc-iii.git] / drivers / input / touchscreen / ar1021_i2c.c
blobf0b954d46a259726d94766e0191f4523351c748a
1 /*
2 * Microchip AR1021 driver for I2C
4 * Author: Christian Gmeiner <christian.gmeiner@gmail.com>
6 * License: GPLv2 as published by the FSF.
7 */
9 #include <linux/module.h>
10 #include <linux/input.h>
11 #include <linux/of.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
21 struct ar1021_i2c {
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;
33 int retval;
35 retval = i2c_master_recv(ar1021->client,
36 ar1021->data, sizeof(ar1021->data));
37 if (retval != sizeof(ar1021->data))
38 goto out;
40 /* sync bit set ? */
41 if ((data[0] & 0x80) == 0)
42 goto out;
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);
51 input_sync(input);
53 out:
54 return IRQ_HANDLED;
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);
64 return 0;
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;
80 int error;
82 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
83 dev_err(&client->dev, "i2c_check_functionality error\n");
84 return -ENXIO;
87 ar1021 = devm_kzalloc(&client->dev, sizeof(*ar1021), GFP_KERNEL);
88 if (!ar1021)
89 return -ENOMEM;
91 input = devm_input_allocate_device(&client->dev);
92 if (!input)
93 return -ENOMEM;
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);
114 if (error) {
115 dev_err(&client->dev,
116 "Failed to enable IRQ, error: %d\n", error);
117 return 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);
124 if (error) {
125 dev_err(&client->dev,
126 "Failed to register input device, error: %d\n", error);
127 return error;
130 i2c_set_clientdata(client, ar1021);
131 return 0;
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);
140 return 0;
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);
149 return 0;
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[] = {
155 { "MICROCHIP_AR1021_I2C", 0 },
156 { },
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 = {
167 .driver = {
168 .name = "ar1021_i2c",
169 .owner = THIS_MODULE,
170 .pm = &ar1021_i2c_pm,
171 .of_match_table = ar1021_i2c_of_match,
174 .probe = ar1021_i2c_probe,
175 .id_table = ar1021_i2c_id,
177 module_i2c_driver(ar1021_i2c_driver);
179 MODULE_AUTHOR("Christian Gmeiner <christian.gmeiner@gmail.com>");
180 MODULE_DESCRIPTION("Microchip AR1021 I2C Driver");
181 MODULE_LICENSE("GPL");