blob: 0922dd3a08d3337c96165bf3f1b6b1c1ddb92307 [file] [log] [blame]
Marc Dietrich162c7d82011-09-27 19:00:40 +02001/*
2 * nvec_ps2: mouse driver for a NVIDIA compliant embedded controller
3 *
4 * Copyright (C) 2011 The AC100 Kernel Team <ac100@lists.launchpad.net>
5 *
6 * Authors: Pierre-Hugues Husson <phhusson@free.fr>
7 * Ilya Petrov <ilya.muromec@gmail.com>
8 * Marc Dietrich <marvin24@gmx.de>
9 *
10 * This file is subject to the terms and conditions of the GNU General Public
11 * License. See the file "COPYING" in the main directory of this archive
12 * for more details.
13 *
14 */
15
Julian Andres Klode79740352011-09-27 19:00:39 +020016#include <linux/module.h>
Marc Dietrich32890b92011-05-19 16:34:42 +020017#include <linux/slab.h>
18#include <linux/serio.h>
19#include <linux/delay.h>
Marc Dietrichf686e9a2011-08-24 20:23:07 +020020#include <linux/platform_device.h>
Marc Dietrich162c7d82011-09-27 19:00:40 +020021
Marc Dietrich32890b92011-05-19 16:34:42 +020022#include "nvec.h"
23
Marc Dietrich93eff832013-01-27 17:43:43 +010024#define PACKET_SIZE 6
Marc Dietrich32890b92011-05-19 16:34:42 +020025
Marc Dietrich93eff832013-01-27 17:43:43 +010026#define ENABLE_MOUSE 0xf4
27#define DISABLE_MOUSE 0xf5
28#define PSMOUSE_RST 0xff
Marc Dietrich85a90522013-01-27 17:43:42 +010029
Marc Dietrich0eedab72011-12-26 17:57:35 +010030#ifdef NVEC_PS2_DEBUG
31#define NVEC_PHD(str, buf, len) \
32 print_hex_dump(KERN_DEBUG, str, DUMP_PREFIX_NONE, \
33 16, 1, buf, len, false)
34#else
35#define NVEC_PHD(str, buf, len)
36#endif
37
Marc Dietrich93eff832013-01-27 17:43:43 +010038enum ps2_subcmds {
39 SEND_COMMAND = 1,
40 RECEIVE_N,
41 AUTO_RECEIVE_N,
42 CANCEL_AUTO_RECEIVE,
43};
Marc Dietrich162c7d82011-09-27 19:00:40 +020044
45struct nvec_ps2 {
Marc Dietrich32890b92011-05-19 16:34:42 +020046 struct serio *ser_dev;
47 struct notifier_block notifier;
48 struct nvec_chip *nvec;
49};
50
51static struct nvec_ps2 ps2_dev;
52
53static int ps2_startstreaming(struct serio *ser_dev)
54{
Marc Dietrich93eff832013-01-27 17:43:43 +010055 unsigned char buf[] = { NVEC_PS2, AUTO_RECEIVE_N, PACKET_SIZE };
Pawel Lebiodafa799612014-07-03 21:15:57 +020056
Julian Andres Klodeff006d12011-09-27 19:01:02 +020057 return nvec_write_async(ps2_dev.nvec, buf, sizeof(buf));
Marc Dietrich32890b92011-05-19 16:34:42 +020058}
59
60static void ps2_stopstreaming(struct serio *ser_dev)
61{
Marc Dietrich93eff832013-01-27 17:43:43 +010062 unsigned char buf[] = { NVEC_PS2, CANCEL_AUTO_RECEIVE };
Pawel Lebiodafa799612014-07-03 21:15:57 +020063
Marc Dietrich32890b92011-05-19 16:34:42 +020064 nvec_write_async(ps2_dev.nvec, buf, sizeof(buf));
65}
66
Marc Dietrich32890b92011-05-19 16:34:42 +020067static int ps2_sendcommand(struct serio *ser_dev, unsigned char cmd)
68{
Marc Dietrich93eff832013-01-27 17:43:43 +010069 unsigned char buf[] = { NVEC_PS2, SEND_COMMAND, ENABLE_MOUSE, 1 };
Marc Dietrich32890b92011-05-19 16:34:42 +020070
71 buf[2] = cmd & 0xff;
72
73 dev_dbg(&ser_dev->dev, "Sending ps2 cmd %02x\n", cmd);
Julian Andres Klodeff006d12011-09-27 19:01:02 +020074 return nvec_write_async(ps2_dev.nvec, buf, sizeof(buf));
Marc Dietrich32890b92011-05-19 16:34:42 +020075}
76
77static int nvec_ps2_notifier(struct notifier_block *nb,
Marc Dietrich162c7d82011-09-27 19:00:40 +020078 unsigned long event_type, void *data)
Marc Dietrich32890b92011-05-19 16:34:42 +020079{
80 int i;
81 unsigned char *msg = (unsigned char *)data;
82
83 switch (event_type) {
Marc Dietrich162c7d82011-09-27 19:00:40 +020084 case NVEC_PS2_EVT:
Julian Andres Klode1e46e622011-09-27 19:01:03 +020085 for (i = 0; i < msg[1]; i++)
86 serio_interrupt(ps2_dev.ser_dev, msg[2 + i], 0);
Marc Dietrich0eedab72011-12-26 17:57:35 +010087 NVEC_PHD("ps/2 mouse event: ", &msg[2], msg[1]);
Marc Dietrich162c7d82011-09-27 19:00:40 +020088 return NOTIFY_STOP;
Marc Dietrich32890b92011-05-19 16:34:42 +020089
Marc Dietrich162c7d82011-09-27 19:00:40 +020090 case NVEC_PS2:
Marc Dietrich0eedab72011-12-26 17:57:35 +010091 if (msg[2] == 1) {
Marc Dietrich162c7d82011-09-27 19:00:40 +020092 for (i = 0; i < (msg[1] - 2); i++)
93 serio_interrupt(ps2_dev.ser_dev, msg[i + 4], 0);
Marc Dietrich0eedab72011-12-26 17:57:35 +010094 NVEC_PHD("ps/2 mouse reply: ", &msg[4], msg[1] - 2);
Marc Dietrich162c7d82011-09-27 19:00:40 +020095 }
Marc Dietrich32890b92011-05-19 16:34:42 +020096
Marc Dietrich0eedab72011-12-26 17:57:35 +010097 else if (msg[1] != 2) /* !ack */
98 NVEC_PHD("unhandled mouse event: ", msg, msg[1] + 2);
Marc Dietrich162c7d82011-09-27 19:00:40 +020099 return NOTIFY_STOP;
Marc Dietrich32890b92011-05-19 16:34:42 +0200100 }
101
102 return NOTIFY_DONE;
103}
104
Bill Pemberton46620802012-11-19 13:22:07 -0500105static int nvec_mouse_probe(struct platform_device *pdev)
Marc Dietrich32890b92011-05-19 16:34:42 +0200106{
Marc Dietrichf686e9a2011-08-24 20:23:07 +0200107 struct nvec_chip *nvec = dev_get_drvdata(pdev->dev.parent);
Marc Dietrichf5e33522012-06-24 23:25:16 +0200108 struct serio *ser_dev;
Marc Dietrich93eff832013-01-27 17:43:43 +0100109 char mouse_reset[] = { NVEC_PS2, SEND_COMMAND, PSMOUSE_RST, 3 };
Marc Dietrichf5e33522012-06-24 23:25:16 +0200110
Himangi Saraogie534f3e2014-03-07 21:39:27 +0530111 ser_dev = devm_kzalloc(&pdev->dev, sizeof(struct serio), GFP_KERNEL);
Somya Anand4c42d972015-03-16 19:34:11 +0530112 if (!ser_dev)
Marc Dietrichf5e33522012-06-24 23:25:16 +0200113 return -ENOMEM;
Marc Dietrich32890b92011-05-19 16:34:42 +0200114
Marc Dietrich36b30d62011-12-26 17:57:37 +0100115 ser_dev->id.type = SERIO_PS_PSTHRU;
Marc Dietrich162c7d82011-09-27 19:00:40 +0200116 ser_dev->write = ps2_sendcommand;
Marc Dietrich34ba1432011-12-26 17:57:33 +0100117 ser_dev->start = ps2_startstreaming;
118 ser_dev->stop = ps2_stopstreaming;
Marc Dietrich32890b92011-05-19 16:34:42 +0200119
Marc Dietrich162c7d82011-09-27 19:00:40 +0200120 strlcpy(ser_dev->name, "nvec mouse", sizeof(ser_dev->name));
121 strlcpy(ser_dev->phys, "nvec", sizeof(ser_dev->phys));
Marc Dietrich32890b92011-05-19 16:34:42 +0200122
123 ps2_dev.ser_dev = ser_dev;
124 ps2_dev.notifier.notifier_call = nvec_ps2_notifier;
125 ps2_dev.nvec = nvec;
126 nvec_register_notifier(nvec, &ps2_dev.notifier, 0);
127
128 serio_register_port(ser_dev);
129
130 /* mouse reset */
Marc Dietrich93eff832013-01-27 17:43:43 +0100131 nvec_write_async(nvec, mouse_reset, sizeof(mouse_reset));
Marc Dietrich32890b92011-05-19 16:34:42 +0200132
133 return 0;
134}
Marc Dietrichf686e9a2011-08-24 20:23:07 +0200135
Bill Pemberton1a6a8a82012-11-19 13:26:41 -0500136static int nvec_mouse_remove(struct platform_device *pdev)
Marc Dietrich3cdde3a2012-06-24 23:25:21 +0200137{
Marc Dietrichc2b62f62013-04-29 23:14:52 +0200138 struct nvec_chip *nvec = dev_get_drvdata(pdev->dev.parent);
139
140 ps2_sendcommand(ps2_dev.ser_dev, DISABLE_MOUSE);
141 ps2_stopstreaming(ps2_dev.ser_dev);
142 nvec_unregister_notifier(nvec, &ps2_dev.notifier);
Marc Dietrich3cdde3a2012-06-24 23:25:21 +0200143 serio_unregister_port(ps2_dev.ser_dev);
144
145 return 0;
146}
147
Marc Dietrichebefae22012-06-24 23:25:19 +0200148#ifdef CONFIG_PM_SLEEP
149static int nvec_mouse_suspend(struct device *dev)
Marc Dietrichd1b53422011-12-26 17:57:32 +0100150{
Marc Dietricha5732982011-12-26 17:57:36 +0100151 /* disable mouse */
Marc Dietrich85a90522013-01-27 17:43:42 +0100152 ps2_sendcommand(ps2_dev.ser_dev, DISABLE_MOUSE);
Marc Dietricha5732982011-12-26 17:57:36 +0100153
Marc Dietrichd1b53422011-12-26 17:57:32 +0100154 /* send cancel autoreceive */
Marc Dietrich85a90522013-01-27 17:43:42 +0100155 ps2_stopstreaming(ps2_dev.ser_dev);
Marc Dietrichd1b53422011-12-26 17:57:32 +0100156
157 return 0;
158}
159
Marc Dietrichebefae22012-06-24 23:25:19 +0200160static int nvec_mouse_resume(struct device *dev)
Marc Dietrichd1b53422011-12-26 17:57:32 +0100161{
Marc Dietrich85a90522013-01-27 17:43:42 +0100162 /* start streaming */
Marc Dietrichd1b53422011-12-26 17:57:32 +0100163 ps2_startstreaming(ps2_dev.ser_dev);
164
Marc Dietricha5732982011-12-26 17:57:36 +0100165 /* enable mouse */
Marc Dietrich85a90522013-01-27 17:43:42 +0100166 ps2_sendcommand(ps2_dev.ser_dev, ENABLE_MOUSE);
Marc Dietricha5732982011-12-26 17:57:36 +0100167
Marc Dietrichd1b53422011-12-26 17:57:32 +0100168 return 0;
169}
Marc Dietrichebefae22012-06-24 23:25:19 +0200170#endif
171
Peng Fan716baa72015-06-16 23:13:21 +0800172static SIMPLE_DEV_PM_OPS(nvec_mouse_pm_ops, nvec_mouse_suspend,
173 nvec_mouse_resume);
Marc Dietrichd1b53422011-12-26 17:57:32 +0100174
Marc Dietrichf686e9a2011-08-24 20:23:07 +0200175static struct platform_driver nvec_mouse_driver = {
Marc Dietrich162c7d82011-09-27 19:00:40 +0200176 .probe = nvec_mouse_probe,
Bill Pemberton44b90a32012-11-19 13:20:55 -0500177 .remove = nvec_mouse_remove,
Marc Dietrich162c7d82011-09-27 19:00:40 +0200178 .driver = {
179 .name = "nvec-mouse",
Marc Dietrichebefae22012-06-24 23:25:19 +0200180 .pm = &nvec_mouse_pm_ops,
Marc Dietrichf686e9a2011-08-24 20:23:07 +0200181 },
182};
183
Marc Dietrich9891b1c2012-06-24 23:25:18 +0200184module_platform_driver(nvec_mouse_driver);
Marc Dietrich162c7d82011-09-27 19:00:40 +0200185
186MODULE_DESCRIPTION("NVEC mouse driver");
187MODULE_AUTHOR("Marc Dietrich <marvin24@gmx.de>");
Marc Dietrich99758de2013-04-29 23:14:50 +0200188MODULE_ALIAS("platform:nvec-mouse");
Marc Dietrich162c7d82011-09-27 19:00:40 +0200189MODULE_LICENSE("GPL");