xref: /linux/drivers/clk/qcom/gdsc.c (revision 502d45774af09f1c681c754c4b7cdfb5d7f72fd9)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2015, 2017-2018, 2022, The Linux Foundation. All rights reserved.
4  */
5 
6 #include <linux/bitops.h>
7 #include <linux/delay.h>
8 #include <linux/err.h>
9 #include <linux/export.h>
10 #include <linux/interconnect.h>
11 #include <linux/jiffies.h>
12 #include <linux/kernel.h>
13 #include <linux/ktime.h>
14 #include <linux/pm_domain.h>
15 #include <linux/regmap.h>
16 #include <linux/regulator/consumer.h>
17 #include <linux/reset-controller.h>
18 #include <linux/slab.h>
19 #include "gdsc.h"
20 
21 #define PWR_ON_MASK		BIT(31)
22 #define EN_REST_WAIT_MASK	GENMASK_ULL(23, 20)
23 #define EN_FEW_WAIT_MASK	GENMASK_ULL(19, 16)
24 #define CLK_DIS_WAIT_MASK	GENMASK_ULL(15, 12)
25 #define SW_OVERRIDE_MASK	BIT(2)
26 #define HW_CONTROL_MASK		BIT(1)
27 #define SW_COLLAPSE_MASK	BIT(0)
28 #define GMEM_CLAMP_IO_MASK	BIT(0)
29 #define GMEM_RESET_MASK		BIT(4)
30 
31 /* CFG_GDSCR */
32 #define GDSC_POWER_UP_COMPLETE		BIT(16)
33 #define GDSC_POWER_DOWN_COMPLETE	BIT(15)
34 #define GDSC_RETAIN_FF_ENABLE		BIT(11)
35 #define CFG_GDSCR_OFFSET		0x4
36 
37 /* Wait 2^n CXO cycles between all states. Here, n=2 (4 cycles). */
38 #define EN_REST_WAIT_VAL	0x2
39 #define EN_FEW_WAIT_VAL		0x8
40 #define CLK_DIS_WAIT_VAL	0x2
41 
42 /* Transition delay shifts */
43 #define EN_REST_WAIT_SHIFT	20
44 #define EN_FEW_WAIT_SHIFT	16
45 #define CLK_DIS_WAIT_SHIFT	12
46 
47 #define RETAIN_MEM		BIT(14)
48 #define RETAIN_PERIPH		BIT(13)
49 
50 #define STATUS_POLL_TIMEOUT_US	2000
51 #define TIMEOUT_US		500
52 
53 #define domain_to_gdsc(domain) container_of(domain, struct gdsc, pd)
54 
55 enum gdsc_status {
56 	GDSC_OFF,
57 	GDSC_ON
58 };
59 
60 /* Returns 1 if GDSC status is status, 0 if not, and < 0 on error */
gdsc_check_status(struct gdsc * sc,enum gdsc_status status)61 static int gdsc_check_status(struct gdsc *sc, enum gdsc_status status)
62 {
63 	unsigned int reg;
64 	u32 val;
65 	int ret;
66 
67 	if (sc->flags & POLL_CFG_GDSCR)
68 		reg = sc->gdscr + CFG_GDSCR_OFFSET;
69 	else if (sc->gds_hw_ctrl)
70 		reg = sc->gds_hw_ctrl;
71 	else
72 		reg = sc->gdscr;
73 
74 	ret = regmap_read(sc->regmap, reg, &val);
75 	if (ret)
76 		return ret;
77 
78 	if (sc->flags & POLL_CFG_GDSCR) {
79 		switch (status) {
80 		case GDSC_ON:
81 			return !!(val & GDSC_POWER_UP_COMPLETE);
82 		case GDSC_OFF:
83 			return !!(val & GDSC_POWER_DOWN_COMPLETE);
84 		}
85 	}
86 
87 	switch (status) {
88 	case GDSC_ON:
89 		return !!(val & PWR_ON_MASK);
90 	case GDSC_OFF:
91 		return !(val & PWR_ON_MASK);
92 	}
93 
94 	return -EINVAL;
95 }
96 
gdsc_hwctrl(struct gdsc * sc,bool en)97 static int gdsc_hwctrl(struct gdsc *sc, bool en)
98 {
99 	u32 val = en ? HW_CONTROL_MASK : 0;
100 
101 	return regmap_update_bits(sc->regmap, sc->gdscr, HW_CONTROL_MASK, val);
102 }
103 
gdsc_poll_status(struct gdsc * sc,enum gdsc_status status)104 static int gdsc_poll_status(struct gdsc *sc, enum gdsc_status status)
105 {
106 	ktime_t start;
107 	int ret;
108 
109 	start = ktime_get();
110 	do {
111 		ret = gdsc_check_status(sc, status);
112 		if (ret < 0)
113 			return ret;
114 		if (ret)
115 			return 0;
116 	} while (ktime_us_delta(ktime_get(), start) < STATUS_POLL_TIMEOUT_US);
117 
118 	ret = gdsc_check_status(sc, status);
119 	if (ret < 0)
120 		return ret;
121 	if (ret)
122 		return 0;
123 
124 	return -ETIMEDOUT;
125 }
126 
gdsc_update_collapse_bit(struct gdsc * sc,bool val)127 static int gdsc_update_collapse_bit(struct gdsc *sc, bool val)
128 {
129 	u32 reg, mask;
130 	int ret;
131 
132 	if (sc->collapse_mask) {
133 		reg = sc->collapse_ctrl;
134 		mask = sc->collapse_mask;
135 	} else {
136 		reg = sc->gdscr;
137 		mask = SW_COLLAPSE_MASK;
138 	}
139 
140 	ret = regmap_update_bits(sc->regmap, reg, mask, val ? mask : 0);
141 	if (ret)
142 		return ret;
143 
144 	return 0;
145 }
146 
gdsc_toggle_logic(struct gdsc * sc,enum gdsc_status status,bool wait)147 static int gdsc_toggle_logic(struct gdsc *sc, enum gdsc_status status,
148 		bool wait)
149 {
150 	int ret;
151 
152 	if (status == GDSC_ON && sc->rsupply) {
153 		ret = regulator_enable(sc->rsupply);
154 		if (ret < 0)
155 			return ret;
156 	}
157 
158 	if (status == GDSC_ON) {
159 		ret = icc_set_bw(sc->icc_path, 1, 1);
160 		if (ret)
161 			goto err_disable_supply;
162 	}
163 
164 	ret = gdsc_update_collapse_bit(sc, status == GDSC_OFF);
165 
166 	/* If disabling votable gdscs, don't poll on status */
167 	if ((sc->flags & VOTABLE) && status == GDSC_OFF && !wait) {
168 		/*
169 		 * Add a short delay here to ensure that an enable
170 		 * right after it was disabled does not put it in an
171 		 * unknown state
172 		 */
173 		udelay(TIMEOUT_US);
174 		return 0;
175 	}
176 
177 	if (sc->gds_hw_ctrl) {
178 		/*
179 		 * The gds hw controller asserts/de-asserts the status bit soon
180 		 * after it receives a power on/off request from a master.
181 		 * The controller then takes around 8 xo cycles to start its
182 		 * internal state machine and update the status bit. During
183 		 * this time, the status bit does not reflect the true status
184 		 * of the core.
185 		 * Add a delay of 1 us between writing to the SW_COLLAPSE bit
186 		 * and polling the status bit.
187 		 */
188 		udelay(1);
189 	}
190 
191 	ret = gdsc_poll_status(sc, status);
192 	WARN(ret, "%s status stuck at 'o%s'", sc->pd.name, status ? "ff" : "n");
193 
194 	if (!ret && status == GDSC_OFF) {
195 		ret = icc_set_bw(sc->icc_path, 0, 0);
196 		if (ret)
197 			return ret;
198 	}
199 
200 	if (!ret && status == GDSC_OFF && sc->rsupply) {
201 		ret = regulator_disable(sc->rsupply);
202 		if (ret < 0)
203 			return ret;
204 	}
205 
206 	return ret;
207 
208 err_disable_supply:
209 	if (status == GDSC_ON && sc->rsupply)
210 		regulator_disable(sc->rsupply);
211 
212 	return ret;
213 }
214 
gdsc_deassert_reset(struct gdsc * sc)215 static inline int gdsc_deassert_reset(struct gdsc *sc)
216 {
217 	int i;
218 
219 	for (i = 0; i < sc->reset_count; i++)
220 		sc->rcdev->ops->deassert(sc->rcdev, sc->resets[i]);
221 	return 0;
222 }
223 
gdsc_assert_reset(struct gdsc * sc)224 static inline int gdsc_assert_reset(struct gdsc *sc)
225 {
226 	int i;
227 
228 	for (i = 0; i < sc->reset_count; i++)
229 		sc->rcdev->ops->assert(sc->rcdev, sc->resets[i]);
230 	return 0;
231 }
232 
gdsc_force_mem_on(struct gdsc * sc)233 static inline void gdsc_force_mem_on(struct gdsc *sc)
234 {
235 	int i;
236 	u32 mask = RETAIN_MEM;
237 
238 	if (!(sc->flags & NO_RET_PERIPH))
239 		mask |= RETAIN_PERIPH;
240 
241 	for (i = 0; i < sc->cxc_count; i++)
242 		regmap_update_bits(sc->regmap, sc->cxcs[i], mask, mask);
243 }
244 
gdsc_clear_mem_on(struct gdsc * sc)245 static inline void gdsc_clear_mem_on(struct gdsc *sc)
246 {
247 	int i;
248 	u32 mask = RETAIN_MEM;
249 
250 	if (!(sc->flags & NO_RET_PERIPH))
251 		mask |= RETAIN_PERIPH;
252 
253 	for (i = 0; i < sc->cxc_count; i++)
254 		regmap_update_bits(sc->regmap, sc->cxcs[i], mask, 0);
255 }
256 
gdsc_deassert_clamp_io(struct gdsc * sc)257 static inline void gdsc_deassert_clamp_io(struct gdsc *sc)
258 {
259 	regmap_update_bits(sc->regmap, sc->clamp_io_ctrl,
260 			   GMEM_CLAMP_IO_MASK, 0);
261 }
262 
gdsc_assert_clamp_io(struct gdsc * sc)263 static inline void gdsc_assert_clamp_io(struct gdsc *sc)
264 {
265 	regmap_update_bits(sc->regmap, sc->clamp_io_ctrl,
266 			   GMEM_CLAMP_IO_MASK, 1);
267 }
268 
gdsc_assert_reset_aon(struct gdsc * sc)269 static inline void gdsc_assert_reset_aon(struct gdsc *sc)
270 {
271 	regmap_update_bits(sc->regmap, sc->clamp_io_ctrl,
272 			   GMEM_RESET_MASK, 1);
273 	udelay(1);
274 	regmap_update_bits(sc->regmap, sc->clamp_io_ctrl,
275 			   GMEM_RESET_MASK, 0);
276 }
277 
gdsc_retain_ff_on(struct gdsc * sc)278 static void gdsc_retain_ff_on(struct gdsc *sc)
279 {
280 	u32 mask = GDSC_RETAIN_FF_ENABLE;
281 
282 	regmap_update_bits(sc->regmap, sc->gdscr, mask, mask);
283 }
284 
gdsc_enable(struct generic_pm_domain * domain)285 static int gdsc_enable(struct generic_pm_domain *domain)
286 {
287 	struct gdsc *sc = domain_to_gdsc(domain);
288 	int ret;
289 
290 	if (sc->pwrsts == PWRSTS_ON)
291 		return gdsc_deassert_reset(sc);
292 
293 	if (sc->flags & SW_RESET) {
294 		gdsc_assert_reset(sc);
295 		udelay(1);
296 		gdsc_deassert_reset(sc);
297 	}
298 
299 	if (sc->flags & CLAMP_IO) {
300 		if (sc->flags & AON_RESET)
301 			gdsc_assert_reset_aon(sc);
302 		gdsc_deassert_clamp_io(sc);
303 	}
304 
305 	ret = gdsc_toggle_logic(sc, GDSC_ON, false);
306 	if (ret)
307 		return ret;
308 
309 	if (sc->pwrsts & PWRSTS_OFF)
310 		gdsc_force_mem_on(sc);
311 
312 	/*
313 	 * If clocks to this power domain were already on, they will take an
314 	 * additional 4 clock cycles to re-enable after the power domain is
315 	 * enabled. Delay to account for this. A delay is also needed to ensure
316 	 * clocks are not enabled within 400ns of enabling power to the
317 	 * memories.
318 	 */
319 	udelay(1);
320 
321 	if (sc->flags & RETAIN_FF_ENABLE)
322 		gdsc_retain_ff_on(sc);
323 
324 	/* Turn on HW trigger mode if supported */
325 	if (sc->flags & HW_CTRL) {
326 		ret = gdsc_hwctrl(sc, true);
327 		if (ret)
328 			return ret;
329 		/*
330 		 * Wait for the GDSC to go through a power down and
331 		 * up cycle.  In case a firmware ends up polling status
332 		 * bits for the gdsc, it might read an 'on' status before
333 		 * the GDSC can finish the power cycle.
334 		 * We wait 1us before returning to ensure the firmware
335 		 * can't immediately poll the status bits.
336 		 */
337 		udelay(1);
338 	}
339 
340 	return 0;
341 }
342 
gdsc_disable(struct generic_pm_domain * domain)343 static int gdsc_disable(struct generic_pm_domain *domain)
344 {
345 	struct gdsc *sc = domain_to_gdsc(domain);
346 	int ret;
347 
348 	if (sc->pwrsts == PWRSTS_ON)
349 		return gdsc_assert_reset(sc);
350 
351 	/* Turn off HW trigger mode if supported */
352 	if (sc->flags & HW_CTRL) {
353 		ret = gdsc_hwctrl(sc, false);
354 		if (ret < 0)
355 			return ret;
356 		/*
357 		 * Wait for the GDSC to go through a power down and
358 		 * up cycle.  In case we end up polling status
359 		 * bits for the gdsc before the power cycle is completed
360 		 * it might read an 'on' status wrongly.
361 		 */
362 		udelay(1);
363 
364 		ret = gdsc_poll_status(sc, GDSC_ON);
365 		if (ret)
366 			return ret;
367 	}
368 
369 	if (sc->pwrsts & PWRSTS_OFF)
370 		gdsc_clear_mem_on(sc);
371 
372 	/*
373 	 * If the GDSC supports only a Retention state, apart from ON,
374 	 * leave it in ON state.
375 	 * There is no SW control to transition the GDSC into
376 	 * Retention state. This happens in HW when the parent
377 	 * domain goes down to a Low power state
378 	 */
379 	if (sc->pwrsts == PWRSTS_RET_ON)
380 		return 0;
381 
382 	ret = gdsc_toggle_logic(sc, GDSC_OFF, domain->synced_poweroff);
383 	if (ret)
384 		return ret;
385 
386 	if (sc->flags & CLAMP_IO)
387 		gdsc_assert_clamp_io(sc);
388 
389 	return 0;
390 }
391 
gdsc_set_hwmode(struct generic_pm_domain * domain,struct device * dev,bool mode)392 static int gdsc_set_hwmode(struct generic_pm_domain *domain, struct device *dev, bool mode)
393 {
394 	struct gdsc *sc = domain_to_gdsc(domain);
395 	int ret;
396 
397 	ret = gdsc_hwctrl(sc, mode);
398 	if (ret)
399 		return ret;
400 
401 	/*
402 	 * Wait for the GDSC to go through a power down and
403 	 * up cycle. If we poll the status register before the
404 	 * power cycle is finished we might read incorrect values.
405 	 */
406 	udelay(1);
407 
408 	/*
409 	 * When the GDSC is switched to HW mode, HW can disable the GDSC.
410 	 * When the GDSC is switched back to SW mode, the GDSC will be enabled
411 	 * again, hence we need to poll for GDSC to complete the power up.
412 	 */
413 	if (!mode)
414 		return gdsc_poll_status(sc, GDSC_ON);
415 
416 	return 0;
417 }
418 
gdsc_get_hwmode(struct generic_pm_domain * domain,struct device * dev)419 static bool gdsc_get_hwmode(struct generic_pm_domain *domain, struct device *dev)
420 {
421 	struct gdsc *sc = domain_to_gdsc(domain);
422 	u32 val;
423 
424 	regmap_read(sc->regmap, sc->gdscr, &val);
425 
426 	return !!(val & HW_CONTROL_MASK);
427 }
428 
gdsc_init(struct gdsc * sc)429 static int gdsc_init(struct gdsc *sc)
430 {
431 	u32 mask, val;
432 	int on, ret;
433 
434 	/*
435 	 * Disable HW trigger: collapse/restore occur based on registers writes.
436 	 * Disable SW override: Use hardware state-machine for sequencing.
437 	 * Configure wait time between states.
438 	 */
439 	mask = HW_CONTROL_MASK | SW_OVERRIDE_MASK |
440 	       EN_REST_WAIT_MASK | EN_FEW_WAIT_MASK | CLK_DIS_WAIT_MASK;
441 
442 	if (!sc->en_rest_wait_val)
443 		sc->en_rest_wait_val = EN_REST_WAIT_VAL;
444 	if (!sc->en_few_wait_val)
445 		sc->en_few_wait_val = EN_FEW_WAIT_VAL;
446 	if (!sc->clk_dis_wait_val)
447 		sc->clk_dis_wait_val = CLK_DIS_WAIT_VAL;
448 
449 	val = sc->en_rest_wait_val << EN_REST_WAIT_SHIFT |
450 		sc->en_few_wait_val << EN_FEW_WAIT_SHIFT |
451 		sc->clk_dis_wait_val << CLK_DIS_WAIT_SHIFT;
452 
453 	ret = regmap_update_bits(sc->regmap, sc->gdscr, mask, val);
454 	if (ret)
455 		return ret;
456 
457 	/* Force gdsc ON if only ON state is supported */
458 	if (sc->pwrsts == PWRSTS_ON) {
459 		ret = gdsc_toggle_logic(sc, GDSC_ON, false);
460 		if (ret)
461 			return ret;
462 	}
463 
464 	on = gdsc_check_status(sc, GDSC_ON);
465 	if (on < 0)
466 		return on;
467 
468 	if (on) {
469 		/* The regulator must be on, sync the kernel state */
470 		if (sc->rsupply) {
471 			ret = regulator_enable(sc->rsupply);
472 			if (ret < 0)
473 				return ret;
474 		}
475 
476 		/*
477 		 * Votable GDSCs can be ON due to Vote from other masters.
478 		 * If a Votable GDSC is ON, make sure we have a Vote.
479 		 */
480 		if (sc->flags & VOTABLE) {
481 			ret = gdsc_update_collapse_bit(sc, false);
482 			if (ret)
483 				goto err_disable_supply;
484 		}
485 
486 		/*
487 		 * Make sure the retain bit is set if the GDSC is already on,
488 		 * otherwise we end up turning off the GDSC and destroying all
489 		 * the register contents that we thought we were saving.
490 		 */
491 		if (sc->flags & RETAIN_FF_ENABLE)
492 			gdsc_retain_ff_on(sc);
493 
494 		/* Turn on HW trigger mode if supported */
495 		if (sc->flags & HW_CTRL) {
496 			ret = gdsc_hwctrl(sc, true);
497 			if (ret < 0)
498 				goto err_disable_supply;
499 		}
500 
501 	} else if (sc->flags & ALWAYS_ON) {
502 		/* If ALWAYS_ON GDSCs are not ON, turn them ON */
503 		ret = gdsc_enable(&sc->pd);
504 		if (ret)
505 			return ret;
506 		on = true;
507 	}
508 
509 	if (on || (sc->pwrsts & PWRSTS_RET))
510 		gdsc_force_mem_on(sc);
511 	else
512 		gdsc_clear_mem_on(sc);
513 
514 	if (sc->flags & ALWAYS_ON)
515 		sc->pd.flags |= GENPD_FLAG_ALWAYS_ON;
516 	if (!sc->pd.power_off)
517 		sc->pd.power_off = gdsc_disable;
518 	if (!sc->pd.power_on)
519 		sc->pd.power_on = gdsc_enable;
520 	if (sc->flags & HW_CTRL_TRIGGER) {
521 		sc->pd.set_hwmode_dev = gdsc_set_hwmode;
522 		sc->pd.get_hwmode_dev = gdsc_get_hwmode;
523 	}
524 
525 	ret = pm_genpd_init(&sc->pd, NULL, !on);
526 	if (ret)
527 		goto err_disable_supply;
528 
529 	return 0;
530 
531 err_disable_supply:
532 	if (on && sc->rsupply)
533 		regulator_disable(sc->rsupply);
534 
535 	return ret;
536 }
537 
gdsc_add_subdomain_list(struct dev_pm_domain_list * pd_list,struct generic_pm_domain * subdomain)538 static int gdsc_add_subdomain_list(struct dev_pm_domain_list *pd_list,
539 				   struct generic_pm_domain *subdomain)
540 {
541 	int i, ret;
542 
543 	for (i = 0; i < pd_list->num_pds; i++) {
544 		struct device *dev = pd_list->pd_devs[i];
545 		struct generic_pm_domain *genpd = pd_to_genpd(dev->pm_domain);
546 
547 		ret = pm_genpd_add_subdomain(genpd, subdomain);
548 		if (ret)
549 			goto remove_added_subdomains;
550 	}
551 
552 	return 0;
553 
554 remove_added_subdomains:
555 	for (i--; i >= 0; i--) {
556 		struct device *dev = pd_list->pd_devs[i];
557 		struct generic_pm_domain *genpd = pd_to_genpd(dev->pm_domain);
558 
559 		pm_genpd_remove_subdomain(genpd, subdomain);
560 	}
561 
562 	return ret;
563 }
564 
gdsc_remove_subdomain_list(struct dev_pm_domain_list * pd_list,struct generic_pm_domain * subdomain)565 static void gdsc_remove_subdomain_list(struct dev_pm_domain_list *pd_list,
566 				       struct generic_pm_domain *subdomain)
567 {
568 	int i;
569 
570 	for (i = 0; i < pd_list->num_pds; i++) {
571 		struct device *dev = pd_list->pd_devs[i];
572 		struct generic_pm_domain *genpd = pd_to_genpd(dev->pm_domain);
573 
574 		pm_genpd_remove_subdomain(genpd, subdomain);
575 	}
576 }
577 
gdsc_pm_subdomain_remove(struct gdsc_desc * desc,size_t num)578 static void gdsc_pm_subdomain_remove(struct gdsc_desc *desc, size_t num)
579 {
580 	struct device *dev = desc->dev;
581 	struct gdsc **scs = desc->scs;
582 	int i;
583 
584 	/* Remove subdomains */
585 	for (i = num - 1; i >= 0; i--) {
586 		if (!scs[i])
587 			continue;
588 		if (scs[i]->parent)
589 			pm_genpd_remove_subdomain(scs[i]->parent, &scs[i]->pd);
590 		else if (!IS_ERR_OR_NULL(dev->pm_domain))
591 			pm_genpd_remove_subdomain(pd_to_genpd(dev->pm_domain), &scs[i]->pd);
592 		else if (desc->pd_list)
593 			gdsc_remove_subdomain_list(desc->pd_list, &scs[i]->pd);
594 	}
595 }
596 
gdsc_register(struct gdsc_desc * desc,struct reset_controller_dev * rcdev,struct regmap * regmap)597 int gdsc_register(struct gdsc_desc *desc,
598 		  struct reset_controller_dev *rcdev, struct regmap *regmap)
599 {
600 	int i, ret;
601 	struct genpd_onecell_data *data;
602 	struct device *dev = desc->dev;
603 	struct gdsc **scs = desc->scs;
604 	size_t num = desc->num;
605 
606 	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
607 	if (!data)
608 		return -ENOMEM;
609 
610 	data->domains = devm_kcalloc(dev, num, sizeof(*data->domains),
611 				     GFP_KERNEL);
612 	if (!data->domains)
613 		return -ENOMEM;
614 
615 	for (i = 0; i < num; i++) {
616 		if (!scs[i] || !scs[i]->needs_icc)
617 			continue;
618 
619 		scs[i]->icc_path = devm_of_icc_get_by_index(dev, scs[i]->icc_path_index);
620 		if (IS_ERR(scs[i]->icc_path)) {
621 			ret = PTR_ERR(scs[i]->icc_path);
622 			if (ret != -ENODEV)
623 				return ret;
624 
625 			scs[i]->icc_path = NULL;
626 		}
627 	}
628 
629 	for (i = 0; i < num; i++) {
630 		if (!scs[i] || !scs[i]->supply)
631 			continue;
632 
633 		scs[i]->rsupply = devm_regulator_get_optional(dev, scs[i]->supply);
634 		if (IS_ERR(scs[i]->rsupply)) {
635 			ret = PTR_ERR(scs[i]->rsupply);
636 			if (ret != -ENODEV)
637 				return ret;
638 
639 			scs[i]->rsupply = NULL;
640 		}
641 	}
642 
643 	data->num_domains = num;
644 	for (i = 0; i < num; i++) {
645 		if (!scs[i])
646 			continue;
647 		scs[i]->regmap = regmap;
648 		scs[i]->rcdev = rcdev;
649 		ret = gdsc_init(scs[i]);
650 		if (ret)
651 			return ret;
652 		data->domains[i] = &scs[i]->pd;
653 	}
654 
655 	/* Add subdomains */
656 	for (i = 0; i < num; i++) {
657 		if (!scs[i])
658 			continue;
659 		if (scs[i]->parent)
660 			ret = pm_genpd_add_subdomain(scs[i]->parent, &scs[i]->pd);
661 		else if (!IS_ERR_OR_NULL(dev->pm_domain))
662 			ret = pm_genpd_add_subdomain(pd_to_genpd(dev->pm_domain), &scs[i]->pd);
663 		else if (desc->pd_list)
664 			ret = gdsc_add_subdomain_list(desc->pd_list, &scs[i]->pd);
665 
666 		if (ret)
667 			goto err_pm_subdomain_remove;
668 	}
669 
670 	return of_genpd_add_provider_onecell(dev->of_node, data);
671 
672 err_pm_subdomain_remove:
673 	gdsc_pm_subdomain_remove(desc, i);
674 
675 	return ret;
676 }
677 
gdsc_unregister(struct gdsc_desc * desc)678 void gdsc_unregister(struct gdsc_desc *desc)
679 {
680 	struct device *dev = desc->dev;
681 	struct gdsc **scs = desc->scs;
682 	size_t num = desc->num;
683 	int i;
684 
685 	of_genpd_del_provider(dev->of_node);
686 	gdsc_pm_subdomain_remove(desc, num);
687 
688 	for (i = 0; i < num; i++) {
689 		if (!scs[i])
690 			continue;
691 		pm_genpd_remove(&scs[i]->pd);
692 	}
693 }
694 
695 /*
696  * On SDM845+ the GPU GX domain is *almost* entirely controlled by the GMU
697  * running in the CX domain so the CPU doesn't need to know anything about the
698  * GX domain EXCEPT....
699  *
700  * Hardware constraints dictate that the GX be powered down before the CX. If
701  * the GMU crashes it could leave the GX on. In order to successfully bring back
702  * the device the CPU needs to disable the GX headswitch. There being no sane
703  * way to reach in and touch that register from deep inside the GPU driver we
704  * need to set up the infrastructure to be able to ensure that the GPU can
705  * ensure that the GX is off during this super special case. We do this by
706  * defining a GX gdsc with a dummy enable function and a "default" disable
707  * function.
708  *
709  * This allows us to attach with genpd_dev_pm_attach_by_name() in the GPU
710  * driver. During power up, nothing will happen from the CPU (and the GMU will
711  * power up normally but during power down this will ensure that the GX domain
712  * is *really* off - this gives us a semi standard way of doing what we need.
713  */
gdsc_gx_do_nothing_enable(struct generic_pm_domain * domain)714 int gdsc_gx_do_nothing_enable(struct generic_pm_domain *domain)
715 {
716 	struct gdsc *sc = domain_to_gdsc(domain);
717 	int ret = 0;
718 
719 	/* Enable the parent supply, when controlled through the regulator framework. */
720 	if (sc->rsupply)
721 		ret = regulator_enable(sc->rsupply);
722 
723 	/* Do nothing with the GDSC itself */
724 
725 	return ret;
726 }
727 EXPORT_SYMBOL_GPL(gdsc_gx_do_nothing_enable);
728 
729 /*
730  * GX GDSC is a special power domain. Normally, its disable sequence
731  * is managed by the GMU firmware, and high level OS must not attempt
732  * to disable it. The only exception is during GMU recovery, where the
733  * GMU driver can set GenPD’s synced_poweroff flag to allow explicitly
734  * disable GX GDSC in hardware.
735  */
gdsc_gx_disable(struct generic_pm_domain * domain)736 int gdsc_gx_disable(struct generic_pm_domain *domain)
737 {
738 	struct gdsc *sc = domain_to_gdsc(domain);
739 
740 	if (domain->synced_poweroff)
741 		return gdsc_disable(domain);
742 
743 	/* Remove parent-supply placed in enable */
744 	if (sc->rsupply)
745 		return regulator_disable(sc->rsupply);
746 
747 	return 0;
748 }
749 EXPORT_SYMBOL_GPL(gdsc_gx_disable);
750