xref: /openbmc/linux/drivers/input/touchscreen/ili210x.c (revision 201f3c803544c052aa1bab9e562e0ada4aefe03d)
1 #include <linux/module.h>
2 #include <linux/i2c.h>
3 #include <linux/interrupt.h>
4 #include <linux/slab.h>
5 #include <linux/input.h>
6 #include <linux/input/mt.h>
7 #include <linux/delay.h>
8 #include <linux/workqueue.h>
9 #include <linux/gpio/consumer.h>
10 
11 #define MAX_TOUCHES		2
12 #define DEFAULT_POLL_PERIOD	20
13 
14 /* Touchscreen commands */
15 #define REG_TOUCHDATA		0x10
16 #define REG_PANEL_INFO		0x20
17 #define REG_FIRMWARE_VERSION	0x40
18 #define REG_CALIBRATE		0xcc
19 
20 struct finger {
21 	u8 x_low;
22 	u8 x_high;
23 	u8 y_low;
24 	u8 y_high;
25 } __packed;
26 
27 struct touchdata {
28 	u8 status;
29 	struct finger finger[MAX_TOUCHES];
30 } __packed;
31 
32 struct panel_info {
33 	struct finger finger_max;
34 	u8 xchannel_num;
35 	u8 ychannel_num;
36 } __packed;
37 
38 struct firmware_version {
39 	u8 id;
40 	u8 major;
41 	u8 minor;
42 } __packed;
43 
44 struct ili210x {
45 	struct i2c_client *client;
46 	struct input_dev *input;
47 	unsigned int poll_period;
48 	struct delayed_work dwork;
49 	struct gpio_desc *reset_gpio;
50 };
51 
52 static int ili210x_read_reg(struct i2c_client *client, u8 reg, void *buf,
53 			    size_t len)
54 {
55 	struct i2c_msg msg[2] = {
56 		{
57 			.addr	= client->addr,
58 			.flags	= 0,
59 			.len	= 1,
60 			.buf	= &reg,
61 		},
62 		{
63 			.addr	= client->addr,
64 			.flags	= I2C_M_RD,
65 			.len	= len,
66 			.buf	= buf,
67 		}
68 	};
69 
70 	if (i2c_transfer(client->adapter, msg, 2) != 2) {
71 		dev_err(&client->dev, "i2c transfer failed\n");
72 		return -EIO;
73 	}
74 
75 	return 0;
76 }
77 
78 static void ili210x_report_events(struct input_dev *input,
79 				  const struct touchdata *touchdata)
80 {
81 	int i;
82 	bool touch;
83 	unsigned int x, y;
84 	const struct finger *finger;
85 
86 	for (i = 0; i < MAX_TOUCHES; i++) {
87 		input_mt_slot(input, i);
88 
89 		finger = &touchdata->finger[i];
90 
91 		touch = touchdata->status & (1 << i);
92 		input_mt_report_slot_state(input, MT_TOOL_FINGER, touch);
93 		if (touch) {
94 			x = finger->x_low | (finger->x_high << 8);
95 			y = finger->y_low | (finger->y_high << 8);
96 
97 			input_report_abs(input, ABS_MT_POSITION_X, x);
98 			input_report_abs(input, ABS_MT_POSITION_Y, y);
99 		}
100 	}
101 
102 	input_mt_report_pointer_emulation(input, false);
103 	input_sync(input);
104 }
105 
106 static void ili210x_work(struct work_struct *work)
107 {
108 	struct ili210x *priv = container_of(work, struct ili210x,
109 					    dwork.work);
110 	struct i2c_client *client = priv->client;
111 	struct touchdata touchdata;
112 	int error;
113 
114 	error = ili210x_read_reg(client, REG_TOUCHDATA,
115 				 &touchdata, sizeof(touchdata));
116 	if (error) {
117 		dev_err(&client->dev,
118 			"Unable to get touchdata, err = %d\n", error);
119 		return;
120 	}
121 
122 	ili210x_report_events(priv->input, &touchdata);
123 
124 	if (touchdata.status & 0xf3)
125 		schedule_delayed_work(&priv->dwork,
126 				      msecs_to_jiffies(priv->poll_period));
127 }
128 
129 static irqreturn_t ili210x_irq(int irq, void *irq_data)
130 {
131 	struct ili210x *priv = irq_data;
132 
133 	schedule_delayed_work(&priv->dwork, 0);
134 
135 	return IRQ_HANDLED;
136 }
137 
138 static ssize_t ili210x_calibrate(struct device *dev,
139 				 struct device_attribute *attr,
140 				 const char *buf, size_t count)
141 {
142 	struct i2c_client *client = to_i2c_client(dev);
143 	struct ili210x *priv = i2c_get_clientdata(client);
144 	unsigned long calibrate;
145 	int rc;
146 	u8 cmd = REG_CALIBRATE;
147 
148 	if (kstrtoul(buf, 10, &calibrate))
149 		return -EINVAL;
150 
151 	if (calibrate > 1)
152 		return -EINVAL;
153 
154 	if (calibrate) {
155 		rc = i2c_master_send(priv->client, &cmd, sizeof(cmd));
156 		if (rc != sizeof(cmd))
157 			return -EIO;
158 	}
159 
160 	return count;
161 }
162 static DEVICE_ATTR(calibrate, S_IWUSR, NULL, ili210x_calibrate);
163 
164 static struct attribute *ili210x_attributes[] = {
165 	&dev_attr_calibrate.attr,
166 	NULL,
167 };
168 
169 static const struct attribute_group ili210x_attr_group = {
170 	.attrs = ili210x_attributes,
171 };
172 
173 static void ili210x_power_down(void *data)
174 {
175 	struct gpio_desc *reset_gpio = data;
176 
177 	gpiod_set_value_cansleep(reset_gpio, 1);
178 }
179 
180 static int ili210x_i2c_probe(struct i2c_client *client,
181 				       const struct i2c_device_id *id)
182 {
183 	struct device *dev = &client->dev;
184 	struct ili210x *priv;
185 	struct gpio_desc *reset_gpio;
186 	struct input_dev *input;
187 	struct panel_info panel;
188 	struct firmware_version firmware;
189 	int xmax, ymax;
190 	int error;
191 
192 	dev_dbg(dev, "Probing for ILI210X I2C Touschreen driver");
193 
194 	if (client->irq <= 0) {
195 		dev_err(dev, "No IRQ!\n");
196 		return -EINVAL;
197 	}
198 
199 	reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
200 	if (IS_ERR(reset_gpio))
201 		return PTR_ERR(reset_gpio);
202 
203 	if (reset_gpio) {
204 		error = devm_add_action_or_reset(dev, ili210x_power_down,
205 						 reset_gpio);
206 		if (error)
207 			return error;
208 
209 		usleep_range(50, 100);
210 		gpiod_set_value_cansleep(reset_gpio, 0);
211 		msleep(100);
212 	}
213 
214 	/* Get firmware version */
215 	error = ili210x_read_reg(client, REG_FIRMWARE_VERSION,
216 				 &firmware, sizeof(firmware));
217 	if (error) {
218 		dev_err(dev, "Failed to get firmware version, err: %d\n",
219 			error);
220 		return error;
221 	}
222 
223 	/* get panel info */
224 	error = ili210x_read_reg(client, REG_PANEL_INFO, &panel, sizeof(panel));
225 	if (error) {
226 		dev_err(dev, "Failed to get panel information, err: %d\n",
227 			error);
228 		return error;
229 	}
230 
231 	xmax = panel.finger_max.x_low | (panel.finger_max.x_high << 8);
232 	ymax = panel.finger_max.y_low | (panel.finger_max.y_high << 8);
233 
234 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
235 	if (!priv)
236 		return -ENOMEM;
237 
238 	input = devm_input_allocate_device(dev);
239 	if (!input)
240 		return -ENOMEM;
241 
242 	priv->client = client;
243 	priv->input = input;
244 	priv->poll_period = DEFAULT_POLL_PERIOD;
245 	INIT_DELAYED_WORK(&priv->dwork, ili210x_work);
246 	priv->reset_gpio = reset_gpio;
247 
248 	/* Setup input device */
249 	input->name = "ILI210x Touchscreen";
250 	input->id.bustype = BUS_I2C;
251 	input->dev.parent = dev;
252 
253 	__set_bit(EV_SYN, input->evbit);
254 	__set_bit(EV_KEY, input->evbit);
255 	__set_bit(EV_ABS, input->evbit);
256 	__set_bit(BTN_TOUCH, input->keybit);
257 
258 	/* Single touch */
259 	input_set_abs_params(input, ABS_X, 0, xmax, 0, 0);
260 	input_set_abs_params(input, ABS_Y, 0, ymax, 0, 0);
261 
262 	/* Multi touch */
263 	input_mt_init_slots(input, MAX_TOUCHES, 0);
264 	input_set_abs_params(input, ABS_MT_POSITION_X, 0, xmax, 0, 0);
265 	input_set_abs_params(input, ABS_MT_POSITION_Y, 0, ymax, 0, 0);
266 
267 	i2c_set_clientdata(client, priv);
268 
269 	error = request_irq(client->irq, ili210x_irq, 0,
270 			    client->name, priv);
271 	if (error) {
272 		dev_err(dev, "Unable to request touchscreen IRQ, err: %d\n",
273 			error);
274 		goto err_free_mem;
275 	}
276 
277 	error = sysfs_create_group(&dev->kobj, &ili210x_attr_group);
278 	if (error) {
279 		dev_err(dev, "Unable to create sysfs attributes, err: %d\n",
280 			error);
281 		goto err_free_irq;
282 	}
283 
284 	error = input_register_device(priv->input);
285 	if (error) {
286 		dev_err(dev, "Cannot register input device, err: %d\n", error);
287 		goto err_remove_sysfs;
288 	}
289 
290 	device_init_wakeup(dev, 1);
291 
292 	dev_dbg(dev,
293 		"ILI210x initialized (IRQ: %d), firmware version %d.%d.%d",
294 		client->irq, firmware.id, firmware.major, firmware.minor);
295 
296 	return 0;
297 
298 err_remove_sysfs:
299 	sysfs_remove_group(&dev->kobj, &ili210x_attr_group);
300 err_free_irq:
301 	free_irq(client->irq, priv);
302 err_free_mem:
303 	return error;
304 }
305 
306 static int ili210x_i2c_remove(struct i2c_client *client)
307 {
308 	struct ili210x *priv = i2c_get_clientdata(client);
309 
310 	sysfs_remove_group(&client->dev.kobj, &ili210x_attr_group);
311 	free_irq(priv->client->irq, priv);
312 	cancel_delayed_work_sync(&priv->dwork);
313 
314 	return 0;
315 }
316 
317 static int __maybe_unused ili210x_i2c_suspend(struct device *dev)
318 {
319 	struct i2c_client *client = to_i2c_client(dev);
320 
321 	if (device_may_wakeup(&client->dev))
322 		enable_irq_wake(client->irq);
323 
324 	return 0;
325 }
326 
327 static int __maybe_unused ili210x_i2c_resume(struct device *dev)
328 {
329 	struct i2c_client *client = to_i2c_client(dev);
330 
331 	if (device_may_wakeup(&client->dev))
332 		disable_irq_wake(client->irq);
333 
334 	return 0;
335 }
336 
337 static SIMPLE_DEV_PM_OPS(ili210x_i2c_pm,
338 			 ili210x_i2c_suspend, ili210x_i2c_resume);
339 
340 static const struct i2c_device_id ili210x_i2c_id[] = {
341 	{ "ili210x", 0 },
342 	{ }
343 };
344 MODULE_DEVICE_TABLE(i2c, ili210x_i2c_id);
345 
346 static struct i2c_driver ili210x_ts_driver = {
347 	.driver = {
348 		.name = "ili210x_i2c",
349 		.pm = &ili210x_i2c_pm,
350 	},
351 	.id_table = ili210x_i2c_id,
352 	.probe = ili210x_i2c_probe,
353 	.remove = ili210x_i2c_remove,
354 };
355 
356 module_i2c_driver(ili210x_ts_driver);
357 
358 MODULE_AUTHOR("Olivier Sobrie <olivier@sobrie.be>");
359 MODULE_DESCRIPTION("ILI210X I2C Touchscreen Driver");
360 MODULE_LICENSE("GPL");
361