| 1 |
// SPDX-License-Identifier: GPL-2.0 |
| 2 |
/* |
| 3 |
* ZynqMP Generic PM domain support |
| 4 |
* |
| 5 |
* Copyright (C) 2015-2019 Xilinx, Inc. |
| 6 |
* |
| 7 |
* Davorin Mista <[email protected]> |
| 8 |
* Jolly Shah <[email protected]> |
| 9 |
* Rajan Vaja <[email protected]> |
| 10 |
*/ |
| 11 |
|
| 12 |
#include <linux/err.h> |
| 13 |
#include <linux/list.h> |
| 14 |
#include <linux/module.h> |
| 15 |
#include <linux/of_platform.h> |
| 16 |
#include <linux/platform_device.h> |
| 17 |
#include <linux/pm_domain.h> |
| 18 |
#include <linux/slab.h> |
| 19 |
|
| 20 |
#include <linux/firmware/xlnx-zynqmp.h> |
| 21 |
|
| 22 |
#define ZYNQMP_NUM_DOMAINS (100) |
| 23 |
|
| 24 |
static int min_capability; |
| 25 |
|
| 26 |
/** |
| 27 |
* struct zynqmp_pm_domain - Wrapper around struct generic_pm_domain |
| 28 |
* @gpd: Generic power domain |
| 29 |
* @node_id: PM node ID corresponding to device inside PM domain |
| 30 |
* @requested: The PM node mapped to the PM domain has been requested |
| 31 |
*/ |
| 32 |
struct zynqmp_pm_domain { |
| 33 |
struct generic_pm_domain gpd; |
| 34 |
u32 node_id; |
| 35 |
bool requested; |
| 36 |
}; |
| 37 |
|
| 38 |
#define to_zynqmp_pm_domain(pm_domain) \ |
| 39 |
container_of(pm_domain, struct zynqmp_pm_domain, gpd) |
| 40 |
|
| 41 |
/** |
| 42 |
* zynqmp_gpd_is_active_wakeup_path() - Check if device is in wakeup source |
| 43 |
* path |
| 44 |
* @dev: Device to check for wakeup source path |
| 45 |
* @not_used: Data member (not required) |
| 46 |
* |
| 47 |
* This function is checks device's child hierarchy and checks if any device is |
| 48 |
* set as wakeup source. |
| 49 |
* |
| 50 |
* Return: 1 if device is in wakeup source path else 0 |
| 51 |
*/ |
| 52 |
static int zynqmp_gpd_is_active_wakeup_path(struct device *dev, void *not_used) |
| 53 |
{ |
| 54 |
int may_wakeup; |
| 55 |
|
| 56 |
may_wakeup = device_may_wakeup(dev); |
| 57 |
if (may_wakeup) |
| 58 |
return may_wakeup; |
| 59 |
|
| 60 |
return device_for_each_child(dev, NULL, |
| 61 |
zynqmp_gpd_is_active_wakeup_path); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* zynqmp_gpd_power_on() - Power on PM domain |
| 66 |
* @domain: Generic PM domain |
| 67 |
* |
| 68 |
* This function is called before devices inside a PM domain are resumed, to |
| 69 |
* power on PM domain. |
| 70 |
* |
| 71 |
* Return: 0 on success, error code otherwise |
| 72 |
*/ |
| 73 |
static int zynqmp_gpd_power_on(struct generic_pm_domain *domain) |
| 74 |
{ |
| 75 |
struct zynqmp_pm_domain *pd = to_zynqmp_pm_domain(domain); |
| 76 |
int ret; |
| 77 |
|
| 78 |
ret = zynqmp_pm_set_requirement(pd->node_id, |
| 79 |
ZYNQMP_PM_CAPABILITY_ACCESS, |
| 80 |
ZYNQMP_PM_MAX_QOS, |
| 81 |
ZYNQMP_PM_REQUEST_ACK_BLOCKING); |
| 82 |
if (ret) { |
| 83 |
dev_err(&domain->dev, |
| 84 |
"failed to set requirement to 0x%x for PM node id %d: %d\n", |
| 85 |
ZYNQMP_PM_CAPABILITY_ACCESS, pd->node_id, ret); |
| 86 |
return ret; |
| 87 |
} |
| 88 |
|
| 89 |
dev_dbg(&domain->dev, "set requirement to 0x%x for PM node id %d\n", |
| 90 |
ZYNQMP_PM_CAPABILITY_ACCESS, pd->node_id); |
| 91 |
|
| 92 |
return 0; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* zynqmp_gpd_power_off() - Power off PM domain |
| 97 |
* @domain: Generic PM domain |
| 98 |
* |
| 99 |
* This function is called after devices inside a PM domain are suspended, to |
| 100 |
* power off PM domain. |
| 101 |
* |
| 102 |
* Return: 0 on success, error code otherwise |
| 103 |
*/ |
| 104 |
static int zynqmp_gpd_power_off(struct generic_pm_domain *domain) |
| 105 |
{ |
| 106 |
struct zynqmp_pm_domain *pd = to_zynqmp_pm_domain(domain); |
| 107 |
int ret; |
| 108 |
struct pm_domain_data *pdd, *tmp; |
| 109 |
u32 capabilities = min_capability; |
| 110 |
bool may_wakeup; |
| 111 |
|
| 112 |
/* If domain is already released there is nothing to be done */ |
| 113 |
if (!pd->requested) { |
| 114 |
dev_dbg(&domain->dev, "PM node id %d is already released\n", |
| 115 |
pd->node_id); |
| 116 |
return 0; |
| 117 |
} |
| 118 |
|
| 119 |
list_for_each_entry_safe(pdd, tmp, &domain->dev_list, list_node) { |
| 120 |
/* If device is in wakeup path, set capability to WAKEUP */ |
| 121 |
may_wakeup = zynqmp_gpd_is_active_wakeup_path(pdd->dev, NULL); |
| 122 |
if (may_wakeup) { |
| 123 |
dev_dbg(pdd->dev, "device is in wakeup path in %s\n", |
| 124 |
domain->name); |
| 125 |
capabilities = ZYNQMP_PM_CAPABILITY_WAKEUP; |
| 126 |
break; |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
ret = zynqmp_pm_set_requirement(pd->node_id, capabilities, 0, |
| 131 |
ZYNQMP_PM_REQUEST_ACK_NO); |
| 132 |
if (ret) { |
| 133 |
dev_err(&domain->dev, |
| 134 |
"failed to set requirement to 0x%x for PM node id %d: %d\n", |
| 135 |
capabilities, pd->node_id, ret); |
| 136 |
return ret; |
| 137 |
} |
| 138 |
|
| 139 |
dev_dbg(&domain->dev, "set requirement to 0x%x for PM node id %d\n", |
| 140 |
capabilities, pd->node_id); |
| 141 |
|
| 142 |
return 0; |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* zynqmp_gpd_attach_dev() - Attach device to the PM domain |
| 147 |
* @domain: Generic PM domain |
| 148 |
* @dev: Device to attach |
| 149 |
* |
| 150 |
* Return: 0 on success, error code otherwise |
| 151 |
*/ |
| 152 |
static int zynqmp_gpd_attach_dev(struct generic_pm_domain *domain, |
| 153 |
struct device *dev) |
| 154 |
{ |
| 155 |
struct zynqmp_pm_domain *pd = to_zynqmp_pm_domain(domain); |
| 156 |
int ret; |
| 157 |
|
| 158 |
/* If this is not the first device to attach there is nothing to do */ |
| 159 |
if (domain->device_count) |
| 160 |
return 0; |
| 161 |
|
| 162 |
ret = zynqmp_pm_request_node(pd->node_id, 0, 0, |
| 163 |
ZYNQMP_PM_REQUEST_ACK_BLOCKING); |
| 164 |
if (ret) { |
| 165 |
dev_err(&domain->dev, "%s request failed for node %d: %d\n", |
| 166 |
domain->name, pd->node_id, ret); |
| 167 |
return ret; |
| 168 |
} |
| 169 |
|
| 170 |
pd->requested = true; |
| 171 |
|
| 172 |
dev_dbg(&domain->dev, "%s requested PM node id %d\n", |
| 173 |
dev_name(dev), pd->node_id); |
| 174 |
|
| 175 |
return 0; |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* zynqmp_gpd_detach_dev() - Detach device from the PM domain |
| 180 |
* @domain: Generic PM domain |
| 181 |
* @dev: Device to detach |
| 182 |
*/ |
| 183 |
static void zynqmp_gpd_detach_dev(struct generic_pm_domain *domain, |
| 184 |
struct device *dev) |
| 185 |
{ |
| 186 |
struct zynqmp_pm_domain *pd = to_zynqmp_pm_domain(domain); |
| 187 |
int ret; |
| 188 |
|
| 189 |
/* If this is not the last device to detach there is nothing to do */ |
| 190 |
if (domain->device_count) |
| 191 |
return; |
| 192 |
|
| 193 |
ret = zynqmp_pm_release_node(pd->node_id); |
| 194 |
if (ret) { |
| 195 |
dev_err(&domain->dev, "failed to release PM node id %d: %d\n", |
| 196 |
pd->node_id, ret); |
| 197 |
return; |
| 198 |
} |
| 199 |
|
| 200 |
pd->requested = false; |
| 201 |
|
| 202 |
dev_dbg(&domain->dev, "%s released PM node id %d\n", |
| 203 |
dev_name(dev), pd->node_id); |
| 204 |
} |
| 205 |
|
| 206 |
static struct generic_pm_domain *zynqmp_gpd_xlate |
| 207 |
(const struct of_phandle_args *genpdspec, void *data) |
| 208 |
{ |
| 209 |
struct genpd_onecell_data *genpd_data = data; |
| 210 |
unsigned int i, idx = genpdspec->args[0]; |
| 211 |
struct zynqmp_pm_domain *pd; |
| 212 |
|
| 213 |
pd = to_zynqmp_pm_domain(genpd_data->domains[0]); |
| 214 |
|
| 215 |
if (genpdspec->args_count != 1) |
| 216 |
return ERR_PTR(-EINVAL); |
| 217 |
|
| 218 |
/* Check for existing pm domains */ |
| 219 |
for (i = 0; i < ZYNQMP_NUM_DOMAINS; i++) { |
| 220 |
if (pd[i].node_id == idx) |
| 221 |
goto done; |
| 222 |
} |
| 223 |
|
| 224 |
/* |
| 225 |
* Add index in empty node_id of power domain list as no existing |
| 226 |
* power domain found for current index. |
| 227 |
*/ |
| 228 |
for (i = 0; i < ZYNQMP_NUM_DOMAINS; i++) { |
| 229 |
if (pd[i].node_id == 0) { |
| 230 |
pd[i].node_id = idx; |
| 231 |
break; |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
done: |
| 236 |
if (!genpd_data->domains[i] || i == ZYNQMP_NUM_DOMAINS) |
| 237 |
return ERR_PTR(-ENOENT); |
| 238 |
|
| 239 |
return genpd_data->domains[i]; |
| 240 |
} |
| 241 |
|
| 242 |
static int zynqmp_gpd_probe(struct platform_device *pdev) |
| 243 |
{ |
| 244 |
int i; |
| 245 |
struct genpd_onecell_data *zynqmp_pd_data; |
| 246 |
struct generic_pm_domain **domains; |
| 247 |
struct zynqmp_pm_domain *pd; |
| 248 |
struct device *dev = &pdev->dev; |
| 249 |
|
| 250 |
pd = devm_kcalloc(dev, ZYNQMP_NUM_DOMAINS, sizeof(*pd), GFP_KERNEL); |
| 251 |
if (!pd) |
| 252 |
return -ENOMEM; |
| 253 |
|
| 254 |
zynqmp_pd_data = devm_kzalloc(dev, sizeof(*zynqmp_pd_data), GFP_KERNEL); |
| 255 |
if (!zynqmp_pd_data) |
| 256 |
return -ENOMEM; |
| 257 |
|
| 258 |
zynqmp_pd_data->xlate = zynqmp_gpd_xlate; |
| 259 |
|
| 260 |
domains = devm_kcalloc(dev, ZYNQMP_NUM_DOMAINS, sizeof(*domains), |
| 261 |
GFP_KERNEL); |
| 262 |
if (!domains) |
| 263 |
return -ENOMEM; |
| 264 |
|
| 265 |
if (!of_device_is_compatible(dev->parent->of_node, |
| 266 |
"xlnx,zynqmp-firmware")) |
| 267 |
min_capability = ZYNQMP_PM_CAPABILITY_UNUSABLE; |
| 268 |
|
| 269 |
for (i = 0; i < ZYNQMP_NUM_DOMAINS; i++, pd++) { |
| 270 |
pd->node_id = 0; |
| 271 |
pd->gpd.name = kasprintf(GFP_KERNEL, "domain%d", i); |
| 272 |
pd->gpd.power_off = zynqmp_gpd_power_off; |
| 273 |
pd->gpd.power_on = zynqmp_gpd_power_on; |
| 274 |
pd->gpd.attach_dev = zynqmp_gpd_attach_dev; |
| 275 |
pd->gpd.detach_dev = zynqmp_gpd_detach_dev; |
| 276 |
|
| 277 |
domains[i] = &pd->gpd; |
| 278 |
|
| 279 |
/* Mark all PM domains as initially powered off */ |
| 280 |
pm_genpd_init(&pd->gpd, NULL, true); |
| 281 |
} |
| 282 |
|
| 283 |
zynqmp_pd_data->domains = domains; |
| 284 |
zynqmp_pd_data->num_domains = ZYNQMP_NUM_DOMAINS; |
| 285 |
of_genpd_add_provider_onecell(dev->parent->of_node, zynqmp_pd_data); |
| 286 |
|
| 287 |
return 0; |
| 288 |
} |
| 289 |
|
| 290 |
static void zynqmp_gpd_remove(struct platform_device *pdev) |
| 291 |
{ |
| 292 |
of_genpd_del_provider(pdev->dev.parent->of_node); |
| 293 |
} |
| 294 |
|
| 295 |
static struct platform_driver zynqmp_power_domain_driver = { |
| 296 |
.driver = { |
| 297 |
.name = "zynqmp_power_controller", |
| 298 |
}, |
| 299 |
.probe = zynqmp_gpd_probe, |
| 300 |
.remove = zynqmp_gpd_remove, |
| 301 |
}; |
| 302 |
module_platform_driver(zynqmp_power_domain_driver); |
| 303 |
|
| 304 |
MODULE_ALIAS("platform:zynqmp_power_controller"); |