1// SPDX-License-Identifier: GPL-2.0
2// Melfas MMS114/MMS152 touchscreen device driver
3//
4// Copyright (c) 2012 Samsung Electronics Co., Ltd.
5// Author: Joonyoung Shim <jy0922.shim@samsung.com>
6
7#include <linux/module.h>
8#include <linux/delay.h>
9#include <linux/of.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>
17
18/* Write only registers */
19#define MMS114_MODE_CONTROL 0x01
20#define MMS114_OPERATION_MODE_MASK 0xE
21#define MMS114_ACTIVE BIT(1)
22
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
28
29/* Read only registers */
30#define MMS114_PACKET_SIZE 0x0F
31#define MMS114_INFORMATION 0x10
32#define MMS114_TSP_REV 0xF0
33
34#define MMS152_FW_REV 0xE1
35#define MMS152_COMPAT_GROUP 0xF2
36
37/* Minimum delay time is 50us between stop and start signal of i2c */
38#define MMS114_I2C_DELAY 50
39
40/* 200ms needs after power on */
41#define MMS114_POWERON_DELAY 200
42
43/* Touchscreen absolute values */
44#define MMS114_MAX_AREA 0xff
45
46#define MMS114_MAX_TOUCH 10
47#define MMS114_PACKET_NUM 8
48
49/* Touch type */
50#define MMS114_TYPE_NONE 0
51#define MMS114_TYPE_TOUCHSCREEN 1
52#define MMS114_TYPE_TOUCHKEY 2
53
54enum mms_type {
55 TYPE_MMS114 = 114,
56 TYPE_MMS152 = 152,
57};
58
59struct mms114_data {
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;
65 enum mms_type type;
66 unsigned int contact_threshold;
67 unsigned int moving_threshold;
68
69 /* Use cache data for mode control register(write only) */
70 u8 cache_mode_control;
71};
72
73struct mms114_touch {
74 u8 id:4, reserved_bit4:1, type:2, pressed:1;
75 u8 x_hi:4, y_hi:4;
76 u8 x_lo;
77 u8 y_lo;
78 u8 width;
79 u8 strength;
80 u8 reserved[2];
81} __packed;
82
83static int __mms114_read_reg(struct mms114_data *data, unsigned int reg,
84 unsigned int len, u8 *val)
85{
86 struct i2c_client *client = data->client;
87 struct i2c_msg xfer[2];
88 u8 buf = reg & 0xff;
89 int error;
90
91 if (reg <= MMS114_MODE_CONTROL && reg + len > MMS114_MODE_CONTROL)
92 BUG();
93
94 /* Write register: use repeated start */
95 xfer[0].addr = client->addr;
96 xfer[0].flags = I2C_M_TEN | I2C_M_NOSTART;
97 xfer[0].len = 1;
98 xfer[0].buf = &buf;
99
100 /* Read data */
101 xfer[1].addr = client->addr;
102 xfer[1].flags = I2C_M_RD;
103 xfer[1].len = len;
104 xfer[1].buf = val;
105
106 error = i2c_transfer(client->adapter, xfer, 2);
107 if (error != 2) {
108 dev_err(&client->dev,
109 "%s: i2c transfer failed (%d)\n", __func__, error);
110 return error < 0 ? error : -EIO;
111 }
112 udelay(MMS114_I2C_DELAY);
113
114 return 0;
115}
116
117static int mms114_read_reg(struct mms114_data *data, unsigned int reg)
118{
119 u8 val;
120 int error;
121
122 if (reg == MMS114_MODE_CONTROL)
123 return data->cache_mode_control;
124
125 error = __mms114_read_reg(data, reg, 1, &val);
126 return error < 0 ? error : val;
127}
128
129static int mms114_write_reg(struct mms114_data *data, unsigned int reg,
130 unsigned int val)
131{
132 struct i2c_client *client = data->client;
133 u8 buf[2];
134 int error;
135
136 buf[0] = reg & 0xff;
137 buf[1] = val & 0xff;
138
139 error = i2c_master_send(client, buf, 2);
140 if (error != 2) {
141 dev_err(&client->dev,
142 "%s: i2c send failed (%d)\n", __func__, error);
143 return error < 0 ? error : -EIO;
144 }
145 udelay(MMS114_I2C_DELAY);
146
147 if (reg == MMS114_MODE_CONTROL)
148 data->cache_mode_control = val;
149
150 return 0;
151}
152
153static void mms114_process_mt(struct mms114_data *data, struct mms114_touch *touch)
154{
155 struct i2c_client *client = data->client;
156 struct input_dev *input_dev = data->input_dev;
157 unsigned int id;
158 unsigned int x;
159 unsigned int y;
160
161 if (touch->id > MMS114_MAX_TOUCH) {
162 dev_err(&client->dev, "Wrong touch id (%d)\n", touch->id);
163 return;
164 }
165
166 if (touch->type != MMS114_TYPE_TOUCHSCREEN) {
167 dev_err(&client->dev, "Wrong touch type (%d)\n", touch->type);
168 return;
169 }
170
171 id = touch->id - 1;
172 x = touch->x_lo | touch->x_hi << 8;
173 y = touch->y_lo | touch->y_hi << 8;
174
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);
179
180 input_mt_slot(input_dev, id);
181 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER, touch->pressed);
182
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);
187 }
188}
189
190static irqreturn_t mms114_interrupt(int irq, void *dev_id)
191{
192 struct mms114_data *data = dev_id;
193 struct input_dev *input_dev = data->input_dev;
194 struct mms114_touch touch[MMS114_MAX_TOUCH];
195 int packet_size;
196 int touch_size;
197 int index;
198 int error;
199
200 mutex_lock(&input_dev->mutex);
201 if (!input_dev->users) {
202 mutex_unlock(&input_dev->mutex);
203 goto out;
204 }
205 mutex_unlock(&input_dev->mutex);
206
207 packet_size = mms114_read_reg(data, MMS114_PACKET_SIZE);
208 if (packet_size <= 0)
209 goto out;
210
211 touch_size = packet_size / MMS114_PACKET_NUM;
212
213 error = __mms114_read_reg(data, MMS114_INFORMATION, packet_size,
214 (u8 *)touch);
215 if (error < 0)
216 goto out;
217
218 for (index = 0; index < touch_size; index++)
219 mms114_process_mt(data, touch + index);
220
221 input_mt_report_pointer_emulation(data->input_dev, true);
222 input_sync(data->input_dev);
223
224out:
225 return IRQ_HANDLED;
226}
227
228static int mms114_set_active(struct mms114_data *data, bool active)
229{
230 int val;
231
232 val = mms114_read_reg(data, MMS114_MODE_CONTROL);
233 if (val < 0)
234 return val;
235
236 val &= ~MMS114_OPERATION_MODE_MASK;
237
238 /* If active is false, sleep mode */
239 if (active)
240 val |= MMS114_ACTIVE;
241
242 return mms114_write_reg(data, MMS114_MODE_CONTROL, val);
243}
244
245static int mms114_get_version(struct mms114_data *data)
246{
247 struct device *dev = &data->client->dev;
248 u8 buf[6];
249 int group;
250 int error;
251
252 switch (data->type) {
253 case TYPE_MMS152:
254 error = __mms114_read_reg(data, MMS152_FW_REV, 3, buf);
255 if (error)
256 return error;
257
258 group = i2c_smbus_read_byte_data(data->client,
259 MMS152_COMPAT_GROUP);
260 if (group < 0)
261 return group;
262
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);
265 break;
266
267 case TYPE_MMS114:
268 error = __mms114_read_reg(data, MMS114_TSP_REV, 6, buf);
269 if (error)
270 return error;
271
272 dev_info(dev, "TSP Rev: 0x%x, HW Rev: 0x%x, Firmware Ver: 0x%x\n",
273 buf[0], buf[1], buf[3]);
274 break;
275 }
276
277 return 0;
278}
279
280static int mms114_setup_regs(struct mms114_data *data)
281{
282 const struct touchscreen_properties *props = &data->props;
283 int val;
284 int error;
285
286 error = mms114_get_version(data);
287 if (error < 0)
288 return error;
289
290 /* MMS152 has no configuration or power on registers */
291 if (data->type == TYPE_MMS152)
292 return 0;
293
294 error = mms114_set_active(data, true);
295 if (error < 0)
296 return error;
297
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);
301 if (error < 0)
302 return error;
303
304 val = props->max_x & 0xff;
305 error = mms114_write_reg(data, MMS114_X_RESOLUTION, val);
306 if (error < 0)
307 return error;
308
309 val = props->max_x & 0xff;
310 error = mms114_write_reg(data, MMS114_Y_RESOLUTION, val);
311 if (error < 0)
312 return error;
313
314 if (data->contact_threshold) {
315 error = mms114_write_reg(data, MMS114_CONTACT_THRESHOLD,
316 data->contact_threshold);
317 if (error < 0)
318 return error;
319 }
320
321 if (data->moving_threshold) {
322 error = mms114_write_reg(data, MMS114_MOVING_THRESHOLD,
323 data->moving_threshold);
324 if (error < 0)
325 return error;
326 }
327
328 return 0;
329}
330
331static int mms114_start(struct mms114_data *data)
332{
333 struct i2c_client *client = data->client;
334 int error;
335
336 error = regulator_enable(data->core_reg);
337 if (error) {
338 dev_err(&client->dev, "Failed to enable avdd: %d\n", error);
339 return error;
340 }
341
342 error = regulator_enable(data->io_reg);
343 if (error) {
344 dev_err(&client->dev, "Failed to enable vdd: %d\n", error);
345 regulator_disable(data->core_reg);
346 return error;
347 }
348
349 msleep(MMS114_POWERON_DELAY);
350
351 error = mms114_setup_regs(data);
352 if (error < 0) {
353 regulator_disable(data->io_reg);
354 regulator_disable(data->core_reg);
355 return error;
356 }
357
358 enable_irq(client->irq);
359
360 return 0;
361}
362
363static void mms114_stop(struct mms114_data *data)
364{
365 struct i2c_client *client = data->client;
366 int error;
367
368 disable_irq(client->irq);
369
370 error = regulator_disable(data->io_reg);
371 if (error)
372 dev_warn(&client->dev, "Failed to disable vdd: %d\n", error);
373
374 error = regulator_disable(data->core_reg);
375 if (error)
376 dev_warn(&client->dev, "Failed to disable avdd: %d\n", error);
377}
378
379static int mms114_input_open(struct input_dev *dev)
380{
381 struct mms114_data *data = input_get_drvdata(dev);
382
383 return mms114_start(data);
384}
385
386static void mms114_input_close(struct input_dev *dev)
387{
388 struct mms114_data *data = input_get_drvdata(dev);
389
390 mms114_stop(data);
391}
392
393static int mms114_parse_legacy_bindings(struct mms114_data *data)
394{
395 struct device *dev = &data->client->dev;
396 struct touchscreen_properties *props = &data->props;
397
398 if (device_property_read_u32(dev, "x-size", &props->max_x)) {
399 dev_dbg(dev, "failed to get legacy x-size property\n");
400 return -EINVAL;
401 }
402
403 if (device_property_read_u32(dev, "y-size", &props->max_y)) {
404 dev_dbg(dev, "failed to get legacy y-size property\n");
405 return -EINVAL;
406 }
407
408 device_property_read_u32(dev, "contact-threshold",
409 &data->contact_threshold);
410 device_property_read_u32(dev, "moving-threshold",
411 &data->moving_threshold);
412
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;
417
418 props->swap_x_y = false;
419
420 return 0;
421}
422
423static int mms114_probe(struct i2c_client *client,
424 const struct i2c_device_id *id)
425{
426 struct mms114_data *data;
427 struct input_dev *input_dev;
428 const void *match_data;
429 int error;
430
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");
435 return -ENODEV;
436 }
437
438 data = devm_kzalloc(&client->dev, sizeof(struct mms114_data),
439 GFP_KERNEL);
440 input_dev = devm_input_allocate_device(&client->dev);
441 if (!data || !input_dev) {
442 dev_err(&client->dev, "Failed to allocate memory\n");
443 return -ENOMEM;
444 }
445
446 data->client = client;
447 data->input_dev = input_dev;
448
449 /* FIXME: switch to device_get_match_data() when available */
450 match_data = of_device_get_match_data(&client->dev);
451 if (!match_data)
452 return -EINVAL;
453
454 data->type = (enum mms_type)match_data;
455
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);
461
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);
467 if (error)
468 return error;
469
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);
474 }
475
476 if (data->type == TYPE_MMS114) {
477 /*
478 * The firmware handles movement and pressure fuzz, so
479 * don't duplicate that in software.
480 */
481 data->moving_threshold = input_abs_get_fuzz(input_dev,
482 ABS_MT_POSITION_X);
483 data->contact_threshold = input_abs_get_fuzz(input_dev,
484 ABS_MT_PRESSURE);
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);
488 }
489
490 input_dev->name = devm_kasprintf(&client->dev, GFP_KERNEL,
491 "MELFAS MMS%d Touchscreen",
492 data->type);
493 if (!input_dev->name)
494 return -ENOMEM;
495
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;
500
501 error = input_mt_init_slots(input_dev, MMS114_MAX_TOUCH,
502 INPUT_MT_DIRECT);
503 if (error)
504 return error;
505
506 input_set_drvdata(input_dev, data);
507 i2c_set_clientdata(client, data);
508
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);
514 return error;
515 }
516
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);
522 return error;
523 }
524
525 error = devm_request_threaded_irq(&client->dev, client->irq,
526 NULL, mms114_interrupt, IRQF_ONESHOT,
527 dev_name(&client->dev), data);
528 if (error) {
529 dev_err(&client->dev, "Failed to register interrupt\n");
530 return error;
531 }
532 disable_irq(client->irq);
533
534 error = input_register_device(data->input_dev);
535 if (error) {
536 dev_err(&client->dev, "Failed to register input device\n");
537 return error;
538 }
539
540 return 0;
541}
542
543static int __maybe_unused mms114_suspend(struct device *dev)
544{
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;
548 int id;
549
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);
554 }
555
556 input_mt_report_pointer_emulation(input_dev, true);
557 input_sync(input_dev);
558
559 mutex_lock(&input_dev->mutex);
560 if (input_dev->users)
561 mms114_stop(data);
562 mutex_unlock(&input_dev->mutex);
563
564 return 0;
565}
566
567static int __maybe_unused mms114_resume(struct device *dev)
568{
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;
572 int error;
573
574 mutex_lock(&input_dev->mutex);
575 if (input_dev->users) {
576 error = mms114_start(data);
577 if (error < 0) {
578 mutex_unlock(&input_dev->mutex);
579 return error;
580 }
581 }
582 mutex_unlock(&input_dev->mutex);
583
584 return 0;
585}
586
587static SIMPLE_DEV_PM_OPS(mms114_pm_ops, mms114_suspend, mms114_resume);
588
589static const struct i2c_device_id mms114_id[] = {
590 { "mms114", 0 },
591 { }
592};
593MODULE_DEVICE_TABLE(i2c, mms114_id);
594
595#ifdef CONFIG_OF
596static const struct of_device_id mms114_dt_match[] = {
597 {
598 .compatible = "melfas,mms114",
599 .data = (void *)TYPE_MMS114,
600 }, {
601 .compatible = "melfas,mms152",
602 .data = (void *)TYPE_MMS152,
603 },
604 { }
605};
606MODULE_DEVICE_TABLE(of, mms114_dt_match);
607#endif
608
609static struct i2c_driver mms114_driver = {
610 .driver = {
611 .name = "mms114",
612 .pm = &mms114_pm_ops,
613 .of_match_table = of_match_ptr(mms114_dt_match),
614 },
615 .probe = mms114_probe,
616 .id_table = mms114_id,
617};
618
619module_i2c_driver(mms114_driver);
620
621/* Module information */
622MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
623MODULE_DESCRIPTION("MELFAS mms114 Touchscreen driver");
624MODULE_LICENSE("GPL v2");
625