Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2012 Samsung Electronics |
| 4 | * R. Chandrasekar <rcsekar@samsung.com> |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 5 | */ |
Simon Glass | 4070ba6 | 2018-12-10 10:37:39 -0700 | [diff] [blame] | 6 | #include <audio_codec.h> |
| 7 | #include <dm.h> |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 8 | #include <div64.h> |
Rajeshwari Shinde | d5d9a6e | 2012-12-26 20:03:18 +0000 | [diff] [blame] | 9 | #include <fdtdec.h> |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 10 | #include <i2c.h> |
| 11 | #include <i2s.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 13 | #include <sound.h> |
Simon Glass | f222236 | 2018-12-03 04:37:34 -0700 | [diff] [blame] | 14 | #include <asm/gpio.h> |
| 15 | #include <asm/io.h> |
| 16 | #include <asm/arch/clk.h> |
| 17 | #include <asm/arch/cpu.h> |
Rajeshwari Shinde | d5d9a6e | 2012-12-26 20:03:18 +0000 | [diff] [blame] | 18 | #include <asm/arch/sound.h> |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 19 | #include "wm8994.h" |
| 20 | #include "wm8994_registers.h" |
| 21 | |
| 22 | /* defines for wm8994 system clock selection */ |
| 23 | #define SEL_MCLK1 0x00 |
| 24 | #define SEL_MCLK2 0x08 |
| 25 | #define SEL_FLL1 0x10 |
| 26 | #define SEL_FLL2 0x18 |
| 27 | |
| 28 | /* fll config to configure fll */ |
| 29 | struct wm8994_fll_config { |
| 30 | int src; /* Source */ |
| 31 | int in; /* Input frequency in Hz */ |
| 32 | int out; /* output frequency in Hz */ |
| 33 | }; |
| 34 | |
| 35 | /* codec private data */ |
| 36 | struct wm8994_priv { |
| 37 | enum wm8994_type type; /* codec type of wolfson */ |
| 38 | int revision; /* Revision */ |
| 39 | int sysclk[WM8994_MAX_AIF]; /* System clock frequency in Hz */ |
| 40 | int mclk[WM8994_MAX_AIF]; /* master clock frequency in Hz */ |
| 41 | int aifclk[WM8994_MAX_AIF]; /* audio interface clock in Hz */ |
| 42 | struct wm8994_fll_config fll[2]; /* fll config to configure fll */ |
Simon Glass | 4070ba6 | 2018-12-10 10:37:39 -0700 | [diff] [blame] | 43 | struct udevice *dev; |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | /* wm 8994 supported sampling rate values */ |
| 47 | static unsigned int src_rate[] = { |
| 48 | 8000, 11025, 12000, 16000, 22050, 24000, |
| 49 | 32000, 44100, 48000, 88200, 96000 |
| 50 | }; |
| 51 | |
| 52 | /* op clock divisions */ |
| 53 | static int opclk_divs[] = { 10, 20, 30, 40, 55, 60, 80, 120, 160 }; |
| 54 | |
| 55 | /* lr clock frame size ratio */ |
| 56 | static int fs_ratios[] = { |
| 57 | 64, 128, 192, 256, 348, 512, 768, 1024, 1408, 1536 |
| 58 | }; |
| 59 | |
| 60 | /* bit clock divisors */ |
| 61 | static int bclk_divs[] = { |
| 62 | 10, 15, 20, 30, 40, 50, 60, 80, 110, 120, 160, 220, 240, 320, 440, 480, |
| 63 | 640, 880, 960, 1280, 1760, 1920 |
| 64 | }; |
| 65 | |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 66 | /* |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 67 | * Writes value to a device register through i2c |
| 68 | * |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 69 | * @param priv Private data for driver |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 70 | * @param reg reg number to be write |
| 71 | * @param data data to be writen to the above registor |
| 72 | * |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 73 | * Return: int value 1 for change, 0 for no change or negative error code. |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 74 | */ |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 75 | static int wm8994_i2c_write(struct wm8994_priv *priv, unsigned int reg, |
| 76 | unsigned short data) |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 77 | { |
| 78 | unsigned char val[2]; |
| 79 | |
| 80 | val[0] = (unsigned char)((data >> 8) & 0xff); |
| 81 | val[1] = (unsigned char)(data & 0xff); |
| 82 | debug("Write Addr : 0x%04X, Data : 0x%04X\n", reg, data); |
| 83 | |
Simon Glass | 4070ba6 | 2018-12-10 10:37:39 -0700 | [diff] [blame] | 84 | return dm_i2c_write(priv->dev, reg, val, 2); |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | /* |
| 88 | * Read a value from a device register through i2c |
| 89 | * |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 90 | * @param priv Private data for driver |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 91 | * @param reg reg number to be read |
| 92 | * @param data address of read data to be stored |
| 93 | * |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 94 | * Return: int value 0 for success, -1 in case of error. |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 95 | */ |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 96 | static unsigned int wm8994_i2c_read(struct wm8994_priv *priv, unsigned int reg, |
| 97 | unsigned short *data) |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 98 | { |
| 99 | unsigned char val[2]; |
| 100 | int ret; |
| 101 | |
Simon Glass | 4070ba6 | 2018-12-10 10:37:39 -0700 | [diff] [blame] | 102 | ret = dm_i2c_read(priv->dev, reg, val, 1); |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 103 | if (ret != 0) { |
| 104 | debug("%s: Error while reading register %#04x\n", |
| 105 | __func__, reg); |
| 106 | return -1; |
| 107 | } |
| 108 | |
| 109 | *data = val[0]; |
| 110 | *data <<= 8; |
| 111 | *data |= val[1]; |
| 112 | |
| 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | /* |
| 117 | * update device register bits through i2c |
| 118 | * |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 119 | * @param priv Private data for driver |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 120 | * @param reg codec register |
| 121 | * @param mask register mask |
| 122 | * @param value new value |
| 123 | * |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 124 | * Return: int value 1 if change in the register value, |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 125 | * 0 for no change or negative error code. |
| 126 | */ |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 127 | static int wm8994_bic_or(struct wm8994_priv *priv, unsigned int reg, |
| 128 | unsigned short mask, unsigned short value) |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 129 | { |
| 130 | int change , ret = 0; |
| 131 | unsigned short old, new; |
| 132 | |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 133 | if (wm8994_i2c_read(priv, reg, &old) != 0) |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 134 | return -1; |
| 135 | new = (old & ~mask) | (value & mask); |
| 136 | change = (old != new) ? 1 : 0; |
| 137 | if (change) |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 138 | ret = wm8994_i2c_write(priv, reg, new); |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 139 | if (ret < 0) |
| 140 | return ret; |
| 141 | |
| 142 | return change; |
| 143 | } |
| 144 | |
| 145 | /* |
| 146 | * Sets i2s set format |
| 147 | * |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 148 | * @param priv wm8994 information |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 149 | * @param aif_id Interface ID |
| 150 | * @param fmt i2S format |
| 151 | * |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 152 | * Return: -1 for error and 0 Success. |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 153 | */ |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 154 | static int wm8994_set_fmt(struct wm8994_priv *priv, int aif_id, uint fmt) |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 155 | { |
| 156 | int ms_reg; |
| 157 | int aif_reg; |
| 158 | int ms = 0; |
| 159 | int aif = 0; |
| 160 | int aif_clk = 0; |
| 161 | int error = 0; |
| 162 | |
| 163 | switch (aif_id) { |
| 164 | case 1: |
| 165 | ms_reg = WM8994_AIF1_MASTER_SLAVE; |
| 166 | aif_reg = WM8994_AIF1_CONTROL_1; |
| 167 | aif_clk = WM8994_AIF1_CLOCKING_1; |
| 168 | break; |
| 169 | case 2: |
| 170 | ms_reg = WM8994_AIF2_MASTER_SLAVE; |
| 171 | aif_reg = WM8994_AIF2_CONTROL_1; |
| 172 | aif_clk = WM8994_AIF2_CLOCKING_1; |
| 173 | break; |
| 174 | default: |
| 175 | debug("%s: Invalid audio interface selection\n", __func__); |
| 176 | return -1; |
| 177 | } |
| 178 | |
| 179 | switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { |
| 180 | case SND_SOC_DAIFMT_CBS_CFS: |
| 181 | break; |
| 182 | case SND_SOC_DAIFMT_CBM_CFM: |
| 183 | ms = WM8994_AIF1_MSTR; |
| 184 | break; |
| 185 | default: |
| 186 | debug("%s: Invalid i2s master selection\n", __func__); |
| 187 | return -1; |
| 188 | } |
| 189 | |
| 190 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { |
| 191 | case SND_SOC_DAIFMT_DSP_B: |
| 192 | aif |= WM8994_AIF1_LRCLK_INV; |
| 193 | case SND_SOC_DAIFMT_DSP_A: |
| 194 | aif |= 0x18; |
| 195 | break; |
| 196 | case SND_SOC_DAIFMT_I2S: |
| 197 | aif |= 0x10; |
| 198 | break; |
| 199 | case SND_SOC_DAIFMT_RIGHT_J: |
| 200 | break; |
| 201 | case SND_SOC_DAIFMT_LEFT_J: |
| 202 | aif |= 0x8; |
| 203 | break; |
| 204 | default: |
| 205 | debug("%s: Invalid i2s format selection\n", __func__); |
| 206 | return -1; |
| 207 | } |
| 208 | |
| 209 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { |
| 210 | case SND_SOC_DAIFMT_DSP_A: |
| 211 | case SND_SOC_DAIFMT_DSP_B: |
| 212 | /* frame inversion not valid for DSP modes */ |
| 213 | switch (fmt & SND_SOC_DAIFMT_INV_MASK) { |
| 214 | case SND_SOC_DAIFMT_NB_NF: |
| 215 | break; |
| 216 | case SND_SOC_DAIFMT_IB_NF: |
| 217 | aif |= WM8994_AIF1_BCLK_INV; |
| 218 | break; |
| 219 | default: |
| 220 | debug("%s: Invalid i2s frame inverse selection\n", |
| 221 | __func__); |
| 222 | return -1; |
| 223 | } |
| 224 | break; |
| 225 | |
| 226 | case SND_SOC_DAIFMT_I2S: |
| 227 | case SND_SOC_DAIFMT_RIGHT_J: |
| 228 | case SND_SOC_DAIFMT_LEFT_J: |
| 229 | switch (fmt & SND_SOC_DAIFMT_INV_MASK) { |
| 230 | case SND_SOC_DAIFMT_NB_NF: |
| 231 | break; |
| 232 | case SND_SOC_DAIFMT_IB_IF: |
| 233 | aif |= WM8994_AIF1_BCLK_INV | WM8994_AIF1_LRCLK_INV; |
| 234 | break; |
| 235 | case SND_SOC_DAIFMT_IB_NF: |
| 236 | aif |= WM8994_AIF1_BCLK_INV; |
| 237 | break; |
| 238 | case SND_SOC_DAIFMT_NB_IF: |
| 239 | aif |= WM8994_AIF1_LRCLK_INV; |
| 240 | break; |
| 241 | default: |
| 242 | debug("%s: Invalid i2s clock polarity selection\n", |
| 243 | __func__); |
| 244 | return -1; |
| 245 | } |
| 246 | break; |
| 247 | default: |
| 248 | debug("%s: Invalid i2s format selection\n", __func__); |
| 249 | return -1; |
| 250 | } |
| 251 | |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 252 | error = wm8994_bic_or(priv, aif_reg, WM8994_AIF1_BCLK_INV | |
| 253 | WM8994_AIF1_LRCLK_INV_MASK | |
| 254 | WM8994_AIF1_FMT_MASK, aif); |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 255 | |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 256 | error |= wm8994_bic_or(priv, ms_reg, WM8994_AIF1_MSTR_MASK, ms); |
| 257 | error |= wm8994_bic_or(priv, aif_clk, WM8994_AIF1CLK_ENA_MASK, |
| 258 | WM8994_AIF1CLK_ENA); |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 259 | if (error < 0) { |
| 260 | debug("%s: codec register access error\n", __func__); |
| 261 | return -1; |
| 262 | } |
| 263 | |
| 264 | return 0; |
| 265 | } |
| 266 | |
| 267 | /* |
| 268 | * Sets hw params FOR WM8994 |
| 269 | * |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 270 | * @param priv wm8994 information pointer |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 271 | * @param aif_id Audio interface ID |
| 272 | * @param sampling_rate Sampling rate |
| 273 | * @param bits_per_sample Bits per sample |
| 274 | * @param Channels Channels in the given audio input |
| 275 | * |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 276 | * Return: -1 for error and 0 Success. |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 277 | */ |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 278 | static int wm8994_hw_params(struct wm8994_priv *priv, int aif_id, |
| 279 | uint sampling_rate, uint bits_per_sample, |
| 280 | uint channels) |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 281 | { |
| 282 | int aif1_reg; |
| 283 | int aif2_reg; |
| 284 | int bclk_reg; |
| 285 | int bclk = 0; |
| 286 | int rate_reg; |
| 287 | int aif1 = 0; |
| 288 | int aif2 = 0; |
| 289 | int rate_val = 0; |
| 290 | int id = aif_id - 1; |
| 291 | int i, cur_val, best_val, bclk_rate, best; |
| 292 | unsigned short reg_data; |
| 293 | int ret = 0; |
| 294 | |
| 295 | switch (aif_id) { |
| 296 | case 1: |
| 297 | aif1_reg = WM8994_AIF1_CONTROL_1; |
| 298 | aif2_reg = WM8994_AIF1_CONTROL_2; |
| 299 | bclk_reg = WM8994_AIF1_BCLK; |
| 300 | rate_reg = WM8994_AIF1_RATE; |
| 301 | break; |
| 302 | case 2: |
| 303 | aif1_reg = WM8994_AIF2_CONTROL_1; |
| 304 | aif2_reg = WM8994_AIF2_CONTROL_2; |
| 305 | bclk_reg = WM8994_AIF2_BCLK; |
| 306 | rate_reg = WM8994_AIF2_RATE; |
| 307 | break; |
| 308 | default: |
| 309 | return -1; |
| 310 | } |
| 311 | |
| 312 | bclk_rate = sampling_rate * 32; |
| 313 | switch (bits_per_sample) { |
| 314 | case 16: |
| 315 | bclk_rate *= 16; |
| 316 | break; |
| 317 | case 20: |
| 318 | bclk_rate *= 20; |
| 319 | aif1 |= 0x20; |
| 320 | break; |
| 321 | case 24: |
| 322 | bclk_rate *= 24; |
| 323 | aif1 |= 0x40; |
| 324 | break; |
| 325 | case 32: |
| 326 | bclk_rate *= 32; |
| 327 | aif1 |= 0x60; |
| 328 | break; |
| 329 | default: |
| 330 | return -1; |
| 331 | } |
| 332 | |
| 333 | /* Try to find an appropriate sample rate; look for an exact match. */ |
| 334 | for (i = 0; i < ARRAY_SIZE(src_rate); i++) |
| 335 | if (src_rate[i] == sampling_rate) |
| 336 | break; |
| 337 | |
| 338 | if (i == ARRAY_SIZE(src_rate)) { |
| 339 | debug("%s: Could not get the best matching samplingrate\n", |
| 340 | __func__); |
| 341 | return -1; |
| 342 | } |
| 343 | |
| 344 | rate_val |= i << WM8994_AIF1_SR_SHIFT; |
| 345 | |
| 346 | /* AIFCLK/fs ratio; look for a close match in either direction */ |
| 347 | best = 0; |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 348 | best_val = abs((fs_ratios[0] * sampling_rate) - priv->aifclk[id]); |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 349 | |
| 350 | for (i = 1; i < ARRAY_SIZE(fs_ratios); i++) { |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 351 | cur_val = abs(fs_ratios[i] * sampling_rate - priv->aifclk[id]); |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 352 | if (cur_val >= best_val) |
| 353 | continue; |
| 354 | best = i; |
| 355 | best_val = cur_val; |
| 356 | } |
| 357 | |
| 358 | rate_val |= best; |
| 359 | |
| 360 | /* |
| 361 | * We may not get quite the right frequency if using |
| 362 | * approximate clocks so look for the closest match that is |
| 363 | * higher than the target (we need to ensure that there enough |
| 364 | * BCLKs to clock out the samples). |
| 365 | */ |
| 366 | best = 0; |
| 367 | for (i = 0; i < ARRAY_SIZE(bclk_divs); i++) { |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 368 | cur_val = (priv->aifclk[id] * 10 / bclk_divs[i]) - bclk_rate; |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 369 | if (cur_val < 0) /* BCLK table is sorted */ |
| 370 | break; |
| 371 | best = i; |
| 372 | } |
| 373 | |
| 374 | if (i == ARRAY_SIZE(bclk_divs)) { |
| 375 | debug("%s: Could not get the best matching bclk division\n", |
| 376 | __func__); |
| 377 | return -1; |
| 378 | } |
| 379 | |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 380 | bclk_rate = priv->aifclk[id] * 10 / bclk_divs[best]; |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 381 | bclk |= best << WM8994_AIF1_BCLK_DIV_SHIFT; |
| 382 | |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 383 | if (wm8994_i2c_read(priv, aif1_reg, ®_data) != 0) { |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 384 | debug("%s: AIF1 register read Failed\n", __func__); |
| 385 | return -1; |
| 386 | } |
| 387 | |
| 388 | if ((channels == 1) && ((reg_data & 0x18) == 0x18)) |
| 389 | aif2 |= WM8994_AIF1_MONO; |
| 390 | |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 391 | if (priv->aifclk[id] == 0) { |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 392 | debug("%s:Audio interface clock not set\n", __func__); |
| 393 | return -1; |
| 394 | } |
| 395 | |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 396 | ret = wm8994_bic_or(priv, aif1_reg, WM8994_AIF1_WL_MASK, aif1); |
| 397 | ret |= wm8994_bic_or(priv, aif2_reg, WM8994_AIF1_MONO, aif2); |
| 398 | ret |= wm8994_bic_or(priv, bclk_reg, WM8994_AIF1_BCLK_DIV_MASK, |
| 399 | bclk); |
| 400 | ret |= wm8994_bic_or(priv, rate_reg, WM8994_AIF1_SR_MASK | |
| 401 | WM8994_AIF1CLK_RATE_MASK, rate_val); |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 402 | |
| 403 | debug("rate vale = %x , bclk val= %x\n", rate_val, bclk); |
| 404 | |
| 405 | if (ret < 0) { |
| 406 | debug("%s: codec register access error\n", __func__); |
| 407 | return -1; |
| 408 | } |
| 409 | |
| 410 | return 0; |
| 411 | } |
| 412 | |
| 413 | /* |
| 414 | * Configures Audio interface Clock |
| 415 | * |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 416 | * @param priv wm8994 information pointer |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 417 | * @param aif Audio Interface ID |
| 418 | * |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 419 | * Return: -1 for error and 0 Success. |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 420 | */ |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 421 | static int configure_aif_clock(struct wm8994_priv *priv, int aif) |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 422 | { |
| 423 | int rate; |
| 424 | int reg1 = 0; |
| 425 | int offset; |
| 426 | int ret; |
| 427 | |
| 428 | /* AIF(1/0) register adress offset calculated */ |
Dani Krishna Mohan | 962e08c | 2013-09-11 16:38:46 +0530 | [diff] [blame] | 429 | if (aif-1) |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 430 | offset = 4; |
| 431 | else |
| 432 | offset = 0; |
| 433 | |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 434 | switch (priv->sysclk[aif - 1]) { |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 435 | case WM8994_SYSCLK_MCLK1: |
| 436 | reg1 |= SEL_MCLK1; |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 437 | rate = priv->mclk[0]; |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 438 | break; |
| 439 | |
| 440 | case WM8994_SYSCLK_MCLK2: |
| 441 | reg1 |= SEL_MCLK2; |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 442 | rate = priv->mclk[1]; |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 443 | break; |
| 444 | |
| 445 | case WM8994_SYSCLK_FLL1: |
| 446 | reg1 |= SEL_FLL1; |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 447 | rate = priv->fll[0].out; |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 448 | break; |
| 449 | |
| 450 | case WM8994_SYSCLK_FLL2: |
| 451 | reg1 |= SEL_FLL2; |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 452 | rate = priv->fll[1].out; |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 453 | break; |
| 454 | |
| 455 | default: |
| 456 | debug("%s: Invalid input clock selection [%d]\n", |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 457 | __func__, priv->sysclk[aif - 1]); |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 458 | return -1; |
| 459 | } |
| 460 | |
| 461 | /* if input clock frequenct is more than 135Mhz then divide */ |
| 462 | if (rate >= WM8994_MAX_INPUT_CLK_FREQ) { |
| 463 | rate /= 2; |
| 464 | reg1 |= WM8994_AIF1CLK_DIV; |
| 465 | } |
| 466 | |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 467 | priv->aifclk[aif - 1] = rate; |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 468 | |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 469 | ret = wm8994_bic_or(priv, WM8994_AIF1_CLOCKING_1 + offset, |
| 470 | WM8994_AIF1CLK_SRC_MASK | WM8994_AIF1CLK_DIV, |
| 471 | reg1); |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 472 | |
Dani Krishna Mohan | 962e08c | 2013-09-11 16:38:46 +0530 | [diff] [blame] | 473 | if (aif == WM8994_AIF1) |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 474 | ret |= wm8994_bic_or(priv, WM8994_CLOCKING_1, |
Dani Krishna Mohan | 962e08c | 2013-09-11 16:38:46 +0530 | [diff] [blame] | 475 | WM8994_AIF1DSPCLK_ENA_MASK | WM8994_SYSDSPCLK_ENA_MASK, |
| 476 | WM8994_AIF1DSPCLK_ENA | WM8994_SYSDSPCLK_ENA); |
| 477 | else if (aif == WM8994_AIF2) |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 478 | ret |= wm8994_bic_or(priv, WM8994_CLOCKING_1, |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 479 | WM8994_SYSCLK_SRC | WM8994_AIF2DSPCLK_ENA_MASK | |
| 480 | WM8994_SYSDSPCLK_ENA_MASK, WM8994_SYSCLK_SRC | |
| 481 | WM8994_AIF2DSPCLK_ENA | WM8994_SYSDSPCLK_ENA); |
| 482 | |
| 483 | if (ret < 0) { |
| 484 | debug("%s: codec register access error\n", __func__); |
| 485 | return -1; |
| 486 | } |
| 487 | |
| 488 | return 0; |
| 489 | } |
| 490 | |
| 491 | /* |
| 492 | * Configures Audio interface for the given frequency |
| 493 | * |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 494 | * @param priv wm8994 information |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 495 | * @param aif_id Audio Interface |
| 496 | * @param clk_id Input Clock ID |
| 497 | * @param freq Sampling frequency in Hz |
| 498 | * |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 499 | * Return: -1 for error and 0 success. |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 500 | */ |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 501 | static int wm8994_set_sysclk(struct wm8994_priv *priv, int aif_id, int clk_id, |
| 502 | unsigned int freq) |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 503 | { |
| 504 | int i; |
| 505 | int ret = 0; |
| 506 | |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 507 | priv->sysclk[aif_id - 1] = clk_id; |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 508 | |
| 509 | switch (clk_id) { |
| 510 | case WM8994_SYSCLK_MCLK1: |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 511 | priv->mclk[0] = freq; |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 512 | if (aif_id == 2) { |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 513 | ret = wm8994_bic_or(priv, WM8994_AIF1_CLOCKING_2, |
| 514 | WM8994_AIF2DAC_DIV_MASK, 0); |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 515 | } |
| 516 | break; |
| 517 | |
| 518 | case WM8994_SYSCLK_MCLK2: |
| 519 | /* TODO: Set GPIO AF */ |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 520 | priv->mclk[1] = freq; |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 521 | break; |
| 522 | |
| 523 | case WM8994_SYSCLK_FLL1: |
| 524 | case WM8994_SYSCLK_FLL2: |
| 525 | break; |
| 526 | |
| 527 | case WM8994_SYSCLK_OPCLK: |
| 528 | /* |
| 529 | * Special case - a division (times 10) is given and |
| 530 | * no effect on main clocking. |
| 531 | */ |
| 532 | if (freq) { |
| 533 | for (i = 0; i < ARRAY_SIZE(opclk_divs); i++) |
| 534 | if (opclk_divs[i] == freq) |
| 535 | break; |
| 536 | if (i == ARRAY_SIZE(opclk_divs)) { |
| 537 | debug("%s frequency divisor not found\n", |
Dani Krishna Mohan | 962e08c | 2013-09-11 16:38:46 +0530 | [diff] [blame] | 538 | __func__); |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 539 | return -1; |
| 540 | } |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 541 | ret = wm8994_bic_or(priv, WM8994_CLOCKING_2, |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 542 | WM8994_OPCLK_DIV_MASK, i); |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 543 | ret |= wm8994_bic_or(priv, WM8994_POWER_MANAGEMENT_2, |
| 544 | WM8994_OPCLK_ENA, |
| 545 | WM8994_OPCLK_ENA); |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 546 | } else { |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 547 | ret |= wm8994_bic_or(priv, WM8994_POWER_MANAGEMENT_2, |
| 548 | WM8994_OPCLK_ENA, 0); |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | default: |
| 552 | debug("%s Invalid input clock selection [%d]\n", |
| 553 | __func__, clk_id); |
| 554 | return -1; |
| 555 | } |
| 556 | |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 557 | ret |= configure_aif_clock(priv, aif_id); |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 558 | |
| 559 | if (ret < 0) { |
| 560 | debug("%s: codec register access error\n", __func__); |
| 561 | return -1; |
| 562 | } |
| 563 | |
| 564 | return 0; |
| 565 | } |
| 566 | |
| 567 | /* |
| 568 | * Initializes Volume for AIF2 to HP path |
| 569 | * |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 570 | * @param priv wm8994 information |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 571 | * @returns -1 for error and 0 Success. |
| 572 | * |
| 573 | */ |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 574 | static int wm8994_init_volume_aif2_dac1(struct wm8994_priv *priv) |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 575 | { |
| 576 | int ret; |
| 577 | |
| 578 | /* Unmute AIF2DAC */ |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 579 | ret = wm8994_bic_or(priv, WM8994_AIF2_DAC_FILTERS_1, |
| 580 | WM8994_AIF2DAC_MUTE_MASK, 0); |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 581 | |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 582 | ret |= wm8994_bic_or(priv, WM8994_AIF2_DAC_LEFT_VOLUME, |
| 583 | WM8994_AIF2DAC_VU_MASK | WM8994_AIF2DACL_VOL_MASK, |
| 584 | WM8994_AIF2DAC_VU | 0xff); |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 585 | |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 586 | ret |= wm8994_bic_or(priv, WM8994_AIF2_DAC_RIGHT_VOLUME, |
| 587 | WM8994_AIF2DAC_VU_MASK | WM8994_AIF2DACR_VOL_MASK, |
| 588 | WM8994_AIF2DAC_VU | 0xff); |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 589 | |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 590 | ret |= wm8994_bic_or(priv, WM8994_DAC1_LEFT_VOLUME, |
| 591 | WM8994_DAC1_VU_MASK | WM8994_DAC1L_VOL_MASK | |
| 592 | WM8994_DAC1L_MUTE_MASK, WM8994_DAC1_VU | 0xc0); |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 593 | |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 594 | ret |= wm8994_bic_or(priv, WM8994_DAC1_RIGHT_VOLUME, |
| 595 | WM8994_DAC1_VU_MASK | WM8994_DAC1R_VOL_MASK | |
| 596 | WM8994_DAC1R_MUTE_MASK, WM8994_DAC1_VU | 0xc0); |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 597 | /* Head Phone Volume */ |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 598 | ret |= wm8994_i2c_write(priv, WM8994_LEFT_OUTPUT_VOLUME, 0x12D); |
| 599 | ret |= wm8994_i2c_write(priv, WM8994_RIGHT_OUTPUT_VOLUME, 0x12D); |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 600 | |
| 601 | if (ret < 0) { |
| 602 | debug("%s: codec register access error\n", __func__); |
| 603 | return -1; |
| 604 | } |
| 605 | |
| 606 | return 0; |
| 607 | } |
| 608 | |
| 609 | /* |
Dani Krishna Mohan | 962e08c | 2013-09-11 16:38:46 +0530 | [diff] [blame] | 610 | * Initializes Volume for AIF1 to HP path |
| 611 | * |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 612 | * @param priv wm8994 information |
Dani Krishna Mohan | 962e08c | 2013-09-11 16:38:46 +0530 | [diff] [blame] | 613 | * @returns -1 for error and 0 Success. |
| 614 | * |
| 615 | */ |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 616 | static int wm8994_init_volume_aif1_dac1(struct wm8994_priv *priv) |
Dani Krishna Mohan | 962e08c | 2013-09-11 16:38:46 +0530 | [diff] [blame] | 617 | { |
| 618 | int ret = 0; |
| 619 | |
| 620 | /* Unmute AIF1DAC */ |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 621 | ret |= wm8994_i2c_write(priv, WM8994_AIF1_DAC_FILTERS_1, 0x0000); |
Dani Krishna Mohan | 962e08c | 2013-09-11 16:38:46 +0530 | [diff] [blame] | 622 | |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 623 | ret |= wm8994_bic_or(priv, WM8994_DAC1_LEFT_VOLUME, |
| 624 | WM8994_DAC1_VU_MASK | WM8994_DAC1L_VOL_MASK | |
| 625 | WM8994_DAC1L_MUTE_MASK, WM8994_DAC1_VU | 0xc0); |
Dani Krishna Mohan | 962e08c | 2013-09-11 16:38:46 +0530 | [diff] [blame] | 626 | |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 627 | ret |= wm8994_bic_or(priv, WM8994_DAC1_RIGHT_VOLUME, |
| 628 | WM8994_DAC1_VU_MASK | WM8994_DAC1R_VOL_MASK | |
| 629 | WM8994_DAC1R_MUTE_MASK, WM8994_DAC1_VU | 0xc0); |
Dani Krishna Mohan | 962e08c | 2013-09-11 16:38:46 +0530 | [diff] [blame] | 630 | /* Head Phone Volume */ |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 631 | ret |= wm8994_i2c_write(priv, WM8994_LEFT_OUTPUT_VOLUME, 0x12D); |
| 632 | ret |= wm8994_i2c_write(priv, WM8994_RIGHT_OUTPUT_VOLUME, 0x12D); |
Dani Krishna Mohan | 962e08c | 2013-09-11 16:38:46 +0530 | [diff] [blame] | 633 | |
| 634 | if (ret < 0) { |
| 635 | debug("%s: codec register access error\n", __func__); |
| 636 | return -1; |
| 637 | } |
| 638 | |
| 639 | return 0; |
| 640 | } |
| 641 | |
| 642 | /* |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 643 | * Intialise wm8994 codec device |
| 644 | * |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 645 | * @param priv wm8994 information |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 646 | * |
| 647 | * @returns -1 for error and 0 Success. |
| 648 | */ |
Simon Glass | 378b23a | 2018-12-03 04:37:27 -0700 | [diff] [blame] | 649 | static int wm8994_device_init(struct wm8994_priv *priv) |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 650 | { |
| 651 | const char *devname; |
| 652 | unsigned short reg_data; |
| 653 | int ret; |
| 654 | |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 655 | wm8994_i2c_write(priv, WM8994_SOFTWARE_RESET, WM8994_SW_RESET); |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 656 | |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 657 | ret = wm8994_i2c_read(priv, WM8994_SOFTWARE_RESET, ®_data); |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 658 | if (ret < 0) { |
| 659 | debug("Failed to read ID register\n"); |
Simon Glass | 378b23a | 2018-12-03 04:37:27 -0700 | [diff] [blame] | 660 | return ret; |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | if (reg_data == WM8994_ID) { |
| 664 | devname = "WM8994"; |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 665 | debug("Device registered as type %d\n", priv->type); |
| 666 | priv->type = WM8994; |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 667 | } else { |
| 668 | debug("Device is not a WM8994, ID is %x\n", ret); |
Simon Glass | 378b23a | 2018-12-03 04:37:27 -0700 | [diff] [blame] | 669 | return -ENXIO; |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 670 | } |
| 671 | |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 672 | ret = wm8994_i2c_read(priv, WM8994_CHIP_REVISION, ®_data); |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 673 | if (ret < 0) { |
| 674 | debug("Failed to read revision register: %d\n", ret); |
Simon Glass | 378b23a | 2018-12-03 04:37:27 -0700 | [diff] [blame] | 675 | return ret; |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 676 | } |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 677 | priv->revision = reg_data; |
| 678 | debug("%s revision %c\n", devname, 'A' + priv->revision); |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 679 | |
Simon Glass | 378b23a | 2018-12-03 04:37:27 -0700 | [diff] [blame] | 680 | return 0; |
| 681 | } |
| 682 | |
| 683 | static int wm8994_setup_interface(struct wm8994_priv *priv, |
| 684 | enum en_audio_interface aif_id) |
| 685 | { |
| 686 | int ret; |
| 687 | |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 688 | /* VMID Selection */ |
Simon Glass | 378b23a | 2018-12-03 04:37:27 -0700 | [diff] [blame] | 689 | ret = wm8994_bic_or(priv, WM8994_POWER_MANAGEMENT_1, |
| 690 | WM8994_VMID_SEL_MASK | WM8994_BIAS_ENA_MASK, 0x3); |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 691 | |
| 692 | /* Charge Pump Enable */ |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 693 | ret |= wm8994_bic_or(priv, WM8994_CHARGE_PUMP_1, WM8994_CP_ENA_MASK, |
| 694 | WM8994_CP_ENA); |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 695 | |
| 696 | /* Head Phone Power Enable */ |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 697 | ret |= wm8994_bic_or(priv, WM8994_POWER_MANAGEMENT_1, |
| 698 | WM8994_HPOUT1L_ENA_MASK, WM8994_HPOUT1L_ENA); |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 699 | |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 700 | ret |= wm8994_bic_or(priv, WM8994_POWER_MANAGEMENT_1, |
| 701 | WM8994_HPOUT1R_ENA_MASK, WM8994_HPOUT1R_ENA); |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 702 | |
Dani Krishna Mohan | 962e08c | 2013-09-11 16:38:46 +0530 | [diff] [blame] | 703 | if (aif_id == WM8994_AIF1) { |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 704 | ret |= wm8994_i2c_write(priv, WM8994_POWER_MANAGEMENT_2, |
Dani Krishna Mohan | 962e08c | 2013-09-11 16:38:46 +0530 | [diff] [blame] | 705 | WM8994_TSHUT_ENA | WM8994_MIXINL_ENA | |
| 706 | WM8994_MIXINR_ENA | WM8994_IN2L_ENA | |
| 707 | WM8994_IN2R_ENA); |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 708 | |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 709 | ret |= wm8994_i2c_write(priv, WM8994_POWER_MANAGEMENT_4, |
Dani Krishna Mohan | 962e08c | 2013-09-11 16:38:46 +0530 | [diff] [blame] | 710 | WM8994_ADCL_ENA | WM8994_ADCR_ENA | |
| 711 | WM8994_AIF1ADC1R_ENA | |
| 712 | WM8994_AIF1ADC1L_ENA); |
| 713 | |
| 714 | /* Power enable for AIF1 and DAC1 */ |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 715 | ret |= wm8994_i2c_write(priv, WM8994_POWER_MANAGEMENT_5, |
Dani Krishna Mohan | 962e08c | 2013-09-11 16:38:46 +0530 | [diff] [blame] | 716 | WM8994_AIF1DACL_ENA | |
| 717 | WM8994_AIF1DACR_ENA | |
| 718 | WM8994_DAC1L_ENA | WM8994_DAC1R_ENA); |
| 719 | } else if (aif_id == WM8994_AIF2) { |
| 720 | /* Power enable for AIF2 and DAC1 */ |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 721 | ret |= wm8994_bic_or(priv, WM8994_POWER_MANAGEMENT_5, |
Dani Krishna Mohan | 962e08c | 2013-09-11 16:38:46 +0530 | [diff] [blame] | 722 | WM8994_AIF2DACL_ENA_MASK | WM8994_AIF2DACR_ENA_MASK | |
| 723 | WM8994_DAC1L_ENA_MASK | WM8994_DAC1R_ENA_MASK, |
| 724 | WM8994_AIF2DACL_ENA | WM8994_AIF2DACR_ENA | |
| 725 | WM8994_DAC1L_ENA | WM8994_DAC1R_ENA); |
| 726 | } |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 727 | /* Head Phone Initialisation */ |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 728 | ret |= wm8994_bic_or(priv, WM8994_ANALOGUE_HP_1, |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 729 | WM8994_HPOUT1L_DLY_MASK | WM8994_HPOUT1R_DLY_MASK, |
| 730 | WM8994_HPOUT1L_DLY | WM8994_HPOUT1R_DLY); |
| 731 | |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 732 | ret |= wm8994_bic_or(priv, WM8994_DC_SERVO_1, |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 733 | WM8994_DCS_ENA_CHAN_0_MASK | |
| 734 | WM8994_DCS_ENA_CHAN_1_MASK , WM8994_DCS_ENA_CHAN_0 | |
| 735 | WM8994_DCS_ENA_CHAN_1); |
| 736 | |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 737 | ret |= wm8994_bic_or(priv, WM8994_ANALOGUE_HP_1, |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 738 | WM8994_HPOUT1L_DLY_MASK | |
| 739 | WM8994_HPOUT1R_DLY_MASK | WM8994_HPOUT1L_OUTP_MASK | |
| 740 | WM8994_HPOUT1R_OUTP_MASK | |
| 741 | WM8994_HPOUT1L_RMV_SHORT_MASK | |
| 742 | WM8994_HPOUT1R_RMV_SHORT_MASK, WM8994_HPOUT1L_DLY | |
| 743 | WM8994_HPOUT1R_DLY | WM8994_HPOUT1L_OUTP | |
| 744 | WM8994_HPOUT1R_OUTP | WM8994_HPOUT1L_RMV_SHORT | |
| 745 | WM8994_HPOUT1R_RMV_SHORT); |
| 746 | |
| 747 | /* MIXER Config DAC1 to HP */ |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 748 | ret |= wm8994_bic_or(priv, WM8994_OUTPUT_MIXER_1, |
| 749 | WM8994_DAC1L_TO_HPOUT1L_MASK, |
| 750 | WM8994_DAC1L_TO_HPOUT1L); |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 751 | |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 752 | ret |= wm8994_bic_or(priv, WM8994_OUTPUT_MIXER_2, |
| 753 | WM8994_DAC1R_TO_HPOUT1R_MASK, |
| 754 | WM8994_DAC1R_TO_HPOUT1R); |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 755 | |
Dani Krishna Mohan | 962e08c | 2013-09-11 16:38:46 +0530 | [diff] [blame] | 756 | if (aif_id == WM8994_AIF1) { |
| 757 | /* Routing AIF1 to DAC1 */ |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 758 | ret |= wm8994_i2c_write(priv, WM8994_DAC1_LEFT_MIXER_ROUTING, |
| 759 | WM8994_AIF1DAC1L_TO_DAC1L); |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 760 | |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 761 | ret |= wm8994_i2c_write(priv, WM8994_DAC1_RIGHT_MIXER_ROUTING, |
Dani Krishna Mohan | 962e08c | 2013-09-11 16:38:46 +0530 | [diff] [blame] | 762 | WM8994_AIF1DAC1R_TO_DAC1R); |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 763 | |
Dani Krishna Mohan | 962e08c | 2013-09-11 16:38:46 +0530 | [diff] [blame] | 764 | /* GPIO Settings for AIF1 */ |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 765 | ret |= wm8994_i2c_write(priv, WM8994_GPIO_1, |
| 766 | WM8994_GPIO_DIR_OUTPUT | |
| 767 | WM8994_GPIO_FUNCTION_I2S_CLK | |
| 768 | WM8994_GPIO_INPUT_DEBOUNCE); |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 769 | |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 770 | ret |= wm8994_init_volume_aif1_dac1(priv); |
Dani Krishna Mohan | 962e08c | 2013-09-11 16:38:46 +0530 | [diff] [blame] | 771 | } else if (aif_id == WM8994_AIF2) { |
| 772 | /* Routing AIF2 to DAC1 */ |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 773 | ret |= wm8994_bic_or(priv, WM8994_DAC1_LEFT_MIXER_ROUTING, |
| 774 | WM8994_AIF2DACL_TO_DAC1L_MASK, |
| 775 | WM8994_AIF2DACL_TO_DAC1L); |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 776 | |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 777 | ret |= wm8994_bic_or(priv, WM8994_DAC1_RIGHT_MIXER_ROUTING, |
| 778 | WM8994_AIF2DACR_TO_DAC1R_MASK, |
| 779 | WM8994_AIF2DACR_TO_DAC1R); |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 780 | |
Dani Krishna Mohan | 962e08c | 2013-09-11 16:38:46 +0530 | [diff] [blame] | 781 | /* GPIO Settings for AIF2 */ |
| 782 | /* B CLK */ |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 783 | ret |= wm8994_bic_or(priv, WM8994_GPIO_3, WM8994_GPIO_DIR_MASK | |
| 784 | WM8994_GPIO_FUNCTION_MASK, |
| 785 | WM8994_GPIO_DIR_OUTPUT); |
Dani Krishna Mohan | 962e08c | 2013-09-11 16:38:46 +0530 | [diff] [blame] | 786 | |
| 787 | /* LR CLK */ |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 788 | ret |= wm8994_bic_or(priv, WM8994_GPIO_4, WM8994_GPIO_DIR_MASK | |
| 789 | WM8994_GPIO_FUNCTION_MASK, |
| 790 | WM8994_GPIO_DIR_OUTPUT); |
Dani Krishna Mohan | 962e08c | 2013-09-11 16:38:46 +0530 | [diff] [blame] | 791 | |
| 792 | /* DATA */ |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 793 | ret |= wm8994_bic_or(priv, WM8994_GPIO_5, WM8994_GPIO_DIR_MASK | |
| 794 | WM8994_GPIO_FUNCTION_MASK, |
| 795 | WM8994_GPIO_DIR_OUTPUT); |
Dani Krishna Mohan | 962e08c | 2013-09-11 16:38:46 +0530 | [diff] [blame] | 796 | |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 797 | ret |= wm8994_init_volume_aif2_dac1(priv); |
Dani Krishna Mohan | 962e08c | 2013-09-11 16:38:46 +0530 | [diff] [blame] | 798 | } |
| 799 | |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 800 | if (ret < 0) |
| 801 | goto err; |
| 802 | |
Simon Glass | 378b23a | 2018-12-03 04:37:27 -0700 | [diff] [blame] | 803 | debug("%s: Codec chip setup ok\n", __func__); |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 804 | return 0; |
| 805 | err: |
Simon Glass | 378b23a | 2018-12-03 04:37:27 -0700 | [diff] [blame] | 806 | debug("%s: Codec chip setup error\n", __func__); |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 807 | return -1; |
| 808 | } |
| 809 | |
Simon Glass | 998f4d8 | 2018-12-03 04:37:26 -0700 | [diff] [blame] | 810 | static int _wm8994_init(struct wm8994_priv *priv, |
| 811 | enum en_audio_interface aif_id, int sampling_rate, |
| 812 | int mclk_freq, int bits_per_sample, |
| 813 | unsigned int channels) |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 814 | { |
Simon Glass | 998f4d8 | 2018-12-03 04:37:26 -0700 | [diff] [blame] | 815 | int ret; |
Rajeshwari Shinde | d5d9a6e | 2012-12-26 20:03:18 +0000 | [diff] [blame] | 816 | |
Simon Glass | 378b23a | 2018-12-03 04:37:27 -0700 | [diff] [blame] | 817 | ret = wm8994_setup_interface(priv, aif_id); |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 818 | if (ret < 0) { |
| 819 | debug("%s: wm8994 codec chip init failed\n", __func__); |
| 820 | return ret; |
| 821 | } |
| 822 | |
Simon Glass | 4070ba6 | 2018-12-10 10:37:39 -0700 | [diff] [blame] | 823 | ret = wm8994_set_sysclk(priv, aif_id, WM8994_SYSCLK_MCLK1, mclk_freq); |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 824 | if (ret < 0) { |
| 825 | debug("%s: wm8994 codec set sys clock failed\n", __func__); |
| 826 | return ret; |
| 827 | } |
| 828 | |
Simon Glass | 998f4d8 | 2018-12-03 04:37:26 -0700 | [diff] [blame] | 829 | ret = wm8994_hw_params(priv, aif_id, sampling_rate, bits_per_sample, |
| 830 | channels); |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 831 | |
| 832 | if (ret == 0) { |
Simon Glass | 998f4d8 | 2018-12-03 04:37:26 -0700 | [diff] [blame] | 833 | ret = wm8994_set_fmt(priv, aif_id, SND_SOC_DAIFMT_I2S | |
| 834 | SND_SOC_DAIFMT_NB_NF | |
Simon Glass | e9be4cf | 2018-12-03 04:37:24 -0700 | [diff] [blame] | 835 | SND_SOC_DAIFMT_CBS_CFS); |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 836 | } |
Simon Glass | 998f4d8 | 2018-12-03 04:37:26 -0700 | [diff] [blame] | 837 | |
Rajeshwari Shinde | bd3841f | 2012-10-25 19:49:23 +0000 | [diff] [blame] | 838 | return ret; |
| 839 | } |
Simon Glass | 4070ba6 | 2018-12-10 10:37:39 -0700 | [diff] [blame] | 840 | |
| 841 | static int wm8994_set_params(struct udevice *dev, int interface, int rate, |
| 842 | int mclk_freq, int bits_per_sample, uint channels) |
| 843 | { |
| 844 | struct wm8994_priv *priv = dev_get_priv(dev); |
| 845 | |
| 846 | return _wm8994_init(priv, interface, rate, mclk_freq, bits_per_sample, |
| 847 | channels); |
| 848 | } |
| 849 | |
| 850 | static int wm8994_probe(struct udevice *dev) |
| 851 | { |
| 852 | struct wm8994_priv *priv = dev_get_priv(dev); |
| 853 | |
| 854 | priv->dev = dev; |
| 855 | return wm8994_device_init(priv); |
| 856 | } |
| 857 | |
| 858 | static const struct audio_codec_ops wm8994_ops = { |
| 859 | .set_params = wm8994_set_params, |
| 860 | }; |
| 861 | |
| 862 | static const struct udevice_id wm8994_ids[] = { |
| 863 | { .compatible = "wolfson,wm8994" }, |
| 864 | { } |
| 865 | }; |
| 866 | |
| 867 | U_BOOT_DRIVER(wm8994) = { |
| 868 | .name = "wm8994", |
| 869 | .id = UCLASS_AUDIO_CODEC, |
| 870 | .of_match = wm8994_ids, |
| 871 | .probe = wm8994_probe, |
| 872 | .ops = &wm8994_ops, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 873 | .priv_auto = sizeof(struct wm8994_priv), |
Simon Glass | 4070ba6 | 2018-12-10 10:37:39 -0700 | [diff] [blame] | 874 | }; |