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_MMS136	= 136,
58 	TYPE_MMS152	= 152,
59 	TYPE_MMS345L	= 345,
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 */
98 	xfer[0].addr = client->addr;
99 	xfer[0].flags = client->flags & I2C_M_TEN;
100 	xfer[0].len = 1;
101 	xfer[0].buf = &buf;
102 
103 	/* Read data */
104 	xfer[1].addr = client->addr;
105 	xfer[1].flags = (client->flags & I2C_M_TEN) | 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_device_enabled(input_dev)) {
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 	/* MMS136 has slightly different event size */
215 	if (data->type == TYPE_MMS136)
216 		touch_size = packet_size / MMS136_EVENT_SIZE;
217 	else
218 		touch_size = packet_size / MMS114_EVENT_SIZE;
219 
220 	error = __mms114_read_reg(data, MMS114_INFORMATION, packet_size,
221 			(u8 *)touch);
222 	if (error < 0)
223 		goto out;
224 
225 	for (index = 0; index < touch_size; index++)
226 		mms114_process_mt(data, touch + index);
227 
228 	input_mt_report_pointer_emulation(data->input_dev, true);
229 	input_sync(data->input_dev);
230 
231 out:
232 	return IRQ_HANDLED;
233 }
234 
235 static int mms114_set_active(struct mms114_data *data, bool active)
236 {
237 	int val;
238 
239 	val = mms114_read_reg(data, MMS114_MODE_CONTROL);
240 	if (val < 0)
241 		return val;
242 
243 	val &= ~MMS114_OPERATION_MODE_MASK;
244 
245 	/* If active is false, sleep mode */
246 	if (active)
247 		val |= MMS114_ACTIVE;
248 
249 	return mms114_write_reg(data, MMS114_MODE_CONTROL, val);
250 }
251 
252 static int mms114_get_version(struct mms114_data *data)
253 {
254 	struct device *dev = &data->client->dev;
255 	u8 buf[6];
256 	int group;
257 	int error;
258 
259 	switch (data->type) {
260 	case TYPE_MMS345L:
261 		error = __mms114_read_reg(data, MMS152_FW_REV, 3, buf);
262 		if (error)
263 			return error;
264 
265 		dev_info(dev, "TSP FW Rev: bootloader 0x%x / core 0x%x / config 0x%x\n",
266 			 buf[0], buf[1], buf[2]);
267 		break;
268 
269 	case TYPE_MMS152:
270 		error = __mms114_read_reg(data, MMS152_FW_REV, 3, buf);
271 		if (error)
272 			return error;
273 
274 		group = i2c_smbus_read_byte_data(data->client,
275 						  MMS152_COMPAT_GROUP);
276 		if (group < 0)
277 			return group;
278 
279 		dev_info(dev, "TSP FW Rev: bootloader 0x%x / core 0x%x / config 0x%x, Compat group: %c\n",
280 			 buf[0], buf[1], buf[2], group);
281 		break;
282 
283 	case TYPE_MMS114:
284 	case TYPE_MMS136:
285 		error = __mms114_read_reg(data, MMS114_TSP_REV, 6, buf);
286 		if (error)
287 			return error;
288 
289 		dev_info(dev, "TSP Rev: 0x%x, HW Rev: 0x%x, Firmware Ver: 0x%x\n",
290 			 buf[0], buf[1], buf[3]);
291 		break;
292 	}
293 
294 	return 0;
295 }
296 
297 static int mms114_setup_regs(struct mms114_data *data)
298 {
299 	const struct touchscreen_properties *props = &data->props;
300 	int val;
301 	int error;
302 
303 	error = mms114_get_version(data);
304 	if (error < 0)
305 		return error;
306 
307 	/* Only MMS114 and MMS136 have configuration and power on registers */
308 	if (data->type != TYPE_MMS114 && data->type != TYPE_MMS136)
309 		return 0;
310 
311 	error = mms114_set_active(data, true);
312 	if (error < 0)
313 		return error;
314 
315 	val = (props->max_x >> 8) & 0xf;
316 	val |= ((props->max_y >> 8) & 0xf) << 4;
317 	error = mms114_write_reg(data, MMS114_XY_RESOLUTION_H, val);
318 	if (error < 0)
319 		return error;
320 
321 	val = props->max_x & 0xff;
322 	error = mms114_write_reg(data, MMS114_X_RESOLUTION, val);
323 	if (error < 0)
324 		return error;
325 
326 	val = props->max_x & 0xff;
327 	error = mms114_write_reg(data, MMS114_Y_RESOLUTION, val);
328 	if (error < 0)
329 		return error;
330 
331 	if (data->contact_threshold) {
332 		error = mms114_write_reg(data, MMS114_CONTACT_THRESHOLD,
333 				data->contact_threshold);
334 		if (error < 0)
335 			return error;
336 	}
337 
338 	if (data->moving_threshold) {
339 		error = mms114_write_reg(data, MMS114_MOVING_THRESHOLD,
340 				data->moving_threshold);
341 		if (error < 0)
342 			return error;
343 	}
344 
345 	return 0;
346 }
347 
348 static int mms114_start(struct mms114_data *data)
349 {
350 	struct i2c_client *client = data->client;
351 	int error;
352 
353 	error = regulator_enable(data->core_reg);
354 	if (error) {
355 		dev_err(&client->dev, "Failed to enable avdd: %d\n", error);
356 		return error;
357 	}
358 
359 	error = regulator_enable(data->io_reg);
360 	if (error) {
361 		dev_err(&client->dev, "Failed to enable vdd: %d\n", error);
362 		regulator_disable(data->core_reg);
363 		return error;
364 	}
365 
366 	msleep(MMS114_POWERON_DELAY);
367 
368 	error = mms114_setup_regs(data);
369 	if (error < 0) {
370 		regulator_disable(data->io_reg);
371 		regulator_disable(data->core_reg);
372 		return error;
373 	}
374 
375 	enable_irq(client->irq);
376 
377 	return 0;
378 }
379 
380 static void mms114_stop(struct mms114_data *data)
381 {
382 	struct i2c_client *client = data->client;
383 	int error;
384 
385 	disable_irq(client->irq);
386 
387 	error = regulator_disable(data->io_reg);
388 	if (error)
389 		dev_warn(&client->dev, "Failed to disable vdd: %d\n", error);
390 
391 	error = regulator_disable(data->core_reg);
392 	if (error)
393 		dev_warn(&client->dev, "Failed to disable avdd: %d\n", error);
394 }
395 
396 static int mms114_input_open(struct input_dev *dev)
397 {
398 	struct mms114_data *data = input_get_drvdata(dev);
399 
400 	return mms114_start(data);
401 }
402 
403 static void mms114_input_close(struct input_dev *dev)
404 {
405 	struct mms114_data *data = input_get_drvdata(dev);
406 
407 	mms114_stop(data);
408 }
409 
410 static int mms114_parse_legacy_bindings(struct mms114_data *data)
411 {
412 	struct device *dev = &data->client->dev;
413 	struct touchscreen_properties *props = &data->props;
414 
415 	if (device_property_read_u32(dev, "x-size", &props->max_x)) {
416 		dev_dbg(dev, "failed to get legacy x-size property\n");
417 		return -EINVAL;
418 	}
419 
420 	if (device_property_read_u32(dev, "y-size", &props->max_y)) {
421 		dev_dbg(dev, "failed to get legacy y-size property\n");
422 		return -EINVAL;
423 	}
424 
425 	device_property_read_u32(dev, "contact-threshold",
426 				&data->contact_threshold);
427 	device_property_read_u32(dev, "moving-threshold",
428 				&data->moving_threshold);
429 
430 	if (device_property_read_bool(dev, "x-invert"))
431 		props->invert_x = true;
432 	if (device_property_read_bool(dev, "y-invert"))
433 		props->invert_y = true;
434 
435 	props->swap_x_y = false;
436 
437 	return 0;
438 }
439 
440 static int mms114_probe(struct i2c_client *client,
441 				  const struct i2c_device_id *id)
442 {
443 	struct mms114_data *data;
444 	struct input_dev *input_dev;
445 	const void *match_data;
446 	int error;
447 
448 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
449 		dev_err(&client->dev, "Not supported I2C adapter\n");
450 		return -ENODEV;
451 	}
452 
453 	data = devm_kzalloc(&client->dev, sizeof(struct mms114_data),
454 			    GFP_KERNEL);
455 	input_dev = devm_input_allocate_device(&client->dev);
456 	if (!data || !input_dev) {
457 		dev_err(&client->dev, "Failed to allocate memory\n");
458 		return -ENOMEM;
459 	}
460 
461 	data->client = client;
462 	data->input_dev = input_dev;
463 
464 	match_data = device_get_match_data(&client->dev);
465 	if (!match_data)
466 		return -EINVAL;
467 
468 	data->type = (enum mms_type)match_data;
469 
470 	input_set_capability(input_dev, EV_ABS, ABS_MT_POSITION_X);
471 	input_set_capability(input_dev, EV_ABS, ABS_MT_POSITION_Y);
472 	input_set_abs_params(input_dev, ABS_MT_PRESSURE, 0, 255, 0, 0);
473 	input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR,
474 			     0, MMS114_MAX_AREA, 0, 0);
475 
476 	touchscreen_parse_properties(input_dev, true, &data->props);
477 	if (!data->props.max_x || !data->props.max_y) {
478 		dev_dbg(&client->dev,
479 			"missing X/Y size properties, trying legacy bindings\n");
480 		error = mms114_parse_legacy_bindings(data);
481 		if (error)
482 			return error;
483 
484 		input_set_abs_params(input_dev, ABS_MT_POSITION_X,
485 				     0, data->props.max_x, 0, 0);
486 		input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
487 				     0, data->props.max_y, 0, 0);
488 	}
489 
490 	if (data->type == TYPE_MMS114 || data->type == TYPE_MMS136) {
491 		/*
492 		 * The firmware handles movement and pressure fuzz, so
493 		 * don't duplicate that in software.
494 		 */
495 		data->moving_threshold = input_abs_get_fuzz(input_dev,
496 							    ABS_MT_POSITION_X);
497 		data->contact_threshold = input_abs_get_fuzz(input_dev,
498 							     ABS_MT_PRESSURE);
499 		input_abs_set_fuzz(input_dev, ABS_MT_POSITION_X, 0);
500 		input_abs_set_fuzz(input_dev, ABS_MT_POSITION_Y, 0);
501 		input_abs_set_fuzz(input_dev, ABS_MT_PRESSURE, 0);
502 	}
503 
504 	input_dev->name = devm_kasprintf(&client->dev, GFP_KERNEL,
505 					 "MELFAS MMS%d Touchscreen",
506 					 data->type);
507 	if (!input_dev->name)
508 		return -ENOMEM;
509 
510 	input_dev->id.bustype = BUS_I2C;
511 	input_dev->dev.parent = &client->dev;
512 	input_dev->open = mms114_input_open;
513 	input_dev->close = mms114_input_close;
514 
515 	error = input_mt_init_slots(input_dev, MMS114_MAX_TOUCH,
516 				    INPUT_MT_DIRECT);
517 	if (error)
518 		return error;
519 
520 	input_set_drvdata(input_dev, data);
521 	i2c_set_clientdata(client, data);
522 
523 	data->core_reg = devm_regulator_get(&client->dev, "avdd");
524 	if (IS_ERR(data->core_reg)) {
525 		error = PTR_ERR(data->core_reg);
526 		dev_err(&client->dev,
527 			"Unable to get the Core regulator (%d)\n", error);
528 		return error;
529 	}
530 
531 	data->io_reg = devm_regulator_get(&client->dev, "vdd");
532 	if (IS_ERR(data->io_reg)) {
533 		error = PTR_ERR(data->io_reg);
534 		dev_err(&client->dev,
535 			"Unable to get the IO regulator (%d)\n", error);
536 		return error;
537 	}
538 
539 	error = devm_request_threaded_irq(&client->dev, client->irq,
540 					  NULL, mms114_interrupt,
541 					  IRQF_ONESHOT | IRQF_NO_AUTOEN,
542 					  dev_name(&client->dev), data);
543 	if (error) {
544 		dev_err(&client->dev, "Failed to register interrupt\n");
545 		return error;
546 	}
547 
548 	error = input_register_device(data->input_dev);
549 	if (error) {
550 		dev_err(&client->dev, "Failed to register input device\n");
551 		return error;
552 	}
553 
554 	return 0;
555 }
556 
557 static int __maybe_unused mms114_suspend(struct device *dev)
558 {
559 	struct i2c_client *client = to_i2c_client(dev);
560 	struct mms114_data *data = i2c_get_clientdata(client);
561 	struct input_dev *input_dev = data->input_dev;
562 	int id;
563 
564 	/* Release all touch */
565 	for (id = 0; id < MMS114_MAX_TOUCH; id++) {
566 		input_mt_slot(input_dev, id);
567 		input_mt_report_slot_inactive(input_dev);
568 	}
569 
570 	input_mt_report_pointer_emulation(input_dev, true);
571 	input_sync(input_dev);
572 
573 	mutex_lock(&input_dev->mutex);
574 	if (input_device_enabled(input_dev))
575 		mms114_stop(data);
576 	mutex_unlock(&input_dev->mutex);
577 
578 	return 0;
579 }
580 
581 static int __maybe_unused mms114_resume(struct device *dev)
582 {
583 	struct i2c_client *client = to_i2c_client(dev);
584 	struct mms114_data *data = i2c_get_clientdata(client);
585 	struct input_dev *input_dev = data->input_dev;
586 	int error;
587 
588 	mutex_lock(&input_dev->mutex);
589 	if (input_device_enabled(input_dev)) {
590 		error = mms114_start(data);
591 		if (error < 0) {
592 			mutex_unlock(&input_dev->mutex);
593 			return error;
594 		}
595 	}
596 	mutex_unlock(&input_dev->mutex);
597 
598 	return 0;
599 }
600 
601 static SIMPLE_DEV_PM_OPS(mms114_pm_ops, mms114_suspend, mms114_resume);
602 
603 static const struct i2c_device_id mms114_id[] = {
604 	{ "mms114", 0 },
605 	{ }
606 };
607 MODULE_DEVICE_TABLE(i2c, mms114_id);
608 
609 #ifdef CONFIG_OF
610 static const struct of_device_id mms114_dt_match[] = {
611 	{
612 		.compatible = "melfas,mms114",
613 		.data = (void *)TYPE_MMS114,
614 	}, {
615 		.compatible = "melfas,mms136",
616 		.data = (void *)TYPE_MMS136,
617 	}, {
618 		.compatible = "melfas,mms152",
619 		.data = (void *)TYPE_MMS152,
620 	}, {
621 		.compatible = "melfas,mms345l",
622 		.data = (void *)TYPE_MMS345L,
623 	},
624 	{ }
625 };
626 MODULE_DEVICE_TABLE(of, mms114_dt_match);
627 #endif
628 
629 static struct i2c_driver mms114_driver = {
630 	.driver = {
631 		.name	= "mms114",
632 		.pm	= &mms114_pm_ops,
633 		.of_match_table = of_match_ptr(mms114_dt_match),
634 	},
635 	.probe		= mms114_probe,
636 	.id_table	= mms114_id,
637 };
638 
639 module_i2c_driver(mms114_driver);
640 
641 /* Module information */
642 MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
643 MODULE_DESCRIPTION("MELFAS mms114 Touchscreen driver");
644 MODULE_LICENSE("GPL v2");
645