summaryrefslogtreecommitdiff
path: root/drivers/input/keyboard/pf1550_onkey.c
blob: 2ba0d155ef9937342983b636bda15a12ba550cae (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
/*
 * Driver for the PF1550 ON_KEY
 * Copyright (C) 2016 Freescale Semiconductor, Inc. All Rights Reserved.
 *
 * The code contained herein is licensed under the GNU General Public
 * License. You may obtain a copy of the GNU General Public License
 * Version 2 or later at the following locations:
 *
 * http://www.opensource.org/licenses/gpl-license.html
 * http://www.gnu.org/copyleft/gpl.html
 */

#include <linux/device.h>
#include <linux/err.h>
#include <linux/init.h>
#include <linux/input.h>
#include <linux/interrupt.h>
#include <linux/io.h>
#include <linux/jiffies.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/mfd/pf1550.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/platform_device.h>
#include <linux/regmap.h>

struct onkey_drv_data {
	struct device *dev;
	struct pf1550_dev *pf1550;
	int irq;
	int keycode;
	int wakeup;
	struct input_dev *input;
};

static struct pf1550_irq_info pf1550_onkey_irqs[] = {
	{ PF1550_ONKEY_IRQ_PUSHI,	"release" },
	{ PF1550_ONKEY_IRQ_1SI,		"1S" },
	{ PF1550_ONKEY_IRQ_2SI,		"2S" },
	{ PF1550_ONKEY_IRQ_3SI,		"3S" },
	{ PF1550_ONKEY_IRQ_4SI,		"4S" },
	{ PF1550_ONKEY_IRQ_8SI,		"8S" },
};

static irqreturn_t pf1550_onkey_irq_handler(int irq, void *data)
{
	struct onkey_drv_data *onkey = data;
	int i, state, irq_type = -1;

	onkey->irq = irq;

	for (i = 0; i < ARRAY_SIZE(pf1550_onkey_irqs); i++)
		if (onkey->irq == pf1550_onkey_irqs[i].virq)
			irq_type = pf1550_onkey_irqs[i].irq;
	switch (irq_type) {
	case PF1550_ONKEY_IRQ_PUSHI:
		state = 0;
		break;
	case PF1550_ONKEY_IRQ_1SI:
	case PF1550_ONKEY_IRQ_2SI:
	case PF1550_ONKEY_IRQ_3SI:
	case PF1550_ONKEY_IRQ_4SI:
	case PF1550_ONKEY_IRQ_8SI:
		state = 1;
		break;
	default:
		dev_err(onkey->dev, "onkey interrupt: irq %d occurred\n",
			irq_type);
		return IRQ_HANDLED;
	}

	input_event(onkey->input, EV_KEY, onkey->keycode, state);
	input_sync(onkey->input);

	return IRQ_HANDLED;
}

static int pf1550_onkey_probe(struct platform_device *pdev)
{
	struct onkey_drv_data *onkey;
	struct input_dev *input = NULL;
	struct device_node *np = pdev->dev.of_node;
	struct pf1550_dev *pf1550 = dev_get_drvdata(pdev->dev.parent);
	int i, error;

	if (!np)
		return -ENODEV;

	onkey = devm_kzalloc(&pdev->dev, sizeof(*onkey), GFP_KERNEL);
	if (!onkey)
		return -ENOMEM;

	if (of_property_read_u32(np, "linux,keycode", &onkey->keycode)) {
		onkey->keycode = KEY_POWER;
		dev_warn(&pdev->dev, "KEY_POWER without setting in dts\n");
	}

	onkey->wakeup = of_property_read_bool(np, "wakeup");

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

	input->name = pdev->name;
	input->phys = "pf1550-onkey/input0";
	input->id.bustype = BUS_HOST;

	input_set_capability(input, EV_KEY, onkey->keycode);

	for (i = 0; i < ARRAY_SIZE(pf1550_onkey_irqs); i++) {
		struct pf1550_irq_info *onkey_irq =
						&pf1550_onkey_irqs[i];
		unsigned int virq = 0;

		virq = regmap_irq_get_virq(pf1550->irq_data_onkey,
					onkey_irq->irq);
		if (!virq)
			return -EINVAL;

		onkey_irq->virq = virq;

		error = devm_request_threaded_irq(&pdev->dev, virq, NULL,
					pf1550_onkey_irq_handler,
					IRQF_NO_SUSPEND,
					onkey_irq->name, onkey);
		if (error) {
			dev_err(&pdev->dev,
				"failed: irq request (IRQ: %d, error :%d)\n",
				onkey_irq->irq, error);
			return error;
		}
	}

	error = input_register_device(input);
	if (error < 0) {
		dev_err(&pdev->dev, "failed to register input device\n");
		input_free_device(input);
		return error;
	}

	onkey->input = input;
	onkey->pf1550 = pf1550;
	platform_set_drvdata(pdev, onkey);

	device_init_wakeup(&pdev->dev, onkey->wakeup);

	return 0;
}

static int pf1550_onkey_suspend(struct device *dev)
{
	struct platform_device *pdev = to_platform_device(dev);
	struct onkey_drv_data *onkey = platform_get_drvdata(pdev);

	if (!device_may_wakeup(&pdev->dev))
		regmap_write(onkey->pf1550->regmap,
			     PF1550_PMIC_REG_ONKEY_INT_MASK0,
			     ONKEY_IRQ_PUSHI | ONKEY_IRQ_1SI | ONKEY_IRQ_2SI |
			     ONKEY_IRQ_3SI | ONKEY_IRQ_4SI | ONKEY_IRQ_8SI);
	else
		enable_irq_wake(onkey->pf1550->irq);

	return 0;
}

static int pf1550_onkey_resume(struct device *dev)
{
	struct platform_device *pdev = to_platform_device(dev);
	struct onkey_drv_data *onkey = platform_get_drvdata(pdev);

	if (!device_may_wakeup(&pdev->dev))
		regmap_write(onkey->pf1550->regmap,
			     PF1550_PMIC_REG_ONKEY_INT_MASK0,
			     ~(ONKEY_IRQ_PUSHI | ONKEY_IRQ_1SI | ONKEY_IRQ_2SI |
			     ONKEY_IRQ_3SI | ONKEY_IRQ_4SI | ONKEY_IRQ_8SI));
	else
		disable_irq_wake(onkey->pf1550->irq);

	return 0;
}

static const struct of_device_id pf1550_onkey_ids[] = {
	{ .compatible = "fsl,pf1550-onkey" },
	{ /* sentinel */ }
};
MODULE_DEVICE_TABLE(of, pf1550_onkey_ids);

static SIMPLE_DEV_PM_OPS(pf1550_onkey_pm_ops, pf1550_onkey_suspend,
				pf1550_onkey_resume);

static struct platform_driver pf1550_onkey_driver = {
	.driver = {
		.name = "pf1550-onkey",
		.pm     = &pf1550_onkey_pm_ops,
		.of_match_table = pf1550_onkey_ids,
	},
	.probe = pf1550_onkey_probe,
};
module_platform_driver(pf1550_onkey_driver);

MODULE_AUTHOR("Freescale Semiconductor");
MODULE_DESCRIPTION(" PF1550 onkey Driver");
MODULE_LICENSE("GPL");