summaryrefslogtreecommitdiff
path: root/arch/powerpc/platforms/cell/cbe_powerbutton.c
blob: bda589dfb0512b89d1dfa61d8ae92d4ace1f84d1 (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
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * driver for powerbutton on IBM cell blades
 *
 * (C) Copyright IBM Corp. 2005-2008
 *
 * Author: Christian Krafft <krafft@de.ibm.com>
 */

#include <linux/input.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <asm/pmi.h>
#include <asm/prom.h>

static struct input_dev *button_dev;
static struct platform_device *button_pdev;

static void cbe_powerbutton_handle_pmi(pmi_message_t pmi_msg)
{
	BUG_ON(pmi_msg.type != PMI_TYPE_POWER_BUTTON);

	input_report_key(button_dev, KEY_POWER, 1);
	input_sync(button_dev);
	input_report_key(button_dev, KEY_POWER, 0);
	input_sync(button_dev);
}

static struct pmi_handler cbe_pmi_handler = {
	.type			= PMI_TYPE_POWER_BUTTON,
	.handle_pmi_message	= cbe_powerbutton_handle_pmi,
};

static int __init cbe_powerbutton_init(void)
{
	int ret = 0;
	struct input_dev *dev;

	if (!of_machine_is_compatible("IBM,CBPLUS-1.0")) {
		printk(KERN_ERR "%s: Not a cell blade.\n", __func__);
		ret = -ENODEV;
		goto out;
	}

	dev = input_allocate_device();
	if (!dev) {
		ret = -ENOMEM;
		printk(KERN_ERR "%s: Not enough memory.\n", __func__);
		goto out;
	}

	set_bit(EV_KEY, dev->evbit);
	set_bit(KEY_POWER, dev->keybit);

	dev->name = "Power Button";
	dev->id.bustype = BUS_HOST;

	/* this makes the button look like an acpi power button
	 * no clue whether anyone relies on that though */
	dev->id.product = 0x02;
	dev->phys = "LNXPWRBN/button/input0";

	button_pdev = platform_device_register_simple("power_button", 0, NULL, 0);
	if (IS_ERR(button_pdev)) {
		ret = PTR_ERR(button_pdev);
		goto out_free_input;
	}

	dev->dev.parent = &button_pdev->dev;
	ret = input_register_device(dev);
	if (ret) {
		printk(KERN_ERR "%s: Failed to register device\n", __func__);
		goto out_free_pdev;
	}

	button_dev = dev;

	ret = pmi_register_handler(&cbe_pmi_handler);
	if (ret) {
		printk(KERN_ERR "%s: Failed to register with pmi.\n", __func__);
		goto out_free_pdev;
	}

	goto out;

out_free_pdev:
	platform_device_unregister(button_pdev);
out_free_input:
	input_free_device(dev);
out:
	return ret;
}

static void __exit cbe_powerbutton_exit(void)
{
	pmi_unregister_handler(&cbe_pmi_handler);
	platform_device_unregister(button_pdev);
	input_free_device(button_dev);
}

module_init(cbe_powerbutton_init);
module_exit(cbe_powerbutton_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Christian Krafft <krafft@de.ibm.com>");