18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Hisilicon PMIC powerkey driver
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Copyright (C) 2013 Hisilicon Ltd.
58c2ecf20Sopenharmony_ci * Copyright (C) 2015, 2016 Linaro Ltd.
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * This file is subject to the terms and conditions of the GNU General
88c2ecf20Sopenharmony_ci * Public License. See the file "COPYING" in the main directory of this
98c2ecf20Sopenharmony_ci * archive for more details.
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci * This program is distributed in the hope that it will be useful,
128c2ecf20Sopenharmony_ci * but WITHOUT ANY WARRANTY; without even the implied warranty of
138c2ecf20Sopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
148c2ecf20Sopenharmony_ci * GNU General Public License for more details.
158c2ecf20Sopenharmony_ci */
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
188c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
198c2ecf20Sopenharmony_ci#include <linux/reboot.h>
208c2ecf20Sopenharmony_ci#include <linux/kernel.h>
218c2ecf20Sopenharmony_ci#include <linux/module.h>
228c2ecf20Sopenharmony_ci#include <linux/of_irq.h>
238c2ecf20Sopenharmony_ci#include <linux/input.h>
248c2ecf20Sopenharmony_ci#include <linux/slab.h>
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci/* the held interrupt will trigger after 4 seconds */
278c2ecf20Sopenharmony_ci#define MAX_HELD_TIME	(4 * MSEC_PER_SEC)
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_cistatic irqreturn_t hi65xx_power_press_isr(int irq, void *q)
308c2ecf20Sopenharmony_ci{
318c2ecf20Sopenharmony_ci	struct input_dev *input = q;
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci	pm_wakeup_event(input->dev.parent, MAX_HELD_TIME);
348c2ecf20Sopenharmony_ci	input_report_key(input, KEY_POWER, 1);
358c2ecf20Sopenharmony_ci	input_sync(input);
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
388c2ecf20Sopenharmony_ci}
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_cistatic irqreturn_t hi65xx_power_release_isr(int irq, void *q)
418c2ecf20Sopenharmony_ci{
428c2ecf20Sopenharmony_ci	struct input_dev *input = q;
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci	pm_wakeup_event(input->dev.parent, MAX_HELD_TIME);
458c2ecf20Sopenharmony_ci	input_report_key(input, KEY_POWER, 0);
468c2ecf20Sopenharmony_ci	input_sync(input);
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
498c2ecf20Sopenharmony_ci}
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_cistatic irqreturn_t hi65xx_restart_toggle_isr(int irq, void *q)
528c2ecf20Sopenharmony_ci{
538c2ecf20Sopenharmony_ci	struct input_dev *input = q;
548c2ecf20Sopenharmony_ci	int value = test_bit(KEY_RESTART, input->key);
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci	pm_wakeup_event(input->dev.parent, MAX_HELD_TIME);
578c2ecf20Sopenharmony_ci	input_report_key(input, KEY_RESTART, !value);
588c2ecf20Sopenharmony_ci	input_sync(input);
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
618c2ecf20Sopenharmony_ci}
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_cistatic const struct {
648c2ecf20Sopenharmony_ci	const char *name;
658c2ecf20Sopenharmony_ci	irqreturn_t (*handler)(int irq, void *q);
668c2ecf20Sopenharmony_ci} hi65xx_irq_info[] = {
678c2ecf20Sopenharmony_ci	{ "down", hi65xx_power_press_isr },
688c2ecf20Sopenharmony_ci	{ "up", hi65xx_power_release_isr },
698c2ecf20Sopenharmony_ci	{ "hold 4s", hi65xx_restart_toggle_isr },
708c2ecf20Sopenharmony_ci};
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_cistatic int hi65xx_powerkey_probe(struct platform_device *pdev)
738c2ecf20Sopenharmony_ci{
748c2ecf20Sopenharmony_ci	struct device *dev = &pdev->dev;
758c2ecf20Sopenharmony_ci	struct input_dev *input;
768c2ecf20Sopenharmony_ci	int irq, i, error;
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci	input = devm_input_allocate_device(dev);
798c2ecf20Sopenharmony_ci	if (!input) {
808c2ecf20Sopenharmony_ci		dev_err(dev, "failed to allocate input device\n");
818c2ecf20Sopenharmony_ci		return -ENOMEM;
828c2ecf20Sopenharmony_ci	}
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci	input->phys = "hisi_on/input0";
858c2ecf20Sopenharmony_ci	input->name = "HISI 65xx PowerOn Key";
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci	input_set_capability(input, EV_KEY, KEY_POWER);
888c2ecf20Sopenharmony_ci	input_set_capability(input, EV_KEY, KEY_RESTART);
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(hi65xx_irq_info); i++) {
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci		irq = platform_get_irq_byname(pdev, hi65xx_irq_info[i].name);
938c2ecf20Sopenharmony_ci		if (irq < 0)
948c2ecf20Sopenharmony_ci			return irq;
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci		error = devm_request_any_context_irq(dev, irq,
978c2ecf20Sopenharmony_ci						     hi65xx_irq_info[i].handler,
988c2ecf20Sopenharmony_ci						     IRQF_ONESHOT,
998c2ecf20Sopenharmony_ci						     hi65xx_irq_info[i].name,
1008c2ecf20Sopenharmony_ci						     input);
1018c2ecf20Sopenharmony_ci		if (error < 0) {
1028c2ecf20Sopenharmony_ci			dev_err(dev, "couldn't request irq %s: %d\n",
1038c2ecf20Sopenharmony_ci				hi65xx_irq_info[i].name, error);
1048c2ecf20Sopenharmony_ci			return error;
1058c2ecf20Sopenharmony_ci		}
1068c2ecf20Sopenharmony_ci	}
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	error = input_register_device(input);
1098c2ecf20Sopenharmony_ci	if (error) {
1108c2ecf20Sopenharmony_ci		dev_err(dev, "failed to register input device: %d\n", error);
1118c2ecf20Sopenharmony_ci		return error;
1128c2ecf20Sopenharmony_ci	}
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci	device_init_wakeup(dev, 1);
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci	return 0;
1178c2ecf20Sopenharmony_ci}
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_cistatic struct platform_driver hi65xx_powerkey_driver = {
1208c2ecf20Sopenharmony_ci	.driver = {
1218c2ecf20Sopenharmony_ci		.name = "hi65xx-powerkey",
1228c2ecf20Sopenharmony_ci	},
1238c2ecf20Sopenharmony_ci	.probe = hi65xx_powerkey_probe,
1248c2ecf20Sopenharmony_ci};
1258c2ecf20Sopenharmony_cimodule_platform_driver(hi65xx_powerkey_driver);
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ciMODULE_AUTHOR("Zhiliang Xue <xuezhiliang@huawei.com");
1288c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Hisi PMIC Power key driver");
1298c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
130