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
61 struct i2c_client
*client
;
62 struct input_dev
*input_dev
;
63 struct regulator
*core_reg
;
64 struct regulator
*io_reg
;
65 struct touchscreen_properties props
;
67 unsigned int contact_threshold
;
68 unsigned int moving_threshold
;
70 /* Use cache data for mode control register(write only) */
71 u8 cache_mode_control
;
75 u8 id
:4, reserved_bit4
:1, type
:2, pressed
:1;
84 static int __mms114_read_reg(struct mms114_data
*data
, unsigned int reg
,
85 unsigned int len
, u8
*val
)
87 struct i2c_client
*client
= data
->client
;
88 struct i2c_msg xfer
[2];
92 if (reg
<= MMS114_MODE_CONTROL
&& reg
+ len
> MMS114_MODE_CONTROL
)
96 xfer
[0].addr
= client
->addr
;
97 xfer
[0].flags
= client
->flags
& I2C_M_TEN
;
102 xfer
[1].addr
= client
->addr
;
103 xfer
[1].flags
= (client
->flags
& I2C_M_TEN
) | I2C_M_RD
;
107 error
= i2c_transfer(client
->adapter
, xfer
, 2);
109 dev_err(&client
->dev
,
110 "%s: i2c transfer failed (%d)\n", __func__
, error
);
111 return error
< 0 ? error
: -EIO
;
113 udelay(MMS114_I2C_DELAY
);
118 static int mms114_read_reg(struct mms114_data
*data
, unsigned int reg
)
123 if (reg
== MMS114_MODE_CONTROL
)
124 return data
->cache_mode_control
;
126 error
= __mms114_read_reg(data
, reg
, 1, &val
);
127 return error
< 0 ? error
: val
;
130 static int mms114_write_reg(struct mms114_data
*data
, unsigned int reg
,
133 struct i2c_client
*client
= data
->client
;
140 error
= i2c_master_send(client
, buf
, 2);
142 dev_err(&client
->dev
,
143 "%s: i2c send failed (%d)\n", __func__
, error
);
144 return error
< 0 ? error
: -EIO
;
146 udelay(MMS114_I2C_DELAY
);
148 if (reg
== MMS114_MODE_CONTROL
)
149 data
->cache_mode_control
= val
;
154 static void mms114_process_mt(struct mms114_data
*data
, struct mms114_touch
*touch
)
156 struct i2c_client
*client
= data
->client
;
157 struct input_dev
*input_dev
= data
->input_dev
;
162 if (touch
->id
> MMS114_MAX_TOUCH
) {
163 dev_err(&client
->dev
, "Wrong touch id (%d)\n", touch
->id
);
167 if (touch
->type
!= MMS114_TYPE_TOUCHSCREEN
) {
168 dev_err(&client
->dev
, "Wrong touch type (%d)\n", touch
->type
);
173 x
= touch
->x_lo
| touch
->x_hi
<< 8;
174 y
= touch
->y_lo
| touch
->y_hi
<< 8;
176 dev_dbg(&client
->dev
,
177 "id: %d, type: %d, pressed: %d, x: %d, y: %d, width: %d, strength: %d\n",
178 id
, touch
->type
, touch
->pressed
,
179 x
, y
, touch
->width
, touch
->strength
);
181 input_mt_slot(input_dev
, id
);
182 input_mt_report_slot_state(input_dev
, MT_TOOL_FINGER
, touch
->pressed
);
184 if (touch
->pressed
) {
185 touchscreen_report_pos(input_dev
, &data
->props
, x
, y
, true);
186 input_report_abs(input_dev
, ABS_MT_TOUCH_MAJOR
, touch
->width
);
187 input_report_abs(input_dev
, ABS_MT_PRESSURE
, touch
->strength
);
191 static irqreturn_t
mms114_interrupt(int irq
, void *dev_id
)
193 struct mms114_data
*data
= dev_id
;
194 struct input_dev
*input_dev
= data
->input_dev
;
195 struct mms114_touch touch
[MMS114_MAX_TOUCH
];
201 mutex_lock(&input_dev
->mutex
);
202 if (!input_device_enabled(input_dev
)) {
203 mutex_unlock(&input_dev
->mutex
);
206 mutex_unlock(&input_dev
->mutex
);
208 packet_size
= mms114_read_reg(data
, MMS114_PACKET_SIZE
);
209 if (packet_size
<= 0)
212 touch_size
= packet_size
/ MMS114_PACKET_NUM
;
214 error
= __mms114_read_reg(data
, MMS114_INFORMATION
, packet_size
,
219 for (index
= 0; index
< touch_size
; index
++)
220 mms114_process_mt(data
, touch
+ index
);
222 input_mt_report_pointer_emulation(data
->input_dev
, true);
223 input_sync(data
->input_dev
);
229 static int mms114_set_active(struct mms114_data
*data
, bool active
)
233 val
= mms114_read_reg(data
, MMS114_MODE_CONTROL
);
237 val
&= ~MMS114_OPERATION_MODE_MASK
;
239 /* If active is false, sleep mode */
241 val
|= MMS114_ACTIVE
;
243 return mms114_write_reg(data
, MMS114_MODE_CONTROL
, val
);
246 static int mms114_get_version(struct mms114_data
*data
)
248 struct device
*dev
= &data
->client
->dev
;
253 switch (data
->type
) {
255 error
= __mms114_read_reg(data
, MMS152_FW_REV
, 3, buf
);
259 dev_info(dev
, "TSP FW Rev: bootloader 0x%x / core 0x%x / config 0x%x\n",
260 buf
[0], buf
[1], buf
[2]);
264 error
= __mms114_read_reg(data
, MMS152_FW_REV
, 3, buf
);
268 group
= i2c_smbus_read_byte_data(data
->client
,
269 MMS152_COMPAT_GROUP
);
273 dev_info(dev
, "TSP FW Rev: bootloader 0x%x / core 0x%x / config 0x%x, Compat group: %c\n",
274 buf
[0], buf
[1], buf
[2], group
);
278 error
= __mms114_read_reg(data
, MMS114_TSP_REV
, 6, buf
);
282 dev_info(dev
, "TSP Rev: 0x%x, HW Rev: 0x%x, Firmware Ver: 0x%x\n",
283 buf
[0], buf
[1], buf
[3]);
290 static int mms114_setup_regs(struct mms114_data
*data
)
292 const struct touchscreen_properties
*props
= &data
->props
;
296 error
= mms114_get_version(data
);
300 /* Only MMS114 has configuration and power on registers */
301 if (data
->type
!= TYPE_MMS114
)
304 error
= mms114_set_active(data
, true);
308 val
= (props
->max_x
>> 8) & 0xf;
309 val
|= ((props
->max_y
>> 8) & 0xf) << 4;
310 error
= mms114_write_reg(data
, MMS114_XY_RESOLUTION_H
, val
);
314 val
= props
->max_x
& 0xff;
315 error
= mms114_write_reg(data
, MMS114_X_RESOLUTION
, val
);
319 val
= props
->max_x
& 0xff;
320 error
= mms114_write_reg(data
, MMS114_Y_RESOLUTION
, val
);
324 if (data
->contact_threshold
) {
325 error
= mms114_write_reg(data
, MMS114_CONTACT_THRESHOLD
,
326 data
->contact_threshold
);
331 if (data
->moving_threshold
) {
332 error
= mms114_write_reg(data
, MMS114_MOVING_THRESHOLD
,
333 data
->moving_threshold
);
341 static int mms114_start(struct mms114_data
*data
)
343 struct i2c_client
*client
= data
->client
;
346 error
= regulator_enable(data
->core_reg
);
348 dev_err(&client
->dev
, "Failed to enable avdd: %d\n", error
);
352 error
= regulator_enable(data
->io_reg
);
354 dev_err(&client
->dev
, "Failed to enable vdd: %d\n", error
);
355 regulator_disable(data
->core_reg
);
359 msleep(MMS114_POWERON_DELAY
);
361 error
= mms114_setup_regs(data
);
363 regulator_disable(data
->io_reg
);
364 regulator_disable(data
->core_reg
);
368 enable_irq(client
->irq
);
373 static void mms114_stop(struct mms114_data
*data
)
375 struct i2c_client
*client
= data
->client
;
378 disable_irq(client
->irq
);
380 error
= regulator_disable(data
->io_reg
);
382 dev_warn(&client
->dev
, "Failed to disable vdd: %d\n", error
);
384 error
= regulator_disable(data
->core_reg
);
386 dev_warn(&client
->dev
, "Failed to disable avdd: %d\n", error
);
389 static int mms114_input_open(struct input_dev
*dev
)
391 struct mms114_data
*data
= input_get_drvdata(dev
);
393 return mms114_start(data
);
396 static void mms114_input_close(struct input_dev
*dev
)
398 struct mms114_data
*data
= input_get_drvdata(dev
);
403 static int mms114_parse_legacy_bindings(struct mms114_data
*data
)
405 struct device
*dev
= &data
->client
->dev
;
406 struct touchscreen_properties
*props
= &data
->props
;
408 if (device_property_read_u32(dev
, "x-size", &props
->max_x
)) {
409 dev_dbg(dev
, "failed to get legacy x-size property\n");
413 if (device_property_read_u32(dev
, "y-size", &props
->max_y
)) {
414 dev_dbg(dev
, "failed to get legacy y-size property\n");
418 device_property_read_u32(dev
, "contact-threshold",
419 &data
->contact_threshold
);
420 device_property_read_u32(dev
, "moving-threshold",
421 &data
->moving_threshold
);
423 if (device_property_read_bool(dev
, "x-invert"))
424 props
->invert_x
= true;
425 if (device_property_read_bool(dev
, "y-invert"))
426 props
->invert_y
= true;
428 props
->swap_x_y
= false;
433 static int mms114_probe(struct i2c_client
*client
,
434 const struct i2c_device_id
*id
)
436 struct mms114_data
*data
;
437 struct input_dev
*input_dev
;
438 const void *match_data
;
441 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_I2C
)) {
442 dev_err(&client
->dev
, "Not supported I2C adapter\n");
446 data
= devm_kzalloc(&client
->dev
, sizeof(struct mms114_data
),
448 input_dev
= devm_input_allocate_device(&client
->dev
);
449 if (!data
|| !input_dev
) {
450 dev_err(&client
->dev
, "Failed to allocate memory\n");
454 data
->client
= client
;
455 data
->input_dev
= input_dev
;
457 match_data
= device_get_match_data(&client
->dev
);
461 data
->type
= (enum mms_type
)match_data
;
463 input_set_capability(input_dev
, EV_ABS
, ABS_MT_POSITION_X
);
464 input_set_capability(input_dev
, EV_ABS
, ABS_MT_POSITION_Y
);
465 input_set_abs_params(input_dev
, ABS_MT_PRESSURE
, 0, 255, 0, 0);
466 input_set_abs_params(input_dev
, ABS_MT_TOUCH_MAJOR
,
467 0, MMS114_MAX_AREA
, 0, 0);
469 touchscreen_parse_properties(input_dev
, true, &data
->props
);
470 if (!data
->props
.max_x
|| !data
->props
.max_y
) {
471 dev_dbg(&client
->dev
,
472 "missing X/Y size properties, trying legacy bindings\n");
473 error
= mms114_parse_legacy_bindings(data
);
477 input_set_abs_params(input_dev
, ABS_MT_POSITION_X
,
478 0, data
->props
.max_x
, 0, 0);
479 input_set_abs_params(input_dev
, ABS_MT_POSITION_Y
,
480 0, data
->props
.max_y
, 0, 0);
483 if (data
->type
== TYPE_MMS114
) {
485 * The firmware handles movement and pressure fuzz, so
486 * don't duplicate that in software.
488 data
->moving_threshold
= input_abs_get_fuzz(input_dev
,
490 data
->contact_threshold
= input_abs_get_fuzz(input_dev
,
492 input_abs_set_fuzz(input_dev
, ABS_MT_POSITION_X
, 0);
493 input_abs_set_fuzz(input_dev
, ABS_MT_POSITION_Y
, 0);
494 input_abs_set_fuzz(input_dev
, ABS_MT_PRESSURE
, 0);
497 input_dev
->name
= devm_kasprintf(&client
->dev
, GFP_KERNEL
,
498 "MELFAS MMS%d Touchscreen",
500 if (!input_dev
->name
)
503 input_dev
->id
.bustype
= BUS_I2C
;
504 input_dev
->dev
.parent
= &client
->dev
;
505 input_dev
->open
= mms114_input_open
;
506 input_dev
->close
= mms114_input_close
;
508 error
= input_mt_init_slots(input_dev
, MMS114_MAX_TOUCH
,
513 input_set_drvdata(input_dev
, data
);
514 i2c_set_clientdata(client
, data
);
516 data
->core_reg
= devm_regulator_get(&client
->dev
, "avdd");
517 if (IS_ERR(data
->core_reg
)) {
518 error
= PTR_ERR(data
->core_reg
);
519 dev_err(&client
->dev
,
520 "Unable to get the Core regulator (%d)\n", error
);
524 data
->io_reg
= devm_regulator_get(&client
->dev
, "vdd");
525 if (IS_ERR(data
->io_reg
)) {
526 error
= PTR_ERR(data
->io_reg
);
527 dev_err(&client
->dev
,
528 "Unable to get the IO regulator (%d)\n", error
);
532 error
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
533 NULL
, mms114_interrupt
, IRQF_ONESHOT
,
534 dev_name(&client
->dev
), data
);
536 dev_err(&client
->dev
, "Failed to register interrupt\n");
539 disable_irq(client
->irq
);
541 error
= input_register_device(data
->input_dev
);
543 dev_err(&client
->dev
, "Failed to register input device\n");
550 static int __maybe_unused
mms114_suspend(struct device
*dev
)
552 struct i2c_client
*client
= to_i2c_client(dev
);
553 struct mms114_data
*data
= i2c_get_clientdata(client
);
554 struct input_dev
*input_dev
= data
->input_dev
;
557 /* Release all touch */
558 for (id
= 0; id
< MMS114_MAX_TOUCH
; id
++) {
559 input_mt_slot(input_dev
, id
);
560 input_mt_report_slot_inactive(input_dev
);
563 input_mt_report_pointer_emulation(input_dev
, true);
564 input_sync(input_dev
);
566 mutex_lock(&input_dev
->mutex
);
567 if (input_device_enabled(input_dev
))
569 mutex_unlock(&input_dev
->mutex
);
574 static int __maybe_unused
mms114_resume(struct device
*dev
)
576 struct i2c_client
*client
= to_i2c_client(dev
);
577 struct mms114_data
*data
= i2c_get_clientdata(client
);
578 struct input_dev
*input_dev
= data
->input_dev
;
581 mutex_lock(&input_dev
->mutex
);
582 if (input_device_enabled(input_dev
)) {
583 error
= mms114_start(data
);
585 mutex_unlock(&input_dev
->mutex
);
589 mutex_unlock(&input_dev
->mutex
);
594 static SIMPLE_DEV_PM_OPS(mms114_pm_ops
, mms114_suspend
, mms114_resume
);
596 static const struct i2c_device_id mms114_id
[] = {
600 MODULE_DEVICE_TABLE(i2c
, mms114_id
);
603 static const struct of_device_id mms114_dt_match
[] = {
605 .compatible
= "melfas,mms114",
606 .data
= (void *)TYPE_MMS114
,
608 .compatible
= "melfas,mms152",
609 .data
= (void *)TYPE_MMS152
,
611 .compatible
= "melfas,mms345l",
612 .data
= (void *)TYPE_MMS345L
,
616 MODULE_DEVICE_TABLE(of
, mms114_dt_match
);
619 static struct i2c_driver mms114_driver
= {
622 .pm
= &mms114_pm_ops
,
623 .of_match_table
= of_match_ptr(mms114_dt_match
),
625 .probe
= mms114_probe
,
626 .id_table
= mms114_id
,
629 module_i2c_driver(mms114_driver
);
631 /* Module information */
632 MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
633 MODULE_DESCRIPTION("MELFAS mms114 Touchscreen driver");
634 MODULE_LICENSE("GPL v2");