1 // SPDX-License-Identifier: GPL-2.0
3 /* Copyright (c) 2012-2018, The Linux Foundation. All rights reserved.
4 * Copyright (C) 2018-2024 Linaro Ltd.
8 #include <linux/device.h>
9 #include <linux/interconnect.h>
11 #include <linux/pm_runtime.h>
13 #include "linux/soc/qcom/qcom_aoss.h"
17 #include "ipa_endpoint.h"
18 #include "ipa_interrupt.h"
19 #include "ipa_modem.h"
20 #include "ipa_power.h"
23 * DOC: IPA Power Management
25 * The IPA hardware is enabled when the IPA core clock and all the
26 * interconnects (buses) it depends on are enabled. Runtime power
27 * management is used to determine whether the core clock and
28 * interconnects are enabled, and if not in use to be suspended
31 * The core clock currently runs at a fixed clock rate when enabled,
32 * an all interconnects use a fixed average and peak bandwidth.
35 #define IPA_AUTOSUSPEND_DELAY 500 /* milliseconds */
38 * struct ipa_power - IPA power management information
39 * @dev: IPA device pointer
40 * @core: IPA core clock
41 * @qmp: QMP handle for AOSS communication
42 * @interconnect_count: Number of elements in interconnect[]
43 * @interconnect: Interconnect array
49 u32 interconnect_count;
50 struct icc_bulk_data interconnect[] __counted_by(interconnect_count);
53 /* Initialize interconnects required for IPA operation */
54 static int ipa_interconnect_init(struct ipa_power *power,
55 const struct ipa_interconnect_data *data)
57 struct icc_bulk_data *interconnect;
61 /* Initialize our interconnect data array for bulk operations */
62 interconnect = &power->interconnect[0];
63 for (i = 0; i < power->interconnect_count; i++) {
64 /* interconnect->path is filled in by of_icc_bulk_get() */
65 interconnect->name = data->name;
66 interconnect->avg_bw = data->average_bandwidth;
67 interconnect->peak_bw = data->peak_bandwidth;
72 ret = of_icc_bulk_get(power->dev, power->interconnect_count,
77 /* All interconnects are initially disabled */
78 icc_bulk_disable(power->interconnect_count, power->interconnect);
80 /* Set the bandwidth values to be used when enabled */
81 ret = icc_bulk_set_bw(power->interconnect_count, power->interconnect);
83 icc_bulk_put(power->interconnect_count, power->interconnect);
88 /* Inverse of ipa_interconnect_init() */
89 static void ipa_interconnect_exit(struct ipa_power *power)
91 icc_bulk_put(power->interconnect_count, power->interconnect);
94 /* Enable IPA power, enabling interconnects and the core clock */
95 static int ipa_power_enable(struct ipa *ipa)
97 struct ipa_power *power = ipa->power;
100 ret = icc_bulk_enable(power->interconnect_count, power->interconnect);
104 ret = clk_prepare_enable(power->core);
106 dev_err(power->dev, "error %d enabling core clock\n", ret);
107 icc_bulk_disable(power->interconnect_count,
108 power->interconnect);
114 /* Inverse of ipa_power_enable() */
115 static void ipa_power_disable(struct ipa *ipa)
117 struct ipa_power *power = ipa->power;
119 clk_disable_unprepare(power->core);
121 icc_bulk_disable(power->interconnect_count, power->interconnect);
124 static int ipa_runtime_suspend(struct device *dev)
126 struct ipa *ipa = dev_get_drvdata(dev);
128 /* Endpoints aren't usable until setup is complete */
129 if (ipa->setup_complete) {
130 ipa_endpoint_suspend(ipa);
131 gsi_suspend(&ipa->gsi);
134 ipa_power_disable(ipa);
139 static int ipa_runtime_resume(struct device *dev)
141 struct ipa *ipa = dev_get_drvdata(dev);
144 ret = ipa_power_enable(ipa);
145 if (WARN_ON(ret < 0))
148 /* Endpoints aren't usable until setup is complete */
149 if (ipa->setup_complete) {
150 gsi_resume(&ipa->gsi);
151 ipa_endpoint_resume(ipa);
157 static int ipa_suspend(struct device *dev)
159 struct ipa *ipa = dev_get_drvdata(dev);
161 /* Increment the disable depth to ensure that the IRQ won't
162 * be re-enabled until the matching _enable call in
163 * ipa_resume(). We do this to ensure that the interrupt
164 * handler won't run whilst PM runtime is disabled.
166 * Note that disabling the IRQ is NOT the same as disabling
167 * irq wake. If wakeup is enabled for the IPA then the IRQ
168 * will still cause the system to wake up, see irq_set_irq_wake().
170 ipa_interrupt_irq_disable(ipa);
172 return pm_runtime_force_suspend(dev);
175 static int ipa_resume(struct device *dev)
177 struct ipa *ipa = dev_get_drvdata(dev);
180 ret = pm_runtime_force_resume(dev);
182 /* Now that PM runtime is enabled again it's safe
183 * to turn the IRQ back on and process any data
184 * that was received during suspend.
186 ipa_interrupt_irq_enable(ipa);
191 /* Return the current IPA core clock rate */
192 u32 ipa_core_clock_rate(struct ipa *ipa)
194 return ipa->power ? (u32)clk_get_rate(ipa->power->core) : 0;
197 static int ipa_power_retention_init(struct ipa_power *power)
199 struct qmp *qmp = qmp_get(power->dev);
202 if (PTR_ERR(qmp) == -EPROBE_DEFER)
203 return -EPROBE_DEFER;
205 /* We assume any other error means it's not defined/needed */
213 static void ipa_power_retention_exit(struct ipa_power *power)
219 /* Control register retention on power collapse */
220 void ipa_power_retention(struct ipa *ipa, bool enable)
222 static const char fmt[] = "{ class: bcm, res: ipa_pc, val: %c }";
223 struct ipa_power *power = ipa->power;
227 return; /* Not needed on this platform */
229 ret = qmp_send(power->qmp, fmt, enable ? '1' : '0');
231 dev_err(power->dev, "error %d sending QMP %sable request\n",
232 ret, enable ? "en" : "dis");
235 /* Initialize IPA power management */
237 ipa_power_init(struct device *dev, const struct ipa_power_data *data)
239 struct ipa_power *power;
244 clk = clk_get(dev, "core");
246 return dev_err_cast_probe(dev, clk, "error getting core clock\n");
248 ret = clk_set_rate(clk, data->core_clock_rate);
250 dev_err(dev, "error %d setting core clock rate to %u\n",
251 ret, data->core_clock_rate);
255 size = struct_size(power, interconnect, data->interconnect_count);
256 power = kzalloc(size, GFP_KERNEL);
263 power->interconnect_count = data->interconnect_count;
265 ret = ipa_interconnect_init(power, data->interconnect_data);
269 ret = ipa_power_retention_init(power);
271 goto err_interconnect_exit;
273 pm_runtime_set_autosuspend_delay(dev, IPA_AUTOSUSPEND_DELAY);
274 pm_runtime_use_autosuspend(dev);
275 pm_runtime_enable(dev);
279 err_interconnect_exit:
280 ipa_interconnect_exit(power);
289 /* Inverse of ipa_power_init() */
290 void ipa_power_exit(struct ipa_power *power)
292 struct device *dev = power->dev;
293 struct clk *clk = power->core;
295 pm_runtime_disable(dev);
296 pm_runtime_dont_use_autosuspend(dev);
297 ipa_power_retention_exit(power);
298 ipa_interconnect_exit(power);
303 const struct dev_pm_ops ipa_pm_ops = {
304 .suspend = ipa_suspend,
305 .resume = ipa_resume,
306 .runtime_suspend = ipa_runtime_suspend,
307 .runtime_resume = ipa_runtime_resume,