1 // SPDX-License-Identifier: GPL-2.0
2 // Melfas MMS114/MMS152 touchscreen device driver
4 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Author: Joonyoung Shim <jy0922.shim@samsung.com>
7 #include <linux/module.h>
8 #include <linux/delay.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>
18 /* Write only registers */
19 #define MMS114_MODE_CONTROL 0x01
20 #define MMS114_OPERATION_MODE_MASK 0xE
21 #define MMS114_ACTIVE BIT(1)
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
29 /* Read only registers */
30 #define MMS114_PACKET_SIZE 0x0F
31 #define MMS114_INFORMATION 0x10
32 #define MMS114_TSP_REV 0xF0
34 #define MMS152_FW_REV 0xE1
35 #define MMS152_COMPAT_GROUP 0xF2
37 /* Minimum delay time is 50us between stop and start signal of i2c */
38 #define MMS114_I2C_DELAY 50
40 /* 200ms needs after power on */
41 #define MMS114_POWERON_DELAY 200
43 /* Touchscreen absolute values */
44 #define MMS114_MAX_AREA 0xff
46 #define MMS114_MAX_TOUCH 10
47 #define MMS114_PACKET_NUM 8
50 #define MMS114_TYPE_NONE 0
51 #define MMS114_TYPE_TOUCHSCREEN 1
52 #define MMS114_TYPE_TOUCHKEY 2
60 struct i2c_client
*client
;
61 struct input_dev
*input_dev
;
62 struct regulator
*core_reg
;
63 struct regulator
*io_reg
;
64 struct touchscreen_properties props
;
66 unsigned int contact_threshold
;
67 unsigned int moving_threshold
;
69 /* Use cache data for mode control register(write only) */
70 u8 cache_mode_control
;
74 u8 id
:4, reserved_bit4
:1, type
:2, pressed
:1;
83 static int __mms114_read_reg(struct mms114_data
*data
, unsigned int reg
,
84 unsigned int len
, u8
*val
)
86 struct i2c_client
*client
= data
->client
;
87 struct i2c_msg xfer
[2];
91 if (reg
<= MMS114_MODE_CONTROL
&& reg
+ len
> MMS114_MODE_CONTROL
)
94 /* Write register: use repeated start */
95 xfer
[0].addr
= client
->addr
;
96 xfer
[0].flags
= I2C_M_TEN
| I2C_M_NOSTART
;
101 xfer
[1].addr
= client
->addr
;
102 xfer
[1].flags
= I2C_M_RD
;
106 error
= i2c_transfer(client
->adapter
, xfer
, 2);
108 dev_err(&client
->dev
,
109 "%s: i2c transfer failed (%d)\n", __func__
, error
);
110 return error
< 0 ? error
: -EIO
;
112 udelay(MMS114_I2C_DELAY
);
117 static int mms114_read_reg(struct mms114_data
*data
, unsigned int reg
)
122 if (reg
== MMS114_MODE_CONTROL
)
123 return data
->cache_mode_control
;
125 error
= __mms114_read_reg(data
, reg
, 1, &val
);
126 return error
< 0 ? error
: val
;
129 static int mms114_write_reg(struct mms114_data
*data
, unsigned int reg
,
132 struct i2c_client
*client
= data
->client
;
139 error
= i2c_master_send(client
, buf
, 2);
141 dev_err(&client
->dev
,
142 "%s: i2c send failed (%d)\n", __func__
, error
);
143 return error
< 0 ? error
: -EIO
;
145 udelay(MMS114_I2C_DELAY
);
147 if (reg
== MMS114_MODE_CONTROL
)
148 data
->cache_mode_control
= val
;
153 static void mms114_process_mt(struct mms114_data
*data
, struct mms114_touch
*touch
)
155 struct i2c_client
*client
= data
->client
;
156 struct input_dev
*input_dev
= data
->input_dev
;
161 if (touch
->id
> MMS114_MAX_TOUCH
) {
162 dev_err(&client
->dev
, "Wrong touch id (%d)\n", touch
->id
);
166 if (touch
->type
!= MMS114_TYPE_TOUCHSCREEN
) {
167 dev_err(&client
->dev
, "Wrong touch type (%d)\n", touch
->type
);
172 x
= touch
->x_lo
| touch
->x_hi
<< 8;
173 y
= touch
->y_lo
| touch
->y_hi
<< 8;
175 dev_dbg(&client
->dev
,
176 "id: %d, type: %d, pressed: %d, x: %d, y: %d, width: %d, strength: %d\n",
177 id
, touch
->type
, touch
->pressed
,
178 x
, y
, touch
->width
, touch
->strength
);
180 input_mt_slot(input_dev
, id
);
181 input_mt_report_slot_state(input_dev
, MT_TOOL_FINGER
, touch
->pressed
);
183 if (touch
->pressed
) {
184 touchscreen_report_pos(input_dev
, &data
->props
, x
, y
, true);
185 input_report_abs(input_dev
, ABS_MT_TOUCH_MAJOR
, touch
->width
);
186 input_report_abs(input_dev
, ABS_MT_PRESSURE
, touch
->strength
);
190 static irqreturn_t
mms114_interrupt(int irq
, void *dev_id
)
192 struct mms114_data
*data
= dev_id
;
193 struct input_dev
*input_dev
= data
->input_dev
;
194 struct mms114_touch touch
[MMS114_MAX_TOUCH
];
200 mutex_lock(&input_dev
->mutex
);
201 if (!input_dev
->users
) {
202 mutex_unlock(&input_dev
->mutex
);
205 mutex_unlock(&input_dev
->mutex
);
207 packet_size
= mms114_read_reg(data
, MMS114_PACKET_SIZE
);
208 if (packet_size
<= 0)
211 touch_size
= packet_size
/ MMS114_PACKET_NUM
;
213 error
= __mms114_read_reg(data
, MMS114_INFORMATION
, packet_size
,
218 for (index
= 0; index
< touch_size
; index
++)
219 mms114_process_mt(data
, touch
+ index
);
221 input_mt_report_pointer_emulation(data
->input_dev
, true);
222 input_sync(data
->input_dev
);
228 static int mms114_set_active(struct mms114_data
*data
, bool active
)
232 val
= mms114_read_reg(data
, MMS114_MODE_CONTROL
);
236 val
&= ~MMS114_OPERATION_MODE_MASK
;
238 /* If active is false, sleep mode */
240 val
|= MMS114_ACTIVE
;
242 return mms114_write_reg(data
, MMS114_MODE_CONTROL
, val
);
245 static int mms114_get_version(struct mms114_data
*data
)
247 struct device
*dev
= &data
->client
->dev
;
252 switch (data
->type
) {
254 error
= __mms114_read_reg(data
, MMS152_FW_REV
, 3, buf
);
258 group
= i2c_smbus_read_byte_data(data
->client
,
259 MMS152_COMPAT_GROUP
);
263 dev_info(dev
, "TSP FW Rev: bootloader 0x%x / core 0x%x / config 0x%x, Compat group: %c\n",
264 buf
[0], buf
[1], buf
[2], group
);
268 error
= __mms114_read_reg(data
, MMS114_TSP_REV
, 6, buf
);
272 dev_info(dev
, "TSP Rev: 0x%x, HW Rev: 0x%x, Firmware Ver: 0x%x\n",
273 buf
[0], buf
[1], buf
[3]);
280 static int mms114_setup_regs(struct mms114_data
*data
)
282 const struct touchscreen_properties
*props
= &data
->props
;
286 error
= mms114_get_version(data
);
290 /* MMS152 has no configuration or power on registers */
291 if (data
->type
== TYPE_MMS152
)
294 error
= mms114_set_active(data
, true);
298 val
= (props
->max_x
>> 8) & 0xf;
299 val
|= ((props
->max_y
>> 8) & 0xf) << 4;
300 error
= mms114_write_reg(data
, MMS114_XY_RESOLUTION_H
, val
);
304 val
= props
->max_x
& 0xff;
305 error
= mms114_write_reg(data
, MMS114_X_RESOLUTION
, val
);
309 val
= props
->max_x
& 0xff;
310 error
= mms114_write_reg(data
, MMS114_Y_RESOLUTION
, val
);
314 if (data
->contact_threshold
) {
315 error
= mms114_write_reg(data
, MMS114_CONTACT_THRESHOLD
,
316 data
->contact_threshold
);
321 if (data
->moving_threshold
) {
322 error
= mms114_write_reg(data
, MMS114_MOVING_THRESHOLD
,
323 data
->moving_threshold
);
331 static int mms114_start(struct mms114_data
*data
)
333 struct i2c_client
*client
= data
->client
;
336 error
= regulator_enable(data
->core_reg
);
338 dev_err(&client
->dev
, "Failed to enable avdd: %d\n", error
);
342 error
= regulator_enable(data
->io_reg
);
344 dev_err(&client
->dev
, "Failed to enable vdd: %d\n", error
);
345 regulator_disable(data
->core_reg
);
349 msleep(MMS114_POWERON_DELAY
);
351 error
= mms114_setup_regs(data
);
353 regulator_disable(data
->io_reg
);
354 regulator_disable(data
->core_reg
);
358 enable_irq(client
->irq
);
363 static void mms114_stop(struct mms114_data
*data
)
365 struct i2c_client
*client
= data
->client
;
368 disable_irq(client
->irq
);
370 error
= regulator_disable(data
->io_reg
);
372 dev_warn(&client
->dev
, "Failed to disable vdd: %d\n", error
);
374 error
= regulator_disable(data
->core_reg
);
376 dev_warn(&client
->dev
, "Failed to disable avdd: %d\n", error
);
379 static int mms114_input_open(struct input_dev
*dev
)
381 struct mms114_data
*data
= input_get_drvdata(dev
);
383 return mms114_start(data
);
386 static void mms114_input_close(struct input_dev
*dev
)
388 struct mms114_data
*data
= input_get_drvdata(dev
);
393 static int mms114_parse_legacy_bindings(struct mms114_data
*data
)
395 struct device
*dev
= &data
->client
->dev
;
396 struct touchscreen_properties
*props
= &data
->props
;
398 if (device_property_read_u32(dev
, "x-size", &props
->max_x
)) {
399 dev_dbg(dev
, "failed to get legacy x-size property\n");
403 if (device_property_read_u32(dev
, "y-size", &props
->max_y
)) {
404 dev_dbg(dev
, "failed to get legacy y-size property\n");
408 device_property_read_u32(dev
, "contact-threshold",
409 &data
->contact_threshold
);
410 device_property_read_u32(dev
, "moving-threshold",
411 &data
->moving_threshold
);
413 if (device_property_read_bool(dev
, "x-invert"))
414 props
->invert_x
= true;
415 if (device_property_read_bool(dev
, "y-invert"))
416 props
->invert_y
= true;
418 props
->swap_x_y
= false;
423 static int mms114_probe(struct i2c_client
*client
,
424 const struct i2c_device_id
*id
)
426 struct mms114_data
*data
;
427 struct input_dev
*input_dev
;
428 const void *match_data
;
431 if (!i2c_check_functionality(client
->adapter
,
432 I2C_FUNC_PROTOCOL_MANGLING
)) {
433 dev_err(&client
->dev
,
434 "Need i2c bus that supports protocol mangling\n");
438 data
= devm_kzalloc(&client
->dev
, sizeof(struct mms114_data
),
440 input_dev
= devm_input_allocate_device(&client
->dev
);
441 if (!data
|| !input_dev
) {
442 dev_err(&client
->dev
, "Failed to allocate memory\n");
446 data
->client
= client
;
447 data
->input_dev
= input_dev
;
449 match_data
= device_get_match_data(&client
->dev
);
453 data
->type
= (enum mms_type
)match_data
;
455 input_set_capability(input_dev
, EV_ABS
, ABS_MT_POSITION_X
);
456 input_set_capability(input_dev
, EV_ABS
, ABS_MT_POSITION_Y
);
457 input_set_abs_params(input_dev
, ABS_MT_PRESSURE
, 0, 255, 0, 0);
458 input_set_abs_params(input_dev
, ABS_MT_TOUCH_MAJOR
,
459 0, MMS114_MAX_AREA
, 0, 0);
461 touchscreen_parse_properties(input_dev
, true, &data
->props
);
462 if (!data
->props
.max_x
|| !data
->props
.max_y
) {
463 dev_dbg(&client
->dev
,
464 "missing X/Y size properties, trying legacy bindings\n");
465 error
= mms114_parse_legacy_bindings(data
);
469 input_set_abs_params(input_dev
, ABS_MT_POSITION_X
,
470 0, data
->props
.max_x
, 0, 0);
471 input_set_abs_params(input_dev
, ABS_MT_POSITION_Y
,
472 0, data
->props
.max_y
, 0, 0);
475 if (data
->type
== TYPE_MMS114
) {
477 * The firmware handles movement and pressure fuzz, so
478 * don't duplicate that in software.
480 data
->moving_threshold
= input_abs_get_fuzz(input_dev
,
482 data
->contact_threshold
= input_abs_get_fuzz(input_dev
,
484 input_abs_set_fuzz(input_dev
, ABS_MT_POSITION_X
, 0);
485 input_abs_set_fuzz(input_dev
, ABS_MT_POSITION_Y
, 0);
486 input_abs_set_fuzz(input_dev
, ABS_MT_PRESSURE
, 0);
489 input_dev
->name
= devm_kasprintf(&client
->dev
, GFP_KERNEL
,
490 "MELFAS MMS%d Touchscreen",
492 if (!input_dev
->name
)
495 input_dev
->id
.bustype
= BUS_I2C
;
496 input_dev
->dev
.parent
= &client
->dev
;
497 input_dev
->open
= mms114_input_open
;
498 input_dev
->close
= mms114_input_close
;
500 error
= input_mt_init_slots(input_dev
, MMS114_MAX_TOUCH
,
505 input_set_drvdata(input_dev
, data
);
506 i2c_set_clientdata(client
, data
);
508 data
->core_reg
= devm_regulator_get(&client
->dev
, "avdd");
509 if (IS_ERR(data
->core_reg
)) {
510 error
= PTR_ERR(data
->core_reg
);
511 dev_err(&client
->dev
,
512 "Unable to get the Core regulator (%d)\n", error
);
516 data
->io_reg
= devm_regulator_get(&client
->dev
, "vdd");
517 if (IS_ERR(data
->io_reg
)) {
518 error
= PTR_ERR(data
->io_reg
);
519 dev_err(&client
->dev
,
520 "Unable to get the IO regulator (%d)\n", error
);
524 error
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
525 NULL
, mms114_interrupt
, IRQF_ONESHOT
,
526 dev_name(&client
->dev
), data
);
528 dev_err(&client
->dev
, "Failed to register interrupt\n");
531 disable_irq(client
->irq
);
533 error
= input_register_device(data
->input_dev
);
535 dev_err(&client
->dev
, "Failed to register input device\n");
542 static int __maybe_unused
mms114_suspend(struct device
*dev
)
544 struct i2c_client
*client
= to_i2c_client(dev
);
545 struct mms114_data
*data
= i2c_get_clientdata(client
);
546 struct input_dev
*input_dev
= data
->input_dev
;
549 /* Release all touch */
550 for (id
= 0; id
< MMS114_MAX_TOUCH
; id
++) {
551 input_mt_slot(input_dev
, id
);
552 input_mt_report_slot_state(input_dev
, MT_TOOL_FINGER
, false);
555 input_mt_report_pointer_emulation(input_dev
, true);
556 input_sync(input_dev
);
558 mutex_lock(&input_dev
->mutex
);
559 if (input_dev
->users
)
561 mutex_unlock(&input_dev
->mutex
);
566 static int __maybe_unused
mms114_resume(struct device
*dev
)
568 struct i2c_client
*client
= to_i2c_client(dev
);
569 struct mms114_data
*data
= i2c_get_clientdata(client
);
570 struct input_dev
*input_dev
= data
->input_dev
;
573 mutex_lock(&input_dev
->mutex
);
574 if (input_dev
->users
) {
575 error
= mms114_start(data
);
577 mutex_unlock(&input_dev
->mutex
);
581 mutex_unlock(&input_dev
->mutex
);
586 static SIMPLE_DEV_PM_OPS(mms114_pm_ops
, mms114_suspend
, mms114_resume
);
588 static const struct i2c_device_id mms114_id
[] = {
592 MODULE_DEVICE_TABLE(i2c
, mms114_id
);
595 static const struct of_device_id mms114_dt_match
[] = {
597 .compatible
= "melfas,mms114",
598 .data
= (void *)TYPE_MMS114
,
600 .compatible
= "melfas,mms152",
601 .data
= (void *)TYPE_MMS152
,
605 MODULE_DEVICE_TABLE(of
, mms114_dt_match
);
608 static struct i2c_driver mms114_driver
= {
611 .pm
= &mms114_pm_ops
,
612 .of_match_table
= of_match_ptr(mms114_dt_match
),
614 .probe
= mms114_probe
,
615 .id_table
= mms114_id
,
618 module_i2c_driver(mms114_driver
);
620 /* Module information */
621 MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
622 MODULE_DESCRIPTION("MELFAS mms114 Touchscreen driver");
623 MODULE_LICENSE("GPL v2");