Andreas Färber | aa9f800 | 2017-02-26 04:09:57 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Actions Semi Owl Smart Power System (SPS) |
| 3 | * |
| 4 | * Copyright 2012 Actions Semi Inc. |
| 5 | * Author: Actions Semi, Inc. |
| 6 | * |
| 7 | * Copyright (c) 2017 Andreas Färber |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify it |
| 10 | * under the terms of the GNU General Public License as published by the |
| 11 | * Free Software Foundation; either version 2 of the License, or (at your |
| 12 | * option) any later version. |
| 13 | */ |
| 14 | |
Andreas Färber | aa9f800 | 2017-02-26 04:09:57 +0100 | [diff] [blame] | 15 | #include <linux/of_address.h> |
| 16 | #include <linux/of_platform.h> |
| 17 | #include <linux/pm_domain.h> |
Andreas Färber | 6932ec6 | 2017-06-05 21:04:21 +0200 | [diff] [blame^] | 18 | #include <linux/soc/actions/owl-sps.h> |
Andreas Färber | aa9f800 | 2017-02-26 04:09:57 +0100 | [diff] [blame] | 19 | #include <dt-bindings/power/owl-s500-powergate.h> |
| 20 | |
Andreas Färber | aa9f800 | 2017-02-26 04:09:57 +0100 | [diff] [blame] | 21 | struct owl_sps_domain_info { |
| 22 | const char *name; |
| 23 | int pwr_bit; |
| 24 | int ack_bit; |
| 25 | unsigned int genpd_flags; |
| 26 | }; |
| 27 | |
| 28 | struct owl_sps_info { |
| 29 | unsigned num_domains; |
| 30 | const struct owl_sps_domain_info *domains; |
| 31 | }; |
| 32 | |
| 33 | struct owl_sps { |
| 34 | struct device *dev; |
| 35 | const struct owl_sps_info *info; |
| 36 | void __iomem *base; |
| 37 | struct genpd_onecell_data genpd_data; |
| 38 | struct generic_pm_domain *domains[]; |
| 39 | }; |
| 40 | |
| 41 | #define to_owl_pd(gpd) container_of(gpd, struct owl_sps_domain, genpd) |
| 42 | |
| 43 | struct owl_sps_domain { |
| 44 | struct generic_pm_domain genpd; |
| 45 | const struct owl_sps_domain_info *info; |
| 46 | struct owl_sps *sps; |
| 47 | }; |
| 48 | |
| 49 | static int owl_sps_set_power(struct owl_sps_domain *pd, bool enable) |
| 50 | { |
Andreas Färber | 6932ec6 | 2017-06-05 21:04:21 +0200 | [diff] [blame^] | 51 | u32 pwr_mask, ack_mask; |
Andreas Färber | aa9f800 | 2017-02-26 04:09:57 +0100 | [diff] [blame] | 52 | |
| 53 | ack_mask = BIT(pd->info->ack_bit); |
| 54 | pwr_mask = BIT(pd->info->pwr_bit); |
Andreas Färber | aa9f800 | 2017-02-26 04:09:57 +0100 | [diff] [blame] | 55 | |
Andreas Färber | 6932ec6 | 2017-06-05 21:04:21 +0200 | [diff] [blame^] | 56 | return owl_sps_set_pg(pd->sps->base, pwr_mask, ack_mask, enable); |
Andreas Färber | aa9f800 | 2017-02-26 04:09:57 +0100 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | static int owl_sps_power_on(struct generic_pm_domain *domain) |
| 60 | { |
| 61 | struct owl_sps_domain *pd = to_owl_pd(domain); |
| 62 | |
| 63 | dev_dbg(pd->sps->dev, "%s power on", pd->info->name); |
| 64 | |
| 65 | return owl_sps_set_power(pd, true); |
| 66 | } |
| 67 | |
| 68 | static int owl_sps_power_off(struct generic_pm_domain *domain) |
| 69 | { |
| 70 | struct owl_sps_domain *pd = to_owl_pd(domain); |
| 71 | |
| 72 | dev_dbg(pd->sps->dev, "%s power off", pd->info->name); |
| 73 | |
| 74 | return owl_sps_set_power(pd, false); |
| 75 | } |
| 76 | |
| 77 | static int owl_sps_init_domain(struct owl_sps *sps, int index) |
| 78 | { |
| 79 | struct owl_sps_domain *pd; |
| 80 | |
| 81 | pd = devm_kzalloc(sps->dev, sizeof(*pd), GFP_KERNEL); |
| 82 | if (!pd) |
| 83 | return -ENOMEM; |
| 84 | |
| 85 | pd->info = &sps->info->domains[index]; |
| 86 | pd->sps = sps; |
| 87 | |
| 88 | pd->genpd.name = pd->info->name; |
| 89 | pd->genpd.power_on = owl_sps_power_on; |
| 90 | pd->genpd.power_off = owl_sps_power_off; |
| 91 | pd->genpd.flags = pd->info->genpd_flags; |
| 92 | pm_genpd_init(&pd->genpd, NULL, false); |
| 93 | |
| 94 | sps->genpd_data.domains[index] = &pd->genpd; |
| 95 | |
| 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | static int owl_sps_probe(struct platform_device *pdev) |
| 100 | { |
| 101 | const struct of_device_id *match; |
| 102 | const struct owl_sps_info *sps_info; |
| 103 | struct owl_sps *sps; |
| 104 | int i, ret; |
| 105 | |
| 106 | if (!pdev->dev.of_node) { |
| 107 | dev_err(&pdev->dev, "no device node\n"); |
| 108 | return -ENODEV; |
| 109 | } |
| 110 | |
| 111 | match = of_match_device(pdev->dev.driver->of_match_table, &pdev->dev); |
| 112 | if (!match || !match->data) { |
| 113 | dev_err(&pdev->dev, "unknown compatible or missing data\n"); |
| 114 | return -EINVAL; |
| 115 | } |
| 116 | |
| 117 | sps_info = match->data; |
| 118 | |
| 119 | sps = devm_kzalloc(&pdev->dev, sizeof(*sps) + |
| 120 | sps_info->num_domains * sizeof(sps->domains[0]), |
| 121 | GFP_KERNEL); |
| 122 | if (!sps) |
| 123 | return -ENOMEM; |
| 124 | |
| 125 | sps->base = of_io_request_and_map(pdev->dev.of_node, 0, "owl-sps"); |
| 126 | if (IS_ERR(sps->base)) { |
| 127 | dev_err(&pdev->dev, "failed to map sps registers\n"); |
| 128 | return PTR_ERR(sps->base); |
| 129 | } |
| 130 | |
| 131 | sps->dev = &pdev->dev; |
| 132 | sps->info = sps_info; |
| 133 | sps->genpd_data.domains = sps->domains; |
| 134 | sps->genpd_data.num_domains = sps_info->num_domains; |
| 135 | |
| 136 | for (i = 0; i < sps_info->num_domains; i++) { |
| 137 | ret = owl_sps_init_domain(sps, i); |
| 138 | if (ret) |
| 139 | return ret; |
| 140 | } |
| 141 | |
| 142 | ret = of_genpd_add_provider_onecell(pdev->dev.of_node, &sps->genpd_data); |
| 143 | if (ret) { |
| 144 | dev_err(&pdev->dev, "failed to add provider (%d)", ret); |
| 145 | return ret; |
| 146 | } |
| 147 | |
| 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | static const struct owl_sps_domain_info s500_sps_domains[] = { |
| 152 | [S500_PD_VDE] = { |
| 153 | .name = "VDE", |
| 154 | .pwr_bit = 0, |
| 155 | .ack_bit = 16, |
| 156 | }, |
| 157 | [S500_PD_VCE_SI] = { |
| 158 | .name = "VCE_SI", |
| 159 | .pwr_bit = 1, |
| 160 | .ack_bit = 17, |
| 161 | }, |
| 162 | [S500_PD_USB2_1] = { |
| 163 | .name = "USB2_1", |
| 164 | .pwr_bit = 2, |
| 165 | .ack_bit = 18, |
| 166 | }, |
| 167 | [S500_PD_CPU2] = { |
| 168 | .name = "CPU2", |
| 169 | .pwr_bit = 5, |
| 170 | .ack_bit = 21, |
| 171 | .genpd_flags = GENPD_FLAG_ALWAYS_ON, |
| 172 | }, |
| 173 | [S500_PD_CPU3] = { |
| 174 | .name = "CPU3", |
| 175 | .pwr_bit = 6, |
| 176 | .ack_bit = 22, |
| 177 | .genpd_flags = GENPD_FLAG_ALWAYS_ON, |
| 178 | }, |
| 179 | [S500_PD_DMA] = { |
| 180 | .name = "DMA", |
| 181 | .pwr_bit = 8, |
| 182 | .ack_bit = 12, |
| 183 | }, |
| 184 | [S500_PD_DS] = { |
| 185 | .name = "DS", |
| 186 | .pwr_bit = 9, |
| 187 | .ack_bit = 13, |
| 188 | }, |
| 189 | [S500_PD_USB3] = { |
| 190 | .name = "USB3", |
| 191 | .pwr_bit = 10, |
| 192 | .ack_bit = 14, |
| 193 | }, |
| 194 | [S500_PD_USB2_0] = { |
| 195 | .name = "USB2_0", |
| 196 | .pwr_bit = 11, |
| 197 | .ack_bit = 15, |
| 198 | }, |
| 199 | }; |
| 200 | |
| 201 | static const struct owl_sps_info s500_sps_info = { |
| 202 | .num_domains = ARRAY_SIZE(s500_sps_domains), |
| 203 | .domains = s500_sps_domains, |
| 204 | }; |
| 205 | |
| 206 | static const struct of_device_id owl_sps_of_matches[] = { |
| 207 | { .compatible = "actions,s500-sps", .data = &s500_sps_info }, |
| 208 | { } |
| 209 | }; |
| 210 | |
| 211 | static struct platform_driver owl_sps_platform_driver = { |
| 212 | .probe = owl_sps_probe, |
| 213 | .driver = { |
| 214 | .name = "owl-sps", |
| 215 | .of_match_table = owl_sps_of_matches, |
| 216 | .suppress_bind_attrs = true, |
| 217 | }, |
| 218 | }; |
| 219 | |
| 220 | static int __init owl_sps_init(void) |
| 221 | { |
| 222 | return platform_driver_register(&owl_sps_platform_driver); |
| 223 | } |
| 224 | postcore_initcall(owl_sps_init); |