Meng Wang | 43bbb87 | 2018-12-10 12:32:05 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Laxminath Kasam | 7adc34e | 2018-11-09 11:24:38 +0530 | [diff] [blame] | 2 | /* |
Laxminath Kasam | ea2b058 | 2018-12-03 20:30:02 +0530 | [diff] [blame] | 3 | * Copyright (c) 2015-2019, The Linux Foundation. All rights reserved. |
Laxminath Kasam | 7adc34e | 2018-11-09 11:24:38 +0530 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <linux/module.h> |
| 7 | #include <linux/slab.h> |
| 8 | #include <sound/soc.h> |
| 9 | #include <linux/kernel.h> |
| 10 | #include <linux/delay.h> |
| 11 | #include <asoc/wcd9xxx_registers.h> |
Meng Wang | 11a25cf | 2018-10-31 14:11:26 +0800 | [diff] [blame] | 12 | #include <asoc/wcd-clsh.h> |
Laxminath Kasam | 7adc34e | 2018-11-09 11:24:38 +0530 | [diff] [blame] | 13 | |
| 14 | #define WCD_USLEEP_RANGE 50 |
| 15 | |
Meng Wang | 15c825d | 2018-09-06 10:49:18 +0800 | [diff] [blame] | 16 | static void (*clsh_state_fp[NUM_CLSH_STATES])(struct snd_soc_component *, |
Laxminath Kasam | 7adc34e | 2018-11-09 11:24:38 +0530 | [diff] [blame] | 17 | struct wcd_clsh_cdc_info *, |
| 18 | u8 req_state, bool en, int mode); |
| 19 | |
| 20 | static const char *mode_to_str(int mode) |
| 21 | { |
| 22 | switch (mode) { |
| 23 | case CLS_H_NORMAL: |
| 24 | return WCD_CLSH_STRINGIFY(CLS_H_NORMAL); |
| 25 | case CLS_H_HIFI: |
| 26 | return WCD_CLSH_STRINGIFY(CLS_H_HIFI); |
| 27 | case CLS_H_LOHIFI: |
| 28 | return WCD_CLSH_STRINGIFY(CLS_H_LOHIFI); |
| 29 | case CLS_H_LP: |
| 30 | return WCD_CLSH_STRINGIFY(CLS_H_LP); |
| 31 | case CLS_H_ULP: |
| 32 | return WCD_CLSH_STRINGIFY(CLS_H_ULP); |
| 33 | case CLS_AB: |
| 34 | return WCD_CLSH_STRINGIFY(CLS_AB); |
| 35 | case CLS_AB_HIFI: |
| 36 | return WCD_CLSH_STRINGIFY(CLS_AB_HIFI); |
Karthikeyan Mani | 7f609a3 | 2019-05-17 16:59:53 -0700 | [diff] [blame^] | 37 | case CLS_AB_LP: |
| 38 | return WCD_CLSH_STRINGIFY(CLS_AB_LP); |
| 39 | case CLS_AB_LOHIFI: |
| 40 | return WCD_CLSH_STRINGIFY(CLS_AB_LOHIFI); |
Laxminath Kasam | 7adc34e | 2018-11-09 11:24:38 +0530 | [diff] [blame] | 41 | default: |
| 42 | return WCD_CLSH_STRINGIFY(CLS_H_INVALID); |
| 43 | }; |
| 44 | } |
| 45 | |
| 46 | static const char *state_to_str(u8 state, char *buf, size_t buflen) |
| 47 | { |
| 48 | int i; |
| 49 | int cnt = 0; |
| 50 | /* |
| 51 | * This array of strings should match with enum wcd_clsh_state_bit. |
| 52 | */ |
| 53 | static const char *const states[] = { |
| 54 | "STATE_EAR", |
| 55 | "STATE_HPH_L", |
| 56 | "STATE_HPH_R", |
| 57 | "STATE_AUX", |
| 58 | }; |
| 59 | |
| 60 | if (state == WCD_CLSH_STATE_IDLE) { |
| 61 | snprintf(buf, buflen, "[STATE_IDLE]"); |
| 62 | goto done; |
| 63 | } |
| 64 | |
| 65 | buf[0] = '\0'; |
| 66 | for (i = 0; i < ARRAY_SIZE(states); i++) { |
| 67 | if (!(state & (1 << i))) |
| 68 | continue; |
| 69 | cnt = snprintf(buf, buflen - cnt - 1, "%s%s%s", buf, |
| 70 | buf[0] == '\0' ? "[" : "|", |
| 71 | states[i]); |
| 72 | } |
| 73 | if (cnt > 0) |
| 74 | strlcat(buf + cnt, "]", buflen); |
| 75 | |
| 76 | done: |
| 77 | if (buf[0] == '\0') |
| 78 | snprintf(buf, buflen, "[STATE_UNKNOWN]"); |
| 79 | return buf; |
| 80 | } |
| 81 | |
| 82 | static inline int wcd_clsh_get_int_mode(struct wcd_clsh_cdc_info *clsh_d, |
| 83 | int clsh_state) |
| 84 | { |
| 85 | int mode; |
| 86 | |
| 87 | if ((clsh_state != WCD_CLSH_STATE_EAR) && |
| 88 | (clsh_state != WCD_CLSH_STATE_HPHL) && |
| 89 | (clsh_state != WCD_CLSH_STATE_HPHR) && |
| 90 | (clsh_state != WCD_CLSH_STATE_AUX)) |
| 91 | mode = CLS_NONE; |
| 92 | else |
| 93 | mode = clsh_d->interpolator_modes[ffs(clsh_state)]; |
| 94 | |
| 95 | return mode; |
| 96 | } |
| 97 | |
| 98 | static inline void wcd_clsh_set_int_mode(struct wcd_clsh_cdc_info *clsh_d, |
| 99 | int clsh_state, int mode) |
| 100 | { |
| 101 | if ((clsh_state != WCD_CLSH_STATE_EAR) && |
| 102 | (clsh_state != WCD_CLSH_STATE_HPHL) && |
| 103 | (clsh_state != WCD_CLSH_STATE_HPHR) && |
| 104 | (clsh_state != WCD_CLSH_STATE_AUX)) |
| 105 | return; |
| 106 | |
| 107 | clsh_d->interpolator_modes[ffs(clsh_state)] = mode; |
| 108 | } |
| 109 | |
Meng Wang | 15c825d | 2018-09-06 10:49:18 +0800 | [diff] [blame] | 110 | static inline void wcd_clsh_set_buck_mode(struct snd_soc_component *component, |
Laxminath Kasam | 7adc34e | 2018-11-09 11:24:38 +0530 | [diff] [blame] | 111 | int mode) |
| 112 | { |
| 113 | if (mode == CLS_H_HIFI || mode == CLS_H_LOHIFI || |
Karthikeyan Mani | 7f609a3 | 2019-05-17 16:59:53 -0700 | [diff] [blame^] | 114 | mode == CLS_AB_HIFI || mode == CLS_AB_LOHIFI) |
Meng Wang | 15c825d | 2018-09-06 10:49:18 +0800 | [diff] [blame] | 115 | snd_soc_component_update_bits(component, |
| 116 | WCD9XXX_ANA_RX_SUPPLIES, |
| 117 | 0x08, 0x08); /* set to HIFI */ |
Laxminath Kasam | 7adc34e | 2018-11-09 11:24:38 +0530 | [diff] [blame] | 118 | else |
Meng Wang | 15c825d | 2018-09-06 10:49:18 +0800 | [diff] [blame] | 119 | snd_soc_component_update_bits(component, |
| 120 | WCD9XXX_ANA_RX_SUPPLIES, |
| 121 | 0x08, 0x00); /* set to default */ |
Laxminath Kasam | 7adc34e | 2018-11-09 11:24:38 +0530 | [diff] [blame] | 122 | } |
| 123 | |
Meng Wang | 15c825d | 2018-09-06 10:49:18 +0800 | [diff] [blame] | 124 | static inline void wcd_clsh_set_flyback_mode( |
| 125 | struct snd_soc_component *component, |
| 126 | int mode) |
Laxminath Kasam | 7adc34e | 2018-11-09 11:24:38 +0530 | [diff] [blame] | 127 | { |
| 128 | if (mode == CLS_H_HIFI || mode == CLS_H_LOHIFI || |
Karthikeyan Mani | 7f609a3 | 2019-05-17 16:59:53 -0700 | [diff] [blame^] | 129 | mode == CLS_AB_HIFI || mode == CLS_AB_LOHIFI) { |
Meng Wang | 15c825d | 2018-09-06 10:49:18 +0800 | [diff] [blame] | 130 | snd_soc_component_update_bits(component, |
| 131 | WCD9XXX_ANA_RX_SUPPLIES, |
| 132 | 0x04, 0x04); |
| 133 | snd_soc_component_update_bits(component, |
| 134 | WCD9XXX_FLYBACK_VNEG_CTRL_4, |
| 135 | 0xF0, 0x80); |
Laxminath Kasam | 7adc34e | 2018-11-09 11:24:38 +0530 | [diff] [blame] | 136 | } else { |
Meng Wang | 15c825d | 2018-09-06 10:49:18 +0800 | [diff] [blame] | 137 | snd_soc_component_update_bits(component, |
| 138 | WCD9XXX_ANA_RX_SUPPLIES, |
| 139 | 0x04, 0x00); /* set to Default */ |
| 140 | snd_soc_component_update_bits(component, |
| 141 | WCD9XXX_FLYBACK_VNEG_CTRL_4, |
| 142 | 0xF0, 0x70); |
Laxminath Kasam | 7adc34e | 2018-11-09 11:24:38 +0530 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | |
Meng Wang | 15c825d | 2018-09-06 10:49:18 +0800 | [diff] [blame] | 146 | static inline void wcd_clsh_force_iq_ctl(struct snd_soc_component *component, |
Laxminath Kasam | 7adc34e | 2018-11-09 11:24:38 +0530 | [diff] [blame] | 147 | int mode, bool enable) |
| 148 | { |
| 149 | if (enable) { |
Meng Wang | 15c825d | 2018-09-06 10:49:18 +0800 | [diff] [blame] | 150 | snd_soc_component_update_bits(component, |
| 151 | WCD9XXX_FLYBACK_VNEGDAC_CTRL_2, |
| 152 | 0xE0, 0xA0); |
Laxminath Kasam | 7adc34e | 2018-11-09 11:24:38 +0530 | [diff] [blame] | 153 | /* 100usec delay is needed as per HW requirement */ |
| 154 | usleep_range(100, 110); |
Meng Wang | 15c825d | 2018-09-06 10:49:18 +0800 | [diff] [blame] | 155 | snd_soc_component_update_bits(component, |
| 156 | WCD9XXX_CLASSH_MODE_3, |
| 157 | 0x02, 0x02); |
| 158 | snd_soc_component_update_bits(component, |
| 159 | WCD9XXX_CLASSH_MODE_2, |
| 160 | 0xFF, 0x1C); |
Karthikeyan Mani | 7f609a3 | 2019-05-17 16:59:53 -0700 | [diff] [blame^] | 161 | if (mode == CLS_H_LOHIFI || mode == CLS_AB_LOHIFI) { |
Meng Wang | 15c825d | 2018-09-06 10:49:18 +0800 | [diff] [blame] | 162 | snd_soc_component_update_bits(component, |
| 163 | WCD9XXX_HPH_NEW_INT_PA_MISC2, |
| 164 | 0x20, 0x20); |
| 165 | snd_soc_component_update_bits(component, |
| 166 | WCD9XXX_RX_BIAS_HPH_LOWPOWER, |
| 167 | 0xF0, 0xC0); |
| 168 | snd_soc_component_update_bits(component, |
| 169 | WCD9XXX_HPH_PA_CTL1, |
| 170 | 0x0E, 0x02); |
Laxminath Kasam | 7adc34e | 2018-11-09 11:24:38 +0530 | [diff] [blame] | 171 | } |
| 172 | } else { |
Meng Wang | 15c825d | 2018-09-06 10:49:18 +0800 | [diff] [blame] | 173 | snd_soc_component_update_bits(component, |
| 174 | WCD9XXX_HPH_NEW_INT_PA_MISC2, |
| 175 | 0x20, 0x00); |
| 176 | snd_soc_component_update_bits(component, |
| 177 | WCD9XXX_RX_BIAS_HPH_LOWPOWER, |
| 178 | 0xF0, 0x80); |
| 179 | snd_soc_component_update_bits(component, |
| 180 | WCD9XXX_HPH_PA_CTL1, |
| 181 | 0x0E, 0x06); |
Laxminath Kasam | 7adc34e | 2018-11-09 11:24:38 +0530 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | |
Meng Wang | 15c825d | 2018-09-06 10:49:18 +0800 | [diff] [blame] | 185 | static void wcd_clsh_buck_ctrl(struct snd_soc_component *component, |
Laxminath Kasam | 7adc34e | 2018-11-09 11:24:38 +0530 | [diff] [blame] | 186 | struct wcd_clsh_cdc_info *clsh_d, |
| 187 | int mode, |
| 188 | bool enable) |
| 189 | { |
| 190 | /* enable/disable buck */ |
| 191 | if ((enable && (++clsh_d->buck_users == 1)) || |
| 192 | (!enable && (--clsh_d->buck_users == 0))) { |
Meng Wang | 15c825d | 2018-09-06 10:49:18 +0800 | [diff] [blame] | 193 | snd_soc_component_update_bits(component, |
| 194 | WCD9XXX_ANA_RX_SUPPLIES, |
| 195 | (1 << 7), (enable << 7)); |
Laxminath Kasam | 7adc34e | 2018-11-09 11:24:38 +0530 | [diff] [blame] | 196 | /* |
| 197 | * 500us sleep is required after buck enable/disable |
| 198 | * as per HW requirement |
| 199 | */ |
| 200 | usleep_range(500, 510); |
| 201 | if (mode == CLS_H_LOHIFI || mode == CLS_H_ULP || |
| 202 | mode == CLS_H_HIFI || mode == CLS_H_LP) |
Meng Wang | 15c825d | 2018-09-06 10:49:18 +0800 | [diff] [blame] | 203 | snd_soc_component_update_bits(component, |
| 204 | WCD9XXX_CLASSH_MODE_3, |
| 205 | 0x02, 0x00); |
Laxminath Kasam | 7adc34e | 2018-11-09 11:24:38 +0530 | [diff] [blame] | 206 | |
Meng Wang | 15c825d | 2018-09-06 10:49:18 +0800 | [diff] [blame] | 207 | snd_soc_component_update_bits(component, |
| 208 | WCD9XXX_CLASSH_MODE_2, |
| 209 | 0xFF, 0x3A); |
Laxminath Kasam | 7adc34e | 2018-11-09 11:24:38 +0530 | [diff] [blame] | 210 | /* 500usec delay is needed as per HW requirement */ |
| 211 | usleep_range(500, 500 + WCD_USLEEP_RANGE); |
| 212 | } |
Meng Wang | 15c825d | 2018-09-06 10:49:18 +0800 | [diff] [blame] | 213 | dev_dbg(component->dev, "%s: buck_users %d, enable %d, mode: %s\n", |
Laxminath Kasam | 7adc34e | 2018-11-09 11:24:38 +0530 | [diff] [blame] | 214 | __func__, clsh_d->buck_users, enable, mode_to_str(mode)); |
| 215 | } |
| 216 | |
Meng Wang | 15c825d | 2018-09-06 10:49:18 +0800 | [diff] [blame] | 217 | static void wcd_clsh_flyback_ctrl(struct snd_soc_component *component, |
Laxminath Kasam | 7adc34e | 2018-11-09 11:24:38 +0530 | [diff] [blame] | 218 | struct wcd_clsh_cdc_info *clsh_d, |
| 219 | int mode, |
| 220 | bool enable) |
| 221 | { |
| 222 | /* enable/disable flyback */ |
| 223 | if ((enable && (++clsh_d->flyback_users == 1)) || |
| 224 | (!enable && (--clsh_d->flyback_users == 0))) { |
Meng Wang | 15c825d | 2018-09-06 10:49:18 +0800 | [diff] [blame] | 225 | snd_soc_component_update_bits(component, |
Laxminath Kasam | f16ed2f | 2019-03-13 12:21:26 +0530 | [diff] [blame] | 226 | WCD9XXX_FLYBACK_VNEG_CTRL_1, |
| 227 | 0xE0, 0xE0); |
| 228 | snd_soc_component_update_bits(component, |
Meng Wang | 15c825d | 2018-09-06 10:49:18 +0800 | [diff] [blame] | 229 | WCD9XXX_ANA_RX_SUPPLIES, |
| 230 | (1 << 6), (enable << 6)); |
Laxminath Kasam | 7adc34e | 2018-11-09 11:24:38 +0530 | [diff] [blame] | 231 | /* |
| 232 | * 100us sleep is required after flyback enable/disable |
| 233 | * as per HW requirement |
| 234 | */ |
| 235 | usleep_range(100, 110); |
Meng Wang | 15c825d | 2018-09-06 10:49:18 +0800 | [diff] [blame] | 236 | snd_soc_component_update_bits(component, |
| 237 | WCD9XXX_FLYBACK_VNEGDAC_CTRL_2, |
| 238 | 0xE0, 0xE0); |
Laxminath Kasam | 7adc34e | 2018-11-09 11:24:38 +0530 | [diff] [blame] | 239 | /* 500usec delay is needed as per HW requirement */ |
| 240 | usleep_range(500, 500 + WCD_USLEEP_RANGE); |
| 241 | } |
Meng Wang | 15c825d | 2018-09-06 10:49:18 +0800 | [diff] [blame] | 242 | dev_dbg(component->dev, "%s: flyback_users %d, enable %d, mode: %s\n", |
Laxminath Kasam | 7adc34e | 2018-11-09 11:24:38 +0530 | [diff] [blame] | 243 | __func__, clsh_d->flyback_users, enable, mode_to_str(mode)); |
| 244 | } |
| 245 | |
Meng Wang | 15c825d | 2018-09-06 10:49:18 +0800 | [diff] [blame] | 246 | static void wcd_clsh_set_hph_mode(struct snd_soc_component *component, |
Laxminath Kasam | 7adc34e | 2018-11-09 11:24:38 +0530 | [diff] [blame] | 247 | int mode) |
| 248 | { |
| 249 | u8 val = 0; |
| 250 | |
| 251 | switch (mode) { |
| 252 | case CLS_H_NORMAL: |
Laxminath Kasam | 7adc34e | 2018-11-09 11:24:38 +0530 | [diff] [blame] | 253 | val = 0x00; |
| 254 | break; |
| 255 | case CLS_AB: |
| 256 | case CLS_H_ULP: |
| 257 | val = 0x0C; |
| 258 | break; |
| 259 | case CLS_AB_HIFI: |
| 260 | case CLS_H_HIFI: |
| 261 | val = 0x08; |
| 262 | break; |
| 263 | case CLS_H_LP: |
Laxminath Kasam | ea2b058 | 2018-12-03 20:30:02 +0530 | [diff] [blame] | 264 | case CLS_H_LOHIFI: |
Karthikeyan Mani | 7f609a3 | 2019-05-17 16:59:53 -0700 | [diff] [blame^] | 265 | case CLS_AB_LP: |
| 266 | case CLS_AB_LOHIFI: |
Laxminath Kasam | 7adc34e | 2018-11-09 11:24:38 +0530 | [diff] [blame] | 267 | val = 0x04; |
| 268 | break; |
| 269 | default: |
Meng Wang | 15c825d | 2018-09-06 10:49:18 +0800 | [diff] [blame] | 270 | dev_err(component->dev, "%s:Invalid mode %d\n", __func__, mode); |
Laxminath Kasam | 7adc34e | 2018-11-09 11:24:38 +0530 | [diff] [blame] | 271 | return; |
| 272 | }; |
| 273 | |
Meng Wang | 15c825d | 2018-09-06 10:49:18 +0800 | [diff] [blame] | 274 | snd_soc_component_update_bits(component, WCD9XXX_ANA_HPH, 0x0C, val); |
Laxminath Kasam | 7adc34e | 2018-11-09 11:24:38 +0530 | [diff] [blame] | 275 | } |
| 276 | |
Meng Wang | 15c825d | 2018-09-06 10:49:18 +0800 | [diff] [blame] | 277 | static void wcd_clsh_set_flyback_current(struct snd_soc_component *component, |
| 278 | int mode) |
Laxminath Kasam | 7adc34e | 2018-11-09 11:24:38 +0530 | [diff] [blame] | 279 | { |
Meng Wang | 15c825d | 2018-09-06 10:49:18 +0800 | [diff] [blame] | 280 | snd_soc_component_update_bits(component, WCD9XXX_RX_BIAS_FLYB_BUFF, |
| 281 | 0x0F, 0x0A); |
| 282 | snd_soc_component_update_bits(component, WCD9XXX_RX_BIAS_FLYB_BUFF, |
| 283 | 0xF0, 0xA0); |
Laxminath Kasam | 7adc34e | 2018-11-09 11:24:38 +0530 | [diff] [blame] | 284 | /* Sleep needed to avoid click and pop as per HW requirement */ |
| 285 | usleep_range(100, 110); |
| 286 | } |
| 287 | |
Meng Wang | 15c825d | 2018-09-06 10:49:18 +0800 | [diff] [blame] | 288 | static void wcd_clsh_set_buck_regulator_mode( |
| 289 | struct snd_soc_component *component, |
| 290 | int mode) |
Laxminath Kasam | 7adc34e | 2018-11-09 11:24:38 +0530 | [diff] [blame] | 291 | { |
Meng Wang | 15c825d | 2018-09-06 10:49:18 +0800 | [diff] [blame] | 292 | snd_soc_component_update_bits(component, WCD9XXX_ANA_RX_SUPPLIES, |
Laxminath Kasam | 7adc34e | 2018-11-09 11:24:38 +0530 | [diff] [blame] | 293 | 0x02, 0x00); |
| 294 | } |
| 295 | |
Meng Wang | 15c825d | 2018-09-06 10:49:18 +0800 | [diff] [blame] | 296 | static void wcd_clsh_state_ear_aux(struct snd_soc_component *component, |
Laxminath Kasam | 7adc34e | 2018-11-09 11:24:38 +0530 | [diff] [blame] | 297 | struct wcd_clsh_cdc_info *clsh_d, |
| 298 | u8 req_state, bool is_enable, int mode) |
| 299 | { |
Meng Wang | 15c825d | 2018-09-06 10:49:18 +0800 | [diff] [blame] | 300 | dev_dbg(component->dev, "%s: mode: %s, %s\n", __func__, |
| 301 | mode_to_str(mode), is_enable ? "enable" : "disable"); |
Laxminath Kasam | 7adc34e | 2018-11-09 11:24:38 +0530 | [diff] [blame] | 302 | } |
| 303 | |
Meng Wang | 15c825d | 2018-09-06 10:49:18 +0800 | [diff] [blame] | 304 | static void wcd_clsh_state_hph_aux(struct snd_soc_component *component, |
Laxminath Kasam | 7adc34e | 2018-11-09 11:24:38 +0530 | [diff] [blame] | 305 | struct wcd_clsh_cdc_info *clsh_d, |
| 306 | u8 req_state, bool is_enable, int mode) |
| 307 | { |
Meng Wang | 15c825d | 2018-09-06 10:49:18 +0800 | [diff] [blame] | 308 | dev_dbg(component->dev, "%s: mode: %s, %s\n", __func__, |
| 309 | mode_to_str(mode), is_enable ? "enable" : "disable"); |
Laxminath Kasam | 7adc34e | 2018-11-09 11:24:38 +0530 | [diff] [blame] | 310 | } |
| 311 | |
Meng Wang | 15c825d | 2018-09-06 10:49:18 +0800 | [diff] [blame] | 312 | static void wcd_clsh_state_hph_ear(struct snd_soc_component *component, |
Laxminath Kasam | 7adc34e | 2018-11-09 11:24:38 +0530 | [diff] [blame] | 313 | struct wcd_clsh_cdc_info *clsh_d, |
| 314 | u8 req_state, bool is_enable, int mode) |
| 315 | { |
Meng Wang | 15c825d | 2018-09-06 10:49:18 +0800 | [diff] [blame] | 316 | dev_dbg(component->dev, "%s: mode: %s, %s\n", __func__, |
| 317 | mode_to_str(mode), is_enable ? "enable" : "disable"); |
Laxminath Kasam | 7adc34e | 2018-11-09 11:24:38 +0530 | [diff] [blame] | 318 | } |
| 319 | |
Meng Wang | 15c825d | 2018-09-06 10:49:18 +0800 | [diff] [blame] | 320 | static void wcd_clsh_state_hph_st(struct snd_soc_component *component, |
Laxminath Kasam | 7adc34e | 2018-11-09 11:24:38 +0530 | [diff] [blame] | 321 | struct wcd_clsh_cdc_info *clsh_d, |
| 322 | u8 req_state, bool is_enable, int mode) |
| 323 | { |
Meng Wang | 15c825d | 2018-09-06 10:49:18 +0800 | [diff] [blame] | 324 | dev_dbg(component->dev, "%s: mode: %s, %s\n", __func__, |
| 325 | mode_to_str(mode), is_enable ? "enable" : "disable"); |
Laxminath Kasam | 7adc34e | 2018-11-09 11:24:38 +0530 | [diff] [blame] | 326 | } |
| 327 | |
Meng Wang | 15c825d | 2018-09-06 10:49:18 +0800 | [diff] [blame] | 328 | static void wcd_clsh_state_hph_r(struct snd_soc_component *component, |
Laxminath Kasam | 7adc34e | 2018-11-09 11:24:38 +0530 | [diff] [blame] | 329 | struct wcd_clsh_cdc_info *clsh_d, |
| 330 | u8 req_state, bool is_enable, int mode) |
| 331 | { |
Meng Wang | 15c825d | 2018-09-06 10:49:18 +0800 | [diff] [blame] | 332 | dev_dbg(component->dev, "%s: mode: %s, %s\n", __func__, |
| 333 | mode_to_str(mode), is_enable ? "enable" : "disable"); |
Laxminath Kasam | 7adc34e | 2018-11-09 11:24:38 +0530 | [diff] [blame] | 334 | |
| 335 | if (mode == CLS_H_NORMAL) { |
Meng Wang | 15c825d | 2018-09-06 10:49:18 +0800 | [diff] [blame] | 336 | dev_dbg(component->dev, "%s: Normal mode not applicable for hph_r\n", |
Laxminath Kasam | 7adc34e | 2018-11-09 11:24:38 +0530 | [diff] [blame] | 337 | __func__); |
| 338 | return; |
| 339 | } |
| 340 | |
| 341 | if (is_enable) { |
Meng Wang | 15c825d | 2018-09-06 10:49:18 +0800 | [diff] [blame] | 342 | wcd_clsh_set_buck_regulator_mode(component, mode); |
| 343 | wcd_clsh_set_flyback_mode(component, mode); |
| 344 | wcd_clsh_force_iq_ctl(component, mode, true); |
| 345 | wcd_clsh_flyback_ctrl(component, clsh_d, mode, true); |
| 346 | wcd_clsh_set_flyback_current(component, mode); |
| 347 | wcd_clsh_set_buck_mode(component, mode); |
| 348 | wcd_clsh_buck_ctrl(component, clsh_d, mode, true); |
| 349 | wcd_clsh_set_hph_mode(component, mode); |
Laxminath Kasam | 7adc34e | 2018-11-09 11:24:38 +0530 | [diff] [blame] | 350 | } else { |
Meng Wang | 15c825d | 2018-09-06 10:49:18 +0800 | [diff] [blame] | 351 | wcd_clsh_set_hph_mode(component, CLS_H_NORMAL); |
Laxminath Kasam | 7adc34e | 2018-11-09 11:24:38 +0530 | [diff] [blame] | 352 | |
| 353 | /* buck and flyback set to default mode and disable */ |
Meng Wang | 15c825d | 2018-09-06 10:49:18 +0800 | [diff] [blame] | 354 | wcd_clsh_flyback_ctrl(component, clsh_d, CLS_H_NORMAL, false); |
| 355 | wcd_clsh_buck_ctrl(component, clsh_d, CLS_H_NORMAL, false); |
| 356 | wcd_clsh_force_iq_ctl(component, CLS_H_NORMAL, false); |
| 357 | wcd_clsh_set_flyback_mode(component, CLS_H_NORMAL); |
| 358 | wcd_clsh_set_buck_mode(component, CLS_H_NORMAL); |
Laxminath Kasam | 7adc34e | 2018-11-09 11:24:38 +0530 | [diff] [blame] | 359 | } |
| 360 | } |
| 361 | |
Meng Wang | 15c825d | 2018-09-06 10:49:18 +0800 | [diff] [blame] | 362 | static void wcd_clsh_state_hph_l(struct snd_soc_component *component, |
Laxminath Kasam | 7adc34e | 2018-11-09 11:24:38 +0530 | [diff] [blame] | 363 | struct wcd_clsh_cdc_info *clsh_d, |
| 364 | u8 req_state, bool is_enable, int mode) |
| 365 | { |
Meng Wang | 15c825d | 2018-09-06 10:49:18 +0800 | [diff] [blame] | 366 | dev_dbg(component->dev, "%s: mode: %s, %s\n", __func__, |
| 367 | mode_to_str(mode), is_enable ? "enable" : "disable"); |
Laxminath Kasam | 7adc34e | 2018-11-09 11:24:38 +0530 | [diff] [blame] | 368 | |
| 369 | if (mode == CLS_H_NORMAL) { |
Meng Wang | 15c825d | 2018-09-06 10:49:18 +0800 | [diff] [blame] | 370 | dev_dbg(component->dev, "%s: Normal mode not applicable for hph_l\n", |
Laxminath Kasam | 7adc34e | 2018-11-09 11:24:38 +0530 | [diff] [blame] | 371 | __func__); |
| 372 | return; |
| 373 | } |
| 374 | |
| 375 | if (is_enable) { |
Meng Wang | 15c825d | 2018-09-06 10:49:18 +0800 | [diff] [blame] | 376 | wcd_clsh_set_buck_regulator_mode(component, mode); |
| 377 | wcd_clsh_set_flyback_mode(component, mode); |
| 378 | wcd_clsh_force_iq_ctl(component, mode, true); |
| 379 | wcd_clsh_flyback_ctrl(component, clsh_d, mode, true); |
| 380 | wcd_clsh_set_flyback_current(component, mode); |
| 381 | wcd_clsh_set_buck_mode(component, mode); |
| 382 | wcd_clsh_buck_ctrl(component, clsh_d, mode, true); |
| 383 | wcd_clsh_set_hph_mode(component, mode); |
Laxminath Kasam | 7adc34e | 2018-11-09 11:24:38 +0530 | [diff] [blame] | 384 | } else { |
Meng Wang | 15c825d | 2018-09-06 10:49:18 +0800 | [diff] [blame] | 385 | wcd_clsh_set_hph_mode(component, CLS_H_NORMAL); |
Laxminath Kasam | 7adc34e | 2018-11-09 11:24:38 +0530 | [diff] [blame] | 386 | |
| 387 | /* set buck and flyback to Default Mode */ |
Meng Wang | 15c825d | 2018-09-06 10:49:18 +0800 | [diff] [blame] | 388 | wcd_clsh_flyback_ctrl(component, clsh_d, CLS_H_NORMAL, false); |
| 389 | wcd_clsh_buck_ctrl(component, clsh_d, CLS_H_NORMAL, false); |
| 390 | wcd_clsh_force_iq_ctl(component, CLS_H_NORMAL, false); |
| 391 | wcd_clsh_set_flyback_mode(component, CLS_H_NORMAL); |
| 392 | wcd_clsh_set_buck_mode(component, CLS_H_NORMAL); |
Laxminath Kasam | 7adc34e | 2018-11-09 11:24:38 +0530 | [diff] [blame] | 393 | } |
| 394 | } |
| 395 | |
Meng Wang | 15c825d | 2018-09-06 10:49:18 +0800 | [diff] [blame] | 396 | static void wcd_clsh_state_aux(struct snd_soc_component *component, |
Laxminath Kasam | 7adc34e | 2018-11-09 11:24:38 +0530 | [diff] [blame] | 397 | struct wcd_clsh_cdc_info *clsh_d, |
| 398 | u8 req_state, bool is_enable, int mode) |
| 399 | { |
Meng Wang | 15c825d | 2018-09-06 10:49:18 +0800 | [diff] [blame] | 400 | dev_dbg(component->dev, "%s: mode: %s, %s\n", __func__, |
| 401 | mode_to_str(mode), is_enable ? "enable" : "disable"); |
Laxminath Kasam | 7adc34e | 2018-11-09 11:24:38 +0530 | [diff] [blame] | 402 | |
| 403 | if (is_enable) { |
Meng Wang | 15c825d | 2018-09-06 10:49:18 +0800 | [diff] [blame] | 404 | wcd_clsh_set_buck_mode(component, mode); |
| 405 | wcd_clsh_set_flyback_mode(component, mode); |
| 406 | wcd_clsh_flyback_ctrl(component, clsh_d, mode, true); |
| 407 | wcd_clsh_set_flyback_current(component, mode); |
| 408 | wcd_clsh_buck_ctrl(component, clsh_d, mode, true); |
Laxminath Kasam | 7adc34e | 2018-11-09 11:24:38 +0530 | [diff] [blame] | 409 | } else { |
Meng Wang | 15c825d | 2018-09-06 10:49:18 +0800 | [diff] [blame] | 410 | wcd_clsh_buck_ctrl(component, clsh_d, mode, false); |
| 411 | wcd_clsh_flyback_ctrl(component, clsh_d, mode, false); |
| 412 | wcd_clsh_set_flyback_mode(component, CLS_H_NORMAL); |
| 413 | wcd_clsh_set_buck_mode(component, CLS_H_NORMAL); |
Laxminath Kasam | 7adc34e | 2018-11-09 11:24:38 +0530 | [diff] [blame] | 414 | } |
| 415 | } |
| 416 | |
Meng Wang | 15c825d | 2018-09-06 10:49:18 +0800 | [diff] [blame] | 417 | static void wcd_clsh_state_ear(struct snd_soc_component *component, |
Laxminath Kasam | 7adc34e | 2018-11-09 11:24:38 +0530 | [diff] [blame] | 418 | struct wcd_clsh_cdc_info *clsh_d, |
| 419 | u8 req_state, bool is_enable, int mode) |
| 420 | { |
Meng Wang | 15c825d | 2018-09-06 10:49:18 +0800 | [diff] [blame] | 421 | dev_dbg(component->dev, "%s: mode: %s, %s\n", __func__, |
| 422 | mode_to_str(mode), |
Laxminath Kasam | 7adc34e | 2018-11-09 11:24:38 +0530 | [diff] [blame] | 423 | is_enable ? "enable" : "disable"); |
| 424 | |
| 425 | if (is_enable) { |
Meng Wang | 15c825d | 2018-09-06 10:49:18 +0800 | [diff] [blame] | 426 | wcd_clsh_set_buck_regulator_mode(component, mode); |
| 427 | wcd_clsh_set_flyback_mode(component, mode); |
| 428 | wcd_clsh_force_iq_ctl(component, mode, true); |
| 429 | wcd_clsh_flyback_ctrl(component, clsh_d, mode, true); |
| 430 | wcd_clsh_set_flyback_current(component, mode); |
| 431 | wcd_clsh_set_buck_mode(component, mode); |
| 432 | wcd_clsh_buck_ctrl(component, clsh_d, mode, true); |
| 433 | wcd_clsh_set_hph_mode(component, mode); |
Laxminath Kasam | 7adc34e | 2018-11-09 11:24:38 +0530 | [diff] [blame] | 434 | } else { |
Meng Wang | 15c825d | 2018-09-06 10:49:18 +0800 | [diff] [blame] | 435 | wcd_clsh_set_hph_mode(component, CLS_H_NORMAL); |
Laxminath Kasam | 7adc34e | 2018-11-09 11:24:38 +0530 | [diff] [blame] | 436 | |
| 437 | /* set buck and flyback to Default Mode */ |
Meng Wang | 15c825d | 2018-09-06 10:49:18 +0800 | [diff] [blame] | 438 | wcd_clsh_flyback_ctrl(component, clsh_d, CLS_H_NORMAL, false); |
| 439 | wcd_clsh_buck_ctrl(component, clsh_d, CLS_H_NORMAL, false); |
| 440 | wcd_clsh_force_iq_ctl(component, CLS_H_NORMAL, false); |
| 441 | wcd_clsh_set_flyback_mode(component, CLS_H_NORMAL); |
| 442 | wcd_clsh_set_buck_mode(component, CLS_H_NORMAL); |
Laxminath Kasam | 7adc34e | 2018-11-09 11:24:38 +0530 | [diff] [blame] | 443 | } |
| 444 | } |
| 445 | |
Meng Wang | 15c825d | 2018-09-06 10:49:18 +0800 | [diff] [blame] | 446 | static void wcd_clsh_state_err(struct snd_soc_component *component, |
Laxminath Kasam | 7adc34e | 2018-11-09 11:24:38 +0530 | [diff] [blame] | 447 | struct wcd_clsh_cdc_info *clsh_d, |
| 448 | u8 req_state, bool is_enable, int mode) |
| 449 | { |
| 450 | char msg[128]; |
| 451 | |
Meng Wang | 15c825d | 2018-09-06 10:49:18 +0800 | [diff] [blame] | 452 | dev_err(component->dev, |
Laxminath Kasam | 7adc34e | 2018-11-09 11:24:38 +0530 | [diff] [blame] | 453 | "%s Wrong request for class H state machine requested to %s %s\n", |
| 454 | __func__, is_enable ? "enable" : "disable", |
| 455 | state_to_str(req_state, msg, sizeof(msg))); |
| 456 | } |
| 457 | |
| 458 | /* |
| 459 | * Function: wcd_clsh_is_state_valid |
| 460 | * Params: state |
| 461 | * Description: |
| 462 | * Provides information on valid states of Class H configuration |
| 463 | */ |
| 464 | static bool wcd_clsh_is_state_valid(u8 state) |
| 465 | { |
| 466 | switch (state) { |
| 467 | case WCD_CLSH_STATE_IDLE: |
| 468 | case WCD_CLSH_STATE_EAR: |
| 469 | case WCD_CLSH_STATE_HPHL: |
| 470 | case WCD_CLSH_STATE_HPHR: |
| 471 | case WCD_CLSH_STATE_HPH_ST: |
| 472 | case WCD_CLSH_STATE_AUX: |
| 473 | case WCD_CLSH_STATE_HPHL_AUX: |
| 474 | case WCD_CLSH_STATE_HPHR_AUX: |
| 475 | case WCD_CLSH_STATE_HPH_ST_AUX: |
| 476 | case WCD_CLSH_STATE_EAR_AUX: |
| 477 | return true; |
| 478 | default: |
| 479 | return false; |
| 480 | }; |
| 481 | } |
| 482 | |
| 483 | /* |
| 484 | * Function: wcd_cls_h_fsm |
Meng Wang | 15c825d | 2018-09-06 10:49:18 +0800 | [diff] [blame] | 485 | * Params: component, cdc_clsh_d, req_state, req_type, clsh_event |
Laxminath Kasam | 7adc34e | 2018-11-09 11:24:38 +0530 | [diff] [blame] | 486 | * Description: |
| 487 | * This function handles PRE DAC and POST DAC conditions of different devices |
| 488 | * and updates class H configuration of different combination of devices |
| 489 | * based on validity of their states. cdc_clsh_d will contain current |
| 490 | * class h state information |
| 491 | */ |
Meng Wang | 15c825d | 2018-09-06 10:49:18 +0800 | [diff] [blame] | 492 | void wcd_cls_h_fsm(struct snd_soc_component *component, |
Laxminath Kasam | 7adc34e | 2018-11-09 11:24:38 +0530 | [diff] [blame] | 493 | struct wcd_clsh_cdc_info *cdc_clsh_d, |
| 494 | u8 clsh_event, u8 req_state, |
| 495 | int int_mode) |
| 496 | { |
| 497 | u8 old_state, new_state; |
| 498 | char msg0[128], msg1[128]; |
| 499 | |
| 500 | switch (clsh_event) { |
| 501 | case WCD_CLSH_EVENT_PRE_DAC: |
| 502 | old_state = cdc_clsh_d->state; |
| 503 | new_state = old_state | req_state; |
| 504 | |
| 505 | if (!wcd_clsh_is_state_valid(new_state)) { |
Meng Wang | 15c825d | 2018-09-06 10:49:18 +0800 | [diff] [blame] | 506 | dev_err(component->dev, |
Laxminath Kasam | 7adc34e | 2018-11-09 11:24:38 +0530 | [diff] [blame] | 507 | "%s: Class-H not a valid new state: %s\n", |
| 508 | __func__, |
| 509 | state_to_str(new_state, msg0, sizeof(msg0))); |
| 510 | return; |
| 511 | } |
| 512 | if (new_state == old_state) { |
Meng Wang | 15c825d | 2018-09-06 10:49:18 +0800 | [diff] [blame] | 513 | dev_err(component->dev, |
Laxminath Kasam | 7adc34e | 2018-11-09 11:24:38 +0530 | [diff] [blame] | 514 | "%s: Class-H already in requested state: %s\n", |
| 515 | __func__, |
| 516 | state_to_str(new_state, msg0, sizeof(msg0))); |
| 517 | return; |
| 518 | } |
| 519 | cdc_clsh_d->state = new_state; |
| 520 | wcd_clsh_set_int_mode(cdc_clsh_d, req_state, int_mode); |
Meng Wang | 15c825d | 2018-09-06 10:49:18 +0800 | [diff] [blame] | 521 | (*clsh_state_fp[new_state]) (component, cdc_clsh_d, req_state, |
Laxminath Kasam | 7adc34e | 2018-11-09 11:24:38 +0530 | [diff] [blame] | 522 | CLSH_REQ_ENABLE, int_mode); |
Meng Wang | 15c825d | 2018-09-06 10:49:18 +0800 | [diff] [blame] | 523 | dev_dbg(component->dev, |
Laxminath Kasam | 7adc34e | 2018-11-09 11:24:38 +0530 | [diff] [blame] | 524 | "%s: ClassH state transition from %s to %s\n", |
| 525 | __func__, state_to_str(old_state, msg0, sizeof(msg0)), |
| 526 | state_to_str(cdc_clsh_d->state, msg1, sizeof(msg1))); |
| 527 | break; |
| 528 | case WCD_CLSH_EVENT_POST_PA: |
| 529 | old_state = cdc_clsh_d->state; |
| 530 | new_state = old_state & (~req_state); |
| 531 | if (new_state < NUM_CLSH_STATES) { |
| 532 | if (!wcd_clsh_is_state_valid(old_state)) { |
Meng Wang | 15c825d | 2018-09-06 10:49:18 +0800 | [diff] [blame] | 533 | dev_err(component->dev, |
Laxminath Kasam | 7adc34e | 2018-11-09 11:24:38 +0530 | [diff] [blame] | 534 | "%s:Invalid old state:%s\n", |
| 535 | __func__, |
| 536 | state_to_str(old_state, msg0, |
| 537 | sizeof(msg0))); |
| 538 | return; |
| 539 | } |
| 540 | if (new_state == old_state) { |
Meng Wang | 15c825d | 2018-09-06 10:49:18 +0800 | [diff] [blame] | 541 | dev_err(component->dev, |
Laxminath Kasam | 7adc34e | 2018-11-09 11:24:38 +0530 | [diff] [blame] | 542 | "%s: Class-H already in requested state: %s\n", |
| 543 | __func__, |
| 544 | state_to_str(new_state, msg0, |
| 545 | sizeof(msg0))); |
| 546 | return; |
| 547 | } |
Meng Wang | 15c825d | 2018-09-06 10:49:18 +0800 | [diff] [blame] | 548 | (*clsh_state_fp[old_state]) (component, cdc_clsh_d, |
Laxminath Kasam | 7adc34e | 2018-11-09 11:24:38 +0530 | [diff] [blame] | 549 | req_state, CLSH_REQ_DISABLE, |
| 550 | int_mode); |
| 551 | cdc_clsh_d->state = new_state; |
| 552 | wcd_clsh_set_int_mode(cdc_clsh_d, req_state, CLS_NONE); |
Meng Wang | 15c825d | 2018-09-06 10:49:18 +0800 | [diff] [blame] | 553 | dev_dbg(component->dev, "%s: ClassH state transition from %s to %s\n", |
Laxminath Kasam | 7adc34e | 2018-11-09 11:24:38 +0530 | [diff] [blame] | 554 | __func__, state_to_str(old_state, msg0, |
| 555 | sizeof(msg0)), |
| 556 | state_to_str(cdc_clsh_d->state, msg1, |
| 557 | sizeof(msg1))); |
| 558 | } |
| 559 | break; |
| 560 | }; |
| 561 | } |
| 562 | EXPORT_SYMBOL(wcd_cls_h_fsm); |
| 563 | |
| 564 | /* |
| 565 | * wcd_cls_h_init: Called to init clsh info |
| 566 | * |
| 567 | * @clsh: pointer for clsh state information. |
| 568 | */ |
| 569 | void wcd_cls_h_init(struct wcd_clsh_cdc_info *clsh) |
| 570 | { |
| 571 | int i; |
| 572 | |
| 573 | clsh->state = WCD_CLSH_STATE_IDLE; |
| 574 | |
| 575 | for (i = 0; i < NUM_CLSH_STATES; i++) |
| 576 | clsh_state_fp[i] = wcd_clsh_state_err; |
| 577 | |
| 578 | clsh_state_fp[WCD_CLSH_STATE_EAR] = wcd_clsh_state_ear; |
| 579 | clsh_state_fp[WCD_CLSH_STATE_HPHL] = wcd_clsh_state_hph_l; |
| 580 | clsh_state_fp[WCD_CLSH_STATE_HPHR] = wcd_clsh_state_hph_r; |
| 581 | clsh_state_fp[WCD_CLSH_STATE_HPH_ST] = wcd_clsh_state_hph_st; |
| 582 | clsh_state_fp[WCD_CLSH_STATE_AUX] = wcd_clsh_state_aux; |
| 583 | clsh_state_fp[WCD_CLSH_STATE_HPHL_AUX] = wcd_clsh_state_hph_aux; |
| 584 | clsh_state_fp[WCD_CLSH_STATE_HPHR_AUX] = wcd_clsh_state_hph_aux; |
| 585 | clsh_state_fp[WCD_CLSH_STATE_HPH_ST_AUX] = |
| 586 | wcd_clsh_state_hph_aux; |
| 587 | clsh_state_fp[WCD_CLSH_STATE_EAR_AUX] = wcd_clsh_state_ear_aux; |
| 588 | clsh_state_fp[WCD_CLSH_STATE_HPHL_EAR] = wcd_clsh_state_hph_ear; |
| 589 | clsh_state_fp[WCD_CLSH_STATE_HPHR_EAR] = wcd_clsh_state_hph_ear; |
| 590 | clsh_state_fp[WCD_CLSH_STATE_HPH_ST_EAR] = wcd_clsh_state_hph_ear; |
| 591 | /* Set interpolaotr modes to NONE */ |
| 592 | wcd_clsh_set_int_mode(clsh, WCD_CLSH_STATE_EAR, CLS_NONE); |
| 593 | wcd_clsh_set_int_mode(clsh, WCD_CLSH_STATE_HPHL, CLS_NONE); |
| 594 | wcd_clsh_set_int_mode(clsh, WCD_CLSH_STATE_HPHR, CLS_NONE); |
| 595 | wcd_clsh_set_int_mode(clsh, WCD_CLSH_STATE_AUX, CLS_NONE); |
| 596 | clsh->flyback_users = 0; |
| 597 | clsh->buck_users = 0; |
| 598 | } |
| 599 | EXPORT_SYMBOL(wcd_cls_h_init); |
| 600 | |
| 601 | MODULE_DESCRIPTION("WCD Class-H Driver"); |
| 602 | MODULE_LICENSE("GPL v2"); |