2 * Copyright (C) 2012-2013 MundoReader S.L.
3 * Author: Heiko Stuebner <heiko@sntech.de>
5 * based in parts on Nook zforce driver
7 * Copyright (C) 2010 Barnes & Noble, Inc.
8 * Author: Pieter Truter<ptruter@intrinsyc.com>
10 * This software is licensed under the terms of the GNU General Public
11 * License version 2, as published by the Free Software Foundation, and
12 * may be copied, distributed, and modified under those terms.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
20 #include <linux/module.h>
21 #include <linux/hrtimer.h>
22 #include <linux/slab.h>
23 #include <linux/input.h>
24 #include <linux/interrupt.h>
25 #include <linux/i2c.h>
26 #include <linux/delay.h>
27 #include <linux/gpio.h>
28 #include <linux/device.h>
29 #include <linux/sysfs.h>
30 #include <linux/input/mt.h>
31 #include <linux/platform_data/zforce_ts.h>
33 #define WAIT_TIMEOUT msecs_to_jiffies(1000)
35 #define FRAME_START 0xee
36 #define FRAME_MAXSIZE 257
38 /* Offsets of the different parts of the payload the controller sends */
39 #define PAYLOAD_HEADER 0
40 #define PAYLOAD_LENGTH 1
41 #define PAYLOAD_BODY 2
43 /* Response offsets */
45 #define RESPONSE_DATA 1
48 #define COMMAND_DEACTIVATE 0x00
49 #define COMMAND_INITIALIZE 0x01
50 #define COMMAND_RESOLUTION 0x02
51 #define COMMAND_SETCONFIG 0x03
52 #define COMMAND_DATAREQUEST 0x04
53 #define COMMAND_SCANFREQ 0x08
54 #define COMMAND_STATUS 0X1e
57 * Responses the controller sends as a result of
60 #define RESPONSE_DEACTIVATE 0x00
61 #define RESPONSE_INITIALIZE 0x01
62 #define RESPONSE_RESOLUTION 0x02
63 #define RESPONSE_SETCONFIG 0x03
64 #define RESPONSE_SCANFREQ 0x08
65 #define RESPONSE_STATUS 0X1e
68 * Notifications are sent by the touch controller without
69 * being requested by the driver and include for example
72 #define NOTIFICATION_TOUCH 0x04
73 #define NOTIFICATION_BOOTCOMPLETE 0x07
74 #define NOTIFICATION_OVERRUN 0x25
75 #define NOTIFICATION_PROXIMITY 0x26
76 #define NOTIFICATION_INVALID_COMMAND 0xfe
78 #define ZFORCE_REPORT_POINTS 2
79 #define ZFORCE_MAX_AREA 0xff
85 #define SETCONFIG_DUALTOUCH (1 << 0)
100 * @client the i2c_client
101 * @input the input device
102 * @suspending in the process of going to suspend (don't emit wakeup
103 * events for commands executed to suspend the device)
104 * @suspended device suspended
105 * @access_mutex serialize i2c-access, to keep multipart reads together
106 * @command_done completion to wait for the command result
107 * @command_mutex serialize commands sent to the ic
108 * @command_waiting the id of the command that is currently waiting
110 * @command_result returned result of the command
113 struct i2c_client *client;
114 struct input_dev *input;
115 const struct zforce_ts_platdata *pdata;
122 /* Firmware version information */
128 struct mutex access_mutex;
130 struct completion command_done;
131 struct mutex command_mutex;
136 static int zforce_command(struct zforce_ts *ts, u8 cmd)
138 struct i2c_client *client = ts->client;
142 dev_dbg(&client->dev, "%s: 0x%x\n", __func__, cmd);
144 buf[0] = FRAME_START;
145 buf[1] = 1; /* data size, command only */
148 mutex_lock(&ts->access_mutex);
149 ret = i2c_master_send(client, &buf[0], ARRAY_SIZE(buf));
150 mutex_unlock(&ts->access_mutex);
152 dev_err(&client->dev, "i2c send data request error: %d\n", ret);
159 static int zforce_send_wait(struct zforce_ts *ts, const char *buf, int len)
161 struct i2c_client *client = ts->client;
164 ret = mutex_trylock(&ts->command_mutex);
166 dev_err(&client->dev, "already waiting for a command\n");
170 dev_dbg(&client->dev, "sending %d bytes for command 0x%x\n",
173 ts->command_waiting = buf[2];
175 mutex_lock(&ts->access_mutex);
176 ret = i2c_master_send(client, buf, len);
177 mutex_unlock(&ts->access_mutex);
179 dev_err(&client->dev, "i2c send data request error: %d\n", ret);
183 dev_dbg(&client->dev, "waiting for result for command 0x%x\n", buf[2]);
185 if (wait_for_completion_timeout(&ts->command_done, WAIT_TIMEOUT) == 0) {
190 ret = ts->command_result;
193 mutex_unlock(&ts->command_mutex);
197 static int zforce_command_wait(struct zforce_ts *ts, u8 cmd)
199 struct i2c_client *client = ts->client;
203 dev_dbg(&client->dev, "%s: 0x%x\n", __func__, cmd);
205 buf[0] = FRAME_START;
206 buf[1] = 1; /* data size, command only */
209 ret = zforce_send_wait(ts, &buf[0], ARRAY_SIZE(buf));
211 dev_err(&client->dev, "i2c send data request error: %d\n", ret);
218 static int zforce_resolution(struct zforce_ts *ts, u16 x, u16 y)
220 struct i2c_client *client = ts->client;
221 char buf[7] = { FRAME_START, 5, COMMAND_RESOLUTION,
222 (x & 0xff), ((x >> 8) & 0xff),
223 (y & 0xff), ((y >> 8) & 0xff) };
225 dev_dbg(&client->dev, "set resolution to (%d,%d)\n", x, y);
227 return zforce_send_wait(ts, &buf[0], ARRAY_SIZE(buf));
230 static int zforce_scan_frequency(struct zforce_ts *ts, u16 idle, u16 finger,
233 struct i2c_client *client = ts->client;
234 char buf[9] = { FRAME_START, 7, COMMAND_SCANFREQ,
235 (idle & 0xff), ((idle >> 8) & 0xff),
236 (finger & 0xff), ((finger >> 8) & 0xff),
237 (stylus & 0xff), ((stylus >> 8) & 0xff) };
239 dev_dbg(&client->dev,
240 "set scan frequency to (idle: %d, finger: %d, stylus: %d)\n",
241 idle, finger, stylus);
243 return zforce_send_wait(ts, &buf[0], ARRAY_SIZE(buf));
246 static int zforce_setconfig(struct zforce_ts *ts, char b1)
248 struct i2c_client *client = ts->client;
249 char buf[7] = { FRAME_START, 5, COMMAND_SETCONFIG,
252 dev_dbg(&client->dev, "set config to (%d)\n", b1);
254 return zforce_send_wait(ts, &buf[0], ARRAY_SIZE(buf));
257 static int zforce_start(struct zforce_ts *ts)
259 struct i2c_client *client = ts->client;
260 const struct zforce_ts_platdata *pdata = dev_get_platdata(&client->dev);
263 dev_dbg(&client->dev, "starting device\n");
265 ret = zforce_command_wait(ts, COMMAND_INITIALIZE);
267 dev_err(&client->dev, "Unable to initialize, %d\n", ret);
271 ret = zforce_resolution(ts, pdata->x_max, pdata->y_max);
273 dev_err(&client->dev, "Unable to set resolution, %d\n", ret);
277 ret = zforce_scan_frequency(ts, 10, 50, 50);
279 dev_err(&client->dev, "Unable to set scan frequency, %d\n",
284 ret = zforce_setconfig(ts, SETCONFIG_DUALTOUCH);
286 dev_err(&client->dev, "Unable to set config\n");
290 /* start sending touch events */
291 ret = zforce_command(ts, COMMAND_DATAREQUEST);
293 dev_err(&client->dev, "Unable to request data\n");
298 * Per NN, initial cal. take max. of 200msec.
299 * Allow time to complete this calibration
306 zforce_command_wait(ts, COMMAND_DEACTIVATE);
310 static int zforce_stop(struct zforce_ts *ts)
312 struct i2c_client *client = ts->client;
315 dev_dbg(&client->dev, "stopping device\n");
317 /* Deactivates touch sensing and puts the device into sleep. */
318 ret = zforce_command_wait(ts, COMMAND_DEACTIVATE);
320 dev_err(&client->dev, "could not deactivate device, %d\n",
328 static int zforce_touch_event(struct zforce_ts *ts, u8 *payload)
330 struct i2c_client *client = ts->client;
331 const struct zforce_ts_platdata *pdata = dev_get_platdata(&client->dev);
332 struct zforce_point point;
333 int count, i, num = 0;
336 if (count > ZFORCE_REPORT_POINTS) {
337 dev_warn(&client->dev,
338 "too many coordinates %d, expected max %d\n",
339 count, ZFORCE_REPORT_POINTS);
340 count = ZFORCE_REPORT_POINTS;
343 for (i = 0; i < count; i++) {
345 payload[9 * i + 2] << 8 | payload[9 * i + 1];
347 payload[9 * i + 4] << 8 | payload[9 * i + 3];
349 if (point.coord_x > pdata->x_max ||
350 point.coord_y > pdata->y_max) {
351 dev_warn(&client->dev, "coordinates (%d,%d) invalid\n",
352 point.coord_x, point.coord_y);
353 point.coord_x = point.coord_y = 0;
356 point.state = payload[9 * i + 5] & 0x03;
357 point.id = (payload[9 * i + 5] & 0xfc) >> 2;
359 /* determine touch major, minor and orientation */
360 point.area_major = max(payload[9 * i + 6],
362 point.area_minor = min(payload[9 * i + 6],
364 point.orientation = payload[9 * i + 6] > payload[9 * i + 7];
366 point.pressure = payload[9 * i + 8];
367 point.prblty = payload[9 * i + 9];
369 dev_dbg(&client->dev,
370 "point %d/%d: state %d, id %d, pressure %d, prblty %d, x %d, y %d, amajor %d, aminor %d, ori %d\n",
371 i, count, point.state, point.id,
372 point.pressure, point.prblty,
373 point.coord_x, point.coord_y,
374 point.area_major, point.area_minor,
377 /* the zforce id starts with "1", so needs to be decreased */
378 input_mt_slot(ts->input, point.id - 1);
380 input_mt_report_slot_state(ts->input, MT_TOOL_FINGER,
381 point.state != STATE_UP);
383 if (point.state != STATE_UP) {
384 input_report_abs(ts->input, ABS_MT_POSITION_X,
386 input_report_abs(ts->input, ABS_MT_POSITION_Y,
388 input_report_abs(ts->input, ABS_MT_TOUCH_MAJOR,
390 input_report_abs(ts->input, ABS_MT_TOUCH_MINOR,
392 input_report_abs(ts->input, ABS_MT_ORIENTATION,
398 input_mt_sync_frame(ts->input);
400 input_mt_report_finger_count(ts->input, num);
402 input_sync(ts->input);
407 static int zforce_read_packet(struct zforce_ts *ts, u8 *buf)
409 struct i2c_client *client = ts->client;
412 mutex_lock(&ts->access_mutex);
414 /* read 2 byte message header */
415 ret = i2c_master_recv(client, buf, 2);
417 dev_err(&client->dev, "error reading header: %d\n", ret);
421 if (buf[PAYLOAD_HEADER] != FRAME_START) {
422 dev_err(&client->dev, "invalid frame start: %d\n", buf[0]);
427 if (buf[PAYLOAD_LENGTH] == 0) {
428 dev_err(&client->dev, "invalid payload length: %d\n",
429 buf[PAYLOAD_LENGTH]);
434 /* read the message */
435 ret = i2c_master_recv(client, &buf[PAYLOAD_BODY], buf[PAYLOAD_LENGTH]);
437 dev_err(&client->dev, "error reading payload: %d\n", ret);
441 dev_dbg(&client->dev, "read %d bytes for response command 0x%x\n",
442 buf[PAYLOAD_LENGTH], buf[PAYLOAD_BODY]);
445 mutex_unlock(&ts->access_mutex);
449 static void zforce_complete(struct zforce_ts *ts, int cmd, int result)
451 struct i2c_client *client = ts->client;
453 if (ts->command_waiting == cmd) {
454 dev_dbg(&client->dev, "completing command 0x%x\n", cmd);
455 ts->command_result = result;
456 complete(&ts->command_done);
458 dev_dbg(&client->dev, "command %d not for us\n", cmd);
462 static irqreturn_t zforce_interrupt(int irq, void *dev_id)
464 struct zforce_ts *ts = dev_id;
465 struct i2c_client *client = ts->client;
466 const struct zforce_ts_platdata *pdata = dev_get_platdata(&client->dev);
468 u8 payload_buffer[FRAME_MAXSIZE];
472 * When suspended, emit a wakeup signal if necessary and return.
473 * Due to the level-interrupt we will get re-triggered later.
476 if (device_may_wakeup(&client->dev))
477 pm_wakeup_event(&client->dev, 500);
482 dev_dbg(&client->dev, "handling interrupt\n");
484 /* Don't emit wakeup events from commands run by zforce_suspend */
485 if (!ts->suspending && device_may_wakeup(&client->dev))
486 pm_stay_awake(&client->dev);
488 while (!gpio_get_value(pdata->gpio_int)) {
489 ret = zforce_read_packet(ts, payload_buffer);
491 dev_err(&client->dev,
492 "could not read packet, ret: %d\n", ret);
496 payload = &payload_buffer[PAYLOAD_BODY];
498 switch (payload[RESPONSE_ID]) {
499 case NOTIFICATION_TOUCH:
501 * Always report touch-events received while
502 * suspending, when being a wakeup source
504 if (ts->suspending && device_may_wakeup(&client->dev))
505 pm_wakeup_event(&client->dev, 500);
506 zforce_touch_event(ts, &payload[RESPONSE_DATA]);
509 case NOTIFICATION_BOOTCOMPLETE:
510 ts->boot_complete = payload[RESPONSE_DATA];
511 zforce_complete(ts, payload[RESPONSE_ID], 0);
514 case RESPONSE_INITIALIZE:
515 case RESPONSE_DEACTIVATE:
516 case RESPONSE_SETCONFIG:
517 case RESPONSE_RESOLUTION:
518 case RESPONSE_SCANFREQ:
519 zforce_complete(ts, payload[RESPONSE_ID],
520 payload[RESPONSE_DATA]);
523 case RESPONSE_STATUS:
525 * Version Payload Results
526 * [2:major] [2:minor] [2:build] [2:rev]
528 ts->version_major = (payload[RESPONSE_DATA + 1] << 8) |
529 payload[RESPONSE_DATA];
530 ts->version_minor = (payload[RESPONSE_DATA + 3] << 8) |
531 payload[RESPONSE_DATA + 2];
532 ts->version_build = (payload[RESPONSE_DATA + 5] << 8) |
533 payload[RESPONSE_DATA + 4];
534 ts->version_rev = (payload[RESPONSE_DATA + 7] << 8) |
535 payload[RESPONSE_DATA + 6];
536 dev_dbg(&ts->client->dev,
537 "Firmware Version %04x:%04x %04x:%04x\n",
538 ts->version_major, ts->version_minor,
539 ts->version_build, ts->version_rev);
541 zforce_complete(ts, payload[RESPONSE_ID], 0);
544 case NOTIFICATION_INVALID_COMMAND:
545 dev_err(&ts->client->dev, "invalid command: 0x%x\n",
546 payload[RESPONSE_DATA]);
550 dev_err(&ts->client->dev,
551 "unrecognized response id: 0x%x\n",
552 payload[RESPONSE_ID]);
557 if (!ts->suspending && device_may_wakeup(&client->dev))
558 pm_relax(&client->dev);
560 dev_dbg(&client->dev, "finished interrupt\n");
565 static int zforce_input_open(struct input_dev *dev)
567 struct zforce_ts *ts = input_get_drvdata(dev);
570 ret = zforce_start(ts);
577 static void zforce_input_close(struct input_dev *dev)
579 struct zforce_ts *ts = input_get_drvdata(dev);
580 struct i2c_client *client = ts->client;
583 ret = zforce_stop(ts);
585 dev_warn(&client->dev, "stopping zforce failed\n");
590 #ifdef CONFIG_PM_SLEEP
591 static int zforce_suspend(struct device *dev)
593 struct i2c_client *client = to_i2c_client(dev);
594 struct zforce_ts *ts = i2c_get_clientdata(client);
595 struct input_dev *input = ts->input;
598 mutex_lock(&input->mutex);
599 ts->suspending = true;
602 * When configured as a wakeup source device should always wake
603 * the system, therefore start device if necessary.
605 if (device_may_wakeup(&client->dev)) {
606 dev_dbg(&client->dev, "suspend while being a wakeup source\n");
608 /* Need to start device, if not open, to be a wakeup source. */
610 ret = zforce_start(ts);
615 enable_irq_wake(client->irq);
616 } else if (input->users) {
617 dev_dbg(&client->dev,
618 "suspend without being a wakeup source\n");
620 ret = zforce_stop(ts);
624 disable_irq(client->irq);
627 ts->suspended = true;
630 ts->suspending = false;
631 mutex_unlock(&input->mutex);
636 static int zforce_resume(struct device *dev)
638 struct i2c_client *client = to_i2c_client(dev);
639 struct zforce_ts *ts = i2c_get_clientdata(client);
640 struct input_dev *input = ts->input;
643 mutex_lock(&input->mutex);
645 ts->suspended = false;
647 if (device_may_wakeup(&client->dev)) {
648 dev_dbg(&client->dev, "resume from being a wakeup source\n");
650 disable_irq_wake(client->irq);
652 /* need to stop device if it was not open on suspend */
654 ret = zforce_stop(ts);
658 } else if (input->users) {
659 dev_dbg(&client->dev, "resume without being a wakeup source\n");
661 enable_irq(client->irq);
663 ret = zforce_start(ts);
669 mutex_unlock(&input->mutex);
675 static SIMPLE_DEV_PM_OPS(zforce_pm_ops, zforce_suspend, zforce_resume);
677 static void zforce_reset(void *data)
679 struct zforce_ts *ts = data;
681 gpio_set_value(ts->pdata->gpio_rst, 0);
684 static int zforce_probe(struct i2c_client *client,
685 const struct i2c_device_id *id)
687 const struct zforce_ts_platdata *pdata = dev_get_platdata(&client->dev);
688 struct zforce_ts *ts;
689 struct input_dev *input_dev;
695 ts = devm_kzalloc(&client->dev, sizeof(struct zforce_ts), GFP_KERNEL);
699 ret = devm_gpio_request_one(&client->dev, pdata->gpio_int, GPIOF_IN,
702 dev_err(&client->dev, "request of gpio %d failed, %d\n",
703 pdata->gpio_int, ret);
707 ret = devm_gpio_request_one(&client->dev, pdata->gpio_rst,
708 GPIOF_OUT_INIT_LOW, "zforce_ts_rst");
710 dev_err(&client->dev, "request of gpio %d failed, %d\n",
711 pdata->gpio_rst, ret);
715 ret = devm_add_action(&client->dev, zforce_reset, ts);
717 dev_err(&client->dev, "failed to register reset action, %d\n",
722 snprintf(ts->phys, sizeof(ts->phys),
723 "%s/input0", dev_name(&client->dev));
725 input_dev = devm_input_allocate_device(&client->dev);
727 dev_err(&client->dev, "could not allocate input device\n");
731 mutex_init(&ts->access_mutex);
732 mutex_init(&ts->command_mutex);
736 ts->input = input_dev;
738 input_dev->name = "Neonode zForce touchscreen";
739 input_dev->phys = ts->phys;
740 input_dev->id.bustype = BUS_I2C;
742 input_dev->open = zforce_input_open;
743 input_dev->close = zforce_input_close;
745 __set_bit(EV_KEY, input_dev->evbit);
746 __set_bit(EV_SYN, input_dev->evbit);
747 __set_bit(EV_ABS, input_dev->evbit);
749 /* For multi touch */
750 input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0,
752 input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0,
755 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR, 0,
756 ZFORCE_MAX_AREA, 0, 0);
757 input_set_abs_params(input_dev, ABS_MT_TOUCH_MINOR, 0,
758 ZFORCE_MAX_AREA, 0, 0);
759 input_set_abs_params(input_dev, ABS_MT_ORIENTATION, 0, 1, 0, 0);
760 input_mt_init_slots(input_dev, ZFORCE_REPORT_POINTS, INPUT_MT_DIRECT);
762 input_set_drvdata(ts->input, ts);
764 init_completion(&ts->command_done);
767 * The zforce pulls the interrupt low when it has data ready.
768 * After it is triggered the isr thread runs until all the available
769 * packets have been read and the interrupt is high again.
770 * Therefore we can trigger the interrupt anytime it is low and do
771 * not need to limit it to the interrupt edge.
773 ret = devm_request_threaded_irq(&client->dev, client->irq, NULL,
775 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
776 input_dev->name, ts);
778 dev_err(&client->dev, "irq %d request failed\n", client->irq);
782 i2c_set_clientdata(client, ts);
784 /* let the controller boot */
785 gpio_set_value(pdata->gpio_rst, 1);
787 ts->command_waiting = NOTIFICATION_BOOTCOMPLETE;
788 if (wait_for_completion_timeout(&ts->command_done, WAIT_TIMEOUT) == 0)
789 dev_warn(&client->dev, "bootcomplete timed out\n");
791 /* need to start device to get version information */
792 ret = zforce_command_wait(ts, COMMAND_INITIALIZE);
794 dev_err(&client->dev, "unable to initialize, %d\n", ret);
798 /* this gets the firmware version among other information */
799 ret = zforce_command_wait(ts, COMMAND_STATUS);
801 dev_err(&client->dev, "couldn't get status, %d\n", ret);
806 /* stop device and put it into sleep until it is opened */
807 ret = zforce_stop(ts);
811 device_set_wakeup_capable(&client->dev, true);
813 ret = input_register_device(input_dev);
815 dev_err(&client->dev, "could not register input device, %d\n",
823 static struct i2c_device_id zforce_idtable[] = {
827 MODULE_DEVICE_TABLE(i2c, zforce_idtable);
829 static struct i2c_driver zforce_driver = {
831 .owner = THIS_MODULE,
833 .pm = &zforce_pm_ops,
835 .probe = zforce_probe,
836 .id_table = zforce_idtable,
839 module_i2c_driver(zforce_driver);
841 MODULE_AUTHOR("Heiko Stuebner <heiko@sntech.de>");
842 MODULE_DESCRIPTION("zForce TouchScreen Driver");
843 MODULE_LICENSE("GPL");