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/init.h>
12 #include <linux/delay.h>
14 #include <linux/i2c.h>
15 #include <linux/i2c/mms114.h>
16 #include <linux/input/mt.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 (1 << 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_INFOMATION 0x10
35 #define MMS114_TSP_REV 0xF0
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
55 struct i2c_client
*client
;
56 struct input_dev
*input_dev
;
57 struct regulator
*core_reg
;
58 struct regulator
*io_reg
;
59 const struct mms114_platform_data
*pdata
;
61 /* Use cache data for mode control register(write only) */
62 u8 cache_mode_control
;
66 u8 id
:4, reserved_bit4
:1, type
:2, pressed
:1;
75 static int __mms114_read_reg(struct mms114_data
*data
, unsigned int reg
,
76 unsigned int len
, u8
*val
)
78 struct i2c_client
*client
= data
->client
;
79 struct i2c_msg xfer
[2];
83 if (reg
<= MMS114_MODE_CONTROL
&& reg
+ len
> MMS114_MODE_CONTROL
)
86 /* Write register: use repeated start */
87 xfer
[0].addr
= client
->addr
;
88 xfer
[0].flags
= I2C_M_TEN
| I2C_M_NOSTART
;
93 xfer
[1].addr
= client
->addr
;
94 xfer
[1].flags
= I2C_M_RD
;
98 error
= i2c_transfer(client
->adapter
, xfer
, 2);
100 dev_err(&client
->dev
,
101 "%s: i2c transfer failed (%d)\n", __func__
, error
);
102 return error
< 0 ? error
: -EIO
;
104 udelay(MMS114_I2C_DELAY
);
109 static int mms114_read_reg(struct mms114_data
*data
, unsigned int reg
)
114 if (reg
== MMS114_MODE_CONTROL
)
115 return data
->cache_mode_control
;
117 error
= __mms114_read_reg(data
, reg
, 1, &val
);
118 return error
< 0 ? error
: val
;
121 static int mms114_write_reg(struct mms114_data
*data
, unsigned int reg
,
124 struct i2c_client
*client
= data
->client
;
131 error
= i2c_master_send(client
, buf
, 2);
133 dev_err(&client
->dev
,
134 "%s: i2c send failed (%d)\n", __func__
, error
);
135 return error
< 0 ? error
: -EIO
;
137 udelay(MMS114_I2C_DELAY
);
139 if (reg
== MMS114_MODE_CONTROL
)
140 data
->cache_mode_control
= val
;
145 static void mms114_process_mt(struct mms114_data
*data
, struct mms114_touch
*touch
)
147 const struct mms114_platform_data
*pdata
= data
->pdata
;
148 struct i2c_client
*client
= data
->client
;
149 struct input_dev
*input_dev
= data
->input_dev
;
154 if (touch
->id
> MMS114_MAX_TOUCH
) {
155 dev_err(&client
->dev
, "Wrong touch id (%d)\n", touch
->id
);
159 if (touch
->type
!= MMS114_TYPE_TOUCHSCREEN
) {
160 dev_err(&client
->dev
, "Wrong touch type (%d)\n", touch
->type
);
165 x
= touch
->x_lo
| touch
->x_hi
<< 8;
166 y
= touch
->y_lo
| touch
->y_hi
<< 8;
167 if (x
> pdata
->x_size
|| y
> pdata
->y_size
) {
168 dev_dbg(&client
->dev
,
169 "Wrong touch coordinates (%d, %d)\n", x
, y
);
174 x
= pdata
->x_size
- x
;
176 y
= pdata
->y_size
- y
;
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 input_report_abs(input_dev
, ABS_MT_TOUCH_MAJOR
, touch
->width
);
188 input_report_abs(input_dev
, ABS_MT_POSITION_X
, x
);
189 input_report_abs(input_dev
, ABS_MT_POSITION_Y
, y
);
190 input_report_abs(input_dev
, ABS_MT_PRESSURE
, touch
->strength
);
194 static irqreturn_t
mms114_interrupt(int irq
, void *dev_id
)
196 struct mms114_data
*data
= dev_id
;
197 struct input_dev
*input_dev
= data
->input_dev
;
198 struct mms114_touch touch
[MMS114_MAX_TOUCH
];
204 mutex_lock(&input_dev
->mutex
);
205 if (!input_dev
->users
) {
206 mutex_unlock(&input_dev
->mutex
);
209 mutex_unlock(&input_dev
->mutex
);
211 packet_size
= mms114_read_reg(data
, MMS114_PACKET_SIZE
);
212 if (packet_size
<= 0)
215 touch_size
= packet_size
/ MMS114_PACKET_NUM
;
217 error
= __mms114_read_reg(data
, MMS114_INFOMATION
, packet_size
,
222 for (index
= 0; index
< touch_size
; index
++)
223 mms114_process_mt(data
, touch
+ index
);
225 input_mt_report_pointer_emulation(data
->input_dev
, true);
226 input_sync(data
->input_dev
);
232 static int mms114_set_active(struct mms114_data
*data
, bool active
)
236 val
= mms114_read_reg(data
, MMS114_MODE_CONTROL
);
240 val
&= ~MMS114_OPERATION_MODE_MASK
;
242 /* If active is false, sleep mode */
244 val
|= MMS114_ACTIVE
;
246 return mms114_write_reg(data
, MMS114_MODE_CONTROL
, val
);
249 static int mms114_get_version(struct mms114_data
*data
)
251 struct device
*dev
= &data
->client
->dev
;
255 error
= __mms114_read_reg(data
, MMS114_TSP_REV
, 6, buf
);
259 dev_info(dev
, "TSP Rev: 0x%x, HW Rev: 0x%x, Firmware Ver: 0x%x\n",
260 buf
[0], buf
[1], buf
[3]);
265 static int mms114_setup_regs(struct mms114_data
*data
)
267 const struct mms114_platform_data
*pdata
= data
->pdata
;
271 error
= mms114_get_version(data
);
275 error
= mms114_set_active(data
, true);
279 val
= (pdata
->x_size
>> 8) & 0xf;
280 val
|= ((pdata
->y_size
>> 8) & 0xf) << 4;
281 error
= mms114_write_reg(data
, MMS114_XY_RESOLUTION_H
, val
);
285 val
= pdata
->x_size
& 0xff;
286 error
= mms114_write_reg(data
, MMS114_X_RESOLUTION
, val
);
290 val
= pdata
->y_size
& 0xff;
291 error
= mms114_write_reg(data
, MMS114_Y_RESOLUTION
, val
);
295 if (pdata
->contact_threshold
) {
296 error
= mms114_write_reg(data
, MMS114_CONTACT_THRESHOLD
,
297 pdata
->contact_threshold
);
302 if (pdata
->moving_threshold
) {
303 error
= mms114_write_reg(data
, MMS114_MOVING_THRESHOLD
,
304 pdata
->moving_threshold
);
312 static int mms114_start(struct mms114_data
*data
)
314 struct i2c_client
*client
= data
->client
;
317 error
= regulator_enable(data
->core_reg
);
319 dev_err(&client
->dev
, "Failed to enable avdd: %d\n", error
);
323 error
= regulator_enable(data
->io_reg
);
325 dev_err(&client
->dev
, "Failed to enable vdd: %d\n", error
);
326 regulator_disable(data
->core_reg
);
330 mdelay(MMS114_POWERON_DELAY
);
332 error
= mms114_setup_regs(data
);
334 regulator_disable(data
->io_reg
);
335 regulator_disable(data
->core_reg
);
339 if (data
->pdata
->cfg_pin
)
340 data
->pdata
->cfg_pin(true);
342 enable_irq(client
->irq
);
347 static void mms114_stop(struct mms114_data
*data
)
349 struct i2c_client
*client
= data
->client
;
352 disable_irq(client
->irq
);
354 if (data
->pdata
->cfg_pin
)
355 data
->pdata
->cfg_pin(false);
357 error
= regulator_disable(data
->io_reg
);
359 dev_warn(&client
->dev
, "Failed to disable vdd: %d\n", error
);
361 error
= regulator_disable(data
->core_reg
);
363 dev_warn(&client
->dev
, "Failed to disable avdd: %d\n", error
);
366 static int mms114_input_open(struct input_dev
*dev
)
368 struct mms114_data
*data
= input_get_drvdata(dev
);
370 return mms114_start(data
);
373 static void mms114_input_close(struct input_dev
*dev
)
375 struct mms114_data
*data
= input_get_drvdata(dev
);
381 static struct mms114_platform_data
*mms114_parse_dt(struct device
*dev
)
383 struct mms114_platform_data
*pdata
;
384 struct device_node
*np
= dev
->of_node
;
389 pdata
= devm_kzalloc(dev
, sizeof(*pdata
), GFP_KERNEL
);
391 dev_err(dev
, "failed to allocate platform data\n");
395 if (of_property_read_u32(np
, "x-size", &pdata
->x_size
)) {
396 dev_err(dev
, "failed to get x-size property\n");
400 if (of_property_read_u32(np
, "y-size", &pdata
->y_size
)) {
401 dev_err(dev
, "failed to get y-size property\n");
405 of_property_read_u32(np
, "contact-threshold",
406 &pdata
->contact_threshold
);
407 of_property_read_u32(np
, "moving-threshold",
408 &pdata
->moving_threshold
);
410 if (of_find_property(np
, "x-invert", NULL
))
411 pdata
->x_invert
= true;
412 if (of_find_property(np
, "y-invert", NULL
))
413 pdata
->y_invert
= true;
418 static inline struct mms114_platform_data
*mms114_parse_dt(struct device
*dev
)
424 static int mms114_probe(struct i2c_client
*client
,
425 const struct i2c_device_id
*id
)
427 const struct mms114_platform_data
*pdata
;
428 struct mms114_data
*data
;
429 struct input_dev
*input_dev
;
432 pdata
= dev_get_platdata(&client
->dev
);
434 pdata
= mms114_parse_dt(&client
->dev
);
437 dev_err(&client
->dev
, "Need platform data\n");
441 if (!i2c_check_functionality(client
->adapter
,
442 I2C_FUNC_PROTOCOL_MANGLING
)) {
443 dev_err(&client
->dev
,
444 "Need i2c bus that supports protocol mangling\n");
448 data
= devm_kzalloc(&client
->dev
, sizeof(struct mms114_data
),
450 input_dev
= devm_input_allocate_device(&client
->dev
);
451 if (!data
|| !input_dev
) {
452 dev_err(&client
->dev
, "Failed to allocate memory\n");
456 data
->client
= client
;
457 data
->input_dev
= input_dev
;
460 input_dev
->name
= "MELPAS MMS114 Touchscreen";
461 input_dev
->id
.bustype
= BUS_I2C
;
462 input_dev
->dev
.parent
= &client
->dev
;
463 input_dev
->open
= mms114_input_open
;
464 input_dev
->close
= mms114_input_close
;
466 __set_bit(EV_ABS
, input_dev
->evbit
);
467 __set_bit(EV_KEY
, input_dev
->evbit
);
468 __set_bit(BTN_TOUCH
, input_dev
->keybit
);
469 input_set_abs_params(input_dev
, ABS_X
, 0, data
->pdata
->x_size
, 0, 0);
470 input_set_abs_params(input_dev
, ABS_Y
, 0, data
->pdata
->y_size
, 0, 0);
472 /* For multi touch */
473 input_mt_init_slots(input_dev
, MMS114_MAX_TOUCH
, 0);
474 input_set_abs_params(input_dev
, ABS_MT_TOUCH_MAJOR
,
475 0, MMS114_MAX_AREA
, 0, 0);
476 input_set_abs_params(input_dev
, ABS_MT_POSITION_X
,
477 0, data
->pdata
->x_size
, 0, 0);
478 input_set_abs_params(input_dev
, ABS_MT_POSITION_Y
,
479 0, data
->pdata
->y_size
, 0, 0);
480 input_set_abs_params(input_dev
, ABS_MT_PRESSURE
, 0, 255, 0, 0);
482 input_set_drvdata(input_dev
, data
);
483 i2c_set_clientdata(client
, data
);
485 data
->core_reg
= devm_regulator_get(&client
->dev
, "avdd");
486 if (IS_ERR(data
->core_reg
)) {
487 error
= PTR_ERR(data
->core_reg
);
488 dev_err(&client
->dev
,
489 "Unable to get the Core regulator (%d)\n", error
);
493 data
->io_reg
= devm_regulator_get(&client
->dev
, "vdd");
494 if (IS_ERR(data
->io_reg
)) {
495 error
= PTR_ERR(data
->io_reg
);
496 dev_err(&client
->dev
,
497 "Unable to get the IO regulator (%d)\n", error
);
501 error
= devm_request_threaded_irq(&client
->dev
, client
->irq
, NULL
,
502 mms114_interrupt
, IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
503 dev_name(&client
->dev
), data
);
505 dev_err(&client
->dev
, "Failed to register interrupt\n");
508 disable_irq(client
->irq
);
510 error
= input_register_device(data
->input_dev
);
512 dev_err(&client
->dev
, "Failed to register input device\n");
519 #ifdef CONFIG_PM_SLEEP
520 static int mms114_suspend(struct device
*dev
)
522 struct i2c_client
*client
= to_i2c_client(dev
);
523 struct mms114_data
*data
= i2c_get_clientdata(client
);
524 struct input_dev
*input_dev
= data
->input_dev
;
527 /* Release all touch */
528 for (id
= 0; id
< MMS114_MAX_TOUCH
; id
++) {
529 input_mt_slot(input_dev
, id
);
530 input_mt_report_slot_state(input_dev
, MT_TOOL_FINGER
, false);
533 input_mt_report_pointer_emulation(input_dev
, true);
534 input_sync(input_dev
);
536 mutex_lock(&input_dev
->mutex
);
537 if (input_dev
->users
)
539 mutex_unlock(&input_dev
->mutex
);
544 static int mms114_resume(struct device
*dev
)
546 struct i2c_client
*client
= to_i2c_client(dev
);
547 struct mms114_data
*data
= i2c_get_clientdata(client
);
548 struct input_dev
*input_dev
= data
->input_dev
;
551 mutex_lock(&input_dev
->mutex
);
552 if (input_dev
->users
) {
553 error
= mms114_start(data
);
555 mutex_unlock(&input_dev
->mutex
);
559 mutex_unlock(&input_dev
->mutex
);
565 static SIMPLE_DEV_PM_OPS(mms114_pm_ops
, mms114_suspend
, mms114_resume
);
567 static const struct i2c_device_id mms114_id
[] = {
571 MODULE_DEVICE_TABLE(i2c
, mms114_id
);
574 static struct of_device_id mms114_dt_match
[] = {
575 { .compatible
= "melfas,mms114" },
580 static struct i2c_driver mms114_driver
= {
583 .owner
= THIS_MODULE
,
584 .pm
= &mms114_pm_ops
,
585 .of_match_table
= of_match_ptr(mms114_dt_match
),
587 .probe
= mms114_probe
,
588 .id_table
= mms114_id
,
591 module_i2c_driver(mms114_driver
);
593 /* Module information */
594 MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
595 MODULE_DESCRIPTION("MELFAS mms114 Touchscreen driver");
596 MODULE_LICENSE("GPL");