1 /*
2  * Copyright (C) 2012 Samsung Electronics Co.Ltd
3  * Author: Joonyoung Shim <jy0922.shim@samsung.com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  */
9 
10 #include <linux/module.h>
11 #include <linux/delay.h>
12 #include <linux/of.h>
13 #include <linux/of_device.h>
14 #include <linux/i2c.h>
15 #include <linux/input/mt.h>
16 #include <linux/input/touchscreen.h>
17 #include <linux/interrupt.h>
18 #include <linux/regulator/consumer.h>
19 #include <linux/slab.h>
20 
21 /* Write only registers */
22 #define MMS114_MODE_CONTROL		0x01
23 #define MMS114_OPERATION_MODE_MASK	0xE
24 #define MMS114_ACTIVE			BIT(1)
25 
26 #define MMS114_XY_RESOLUTION_H		0x02
27 #define MMS114_X_RESOLUTION		0x03
28 #define MMS114_Y_RESOLUTION		0x04
29 #define MMS114_CONTACT_THRESHOLD	0x05
30 #define MMS114_MOVING_THRESHOLD		0x06
31 
32 /* Read only registers */
33 #define MMS114_PACKET_SIZE		0x0F
34 #define MMS114_INFORMATION		0x10
35 #define MMS114_TSP_REV			0xF0
36 
37 #define MMS152_FW_REV			0xE1
38 #define MMS152_COMPAT_GROUP		0xF2
39 
40 /* Minimum delay time is 50us between stop and start signal of i2c */
41 #define MMS114_I2C_DELAY		50
42 
43 /* 200ms needs after power on */
44 #define MMS114_POWERON_DELAY		200
45 
46 /* Touchscreen absolute values */
47 #define MMS114_MAX_AREA			0xff
48 
49 #define MMS114_MAX_TOUCH		10
50 #define MMS114_PACKET_NUM		8
51 
52 /* Touch type */
53 #define MMS114_TYPE_NONE		0
54 #define MMS114_TYPE_TOUCHSCREEN		1
55 #define MMS114_TYPE_TOUCHKEY		2
56 
57 enum mms_type {
58 	TYPE_MMS114	= 114,
59 	TYPE_MMS152	= 152,
60 };
61 
62 struct mms114_data {
63 	struct i2c_client	*client;
64 	struct input_dev	*input_dev;
65 	struct regulator	*core_reg;
66 	struct regulator	*io_reg;
67 	struct touchscreen_properties props;
68 	enum mms_type		type;
69 	unsigned int		contact_threshold;
70 	unsigned int		moving_threshold;
71 
72 	/* Use cache data for mode control register(write only) */
73 	u8			cache_mode_control;
74 };
75 
76 struct mms114_touch {
77 	u8 id:4, reserved_bit4:1, type:2, pressed:1;
78 	u8 x_hi:4, y_hi:4;
79 	u8 x_lo;
80 	u8 y_lo;
81 	u8 width;
82 	u8 strength;
83 	u8 reserved[2];
84 } __packed;
85 
86 static int __mms114_read_reg(struct mms114_data *data, unsigned int reg,
87 			     unsigned int len, u8 *val)
88 {
89 	struct i2c_client *client = data->client;
90 	struct i2c_msg xfer[2];
91 	u8 buf = reg & 0xff;
92 	int error;
93 
94 	if (reg <= MMS114_MODE_CONTROL && reg + len > MMS114_MODE_CONTROL)
95 		BUG();
96 
97 	/* Write register: use repeated start */
98 	xfer[0].addr = client->addr;
99 	xfer[0].flags = I2C_M_TEN | I2C_M_NOSTART;
100 	xfer[0].len = 1;
101 	xfer[0].buf = &buf;
102 
103 	/* Read data */
104 	xfer[1].addr = client->addr;
105 	xfer[1].flags = I2C_M_RD;
106 	xfer[1].len = len;
107 	xfer[1].buf = val;
108 
109 	error = i2c_transfer(client->adapter, xfer, 2);
110 	if (error != 2) {
111 		dev_err(&client->dev,
112 			"%s: i2c transfer failed (%d)\n", __func__, error);
113 		return error < 0 ? error : -EIO;
114 	}
115 	udelay(MMS114_I2C_DELAY);
116 
117 	return 0;
118 }
119 
120 static int mms114_read_reg(struct mms114_data *data, unsigned int reg)
121 {
122 	u8 val;
123 	int error;
124 
125 	if (reg == MMS114_MODE_CONTROL)
126 		return data->cache_mode_control;
127 
128 	error = __mms114_read_reg(data, reg, 1, &val);
129 	return error < 0 ? error : val;
130 }
131 
132 static int mms114_write_reg(struct mms114_data *data, unsigned int reg,
133 			    unsigned int val)
134 {
135 	struct i2c_client *client = data->client;
136 	u8 buf[2];
137 	int error;
138 
139 	buf[0] = reg & 0xff;
140 	buf[1] = val & 0xff;
141 
142 	error = i2c_master_send(client, buf, 2);
143 	if (error != 2) {
144 		dev_err(&client->dev,
145 			"%s: i2c send failed (%d)\n", __func__, error);
146 		return error < 0 ? error : -EIO;
147 	}
148 	udelay(MMS114_I2C_DELAY);
149 
150 	if (reg == MMS114_MODE_CONTROL)
151 		data->cache_mode_control = val;
152 
153 	return 0;
154 }
155 
156 static void mms114_process_mt(struct mms114_data *data, struct mms114_touch *touch)
157 {
158 	struct i2c_client *client = data->client;
159 	struct input_dev *input_dev = data->input_dev;
160 	unsigned int id;
161 	unsigned int x;
162 	unsigned int y;
163 
164 	if (touch->id > MMS114_MAX_TOUCH) {
165 		dev_err(&client->dev, "Wrong touch id (%d)\n", touch->id);
166 		return;
167 	}
168 
169 	if (touch->type != MMS114_TYPE_TOUCHSCREEN) {
170 		dev_err(&client->dev, "Wrong touch type (%d)\n", touch->type);
171 		return;
172 	}
173 
174 	id = touch->id - 1;
175 	x = touch->x_lo | touch->x_hi << 8;
176 	y = touch->y_lo | touch->y_hi << 8;
177 
178 	dev_dbg(&client->dev,
179 		"id: %d, type: %d, pressed: %d, x: %d, y: %d, width: %d, strength: %d\n",
180 		id, touch->type, touch->pressed,
181 		x, y, touch->width, touch->strength);
182 
183 	input_mt_slot(input_dev, id);
184 	input_mt_report_slot_state(input_dev, MT_TOOL_FINGER, touch->pressed);
185 
186 	if (touch->pressed) {
187 		touchscreen_report_pos(input_dev, &data->props, x, y, true);
188 		input_report_abs(input_dev, ABS_MT_TOUCH_MAJOR, touch->width);
189 		input_report_abs(input_dev, ABS_MT_PRESSURE, touch->strength);
190 	}
191 }
192 
193 static irqreturn_t mms114_interrupt(int irq, void *dev_id)
194 {
195 	struct mms114_data *data = dev_id;
196 	struct input_dev *input_dev = data->input_dev;
197 	struct mms114_touch touch[MMS114_MAX_TOUCH];
198 	int packet_size;
199 	int touch_size;
200 	int index;
201 	int error;
202 
203 	mutex_lock(&input_dev->mutex);
204 	if (!input_dev->users) {
205 		mutex_unlock(&input_dev->mutex);
206 		goto out;
207 	}
208 	mutex_unlock(&input_dev->mutex);
209 
210 	packet_size = mms114_read_reg(data, MMS114_PACKET_SIZE);
211 	if (packet_size <= 0)
212 		goto out;
213 
214 	touch_size = packet_size / MMS114_PACKET_NUM;
215 
216 	error = __mms114_read_reg(data, MMS114_INFORMATION, packet_size,
217 			(u8 *)touch);
218 	if (error < 0)
219 		goto out;
220 
221 	for (index = 0; index < touch_size; index++)
222 		mms114_process_mt(data, touch + index);
223 
224 	input_mt_report_pointer_emulation(data->input_dev, true);
225 	input_sync(data->input_dev);
226 
227 out:
228 	return IRQ_HANDLED;
229 }
230 
231 static int mms114_set_active(struct mms114_data *data, bool active)
232 {
233 	int val;
234 
235 	val = mms114_read_reg(data, MMS114_MODE_CONTROL);
236 	if (val < 0)
237 		return val;
238 
239 	val &= ~MMS114_OPERATION_MODE_MASK;
240 
241 	/* If active is false, sleep mode */
242 	if (active)
243 		val |= MMS114_ACTIVE;
244 
245 	return mms114_write_reg(data, MMS114_MODE_CONTROL, val);
246 }
247 
248 static int mms114_get_version(struct mms114_data *data)
249 {
250 	struct device *dev = &data->client->dev;
251 	u8 buf[6];
252 	int group;
253 	int error;
254 
255 	switch (data->type) {
256 	case TYPE_MMS152:
257 		error = __mms114_read_reg(data, MMS152_FW_REV, 3, buf);
258 		if (error)
259 			return error;
260 
261 		group = i2c_smbus_read_byte_data(data->client,
262 						  MMS152_COMPAT_GROUP);
263 		if (group < 0)
264 			return group;
265 
266 		dev_info(dev, "TSP FW Rev: bootloader 0x%x / core 0x%x / config 0x%x, Compat group: %c\n",
267 			 buf[0], buf[1], buf[2], group);
268 		break;
269 
270 	case TYPE_MMS114:
271 		error = __mms114_read_reg(data, MMS114_TSP_REV, 6, buf);
272 		if (error)
273 			return error;
274 
275 		dev_info(dev, "TSP Rev: 0x%x, HW Rev: 0x%x, Firmware Ver: 0x%x\n",
276 			 buf[0], buf[1], buf[3]);
277 		break;
278 	}
279 
280 	return 0;
281 }
282 
283 static int mms114_setup_regs(struct mms114_data *data)
284 {
285 	const struct touchscreen_properties *props = &data->props;
286 	int val;
287 	int error;
288 
289 	error = mms114_get_version(data);
290 	if (error < 0)
291 		return error;
292 
293 	/* MMS152 has no configuration or power on registers */
294 	if (data->type == TYPE_MMS152)
295 		return 0;
296 
297 	error = mms114_set_active(data, true);
298 	if (error < 0)
299 		return error;
300 
301 	val = (props->max_x >> 8) & 0xf;
302 	val |= ((props->max_y >> 8) & 0xf) << 4;
303 	error = mms114_write_reg(data, MMS114_XY_RESOLUTION_H, val);
304 	if (error < 0)
305 		return error;
306 
307 	val = props->max_x & 0xff;
308 	error = mms114_write_reg(data, MMS114_X_RESOLUTION, val);
309 	if (error < 0)
310 		return error;
311 
312 	val = props->max_x & 0xff;
313 	error = mms114_write_reg(data, MMS114_Y_RESOLUTION, val);
314 	if (error < 0)
315 		return error;
316 
317 	if (data->contact_threshold) {
318 		error = mms114_write_reg(data, MMS114_CONTACT_THRESHOLD,
319 				data->contact_threshold);
320 		if (error < 0)
321 			return error;
322 	}
323 
324 	if (data->moving_threshold) {
325 		error = mms114_write_reg(data, MMS114_MOVING_THRESHOLD,
326 				data->moving_threshold);
327 		if (error < 0)
328 			return error;
329 	}
330 
331 	return 0;
332 }
333 
334 static int mms114_start(struct mms114_data *data)
335 {
336 	struct i2c_client *client = data->client;
337 	int error;
338 
339 	error = regulator_enable(data->core_reg);
340 	if (error) {
341 		dev_err(&client->dev, "Failed to enable avdd: %d\n", error);
342 		return error;
343 	}
344 
345 	error = regulator_enable(data->io_reg);
346 	if (error) {
347 		dev_err(&client->dev, "Failed to enable vdd: %d\n", error);
348 		regulator_disable(data->core_reg);
349 		return error;
350 	}
351 
352 	msleep(MMS114_POWERON_DELAY);
353 
354 	error = mms114_setup_regs(data);
355 	if (error < 0) {
356 		regulator_disable(data->io_reg);
357 		regulator_disable(data->core_reg);
358 		return error;
359 	}
360 
361 	enable_irq(client->irq);
362 
363 	return 0;
364 }
365 
366 static void mms114_stop(struct mms114_data *data)
367 {
368 	struct i2c_client *client = data->client;
369 	int error;
370 
371 	disable_irq(client->irq);
372 
373 	error = regulator_disable(data->io_reg);
374 	if (error)
375 		dev_warn(&client->dev, "Failed to disable vdd: %d\n", error);
376 
377 	error = regulator_disable(data->core_reg);
378 	if (error)
379 		dev_warn(&client->dev, "Failed to disable avdd: %d\n", error);
380 }
381 
382 static int mms114_input_open(struct input_dev *dev)
383 {
384 	struct mms114_data *data = input_get_drvdata(dev);
385 
386 	return mms114_start(data);
387 }
388 
389 static void mms114_input_close(struct input_dev *dev)
390 {
391 	struct mms114_data *data = input_get_drvdata(dev);
392 
393 	mms114_stop(data);
394 }
395 
396 static int mms114_parse_legacy_bindings(struct mms114_data *data)
397 {
398 	struct device *dev = &data->client->dev;
399 	struct touchscreen_properties *props = &data->props;
400 
401 	if (device_property_read_u32(dev, "x-size", &props->max_x)) {
402 		dev_dbg(dev, "failed to get legacy x-size property\n");
403 		return -EINVAL;
404 	}
405 
406 	if (device_property_read_u32(dev, "y-size", &props->max_y)) {
407 		dev_dbg(dev, "failed to get legacy y-size property\n");
408 		return -EINVAL;
409 	}
410 
411 	device_property_read_u32(dev, "contact-threshold",
412 				&data->contact_threshold);
413 	device_property_read_u32(dev, "moving-threshold",
414 				&data->moving_threshold);
415 
416 	if (device_property_read_bool(dev, "x-invert"))
417 		props->invert_x = true;
418 	if (device_property_read_bool(dev, "y-invert"))
419 		props->invert_y = true;
420 
421 	props->swap_x_y = false;
422 
423 	return 0;
424 }
425 
426 static int mms114_probe(struct i2c_client *client,
427 				  const struct i2c_device_id *id)
428 {
429 	struct mms114_data *data;
430 	struct input_dev *input_dev;
431 	const void *match_data;
432 	int error;
433 
434 	if (!i2c_check_functionality(client->adapter,
435 				I2C_FUNC_PROTOCOL_MANGLING)) {
436 		dev_err(&client->dev,
437 			"Need i2c bus that supports protocol mangling\n");
438 		return -ENODEV;
439 	}
440 
441 	data = devm_kzalloc(&client->dev, sizeof(struct mms114_data),
442 			    GFP_KERNEL);
443 	input_dev = devm_input_allocate_device(&client->dev);
444 	if (!data || !input_dev) {
445 		dev_err(&client->dev, "Failed to allocate memory\n");
446 		return -ENOMEM;
447 	}
448 
449 	data->client = client;
450 	data->input_dev = input_dev;
451 
452 	/* FIXME: switch to device_get_match_data() when available */
453 	match_data = of_device_get_match_data(&client->dev);
454 	if (!match_data)
455 		return -EINVAL;
456 
457 	data->type = (enum mms_type)match_data;
458 
459 	input_set_capability(input_dev, EV_ABS, ABS_MT_POSITION_X);
460 	input_set_capability(input_dev, EV_ABS, ABS_MT_POSITION_Y);
461 	input_set_abs_params(input_dev, ABS_MT_PRESSURE, 0, 255, 0, 0);
462 	input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR,
463 			     0, MMS114_MAX_AREA, 0, 0);
464 
465 	touchscreen_parse_properties(input_dev, true, &data->props);
466 	if (!data->props.max_x || !data->props.max_y) {
467 		dev_dbg(&client->dev,
468 			"missing X/Y size properties, trying legacy bindings\n");
469 		error = mms114_parse_legacy_bindings(data);
470 		if (error)
471 			return error;
472 
473 		input_set_abs_params(input_dev, ABS_MT_POSITION_X,
474 				     0, data->props.max_x, 0, 0);
475 		input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
476 				     0, data->props.max_y, 0, 0);
477 	}
478 
479 	if (data->type == TYPE_MMS114) {
480 		/*
481 		 * The firmware handles movement and pressure fuzz, so
482 		 * don't duplicate that in software.
483 		 */
484 		data->moving_threshold = input_abs_get_fuzz(input_dev,
485 							    ABS_MT_POSITION_X);
486 		data->contact_threshold = input_abs_get_fuzz(input_dev,
487 							     ABS_MT_PRESSURE);
488 		input_abs_set_fuzz(input_dev, ABS_MT_POSITION_X, 0);
489 		input_abs_set_fuzz(input_dev, ABS_MT_POSITION_Y, 0);
490 		input_abs_set_fuzz(input_dev, ABS_MT_PRESSURE, 0);
491 	}
492 
493 	input_dev->name = devm_kasprintf(&client->dev, GFP_KERNEL,
494 					 "MELFAS MMS%d Touchscreen",
495 					 data->type);
496 	if (!input_dev->name)
497 		return -ENOMEM;
498 
499 	input_dev->id.bustype = BUS_I2C;
500 	input_dev->dev.parent = &client->dev;
501 	input_dev->open = mms114_input_open;
502 	input_dev->close = mms114_input_close;
503 
504 	error = input_mt_init_slots(input_dev, MMS114_MAX_TOUCH,
505 				    INPUT_MT_DIRECT);
506 	if (error)
507 		return error;
508 
509 	input_set_drvdata(input_dev, data);
510 	i2c_set_clientdata(client, data);
511 
512 	data->core_reg = devm_regulator_get(&client->dev, "avdd");
513 	if (IS_ERR(data->core_reg)) {
514 		error = PTR_ERR(data->core_reg);
515 		dev_err(&client->dev,
516 			"Unable to get the Core regulator (%d)\n", error);
517 		return error;
518 	}
519 
520 	data->io_reg = devm_regulator_get(&client->dev, "vdd");
521 	if (IS_ERR(data->io_reg)) {
522 		error = PTR_ERR(data->io_reg);
523 		dev_err(&client->dev,
524 			"Unable to get the IO regulator (%d)\n", error);
525 		return error;
526 	}
527 
528 	error = devm_request_threaded_irq(&client->dev, client->irq,
529 					  NULL, mms114_interrupt, IRQF_ONESHOT,
530 					  dev_name(&client->dev), data);
531 	if (error) {
532 		dev_err(&client->dev, "Failed to register interrupt\n");
533 		return error;
534 	}
535 	disable_irq(client->irq);
536 
537 	error = input_register_device(data->input_dev);
538 	if (error) {
539 		dev_err(&client->dev, "Failed to register input device\n");
540 		return error;
541 	}
542 
543 	return 0;
544 }
545 
546 static int __maybe_unused mms114_suspend(struct device *dev)
547 {
548 	struct i2c_client *client = to_i2c_client(dev);
549 	struct mms114_data *data = i2c_get_clientdata(client);
550 	struct input_dev *input_dev = data->input_dev;
551 	int id;
552 
553 	/* Release all touch */
554 	for (id = 0; id < MMS114_MAX_TOUCH; id++) {
555 		input_mt_slot(input_dev, id);
556 		input_mt_report_slot_state(input_dev, MT_TOOL_FINGER, false);
557 	}
558 
559 	input_mt_report_pointer_emulation(input_dev, true);
560 	input_sync(input_dev);
561 
562 	mutex_lock(&input_dev->mutex);
563 	if (input_dev->users)
564 		mms114_stop(data);
565 	mutex_unlock(&input_dev->mutex);
566 
567 	return 0;
568 }
569 
570 static int __maybe_unused mms114_resume(struct device *dev)
571 {
572 	struct i2c_client *client = to_i2c_client(dev);
573 	struct mms114_data *data = i2c_get_clientdata(client);
574 	struct input_dev *input_dev = data->input_dev;
575 	int error;
576 
577 	mutex_lock(&input_dev->mutex);
578 	if (input_dev->users) {
579 		error = mms114_start(data);
580 		if (error < 0) {
581 			mutex_unlock(&input_dev->mutex);
582 			return error;
583 		}
584 	}
585 	mutex_unlock(&input_dev->mutex);
586 
587 	return 0;
588 }
589 
590 static SIMPLE_DEV_PM_OPS(mms114_pm_ops, mms114_suspend, mms114_resume);
591 
592 static const struct i2c_device_id mms114_id[] = {
593 	{ "mms114", 0 },
594 	{ }
595 };
596 MODULE_DEVICE_TABLE(i2c, mms114_id);
597 
598 #ifdef CONFIG_OF
599 static const struct of_device_id mms114_dt_match[] = {
600 	{
601 		.compatible = "melfas,mms114",
602 		.data = (void *)TYPE_MMS114,
603 	}, {
604 		.compatible = "melfas,mms152",
605 		.data = (void *)TYPE_MMS152,
606 	},
607 	{ }
608 };
609 MODULE_DEVICE_TABLE(of, mms114_dt_match);
610 #endif
611 
612 static struct i2c_driver mms114_driver = {
613 	.driver = {
614 		.name	= "mms114",
615 		.pm	= &mms114_pm_ops,
616 		.of_match_table = of_match_ptr(mms114_dt_match),
617 	},
618 	.probe		= mms114_probe,
619 	.id_table	= mms114_id,
620 };
621 
622 module_i2c_driver(mms114_driver);
623 
624 /* Module information */
625 MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
626 MODULE_DESCRIPTION("MELFAS mms114 Touchscreen driver");
627 MODULE_LICENSE("GPL");
628