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