2 * Copyright (C) 2012 Samsung Electronics Co.Ltd
3 * Author: Joonyoung Shim <jy0922.shim@samsung.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
10 #include <linux/module.h>
11 #include <linux/delay.h>
13 #include <linux/of_device.h>
14 #include <linux/i2c.h>
15 #include <linux/input/mt.h>
16 #include <linux/input/touchscreen.h>
17 #include <linux/interrupt.h>
18 #include <linux/regulator/consumer.h>
19 #include <linux/slab.h>
21 /* Write only registers */
22 #define MMS114_MODE_CONTROL 0x01
23 #define MMS114_OPERATION_MODE_MASK 0xE
24 #define MMS114_ACTIVE BIT(1)
26 #define MMS114_XY_RESOLUTION_H 0x02
27 #define MMS114_X_RESOLUTION 0x03
28 #define MMS114_Y_RESOLUTION 0x04
29 #define MMS114_CONTACT_THRESHOLD 0x05
30 #define MMS114_MOVING_THRESHOLD 0x06
32 /* Read only registers */
33 #define MMS114_PACKET_SIZE 0x0F
34 #define MMS114_INFORMATION 0x10
35 #define MMS114_TSP_REV 0xF0
37 #define MMS152_FW_REV 0xE1
38 #define MMS152_COMPAT_GROUP 0xF2
40 /* Minimum delay time is 50us between stop and start signal of i2c */
41 #define MMS114_I2C_DELAY 50
43 /* 200ms needs after power on */
44 #define MMS114_POWERON_DELAY 200
46 /* Touchscreen absolute values */
47 #define MMS114_MAX_AREA 0xff
49 #define MMS114_MAX_TOUCH 10
50 #define MMS114_PACKET_NUM 8
53 #define MMS114_TYPE_NONE 0
54 #define MMS114_TYPE_TOUCHSCREEN 1
55 #define MMS114_TYPE_TOUCHKEY 2
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
;
69 unsigned int contact_threshold
;
70 unsigned int moving_threshold
;
72 /* Use cache data for mode control register(write only) */
73 u8 cache_mode_control
;
77 u8 id
:4, reserved_bit4
:1, type
:2, pressed
:1;
86 static int __mms114_read_reg(struct mms114_data
*data
, unsigned int reg
,
87 unsigned int len
, u8
*val
)
89 struct i2c_client
*client
= data
->client
;
90 struct i2c_msg xfer
[2];
94 if (reg
<= MMS114_MODE_CONTROL
&& reg
+ len
> MMS114_MODE_CONTROL
)
97 /* Write register: use repeated start */
98 xfer
[0].addr
= client
->addr
;
99 xfer
[0].flags
= I2C_M_TEN
| I2C_M_NOSTART
;
104 xfer
[1].addr
= client
->addr
;
105 xfer
[1].flags
= I2C_M_RD
;
109 error
= i2c_transfer(client
->adapter
, xfer
, 2);
111 dev_err(&client
->dev
,
112 "%s: i2c transfer failed (%d)\n", __func__
, error
);
113 return error
< 0 ? error
: -EIO
;
115 udelay(MMS114_I2C_DELAY
);
120 static int mms114_read_reg(struct mms114_data
*data
, unsigned int reg
)
125 if (reg
== MMS114_MODE_CONTROL
)
126 return data
->cache_mode_control
;
128 error
= __mms114_read_reg(data
, reg
, 1, &val
);
129 return error
< 0 ? error
: val
;
132 static int mms114_write_reg(struct mms114_data
*data
, unsigned int reg
,
135 struct i2c_client
*client
= data
->client
;
142 error
= i2c_master_send(client
, buf
, 2);
144 dev_err(&client
->dev
,
145 "%s: i2c send failed (%d)\n", __func__
, error
);
146 return error
< 0 ? error
: -EIO
;
148 udelay(MMS114_I2C_DELAY
);
150 if (reg
== MMS114_MODE_CONTROL
)
151 data
->cache_mode_control
= val
;
156 static void mms114_process_mt(struct mms114_data
*data
, struct mms114_touch
*touch
)
158 struct i2c_client
*client
= data
->client
;
159 struct input_dev
*input_dev
= data
->input_dev
;
164 if (touch
->id
> MMS114_MAX_TOUCH
) {
165 dev_err(&client
->dev
, "Wrong touch id (%d)\n", touch
->id
);
169 if (touch
->type
!= MMS114_TYPE_TOUCHSCREEN
) {
170 dev_err(&client
->dev
, "Wrong touch type (%d)\n", touch
->type
);
175 x
= touch
->x_lo
| touch
->x_hi
<< 8;
176 y
= touch
->y_lo
| touch
->y_hi
<< 8;
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
);
183 input_mt_slot(input_dev
, id
);
184 input_mt_report_slot_state(input_dev
, MT_TOOL_FINGER
, touch
->pressed
);
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
);
193 static irqreturn_t
mms114_interrupt(int irq
, void *dev_id
)
195 struct mms114_data
*data
= dev_id
;
196 struct input_dev
*input_dev
= data
->input_dev
;
197 struct mms114_touch touch
[MMS114_MAX_TOUCH
];
203 mutex_lock(&input_dev
->mutex
);
204 if (!input_dev
->users
) {
205 mutex_unlock(&input_dev
->mutex
);
208 mutex_unlock(&input_dev
->mutex
);
210 packet_size
= mms114_read_reg(data
, MMS114_PACKET_SIZE
);
211 if (packet_size
<= 0)
214 touch_size
= packet_size
/ MMS114_PACKET_NUM
;
216 error
= __mms114_read_reg(data
, MMS114_INFORMATION
, packet_size
,
221 for (index
= 0; index
< touch_size
; index
++)
222 mms114_process_mt(data
, touch
+ index
);
224 input_mt_report_pointer_emulation(data
->input_dev
, true);
225 input_sync(data
->input_dev
);
231 static int mms114_set_active(struct mms114_data
*data
, bool active
)
235 val
= mms114_read_reg(data
, MMS114_MODE_CONTROL
);
239 val
&= ~MMS114_OPERATION_MODE_MASK
;
241 /* If active is false, sleep mode */
243 val
|= MMS114_ACTIVE
;
245 return mms114_write_reg(data
, MMS114_MODE_CONTROL
, val
);
248 static int mms114_get_version(struct mms114_data
*data
)
250 struct device
*dev
= &data
->client
->dev
;
255 switch (data
->type
) {
257 error
= __mms114_read_reg(data
, MMS152_FW_REV
, 3, buf
);
261 group
= i2c_smbus_read_byte_data(data
->client
,
262 MMS152_COMPAT_GROUP
);
266 dev_info(dev
, "TSP FW Rev: bootloader 0x%x / core 0x%x / config 0x%x, Compat group: %c\n",
267 buf
[0], buf
[1], buf
[2], group
);
271 error
= __mms114_read_reg(data
, MMS114_TSP_REV
, 6, buf
);
275 dev_info(dev
, "TSP Rev: 0x%x, HW Rev: 0x%x, Firmware Ver: 0x%x\n",
276 buf
[0], buf
[1], buf
[3]);
283 static int mms114_setup_regs(struct mms114_data
*data
)
285 const struct touchscreen_properties
*props
= &data
->props
;
289 error
= mms114_get_version(data
);
293 /* MMS152 has no configuration or power on registers */
294 if (data
->type
== TYPE_MMS152
)
297 error
= mms114_set_active(data
, true);
301 val
= (props
->max_x
>> 8) & 0xf;
302 val
|= ((props
->max_y
>> 8) & 0xf) << 4;
303 error
= mms114_write_reg(data
, MMS114_XY_RESOLUTION_H
, val
);
307 val
= props
->max_x
& 0xff;
308 error
= mms114_write_reg(data
, MMS114_X_RESOLUTION
, val
);
312 val
= props
->max_x
& 0xff;
313 error
= mms114_write_reg(data
, MMS114_Y_RESOLUTION
, val
);
317 if (data
->contact_threshold
) {
318 error
= mms114_write_reg(data
, MMS114_CONTACT_THRESHOLD
,
319 data
->contact_threshold
);
324 if (data
->moving_threshold
) {
325 error
= mms114_write_reg(data
, MMS114_MOVING_THRESHOLD
,
326 data
->moving_threshold
);
334 static int mms114_start(struct mms114_data
*data
)
336 struct i2c_client
*client
= data
->client
;
339 error
= regulator_enable(data
->core_reg
);
341 dev_err(&client
->dev
, "Failed to enable avdd: %d\n", error
);
345 error
= regulator_enable(data
->io_reg
);
347 dev_err(&client
->dev
, "Failed to enable vdd: %d\n", error
);
348 regulator_disable(data
->core_reg
);
352 msleep(MMS114_POWERON_DELAY
);
354 error
= mms114_setup_regs(data
);
356 regulator_disable(data
->io_reg
);
357 regulator_disable(data
->core_reg
);
361 enable_irq(client
->irq
);
366 static void mms114_stop(struct mms114_data
*data
)
368 struct i2c_client
*client
= data
->client
;
371 disable_irq(client
->irq
);
373 error
= regulator_disable(data
->io_reg
);
375 dev_warn(&client
->dev
, "Failed to disable vdd: %d\n", error
);
377 error
= regulator_disable(data
->core_reg
);
379 dev_warn(&client
->dev
, "Failed to disable avdd: %d\n", error
);
382 static int mms114_input_open(struct input_dev
*dev
)
384 struct mms114_data
*data
= input_get_drvdata(dev
);
386 return mms114_start(data
);
389 static void mms114_input_close(struct input_dev
*dev
)
391 struct mms114_data
*data
= input_get_drvdata(dev
);
396 static int mms114_parse_legacy_bindings(struct mms114_data
*data
)
398 struct device
*dev
= &data
->client
->dev
;
399 struct touchscreen_properties
*props
= &data
->props
;
401 if (device_property_read_u32(dev
, "x-size", &props
->max_x
)) {
402 dev_dbg(dev
, "failed to get legacy x-size property\n");
406 if (device_property_read_u32(dev
, "y-size", &props
->max_y
)) {
407 dev_dbg(dev
, "failed to get legacy y-size property\n");
411 device_property_read_u32(dev
, "contact-threshold",
412 &data
->contact_threshold
);
413 device_property_read_u32(dev
, "moving-threshold",
414 &data
->moving_threshold
);
416 if (device_property_read_bool(dev
, "x-invert"))
417 props
->invert_x
= true;
418 if (device_property_read_bool(dev
, "y-invert"))
419 props
->invert_y
= true;
421 props
->swap_x_y
= false;
426 static int mms114_probe(struct i2c_client
*client
,
427 const struct i2c_device_id
*id
)
429 struct mms114_data
*data
;
430 struct input_dev
*input_dev
;
431 const void *match_data
;
434 if (!i2c_check_functionality(client
->adapter
,
435 I2C_FUNC_PROTOCOL_MANGLING
)) {
436 dev_err(&client
->dev
,
437 "Need i2c bus that supports protocol mangling\n");
441 data
= devm_kzalloc(&client
->dev
, sizeof(struct mms114_data
),
443 input_dev
= devm_input_allocate_device(&client
->dev
);
444 if (!data
|| !input_dev
) {
445 dev_err(&client
->dev
, "Failed to allocate memory\n");
449 data
->client
= client
;
450 data
->input_dev
= input_dev
;
452 /* FIXME: switch to device_get_match_data() when available */
453 match_data
= of_device_get_match_data(&client
->dev
);
457 data
->type
= (enum mms_type
)match_data
;
459 input_set_capability(input_dev
, EV_ABS
, ABS_MT_POSITION_X
);
460 input_set_capability(input_dev
, EV_ABS
, ABS_MT_POSITION_Y
);
461 input_set_abs_params(input_dev
, ABS_MT_PRESSURE
, 0, 255, 0, 0);
462 input_set_abs_params(input_dev
, ABS_MT_TOUCH_MAJOR
,
463 0, MMS114_MAX_AREA
, 0, 0);
465 touchscreen_parse_properties(input_dev
, true, &data
->props
);
466 if (!data
->props
.max_x
|| !data
->props
.max_y
) {
467 dev_dbg(&client
->dev
,
468 "missing X/Y size properties, trying legacy bindings\n");
469 error
= mms114_parse_legacy_bindings(data
);
473 input_set_abs_params(input_dev
, ABS_MT_POSITION_X
,
474 0, data
->props
.max_x
, 0, 0);
475 input_set_abs_params(input_dev
, ABS_MT_POSITION_Y
,
476 0, data
->props
.max_y
, 0, 0);
479 if (data
->type
== TYPE_MMS114
) {
481 * The firmware handles movement and pressure fuzz, so
482 * don't duplicate that in software.
484 data
->moving_threshold
= input_abs_get_fuzz(input_dev
,
486 data
->contact_threshold
= input_abs_get_fuzz(input_dev
,
488 input_abs_set_fuzz(input_dev
, ABS_MT_POSITION_X
, 0);
489 input_abs_set_fuzz(input_dev
, ABS_MT_POSITION_Y
, 0);
490 input_abs_set_fuzz(input_dev
, ABS_MT_PRESSURE
, 0);
493 input_dev
->name
= devm_kasprintf(&client
->dev
, GFP_KERNEL
,
494 "MELFAS MMS%d Touchscreen",
496 if (!input_dev
->name
)
499 input_dev
->id
.bustype
= BUS_I2C
;
500 input_dev
->dev
.parent
= &client
->dev
;
501 input_dev
->open
= mms114_input_open
;
502 input_dev
->close
= mms114_input_close
;
504 error
= input_mt_init_slots(input_dev
, MMS114_MAX_TOUCH
,
509 input_set_drvdata(input_dev
, data
);
510 i2c_set_clientdata(client
, data
);
512 data
->core_reg
= devm_regulator_get(&client
->dev
, "avdd");
513 if (IS_ERR(data
->core_reg
)) {
514 error
= PTR_ERR(data
->core_reg
);
515 dev_err(&client
->dev
,
516 "Unable to get the Core regulator (%d)\n", error
);
520 data
->io_reg
= devm_regulator_get(&client
->dev
, "vdd");
521 if (IS_ERR(data
->io_reg
)) {
522 error
= PTR_ERR(data
->io_reg
);
523 dev_err(&client
->dev
,
524 "Unable to get the IO regulator (%d)\n", error
);
528 error
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
529 NULL
, mms114_interrupt
, IRQF_ONESHOT
,
530 dev_name(&client
->dev
), data
);
532 dev_err(&client
->dev
, "Failed to register interrupt\n");
535 disable_irq(client
->irq
);
537 error
= input_register_device(data
->input_dev
);
539 dev_err(&client
->dev
, "Failed to register input device\n");
546 static int __maybe_unused
mms114_suspend(struct device
*dev
)
548 struct i2c_client
*client
= to_i2c_client(dev
);
549 struct mms114_data
*data
= i2c_get_clientdata(client
);
550 struct input_dev
*input_dev
= data
->input_dev
;
553 /* Release all touch */
554 for (id
= 0; id
< MMS114_MAX_TOUCH
; id
++) {
555 input_mt_slot(input_dev
, id
);
556 input_mt_report_slot_state(input_dev
, MT_TOOL_FINGER
, false);
559 input_mt_report_pointer_emulation(input_dev
, true);
560 input_sync(input_dev
);
562 mutex_lock(&input_dev
->mutex
);
563 if (input_dev
->users
)
565 mutex_unlock(&input_dev
->mutex
);
570 static int __maybe_unused
mms114_resume(struct device
*dev
)
572 struct i2c_client
*client
= to_i2c_client(dev
);
573 struct mms114_data
*data
= i2c_get_clientdata(client
);
574 struct input_dev
*input_dev
= data
->input_dev
;
577 mutex_lock(&input_dev
->mutex
);
578 if (input_dev
->users
) {
579 error
= mms114_start(data
);
581 mutex_unlock(&input_dev
->mutex
);
585 mutex_unlock(&input_dev
->mutex
);
590 static SIMPLE_DEV_PM_OPS(mms114_pm_ops
, mms114_suspend
, mms114_resume
);
592 static const struct i2c_device_id mms114_id
[] = {
596 MODULE_DEVICE_TABLE(i2c
, mms114_id
);
599 static const struct of_device_id mms114_dt_match
[] = {
601 .compatible
= "melfas,mms114",
602 .data
= (void *)TYPE_MMS114
,
604 .compatible
= "melfas,mms152",
605 .data
= (void *)TYPE_MMS152
,
609 MODULE_DEVICE_TABLE(of
, mms114_dt_match
);
612 static struct i2c_driver mms114_driver
= {
615 .pm
= &mms114_pm_ops
,
616 .of_match_table
= of_match_ptr(mms114_dt_match
),
618 .probe
= mms114_probe
,
619 .id_table
= mms114_id
,
622 module_i2c_driver(mms114_driver
);
624 /* Module information */
625 MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
626 MODULE_DESCRIPTION("MELFAS mms114 Touchscreen driver");
627 MODULE_LICENSE("GPL");