Bhalchandra Gajare | 87fef4c | 2013-02-19 14:57:03 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2013, The Linux Foundation. All rights reserved. |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/module.h> |
| 14 | #include <sound/soc.h> |
| 15 | #include <linux/kernel.h> |
| 16 | #include <linux/delay.h> |
| 17 | #include <linux/mfd/wcd9xxx/wcd9xxx_registers.h> |
| 18 | #include "wcd9xxx-common.h" |
| 19 | |
| 20 | #define CLSH_COMPUTE_EAR 0x01 |
| 21 | #define CLSH_COMPUTE_HPH_L 0x02 |
| 22 | #define CLSH_COMPUTE_HPH_R 0x03 |
| 23 | |
| 24 | #define BUCK_VREF_2V 0xFF |
| 25 | #define BUCK_VREF_1P8V 0xE6 |
| 26 | |
| 27 | #define NCP_FCLK_LEVEL_8 0x08 |
| 28 | #define NCP_FCLK_LEVEL_5 0x05 |
| 29 | |
| 30 | #define BUCK_SETTLE_TIME_US 50 |
| 31 | #define NCP_SETTLE_TIME_US 50 |
| 32 | |
| 33 | static inline void wcd9xxx_enable_clsh_block( |
| 34 | struct snd_soc_codec *codec, |
| 35 | bool on) |
| 36 | { |
| 37 | snd_soc_update_bits(codec, WCD9XXX_A_CDC_CLSH_B1_CTL, |
| 38 | 0x01, on ? 0x01 : 0x00); |
| 39 | } |
| 40 | |
| 41 | static inline void wcd9xxx_enable_anc_delay( |
| 42 | struct snd_soc_codec *codec, |
| 43 | bool on) |
| 44 | { |
| 45 | snd_soc_update_bits(codec, WCD9XXX_A_CDC_CLSH_B1_CTL, |
| 46 | 0x02, on ? 0x02 : 0x00); |
| 47 | } |
| 48 | |
| 49 | static inline void wcd9xxx_enable_ncp( |
| 50 | struct snd_soc_codec *codec, |
| 51 | bool on) |
| 52 | { |
| 53 | snd_soc_update_bits(codec, WCD9XXX_A_NCP_EN, |
| 54 | 0x01, on ? 0x01 : 0x00); |
| 55 | } |
| 56 | |
| 57 | static inline void wcd9xxx_enable_buck( |
| 58 | struct snd_soc_codec *codec, |
| 59 | bool on) |
| 60 | { |
| 61 | snd_soc_update_bits(codec, WCD9XXX_A_BUCK_MODE_1, |
| 62 | 0x80, on ? 0x80 : 0x00); |
| 63 | } |
| 64 | |
| 65 | static int cdc_lo_count; |
| 66 | |
| 67 | static void (*clsh_state_fp[NUM_CLSH_STATES]) |
| 68 | (struct snd_soc_codec *, |
| 69 | struct wcd9xxx_clsh_cdc_data *, |
| 70 | u8 req_state, bool req_type); |
| 71 | |
| 72 | static const char *state_to_str(u8 state) |
| 73 | { |
| 74 | if (state == WCD9XXX_CLSH_STATE_IDLE) |
| 75 | return "STATE_IDLE"; |
| 76 | else if (state == WCD9XXX_CLSH_STATE_EAR) |
| 77 | return "STATE_EAR"; |
| 78 | else if (state == WCD9XXX_CLSH_STATE_HPHL) |
| 79 | return "STATE_HPH_L"; |
| 80 | else if (state == WCD9XXX_CLSH_STATE_HPHR) |
| 81 | return "STATE_HPH_R"; |
| 82 | else if (state == (WCD9XXX_CLSH_STATE_HPHL |
| 83 | | WCD9XXX_CLSH_STATE_HPHR)) |
| 84 | return "STATE_HPH_L_R"; |
| 85 | else if (state == WCD9XXX_CLSH_STATE_LO) |
| 86 | return "STATE_LO"; |
| 87 | |
| 88 | return "UNKNOWN_STATE"; |
| 89 | } |
| 90 | |
| 91 | static void wcd9xxx_cfg_clsh_buck( |
| 92 | struct snd_soc_codec *codec) |
| 93 | { |
| 94 | int i; |
| 95 | const struct wcd9xxx_reg_mask_val reg_set[] = { |
| 96 | {WCD9XXX_A_BUCK_CTRL_CCL_4, 0x0B, 0x00}, |
| 97 | {WCD9XXX_A_BUCK_CTRL_CCL_1, 0xF0, 0x50}, |
| 98 | {WCD9XXX_A_BUCK_CTRL_CCL_3, 0x03, 0x00}, |
| 99 | {WCD9XXX_A_BUCK_CTRL_CCL_3, 0x0B, 0x00}, |
| 100 | }; |
| 101 | |
| 102 | for (i = 0; i < ARRAY_SIZE(reg_set); i++) |
| 103 | snd_soc_update_bits(codec, reg_set[i].reg, reg_set[i].mask, |
| 104 | reg_set[i].val); |
| 105 | |
| 106 | dev_dbg(codec->dev, "%s: Programmed buck parameters", __func__); |
| 107 | } |
| 108 | |
| 109 | static void wcd9xxx_cfg_clsh_param_common( |
| 110 | struct snd_soc_codec *codec) |
| 111 | { |
| 112 | int i; |
| 113 | const struct wcd9xxx_reg_mask_val reg_set[] = { |
| 114 | {WCD9XXX_A_CDC_CLSH_BUCK_NCP_VARS, 0x3 << 0, 0}, |
| 115 | {WCD9XXX_A_CDC_CLSH_BUCK_NCP_VARS, 0x3 << 2, 1 << 2}, |
| 116 | {WCD9XXX_A_CDC_CLSH_BUCK_NCP_VARS, (0x1 << 4), 0}, |
| 117 | {WCD9XXX_A_CDC_CLSH_B2_CTL, (0x3 << 0), 0x01}, |
| 118 | {WCD9XXX_A_CDC_CLSH_B2_CTL, (0x3 << 2), (0x01 << 2)}, |
| 119 | {WCD9XXX_A_CDC_CLSH_B2_CTL, (0xf << 4), (0x03 << 4)}, |
| 120 | {WCD9XXX_A_CDC_CLSH_B3_CTL, (0xf << 4), (0x03 << 4)}, |
| 121 | {WCD9XXX_A_CDC_CLSH_B3_CTL, (0xf << 0), (0x0B)}, |
| 122 | {WCD9XXX_A_CDC_CLSH_B1_CTL, (0x1 << 5), (0x01 << 5)}, |
| 123 | {WCD9XXX_A_CDC_CLSH_B1_CTL, (0x1 << 1), (0x01 << 1)}, |
| 124 | }; |
| 125 | |
| 126 | for (i = 0; i < ARRAY_SIZE(reg_set); i++) |
| 127 | snd_soc_update_bits(codec, reg_set[i].reg, reg_set[i].mask, |
| 128 | reg_set[i].val); |
| 129 | |
| 130 | dev_dbg(codec->dev, "%s: Programmed class H controller common parameters", |
| 131 | __func__); |
| 132 | } |
| 133 | |
| 134 | static void wcd9xxx_chargepump_request( |
| 135 | struct snd_soc_codec *codec, bool on) |
| 136 | { |
| 137 | static int cp_count; |
| 138 | |
| 139 | if (on && (++cp_count == 1)) { |
| 140 | snd_soc_update_bits(codec, WCD9XXX_A_CDC_CLK_OTHR_CTL, |
| 141 | 0x01, 0x01); |
Bhalchandra Gajare | db8ffcb | 2013-02-27 13:58:17 -0800 | [diff] [blame] | 142 | dev_dbg(codec->dev, "%s: Charge Pump enabled, count = %d\n", |
Bhalchandra Gajare | 87fef4c | 2013-02-19 14:57:03 -0800 | [diff] [blame] | 143 | __func__, cp_count); |
| 144 | } |
| 145 | |
| 146 | else if (!on) { |
| 147 | if (--cp_count < 0) { |
| 148 | dev_dbg(codec->dev, "%s: Unbalanced disable for charge pump\n", |
| 149 | __func__); |
| 150 | if (snd_soc_read(codec, WCD9XXX_A_CDC_CLK_OTHR_CTL) |
| 151 | & 0x01) { |
Bhalchandra Gajare | db8ffcb | 2013-02-27 13:58:17 -0800 | [diff] [blame] | 152 | dev_dbg(codec->dev, "%s: Actual chargepump is ON\n", |
Bhalchandra Gajare | 87fef4c | 2013-02-19 14:57:03 -0800 | [diff] [blame] | 153 | __func__); |
| 154 | } |
| 155 | cp_count = 0; |
| 156 | WARN_ON(1); |
| 157 | } |
| 158 | |
| 159 | if (cp_count == 0) { |
| 160 | snd_soc_update_bits(codec, WCD9XXX_A_CDC_CLK_OTHR_CTL, |
| 161 | 0x01, 0x00); |
| 162 | dev_dbg(codec->dev, "%s: Charge pump disabled, count = %d\n", |
| 163 | __func__, cp_count); |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | |
Joonwoo Park | a08e055 | 2013-03-05 18:28:23 -0800 | [diff] [blame] | 168 | static void wcd9xxx_clsh_comp_req(struct snd_soc_codec *codec, |
| 169 | struct wcd9xxx_clsh_cdc_data *clsh_d, |
| 170 | int compute_pa, bool on) |
Bhalchandra Gajare | 87fef4c | 2013-02-19 14:57:03 -0800 | [diff] [blame] | 171 | { |
Joonwoo Park | a08e055 | 2013-03-05 18:28:23 -0800 | [diff] [blame] | 172 | u8 shift; |
Bhalchandra Gajare | 87fef4c | 2013-02-19 14:57:03 -0800 | [diff] [blame] | 173 | |
Joonwoo Park | a08e055 | 2013-03-05 18:28:23 -0800 | [diff] [blame] | 174 | if (compute_pa == CLSH_COMPUTE_EAR) { |
| 175 | snd_soc_update_bits(codec, WCD9XXX_A_CDC_CLSH_B1_CTL, 0x10, |
| 176 | (on ? 0x10 : 0)); |
| 177 | } else { |
| 178 | if (compute_pa == CLSH_COMPUTE_HPH_L) { |
| 179 | shift = 3; |
| 180 | } else if (compute_pa == CLSH_COMPUTE_HPH_R) { |
| 181 | shift = 2; |
| 182 | } else { |
| 183 | dev_dbg(codec->dev, |
| 184 | "%s: classh computation request is incorrect\n", |
| 185 | __func__); |
| 186 | return; |
| 187 | } |
| 188 | |
| 189 | if (on) |
| 190 | wcd9xxx_resmgr_add_cond_update_bits(clsh_d->resmgr, |
| 191 | WCD9XXX_COND_HPH, |
| 192 | WCD9XXX_A_CDC_CLSH_B1_CTL, |
| 193 | shift, false); |
| 194 | else |
| 195 | wcd9xxx_resmgr_rm_cond_update_bits(clsh_d->resmgr, |
| 196 | WCD9XXX_COND_HPH, |
| 197 | WCD9XXX_A_CDC_CLSH_B1_CTL, |
| 198 | shift, false); |
Bhalchandra Gajare | 87fef4c | 2013-02-19 14:57:03 -0800 | [diff] [blame] | 199 | } |
Bhalchandra Gajare | 87fef4c | 2013-02-19 14:57:03 -0800 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | static void wcd9xxx_enable_buck_mode(struct snd_soc_codec *codec, |
| 203 | u8 buck_vref) |
| 204 | { |
| 205 | int i; |
| 206 | const struct wcd9xxx_reg_mask_val reg_set[] = { |
| 207 | {WCD9XXX_A_BUCK_MODE_5, 0x02, 0x03}, |
| 208 | {WCD9XXX_A_BUCK_MODE_4, 0xFF, buck_vref}, |
| 209 | {WCD9XXX_A_BUCK_MODE_1, 0x04, 0x04}, |
| 210 | {WCD9XXX_A_BUCK_MODE_1, 0x08, 0x00}, |
| 211 | {WCD9XXX_A_BUCK_MODE_3, 0x04, 0x00}, |
| 212 | {WCD9XXX_A_BUCK_MODE_3, 0x08, 0x00}, |
| 213 | {WCD9XXX_A_BUCK_MODE_1, 0x80, 0x80}, |
| 214 | }; |
| 215 | |
| 216 | for (i = 0; i < ARRAY_SIZE(reg_set); i++) |
| 217 | snd_soc_update_bits(codec, reg_set[i].reg, |
| 218 | reg_set[i].mask, reg_set[i].val); |
| 219 | |
| 220 | dev_dbg(codec->dev, "%s: Done\n", __func__); |
| 221 | usleep_range(BUCK_SETTLE_TIME_US, BUCK_SETTLE_TIME_US); |
| 222 | } |
| 223 | |
| 224 | static void wcd9xxx_clsh_enable_post_pa(struct snd_soc_codec *codec) |
| 225 | { |
| 226 | int i; |
| 227 | const struct wcd9xxx_reg_mask_val reg_set[] = { |
| 228 | {WCD9XXX_A_BUCK_MODE_5, 0x02, 0x00}, |
| 229 | {WCD9XXX_A_NCP_STATIC, 0x20, 0x00}, |
| 230 | {WCD9XXX_A_BUCK_MODE_3, 0x04, 0x04}, |
| 231 | {WCD9XXX_A_BUCK_MODE_3, 0x08, 0x08}, |
| 232 | }; |
| 233 | |
| 234 | for (i = 0; i < ARRAY_SIZE(reg_set); i++) |
| 235 | snd_soc_update_bits(codec, reg_set[i].reg, |
| 236 | reg_set[i].mask, reg_set[i].val); |
| 237 | |
| 238 | dev_dbg(codec->dev, "%s: completed clsh mode settings after PA enable\n", |
| 239 | __func__); |
| 240 | |
| 241 | } |
| 242 | |
| 243 | static void wcd9xxx_set_fclk_enable_ncp(struct snd_soc_codec *codec, |
| 244 | u8 fclk_level) |
| 245 | { |
| 246 | int i; |
| 247 | const struct wcd9xxx_reg_mask_val reg_set[] = { |
| 248 | {WCD9XXX_A_NCP_STATIC, 0x20, 0x20}, |
| 249 | {WCD9XXX_A_NCP_EN, 0x01, 0x01}, |
| 250 | }; |
| 251 | snd_soc_update_bits(codec, WCD9XXX_A_NCP_STATIC, |
| 252 | 0x010, 0x00); |
| 253 | snd_soc_update_bits(codec, WCD9XXX_A_NCP_STATIC, |
| 254 | 0x0F, fclk_level); |
| 255 | for (i = 0; i < ARRAY_SIZE(reg_set); i++) |
| 256 | snd_soc_update_bits(codec, reg_set[i].reg, |
| 257 | reg_set[i].mask, reg_set[i].val); |
| 258 | |
| 259 | usleep_range(NCP_SETTLE_TIME_US, NCP_SETTLE_TIME_US); |
| 260 | |
| 261 | dev_dbg(codec->dev, "%s: set ncp done\n", __func__); |
| 262 | } |
| 263 | |
| 264 | static void wcd9xxx_cfg_clsh_param_ear(struct snd_soc_codec *codec) |
| 265 | { |
| 266 | int i; |
| 267 | const struct wcd9xxx_reg_mask_val reg_set[] = { |
| 268 | {WCD9XXX_A_CDC_CLSH_B1_CTL, (0x1 << 7), 0}, |
| 269 | {WCD9XXX_A_CDC_CLSH_V_PA_HD_EAR, (0x3f << 0), 0x0D}, |
| 270 | {WCD9XXX_A_CDC_CLSH_V_PA_MIN_EAR, (0x3f << 0), 0x3A}, |
| 271 | |
| 272 | /* Under assumption that EAR load is 10.7ohm */ |
| 273 | {WCD9XXX_A_CDC_CLSH_IDLE_EAR_THSD, (0x3f << 0), 0x26}, |
| 274 | {WCD9XXX_A_CDC_CLSH_FCLKONLY_EAR_THSD, (0x3f << 0), 0x2C}, |
| 275 | {WCD9XXX_A_CDC_CLSH_I_PA_FACT_EAR_L, 0xff, 0xA9}, |
| 276 | {WCD9XXX_A_CDC_CLSH_I_PA_FACT_EAR_U, 0xff, 0x07}, |
| 277 | {WCD9XXX_A_CDC_CLSH_K_ADDR, (0x1 << 7), 0}, |
| 278 | {WCD9XXX_A_CDC_CLSH_K_ADDR, (0xf << 0), 0x08}, |
| 279 | {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x1b}, |
| 280 | {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x00}, |
| 281 | {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x2d}, |
| 282 | {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x00}, |
| 283 | {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x36}, |
| 284 | {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x00}, |
| 285 | {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x37}, |
| 286 | {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x00}, |
| 287 | }; |
| 288 | |
| 289 | for (i = 0; i < ARRAY_SIZE(reg_set); i++) |
| 290 | snd_soc_update_bits(codec, reg_set[i].reg, |
| 291 | reg_set[i].mask, reg_set[i].val); |
| 292 | |
| 293 | dev_dbg(codec->dev, "%s: Programmed Class H controller EAR specific params\n", |
| 294 | __func__); |
| 295 | } |
| 296 | |
| 297 | static void wcd9xxx_cfg_clsh_param_hph(struct snd_soc_codec *codec) |
| 298 | { |
| 299 | int i; |
| 300 | const struct wcd9xxx_reg_mask_val reg_set[] = { |
| 301 | {WCD9XXX_A_CDC_CLSH_B1_CTL, (0x1 << 6), 0}, |
| 302 | {WCD9XXX_A_CDC_CLSH_V_PA_HD_HPH, 0x3f, 0x0D}, |
| 303 | {WCD9XXX_A_CDC_CLSH_V_PA_MIN_HPH, 0x3f, 0x1D}, |
| 304 | |
| 305 | /* Under assumption that HPH load is 16ohm per channel */ |
| 306 | {WCD9XXX_A_CDC_CLSH_IDLE_HPH_THSD, 0x3f, 0x13}, |
| 307 | {WCD9XXX_A_CDC_CLSH_FCLKONLY_HPH_THSD, 0x1f, 0x19}, |
| 308 | {WCD9XXX_A_CDC_CLSH_I_PA_FACT_HPH_L, 0xff, 0x97}, |
| 309 | {WCD9XXX_A_CDC_CLSH_I_PA_FACT_HPH_U, 0xff, 0x05}, |
| 310 | {WCD9XXX_A_CDC_CLSH_K_ADDR, (0x1 << 7), 0}, |
| 311 | {WCD9XXX_A_CDC_CLSH_K_ADDR, 0x0f, 0}, |
| 312 | {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0xAE}, |
| 313 | {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x01}, |
| 314 | {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x1C}, |
| 315 | {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x00}, |
| 316 | {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x24}, |
| 317 | {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x00}, |
| 318 | {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x25}, |
| 319 | {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x00}, |
| 320 | }; |
| 321 | |
| 322 | for (i = 0; i < ARRAY_SIZE(reg_set); i++) |
| 323 | snd_soc_update_bits(codec, reg_set[i].reg, reg_set[i].mask, |
| 324 | reg_set[i].val); |
| 325 | dev_dbg(codec->dev, "%s: Programmed Class H controller HPH specific params\n", |
| 326 | __func__); |
| 327 | } |
| 328 | |
| 329 | static void wcd9xxx_clsh_turnoff_postpa |
| 330 | (struct snd_soc_codec *codec) |
| 331 | { |
| 332 | |
| 333 | int i; |
| 334 | |
| 335 | const struct wcd9xxx_reg_mask_val reg_set[] = { |
| 336 | {WCD9XXX_A_NCP_EN, 0x01, 0x00}, |
| 337 | {WCD9XXX_A_BUCK_MODE_1, 0x80, 0x00}, |
| 338 | {WCD9XXX_A_CDC_CLSH_B1_CTL, 0x10, 0x00}, |
| 339 | }; |
| 340 | |
| 341 | wcd9xxx_chargepump_request(codec, false); |
| 342 | |
| 343 | for (i = 0; i < ARRAY_SIZE(reg_set); i++) |
| 344 | snd_soc_update_bits(codec, reg_set[i].reg, |
| 345 | reg_set[i].mask, reg_set[i].val); |
| 346 | |
| 347 | wcd9xxx_enable_clsh_block(codec, false); |
| 348 | |
| 349 | dev_dbg(codec->dev, "%s: Done\n", __func__); |
| 350 | } |
| 351 | |
| 352 | static void wcd9xxx_clsh_state_idle(struct snd_soc_codec *codec, |
| 353 | struct wcd9xxx_clsh_cdc_data *clsh_d, |
| 354 | u8 req_state, bool is_enable) |
| 355 | { |
| 356 | if (is_enable) { |
| 357 | dev_dbg(codec->dev, "%s: wrong transition, cannot enable IDLE state\n", |
| 358 | __func__); |
| 359 | } else { |
| 360 | if (req_state == WCD9XXX_CLSH_STATE_EAR) { |
| 361 | wcd9xxx_clsh_turnoff_postpa(codec); |
| 362 | } else if (req_state == WCD9XXX_CLSH_STATE_HPHL) { |
Joonwoo Park | a08e055 | 2013-03-05 18:28:23 -0800 | [diff] [blame] | 363 | wcd9xxx_clsh_comp_req(codec, clsh_d, CLSH_COMPUTE_HPH_L, |
| 364 | false); |
Bhalchandra Gajare | 87fef4c | 2013-02-19 14:57:03 -0800 | [diff] [blame] | 365 | wcd9xxx_clsh_turnoff_postpa(codec); |
| 366 | } else if (req_state == WCD9XXX_CLSH_STATE_HPHR) { |
Joonwoo Park | a08e055 | 2013-03-05 18:28:23 -0800 | [diff] [blame] | 367 | wcd9xxx_clsh_comp_req(codec, clsh_d, CLSH_COMPUTE_HPH_R, |
| 368 | false); |
Bhalchandra Gajare | 87fef4c | 2013-02-19 14:57:03 -0800 | [diff] [blame] | 369 | wcd9xxx_clsh_turnoff_postpa(codec); |
| 370 | } else if (req_state == WCD9XXX_CLSH_STATE_LO) { |
| 371 | wcd9xxx_enable_ncp(codec, false); |
| 372 | wcd9xxx_enable_buck(codec, false); |
| 373 | } |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | static void wcd9xxx_clsh_state_ear(struct snd_soc_codec *codec, |
| 378 | struct wcd9xxx_clsh_cdc_data *clsh_d, |
| 379 | u8 req_state, bool is_enable) |
| 380 | { |
| 381 | if (is_enable) { |
| 382 | wcd9xxx_cfg_clsh_buck(codec); |
| 383 | wcd9xxx_cfg_clsh_param_common(codec); |
| 384 | wcd9xxx_cfg_clsh_param_ear(codec); |
| 385 | wcd9xxx_enable_clsh_block(codec, true); |
| 386 | wcd9xxx_chargepump_request(codec, true); |
| 387 | wcd9xxx_enable_anc_delay(codec, true); |
Joonwoo Park | a08e055 | 2013-03-05 18:28:23 -0800 | [diff] [blame] | 388 | wcd9xxx_clsh_comp_req(codec, clsh_d, CLSH_COMPUTE_EAR, true); |
Bhalchandra Gajare | 87fef4c | 2013-02-19 14:57:03 -0800 | [diff] [blame] | 389 | wcd9xxx_enable_buck_mode(codec, BUCK_VREF_2V); |
| 390 | wcd9xxx_set_fclk_enable_ncp(codec, NCP_FCLK_LEVEL_8); |
| 391 | |
| 392 | dev_dbg(codec->dev, "%s: Enabled ear mode class h\n", __func__); |
| 393 | } else { |
| 394 | dev_dbg(codec->dev, "%s: stub fallback to ear\n", __func__); |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | static void wcd9xxx_clsh_state_hph_l(struct snd_soc_codec *codec, |
| 399 | struct wcd9xxx_clsh_cdc_data *clsh_d, |
| 400 | u8 req_state, bool is_enable) |
| 401 | { |
| 402 | if (is_enable) { |
| 403 | wcd9xxx_cfg_clsh_buck(codec); |
| 404 | wcd9xxx_cfg_clsh_param_common(codec); |
| 405 | wcd9xxx_cfg_clsh_param_hph(codec); |
| 406 | wcd9xxx_enable_clsh_block(codec, true); |
| 407 | wcd9xxx_chargepump_request(codec, true); |
| 408 | wcd9xxx_enable_anc_delay(codec, true); |
Joonwoo Park | a08e055 | 2013-03-05 18:28:23 -0800 | [diff] [blame] | 409 | wcd9xxx_clsh_comp_req(codec, clsh_d, CLSH_COMPUTE_HPH_L, true); |
Bhalchandra Gajare | 87fef4c | 2013-02-19 14:57:03 -0800 | [diff] [blame] | 410 | wcd9xxx_enable_buck_mode(codec, BUCK_VREF_2V); |
| 411 | wcd9xxx_set_fclk_enable_ncp(codec, NCP_FCLK_LEVEL_8); |
| 412 | |
| 413 | dev_dbg(codec->dev, "%s: Done\n", __func__); |
| 414 | } else { |
| 415 | if (req_state == WCD9XXX_CLSH_STATE_HPHR) { |
Joonwoo Park | a08e055 | 2013-03-05 18:28:23 -0800 | [diff] [blame] | 416 | wcd9xxx_clsh_comp_req(codec, clsh_d, CLSH_COMPUTE_HPH_R, |
| 417 | false); |
Bhalchandra Gajare | 87fef4c | 2013-02-19 14:57:03 -0800 | [diff] [blame] | 418 | } else { |
| 419 | dev_dbg(codec->dev, "%s: stub fallback to hph_l\n", |
| 420 | __func__); |
| 421 | } |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | static void wcd9xxx_clsh_state_hph_r(struct snd_soc_codec *codec, |
| 426 | struct wcd9xxx_clsh_cdc_data *clsh_d, |
| 427 | u8 req_state, bool is_enable) |
| 428 | { |
| 429 | if (is_enable) { |
| 430 | |
| 431 | wcd9xxx_cfg_clsh_buck(codec); |
| 432 | wcd9xxx_cfg_clsh_param_common(codec); |
| 433 | wcd9xxx_cfg_clsh_param_hph(codec); |
| 434 | wcd9xxx_enable_clsh_block(codec, true); |
| 435 | wcd9xxx_chargepump_request(codec, true); |
| 436 | wcd9xxx_enable_anc_delay(codec, true); |
Joonwoo Park | a08e055 | 2013-03-05 18:28:23 -0800 | [diff] [blame] | 437 | wcd9xxx_clsh_comp_req(codec, clsh_d, CLSH_COMPUTE_HPH_R, true); |
Bhalchandra Gajare | 87fef4c | 2013-02-19 14:57:03 -0800 | [diff] [blame] | 438 | wcd9xxx_enable_buck_mode(codec, BUCK_VREF_2V); |
| 439 | wcd9xxx_set_fclk_enable_ncp(codec, NCP_FCLK_LEVEL_8); |
| 440 | |
| 441 | dev_dbg(codec->dev, "%s: Done\n", __func__); |
| 442 | } else { |
| 443 | if (req_state == WCD9XXX_CLSH_STATE_HPHL) { |
Joonwoo Park | a08e055 | 2013-03-05 18:28:23 -0800 | [diff] [blame] | 444 | wcd9xxx_clsh_comp_req(codec, clsh_d, CLSH_COMPUTE_HPH_L, |
| 445 | false); |
Bhalchandra Gajare | 87fef4c | 2013-02-19 14:57:03 -0800 | [diff] [blame] | 446 | } else { |
| 447 | dev_dbg(codec->dev, "%s: stub fallback to hph_r\n", |
| 448 | __func__); |
| 449 | } |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | static void wcd9xxx_clsh_state_hph_st(struct snd_soc_codec *codec, |
| 454 | struct wcd9xxx_clsh_cdc_data *clsh_d, |
| 455 | u8 req_state, bool is_enable) |
| 456 | { |
| 457 | if (is_enable) { |
Joonwoo Park | a08e055 | 2013-03-05 18:28:23 -0800 | [diff] [blame] | 458 | wcd9xxx_clsh_comp_req(codec, clsh_d, CLSH_COMPUTE_HPH_L, true); |
| 459 | wcd9xxx_clsh_comp_req(codec, clsh_d, CLSH_COMPUTE_HPH_R, true); |
Bhalchandra Gajare | 87fef4c | 2013-02-19 14:57:03 -0800 | [diff] [blame] | 460 | } else { |
| 461 | dev_dbg(codec->dev, "%s: stub fallback to hph_st\n", __func__); |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | static void wcd9xxx_clsh_state_lo(struct snd_soc_codec *codec, |
| 466 | struct wcd9xxx_clsh_cdc_data *clsh_d, |
| 467 | u8 req_state, bool is_enable) |
| 468 | { |
| 469 | if (is_enable) { |
| 470 | if (++cdc_lo_count > 1) |
| 471 | return; |
| 472 | |
| 473 | wcd9xxx_enable_buck_mode(codec, BUCK_VREF_1P8V); |
| 474 | wcd9xxx_set_fclk_enable_ncp(codec, NCP_FCLK_LEVEL_5); |
| 475 | |
| 476 | if (clsh_d->buck_mv == WCD9XXX_CDC_BUCK_MV_1P8) { |
| 477 | wcd9xxx_enable_buck(codec, false); |
| 478 | snd_soc_update_bits(codec, WCD9XXX_A_NCP_STATIC, |
| 479 | 0x20, 0x01); |
| 480 | wcd9xxx_enable_ncp(codec, true); |
| 481 | msleep(NCP_SETTLE_TIME_US); |
| 482 | |
| 483 | } else { |
| 484 | snd_soc_update_bits(codec, WCD9XXX_A_NCP_EN, |
| 485 | 0x40, 0x00); |
| 486 | wcd9xxx_enable_ncp(codec, true); |
| 487 | msleep(NCP_SETTLE_TIME_US); |
| 488 | snd_soc_update_bits(codec, WCD9XXX_A_BUCK_MODE_5, |
| 489 | 0x01, 0x01); |
| 490 | snd_soc_update_bits(codec, WCD9XXX_A_BUCK_MODE_5, |
| 491 | 0xFB, (0x02 << 2)); |
| 492 | } |
| 493 | snd_soc_update_bits(codec, WCD9XXX_A_BUCK_MODE_1, |
| 494 | 0x04, 0x00); |
| 495 | } else { |
| 496 | dev_dbg(codec->dev, "%s: stub fallback to lineout\n", __func__); |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | static void wcd9xxx_clsh_state_err(struct snd_soc_codec *codec, |
| 501 | struct wcd9xxx_clsh_cdc_data *clsh_d, |
| 502 | u8 req_state, bool is_enable) |
| 503 | { |
| 504 | dev_dbg(codec->dev, "%s Wrong request for class H state machine requested to %s %s" |
| 505 | , __func__, is_enable ? "enable" : "disable", |
| 506 | state_to_str(req_state)); |
| 507 | WARN_ON(1); |
| 508 | } |
| 509 | |
| 510 | void wcd9xxx_clsh_fsm(struct snd_soc_codec *codec, |
| 511 | struct wcd9xxx_clsh_cdc_data *cdc_clsh_d, |
| 512 | u8 req_state, bool req_type, u8 clsh_event) |
| 513 | { |
| 514 | u8 old_state, new_state; |
| 515 | |
| 516 | switch (clsh_event) { |
| 517 | |
| 518 | case WCD9XXX_CLSH_EVENT_PRE_DAC: |
| 519 | |
| 520 | /* PRE_DAC event should be used only for Enable */ |
| 521 | BUG_ON(req_type != WCD9XXX_CLSH_REQ_ENABLE); |
| 522 | |
| 523 | old_state = cdc_clsh_d->state; |
| 524 | new_state = old_state | req_state; |
| 525 | |
| 526 | (*clsh_state_fp[new_state]) (codec, cdc_clsh_d, |
| 527 | req_state, req_type); |
| 528 | cdc_clsh_d->state = new_state; |
Bhalchandra Gajare | db8ffcb | 2013-02-27 13:58:17 -0800 | [diff] [blame] | 529 | dev_dbg(codec->dev, "%s: ClassH state transition from %s to %s\n", |
Bhalchandra Gajare | 87fef4c | 2013-02-19 14:57:03 -0800 | [diff] [blame] | 530 | __func__, state_to_str(old_state), |
| 531 | state_to_str(cdc_clsh_d->state)); |
| 532 | |
| 533 | break; |
| 534 | |
| 535 | case WCD9XXX_CLSH_EVENT_POST_PA: |
| 536 | |
| 537 | if (req_type == WCD9XXX_CLSH_REQ_DISABLE) { |
| 538 | if (req_state == WCD9XXX_CLSH_STATE_LO |
| 539 | && --cdc_lo_count > 0) |
| 540 | break; |
| 541 | |
| 542 | old_state = cdc_clsh_d->state; |
| 543 | new_state = old_state & (~req_state); |
| 544 | |
| 545 | if (new_state < NUM_CLSH_STATES) { |
| 546 | (*clsh_state_fp[new_state]) (codec, cdc_clsh_d, |
| 547 | req_state, req_type); |
| 548 | cdc_clsh_d->state = new_state; |
Bhalchandra Gajare | db8ffcb | 2013-02-27 13:58:17 -0800 | [diff] [blame] | 549 | dev_dbg(codec->dev, "%s: ClassH state transition from %s to %s\n", |
Bhalchandra Gajare | 87fef4c | 2013-02-19 14:57:03 -0800 | [diff] [blame] | 550 | __func__, state_to_str(old_state), |
| 551 | state_to_str(cdc_clsh_d->state)); |
| 552 | |
| 553 | } else { |
| 554 | dev_dbg(codec->dev, "%s: wrong new state = %x\n", |
| 555 | __func__, new_state); |
| 556 | } |
| 557 | |
| 558 | |
| 559 | } else if (req_state != WCD9XXX_CLSH_STATE_LO) { |
| 560 | wcd9xxx_clsh_enable_post_pa(codec); |
| 561 | } |
| 562 | |
| 563 | break; |
| 564 | } |
| 565 | |
| 566 | } |
| 567 | EXPORT_SYMBOL_GPL(wcd9xxx_clsh_fsm); |
| 568 | |
Joonwoo Park | a08e055 | 2013-03-05 18:28:23 -0800 | [diff] [blame] | 569 | void wcd9xxx_clsh_init(struct wcd9xxx_clsh_cdc_data *clsh, |
| 570 | struct wcd9xxx_resmgr *resmgr) |
Bhalchandra Gajare | 87fef4c | 2013-02-19 14:57:03 -0800 | [diff] [blame] | 571 | { |
| 572 | int i; |
| 573 | clsh->state = WCD9XXX_CLSH_STATE_IDLE; |
Joonwoo Park | a08e055 | 2013-03-05 18:28:23 -0800 | [diff] [blame] | 574 | clsh->resmgr = resmgr; |
Bhalchandra Gajare | 87fef4c | 2013-02-19 14:57:03 -0800 | [diff] [blame] | 575 | |
| 576 | for (i = 0; i < NUM_CLSH_STATES; i++) |
| 577 | clsh_state_fp[i] = wcd9xxx_clsh_state_err; |
| 578 | |
| 579 | clsh_state_fp[WCD9XXX_CLSH_STATE_IDLE] = wcd9xxx_clsh_state_idle; |
| 580 | clsh_state_fp[WCD9XXX_CLSH_STATE_EAR] = wcd9xxx_clsh_state_ear; |
| 581 | clsh_state_fp[WCD9XXX_CLSH_STATE_HPHL] = |
| 582 | wcd9xxx_clsh_state_hph_l; |
| 583 | clsh_state_fp[WCD9XXX_CLSH_STATE_HPHR] = |
| 584 | wcd9xxx_clsh_state_hph_r; |
| 585 | clsh_state_fp[WCD9XXX_CLSH_STATE_HPH_ST] = |
| 586 | wcd9xxx_clsh_state_hph_st; |
| 587 | clsh_state_fp[WCD9XXX_CLSH_STATE_LO] = wcd9xxx_clsh_state_lo; |
| 588 | |
| 589 | } |
| 590 | EXPORT_SYMBOL_GPL(wcd9xxx_clsh_init); |
| 591 | |
| 592 | MODULE_DESCRIPTION("WCD9XXX Common"); |
| 593 | MODULE_LICENSE("GPL v2"); |