dt-bindings: mtd: ingenic: Use standard ecc-engine property
[linux/fpc-iii.git] / drivers / input / mouse / sentelic.c
blob1d6010d463e2cf7b7ee13b93ba8f6f4fb0e7127d
1 /*-
2 * Finger Sensing Pad PS/2 mouse driver.
4 * Copyright (C) 2005-2007 Asia Vital Components Co., Ltd.
5 * Copyright (C) 2005-2012 Tai-hwa Liang, Sentelic Corporation.
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <linux/module.h>
23 #include <linux/input.h>
24 #include <linux/input/mt.h>
25 #include <linux/ctype.h>
26 #include <linux/libps2.h>
27 #include <linux/serio.h>
28 #include <linux/jiffies.h>
29 #include <linux/slab.h>
31 #include "psmouse.h"
32 #include "sentelic.h"
35 * Timeout for FSP PS/2 command only (in milliseconds).
37 #define FSP_CMD_TIMEOUT 200
38 #define FSP_CMD_TIMEOUT2 30
40 #define GET_ABS_X(packet) ((packet[1] << 2) | ((packet[3] >> 2) & 0x03))
41 #define GET_ABS_Y(packet) ((packet[2] << 2) | (packet[3] & 0x03))
43 /** Driver version. */
44 static const char fsp_drv_ver[] = "1.1.0-K";
47 * Make sure that the value being sent to FSP will not conflict with
48 * possible sample rate values.
50 static unsigned char fsp_test_swap_cmd(unsigned char reg_val)
52 switch (reg_val) {
53 case 10: case 20: case 40: case 60: case 80: case 100: case 200:
55 * The requested value being sent to FSP matched to possible
56 * sample rates, swap the given value such that the hardware
57 * wouldn't get confused.
59 return (reg_val >> 4) | (reg_val << 4);
60 default:
61 return reg_val; /* swap isn't necessary */
66 * Make sure that the value being sent to FSP will not conflict with certain
67 * commands.
69 static unsigned char fsp_test_invert_cmd(unsigned char reg_val)
71 switch (reg_val) {
72 case 0xe9: case 0xee: case 0xf2: case 0xff:
74 * The requested value being sent to FSP matched to certain
75 * commands, inverse the given value such that the hardware
76 * wouldn't get confused.
78 return ~reg_val;
79 default:
80 return reg_val; /* inversion isn't necessary */
84 static int fsp_reg_read(struct psmouse *psmouse, int reg_addr, int *reg_val)
86 struct ps2dev *ps2dev = &psmouse->ps2dev;
87 unsigned char param[3];
88 unsigned char addr;
89 int rc = -1;
92 * We need to shut off the device and switch it into command
93 * mode so we don't confuse our protocol handler. We don't need
94 * to do that for writes because sysfs set helper does this for
95 * us.
97 psmouse_deactivate(psmouse);
99 ps2_begin_command(ps2dev);
101 if (ps2_sendbyte(ps2dev, 0xf3, FSP_CMD_TIMEOUT) < 0)
102 goto out;
104 /* should return 0xfe(request for resending) */
105 ps2_sendbyte(ps2dev, 0x66, FSP_CMD_TIMEOUT2);
106 /* should return 0xfc(failed) */
107 ps2_sendbyte(ps2dev, 0x88, FSP_CMD_TIMEOUT2);
109 if (ps2_sendbyte(ps2dev, 0xf3, FSP_CMD_TIMEOUT) < 0)
110 goto out;
112 if ((addr = fsp_test_invert_cmd(reg_addr)) != reg_addr) {
113 ps2_sendbyte(ps2dev, 0x68, FSP_CMD_TIMEOUT2);
114 } else if ((addr = fsp_test_swap_cmd(reg_addr)) != reg_addr) {
115 /* swapping is required */
116 ps2_sendbyte(ps2dev, 0xcc, FSP_CMD_TIMEOUT2);
117 /* expect 0xfe */
118 } else {
119 /* swapping isn't necessary */
120 ps2_sendbyte(ps2dev, 0x66, FSP_CMD_TIMEOUT2);
121 /* expect 0xfe */
123 /* should return 0xfc(failed) */
124 ps2_sendbyte(ps2dev, addr, FSP_CMD_TIMEOUT);
126 if (__ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO) < 0)
127 goto out;
129 *reg_val = param[2];
130 rc = 0;
132 out:
133 ps2_end_command(ps2dev);
134 psmouse_activate(psmouse);
135 psmouse_dbg(psmouse,
136 "READ REG: 0x%02x is 0x%02x (rc = %d)\n",
137 reg_addr, *reg_val, rc);
138 return rc;
141 static int fsp_reg_write(struct psmouse *psmouse, int reg_addr, int reg_val)
143 struct ps2dev *ps2dev = &psmouse->ps2dev;
144 unsigned char v;
145 int rc = -1;
147 ps2_begin_command(ps2dev);
149 if (ps2_sendbyte(ps2dev, 0xf3, FSP_CMD_TIMEOUT) < 0)
150 goto out;
152 if ((v = fsp_test_invert_cmd(reg_addr)) != reg_addr) {
153 /* inversion is required */
154 ps2_sendbyte(ps2dev, 0x74, FSP_CMD_TIMEOUT2);
155 } else {
156 if ((v = fsp_test_swap_cmd(reg_addr)) != reg_addr) {
157 /* swapping is required */
158 ps2_sendbyte(ps2dev, 0x77, FSP_CMD_TIMEOUT2);
159 } else {
160 /* swapping isn't necessary */
161 ps2_sendbyte(ps2dev, 0x55, FSP_CMD_TIMEOUT2);
164 /* write the register address in correct order */
165 ps2_sendbyte(ps2dev, v, FSP_CMD_TIMEOUT2);
167 if (ps2_sendbyte(ps2dev, 0xf3, FSP_CMD_TIMEOUT) < 0)
168 goto out;
170 if ((v = fsp_test_invert_cmd(reg_val)) != reg_val) {
171 /* inversion is required */
172 ps2_sendbyte(ps2dev, 0x47, FSP_CMD_TIMEOUT2);
173 } else if ((v = fsp_test_swap_cmd(reg_val)) != reg_val) {
174 /* swapping is required */
175 ps2_sendbyte(ps2dev, 0x44, FSP_CMD_TIMEOUT2);
176 } else {
177 /* swapping isn't necessary */
178 ps2_sendbyte(ps2dev, 0x33, FSP_CMD_TIMEOUT2);
181 /* write the register value in correct order */
182 ps2_sendbyte(ps2dev, v, FSP_CMD_TIMEOUT2);
183 rc = 0;
185 out:
186 ps2_end_command(ps2dev);
187 psmouse_dbg(psmouse,
188 "WRITE REG: 0x%02x to 0x%02x (rc = %d)\n",
189 reg_addr, reg_val, rc);
190 return rc;
193 /* Enable register clock gating for writing certain registers */
194 static int fsp_reg_write_enable(struct psmouse *psmouse, bool enable)
196 int v, nv;
198 if (fsp_reg_read(psmouse, FSP_REG_SYSCTL1, &v) == -1)
199 return -1;
201 if (enable)
202 nv = v | FSP_BIT_EN_REG_CLK;
203 else
204 nv = v & ~FSP_BIT_EN_REG_CLK;
206 /* only write if necessary */
207 if (nv != v)
208 if (fsp_reg_write(psmouse, FSP_REG_SYSCTL1, nv) == -1)
209 return -1;
211 return 0;
214 static int fsp_page_reg_read(struct psmouse *psmouse, int *reg_val)
216 struct ps2dev *ps2dev = &psmouse->ps2dev;
217 unsigned char param[3];
218 int rc = -1;
220 psmouse_deactivate(psmouse);
222 ps2_begin_command(ps2dev);
224 if (ps2_sendbyte(ps2dev, 0xf3, FSP_CMD_TIMEOUT) < 0)
225 goto out;
227 ps2_sendbyte(ps2dev, 0x66, FSP_CMD_TIMEOUT2);
228 ps2_sendbyte(ps2dev, 0x88, FSP_CMD_TIMEOUT2);
230 if (ps2_sendbyte(ps2dev, 0xf3, FSP_CMD_TIMEOUT) < 0)
231 goto out;
233 ps2_sendbyte(ps2dev, 0x83, FSP_CMD_TIMEOUT2);
234 ps2_sendbyte(ps2dev, 0x88, FSP_CMD_TIMEOUT2);
236 /* get the returned result */
237 if (__ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
238 goto out;
240 *reg_val = param[2];
241 rc = 0;
243 out:
244 ps2_end_command(ps2dev);
245 psmouse_activate(psmouse);
246 psmouse_dbg(psmouse,
247 "READ PAGE REG: 0x%02x (rc = %d)\n",
248 *reg_val, rc);
249 return rc;
252 static int fsp_page_reg_write(struct psmouse *psmouse, int reg_val)
254 struct ps2dev *ps2dev = &psmouse->ps2dev;
255 unsigned char v;
256 int rc = -1;
258 ps2_begin_command(ps2dev);
260 if (ps2_sendbyte(ps2dev, 0xf3, FSP_CMD_TIMEOUT) < 0)
261 goto out;
263 ps2_sendbyte(ps2dev, 0x38, FSP_CMD_TIMEOUT2);
264 ps2_sendbyte(ps2dev, 0x88, FSP_CMD_TIMEOUT2);
266 if (ps2_sendbyte(ps2dev, 0xf3, FSP_CMD_TIMEOUT) < 0)
267 goto out;
269 if ((v = fsp_test_invert_cmd(reg_val)) != reg_val) {
270 ps2_sendbyte(ps2dev, 0x47, FSP_CMD_TIMEOUT2);
271 } else if ((v = fsp_test_swap_cmd(reg_val)) != reg_val) {
272 /* swapping is required */
273 ps2_sendbyte(ps2dev, 0x44, FSP_CMD_TIMEOUT2);
274 } else {
275 /* swapping isn't necessary */
276 ps2_sendbyte(ps2dev, 0x33, FSP_CMD_TIMEOUT2);
279 ps2_sendbyte(ps2dev, v, FSP_CMD_TIMEOUT2);
280 rc = 0;
282 out:
283 ps2_end_command(ps2dev);
284 psmouse_dbg(psmouse,
285 "WRITE PAGE REG: to 0x%02x (rc = %d)\n",
286 reg_val, rc);
287 return rc;
290 static int fsp_get_version(struct psmouse *psmouse, int *version)
292 if (fsp_reg_read(psmouse, FSP_REG_VERSION, version))
293 return -EIO;
295 return 0;
298 static int fsp_get_revision(struct psmouse *psmouse, int *rev)
300 if (fsp_reg_read(psmouse, FSP_REG_REVISION, rev))
301 return -EIO;
303 return 0;
306 static int fsp_get_sn(struct psmouse *psmouse, int *sn)
308 int v0, v1, v2;
309 int rc = -EIO;
311 /* production number since Cx is available at: 0x0b40 ~ 0x0b42 */
312 if (fsp_page_reg_write(psmouse, FSP_PAGE_0B))
313 goto out;
314 if (fsp_reg_read(psmouse, FSP_REG_SN0, &v0))
315 goto out;
316 if (fsp_reg_read(psmouse, FSP_REG_SN1, &v1))
317 goto out;
318 if (fsp_reg_read(psmouse, FSP_REG_SN2, &v2))
319 goto out;
320 *sn = (v0 << 16) | (v1 << 8) | v2;
321 rc = 0;
322 out:
323 fsp_page_reg_write(psmouse, FSP_PAGE_DEFAULT);
324 return rc;
327 static int fsp_get_buttons(struct psmouse *psmouse, int *btn)
329 static const int buttons[] = {
330 0x16, /* Left/Middle/Right/Forward/Backward & Scroll Up/Down */
331 0x06, /* Left/Middle/Right & Scroll Up/Down/Right/Left */
332 0x04, /* Left/Middle/Right & Scroll Up/Down */
333 0x02, /* Left/Middle/Right */
335 int val;
337 if (fsp_reg_read(psmouse, FSP_REG_TMOD_STATUS, &val) == -1)
338 return -EIO;
340 *btn = buttons[(val & 0x30) >> 4];
341 return 0;
344 /* Enable on-pad command tag output */
345 static int fsp_opc_tag_enable(struct psmouse *psmouse, bool enable)
347 int v, nv;
348 int res = 0;
350 if (fsp_reg_read(psmouse, FSP_REG_OPC_QDOWN, &v) == -1) {
351 psmouse_err(psmouse, "Unable get OPC state.\n");
352 return -EIO;
355 if (enable)
356 nv = v | FSP_BIT_EN_OPC_TAG;
357 else
358 nv = v & ~FSP_BIT_EN_OPC_TAG;
360 /* only write if necessary */
361 if (nv != v) {
362 fsp_reg_write_enable(psmouse, true);
363 res = fsp_reg_write(psmouse, FSP_REG_OPC_QDOWN, nv);
364 fsp_reg_write_enable(psmouse, false);
367 if (res != 0) {
368 psmouse_err(psmouse, "Unable to enable OPC tag.\n");
369 res = -EIO;
372 return res;
375 static int fsp_onpad_vscr(struct psmouse *psmouse, bool enable)
377 struct fsp_data *pad = psmouse->private;
378 int val;
380 if (fsp_reg_read(psmouse, FSP_REG_ONPAD_CTL, &val))
381 return -EIO;
383 pad->vscroll = enable;
385 if (enable)
386 val |= (FSP_BIT_FIX_VSCR | FSP_BIT_ONPAD_ENABLE);
387 else
388 val &= ~FSP_BIT_FIX_VSCR;
390 if (fsp_reg_write(psmouse, FSP_REG_ONPAD_CTL, val))
391 return -EIO;
393 return 0;
396 static int fsp_onpad_hscr(struct psmouse *psmouse, bool enable)
398 struct fsp_data *pad = psmouse->private;
399 int val, v2;
401 if (fsp_reg_read(psmouse, FSP_REG_ONPAD_CTL, &val))
402 return -EIO;
404 if (fsp_reg_read(psmouse, FSP_REG_SYSCTL5, &v2))
405 return -EIO;
407 pad->hscroll = enable;
409 if (enable) {
410 val |= (FSP_BIT_FIX_HSCR | FSP_BIT_ONPAD_ENABLE);
411 v2 |= FSP_BIT_EN_MSID6;
412 } else {
413 val &= ~FSP_BIT_FIX_HSCR;
414 v2 &= ~(FSP_BIT_EN_MSID6 | FSP_BIT_EN_MSID7 | FSP_BIT_EN_MSID8);
417 if (fsp_reg_write(psmouse, FSP_REG_ONPAD_CTL, val))
418 return -EIO;
420 /* reconfigure horizontal scrolling packet output */
421 if (fsp_reg_write(psmouse, FSP_REG_SYSCTL5, v2))
422 return -EIO;
424 return 0;
428 * Write device specific initial parameters.
430 * ex: 0xab 0xcd - write oxcd into register 0xab
432 static ssize_t fsp_attr_set_setreg(struct psmouse *psmouse, void *data,
433 const char *buf, size_t count)
435 unsigned int reg, val;
436 char *rest;
437 ssize_t retval;
439 reg = simple_strtoul(buf, &rest, 16);
440 if (rest == buf || *rest != ' ' || reg > 0xff)
441 return -EINVAL;
443 retval = kstrtouint(rest + 1, 16, &val);
444 if (retval)
445 return retval;
447 if (val > 0xff)
448 return -EINVAL;
450 if (fsp_reg_write_enable(psmouse, true))
451 return -EIO;
453 retval = fsp_reg_write(psmouse, reg, val) < 0 ? -EIO : count;
455 fsp_reg_write_enable(psmouse, false);
457 return count;
460 PSMOUSE_DEFINE_WO_ATTR(setreg, S_IWUSR, NULL, fsp_attr_set_setreg);
462 static ssize_t fsp_attr_show_getreg(struct psmouse *psmouse,
463 void *data, char *buf)
465 struct fsp_data *pad = psmouse->private;
467 return sprintf(buf, "%02x%02x\n", pad->last_reg, pad->last_val);
471 * Read a register from device.
473 * ex: 0xab -- read content from register 0xab
475 static ssize_t fsp_attr_set_getreg(struct psmouse *psmouse, void *data,
476 const char *buf, size_t count)
478 struct fsp_data *pad = psmouse->private;
479 unsigned int reg, val;
480 int err;
482 err = kstrtouint(buf, 16, &reg);
483 if (err)
484 return err;
486 if (reg > 0xff)
487 return -EINVAL;
489 if (fsp_reg_read(psmouse, reg, &val))
490 return -EIO;
492 pad->last_reg = reg;
493 pad->last_val = val;
495 return count;
498 PSMOUSE_DEFINE_ATTR(getreg, S_IWUSR | S_IRUGO, NULL,
499 fsp_attr_show_getreg, fsp_attr_set_getreg);
501 static ssize_t fsp_attr_show_pagereg(struct psmouse *psmouse,
502 void *data, char *buf)
504 int val = 0;
506 if (fsp_page_reg_read(psmouse, &val))
507 return -EIO;
509 return sprintf(buf, "%02x\n", val);
512 static ssize_t fsp_attr_set_pagereg(struct psmouse *psmouse, void *data,
513 const char *buf, size_t count)
515 unsigned int val;
516 int err;
518 err = kstrtouint(buf, 16, &val);
519 if (err)
520 return err;
522 if (val > 0xff)
523 return -EINVAL;
525 if (fsp_page_reg_write(psmouse, val))
526 return -EIO;
528 return count;
531 PSMOUSE_DEFINE_ATTR(page, S_IWUSR | S_IRUGO, NULL,
532 fsp_attr_show_pagereg, fsp_attr_set_pagereg);
534 static ssize_t fsp_attr_show_vscroll(struct psmouse *psmouse,
535 void *data, char *buf)
537 struct fsp_data *pad = psmouse->private;
539 return sprintf(buf, "%d\n", pad->vscroll);
542 static ssize_t fsp_attr_set_vscroll(struct psmouse *psmouse, void *data,
543 const char *buf, size_t count)
545 unsigned int val;
546 int err;
548 err = kstrtouint(buf, 10, &val);
549 if (err)
550 return err;
552 if (val > 1)
553 return -EINVAL;
555 fsp_onpad_vscr(psmouse, val);
557 return count;
560 PSMOUSE_DEFINE_ATTR(vscroll, S_IWUSR | S_IRUGO, NULL,
561 fsp_attr_show_vscroll, fsp_attr_set_vscroll);
563 static ssize_t fsp_attr_show_hscroll(struct psmouse *psmouse,
564 void *data, char *buf)
566 struct fsp_data *pad = psmouse->private;
568 return sprintf(buf, "%d\n", pad->hscroll);
571 static ssize_t fsp_attr_set_hscroll(struct psmouse *psmouse, void *data,
572 const char *buf, size_t count)
574 unsigned int val;
575 int err;
577 err = kstrtouint(buf, 10, &val);
578 if (err)
579 return err;
581 if (val > 1)
582 return -EINVAL;
584 fsp_onpad_hscr(psmouse, val);
586 return count;
589 PSMOUSE_DEFINE_ATTR(hscroll, S_IWUSR | S_IRUGO, NULL,
590 fsp_attr_show_hscroll, fsp_attr_set_hscroll);
592 static ssize_t fsp_attr_show_flags(struct psmouse *psmouse,
593 void *data, char *buf)
595 struct fsp_data *pad = psmouse->private;
597 return sprintf(buf, "%c\n",
598 pad->flags & FSPDRV_FLAG_EN_OPC ? 'C' : 'c');
601 static ssize_t fsp_attr_set_flags(struct psmouse *psmouse, void *data,
602 const char *buf, size_t count)
604 struct fsp_data *pad = psmouse->private;
605 size_t i;
607 for (i = 0; i < count; i++) {
608 switch (buf[i]) {
609 case 'C':
610 pad->flags |= FSPDRV_FLAG_EN_OPC;
611 break;
612 case 'c':
613 pad->flags &= ~FSPDRV_FLAG_EN_OPC;
614 break;
615 default:
616 return -EINVAL;
619 return count;
622 PSMOUSE_DEFINE_ATTR(flags, S_IWUSR | S_IRUGO, NULL,
623 fsp_attr_show_flags, fsp_attr_set_flags);
625 static ssize_t fsp_attr_show_ver(struct psmouse *psmouse,
626 void *data, char *buf)
628 return sprintf(buf, "Sentelic FSP kernel module %s\n", fsp_drv_ver);
631 PSMOUSE_DEFINE_RO_ATTR(ver, S_IRUGO, NULL, fsp_attr_show_ver);
633 static struct attribute *fsp_attributes[] = {
634 &psmouse_attr_setreg.dattr.attr,
635 &psmouse_attr_getreg.dattr.attr,
636 &psmouse_attr_page.dattr.attr,
637 &psmouse_attr_vscroll.dattr.attr,
638 &psmouse_attr_hscroll.dattr.attr,
639 &psmouse_attr_flags.dattr.attr,
640 &psmouse_attr_ver.dattr.attr,
641 NULL
644 static struct attribute_group fsp_attribute_group = {
645 .attrs = fsp_attributes,
648 #ifdef FSP_DEBUG
649 static void fsp_packet_debug(struct psmouse *psmouse, unsigned char packet[])
651 static unsigned int ps2_packet_cnt;
652 static unsigned int ps2_last_second;
653 unsigned int jiffies_msec;
654 const char *packet_type = "UNKNOWN";
655 unsigned short abs_x = 0, abs_y = 0;
657 /* Interpret & dump the packet data. */
658 switch (packet[0] >> FSP_PKT_TYPE_SHIFT) {
659 case FSP_PKT_TYPE_ABS:
660 packet_type = "Absolute";
661 abs_x = GET_ABS_X(packet);
662 abs_y = GET_ABS_Y(packet);
663 break;
664 case FSP_PKT_TYPE_NORMAL:
665 packet_type = "Normal";
666 break;
667 case FSP_PKT_TYPE_NOTIFY:
668 packet_type = "Notify";
669 break;
670 case FSP_PKT_TYPE_NORMAL_OPC:
671 packet_type = "Normal-OPC";
672 break;
675 ps2_packet_cnt++;
676 jiffies_msec = jiffies_to_msecs(jiffies);
677 psmouse_dbg(psmouse,
678 "%08dms %s packets: %02x, %02x, %02x, %02x; "
679 "abs_x: %d, abs_y: %d\n",
680 jiffies_msec, packet_type,
681 packet[0], packet[1], packet[2], packet[3], abs_x, abs_y);
683 if (jiffies_msec - ps2_last_second > 1000) {
684 psmouse_dbg(psmouse, "PS/2 packets/sec = %d\n", ps2_packet_cnt);
685 ps2_packet_cnt = 0;
686 ps2_last_second = jiffies_msec;
689 #else
690 static void fsp_packet_debug(struct psmouse *psmouse, unsigned char packet[])
693 #endif
695 static void fsp_set_slot(struct input_dev *dev, int slot, bool active,
696 unsigned int x, unsigned int y)
698 input_mt_slot(dev, slot);
699 input_mt_report_slot_state(dev, MT_TOOL_FINGER, active);
700 if (active) {
701 input_report_abs(dev, ABS_MT_POSITION_X, x);
702 input_report_abs(dev, ABS_MT_POSITION_Y, y);
706 static psmouse_ret_t fsp_process_byte(struct psmouse *psmouse)
708 struct input_dev *dev = psmouse->dev;
709 struct fsp_data *ad = psmouse->private;
710 unsigned char *packet = psmouse->packet;
711 unsigned char button_status = 0, lscroll = 0, rscroll = 0;
712 unsigned short abs_x, abs_y, fgrs = 0;
714 if (psmouse->pktcnt < 4)
715 return PSMOUSE_GOOD_DATA;
718 * Full packet accumulated, process it
721 fsp_packet_debug(psmouse, packet);
723 switch (psmouse->packet[0] >> FSP_PKT_TYPE_SHIFT) {
724 case FSP_PKT_TYPE_ABS:
726 if ((packet[0] == 0x48 || packet[0] == 0x49) &&
727 packet[1] == 0 && packet[2] == 0) {
729 * Ignore coordinate noise when finger leaving the
730 * surface, otherwise cursor may jump to upper-left
731 * corner.
733 packet[3] &= 0xf0;
736 abs_x = GET_ABS_X(packet);
737 abs_y = GET_ABS_Y(packet);
739 if (packet[0] & FSP_PB0_MFMC) {
741 * MFMC packet: assume that there are two fingers on
742 * pad
744 fgrs = 2;
746 /* MFMC packet */
747 if (packet[0] & FSP_PB0_MFMC_FGR2) {
748 /* 2nd finger */
749 if (ad->last_mt_fgr == 2) {
751 * workaround for buggy firmware
752 * which doesn't clear MFMC bit if
753 * the 1st finger is up
755 fgrs = 1;
756 fsp_set_slot(dev, 0, false, 0, 0);
758 ad->last_mt_fgr = 2;
760 fsp_set_slot(dev, 1, fgrs == 2, abs_x, abs_y);
761 } else {
762 /* 1st finger */
763 if (ad->last_mt_fgr == 1) {
765 * workaround for buggy firmware
766 * which doesn't clear MFMC bit if
767 * the 2nd finger is up
769 fgrs = 1;
770 fsp_set_slot(dev, 1, false, 0, 0);
772 ad->last_mt_fgr = 1;
773 fsp_set_slot(dev, 0, fgrs != 0, abs_x, abs_y);
775 } else {
776 /* SFAC packet */
777 if ((packet[0] & (FSP_PB0_LBTN|FSP_PB0_PHY_BTN)) ==
778 FSP_PB0_LBTN) {
779 /* On-pad click in SFAC mode should be handled
780 * by userspace. On-pad clicks in MFMC mode
781 * are real clickpad clicks, and not ignored.
783 packet[0] &= ~FSP_PB0_LBTN;
786 /* no multi-finger information */
787 ad->last_mt_fgr = 0;
789 if (abs_x != 0 && abs_y != 0)
790 fgrs = 1;
792 fsp_set_slot(dev, 0, fgrs > 0, abs_x, abs_y);
793 fsp_set_slot(dev, 1, false, 0, 0);
795 if (fgrs == 1 || (fgrs == 2 && !(packet[0] & FSP_PB0_MFMC_FGR2))) {
796 input_report_abs(dev, ABS_X, abs_x);
797 input_report_abs(dev, ABS_Y, abs_y);
799 input_report_key(dev, BTN_LEFT, packet[0] & 0x01);
800 input_report_key(dev, BTN_RIGHT, packet[0] & 0x02);
801 input_report_key(dev, BTN_TOUCH, fgrs);
802 input_report_key(dev, BTN_TOOL_FINGER, fgrs == 1);
803 input_report_key(dev, BTN_TOOL_DOUBLETAP, fgrs == 2);
804 break;
806 case FSP_PKT_TYPE_NORMAL_OPC:
807 /* on-pad click, filter it if necessary */
808 if ((ad->flags & FSPDRV_FLAG_EN_OPC) != FSPDRV_FLAG_EN_OPC)
809 packet[0] &= ~FSP_PB0_LBTN;
810 /* fall through */
812 case FSP_PKT_TYPE_NORMAL:
813 /* normal packet */
814 /* special packet data translation from on-pad packets */
815 if (packet[3] != 0) {
816 if (packet[3] & BIT(0))
817 button_status |= 0x01; /* wheel down */
818 if (packet[3] & BIT(1))
819 button_status |= 0x0f; /* wheel up */
820 if (packet[3] & BIT(2))
821 button_status |= BIT(4);/* horizontal left */
822 if (packet[3] & BIT(3))
823 button_status |= BIT(5);/* horizontal right */
824 /* push back to packet queue */
825 if (button_status != 0)
826 packet[3] = button_status;
827 rscroll = (packet[3] >> 4) & 1;
828 lscroll = (packet[3] >> 5) & 1;
831 * Processing wheel up/down and extra button events
833 input_report_rel(dev, REL_WHEEL,
834 (int)(packet[3] & 8) - (int)(packet[3] & 7));
835 input_report_rel(dev, REL_HWHEEL, lscroll - rscroll);
836 input_report_key(dev, BTN_BACK, lscroll);
837 input_report_key(dev, BTN_FORWARD, rscroll);
840 * Standard PS/2 Mouse
842 psmouse_report_standard_packet(dev, packet);
843 break;
846 input_sync(dev);
848 return PSMOUSE_FULL_PACKET;
851 static int fsp_activate_protocol(struct psmouse *psmouse)
853 struct fsp_data *pad = psmouse->private;
854 struct ps2dev *ps2dev = &psmouse->ps2dev;
855 unsigned char param[2];
856 int val;
859 * Standard procedure to enter FSP Intellimouse mode
860 * (scrolling wheel, 4th and 5th buttons)
862 param[0] = 200;
863 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
864 param[0] = 200;
865 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
866 param[0] = 80;
867 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
869 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
870 if (param[0] != 0x04) {
871 psmouse_err(psmouse,
872 "Unable to enable 4 bytes packet format.\n");
873 return -EIO;
876 if (pad->ver < FSP_VER_STL3888_C0) {
877 /* Preparing relative coordinates output for older hardware */
878 if (fsp_reg_read(psmouse, FSP_REG_SYSCTL5, &val)) {
879 psmouse_err(psmouse,
880 "Unable to read SYSCTL5 register.\n");
881 return -EIO;
884 if (fsp_get_buttons(psmouse, &pad->buttons)) {
885 psmouse_err(psmouse,
886 "Unable to retrieve number of buttons.\n");
887 return -EIO;
890 val &= ~(FSP_BIT_EN_MSID7 | FSP_BIT_EN_MSID8 | FSP_BIT_EN_AUTO_MSID8);
891 /* Ensure we are not in absolute mode */
892 val &= ~FSP_BIT_EN_PKT_G0;
893 if (pad->buttons == 0x06) {
894 /* Left/Middle/Right & Scroll Up/Down/Right/Left */
895 val |= FSP_BIT_EN_MSID6;
898 if (fsp_reg_write(psmouse, FSP_REG_SYSCTL5, val)) {
899 psmouse_err(psmouse,
900 "Unable to set up required mode bits.\n");
901 return -EIO;
905 * Enable OPC tags such that driver can tell the difference
906 * between on-pad and real button click
908 if (fsp_opc_tag_enable(psmouse, true))
909 psmouse_warn(psmouse,
910 "Failed to enable OPC tag mode.\n");
911 /* enable on-pad click by default */
912 pad->flags |= FSPDRV_FLAG_EN_OPC;
914 /* Enable on-pad vertical and horizontal scrolling */
915 fsp_onpad_vscr(psmouse, true);
916 fsp_onpad_hscr(psmouse, true);
917 } else {
918 /* Enable absolute coordinates output for Cx/Dx hardware */
919 if (fsp_reg_write(psmouse, FSP_REG_SWC1,
920 FSP_BIT_SWC1_EN_ABS_1F |
921 FSP_BIT_SWC1_EN_ABS_2F |
922 FSP_BIT_SWC1_EN_FUP_OUT |
923 FSP_BIT_SWC1_EN_ABS_CON)) {
924 psmouse_err(psmouse,
925 "Unable to enable absolute coordinates output.\n");
926 return -EIO;
930 return 0;
933 static int fsp_set_input_params(struct psmouse *psmouse)
935 struct input_dev *dev = psmouse->dev;
936 struct fsp_data *pad = psmouse->private;
938 if (pad->ver < FSP_VER_STL3888_C0) {
939 __set_bit(BTN_MIDDLE, dev->keybit);
940 __set_bit(BTN_BACK, dev->keybit);
941 __set_bit(BTN_FORWARD, dev->keybit);
942 __set_bit(REL_WHEEL, dev->relbit);
943 __set_bit(REL_HWHEEL, dev->relbit);
944 } else {
946 * Hardware prior to Cx performs much better in relative mode;
947 * hence, only enable absolute coordinates output as well as
948 * multi-touch output for the newer hardware.
950 * Maximum coordinates can be computed as:
952 * number of scanlines * 64 - 57
954 * where number of X/Y scanline lines are 16/12.
956 int abs_x = 967, abs_y = 711;
958 __set_bit(EV_ABS, dev->evbit);
959 __clear_bit(EV_REL, dev->evbit);
960 __set_bit(BTN_TOUCH, dev->keybit);
961 __set_bit(BTN_TOOL_FINGER, dev->keybit);
962 __set_bit(BTN_TOOL_DOUBLETAP, dev->keybit);
963 __set_bit(INPUT_PROP_SEMI_MT, dev->propbit);
965 input_set_abs_params(dev, ABS_X, 0, abs_x, 0, 0);
966 input_set_abs_params(dev, ABS_Y, 0, abs_y, 0, 0);
967 input_mt_init_slots(dev, 2, 0);
968 input_set_abs_params(dev, ABS_MT_POSITION_X, 0, abs_x, 0, 0);
969 input_set_abs_params(dev, ABS_MT_POSITION_Y, 0, abs_y, 0, 0);
972 return 0;
975 int fsp_detect(struct psmouse *psmouse, bool set_properties)
977 int id;
979 if (fsp_reg_read(psmouse, FSP_REG_DEVICE_ID, &id))
980 return -EIO;
982 if (id != 0x01)
983 return -ENODEV;
985 if (set_properties) {
986 psmouse->vendor = "Sentelic";
987 psmouse->name = "FingerSensingPad";
990 return 0;
993 static void fsp_reset(struct psmouse *psmouse)
995 fsp_opc_tag_enable(psmouse, false);
996 fsp_onpad_vscr(psmouse, false);
997 fsp_onpad_hscr(psmouse, false);
1000 static void fsp_disconnect(struct psmouse *psmouse)
1002 sysfs_remove_group(&psmouse->ps2dev.serio->dev.kobj,
1003 &fsp_attribute_group);
1005 fsp_reset(psmouse);
1006 kfree(psmouse->private);
1009 static int fsp_reconnect(struct psmouse *psmouse)
1011 int version;
1013 if (fsp_detect(psmouse, 0))
1014 return -ENODEV;
1016 if (fsp_get_version(psmouse, &version))
1017 return -ENODEV;
1019 if (fsp_activate_protocol(psmouse))
1020 return -EIO;
1022 return 0;
1025 int fsp_init(struct psmouse *psmouse)
1027 struct fsp_data *priv;
1028 int ver, rev, sn = 0;
1029 int error;
1031 if (fsp_get_version(psmouse, &ver) ||
1032 fsp_get_revision(psmouse, &rev)) {
1033 return -ENODEV;
1035 if (ver >= FSP_VER_STL3888_C0) {
1036 /* firmware information is only available since C0 */
1037 fsp_get_sn(psmouse, &sn);
1040 psmouse_info(psmouse,
1041 "Finger Sensing Pad, hw: %d.%d.%d, sn: %x, sw: %s\n",
1042 ver >> 4, ver & 0x0F, rev, sn, fsp_drv_ver);
1044 psmouse->private = priv = kzalloc(sizeof(struct fsp_data), GFP_KERNEL);
1045 if (!priv)
1046 return -ENOMEM;
1048 priv->ver = ver;
1049 priv->rev = rev;
1051 psmouse->protocol_handler = fsp_process_byte;
1052 psmouse->disconnect = fsp_disconnect;
1053 psmouse->reconnect = fsp_reconnect;
1054 psmouse->cleanup = fsp_reset;
1055 psmouse->pktsize = 4;
1057 error = fsp_activate_protocol(psmouse);
1058 if (error)
1059 goto err_out;
1061 /* Set up various supported input event bits */
1062 error = fsp_set_input_params(psmouse);
1063 if (error)
1064 goto err_out;
1066 error = sysfs_create_group(&psmouse->ps2dev.serio->dev.kobj,
1067 &fsp_attribute_group);
1068 if (error) {
1069 psmouse_err(psmouse,
1070 "Failed to create sysfs attributes (%d)", error);
1071 goto err_out;
1074 return 0;
1076 err_out:
1077 kfree(psmouse->private);
1078 psmouse->private = NULL;
1079 return error;