blob: 875225bfa21c2e77fd0d791a24a921c6b1469c34 [file] [log] [blame]
Andreas Färberaa9f8002017-02-26 04:09:57 +01001/*
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ärberaa9f8002017-02-26 04:09:57 +010015#include <linux/of_address.h>
16#include <linux/of_platform.h>
17#include <linux/pm_domain.h>
Andreas Färber6932ec62017-06-05 21:04:21 +020018#include <linux/soc/actions/owl-sps.h>
Andreas Färberaa9f8002017-02-26 04:09:57 +010019#include <dt-bindings/power/owl-s500-powergate.h>
20
Andreas Färberaa9f8002017-02-26 04:09:57 +010021struct owl_sps_domain_info {
22 const char *name;
23 int pwr_bit;
24 int ack_bit;
25 unsigned int genpd_flags;
26};
27
28struct owl_sps_info {
29 unsigned num_domains;
30 const struct owl_sps_domain_info *domains;
31};
32
33struct 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
43struct owl_sps_domain {
44 struct generic_pm_domain genpd;
45 const struct owl_sps_domain_info *info;
46 struct owl_sps *sps;
47};
48
49static int owl_sps_set_power(struct owl_sps_domain *pd, bool enable)
50{
Andreas Färber6932ec62017-06-05 21:04:21 +020051 u32 pwr_mask, ack_mask;
Andreas Färberaa9f8002017-02-26 04:09:57 +010052
53 ack_mask = BIT(pd->info->ack_bit);
54 pwr_mask = BIT(pd->info->pwr_bit);
Andreas Färberaa9f8002017-02-26 04:09:57 +010055
Andreas Färber6932ec62017-06-05 21:04:21 +020056 return owl_sps_set_pg(pd->sps->base, pwr_mask, ack_mask, enable);
Andreas Färberaa9f8002017-02-26 04:09:57 +010057}
58
59static 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
68static 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
77static 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
99static 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
151static 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
201static const struct owl_sps_info s500_sps_info = {
202 .num_domains = ARRAY_SIZE(s500_sps_domains),
203 .domains = s500_sps_domains,
204};
205
206static const struct of_device_id owl_sps_of_matches[] = {
207 { .compatible = "actions,s500-sps", .data = &s500_sps_info },
208 { }
209};
210
211static 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
220static int __init owl_sps_init(void)
221{
222 return platform_driver_register(&owl_sps_platform_driver);
223}
224postcore_initcall(owl_sps_init);