1 // SPDX-License-Identifier: GPL-2.0
3 * Xilinx Zynq MPSoC Power Management
5 * Copyright (C) 2014-2019 Xilinx, Inc.
12 #include <linux/mailbox_client.h>
13 #include <linux/module.h>
14 #include <linux/platform_device.h>
15 #include <linux/reboot.h>
16 #include <linux/suspend.h>
18 #include <linux/firmware/xlnx-zynqmp.h>
19 #include <linux/mailbox/zynqmp-ipi-message.h>
22 * struct zynqmp_pm_work_struct - Wrapper for struct work_struct
23 * @callback_work: Work structure
24 * @args: Callback arguments
26 struct zynqmp_pm_work_struct {
27 struct work_struct callback_work;
31 static struct zynqmp_pm_work_struct *zynqmp_pm_init_suspend_work;
32 static struct mbox_chan *rx_chan;
33 static const struct zynqmp_eemi_ops *eemi_ops;
35 enum pm_suspend_mode {
36 PM_SUSPEND_MODE_FIRST = 0,
37 PM_SUSPEND_MODE_STD = PM_SUSPEND_MODE_FIRST,
38 PM_SUSPEND_MODE_POWER_OFF,
41 #define PM_SUSPEND_MODE_FIRST PM_SUSPEND_MODE_STD
43 static const char *const suspend_modes[] = {
44 [PM_SUSPEND_MODE_STD] = "standard",
45 [PM_SUSPEND_MODE_POWER_OFF] = "power-off",
48 static enum pm_suspend_mode suspend_mode = PM_SUSPEND_MODE_STD;
51 PM_INIT_SUSPEND_CB = 30,
56 static void zynqmp_pm_get_callback_data(u32 *buf)
58 zynqmp_pm_invoke_fn(GET_CALLBACK_DATA, 0, 0, 0, 0, buf);
61 static irqreturn_t zynqmp_pm_isr(int irq, void *data)
63 u32 payload[CB_PAYLOAD_SIZE];
65 zynqmp_pm_get_callback_data(payload);
67 /* First element is callback API ID, others are callback arguments */
68 if (payload[0] == PM_INIT_SUSPEND_CB) {
70 case SUSPEND_SYSTEM_SHUTDOWN:
71 orderly_poweroff(true);
73 case SUSPEND_POWER_REQUEST:
74 pm_suspend(PM_SUSPEND_MEM);
77 pr_err("%s Unsupported InitSuspendCb reason "
78 "code %d\n", __func__, payload[1]);
85 static void ipi_receive_callback(struct mbox_client *cl, void *data)
87 struct zynqmp_ipi_message *msg = (struct zynqmp_ipi_message *)data;
88 u32 payload[CB_PAYLOAD_SIZE];
91 memcpy(payload, msg->data, sizeof(msg->len));
92 /* First element is callback API ID, others are callback arguments */
93 if (payload[0] == PM_INIT_SUSPEND_CB) {
94 if (work_pending(&zynqmp_pm_init_suspend_work->callback_work))
97 /* Copy callback arguments into work's structure */
98 memcpy(zynqmp_pm_init_suspend_work->args, &payload[1],
99 sizeof(zynqmp_pm_init_suspend_work->args));
101 queue_work(system_unbound_wq,
102 &zynqmp_pm_init_suspend_work->callback_work);
104 /* Send NULL message to mbox controller to ack the message */
105 ret = mbox_send_message(rx_chan, NULL);
107 pr_err("IPI ack failed. Error %d\n", ret);
112 * zynqmp_pm_init_suspend_work_fn - Initialize suspend
113 * @work: Pointer to work_struct
115 * Bottom-half of PM callback IRQ handler.
117 static void zynqmp_pm_init_suspend_work_fn(struct work_struct *work)
119 struct zynqmp_pm_work_struct *pm_work =
120 container_of(work, struct zynqmp_pm_work_struct, callback_work);
122 if (pm_work->args[0] == SUSPEND_SYSTEM_SHUTDOWN) {
123 orderly_poweroff(true);
124 } else if (pm_work->args[0] == SUSPEND_POWER_REQUEST) {
125 pm_suspend(PM_SUSPEND_MEM);
127 pr_err("%s Unsupported InitSuspendCb reason code %d.\n",
128 __func__, pm_work->args[0]);
132 static ssize_t suspend_mode_show(struct device *dev,
133 struct device_attribute *attr, char *buf)
138 for (md = PM_SUSPEND_MODE_FIRST; md < ARRAY_SIZE(suspend_modes); md++)
139 if (suspend_modes[md]) {
140 if (md == suspend_mode)
141 s += sprintf(s, "[%s] ", suspend_modes[md]);
143 s += sprintf(s, "%s ", suspend_modes[md]);
146 /* Convert last space to newline */
152 static ssize_t suspend_mode_store(struct device *dev,
153 struct device_attribute *attr,
154 const char *buf, size_t count)
156 int md, ret = -EINVAL;
158 if (!eemi_ops->set_suspend_mode)
161 for (md = PM_SUSPEND_MODE_FIRST; md < ARRAY_SIZE(suspend_modes); md++)
162 if (suspend_modes[md] &&
163 sysfs_streq(suspend_modes[md], buf)) {
168 if (!ret && md != suspend_mode) {
169 ret = eemi_ops->set_suspend_mode(md);
174 return ret ? ret : count;
177 static DEVICE_ATTR_RW(suspend_mode);
179 static int zynqmp_pm_probe(struct platform_device *pdev)
183 struct mbox_client *client;
185 eemi_ops = zynqmp_pm_get_eemi_ops();
186 if (IS_ERR(eemi_ops))
187 return PTR_ERR(eemi_ops);
189 if (!eemi_ops->get_api_version || !eemi_ops->init_finalize)
192 eemi_ops->init_finalize();
193 eemi_ops->get_api_version(&pm_api_version);
195 /* Check PM API version number */
196 if (pm_api_version < ZYNQMP_PM_VERSION)
199 if (of_find_property(pdev->dev.of_node, "mboxes", NULL)) {
200 zynqmp_pm_init_suspend_work =
201 devm_kzalloc(&pdev->dev,
202 sizeof(struct zynqmp_pm_work_struct),
204 if (!zynqmp_pm_init_suspend_work)
207 INIT_WORK(&zynqmp_pm_init_suspend_work->callback_work,
208 zynqmp_pm_init_suspend_work_fn);
209 client = devm_kzalloc(&pdev->dev, sizeof(*client), GFP_KERNEL);
213 client->dev = &pdev->dev;
214 client->rx_callback = ipi_receive_callback;
216 rx_chan = mbox_request_channel_byname(client, "rx");
217 if (IS_ERR(rx_chan)) {
218 dev_err(&pdev->dev, "Failed to request rx channel\n");
219 return IS_ERR(rx_chan);
221 } else if (of_find_property(pdev->dev.of_node, "interrupts", NULL)) {
222 irq = platform_get_irq(pdev, 0);
226 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
228 IRQF_NO_SUSPEND | IRQF_ONESHOT,
229 dev_name(&pdev->dev),
232 dev_err(&pdev->dev, "devm_request_threaded_irq '%d' "
233 "failed with %d\n", irq, ret);
237 dev_err(&pdev->dev, "Required property not found in DT node\n");
241 ret = sysfs_create_file(&pdev->dev.kobj, &dev_attr_suspend_mode.attr);
243 dev_err(&pdev->dev, "unable to create sysfs interface\n");
250 static int zynqmp_pm_remove(struct platform_device *pdev)
252 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_suspend_mode.attr);
255 mbox_free_channel(rx_chan);
260 static const struct of_device_id pm_of_match[] = {
261 { .compatible = "xlnx,zynqmp-power", },
262 { /* end of table */ },
264 MODULE_DEVICE_TABLE(of, pm_of_match);
266 static struct platform_driver zynqmp_pm_platform_driver = {
267 .probe = zynqmp_pm_probe,
268 .remove = zynqmp_pm_remove,
270 .name = "zynqmp_power",
271 .of_match_table = pm_of_match,
274 module_platform_driver(zynqmp_pm_platform_driver);