Stephen Boyd | 45dd0e5 | 2015-08-06 16:07:42 +0530 | [diff] [blame] | 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/pm_domain.h> |
| 20 | #include <linux/regmap.h> |
| 21 | #include <linux/slab.h> |
| 22 | #include "gdsc.h" |
| 23 | |
| 24 | #define PWR_ON_MASK BIT(31) |
| 25 | #define EN_REST_WAIT_MASK GENMASK_ULL(23, 20) |
| 26 | #define EN_FEW_WAIT_MASK GENMASK_ULL(19, 16) |
| 27 | #define CLK_DIS_WAIT_MASK GENMASK_ULL(15, 12) |
| 28 | #define SW_OVERRIDE_MASK BIT(2) |
| 29 | #define HW_CONTROL_MASK BIT(1) |
| 30 | #define SW_COLLAPSE_MASK BIT(0) |
| 31 | |
| 32 | /* Wait 2^n CXO cycles between all states. Here, n=2 (4 cycles). */ |
| 33 | #define EN_REST_WAIT_VAL (0x2 << 20) |
| 34 | #define EN_FEW_WAIT_VAL (0x8 << 16) |
| 35 | #define CLK_DIS_WAIT_VAL (0x2 << 12) |
| 36 | |
Rajendra Nayak | 014e193 | 2015-08-06 16:07:44 +0530 | [diff] [blame^] | 37 | #define RETAIN_MEM BIT(14) |
| 38 | #define RETAIN_PERIPH BIT(13) |
| 39 | |
Stephen Boyd | 45dd0e5 | 2015-08-06 16:07:42 +0530 | [diff] [blame] | 40 | #define TIMEOUT_US 100 |
| 41 | |
| 42 | #define domain_to_gdsc(domain) container_of(domain, struct gdsc, pd) |
| 43 | |
| 44 | static int gdsc_is_enabled(struct gdsc *sc) |
| 45 | { |
| 46 | u32 val; |
| 47 | int ret; |
| 48 | |
| 49 | ret = regmap_read(sc->regmap, sc->gdscr, &val); |
| 50 | if (ret) |
| 51 | return ret; |
| 52 | |
| 53 | return !!(val & PWR_ON_MASK); |
| 54 | } |
| 55 | |
| 56 | static int gdsc_toggle_logic(struct gdsc *sc, bool en) |
| 57 | { |
| 58 | int ret; |
| 59 | u32 val = en ? 0 : SW_COLLAPSE_MASK; |
| 60 | u32 check = en ? PWR_ON_MASK : 0; |
| 61 | unsigned long timeout; |
| 62 | |
| 63 | ret = regmap_update_bits(sc->regmap, sc->gdscr, SW_COLLAPSE_MASK, val); |
| 64 | if (ret) |
| 65 | return ret; |
| 66 | |
| 67 | timeout = jiffies + usecs_to_jiffies(TIMEOUT_US); |
| 68 | do { |
| 69 | ret = regmap_read(sc->regmap, sc->gdscr, &val); |
| 70 | if (ret) |
| 71 | return ret; |
| 72 | |
| 73 | if ((val & PWR_ON_MASK) == check) |
| 74 | return 0; |
| 75 | } while (time_before(jiffies, timeout)); |
| 76 | |
| 77 | ret = regmap_read(sc->regmap, sc->gdscr, &val); |
| 78 | if (ret) |
| 79 | return ret; |
| 80 | |
| 81 | if ((val & PWR_ON_MASK) == check) |
| 82 | return 0; |
| 83 | |
| 84 | return -ETIMEDOUT; |
| 85 | } |
| 86 | |
Rajendra Nayak | 014e193 | 2015-08-06 16:07:44 +0530 | [diff] [blame^] | 87 | static inline void gdsc_force_mem_on(struct gdsc *sc) |
| 88 | { |
| 89 | int i; |
| 90 | u32 mask = RETAIN_MEM | RETAIN_PERIPH; |
| 91 | |
| 92 | for (i = 0; i < sc->cxc_count; i++) |
| 93 | regmap_update_bits(sc->regmap, sc->cxcs[i], mask, mask); |
| 94 | } |
| 95 | |
| 96 | static inline void gdsc_clear_mem_on(struct gdsc *sc) |
| 97 | { |
| 98 | int i; |
| 99 | u32 mask = RETAIN_MEM | RETAIN_PERIPH; |
| 100 | |
| 101 | for (i = 0; i < sc->cxc_count; i++) |
| 102 | regmap_update_bits(sc->regmap, sc->cxcs[i], mask, 0); |
| 103 | } |
| 104 | |
Stephen Boyd | 45dd0e5 | 2015-08-06 16:07:42 +0530 | [diff] [blame] | 105 | static int gdsc_enable(struct generic_pm_domain *domain) |
| 106 | { |
| 107 | struct gdsc *sc = domain_to_gdsc(domain); |
| 108 | int ret; |
| 109 | |
| 110 | ret = gdsc_toggle_logic(sc, true); |
| 111 | if (ret) |
| 112 | return ret; |
Rajendra Nayak | 014e193 | 2015-08-06 16:07:44 +0530 | [diff] [blame^] | 113 | |
| 114 | if (sc->pwrsts & PWRSTS_OFF) |
| 115 | gdsc_force_mem_on(sc); |
| 116 | |
Stephen Boyd | 45dd0e5 | 2015-08-06 16:07:42 +0530 | [diff] [blame] | 117 | /* |
| 118 | * If clocks to this power domain were already on, they will take an |
| 119 | * additional 4 clock cycles to re-enable after the power domain is |
| 120 | * enabled. Delay to account for this. A delay is also needed to ensure |
| 121 | * clocks are not enabled within 400ns of enabling power to the |
| 122 | * memories. |
| 123 | */ |
| 124 | udelay(1); |
| 125 | |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | static int gdsc_disable(struct generic_pm_domain *domain) |
| 130 | { |
| 131 | struct gdsc *sc = domain_to_gdsc(domain); |
| 132 | |
Rajendra Nayak | 014e193 | 2015-08-06 16:07:44 +0530 | [diff] [blame^] | 133 | if (sc->pwrsts & PWRSTS_OFF) |
| 134 | gdsc_clear_mem_on(sc); |
| 135 | |
Stephen Boyd | 45dd0e5 | 2015-08-06 16:07:42 +0530 | [diff] [blame] | 136 | return gdsc_toggle_logic(sc, false); |
| 137 | } |
| 138 | |
| 139 | static int gdsc_init(struct gdsc *sc) |
| 140 | { |
| 141 | u32 mask, val; |
| 142 | int on, ret; |
| 143 | |
| 144 | /* |
| 145 | * Disable HW trigger: collapse/restore occur based on registers writes. |
| 146 | * Disable SW override: Use hardware state-machine for sequencing. |
| 147 | * Configure wait time between states. |
| 148 | */ |
| 149 | mask = HW_CONTROL_MASK | SW_OVERRIDE_MASK | |
| 150 | EN_REST_WAIT_MASK | EN_FEW_WAIT_MASK | CLK_DIS_WAIT_MASK; |
| 151 | val = EN_REST_WAIT_VAL | EN_FEW_WAIT_VAL | CLK_DIS_WAIT_VAL; |
| 152 | ret = regmap_update_bits(sc->regmap, sc->gdscr, mask, val); |
| 153 | if (ret) |
| 154 | return ret; |
| 155 | |
| 156 | on = gdsc_is_enabled(sc); |
| 157 | if (on < 0) |
| 158 | return on; |
| 159 | |
Rajendra Nayak | 014e193 | 2015-08-06 16:07:44 +0530 | [diff] [blame^] | 160 | if (on || (sc->pwrsts & PWRSTS_RET)) |
| 161 | gdsc_force_mem_on(sc); |
| 162 | else |
| 163 | gdsc_clear_mem_on(sc); |
| 164 | |
Stephen Boyd | 45dd0e5 | 2015-08-06 16:07:42 +0530 | [diff] [blame] | 165 | sc->pd.power_off = gdsc_disable; |
| 166 | sc->pd.power_on = gdsc_enable; |
| 167 | pm_genpd_init(&sc->pd, NULL, !on); |
| 168 | |
| 169 | return 0; |
| 170 | } |
| 171 | |
| 172 | int gdsc_register(struct device *dev, struct gdsc **scs, size_t num, |
| 173 | struct regmap *regmap) |
| 174 | { |
| 175 | int i, ret; |
| 176 | struct genpd_onecell_data *data; |
| 177 | |
| 178 | data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); |
| 179 | if (!data) |
| 180 | return -ENOMEM; |
| 181 | |
| 182 | data->domains = devm_kcalloc(dev, num, sizeof(*data->domains), |
| 183 | GFP_KERNEL); |
| 184 | if (!data->domains) |
| 185 | return -ENOMEM; |
| 186 | |
| 187 | data->num_domains = num; |
| 188 | for (i = 0; i < num; i++) { |
| 189 | if (!scs[i]) |
| 190 | continue; |
| 191 | scs[i]->regmap = regmap; |
| 192 | ret = gdsc_init(scs[i]); |
| 193 | if (ret) |
| 194 | return ret; |
| 195 | data->domains[i] = &scs[i]->pd; |
| 196 | } |
| 197 | |
| 198 | return of_genpd_add_provider_onecell(dev->of_node, data); |
| 199 | } |
| 200 | |
| 201 | void gdsc_unregister(struct device *dev) |
| 202 | { |
| 203 | of_genpd_del_provider(dev->of_node); |
| 204 | } |