]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/clk/qcom/gdsc.c
Merge tag 'clk-renesas-for-v4.10-tag2' of git://git.kernel.org/pub/scm/linux/kernel...
[karo-tx-linux.git] / drivers / clk / qcom / gdsc.c
1 /*
2  * Copyright (c) 2015, The Linux Foundation. All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 and
6  * only version 2 as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  */
13
14 #include <linux/bitops.h>
15 #include <linux/delay.h>
16 #include <linux/err.h>
17 #include <linux/jiffies.h>
18 #include <linux/kernel.h>
19 #include <linux/ktime.h>
20 #include <linux/pm_domain.h>
21 #include <linux/regmap.h>
22 #include <linux/reset-controller.h>
23 #include <linux/slab.h>
24 #include "gdsc.h"
25
26 #define PWR_ON_MASK             BIT(31)
27 #define EN_REST_WAIT_MASK       GENMASK_ULL(23, 20)
28 #define EN_FEW_WAIT_MASK        GENMASK_ULL(19, 16)
29 #define CLK_DIS_WAIT_MASK       GENMASK_ULL(15, 12)
30 #define SW_OVERRIDE_MASK        BIT(2)
31 #define HW_CONTROL_MASK         BIT(1)
32 #define SW_COLLAPSE_MASK        BIT(0)
33 #define GMEM_CLAMP_IO_MASK      BIT(0)
34
35 /* Wait 2^n CXO cycles between all states. Here, n=2 (4 cycles). */
36 #define EN_REST_WAIT_VAL        (0x2 << 20)
37 #define EN_FEW_WAIT_VAL         (0x8 << 16)
38 #define CLK_DIS_WAIT_VAL        (0x2 << 12)
39
40 #define RETAIN_MEM              BIT(14)
41 #define RETAIN_PERIPH           BIT(13)
42
43 #define TIMEOUT_US              100
44
45 #define domain_to_gdsc(domain) container_of(domain, struct gdsc, pd)
46
47 static int gdsc_is_enabled(struct gdsc *sc, unsigned int reg)
48 {
49         u32 val;
50         int ret;
51
52         ret = regmap_read(sc->regmap, reg, &val);
53         if (ret)
54                 return ret;
55
56         return !!(val & PWR_ON_MASK);
57 }
58
59 static int gdsc_toggle_logic(struct gdsc *sc, bool en)
60 {
61         int ret;
62         u32 val = en ? 0 : SW_COLLAPSE_MASK;
63         ktime_t start;
64         unsigned int status_reg = sc->gdscr;
65
66         ret = regmap_update_bits(sc->regmap, sc->gdscr, SW_COLLAPSE_MASK, val);
67         if (ret)
68                 return ret;
69
70         /* If disabling votable gdscs, don't poll on status */
71         if ((sc->flags & VOTABLE) && !en) {
72                 /*
73                  * Add a short delay here to ensure that an enable
74                  * right after it was disabled does not put it in an
75                  * unknown state
76                  */
77                 udelay(TIMEOUT_US);
78                 return 0;
79         }
80
81         if (sc->gds_hw_ctrl) {
82                 status_reg = sc->gds_hw_ctrl;
83                 /*
84                  * The gds hw controller asserts/de-asserts the status bit soon
85                  * after it receives a power on/off request from a master.
86                  * The controller then takes around 8 xo cycles to start its
87                  * internal state machine and update the status bit. During
88                  * this time, the status bit does not reflect the true status
89                  * of the core.
90                  * Add a delay of 1 us between writing to the SW_COLLAPSE bit
91                  * and polling the status bit.
92                  */
93                 udelay(1);
94         }
95
96         start = ktime_get();
97         do {
98                 if (gdsc_is_enabled(sc, status_reg) == en)
99                         return 0;
100         } while (ktime_us_delta(ktime_get(), start) < TIMEOUT_US);
101
102         if (gdsc_is_enabled(sc, status_reg) == en)
103                 return 0;
104
105         return -ETIMEDOUT;
106 }
107
108 static inline int gdsc_deassert_reset(struct gdsc *sc)
109 {
110         int i;
111
112         for (i = 0; i < sc->reset_count; i++)
113                 sc->rcdev->ops->deassert(sc->rcdev, sc->resets[i]);
114         return 0;
115 }
116
117 static inline int gdsc_assert_reset(struct gdsc *sc)
118 {
119         int i;
120
121         for (i = 0; i < sc->reset_count; i++)
122                 sc->rcdev->ops->assert(sc->rcdev, sc->resets[i]);
123         return 0;
124 }
125
126 static inline void gdsc_force_mem_on(struct gdsc *sc)
127 {
128         int i;
129         u32 mask = RETAIN_MEM | RETAIN_PERIPH;
130
131         for (i = 0; i < sc->cxc_count; i++)
132                 regmap_update_bits(sc->regmap, sc->cxcs[i], mask, mask);
133 }
134
135 static inline void gdsc_clear_mem_on(struct gdsc *sc)
136 {
137         int i;
138         u32 mask = RETAIN_MEM | RETAIN_PERIPH;
139
140         for (i = 0; i < sc->cxc_count; i++)
141                 regmap_update_bits(sc->regmap, sc->cxcs[i], mask, 0);
142 }
143
144 static inline void gdsc_deassert_clamp_io(struct gdsc *sc)
145 {
146         regmap_update_bits(sc->regmap, sc->clamp_io_ctrl,
147                            GMEM_CLAMP_IO_MASK, 0);
148 }
149
150 static inline void gdsc_assert_clamp_io(struct gdsc *sc)
151 {
152         regmap_update_bits(sc->regmap, sc->clamp_io_ctrl,
153                            GMEM_CLAMP_IO_MASK, 1);
154 }
155
156 static int gdsc_enable(struct generic_pm_domain *domain)
157 {
158         struct gdsc *sc = domain_to_gdsc(domain);
159         int ret;
160
161         if (sc->pwrsts == PWRSTS_ON)
162                 return gdsc_deassert_reset(sc);
163
164         if (sc->flags & CLAMP_IO)
165                 gdsc_deassert_clamp_io(sc);
166
167         ret = gdsc_toggle_logic(sc, true);
168         if (ret)
169                 return ret;
170
171         if (sc->pwrsts & PWRSTS_OFF)
172                 gdsc_force_mem_on(sc);
173
174         /*
175          * If clocks to this power domain were already on, they will take an
176          * additional 4 clock cycles to re-enable after the power domain is
177          * enabled. Delay to account for this. A delay is also needed to ensure
178          * clocks are not enabled within 400ns of enabling power to the
179          * memories.
180          */
181         udelay(1);
182
183         return 0;
184 }
185
186 static int gdsc_disable(struct generic_pm_domain *domain)
187 {
188         struct gdsc *sc = domain_to_gdsc(domain);
189         int ret;
190
191         if (sc->pwrsts == PWRSTS_ON)
192                 return gdsc_assert_reset(sc);
193
194         if (sc->pwrsts & PWRSTS_OFF)
195                 gdsc_clear_mem_on(sc);
196
197         ret = gdsc_toggle_logic(sc, false);
198         if (ret)
199                 return ret;
200
201         if (sc->flags & CLAMP_IO)
202                 gdsc_assert_clamp_io(sc);
203
204         return 0;
205 }
206
207 static int gdsc_init(struct gdsc *sc)
208 {
209         u32 mask, val;
210         int on, ret;
211         unsigned int reg;
212
213         /*
214          * Disable HW trigger: collapse/restore occur based on registers writes.
215          * Disable SW override: Use hardware state-machine for sequencing.
216          * Configure wait time between states.
217          */
218         mask = HW_CONTROL_MASK | SW_OVERRIDE_MASK |
219                EN_REST_WAIT_MASK | EN_FEW_WAIT_MASK | CLK_DIS_WAIT_MASK;
220         val = EN_REST_WAIT_VAL | EN_FEW_WAIT_VAL | CLK_DIS_WAIT_VAL;
221         ret = regmap_update_bits(sc->regmap, sc->gdscr, mask, val);
222         if (ret)
223                 return ret;
224
225         /* Force gdsc ON if only ON state is supported */
226         if (sc->pwrsts == PWRSTS_ON) {
227                 ret = gdsc_toggle_logic(sc, true);
228                 if (ret)
229                         return ret;
230         }
231
232         reg = sc->gds_hw_ctrl ? sc->gds_hw_ctrl : sc->gdscr;
233         on = gdsc_is_enabled(sc, reg);
234         if (on < 0)
235                 return on;
236
237         /*
238          * Votable GDSCs can be ON due to Vote from other masters.
239          * If a Votable GDSC is ON, make sure we have a Vote.
240          */
241         if ((sc->flags & VOTABLE) && on)
242                 gdsc_enable(&sc->pd);
243
244         if (on || (sc->pwrsts & PWRSTS_RET))
245                 gdsc_force_mem_on(sc);
246         else
247                 gdsc_clear_mem_on(sc);
248
249         sc->pd.power_off = gdsc_disable;
250         sc->pd.power_on = gdsc_enable;
251         pm_genpd_init(&sc->pd, NULL, !on);
252
253         return 0;
254 }
255
256 int gdsc_register(struct gdsc_desc *desc,
257                   struct reset_controller_dev *rcdev, struct regmap *regmap)
258 {
259         int i, ret;
260         struct genpd_onecell_data *data;
261         struct device *dev = desc->dev;
262         struct gdsc **scs = desc->scs;
263         size_t num = desc->num;
264
265         data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
266         if (!data)
267                 return -ENOMEM;
268
269         data->domains = devm_kcalloc(dev, num, sizeof(*data->domains),
270                                      GFP_KERNEL);
271         if (!data->domains)
272                 return -ENOMEM;
273
274         data->num_domains = num;
275         for (i = 0; i < num; i++) {
276                 if (!scs[i])
277                         continue;
278                 scs[i]->regmap = regmap;
279                 scs[i]->rcdev = rcdev;
280                 ret = gdsc_init(scs[i]);
281                 if (ret)
282                         return ret;
283                 data->domains[i] = &scs[i]->pd;
284         }
285
286         /* Add subdomains */
287         for (i = 0; i < num; i++) {
288                 if (!scs[i])
289                         continue;
290                 if (scs[i]->parent)
291                         pm_genpd_add_subdomain(scs[i]->parent, &scs[i]->pd);
292         }
293
294         return of_genpd_add_provider_onecell(dev->of_node, data);
295 }
296
297 void gdsc_unregister(struct gdsc_desc *desc)
298 {
299         int i;
300         struct device *dev = desc->dev;
301         struct gdsc **scs = desc->scs;
302         size_t num = desc->num;
303
304         /* Remove subdomains */
305         for (i = 0; i < num; i++) {
306                 if (!scs[i])
307                         continue;
308                 if (scs[i]->parent)
309                         pm_genpd_remove_subdomain(scs[i]->parent, &scs[i]->pd);
310         }
311         of_genpd_del_provider(dev->of_node);
312 }