gpio: rcar: Fix runtime PM imbalance on error
[linux/fpc-iii.git] / drivers / misc / pvpanic.c
bloba6e1a8983e1fcc6201f50140672babe34a9613c7
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Pvpanic Device Support
5 * Copyright (C) 2013 Fujitsu.
6 * Copyright (C) 2018 ZTE.
7 */
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 #include <linux/acpi.h>
12 #include <linux/kernel.h>
13 #include <linux/kexec.h>
14 #include <linux/module.h>
15 #include <linux/of.h>
16 #include <linux/of_address.h>
17 #include <linux/platform_device.h>
18 #include <linux/types.h>
19 #include <uapi/misc/pvpanic.h>
21 static void __iomem *base;
23 MODULE_AUTHOR("Hu Tao <hutao@cn.fujitsu.com>");
24 MODULE_DESCRIPTION("pvpanic device driver");
25 MODULE_LICENSE("GPL");
27 static void
28 pvpanic_send_event(unsigned int event)
30 iowrite8(event, base);
33 static int
34 pvpanic_panic_notify(struct notifier_block *nb, unsigned long code,
35 void *unused)
37 unsigned int event = PVPANIC_PANICKED;
39 if (kexec_crash_loaded())
40 event = PVPANIC_CRASH_LOADED;
42 pvpanic_send_event(event);
44 return NOTIFY_DONE;
47 static struct notifier_block pvpanic_panic_nb = {
48 .notifier_call = pvpanic_panic_notify,
49 .priority = 1, /* let this called before broken drm_fb_helper */
52 #ifdef CONFIG_ACPI
53 static int pvpanic_add(struct acpi_device *device);
54 static int pvpanic_remove(struct acpi_device *device);
56 static const struct acpi_device_id pvpanic_device_ids[] = {
57 { "QEMU0001", 0 },
58 { "", 0 }
60 MODULE_DEVICE_TABLE(acpi, pvpanic_device_ids);
62 static struct acpi_driver pvpanic_driver = {
63 .name = "pvpanic",
64 .class = "QEMU",
65 .ids = pvpanic_device_ids,
66 .ops = {
67 .add = pvpanic_add,
68 .remove = pvpanic_remove,
70 .owner = THIS_MODULE,
73 static acpi_status
74 pvpanic_walk_resources(struct acpi_resource *res, void *context)
76 struct resource r;
78 if (acpi_dev_resource_io(res, &r)) {
79 #ifdef CONFIG_HAS_IOPORT_MAP
80 base = ioport_map(r.start, resource_size(&r));
81 return AE_OK;
82 #else
83 return AE_ERROR;
84 #endif
85 } else if (acpi_dev_resource_memory(res, &r)) {
86 base = ioremap(r.start, resource_size(&r));
87 return AE_OK;
90 return AE_ERROR;
93 static int pvpanic_add(struct acpi_device *device)
95 int ret;
97 ret = acpi_bus_get_status(device);
98 if (ret < 0)
99 return ret;
101 if (!device->status.enabled || !device->status.functional)
102 return -ENODEV;
104 acpi_walk_resources(device->handle, METHOD_NAME__CRS,
105 pvpanic_walk_resources, NULL);
107 if (!base)
108 return -ENODEV;
110 atomic_notifier_chain_register(&panic_notifier_list,
111 &pvpanic_panic_nb);
113 return 0;
116 static int pvpanic_remove(struct acpi_device *device)
119 atomic_notifier_chain_unregister(&panic_notifier_list,
120 &pvpanic_panic_nb);
121 iounmap(base);
123 return 0;
126 static int pvpanic_register_acpi_driver(void)
128 return acpi_bus_register_driver(&pvpanic_driver);
131 static void pvpanic_unregister_acpi_driver(void)
133 acpi_bus_unregister_driver(&pvpanic_driver);
135 #else
136 static int pvpanic_register_acpi_driver(void)
138 return -ENODEV;
141 static void pvpanic_unregister_acpi_driver(void) {}
142 #endif
144 static int pvpanic_mmio_probe(struct platform_device *pdev)
146 struct resource *mem;
148 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
149 if (!mem)
150 return -EINVAL;
152 base = devm_ioremap_resource(&pdev->dev, mem);
153 if (IS_ERR(base))
154 return PTR_ERR(base);
156 atomic_notifier_chain_register(&panic_notifier_list,
157 &pvpanic_panic_nb);
159 return 0;
162 static int pvpanic_mmio_remove(struct platform_device *pdev)
165 atomic_notifier_chain_unregister(&panic_notifier_list,
166 &pvpanic_panic_nb);
168 return 0;
171 static const struct of_device_id pvpanic_mmio_match[] = {
172 { .compatible = "qemu,pvpanic-mmio", },
176 static struct platform_driver pvpanic_mmio_driver = {
177 .driver = {
178 .name = "pvpanic-mmio",
179 .of_match_table = pvpanic_mmio_match,
181 .probe = pvpanic_mmio_probe,
182 .remove = pvpanic_mmio_remove,
185 static int __init pvpanic_mmio_init(void)
187 if (acpi_disabled)
188 return platform_driver_register(&pvpanic_mmio_driver);
189 else
190 return pvpanic_register_acpi_driver();
193 static void __exit pvpanic_mmio_exit(void)
195 if (acpi_disabled)
196 platform_driver_unregister(&pvpanic_mmio_driver);
197 else
198 pvpanic_unregister_acpi_driver();
201 module_init(pvpanic_mmio_init);
202 module_exit(pvpanic_mmio_exit);