xref: /linux/drivers/soc/xilinx/zynqmp_power.c (revision f86fd32d)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Xilinx Zynq MPSoC Power Management
4  *
5  *  Copyright (C) 2014-2019 Xilinx, Inc.
6  *
7  *  Davorin Mista <davorin.mista@aggios.com>
8  *  Jolly Shah <jollys@xilinx.com>
9  *  Rajan Vaja <rajan.vaja@xilinx.com>
10  */
11 
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>
17 
18 #include <linux/firmware/xlnx-zynqmp.h>
19 #include <linux/mailbox/zynqmp-ipi-message.h>
20 
21 /**
22  * struct zynqmp_pm_work_struct - Wrapper for struct work_struct
23  * @callback_work:	Work structure
24  * @args:		Callback arguments
25  */
26 struct zynqmp_pm_work_struct {
27 	struct work_struct callback_work;
28 	u32 args[CB_ARG_CNT];
29 };
30 
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;
34 
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,
39 };
40 
41 #define PM_SUSPEND_MODE_FIRST	PM_SUSPEND_MODE_STD
42 
43 static const char *const suspend_modes[] = {
44 	[PM_SUSPEND_MODE_STD] = "standard",
45 	[PM_SUSPEND_MODE_POWER_OFF] = "power-off",
46 };
47 
48 static enum pm_suspend_mode suspend_mode = PM_SUSPEND_MODE_STD;
49 
50 enum pm_api_cb_id {
51 	PM_INIT_SUSPEND_CB = 30,
52 	PM_ACKNOWLEDGE_CB,
53 	PM_NOTIFY_CB,
54 };
55 
56 static void zynqmp_pm_get_callback_data(u32 *buf)
57 {
58 	zynqmp_pm_invoke_fn(GET_CALLBACK_DATA, 0, 0, 0, 0, buf);
59 }
60 
61 static irqreturn_t zynqmp_pm_isr(int irq, void *data)
62 {
63 	u32 payload[CB_PAYLOAD_SIZE];
64 
65 	zynqmp_pm_get_callback_data(payload);
66 
67 	/* First element is callback API ID, others are callback arguments */
68 	if (payload[0] == PM_INIT_SUSPEND_CB) {
69 		switch (payload[1]) {
70 		case SUSPEND_SYSTEM_SHUTDOWN:
71 			orderly_poweroff(true);
72 			break;
73 		case SUSPEND_POWER_REQUEST:
74 			pm_suspend(PM_SUSPEND_MEM);
75 			break;
76 		default:
77 			pr_err("%s Unsupported InitSuspendCb reason "
78 				"code %d\n", __func__, payload[1]);
79 		}
80 	}
81 
82 	return IRQ_HANDLED;
83 }
84 
85 static void ipi_receive_callback(struct mbox_client *cl, void *data)
86 {
87 	struct zynqmp_ipi_message *msg = (struct zynqmp_ipi_message *)data;
88 	u32 payload[CB_PAYLOAD_SIZE];
89 	int ret;
90 
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))
95 			return;
96 
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));
100 
101 		queue_work(system_unbound_wq,
102 			   &zynqmp_pm_init_suspend_work->callback_work);
103 
104 		/* Send NULL message to mbox controller to ack the message */
105 		ret = mbox_send_message(rx_chan, NULL);
106 		if (ret)
107 			pr_err("IPI ack failed. Error %d\n", ret);
108 	}
109 }
110 
111 /**
112  * zynqmp_pm_init_suspend_work_fn - Initialize suspend
113  * @work:	Pointer to work_struct
114  *
115  * Bottom-half of PM callback IRQ handler.
116  */
117 static void zynqmp_pm_init_suspend_work_fn(struct work_struct *work)
118 {
119 	struct zynqmp_pm_work_struct *pm_work =
120 		container_of(work, struct zynqmp_pm_work_struct, callback_work);
121 
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);
126 	} else {
127 		pr_err("%s Unsupported InitSuspendCb reason code %d.\n",
128 		       __func__, pm_work->args[0]);
129 	}
130 }
131 
132 static ssize_t suspend_mode_show(struct device *dev,
133 				 struct device_attribute *attr, char *buf)
134 {
135 	char *s = buf;
136 	int md;
137 
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]);
142 			else
143 				s += sprintf(s, "%s ", suspend_modes[md]);
144 		}
145 
146 	/* Convert last space to newline */
147 	if (s != buf)
148 		*(s - 1) = '\n';
149 	return (s - buf);
150 }
151 
152 static ssize_t suspend_mode_store(struct device *dev,
153 				  struct device_attribute *attr,
154 				  const char *buf, size_t count)
155 {
156 	int md, ret = -EINVAL;
157 
158 	if (!eemi_ops->set_suspend_mode)
159 		return ret;
160 
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)) {
164 			ret = 0;
165 			break;
166 		}
167 
168 	if (!ret && md != suspend_mode) {
169 		ret = eemi_ops->set_suspend_mode(md);
170 		if (likely(!ret))
171 			suspend_mode = md;
172 	}
173 
174 	return ret ? ret : count;
175 }
176 
177 static DEVICE_ATTR_RW(suspend_mode);
178 
179 static int zynqmp_pm_probe(struct platform_device *pdev)
180 {
181 	int ret, irq;
182 	u32 pm_api_version;
183 	struct mbox_client *client;
184 
185 	eemi_ops = zynqmp_pm_get_eemi_ops();
186 	if (IS_ERR(eemi_ops))
187 		return PTR_ERR(eemi_ops);
188 
189 	if (!eemi_ops->get_api_version || !eemi_ops->init_finalize)
190 		return -ENXIO;
191 
192 	eemi_ops->init_finalize();
193 	eemi_ops->get_api_version(&pm_api_version);
194 
195 	/* Check PM API version number */
196 	if (pm_api_version < ZYNQMP_PM_VERSION)
197 		return -ENODEV;
198 
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),
203 				     GFP_KERNEL);
204 		if (!zynqmp_pm_init_suspend_work)
205 			return -ENOMEM;
206 
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);
210 		if (!client)
211 			return -ENOMEM;
212 
213 		client->dev = &pdev->dev;
214 		client->rx_callback = ipi_receive_callback;
215 
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);
220 		}
221 	} else if (of_find_property(pdev->dev.of_node, "interrupts", NULL)) {
222 		irq = platform_get_irq(pdev, 0);
223 		if (irq <= 0)
224 			return -ENXIO;
225 
226 		ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
227 						zynqmp_pm_isr,
228 						IRQF_NO_SUSPEND | IRQF_ONESHOT,
229 						dev_name(&pdev->dev),
230 						&pdev->dev);
231 		if (ret) {
232 			dev_err(&pdev->dev, "devm_request_threaded_irq '%d' "
233 					    "failed with %d\n", irq, ret);
234 			return ret;
235 		}
236 	} else {
237 		dev_err(&pdev->dev, "Required property not found in DT node\n");
238 		return -ENOENT;
239 	}
240 
241 	ret = sysfs_create_file(&pdev->dev.kobj, &dev_attr_suspend_mode.attr);
242 	if (ret) {
243 		dev_err(&pdev->dev, "unable to create sysfs interface\n");
244 		return ret;
245 	}
246 
247 	return 0;
248 }
249 
250 static int zynqmp_pm_remove(struct platform_device *pdev)
251 {
252 	sysfs_remove_file(&pdev->dev.kobj, &dev_attr_suspend_mode.attr);
253 
254 	if (!rx_chan)
255 		mbox_free_channel(rx_chan);
256 
257 	return 0;
258 }
259 
260 static const struct of_device_id pm_of_match[] = {
261 	{ .compatible = "xlnx,zynqmp-power", },
262 	{ /* end of table */ },
263 };
264 MODULE_DEVICE_TABLE(of, pm_of_match);
265 
266 static struct platform_driver zynqmp_pm_platform_driver = {
267 	.probe = zynqmp_pm_probe,
268 	.remove = zynqmp_pm_remove,
269 	.driver = {
270 		.name = "zynqmp_power",
271 		.of_match_table = pm_of_match,
272 	},
273 };
274 module_platform_driver(zynqmp_pm_platform_driver);
275