scpi_pm_domain.c (3879B)
1// SPDX-License-Identifier: GPL-2.0-only 2/* 3 * SCPI Generic power domain support. 4 * 5 * Copyright (C) 2016 ARM Ltd. 6 */ 7 8#include <linux/err.h> 9#include <linux/io.h> 10#include <linux/module.h> 11#include <linux/of_platform.h> 12#include <linux/pm_domain.h> 13#include <linux/scpi_protocol.h> 14 15struct scpi_pm_domain { 16 struct generic_pm_domain genpd; 17 struct scpi_ops *ops; 18 u32 domain; 19}; 20 21/* 22 * These device power state values are not well-defined in the specification. 23 * In case, different implementations use different values, we can make these 24 * specific to compatibles rather than getting these values from device tree. 25 */ 26enum scpi_power_domain_state { 27 SCPI_PD_STATE_ON = 0, 28 SCPI_PD_STATE_OFF = 3, 29}; 30 31#define to_scpi_pd(gpd) container_of(gpd, struct scpi_pm_domain, genpd) 32 33static int scpi_pd_power(struct scpi_pm_domain *pd, bool power_on) 34{ 35 int ret; 36 enum scpi_power_domain_state state; 37 38 if (power_on) 39 state = SCPI_PD_STATE_ON; 40 else 41 state = SCPI_PD_STATE_OFF; 42 43 ret = pd->ops->device_set_power_state(pd->domain, state); 44 if (ret) 45 return ret; 46 47 return !(state == pd->ops->device_get_power_state(pd->domain)); 48} 49 50static int scpi_pd_power_on(struct generic_pm_domain *domain) 51{ 52 struct scpi_pm_domain *pd = to_scpi_pd(domain); 53 54 return scpi_pd_power(pd, true); 55} 56 57static int scpi_pd_power_off(struct generic_pm_domain *domain) 58{ 59 struct scpi_pm_domain *pd = to_scpi_pd(domain); 60 61 return scpi_pd_power(pd, false); 62} 63 64static int scpi_pm_domain_probe(struct platform_device *pdev) 65{ 66 struct device *dev = &pdev->dev; 67 struct device_node *np = dev->of_node; 68 struct scpi_pm_domain *scpi_pd; 69 struct genpd_onecell_data *scpi_pd_data; 70 struct generic_pm_domain **domains; 71 struct scpi_ops *scpi_ops; 72 int ret, num_domains, i; 73 74 scpi_ops = get_scpi_ops(); 75 if (!scpi_ops) 76 return -EPROBE_DEFER; 77 78 if (!np) { 79 dev_err(dev, "device tree node not found\n"); 80 return -ENODEV; 81 } 82 83 if (!scpi_ops->device_set_power_state || 84 !scpi_ops->device_get_power_state) { 85 dev_err(dev, "power domains not supported in the firmware\n"); 86 return -ENODEV; 87 } 88 89 ret = of_property_read_u32(np, "num-domains", &num_domains); 90 if (ret) { 91 dev_err(dev, "number of domains not found\n"); 92 return -EINVAL; 93 } 94 95 scpi_pd = devm_kcalloc(dev, num_domains, sizeof(*scpi_pd), GFP_KERNEL); 96 if (!scpi_pd) 97 return -ENOMEM; 98 99 scpi_pd_data = devm_kzalloc(dev, sizeof(*scpi_pd_data), GFP_KERNEL); 100 if (!scpi_pd_data) 101 return -ENOMEM; 102 103 domains = devm_kcalloc(dev, num_domains, sizeof(*domains), GFP_KERNEL); 104 if (!domains) 105 return -ENOMEM; 106 107 for (i = 0; i < num_domains; i++, scpi_pd++) { 108 domains[i] = &scpi_pd->genpd; 109 110 scpi_pd->domain = i; 111 scpi_pd->ops = scpi_ops; 112 scpi_pd->genpd.name = devm_kasprintf(dev, GFP_KERNEL, 113 "%pOFn.%d", np, i); 114 if (!scpi_pd->genpd.name) { 115 dev_err(dev, "Failed to allocate genpd name:%pOFn.%d\n", 116 np, i); 117 continue; 118 } 119 scpi_pd->genpd.power_off = scpi_pd_power_off; 120 scpi_pd->genpd.power_on = scpi_pd_power_on; 121 122 /* 123 * Treat all power domains as off at boot. 124 * 125 * The SCP firmware itself may have switched on some domains, 126 * but for reference counting purpose, keep it this way. 127 */ 128 pm_genpd_init(&scpi_pd->genpd, NULL, true); 129 } 130 131 scpi_pd_data->domains = domains; 132 scpi_pd_data->num_domains = num_domains; 133 134 of_genpd_add_provider_onecell(np, scpi_pd_data); 135 136 return 0; 137} 138 139static const struct of_device_id scpi_power_domain_ids[] = { 140 { .compatible = "arm,scpi-power-domains", }, 141 { /* sentinel */ } 142}; 143MODULE_DEVICE_TABLE(of, scpi_power_domain_ids); 144 145static struct platform_driver scpi_power_domain_driver = { 146 .driver = { 147 .name = "scpi_power_domain", 148 .of_match_table = scpi_power_domain_ids, 149 }, 150 .probe = scpi_pm_domain_probe, 151}; 152module_platform_driver(scpi_power_domain_driver); 153 154MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>"); 155MODULE_DESCRIPTION("ARM SCPI power domain driver"); 156MODULE_LICENSE("GPL v2");