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 /* FIXME: switch to device_get_match_data() when available */
450 match_data
= of_device_get_match_data(&client
->dev
);
454 data
->type
= (enum mms_type
)match_data
;
456 input_set_capability(input_dev
, EV_ABS
, ABS_MT_POSITION_X
);
457 input_set_capability(input_dev
, EV_ABS
, ABS_MT_POSITION_Y
);
458 input_set_abs_params(input_dev
, ABS_MT_PRESSURE
, 0, 255, 0, 0);
459 input_set_abs_params(input_dev
, ABS_MT_TOUCH_MAJOR
,
460 0, MMS114_MAX_AREA
, 0, 0);
462 touchscreen_parse_properties(input_dev
, true, &data
->props
);
463 if (!data
->props
.max_x
|| !data
->props
.max_y
) {
464 dev_dbg(&client
->dev
,
465 "missing X/Y size properties, trying legacy bindings\n");
466 error
= mms114_parse_legacy_bindings(data
);
470 input_set_abs_params(input_dev
, ABS_MT_POSITION_X
,
471 0, data
->props
.max_x
, 0, 0);
472 input_set_abs_params(input_dev
, ABS_MT_POSITION_Y
,
473 0, data
->props
.max_y
, 0, 0);
476 if (data
->type
== TYPE_MMS114
) {
478 * The firmware handles movement and pressure fuzz, so
479 * don't duplicate that in software.
481 data
->moving_threshold
= input_abs_get_fuzz(input_dev
,
483 data
->contact_threshold
= input_abs_get_fuzz(input_dev
,
485 input_abs_set_fuzz(input_dev
, ABS_MT_POSITION_X
, 0);
486 input_abs_set_fuzz(input_dev
, ABS_MT_POSITION_Y
, 0);
487 input_abs_set_fuzz(input_dev
, ABS_MT_PRESSURE
, 0);
490 input_dev
->name
= devm_kasprintf(&client
->dev
, GFP_KERNEL
,
491 "MELFAS MMS%d Touchscreen",
493 if (!input_dev
->name
)
496 input_dev
->id
.bustype
= BUS_I2C
;
497 input_dev
->dev
.parent
= &client
->dev
;
498 input_dev
->open
= mms114_input_open
;
499 input_dev
->close
= mms114_input_close
;
501 error
= input_mt_init_slots(input_dev
, MMS114_MAX_TOUCH
,
506 input_set_drvdata(input_dev
, data
);
507 i2c_set_clientdata(client
, data
);
509 data
->core_reg
= devm_regulator_get(&client
->dev
, "avdd");
510 if (IS_ERR(data
->core_reg
)) {
511 error
= PTR_ERR(data
->core_reg
);
512 dev_err(&client
->dev
,
513 "Unable to get the Core regulator (%d)\n", error
);
517 data
->io_reg
= devm_regulator_get(&client
->dev
, "vdd");
518 if (IS_ERR(data
->io_reg
)) {
519 error
= PTR_ERR(data
->io_reg
);
520 dev_err(&client
->dev
,
521 "Unable to get the IO regulator (%d)\n", error
);
525 error
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
526 NULL
, mms114_interrupt
, IRQF_ONESHOT
,
527 dev_name(&client
->dev
), data
);
529 dev_err(&client
->dev
, "Failed to register interrupt\n");
532 disable_irq(client
->irq
);
534 error
= input_register_device(data
->input_dev
);
536 dev_err(&client
->dev
, "Failed to register input device\n");
543 static int __maybe_unused
mms114_suspend(struct device
*dev
)
545 struct i2c_client
*client
= to_i2c_client(dev
);
546 struct mms114_data
*data
= i2c_get_clientdata(client
);
547 struct input_dev
*input_dev
= data
->input_dev
;
550 /* Release all touch */
551 for (id
= 0; id
< MMS114_MAX_TOUCH
; id
++) {
552 input_mt_slot(input_dev
, id
);
553 input_mt_report_slot_state(input_dev
, MT_TOOL_FINGER
, false);
556 input_mt_report_pointer_emulation(input_dev
, true);
557 input_sync(input_dev
);
559 mutex_lock(&input_dev
->mutex
);
560 if (input_dev
->users
)
562 mutex_unlock(&input_dev
->mutex
);
567 static int __maybe_unused
mms114_resume(struct device
*dev
)
569 struct i2c_client
*client
= to_i2c_client(dev
);
570 struct mms114_data
*data
= i2c_get_clientdata(client
);
571 struct input_dev
*input_dev
= data
->input_dev
;
574 mutex_lock(&input_dev
->mutex
);
575 if (input_dev
->users
) {
576 error
= mms114_start(data
);
578 mutex_unlock(&input_dev
->mutex
);
582 mutex_unlock(&input_dev
->mutex
);
587 static SIMPLE_DEV_PM_OPS(mms114_pm_ops
, mms114_suspend
, mms114_resume
);
589 static const struct i2c_device_id mms114_id
[] = {
593 MODULE_DEVICE_TABLE(i2c
, mms114_id
);
596 static const struct of_device_id mms114_dt_match
[] = {
598 .compatible
= "melfas,mms114",
599 .data
= (void *)TYPE_MMS114
,
601 .compatible
= "melfas,mms152",
602 .data
= (void *)TYPE_MMS152
,
606 MODULE_DEVICE_TABLE(of
, mms114_dt_match
);
609 static struct i2c_driver mms114_driver
= {
612 .pm
= &mms114_pm_ops
,
613 .of_match_table
= of_match_ptr(mms114_dt_match
),
615 .probe
= mms114_probe
,
616 .id_table
= mms114_id
,
619 module_i2c_driver(mms114_driver
);
621 /* Module information */
622 MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
623 MODULE_DESCRIPTION("MELFAS mms114 Touchscreen driver");
624 MODULE_LICENSE("GPL v2");