summaryrefslogtreecommitdiff
path: root/drivers/input/keyboard/rpmsg-keys.c
blob: f6012e87f9a632e4b4593f3402251720d7b43297 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
 * Copyright 2017 NXP
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 */

#include <linux/err.h>
#include <linux/slab.h>
#include <linux/imx_rpmsg.h>
#include <linux/input.h>
#include <linux/interrupt.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/platform_device.h>
#include <linux/pm_qos.h>
#include <linux/rpmsg.h>
#include <linux/uaccess.h>
#include <linux/virtio.h>

#define RPMSG_TIMEOUT 1000

enum key_cmd_type {
	KEY_RPMSG_SETUP,
	KEY_RPMSG_REPLY,
	KEY_RPMSG_NOTIFY,
};

enum keys_type {
	KEY_PRESS = 1,
	KEY_RELEASE,
	KEY_BOTH,
};

struct key_rpmsg_data {
	struct imx_rpmsg_head header;
	u8 key_index;
	union {
		u8 event;
		u8 retcode;
	};
	u8 wakeup;
} __attribute__((packed));

struct rpmsg_keys_button {
	unsigned int code;
	enum keys_type type;
	int wakeup;
	struct input_dev *input;
};

struct rpmsg_keys_drvdata {
	struct input_dev *input;
	struct rpmsg_device *rpdev;
	struct device *dev;
	struct key_rpmsg_data *msg;
	bool ack;
	struct pm_qos_request pm_qos_req;
	struct delayed_work keysetup_work;
	struct completion cmd_complete;
	int nbuttons;
	struct rpmsg_keys_button buttons[0];
};

static struct rpmsg_keys_drvdata *keys_rpmsg;

static int key_send_message(struct key_rpmsg_data *msg,
			struct rpmsg_keys_drvdata *info, bool ack)
{
	int err;

	if (!info->rpdev) {
		dev_dbg(info->dev,
			"rpmsg channel not ready, m4 image ready?\n");
		return -EINVAL;
	}

	pm_qos_add_request(&info->pm_qos_req,
			PM_QOS_CPU_DMA_LATENCY, 0);

	if (ack) {
		info->ack = true;
		reinit_completion(&info->cmd_complete);
	}

	err = rpmsg_send(info->rpdev->ept, (void *)msg,
			    sizeof(struct key_rpmsg_data));
	if (err) {
		dev_err(&info->rpdev->dev, "rpmsg_send failed: %d\n", err);
		goto err_out;
	}

	if (ack) {
		err = wait_for_completion_timeout(&info->cmd_complete,
					msecs_to_jiffies(RPMSG_TIMEOUT));
		if (!err) {
			dev_err(&info->rpdev->dev, "rpmsg_send timeout!\n");
			err = -ETIMEDOUT;
			goto err_out;
		}

		if (info->msg->retcode != 0) {
			dev_err(&info->rpdev->dev, "rpmsg not ack %d!\n",
				info->msg->retcode);
			err = -EINVAL;
			goto err_out;
		}

		err = 0;
	}

err_out:
	info->ack = true;
	pm_qos_remove_request(&info->pm_qos_req);

	return err;
}

static int keys_rpmsg_cb(struct rpmsg_device *rpdev,
	void *data, int len, void *priv, u32 src)
{
	struct key_rpmsg_data *msg = (struct key_rpmsg_data *)data;

	if (msg->header.type == KEY_RPMSG_REPLY) {
		keys_rpmsg->msg = msg;
		complete(&keys_rpmsg->cmd_complete);
		return 0;
	} else if (msg->header.type == KEY_RPMSG_NOTIFY) {
		keys_rpmsg->msg = msg;
		keys_rpmsg->ack = false;
	} else
		dev_err(&keys_rpmsg->rpdev->dev, "wrong command type!\n");

	input_event(keys_rpmsg->input, EV_KEY, msg->key_index, msg->event);
	input_sync(keys_rpmsg->input);

	return 0;
}

static void keys_init_handler(struct work_struct *work)
{
	struct key_rpmsg_data msg;
	int i;

	/* setup keys */
	for (i = 0; i < keys_rpmsg->nbuttons; i++) {
		struct rpmsg_keys_button *button = &keys_rpmsg->buttons[i];

		msg.header.cate = IMX_RPMSG_KEY;
		msg.header.major = IMX_RMPSG_MAJOR;
		msg.header.minor = IMX_RMPSG_MINOR;
		msg.header.type = KEY_RPMSG_SETUP;
		msg.header.cmd = 0;
		msg.key_index = button->code;
		msg.wakeup = button->wakeup;
		msg.event = button->type;
		if (key_send_message(&msg, keys_rpmsg, true))
			dev_err(&keys_rpmsg->rpdev->dev,
				"key %d setup failed!\n", button->code);
	}
}

static int keys_rpmsg_probe(struct rpmsg_device *rpdev)
{
	keys_rpmsg->rpdev = rpdev;

	dev_info(&rpdev->dev, "new channel: 0x%x -> 0x%x!\n",
			rpdev->src, rpdev->dst);

	init_completion(&keys_rpmsg->cmd_complete);

	INIT_DELAYED_WORK(&keys_rpmsg->keysetup_work,
				keys_init_handler);
	schedule_delayed_work(&keys_rpmsg->keysetup_work,
			msecs_to_jiffies(100));

	return 0;
}

static struct rpmsg_device_id keys_rpmsg_id_table[] = {
	{ .name	= "rpmsg-keypad-channel" },
	{ },
};

static struct rpmsg_driver keys_rpmsg_driver = {
	.drv.name	= "key_rpmsg",
	.drv.owner	= THIS_MODULE,
	.id_table	= keys_rpmsg_id_table,
	.probe		= keys_rpmsg_probe,
	.callback	= keys_rpmsg_cb,
};

static struct rpmsg_keys_drvdata *
rpmsg_keys_get_devtree_pdata(struct device *dev)
{
	struct device_node *node, *pp;
	struct rpmsg_keys_drvdata *ddata;
	struct rpmsg_keys_button *button;
	int nbuttons;
	int i;

	node = dev->of_node;
	if (!node)
		return ERR_PTR(-ENODEV);

	nbuttons = of_get_child_count(node);
	if (nbuttons == 0)
		return ERR_PTR(-ENODEV);

	ddata = devm_kzalloc(dev,
			     sizeof(*ddata) + nbuttons *
			     sizeof(struct rpmsg_keys_button),
			     GFP_KERNEL);
	if (!ddata)
		return ERR_PTR(-ENOMEM);

	ddata->nbuttons = nbuttons;

	i = 0;
	for_each_child_of_node(node, pp) {
		button = &ddata->buttons[i++];

		if (of_property_read_u32(pp, "linux,code", &button->code)) {
			dev_err(dev, "Button without keycode: 0x%x\n",
				button->code);
			return ERR_PTR(-EINVAL);
		}

		button->wakeup = !!of_get_property(pp, "rpmsg-key,wakeup",
							NULL);
		button->type = KEY_BOTH;
	}

	return ddata;
}

static int rpmsg_keys_probe(struct platform_device *pdev)
{
	struct device *dev = &pdev->dev;
	struct rpmsg_keys_drvdata *ddata;
	int i, error;
	struct input_dev *input;

	ddata = rpmsg_keys_get_devtree_pdata(dev);
	if (IS_ERR(ddata))
		return PTR_ERR(ddata);

	input = devm_input_allocate_device(dev);
	if (!input) {
		dev_err(dev, "failed to allocate input device\n");
		return -ENOMEM;
	}

	ddata->input = input;

	keys_rpmsg = ddata;
	platform_set_drvdata(pdev, ddata);

	input->name = pdev->name;
	input->phys = "rpmsg-keys/input0";
	input->dev.parent = &pdev->dev;

	input->id.bustype = BUS_HOST;

	for (i = 0; i < ddata->nbuttons; i++) {
		struct rpmsg_keys_button *button = &ddata->buttons[i];

		input_set_capability(input, EV_KEY, button->code);
	}

	error = input_register_device(input);
	if (error) {
		dev_err(dev, "Unable to register input device, error: %d\n",
			error);
		goto err_out;
	}

	return register_rpmsg_driver(&keys_rpmsg_driver);
err_out:
	return error;
}

static const struct of_device_id rpmsg_keys_of_match[] = {
	{ .compatible = "fsl,rpmsg-keys", },
	{ },
};

MODULE_DEVICE_TABLE(of, rpmsg_keys_of_match);

static struct platform_driver rpmsg_keys_device_driver = {
	.probe		= rpmsg_keys_probe,
	.driver		=  {
		.name	= "rpmsg-keys",
		.of_match_table = of_match_ptr(rpmsg_keys_of_match)
	}
};

module_platform_driver(rpmsg_keys_device_driver);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Robin Gong <yibin.gong@nxp.com>");
MODULE_DESCRIPTION("Keyboard driver based on rpmsg");