treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / input / touchscreen / mms114.c
blob69c6d559eeb0056aef5afc4df6473760b6036428
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>
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>
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
49 /* Touch type */
50 #define MMS114_TYPE_NONE 0
51 #define MMS114_TYPE_TOUCHSCREEN 1
52 #define MMS114_TYPE_TOUCHKEY 2
54 enum mms_type {
55 TYPE_MMS114 = 114,
56 TYPE_MMS152 = 152,
59 struct 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;
69 /* Use cache data for mode control register(write only) */
70 u8 cache_mode_control;
73 struct 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;
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];
88 u8 buf = reg & 0xff;
89 int error;
91 if (reg <= MMS114_MODE_CONTROL && reg + len > MMS114_MODE_CONTROL)
92 BUG();
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;
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;
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;
112 udelay(MMS114_I2C_DELAY);
114 return 0;
117 static int mms114_read_reg(struct mms114_data *data, unsigned int reg)
119 u8 val;
120 int error;
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,
130 unsigned int val)
132 struct i2c_client *client = data->client;
133 u8 buf[2];
134 int error;
136 buf[0] = reg & 0xff;
137 buf[1] = val & 0xff;
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;
145 udelay(MMS114_I2C_DELAY);
147 if (reg == MMS114_MODE_CONTROL)
148 data->cache_mode_control = val;
150 return 0;
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;
157 unsigned int id;
158 unsigned int x;
159 unsigned int y;
161 if (touch->id > MMS114_MAX_TOUCH) {
162 dev_err(&client->dev, "Wrong touch id (%d)\n", touch->id);
163 return;
166 if (touch->type != MMS114_TYPE_TOUCHSCREEN) {
167 dev_err(&client->dev, "Wrong touch type (%d)\n", touch->type);
168 return;
171 id = touch->id - 1;
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];
195 int packet_size;
196 int touch_size;
197 int index;
198 int error;
200 mutex_lock(&input_dev->mutex);
201 if (!input_dev->users) {
202 mutex_unlock(&input_dev->mutex);
203 goto out;
205 mutex_unlock(&input_dev->mutex);
207 packet_size = mms114_read_reg(data, MMS114_PACKET_SIZE);
208 if (packet_size <= 0)
209 goto out;
211 touch_size = packet_size / MMS114_PACKET_NUM;
213 error = __mms114_read_reg(data, MMS114_INFORMATION, packet_size,
214 (u8 *)touch);
215 if (error < 0)
216 goto out;
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);
224 out:
225 return IRQ_HANDLED;
228 static int mms114_set_active(struct mms114_data *data, bool active)
230 int val;
232 val = mms114_read_reg(data, MMS114_MODE_CONTROL);
233 if (val < 0)
234 return val;
236 val &= ~MMS114_OPERATION_MODE_MASK;
238 /* If active is false, sleep mode */
239 if (active)
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;
248 u8 buf[6];
249 int group;
250 int error;
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;
258 group = i2c_smbus_read_byte_data(data->client,
259 MMS152_COMPAT_GROUP);
260 if (group < 0)
261 return 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);
265 break;
267 case TYPE_MMS114:
268 error = __mms114_read_reg(data, MMS114_TSP_REV, 6, buf);
269 if (error)
270 return error;
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;
277 return 0;
280 static int mms114_setup_regs(struct mms114_data *data)
282 const struct touchscreen_properties *props = &data->props;
283 int val;
284 int error;
286 error = mms114_get_version(data);
287 if (error < 0)
288 return error;
290 /* MMS152 has no configuration or power on registers */
291 if (data->type == TYPE_MMS152)
292 return 0;
294 error = mms114_set_active(data, true);
295 if (error < 0)
296 return error;
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;
304 val = props->max_x & 0xff;
305 error = mms114_write_reg(data, MMS114_X_RESOLUTION, val);
306 if (error < 0)
307 return error;
309 val = props->max_x & 0xff;
310 error = mms114_write_reg(data, MMS114_Y_RESOLUTION, val);
311 if (error < 0)
312 return error;
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;
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;
328 return 0;
331 static int mms114_start(struct mms114_data *data)
333 struct i2c_client *client = data->client;
334 int error;
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;
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;
349 msleep(MMS114_POWERON_DELAY);
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;
358 enable_irq(client->irq);
360 return 0;
363 static void mms114_stop(struct mms114_data *data)
365 struct i2c_client *client = data->client;
366 int error;
368 disable_irq(client->irq);
370 error = regulator_disable(data->io_reg);
371 if (error)
372 dev_warn(&client->dev, "Failed to disable vdd: %d\n", error);
374 error = regulator_disable(data->core_reg);
375 if (error)
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);
390 mms114_stop(data);
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");
400 return -EINVAL;
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;
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;
420 return 0;
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;
429 int error;
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;
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;
446 data->client = client;
447 data->input_dev = input_dev;
449 match_data = device_get_match_data(&client->dev);
450 if (!match_data)
451 return -EINVAL;
453 data->type = (enum mms_type)match_data;
455 input_set_capability(input_dev, EV_ABS, ABS_MT_POSITION_X);
456 input_set_capability(input_dev, EV_ABS, ABS_MT_POSITION_Y);
457 input_set_abs_params(input_dev, ABS_MT_PRESSURE, 0, 255, 0, 0);
458 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR,
459 0, MMS114_MAX_AREA, 0, 0);
461 touchscreen_parse_properties(input_dev, true, &data->props);
462 if (!data->props.max_x || !data->props.max_y) {
463 dev_dbg(&client->dev,
464 "missing X/Y size properties, trying legacy bindings\n");
465 error = mms114_parse_legacy_bindings(data);
466 if (error)
467 return error;
469 input_set_abs_params(input_dev, ABS_MT_POSITION_X,
470 0, data->props.max_x, 0, 0);
471 input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
472 0, data->props.max_y, 0, 0);
475 if (data->type == TYPE_MMS114) {
477 * The firmware handles movement and pressure fuzz, so
478 * don't duplicate that in software.
480 data->moving_threshold = input_abs_get_fuzz(input_dev,
481 ABS_MT_POSITION_X);
482 data->contact_threshold = input_abs_get_fuzz(input_dev,
483 ABS_MT_PRESSURE);
484 input_abs_set_fuzz(input_dev, ABS_MT_POSITION_X, 0);
485 input_abs_set_fuzz(input_dev, ABS_MT_POSITION_Y, 0);
486 input_abs_set_fuzz(input_dev, ABS_MT_PRESSURE, 0);
489 input_dev->name = devm_kasprintf(&client->dev, GFP_KERNEL,
490 "MELFAS MMS%d Touchscreen",
491 data->type);
492 if (!input_dev->name)
493 return -ENOMEM;
495 input_dev->id.bustype = BUS_I2C;
496 input_dev->dev.parent = &client->dev;
497 input_dev->open = mms114_input_open;
498 input_dev->close = mms114_input_close;
500 error = input_mt_init_slots(input_dev, MMS114_MAX_TOUCH,
501 INPUT_MT_DIRECT);
502 if (error)
503 return error;
505 input_set_drvdata(input_dev, data);
506 i2c_set_clientdata(client, data);
508 data->core_reg = devm_regulator_get(&client->dev, "avdd");
509 if (IS_ERR(data->core_reg)) {
510 error = PTR_ERR(data->core_reg);
511 dev_err(&client->dev,
512 "Unable to get the Core regulator (%d)\n", error);
513 return error;
516 data->io_reg = devm_regulator_get(&client->dev, "vdd");
517 if (IS_ERR(data->io_reg)) {
518 error = PTR_ERR(data->io_reg);
519 dev_err(&client->dev,
520 "Unable to get the IO regulator (%d)\n", error);
521 return error;
524 error = devm_request_threaded_irq(&client->dev, client->irq,
525 NULL, mms114_interrupt, IRQF_ONESHOT,
526 dev_name(&client->dev), data);
527 if (error) {
528 dev_err(&client->dev, "Failed to register interrupt\n");
529 return error;
531 disable_irq(client->irq);
533 error = input_register_device(data->input_dev);
534 if (error) {
535 dev_err(&client->dev, "Failed to register input device\n");
536 return error;
539 return 0;
542 static int __maybe_unused mms114_suspend(struct device *dev)
544 struct i2c_client *client = to_i2c_client(dev);
545 struct mms114_data *data = i2c_get_clientdata(client);
546 struct input_dev *input_dev = data->input_dev;
547 int id;
549 /* Release all touch */
550 for (id = 0; id < MMS114_MAX_TOUCH; id++) {
551 input_mt_slot(input_dev, id);
552 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER, false);
555 input_mt_report_pointer_emulation(input_dev, true);
556 input_sync(input_dev);
558 mutex_lock(&input_dev->mutex);
559 if (input_dev->users)
560 mms114_stop(data);
561 mutex_unlock(&input_dev->mutex);
563 return 0;
566 static int __maybe_unused mms114_resume(struct device *dev)
568 struct i2c_client *client = to_i2c_client(dev);
569 struct mms114_data *data = i2c_get_clientdata(client);
570 struct input_dev *input_dev = data->input_dev;
571 int error;
573 mutex_lock(&input_dev->mutex);
574 if (input_dev->users) {
575 error = mms114_start(data);
576 if (error < 0) {
577 mutex_unlock(&input_dev->mutex);
578 return error;
581 mutex_unlock(&input_dev->mutex);
583 return 0;
586 static SIMPLE_DEV_PM_OPS(mms114_pm_ops, mms114_suspend, mms114_resume);
588 static const struct i2c_device_id mms114_id[] = {
589 { "mms114", 0 },
592 MODULE_DEVICE_TABLE(i2c, mms114_id);
594 #ifdef CONFIG_OF
595 static const struct of_device_id mms114_dt_match[] = {
597 .compatible = "melfas,mms114",
598 .data = (void *)TYPE_MMS114,
599 }, {
600 .compatible = "melfas,mms152",
601 .data = (void *)TYPE_MMS152,
605 MODULE_DEVICE_TABLE(of, mms114_dt_match);
606 #endif
608 static struct i2c_driver mms114_driver = {
609 .driver = {
610 .name = "mms114",
611 .pm = &mms114_pm_ops,
612 .of_match_table = of_match_ptr(mms114_dt_match),
614 .probe = mms114_probe,
615 .id_table = mms114_id,
618 module_i2c_driver(mms114_driver);
620 /* Module information */
621 MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
622 MODULE_DESCRIPTION("MELFAS mms114 Touchscreen driver");
623 MODULE_LICENSE("GPL v2");