1 // SPDX-License-Identifier: GPL-2.0
2 // Melfas MMS114/MMS136/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/i2c.h>
11 #include <linux/input/mt.h>
12 #include <linux/input/touchscreen.h>
13 #include <linux/interrupt.h>
14 #include <linux/regulator/consumer.h>
15 #include <linux/slab.h>
17 /* Write only registers */
18 #define MMS114_MODE_CONTROL 0x01
19 #define MMS114_OPERATION_MODE_MASK 0xE
20 #define MMS114_ACTIVE BIT(1)
22 #define MMS114_XY_RESOLUTION_H 0x02
23 #define MMS114_X_RESOLUTION 0x03
24 #define MMS114_Y_RESOLUTION 0x04
25 #define MMS114_CONTACT_THRESHOLD 0x05
26 #define MMS114_MOVING_THRESHOLD 0x06
28 /* Read only registers */
29 #define MMS114_PACKET_SIZE 0x0F
30 #define MMS114_INFORMATION 0x10
31 #define MMS114_TSP_REV 0xF0
33 #define MMS152_FW_REV 0xE1
34 #define MMS152_COMPAT_GROUP 0xF2
36 /* Minimum delay time is 50us between stop and start signal of i2c */
37 #define MMS114_I2C_DELAY 50
39 /* 200ms needs after power on */
40 #define MMS114_POWERON_DELAY 200
42 /* Touchscreen absolute values */
43 #define MMS114_MAX_AREA 0xff
45 #define MMS114_MAX_TOUCHKEYS 15
46 #define MMS114_MAX_TOUCH 10
47 #define MMS114_EVENT_SIZE 8
48 #define MMS136_EVENT_SIZE 6
51 #define MMS114_TYPE_NONE 0
52 #define MMS114_TYPE_TOUCHSCREEN 1
53 #define MMS114_TYPE_TOUCHKEY 2
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
;
70 unsigned int contact_threshold
;
71 unsigned int moving_threshold
;
73 u32 keycodes
[MMS114_MAX_TOUCHKEYS
];
76 /* Use cache data for mode control register(write only) */
77 u8 cache_mode_control
;
81 u8 id
:4, reserved_bit4
:1, type
:2, pressed
:1;
90 static int __mms114_read_reg(struct mms114_data
*data
, unsigned int reg
,
91 unsigned int len
, u8
*val
)
93 struct i2c_client
*client
= data
->client
;
94 struct i2c_msg xfer
[2];
98 if (reg
<= MMS114_MODE_CONTROL
&& reg
+ len
> MMS114_MODE_CONTROL
)
102 xfer
[0].addr
= client
->addr
;
103 xfer
[0].flags
= client
->flags
& I2C_M_TEN
;
108 xfer
[1].addr
= client
->addr
;
109 xfer
[1].flags
= (client
->flags
& I2C_M_TEN
) | I2C_M_RD
;
113 error
= i2c_transfer(client
->adapter
, xfer
, 2);
115 dev_err(&client
->dev
,
116 "%s: i2c transfer failed (%d)\n", __func__
, error
);
117 return error
< 0 ? error
: -EIO
;
119 udelay(MMS114_I2C_DELAY
);
124 static int mms114_read_reg(struct mms114_data
*data
, unsigned int reg
)
129 if (reg
== MMS114_MODE_CONTROL
)
130 return data
->cache_mode_control
;
132 error
= __mms114_read_reg(data
, reg
, 1, &val
);
133 return error
< 0 ? error
: val
;
136 static int mms114_write_reg(struct mms114_data
*data
, unsigned int reg
,
139 struct i2c_client
*client
= data
->client
;
146 error
= i2c_master_send(client
, buf
, 2);
148 dev_err(&client
->dev
,
149 "%s: i2c send failed (%d)\n", __func__
, error
);
150 return error
< 0 ? error
: -EIO
;
152 udelay(MMS114_I2C_DELAY
);
154 if (reg
== MMS114_MODE_CONTROL
)
155 data
->cache_mode_control
= val
;
160 static void mms114_process_mt(struct mms114_data
*data
, struct mms114_touch
*touch
)
162 struct i2c_client
*client
= data
->client
;
163 struct input_dev
*input_dev
= data
->input_dev
;
168 if (touch
->id
> MMS114_MAX_TOUCH
) {
169 dev_err(&client
->dev
, "Wrong touch id (%d)\n", touch
->id
);
174 x
= touch
->x_lo
| touch
->x_hi
<< 8;
175 y
= touch
->y_lo
| touch
->y_hi
<< 8;
177 dev_dbg(&client
->dev
,
178 "id: %d, type: %d, pressed: %d, x: %d, y: %d, width: %d, strength: %d\n",
179 id
, touch
->type
, touch
->pressed
,
180 x
, y
, touch
->width
, touch
->strength
);
182 input_mt_slot(input_dev
, id
);
183 input_mt_report_slot_state(input_dev
, MT_TOOL_FINGER
, touch
->pressed
);
185 if (touch
->pressed
) {
186 touchscreen_report_pos(input_dev
, &data
->props
, x
, y
, true);
187 input_report_abs(input_dev
, ABS_MT_TOUCH_MAJOR
, touch
->width
);
188 input_report_abs(input_dev
, ABS_MT_PRESSURE
, touch
->strength
);
192 static void mms114_process_touchkey(struct mms114_data
*data
,
193 struct mms114_touch
*touch
)
195 struct i2c_client
*client
= data
->client
;
196 struct input_dev
*input_dev
= data
->input_dev
;
197 unsigned int keycode_id
;
202 if (touch
->id
> data
->num_keycodes
) {
203 dev_err(&client
->dev
, "Wrong touch id for touchkey (%d)\n",
208 keycode_id
= touch
->id
- 1;
209 dev_dbg(&client
->dev
, "keycode id: %d, pressed: %d\n", keycode_id
,
212 input_report_key(input_dev
, data
->keycodes
[keycode_id
], touch
->pressed
);
215 static irqreturn_t
mms114_interrupt(int irq
, void *dev_id
)
217 struct mms114_data
*data
= dev_id
;
218 struct i2c_client
*client
= data
->client
;
219 struct input_dev
*input_dev
= data
->input_dev
;
220 struct mms114_touch touch
[MMS114_MAX_TOUCH
];
226 mutex_lock(&input_dev
->mutex
);
227 if (!input_device_enabled(input_dev
)) {
228 mutex_unlock(&input_dev
->mutex
);
231 mutex_unlock(&input_dev
->mutex
);
233 packet_size
= mms114_read_reg(data
, MMS114_PACKET_SIZE
);
234 if (packet_size
<= 0)
237 /* MMS136 has slightly different event size */
238 if (data
->type
== TYPE_MMS134S
|| data
->type
== TYPE_MMS136
)
239 touch_size
= packet_size
/ MMS136_EVENT_SIZE
;
241 touch_size
= packet_size
/ MMS114_EVENT_SIZE
;
243 error
= __mms114_read_reg(data
, MMS114_INFORMATION
, packet_size
,
248 for (index
= 0; index
< touch_size
; index
++) {
249 switch (touch
[index
].type
) {
250 case MMS114_TYPE_TOUCHSCREEN
:
251 mms114_process_mt(data
, touch
+ index
);
254 case MMS114_TYPE_TOUCHKEY
:
255 mms114_process_touchkey(data
, touch
+ index
);
259 dev_err(&client
->dev
, "Wrong touch type (%d)\n",
265 input_mt_report_pointer_emulation(data
->input_dev
, true);
266 input_sync(data
->input_dev
);
272 static int mms114_set_active(struct mms114_data
*data
, bool active
)
276 val
= mms114_read_reg(data
, MMS114_MODE_CONTROL
);
280 val
&= ~MMS114_OPERATION_MODE_MASK
;
282 /* If active is false, sleep mode */
284 val
|= MMS114_ACTIVE
;
286 return mms114_write_reg(data
, MMS114_MODE_CONTROL
, val
);
289 static int mms114_get_version(struct mms114_data
*data
)
291 struct device
*dev
= &data
->client
->dev
;
296 switch (data
->type
) {
298 error
= __mms114_read_reg(data
, MMS152_FW_REV
, 3, buf
);
302 dev_info(dev
, "TSP FW Rev: bootloader 0x%x / core 0x%x / config 0x%x\n",
303 buf
[0], buf
[1], buf
[2]);
307 error
= __mms114_read_reg(data
, MMS152_FW_REV
, 3, buf
);
311 group
= i2c_smbus_read_byte_data(data
->client
,
312 MMS152_COMPAT_GROUP
);
316 dev_info(dev
, "TSP FW Rev: bootloader 0x%x / core 0x%x / config 0x%x, Compat group: %c\n",
317 buf
[0], buf
[1], buf
[2], group
);
323 error
= __mms114_read_reg(data
, MMS114_TSP_REV
, 6, buf
);
327 dev_info(dev
, "TSP Rev: 0x%x, HW Rev: 0x%x, Firmware Ver: 0x%x\n",
328 buf
[0], buf
[1], buf
[3]);
335 static int mms114_setup_regs(struct mms114_data
*data
)
337 const struct touchscreen_properties
*props
= &data
->props
;
341 error
= mms114_get_version(data
);
345 /* MMS114, MMS134S and MMS136 have configuration and power on registers */
346 if (data
->type
!= TYPE_MMS114
&& data
->type
!= TYPE_MMS134S
&&
347 data
->type
!= TYPE_MMS136
)
350 error
= mms114_set_active(data
, true);
354 val
= (props
->max_x
>> 8) & 0xf;
355 val
|= ((props
->max_y
>> 8) & 0xf) << 4;
356 error
= mms114_write_reg(data
, MMS114_XY_RESOLUTION_H
, val
);
360 val
= props
->max_x
& 0xff;
361 error
= mms114_write_reg(data
, MMS114_X_RESOLUTION
, val
);
365 val
= props
->max_x
& 0xff;
366 error
= mms114_write_reg(data
, MMS114_Y_RESOLUTION
, val
);
370 if (data
->contact_threshold
) {
371 error
= mms114_write_reg(data
, MMS114_CONTACT_THRESHOLD
,
372 data
->contact_threshold
);
377 if (data
->moving_threshold
) {
378 error
= mms114_write_reg(data
, MMS114_MOVING_THRESHOLD
,
379 data
->moving_threshold
);
387 static int mms114_start(struct mms114_data
*data
)
389 struct i2c_client
*client
= data
->client
;
392 error
= regulator_enable(data
->core_reg
);
394 dev_err(&client
->dev
, "Failed to enable avdd: %d\n", error
);
398 error
= regulator_enable(data
->io_reg
);
400 dev_err(&client
->dev
, "Failed to enable vdd: %d\n", error
);
401 regulator_disable(data
->core_reg
);
405 msleep(MMS114_POWERON_DELAY
);
407 error
= mms114_setup_regs(data
);
409 regulator_disable(data
->io_reg
);
410 regulator_disable(data
->core_reg
);
414 enable_irq(client
->irq
);
419 static void mms114_stop(struct mms114_data
*data
)
421 struct i2c_client
*client
= data
->client
;
424 disable_irq(client
->irq
);
426 error
= regulator_disable(data
->io_reg
);
428 dev_warn(&client
->dev
, "Failed to disable vdd: %d\n", error
);
430 error
= regulator_disable(data
->core_reg
);
432 dev_warn(&client
->dev
, "Failed to disable avdd: %d\n", error
);
435 static int mms114_input_open(struct input_dev
*dev
)
437 struct mms114_data
*data
= input_get_drvdata(dev
);
439 return mms114_start(data
);
442 static void mms114_input_close(struct input_dev
*dev
)
444 struct mms114_data
*data
= input_get_drvdata(dev
);
449 static int mms114_parse_legacy_bindings(struct mms114_data
*data
)
451 struct device
*dev
= &data
->client
->dev
;
452 struct touchscreen_properties
*props
= &data
->props
;
454 if (device_property_read_u32(dev
, "x-size", &props
->max_x
)) {
455 dev_dbg(dev
, "failed to get legacy x-size property\n");
459 if (device_property_read_u32(dev
, "y-size", &props
->max_y
)) {
460 dev_dbg(dev
, "failed to get legacy y-size property\n");
464 device_property_read_u32(dev
, "contact-threshold",
465 &data
->contact_threshold
);
466 device_property_read_u32(dev
, "moving-threshold",
467 &data
->moving_threshold
);
469 if (device_property_read_bool(dev
, "x-invert"))
470 props
->invert_x
= true;
471 if (device_property_read_bool(dev
, "y-invert"))
472 props
->invert_y
= true;
474 props
->swap_x_y
= false;
479 static int mms114_probe(struct i2c_client
*client
)
481 struct mms114_data
*data
;
482 struct input_dev
*input_dev
;
483 const void *match_data
;
487 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_I2C
)) {
488 dev_err(&client
->dev
, "Not supported I2C adapter\n");
492 data
= devm_kzalloc(&client
->dev
, sizeof(struct mms114_data
),
494 input_dev
= devm_input_allocate_device(&client
->dev
);
495 if (!data
|| !input_dev
) {
496 dev_err(&client
->dev
, "Failed to allocate memory\n");
500 data
->client
= client
;
501 data
->input_dev
= input_dev
;
503 match_data
= device_get_match_data(&client
->dev
);
507 data
->type
= (enum mms_type
)match_data
;
509 data
->num_keycodes
= device_property_count_u32(&client
->dev
,
511 if (data
->num_keycodes
== -EINVAL
) {
512 data
->num_keycodes
= 0;
513 } else if (data
->num_keycodes
< 0) {
514 dev_err(&client
->dev
,
515 "Unable to parse linux,keycodes property: %d\n",
517 return data
->num_keycodes
;
518 } else if (data
->num_keycodes
> MMS114_MAX_TOUCHKEYS
) {
519 dev_warn(&client
->dev
,
520 "Found %d linux,keycodes but max is %d, ignoring the rest\n",
521 data
->num_keycodes
, MMS114_MAX_TOUCHKEYS
);
522 data
->num_keycodes
= MMS114_MAX_TOUCHKEYS
;
525 if (data
->num_keycodes
> 0) {
526 error
= device_property_read_u32_array(&client
->dev
,
531 dev_err(&client
->dev
,
532 "Unable to read linux,keycodes values: %d\n",
537 input_dev
->keycode
= data
->keycodes
;
538 input_dev
->keycodemax
= data
->num_keycodes
;
539 input_dev
->keycodesize
= sizeof(data
->keycodes
[0]);
540 for (i
= 0; i
< data
->num_keycodes
; i
++)
541 input_set_capability(input_dev
,
542 EV_KEY
, data
->keycodes
[i
]);
545 input_set_capability(input_dev
, EV_ABS
, ABS_MT_POSITION_X
);
546 input_set_capability(input_dev
, EV_ABS
, ABS_MT_POSITION_Y
);
547 input_set_abs_params(input_dev
, ABS_MT_PRESSURE
, 0, 255, 0, 0);
548 input_set_abs_params(input_dev
, ABS_MT_TOUCH_MAJOR
,
549 0, MMS114_MAX_AREA
, 0, 0);
551 touchscreen_parse_properties(input_dev
, true, &data
->props
);
552 if (!data
->props
.max_x
|| !data
->props
.max_y
) {
553 dev_dbg(&client
->dev
,
554 "missing X/Y size properties, trying legacy bindings\n");
555 error
= mms114_parse_legacy_bindings(data
);
559 input_set_abs_params(input_dev
, ABS_MT_POSITION_X
,
560 0, data
->props
.max_x
, 0, 0);
561 input_set_abs_params(input_dev
, ABS_MT_POSITION_Y
,
562 0, data
->props
.max_y
, 0, 0);
565 if (data
->type
== TYPE_MMS114
|| data
->type
== TYPE_MMS134S
||
566 data
->type
== TYPE_MMS136
) {
568 * The firmware handles movement and pressure fuzz, so
569 * don't duplicate that in software.
571 data
->moving_threshold
= input_abs_get_fuzz(input_dev
,
573 data
->contact_threshold
= input_abs_get_fuzz(input_dev
,
575 input_abs_set_fuzz(input_dev
, ABS_MT_POSITION_X
, 0);
576 input_abs_set_fuzz(input_dev
, ABS_MT_POSITION_Y
, 0);
577 input_abs_set_fuzz(input_dev
, ABS_MT_PRESSURE
, 0);
580 input_dev
->name
= devm_kasprintf(&client
->dev
, GFP_KERNEL
,
581 "MELFAS MMS%d Touchscreen",
583 if (!input_dev
->name
)
586 input_dev
->id
.bustype
= BUS_I2C
;
587 input_dev
->dev
.parent
= &client
->dev
;
588 input_dev
->open
= mms114_input_open
;
589 input_dev
->close
= mms114_input_close
;
591 error
= input_mt_init_slots(input_dev
, MMS114_MAX_TOUCH
,
596 input_set_drvdata(input_dev
, data
);
597 i2c_set_clientdata(client
, data
);
599 data
->core_reg
= devm_regulator_get(&client
->dev
, "avdd");
600 if (IS_ERR(data
->core_reg
)) {
601 error
= PTR_ERR(data
->core_reg
);
602 dev_err(&client
->dev
,
603 "Unable to get the Core regulator (%d)\n", error
);
607 data
->io_reg
= devm_regulator_get(&client
->dev
, "vdd");
608 if (IS_ERR(data
->io_reg
)) {
609 error
= PTR_ERR(data
->io_reg
);
610 dev_err(&client
->dev
,
611 "Unable to get the IO regulator (%d)\n", error
);
615 error
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
616 NULL
, mms114_interrupt
,
617 IRQF_ONESHOT
| IRQF_NO_AUTOEN
,
618 dev_name(&client
->dev
), data
);
620 dev_err(&client
->dev
, "Failed to register interrupt\n");
624 error
= input_register_device(data
->input_dev
);
626 dev_err(&client
->dev
, "Failed to register input device\n");
633 static int mms114_suspend(struct device
*dev
)
635 struct i2c_client
*client
= to_i2c_client(dev
);
636 struct mms114_data
*data
= i2c_get_clientdata(client
);
637 struct input_dev
*input_dev
= data
->input_dev
;
640 /* Release all touch */
641 for (id
= 0; id
< MMS114_MAX_TOUCH
; id
++) {
642 input_mt_slot(input_dev
, id
);
643 input_mt_report_slot_inactive(input_dev
);
646 input_mt_report_pointer_emulation(input_dev
, true);
647 input_sync(input_dev
);
649 mutex_lock(&input_dev
->mutex
);
650 if (input_device_enabled(input_dev
))
652 mutex_unlock(&input_dev
->mutex
);
657 static int mms114_resume(struct device
*dev
)
659 struct i2c_client
*client
= to_i2c_client(dev
);
660 struct mms114_data
*data
= i2c_get_clientdata(client
);
661 struct input_dev
*input_dev
= data
->input_dev
;
664 mutex_lock(&input_dev
->mutex
);
665 if (input_device_enabled(input_dev
)) {
666 error
= mms114_start(data
);
668 mutex_unlock(&input_dev
->mutex
);
672 mutex_unlock(&input_dev
->mutex
);
677 static DEFINE_SIMPLE_DEV_PM_OPS(mms114_pm_ops
, mms114_suspend
, mms114_resume
);
679 static const struct i2c_device_id mms114_id
[] = {
683 MODULE_DEVICE_TABLE(i2c
, mms114_id
);
686 static const struct of_device_id mms114_dt_match
[] = {
688 .compatible
= "melfas,mms114",
689 .data
= (void *)TYPE_MMS114
,
691 .compatible
= "melfas,mms134s",
692 .data
= (void *)TYPE_MMS134S
,
694 .compatible
= "melfas,mms136",
695 .data
= (void *)TYPE_MMS136
,
697 .compatible
= "melfas,mms152",
698 .data
= (void *)TYPE_MMS152
,
700 .compatible
= "melfas,mms345l",
701 .data
= (void *)TYPE_MMS345L
,
705 MODULE_DEVICE_TABLE(of
, mms114_dt_match
);
708 static struct i2c_driver mms114_driver
= {
711 .pm
= pm_sleep_ptr(&mms114_pm_ops
),
712 .of_match_table
= of_match_ptr(mms114_dt_match
),
714 .probe
= mms114_probe
,
715 .id_table
= mms114_id
,
718 module_i2c_driver(mms114_driver
);
720 /* Module information */
721 MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
722 MODULE_DESCRIPTION("MELFAS mms114 Touchscreen driver");
723 MODULE_LICENSE("GPL v2");