1 // SPDX-License-Identifier: GPL-2.0
3 * Xilinx Zynq MPSoC Power Management
5 * Copyright (C) 2014-2019 Xilinx, Inc.
7 * Davorin Mista <davorin.mista@aggios.com>
8 * Jolly Shah <jollys@xilinx.com>
9 * Rajan Vaja <rajan.vaja@xilinx.com>
12 #include <linux/mailbox_client.h>
13 #include <linux/module.h>
15 #include <linux/platform_device.h>
16 #include <linux/reboot.h>
17 #include <linux/suspend.h>
19 #include <linux/firmware/xlnx-zynqmp.h>
20 #include <linux/firmware/xlnx-event-manager.h>
21 #include <linux/mailbox/zynqmp-ipi-message.h>
24 * struct zynqmp_pm_work_struct - Wrapper for struct work_struct
25 * @callback_work: Work structure
26 * @args: Callback arguments
28 struct zynqmp_pm_work_struct {
29 struct work_struct callback_work;
33 static struct zynqmp_pm_work_struct *zynqmp_pm_init_suspend_work;
34 static struct mbox_chan *rx_chan;
35 static bool event_registered;
37 enum pm_suspend_mode {
38 PM_SUSPEND_MODE_FIRST = 0,
39 PM_SUSPEND_MODE_STD = PM_SUSPEND_MODE_FIRST,
40 PM_SUSPEND_MODE_POWER_OFF,
43 #define PM_SUSPEND_MODE_FIRST PM_SUSPEND_MODE_STD
45 static const char *const suspend_modes[] = {
46 [PM_SUSPEND_MODE_STD] = "standard",
47 [PM_SUSPEND_MODE_POWER_OFF] = "power-off",
50 static enum pm_suspend_mode suspend_mode = PM_SUSPEND_MODE_STD;
52 static void zynqmp_pm_get_callback_data(u32 *buf)
54 zynqmp_pm_invoke_fn(GET_CALLBACK_DATA, 0, 0, 0, 0, buf);
57 static void suspend_event_callback(const u32 *payload, void *data)
59 /* First element is callback API ID, others are callback arguments */
60 if (work_pending(&zynqmp_pm_init_suspend_work->callback_work))
63 /* Copy callback arguments into work's structure */
64 memcpy(zynqmp_pm_init_suspend_work->args, &payload[1],
65 sizeof(zynqmp_pm_init_suspend_work->args));
67 queue_work(system_unbound_wq, &zynqmp_pm_init_suspend_work->callback_work);
70 static irqreturn_t zynqmp_pm_isr(int irq, void *data)
72 u32 payload[CB_PAYLOAD_SIZE];
74 zynqmp_pm_get_callback_data(payload);
76 /* First element is callback API ID, others are callback arguments */
77 if (payload[0] == PM_INIT_SUSPEND_CB) {
79 case SUSPEND_SYSTEM_SHUTDOWN:
80 orderly_poweroff(true);
82 case SUSPEND_POWER_REQUEST:
83 pm_suspend(PM_SUSPEND_MEM);
86 pr_err("%s Unsupported InitSuspendCb reason "
87 "code %d\n", __func__, payload[1]);
94 static void ipi_receive_callback(struct mbox_client *cl, void *data)
96 struct zynqmp_ipi_message *msg = (struct zynqmp_ipi_message *)data;
97 u32 payload[CB_PAYLOAD_SIZE];
100 memcpy(payload, msg->data, sizeof(msg->len));
101 /* First element is callback API ID, others are callback arguments */
102 if (payload[0] == PM_INIT_SUSPEND_CB) {
103 if (work_pending(&zynqmp_pm_init_suspend_work->callback_work))
106 /* Copy callback arguments into work's structure */
107 memcpy(zynqmp_pm_init_suspend_work->args, &payload[1],
108 sizeof(zynqmp_pm_init_suspend_work->args));
110 queue_work(system_unbound_wq,
111 &zynqmp_pm_init_suspend_work->callback_work);
113 /* Send NULL message to mbox controller to ack the message */
114 ret = mbox_send_message(rx_chan, NULL);
116 pr_err("IPI ack failed. Error %d\n", ret);
121 * zynqmp_pm_init_suspend_work_fn - Initialize suspend
122 * @work: Pointer to work_struct
124 * Bottom-half of PM callback IRQ handler.
126 static void zynqmp_pm_init_suspend_work_fn(struct work_struct *work)
128 struct zynqmp_pm_work_struct *pm_work =
129 container_of(work, struct zynqmp_pm_work_struct, callback_work);
131 if (pm_work->args[0] == SUSPEND_SYSTEM_SHUTDOWN) {
132 orderly_poweroff(true);
133 } else if (pm_work->args[0] == SUSPEND_POWER_REQUEST) {
134 pm_suspend(PM_SUSPEND_MEM);
136 pr_err("%s Unsupported InitSuspendCb reason code %d.\n",
137 __func__, pm_work->args[0]);
141 static ssize_t suspend_mode_show(struct device *dev,
142 struct device_attribute *attr, char *buf)
147 for (md = PM_SUSPEND_MODE_FIRST; md < ARRAY_SIZE(suspend_modes); md++)
148 if (suspend_modes[md]) {
149 if (md == suspend_mode)
150 s += sprintf(s, "[%s] ", suspend_modes[md]);
152 s += sprintf(s, "%s ", suspend_modes[md]);
155 /* Convert last space to newline */
161 static ssize_t suspend_mode_store(struct device *dev,
162 struct device_attribute *attr,
163 const char *buf, size_t count)
165 int md, ret = -EINVAL;
167 for (md = PM_SUSPEND_MODE_FIRST; md < ARRAY_SIZE(suspend_modes); md++)
168 if (suspend_modes[md] &&
169 sysfs_streq(suspend_modes[md], buf)) {
174 if (!ret && md != suspend_mode) {
175 ret = zynqmp_pm_set_suspend_mode(md);
180 return ret ? ret : count;
183 static DEVICE_ATTR_RW(suspend_mode);
185 static int zynqmp_pm_probe(struct platform_device *pdev)
189 struct mbox_client *client;
191 zynqmp_pm_get_api_version(&pm_api_version);
193 /* Check PM API version number */
194 if (pm_api_version < ZYNQMP_PM_VERSION)
198 * First try to use Xilinx Event Manager by registering suspend_event_callback
199 * for suspend/shutdown event.
200 * If xlnx_register_event() returns -EACCES (Xilinx Event Manager
201 * is not available to use) or -ENODEV(Xilinx Event Manager not compiled),
202 * then use ipi-mailbox or interrupt method.
204 ret = xlnx_register_event(PM_INIT_SUSPEND_CB, 0, 0, false,
205 suspend_event_callback, NULL);
207 zynqmp_pm_init_suspend_work = devm_kzalloc(&pdev->dev,
208 sizeof(struct zynqmp_pm_work_struct),
210 if (!zynqmp_pm_init_suspend_work) {
211 xlnx_unregister_event(PM_INIT_SUSPEND_CB, 0, 0,
212 suspend_event_callback, NULL);
215 event_registered = true;
217 INIT_WORK(&zynqmp_pm_init_suspend_work->callback_work,
218 zynqmp_pm_init_suspend_work_fn);
219 } else if (ret != -EACCES && ret != -ENODEV) {
220 dev_err(&pdev->dev, "Failed to Register with Xilinx Event manager %d\n", ret);
222 } else if (of_property_present(pdev->dev.of_node, "mboxes")) {
223 zynqmp_pm_init_suspend_work =
224 devm_kzalloc(&pdev->dev,
225 sizeof(struct zynqmp_pm_work_struct),
227 if (!zynqmp_pm_init_suspend_work)
230 INIT_WORK(&zynqmp_pm_init_suspend_work->callback_work,
231 zynqmp_pm_init_suspend_work_fn);
232 client = devm_kzalloc(&pdev->dev, sizeof(*client), GFP_KERNEL);
236 client->dev = &pdev->dev;
237 client->rx_callback = ipi_receive_callback;
239 rx_chan = mbox_request_channel_byname(client, "rx");
240 if (IS_ERR(rx_chan)) {
241 dev_err(&pdev->dev, "Failed to request rx channel\n");
242 return PTR_ERR(rx_chan);
244 } else if (of_property_present(pdev->dev.of_node, "interrupts")) {
245 irq = platform_get_irq(pdev, 0);
249 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
251 IRQF_NO_SUSPEND | IRQF_ONESHOT,
252 dev_name(&pdev->dev),
255 dev_err(&pdev->dev, "devm_request_threaded_irq '%d' "
256 "failed with %d\n", irq, ret);
260 dev_err(&pdev->dev, "Required property not found in DT node\n");
264 ret = sysfs_create_file(&pdev->dev.kobj, &dev_attr_suspend_mode.attr);
266 if (event_registered) {
267 xlnx_unregister_event(PM_INIT_SUSPEND_CB, 0, 0, suspend_event_callback,
269 event_registered = false;
271 dev_err(&pdev->dev, "unable to create sysfs interface\n");
278 static int zynqmp_pm_remove(struct platform_device *pdev)
280 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_suspend_mode.attr);
281 if (event_registered)
282 xlnx_unregister_event(PM_INIT_SUSPEND_CB, 0, 0, suspend_event_callback, NULL);
285 mbox_free_channel(rx_chan);
290 static const struct of_device_id pm_of_match[] = {
291 { .compatible = "xlnx,zynqmp-power", },
292 { /* end of table */ },
294 MODULE_DEVICE_TABLE(of, pm_of_match);
296 static struct platform_driver zynqmp_pm_platform_driver = {
297 .probe = zynqmp_pm_probe,
298 .remove = zynqmp_pm_remove,
300 .name = "zynqmp_power",
301 .of_match_table = pm_of_match,
304 module_platform_driver(zynqmp_pm_platform_driver);