1 /*
2  * Microchip AR1021 driver for I2C
3  *
4  * Author: Christian Gmeiner <christian.gmeiner@gmail.com>
5  *
6  * License: GPLv2 as published by the FSF.
7  */
8 
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>
15 
16 #define AR1021_TOCUH_PKG_SIZE	5
17 
18 #define AR1021_MAX_X	4095
19 #define AR1021_MAX_Y	4095
20 
21 struct ar1021_i2c {
22 	struct i2c_client *client;
23 	struct input_dev *input;
24 	u8 data[AR1021_TOCUH_PKG_SIZE];
25 };
26 
27 static irqreturn_t ar1021_i2c_irq(int irq, void *dev_id)
28 {
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;
34 
35 	retval = i2c_master_recv(ar1021->client,
36 				ar1021->data, sizeof(ar1021->data));
37 	if (retval != sizeof(ar1021->data))
38 		goto out;
39 
40 	/* sync bit set ? */
41 	if ((data[0] & 0x80) == 0)
42 		goto out;
43 
44 	button = data[0] & BIT(0);
45 	x = ((data[2] & 0x1f) << 7) | (data[1] & 0x7f);
46 	y = ((data[4] & 0x1f) << 7) | (data[3] & 0x7f);
47 
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);
52 
53 out:
54 	return IRQ_HANDLED;
55 }
56 
57 static int ar1021_i2c_open(struct input_dev *dev)
58 {
59 	struct ar1021_i2c *ar1021 = input_get_drvdata(dev);
60 	struct i2c_client *client = ar1021->client;
61 
62 	enable_irq(client->irq);
63 
64 	return 0;
65 }
66 
67 static void ar1021_i2c_close(struct input_dev *dev)
68 {
69 	struct ar1021_i2c *ar1021 = input_get_drvdata(dev);
70 	struct i2c_client *client = ar1021->client;
71 
72 	disable_irq(client->irq);
73 }
74 
75 static int ar1021_i2c_probe(struct i2c_client *client,
76 				     const struct i2c_device_id *id)
77 {
78 	struct ar1021_i2c *ar1021;
79 	struct input_dev *input;
80 	int error;
81 
82 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
83 		dev_err(&client->dev, "i2c_check_functionality error\n");
84 		return -ENXIO;
85 	}
86 
87 	ar1021 = devm_kzalloc(&client->dev, sizeof(*ar1021), GFP_KERNEL);
88 	if (!ar1021)
89 		return -ENOMEM;
90 
91 	input = devm_input_allocate_device(&client->dev);
92 	if (!input)
93 		return -ENOMEM;
94 
95 	ar1021->client = client;
96 	ar1021->input = input;
97 
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;
103 
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);
107 
108 	input_set_drvdata(input, ar1021);
109 
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;
118 	}
119 
120 	/* Disable the IRQ, we'll enable it in ar1021_i2c_open() */
121 	disable_irq(client->irq);
122 
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;
128 	}
129 
130 	i2c_set_clientdata(client, ar1021);
131 	return 0;
132 }
133 
134 static int __maybe_unused ar1021_i2c_suspend(struct device *dev)
135 {
136 	struct i2c_client *client = to_i2c_client(dev);
137 
138 	disable_irq(client->irq);
139 
140 	return 0;
141 }
142 
143 static int __maybe_unused ar1021_i2c_resume(struct device *dev)
144 {
145 	struct i2c_client *client = to_i2c_client(dev);
146 
147 	enable_irq(client->irq);
148 
149 	return 0;
150 }
151 
152 static SIMPLE_DEV_PM_OPS(ar1021_i2c_pm, ar1021_i2c_suspend, ar1021_i2c_resume);
153 
154 static const struct i2c_device_id ar1021_i2c_id[] = {
155 	{ "MICROCHIP_AR1021_I2C", 0 },
156 	{ },
157 };
158 MODULE_DEVICE_TABLE(i2c, ar1021_i2c_id);
159 
160 static const struct of_device_id ar1021_i2c_of_match[] = {
161 	{ .compatible = "microchip,ar1021-i2c", },
162 	{ }
163 };
164 MODULE_DEVICE_TABLE(of, ar1021_i2c_of_match);
165 
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,
172 	},
173 
174 	.probe		= ar1021_i2c_probe,
175 	.id_table	= ar1021_i2c_id,
176 };
177 module_i2c_driver(ar1021_i2c_driver);
178 
179 MODULE_AUTHOR("Christian Gmeiner <christian.gmeiner@gmail.com>");
180 MODULE_DESCRIPTION("Microchip AR1021 I2C Driver");
181 MODULE_LICENSE("GPL");
182