sched: Remove double_rq_lock() from __migrate_task()
[linux/fpc-iii.git] / drivers / input / touchscreen / mms114.c
blob372bbf7658fec2f85a8fb48cb03c24b2c0c78bee
1 /*
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.
8 */
10 #include <linux/module.h>
11 #include <linux/delay.h>
12 #include <linux/of.h>
13 #include <linux/i2c.h>
14 #include <linux/i2c/mms114.h>
15 #include <linux/input/mt.h>
16 #include <linux/interrupt.h>
17 #include <linux/regulator/consumer.h>
18 #include <linux/slab.h>
20 /* Write only registers */
21 #define MMS114_MODE_CONTROL 0x01
22 #define MMS114_OPERATION_MODE_MASK 0xE
23 #define MMS114_ACTIVE (1 << 1)
25 #define MMS114_XY_RESOLUTION_H 0x02
26 #define MMS114_X_RESOLUTION 0x03
27 #define MMS114_Y_RESOLUTION 0x04
28 #define MMS114_CONTACT_THRESHOLD 0x05
29 #define MMS114_MOVING_THRESHOLD 0x06
31 /* Read only registers */
32 #define MMS114_PACKET_SIZE 0x0F
33 #define MMS114_INFOMATION 0x10
34 #define MMS114_TSP_REV 0xF0
36 /* Minimum delay time is 50us between stop and start signal of i2c */
37 #define MMS114_I2C_DELAY 50
39 /* 200ms needs after power on */
40 #define MMS114_POWERON_DELAY 200
42 /* Touchscreen absolute values */
43 #define MMS114_MAX_AREA 0xff
45 #define MMS114_MAX_TOUCH 10
46 #define MMS114_PACKET_NUM 8
48 /* Touch type */
49 #define MMS114_TYPE_NONE 0
50 #define MMS114_TYPE_TOUCHSCREEN 1
51 #define MMS114_TYPE_TOUCHKEY 2
53 struct mms114_data {
54 struct i2c_client *client;
55 struct input_dev *input_dev;
56 struct regulator *core_reg;
57 struct regulator *io_reg;
58 const struct mms114_platform_data *pdata;
60 /* Use cache data for mode control register(write only) */
61 u8 cache_mode_control;
64 struct mms114_touch {
65 u8 id:4, reserved_bit4:1, type:2, pressed:1;
66 u8 x_hi:4, y_hi:4;
67 u8 x_lo;
68 u8 y_lo;
69 u8 width;
70 u8 strength;
71 u8 reserved[2];
72 } __packed;
74 static int __mms114_read_reg(struct mms114_data *data, unsigned int reg,
75 unsigned int len, u8 *val)
77 struct i2c_client *client = data->client;
78 struct i2c_msg xfer[2];
79 u8 buf = reg & 0xff;
80 int error;
82 if (reg <= MMS114_MODE_CONTROL && reg + len > MMS114_MODE_CONTROL)
83 BUG();
85 /* Write register: use repeated start */
86 xfer[0].addr = client->addr;
87 xfer[0].flags = I2C_M_TEN | I2C_M_NOSTART;
88 xfer[0].len = 1;
89 xfer[0].buf = &buf;
91 /* Read data */
92 xfer[1].addr = client->addr;
93 xfer[1].flags = I2C_M_RD;
94 xfer[1].len = len;
95 xfer[1].buf = val;
97 error = i2c_transfer(client->adapter, xfer, 2);
98 if (error != 2) {
99 dev_err(&client->dev,
100 "%s: i2c transfer failed (%d)\n", __func__, error);
101 return error < 0 ? error : -EIO;
103 udelay(MMS114_I2C_DELAY);
105 return 0;
108 static int mms114_read_reg(struct mms114_data *data, unsigned int reg)
110 u8 val;
111 int error;
113 if (reg == MMS114_MODE_CONTROL)
114 return data->cache_mode_control;
116 error = __mms114_read_reg(data, reg, 1, &val);
117 return error < 0 ? error : val;
120 static int mms114_write_reg(struct mms114_data *data, unsigned int reg,
121 unsigned int val)
123 struct i2c_client *client = data->client;
124 u8 buf[2];
125 int error;
127 buf[0] = reg & 0xff;
128 buf[1] = val & 0xff;
130 error = i2c_master_send(client, buf, 2);
131 if (error != 2) {
132 dev_err(&client->dev,
133 "%s: i2c send failed (%d)\n", __func__, error);
134 return error < 0 ? error : -EIO;
136 udelay(MMS114_I2C_DELAY);
138 if (reg == MMS114_MODE_CONTROL)
139 data->cache_mode_control = val;
141 return 0;
144 static void mms114_process_mt(struct mms114_data *data, struct mms114_touch *touch)
146 const struct mms114_platform_data *pdata = data->pdata;
147 struct i2c_client *client = data->client;
148 struct input_dev *input_dev = data->input_dev;
149 unsigned int id;
150 unsigned int x;
151 unsigned int y;
153 if (touch->id > MMS114_MAX_TOUCH) {
154 dev_err(&client->dev, "Wrong touch id (%d)\n", touch->id);
155 return;
158 if (touch->type != MMS114_TYPE_TOUCHSCREEN) {
159 dev_err(&client->dev, "Wrong touch type (%d)\n", touch->type);
160 return;
163 id = touch->id - 1;
164 x = touch->x_lo | touch->x_hi << 8;
165 y = touch->y_lo | touch->y_hi << 8;
166 if (x > pdata->x_size || y > pdata->y_size) {
167 dev_dbg(&client->dev,
168 "Wrong touch coordinates (%d, %d)\n", x, y);
169 return;
172 if (pdata->x_invert)
173 x = pdata->x_size - x;
174 if (pdata->y_invert)
175 y = pdata->y_size - y;
177 dev_dbg(&client->dev,
178 "id: %d, type: %d, pressed: %d, x: %d, y: %d, width: %d, strength: %d\n",
179 id, touch->type, touch->pressed,
180 x, y, touch->width, touch->strength);
182 input_mt_slot(input_dev, id);
183 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER, touch->pressed);
185 if (touch->pressed) {
186 input_report_abs(input_dev, ABS_MT_TOUCH_MAJOR, touch->width);
187 input_report_abs(input_dev, ABS_MT_POSITION_X, x);
188 input_report_abs(input_dev, ABS_MT_POSITION_Y, y);
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];
198 int packet_size;
199 int touch_size;
200 int index;
201 int error;
203 mutex_lock(&input_dev->mutex);
204 if (!input_dev->users) {
205 mutex_unlock(&input_dev->mutex);
206 goto out;
208 mutex_unlock(&input_dev->mutex);
210 packet_size = mms114_read_reg(data, MMS114_PACKET_SIZE);
211 if (packet_size <= 0)
212 goto out;
214 touch_size = packet_size / MMS114_PACKET_NUM;
216 error = __mms114_read_reg(data, MMS114_INFOMATION, packet_size,
217 (u8 *)touch);
218 if (error < 0)
219 goto out;
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);
227 out:
228 return IRQ_HANDLED;
231 static int mms114_set_active(struct mms114_data *data, bool active)
233 int val;
235 val = mms114_read_reg(data, MMS114_MODE_CONTROL);
236 if (val < 0)
237 return val;
239 val &= ~MMS114_OPERATION_MODE_MASK;
241 /* If active is false, sleep mode */
242 if (active)
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;
251 u8 buf[6];
252 int error;
254 error = __mms114_read_reg(data, MMS114_TSP_REV, 6, buf);
255 if (error < 0)
256 return error;
258 dev_info(dev, "TSP Rev: 0x%x, HW Rev: 0x%x, Firmware Ver: 0x%x\n",
259 buf[0], buf[1], buf[3]);
261 return 0;
264 static int mms114_setup_regs(struct mms114_data *data)
266 const struct mms114_platform_data *pdata = data->pdata;
267 int val;
268 int error;
270 error = mms114_get_version(data);
271 if (error < 0)
272 return error;
274 error = mms114_set_active(data, true);
275 if (error < 0)
276 return error;
278 val = (pdata->x_size >> 8) & 0xf;
279 val |= ((pdata->y_size >> 8) & 0xf) << 4;
280 error = mms114_write_reg(data, MMS114_XY_RESOLUTION_H, val);
281 if (error < 0)
282 return error;
284 val = pdata->x_size & 0xff;
285 error = mms114_write_reg(data, MMS114_X_RESOLUTION, val);
286 if (error < 0)
287 return error;
289 val = pdata->y_size & 0xff;
290 error = mms114_write_reg(data, MMS114_Y_RESOLUTION, val);
291 if (error < 0)
292 return error;
294 if (pdata->contact_threshold) {
295 error = mms114_write_reg(data, MMS114_CONTACT_THRESHOLD,
296 pdata->contact_threshold);
297 if (error < 0)
298 return error;
301 if (pdata->moving_threshold) {
302 error = mms114_write_reg(data, MMS114_MOVING_THRESHOLD,
303 pdata->moving_threshold);
304 if (error < 0)
305 return error;
308 return 0;
311 static int mms114_start(struct mms114_data *data)
313 struct i2c_client *client = data->client;
314 int error;
316 error = regulator_enable(data->core_reg);
317 if (error) {
318 dev_err(&client->dev, "Failed to enable avdd: %d\n", error);
319 return error;
322 error = regulator_enable(data->io_reg);
323 if (error) {
324 dev_err(&client->dev, "Failed to enable vdd: %d\n", error);
325 regulator_disable(data->core_reg);
326 return error;
329 mdelay(MMS114_POWERON_DELAY);
331 error = mms114_setup_regs(data);
332 if (error < 0) {
333 regulator_disable(data->io_reg);
334 regulator_disable(data->core_reg);
335 return error;
338 if (data->pdata->cfg_pin)
339 data->pdata->cfg_pin(true);
341 enable_irq(client->irq);
343 return 0;
346 static void mms114_stop(struct mms114_data *data)
348 struct i2c_client *client = data->client;
349 int error;
351 disable_irq(client->irq);
353 if (data->pdata->cfg_pin)
354 data->pdata->cfg_pin(false);
356 error = regulator_disable(data->io_reg);
357 if (error)
358 dev_warn(&client->dev, "Failed to disable vdd: %d\n", error);
360 error = regulator_disable(data->core_reg);
361 if (error)
362 dev_warn(&client->dev, "Failed to disable avdd: %d\n", error);
365 static int mms114_input_open(struct input_dev *dev)
367 struct mms114_data *data = input_get_drvdata(dev);
369 return mms114_start(data);
372 static void mms114_input_close(struct input_dev *dev)
374 struct mms114_data *data = input_get_drvdata(dev);
376 mms114_stop(data);
379 #ifdef CONFIG_OF
380 static struct mms114_platform_data *mms114_parse_dt(struct device *dev)
382 struct mms114_platform_data *pdata;
383 struct device_node *np = dev->of_node;
385 if (!np)
386 return NULL;
388 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
389 if (!pdata) {
390 dev_err(dev, "failed to allocate platform data\n");
391 return NULL;
394 if (of_property_read_u32(np, "x-size", &pdata->x_size)) {
395 dev_err(dev, "failed to get x-size property\n");
396 return NULL;
399 if (of_property_read_u32(np, "y-size", &pdata->y_size)) {
400 dev_err(dev, "failed to get y-size property\n");
401 return NULL;
404 of_property_read_u32(np, "contact-threshold",
405 &pdata->contact_threshold);
406 of_property_read_u32(np, "moving-threshold",
407 &pdata->moving_threshold);
409 if (of_find_property(np, "x-invert", NULL))
410 pdata->x_invert = true;
411 if (of_find_property(np, "y-invert", NULL))
412 pdata->y_invert = true;
414 return pdata;
416 #else
417 static inline struct mms114_platform_data *mms114_parse_dt(struct device *dev)
419 return NULL;
421 #endif
423 static int mms114_probe(struct i2c_client *client,
424 const struct i2c_device_id *id)
426 const struct mms114_platform_data *pdata;
427 struct mms114_data *data;
428 struct input_dev *input_dev;
429 int error;
431 pdata = dev_get_platdata(&client->dev);
432 if (!pdata)
433 pdata = mms114_parse_dt(&client->dev);
435 if (!pdata) {
436 dev_err(&client->dev, "Need platform data\n");
437 return -EINVAL;
440 if (!i2c_check_functionality(client->adapter,
441 I2C_FUNC_PROTOCOL_MANGLING)) {
442 dev_err(&client->dev,
443 "Need i2c bus that supports protocol mangling\n");
444 return -ENODEV;
447 data = devm_kzalloc(&client->dev, sizeof(struct mms114_data),
448 GFP_KERNEL);
449 input_dev = devm_input_allocate_device(&client->dev);
450 if (!data || !input_dev) {
451 dev_err(&client->dev, "Failed to allocate memory\n");
452 return -ENOMEM;
455 data->client = client;
456 data->input_dev = input_dev;
457 data->pdata = pdata;
459 input_dev->name = "MELFAS MMS114 Touchscreen";
460 input_dev->id.bustype = BUS_I2C;
461 input_dev->dev.parent = &client->dev;
462 input_dev->open = mms114_input_open;
463 input_dev->close = mms114_input_close;
465 __set_bit(EV_ABS, input_dev->evbit);
466 __set_bit(EV_KEY, input_dev->evbit);
467 __set_bit(BTN_TOUCH, input_dev->keybit);
468 input_set_abs_params(input_dev, ABS_X, 0, data->pdata->x_size, 0, 0);
469 input_set_abs_params(input_dev, ABS_Y, 0, data->pdata->y_size, 0, 0);
471 /* For multi touch */
472 input_mt_init_slots(input_dev, MMS114_MAX_TOUCH, 0);
473 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR,
474 0, MMS114_MAX_AREA, 0, 0);
475 input_set_abs_params(input_dev, ABS_MT_POSITION_X,
476 0, data->pdata->x_size, 0, 0);
477 input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
478 0, data->pdata->y_size, 0, 0);
479 input_set_abs_params(input_dev, ABS_MT_PRESSURE, 0, 255, 0, 0);
481 input_set_drvdata(input_dev, data);
482 i2c_set_clientdata(client, data);
484 data->core_reg = devm_regulator_get(&client->dev, "avdd");
485 if (IS_ERR(data->core_reg)) {
486 error = PTR_ERR(data->core_reg);
487 dev_err(&client->dev,
488 "Unable to get the Core regulator (%d)\n", error);
489 return error;
492 data->io_reg = devm_regulator_get(&client->dev, "vdd");
493 if (IS_ERR(data->io_reg)) {
494 error = PTR_ERR(data->io_reg);
495 dev_err(&client->dev,
496 "Unable to get the IO regulator (%d)\n", error);
497 return error;
500 error = devm_request_threaded_irq(&client->dev, client->irq, NULL,
501 mms114_interrupt, IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
502 dev_name(&client->dev), data);
503 if (error) {
504 dev_err(&client->dev, "Failed to register interrupt\n");
505 return error;
507 disable_irq(client->irq);
509 error = input_register_device(data->input_dev);
510 if (error) {
511 dev_err(&client->dev, "Failed to register input device\n");
512 return error;
515 return 0;
518 #ifdef CONFIG_PM_SLEEP
519 static int mms114_suspend(struct device *dev)
521 struct i2c_client *client = to_i2c_client(dev);
522 struct mms114_data *data = i2c_get_clientdata(client);
523 struct input_dev *input_dev = data->input_dev;
524 int id;
526 /* Release all touch */
527 for (id = 0; id < MMS114_MAX_TOUCH; id++) {
528 input_mt_slot(input_dev, id);
529 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER, false);
532 input_mt_report_pointer_emulation(input_dev, true);
533 input_sync(input_dev);
535 mutex_lock(&input_dev->mutex);
536 if (input_dev->users)
537 mms114_stop(data);
538 mutex_unlock(&input_dev->mutex);
540 return 0;
543 static int mms114_resume(struct device *dev)
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 error;
550 mutex_lock(&input_dev->mutex);
551 if (input_dev->users) {
552 error = mms114_start(data);
553 if (error < 0) {
554 mutex_unlock(&input_dev->mutex);
555 return error;
558 mutex_unlock(&input_dev->mutex);
560 return 0;
562 #endif
564 static SIMPLE_DEV_PM_OPS(mms114_pm_ops, mms114_suspend, mms114_resume);
566 static const struct i2c_device_id mms114_id[] = {
567 { "mms114", 0 },
570 MODULE_DEVICE_TABLE(i2c, mms114_id);
572 #ifdef CONFIG_OF
573 static const struct of_device_id mms114_dt_match[] = {
574 { .compatible = "melfas,mms114" },
577 #endif
579 static struct i2c_driver mms114_driver = {
580 .driver = {
581 .name = "mms114",
582 .owner = THIS_MODULE,
583 .pm = &mms114_pm_ops,
584 .of_match_table = of_match_ptr(mms114_dt_match),
586 .probe = mms114_probe,
587 .id_table = mms114_id,
590 module_i2c_driver(mms114_driver);
592 /* Module information */
593 MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
594 MODULE_DESCRIPTION("MELFAS mms114 Touchscreen driver");
595 MODULE_LICENSE("GPL");