Simon Glass | c301445 | 2018-12-10 10:37:48 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Rajeshwari Shinde | cd93e9b | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 2 | /* |
| 3 | * max98095.c -- MAX98095 ALSA SoC Audio driver |
| 4 | * |
| 5 | * Copyright 2011 Maxim Integrated Products |
| 6 | * |
Simon Glass | c301445 | 2018-12-10 10:37:48 -0700 | [diff] [blame] | 7 | * Modified for U-Boot by R. Chandrasekar (rcsekar@samsung.com) |
Rajeshwari Shinde | cd93e9b | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 8 | */ |
Simon Glass | 1d9af1f | 2017-05-30 21:47:09 -0600 | [diff] [blame] | 9 | |
| 10 | #include <common.h> |
Simon Glass | 4070ba6 | 2018-12-10 10:37:39 -0700 | [diff] [blame] | 11 | #include <audio_codec.h> |
| 12 | #include <dm.h> |
Rajeshwari Shinde | cd93e9b | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 13 | #include <div64.h> |
| 14 | #include <fdtdec.h> |
| 15 | #include <i2c.h> |
| 16 | #include <sound.h> |
Simon Glass | f222236 | 2018-12-03 04:37:34 -0700 | [diff] [blame] | 17 | #include <asm/gpio.h> |
Rajeshwari Shinde | cd93e9b | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 18 | #include "i2s.h" |
| 19 | #include "max98095.h" |
| 20 | |
Rajeshwari Shinde | cd93e9b | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 21 | /* Index 0 is reserved. */ |
| 22 | int rate_table[] = {0, 8000, 11025, 16000, 22050, 24000, 32000, 44100, 48000, |
| 23 | 88200, 96000}; |
| 24 | |
| 25 | /* |
Rajeshwari Shinde | cd93e9b | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 26 | * codec mclk clock divider coefficients based on sampling rate |
| 27 | * |
| 28 | * @param rate sampling rate |
| 29 | * @param value address of indexvalue to be stored |
| 30 | * |
| 31 | * @return 0 for success or negative error code. |
| 32 | */ |
| 33 | static int rate_value(int rate, u8 *value) |
| 34 | { |
| 35 | int i; |
| 36 | |
| 37 | for (i = 1; i < ARRAY_SIZE(rate_table); i++) { |
| 38 | if (rate_table[i] >= rate) { |
| 39 | *value = i; |
| 40 | return 0; |
| 41 | } |
| 42 | } |
| 43 | *value = 1; |
| 44 | |
Simon Glass | ad2a5e3 | 2018-12-10 10:37:49 -0700 | [diff] [blame] | 45 | return -EINVAL; |
Rajeshwari Shinde | cd93e9b | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | /* |
| 49 | * Sets hw params for max98095 |
| 50 | * |
Simon Glass | fd7d697 | 2018-12-03 04:37:25 -0700 | [diff] [blame] | 51 | * @param priv max98095 information pointer |
Rajeshwari Shinde | cd93e9b | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 52 | * @param rate Sampling rate |
| 53 | * @param bits_per_sample Bits per sample |
| 54 | * |
Simon Glass | ad2a5e3 | 2018-12-10 10:37:49 -0700 | [diff] [blame] | 55 | * @return 0 for success or negative error code. |
Rajeshwari Shinde | cd93e9b | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 56 | */ |
Simon Glass | 504a790 | 2018-12-10 10:37:42 -0700 | [diff] [blame] | 57 | static int max98095_hw_params(struct maxim_priv *priv, |
Dani Krishna Mohan | 6e19490 | 2013-09-11 16:38:50 +0530 | [diff] [blame] | 58 | enum en_max_audio_interface aif_id, |
| 59 | unsigned int rate, unsigned int bits_per_sample) |
Rajeshwari Shinde | cd93e9b | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 60 | { |
| 61 | u8 regval; |
| 62 | int error; |
Dani Krishna Mohan | 6e19490 | 2013-09-11 16:38:50 +0530 | [diff] [blame] | 63 | unsigned short M98095_DAI_CLKMODE; |
| 64 | unsigned short M98095_DAI_FORMAT; |
| 65 | unsigned short M98095_DAI_FILTERS; |
| 66 | |
| 67 | if (aif_id == AIF1) { |
| 68 | M98095_DAI_CLKMODE = M98095_027_DAI1_CLKMODE; |
| 69 | M98095_DAI_FORMAT = M98095_02A_DAI1_FORMAT; |
| 70 | M98095_DAI_FILTERS = M98095_02E_DAI1_FILTERS; |
| 71 | } else { |
| 72 | M98095_DAI_CLKMODE = M98095_031_DAI2_CLKMODE; |
| 73 | M98095_DAI_FORMAT = M98095_034_DAI2_FORMAT; |
| 74 | M98095_DAI_FILTERS = M98095_038_DAI2_FILTERS; |
| 75 | } |
Rajeshwari Shinde | cd93e9b | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 76 | |
| 77 | switch (bits_per_sample) { |
| 78 | case 16: |
Simon Glass | 504a790 | 2018-12-10 10:37:42 -0700 | [diff] [blame] | 79 | error = maxim_bic_or(priv, M98095_DAI_FORMAT, M98095_DAI_WS, 0); |
Rajeshwari Shinde | cd93e9b | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 80 | break; |
| 81 | case 24: |
Simon Glass | 504a790 | 2018-12-10 10:37:42 -0700 | [diff] [blame] | 82 | error = maxim_bic_or(priv, M98095_DAI_FORMAT, M98095_DAI_WS, |
| 83 | M98095_DAI_WS); |
Rajeshwari Shinde | cd93e9b | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 84 | break; |
| 85 | default: |
| 86 | debug("%s: Illegal bits per sample %d.\n", |
Dani Krishna Mohan | 6e19490 | 2013-09-11 16:38:50 +0530 | [diff] [blame] | 87 | __func__, bits_per_sample); |
Simon Glass | ad2a5e3 | 2018-12-10 10:37:49 -0700 | [diff] [blame] | 88 | return -EINVAL; |
Rajeshwari Shinde | cd93e9b | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | if (rate_value(rate, ®val)) { |
| 92 | debug("%s: Failed to set sample rate to %d.\n", |
Dani Krishna Mohan | 6e19490 | 2013-09-11 16:38:50 +0530 | [diff] [blame] | 93 | __func__, rate); |
Simon Glass | ad2a5e3 | 2018-12-10 10:37:49 -0700 | [diff] [blame] | 94 | return -EINVAL; |
Rajeshwari Shinde | cd93e9b | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 95 | } |
Simon Glass | fd7d697 | 2018-12-03 04:37:25 -0700 | [diff] [blame] | 96 | priv->rate = rate; |
Rajeshwari Shinde | cd93e9b | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 97 | |
Simon Glass | 504a790 | 2018-12-10 10:37:42 -0700 | [diff] [blame] | 98 | error |= maxim_bic_or(priv, M98095_DAI_CLKMODE, M98095_CLKMODE_MASK, |
Simon Glass | fd7d697 | 2018-12-03 04:37:25 -0700 | [diff] [blame] | 99 | regval); |
Rajeshwari Shinde | cd93e9b | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 100 | |
| 101 | /* Update sample rate mode */ |
| 102 | if (rate < 50000) |
Simon Glass | 504a790 | 2018-12-10 10:37:42 -0700 | [diff] [blame] | 103 | error |= maxim_bic_or(priv, M98095_DAI_FILTERS, |
Simon Glass | fd7d697 | 2018-12-03 04:37:25 -0700 | [diff] [blame] | 104 | M98095_DAI_DHF, 0); |
Rajeshwari Shinde | cd93e9b | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 105 | else |
Simon Glass | 504a790 | 2018-12-10 10:37:42 -0700 | [diff] [blame] | 106 | error |= maxim_bic_or(priv, M98095_DAI_FILTERS, |
Simon Glass | fd7d697 | 2018-12-03 04:37:25 -0700 | [diff] [blame] | 107 | M98095_DAI_DHF, M98095_DAI_DHF); |
Rajeshwari Shinde | cd93e9b | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 108 | |
| 109 | if (error < 0) { |
| 110 | debug("%s: Error setting hardware params.\n", __func__); |
Simon Glass | ad2a5e3 | 2018-12-10 10:37:49 -0700 | [diff] [blame] | 111 | return -EIO; |
Rajeshwari Shinde | cd93e9b | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | /* |
| 118 | * Configures Audio interface system clock for the given frequency |
| 119 | * |
Simon Glass | fd7d697 | 2018-12-03 04:37:25 -0700 | [diff] [blame] | 120 | * @param priv max98095 information |
Rajeshwari Shinde | cd93e9b | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 121 | * @param freq Sampling frequency in Hz |
| 122 | * |
Simon Glass | ad2a5e3 | 2018-12-10 10:37:49 -0700 | [diff] [blame] | 123 | * @return 0 for success or negative error code. |
Rajeshwari Shinde | cd93e9b | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 124 | */ |
Simon Glass | 504a790 | 2018-12-10 10:37:42 -0700 | [diff] [blame] | 125 | static int max98095_set_sysclk(struct maxim_priv *priv, unsigned int freq) |
Rajeshwari Shinde | cd93e9b | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 126 | { |
| 127 | int error = 0; |
| 128 | |
| 129 | /* Requested clock frequency is already setup */ |
Simon Glass | fd7d697 | 2018-12-03 04:37:25 -0700 | [diff] [blame] | 130 | if (freq == priv->sysclk) |
Rajeshwari Shinde | cd93e9b | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 131 | return 0; |
| 132 | |
| 133 | /* Setup clocks for slave mode, and using the PLL |
| 134 | * PSCLK = 0x01 (when master clk is 10MHz to 20MHz) |
| 135 | * 0x02 (when master clk is 20MHz to 40MHz).. |
| 136 | * 0x03 (when master clk is 40MHz to 60MHz).. |
| 137 | */ |
| 138 | if ((freq >= 10000000) && (freq < 20000000)) { |
Simon Glass | 504a790 | 2018-12-10 10:37:42 -0700 | [diff] [blame] | 139 | error = maxim_i2c_write(priv, M98095_026_SYS_CLK, 0x10); |
Rajeshwari Shinde | cd93e9b | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 140 | } else if ((freq >= 20000000) && (freq < 40000000)) { |
Simon Glass | 504a790 | 2018-12-10 10:37:42 -0700 | [diff] [blame] | 141 | error = maxim_i2c_write(priv, M98095_026_SYS_CLK, 0x20); |
Rajeshwari Shinde | cd93e9b | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 142 | } else if ((freq >= 40000000) && (freq < 60000000)) { |
Simon Glass | 504a790 | 2018-12-10 10:37:42 -0700 | [diff] [blame] | 143 | error = maxim_i2c_write(priv, M98095_026_SYS_CLK, 0x30); |
Rajeshwari Shinde | cd93e9b | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 144 | } else { |
| 145 | debug("%s: Invalid master clock frequency\n", __func__); |
Simon Glass | ad2a5e3 | 2018-12-10 10:37:49 -0700 | [diff] [blame] | 146 | return -EINVAL; |
Rajeshwari Shinde | cd93e9b | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | debug("%s: Clock at %uHz\n", __func__, freq); |
| 150 | |
| 151 | if (error < 0) |
Simon Glass | ad2a5e3 | 2018-12-10 10:37:49 -0700 | [diff] [blame] | 152 | return -EIO; |
Rajeshwari Shinde | cd93e9b | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 153 | |
Simon Glass | fd7d697 | 2018-12-03 04:37:25 -0700 | [diff] [blame] | 154 | priv->sysclk = freq; |
Rajeshwari Shinde | cd93e9b | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | /* |
| 159 | * Sets Max98095 I2S format |
| 160 | * |
Simon Glass | fd7d697 | 2018-12-03 04:37:25 -0700 | [diff] [blame] | 161 | * @param priv max98095 information |
Rajeshwari Shinde | cd93e9b | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 162 | * @param fmt i2S format - supports a subset of the options defined |
| 163 | * in i2s.h. |
| 164 | * |
Simon Glass | ad2a5e3 | 2018-12-10 10:37:49 -0700 | [diff] [blame] | 165 | * @return 0 for success or negative error code. |
Rajeshwari Shinde | cd93e9b | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 166 | */ |
Simon Glass | 504a790 | 2018-12-10 10:37:42 -0700 | [diff] [blame] | 167 | static int max98095_set_fmt(struct maxim_priv *priv, int fmt, |
Dani Krishna Mohan | 6e19490 | 2013-09-11 16:38:50 +0530 | [diff] [blame] | 168 | enum en_max_audio_interface aif_id) |
Rajeshwari Shinde | cd93e9b | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 169 | { |
| 170 | u8 regval = 0; |
| 171 | int error = 0; |
Dani Krishna Mohan | 6e19490 | 2013-09-11 16:38:50 +0530 | [diff] [blame] | 172 | unsigned short M98095_DAI_CLKCFG_HI; |
| 173 | unsigned short M98095_DAI_CLKCFG_LO; |
| 174 | unsigned short M98095_DAI_FORMAT; |
| 175 | unsigned short M98095_DAI_CLOCK; |
Rajeshwari Shinde | cd93e9b | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 176 | |
Simon Glass | fd7d697 | 2018-12-03 04:37:25 -0700 | [diff] [blame] | 177 | if (fmt == priv->fmt) |
Rajeshwari Shinde | cd93e9b | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 178 | return 0; |
| 179 | |
Simon Glass | fd7d697 | 2018-12-03 04:37:25 -0700 | [diff] [blame] | 180 | priv->fmt = fmt; |
Rajeshwari Shinde | cd93e9b | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 181 | |
Dani Krishna Mohan | 6e19490 | 2013-09-11 16:38:50 +0530 | [diff] [blame] | 182 | if (aif_id == AIF1) { |
| 183 | M98095_DAI_CLKCFG_HI = M98095_028_DAI1_CLKCFG_HI; |
| 184 | M98095_DAI_CLKCFG_LO = M98095_029_DAI1_CLKCFG_LO; |
| 185 | M98095_DAI_FORMAT = M98095_02A_DAI1_FORMAT; |
| 186 | M98095_DAI_CLOCK = M98095_02B_DAI1_CLOCK; |
| 187 | } else { |
| 188 | M98095_DAI_CLKCFG_HI = M98095_032_DAI2_CLKCFG_HI; |
| 189 | M98095_DAI_CLKCFG_LO = M98095_033_DAI2_CLKCFG_LO; |
| 190 | M98095_DAI_FORMAT = M98095_034_DAI2_FORMAT; |
| 191 | M98095_DAI_CLOCK = M98095_035_DAI2_CLOCK; |
| 192 | } |
| 193 | |
Rajeshwari Shinde | cd93e9b | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 194 | switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { |
| 195 | case SND_SOC_DAIFMT_CBS_CFS: |
| 196 | /* Slave mode PLL */ |
Simon Glass | 504a790 | 2018-12-10 10:37:42 -0700 | [diff] [blame] | 197 | error |= maxim_i2c_write(priv, M98095_DAI_CLKCFG_HI, 0x80); |
| 198 | error |= maxim_i2c_write(priv, M98095_DAI_CLKCFG_LO, 0x00); |
Rajeshwari Shinde | cd93e9b | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 199 | break; |
| 200 | case SND_SOC_DAIFMT_CBM_CFM: |
| 201 | /* Set to master mode */ |
| 202 | regval |= M98095_DAI_MAS; |
| 203 | break; |
| 204 | case SND_SOC_DAIFMT_CBS_CFM: |
| 205 | case SND_SOC_DAIFMT_CBM_CFS: |
| 206 | default: |
| 207 | debug("%s: Clock mode unsupported\n", __func__); |
Simon Glass | ad2a5e3 | 2018-12-10 10:37:49 -0700 | [diff] [blame] | 208 | return -EINVAL; |
Rajeshwari Shinde | cd93e9b | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { |
| 212 | case SND_SOC_DAIFMT_I2S: |
| 213 | regval |= M98095_DAI_DLY; |
| 214 | break; |
| 215 | case SND_SOC_DAIFMT_LEFT_J: |
| 216 | break; |
| 217 | default: |
| 218 | debug("%s: Unrecognized format.\n", __func__); |
Simon Glass | ad2a5e3 | 2018-12-10 10:37:49 -0700 | [diff] [blame] | 219 | return -EINVAL; |
Rajeshwari Shinde | cd93e9b | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | switch (fmt & SND_SOC_DAIFMT_INV_MASK) { |
| 223 | case SND_SOC_DAIFMT_NB_NF: |
| 224 | break; |
| 225 | case SND_SOC_DAIFMT_NB_IF: |
| 226 | regval |= M98095_DAI_WCI; |
| 227 | break; |
| 228 | case SND_SOC_DAIFMT_IB_NF: |
| 229 | regval |= M98095_DAI_BCI; |
| 230 | break; |
| 231 | case SND_SOC_DAIFMT_IB_IF: |
| 232 | regval |= M98095_DAI_BCI | M98095_DAI_WCI; |
| 233 | break; |
| 234 | default: |
| 235 | debug("%s: Unrecognized inversion settings.\n", __func__); |
Simon Glass | ad2a5e3 | 2018-12-10 10:37:49 -0700 | [diff] [blame] | 236 | return -EINVAL; |
Rajeshwari Shinde | cd93e9b | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 237 | } |
| 238 | |
Simon Glass | 504a790 | 2018-12-10 10:37:42 -0700 | [diff] [blame] | 239 | error |= maxim_bic_or(priv, M98095_DAI_FORMAT, |
Simon Glass | fd7d697 | 2018-12-03 04:37:25 -0700 | [diff] [blame] | 240 | M98095_DAI_MAS | M98095_DAI_DLY | |
| 241 | M98095_DAI_BCI | M98095_DAI_WCI, regval); |
Rajeshwari Shinde | cd93e9b | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 242 | |
Simon Glass | 504a790 | 2018-12-10 10:37:42 -0700 | [diff] [blame] | 243 | error |= maxim_i2c_write(priv, M98095_DAI_CLOCK, M98095_DAI_BSEL64); |
Rajeshwari Shinde | cd93e9b | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 244 | |
| 245 | if (error < 0) { |
| 246 | debug("%s: Error setting i2s format.\n", __func__); |
Simon Glass | ad2a5e3 | 2018-12-10 10:37:49 -0700 | [diff] [blame] | 247 | return -EIO; |
Rajeshwari Shinde | cd93e9b | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | return 0; |
| 251 | } |
| 252 | |
| 253 | /* |
| 254 | * resets the audio codec |
| 255 | * |
Simon Glass | fd7d697 | 2018-12-03 04:37:25 -0700 | [diff] [blame] | 256 | * @param priv Private data for driver |
Simon Glass | ad2a5e3 | 2018-12-10 10:37:49 -0700 | [diff] [blame] | 257 | * @return 0 for success or negative error code. |
Rajeshwari Shinde | cd93e9b | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 258 | */ |
Simon Glass | 504a790 | 2018-12-10 10:37:42 -0700 | [diff] [blame] | 259 | static int max98095_reset(struct maxim_priv *priv) |
Rajeshwari Shinde | cd93e9b | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 260 | { |
| 261 | int i, ret; |
| 262 | |
| 263 | /* |
| 264 | * Gracefully reset the DSP core and the codec hardware in a proper |
| 265 | * sequence. |
| 266 | */ |
Simon Glass | 504a790 | 2018-12-10 10:37:42 -0700 | [diff] [blame] | 267 | ret = maxim_i2c_write(priv, M98095_00F_HOST_CFG, 0); |
Rajeshwari Shinde | cd93e9b | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 268 | if (ret != 0) { |
| 269 | debug("%s: Failed to reset DSP: %d\n", __func__, ret); |
| 270 | return ret; |
| 271 | } |
| 272 | |
Simon Glass | 504a790 | 2018-12-10 10:37:42 -0700 | [diff] [blame] | 273 | ret = maxim_i2c_write(priv, M98095_097_PWR_SYS, 0); |
Rajeshwari Shinde | cd93e9b | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 274 | if (ret != 0) { |
| 275 | debug("%s: Failed to reset codec: %d\n", __func__, ret); |
| 276 | return ret; |
| 277 | } |
| 278 | |
| 279 | /* |
| 280 | * Reset to hardware default for registers, as there is not a soft |
| 281 | * reset hardware control register. |
| 282 | */ |
| 283 | for (i = M98095_010_HOST_INT_CFG; i < M98095_REG_MAX_CACHED; i++) { |
Simon Glass | 504a790 | 2018-12-10 10:37:42 -0700 | [diff] [blame] | 284 | ret = maxim_i2c_write(priv, i, 0); |
Rajeshwari Shinde | cd93e9b | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 285 | if (ret < 0) { |
| 286 | debug("%s: Failed to reset: %d\n", __func__, ret); |
| 287 | return ret; |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | return 0; |
| 292 | } |
| 293 | |
| 294 | /* |
| 295 | * Intialise max98095 codec device |
| 296 | * |
Simon Glass | fd7d697 | 2018-12-03 04:37:25 -0700 | [diff] [blame] | 297 | * @param priv max98095 information |
Simon Glass | ad2a5e3 | 2018-12-10 10:37:49 -0700 | [diff] [blame] | 298 | * @return 0 for success or negative error code. |
Rajeshwari Shinde | cd93e9b | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 299 | */ |
Simon Glass | 504a790 | 2018-12-10 10:37:42 -0700 | [diff] [blame] | 300 | static int max98095_device_init(struct maxim_priv *priv) |
Rajeshwari Shinde | cd93e9b | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 301 | { |
| 302 | unsigned char id; |
Simon Glass | ad2a5e3 | 2018-12-10 10:37:49 -0700 | [diff] [blame] | 303 | int ret; |
Rajeshwari Shinde | cd93e9b | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 304 | |
| 305 | /* reset the codec, the DSP core, and disable all interrupts */ |
Simon Glass | ad2a5e3 | 2018-12-10 10:37:49 -0700 | [diff] [blame] | 306 | ret = max98095_reset(priv); |
| 307 | if (ret != 0) { |
Rajeshwari Shinde | cd93e9b | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 308 | debug("Reset\n"); |
Simon Glass | ad2a5e3 | 2018-12-10 10:37:49 -0700 | [diff] [blame] | 309 | return ret; |
Rajeshwari Shinde | cd93e9b | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | /* initialize private data */ |
Simon Glass | fd7d697 | 2018-12-03 04:37:25 -0700 | [diff] [blame] | 313 | priv->sysclk = -1U; |
| 314 | priv->rate = -1U; |
| 315 | priv->fmt = -1U; |
Rajeshwari Shinde | cd93e9b | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 316 | |
Simon Glass | ad2a5e3 | 2018-12-10 10:37:49 -0700 | [diff] [blame] | 317 | ret = maxim_i2c_read(priv, M98095_0FF_REV_ID, &id); |
| 318 | if (ret < 0) { |
Rajeshwari Shinde | cd93e9b | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 319 | debug("%s: Failure reading hardware revision: %d\n", |
Dani Krishna Mohan | 6e19490 | 2013-09-11 16:38:50 +0530 | [diff] [blame] | 320 | __func__, id); |
Simon Glass | ad2a5e3 | 2018-12-10 10:37:49 -0700 | [diff] [blame] | 321 | return ret; |
Rajeshwari Shinde | cd93e9b | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 322 | } |
| 323 | debug("%s: Hardware revision: %c\n", __func__, (id - 0x40) + 'A'); |
| 324 | |
Simon Glass | e1458f6 | 2018-12-03 04:37:28 -0700 | [diff] [blame] | 325 | return 0; |
| 326 | } |
| 327 | |
Simon Glass | 504a790 | 2018-12-10 10:37:42 -0700 | [diff] [blame] | 328 | static int max98095_setup_interface(struct maxim_priv *priv, |
Simon Glass | e1458f6 | 2018-12-03 04:37:28 -0700 | [diff] [blame] | 329 | enum en_max_audio_interface aif_id) |
| 330 | { |
| 331 | int error; |
| 332 | |
Simon Glass | 504a790 | 2018-12-10 10:37:42 -0700 | [diff] [blame] | 333 | error = maxim_i2c_write(priv, M98095_097_PWR_SYS, M98095_PWRSV); |
Rajeshwari Shinde | cd93e9b | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 334 | |
| 335 | /* |
| 336 | * initialize registers to hardware default configuring audio |
| 337 | * interface2 to DAC |
| 338 | */ |
Dani Krishna Mohan | 6e19490 | 2013-09-11 16:38:50 +0530 | [diff] [blame] | 339 | if (aif_id == AIF1) |
Simon Glass | 504a790 | 2018-12-10 10:37:42 -0700 | [diff] [blame] | 340 | error |= maxim_i2c_write(priv, M98095_048_MIX_DAC_LR, |
Dani Krishna Mohan | 6e19490 | 2013-09-11 16:38:50 +0530 | [diff] [blame] | 341 | M98095_DAI1L_TO_DACL | |
| 342 | M98095_DAI1R_TO_DACR); |
| 343 | else |
Simon Glass | 504a790 | 2018-12-10 10:37:42 -0700 | [diff] [blame] | 344 | error |= maxim_i2c_write(priv, M98095_048_MIX_DAC_LR, |
Dani Krishna Mohan | 6e19490 | 2013-09-11 16:38:50 +0530 | [diff] [blame] | 345 | M98095_DAI2M_TO_DACL | |
| 346 | M98095_DAI2M_TO_DACR); |
Rajeshwari Shinde | cd93e9b | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 347 | |
Simon Glass | 504a790 | 2018-12-10 10:37:42 -0700 | [diff] [blame] | 348 | error |= maxim_i2c_write(priv, M98095_092_PWR_EN_OUT, |
Dani Krishna Mohan | 6e19490 | 2013-09-11 16:38:50 +0530 | [diff] [blame] | 349 | M98095_SPK_SPREADSPECTRUM); |
Simon Glass | 504a790 | 2018-12-10 10:37:42 -0700 | [diff] [blame] | 350 | error |= maxim_i2c_write(priv, M98095_04E_CFG_HP, M98095_HPNORMAL); |
Dani Krishna Mohan | 6e19490 | 2013-09-11 16:38:50 +0530 | [diff] [blame] | 351 | if (aif_id == AIF1) |
Simon Glass | 504a790 | 2018-12-10 10:37:42 -0700 | [diff] [blame] | 352 | error |= maxim_i2c_write(priv, M98095_02C_DAI1_IOCFG, |
Dani Krishna Mohan | 6e19490 | 2013-09-11 16:38:50 +0530 | [diff] [blame] | 353 | M98095_S1NORMAL | M98095_SDATA); |
| 354 | else |
Simon Glass | 504a790 | 2018-12-10 10:37:42 -0700 | [diff] [blame] | 355 | error |= maxim_i2c_write(priv, M98095_036_DAI2_IOCFG, |
Dani Krishna Mohan | 6e19490 | 2013-09-11 16:38:50 +0530 | [diff] [blame] | 356 | M98095_S2NORMAL | M98095_SDATA); |
Rajeshwari Shinde | cd93e9b | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 357 | |
| 358 | /* take the codec out of the shut down */ |
Simon Glass | 504a790 | 2018-12-10 10:37:42 -0700 | [diff] [blame] | 359 | error |= maxim_bic_or(priv, M98095_097_PWR_SYS, M98095_SHDNRUN, |
Simon Glass | fd7d697 | 2018-12-03 04:37:25 -0700 | [diff] [blame] | 360 | M98095_SHDNRUN); |
| 361 | /* |
| 362 | * route DACL and DACR output to HO and Speakers |
| 363 | * Ordering: DACL, DACR, DACL, DACR |
| 364 | */ |
Simon Glass | 504a790 | 2018-12-10 10:37:42 -0700 | [diff] [blame] | 365 | error |= maxim_i2c_write(priv, M98095_050_MIX_SPK_LEFT, 0x01); |
| 366 | error |= maxim_i2c_write(priv, M98095_051_MIX_SPK_RIGHT, 0x01); |
| 367 | error |= maxim_i2c_write(priv, M98095_04C_MIX_HP_LEFT, 0x01); |
| 368 | error |= maxim_i2c_write(priv, M98095_04D_MIX_HP_RIGHT, 0x01); |
Rajeshwari Shinde | cd93e9b | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 369 | |
| 370 | /* power Enable */ |
Simon Glass | 504a790 | 2018-12-10 10:37:42 -0700 | [diff] [blame] | 371 | error |= maxim_i2c_write(priv, M98095_091_PWR_EN_OUT, 0xF3); |
Rajeshwari Shinde | cd93e9b | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 372 | |
| 373 | /* set Volume */ |
Simon Glass | 504a790 | 2018-12-10 10:37:42 -0700 | [diff] [blame] | 374 | error |= maxim_i2c_write(priv, M98095_064_LVL_HP_L, 15); |
| 375 | error |= maxim_i2c_write(priv, M98095_065_LVL_HP_R, 15); |
| 376 | error |= maxim_i2c_write(priv, M98095_067_LVL_SPK_L, 16); |
| 377 | error |= maxim_i2c_write(priv, M98095_068_LVL_SPK_R, 16); |
Rajeshwari Shinde | cd93e9b | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 378 | |
| 379 | /* Enable DAIs */ |
Simon Glass | 504a790 | 2018-12-10 10:37:42 -0700 | [diff] [blame] | 380 | error |= maxim_i2c_write(priv, M98095_093_BIAS_CTRL, 0x30); |
Dani Krishna Mohan | 6e19490 | 2013-09-11 16:38:50 +0530 | [diff] [blame] | 381 | if (aif_id == AIF1) |
Simon Glass | 504a790 | 2018-12-10 10:37:42 -0700 | [diff] [blame] | 382 | error |= maxim_i2c_write(priv, M98095_096_PWR_DAC_CK, 0x01); |
Dani Krishna Mohan | 6e19490 | 2013-09-11 16:38:50 +0530 | [diff] [blame] | 383 | else |
Simon Glass | 504a790 | 2018-12-10 10:37:42 -0700 | [diff] [blame] | 384 | error |= maxim_i2c_write(priv, M98095_096_PWR_DAC_CK, 0x07); |
Rajeshwari Shinde | cd93e9b | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 385 | |
Rajeshwari Shinde | cd93e9b | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 386 | if (error < 0) |
Simon Glass | ad2a5e3 | 2018-12-10 10:37:49 -0700 | [diff] [blame] | 387 | return -EIO; |
Rajeshwari Shinde | cd93e9b | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 388 | |
| 389 | return 0; |
| 390 | } |
| 391 | |
Simon Glass | 504a790 | 2018-12-10 10:37:42 -0700 | [diff] [blame] | 392 | static int max98095_do_init(struct maxim_priv *priv, |
Dani Krishna Mohan | 6e19490 | 2013-09-11 16:38:50 +0530 | [diff] [blame] | 393 | enum en_max_audio_interface aif_id, |
| 394 | int sampling_rate, int mclk_freq, |
| 395 | int bits_per_sample) |
Rajeshwari Shinde | cd93e9b | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 396 | { |
| 397 | int ret = 0; |
| 398 | |
Simon Glass | 860b11c | 2018-12-03 04:37:32 -0700 | [diff] [blame] | 399 | ret = max98095_setup_interface(priv, aif_id); |
Rajeshwari Shinde | cd93e9b | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 400 | if (ret < 0) { |
Simon Glass | 4070ba6 | 2018-12-10 10:37:39 -0700 | [diff] [blame] | 401 | debug("%s: max98095 setup interface failed\n", __func__); |
Rajeshwari Shinde | cd93e9b | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 402 | return ret; |
| 403 | } |
| 404 | |
Simon Glass | 860b11c | 2018-12-03 04:37:32 -0700 | [diff] [blame] | 405 | ret = max98095_set_sysclk(priv, mclk_freq); |
Rajeshwari Shinde | cd93e9b | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 406 | if (ret < 0) { |
| 407 | debug("%s: max98095 codec set sys clock failed\n", __func__); |
| 408 | return ret; |
| 409 | } |
| 410 | |
Simon Glass | 860b11c | 2018-12-03 04:37:32 -0700 | [diff] [blame] | 411 | ret = max98095_hw_params(priv, aif_id, sampling_rate, |
Dani Krishna Mohan | 6e19490 | 2013-09-11 16:38:50 +0530 | [diff] [blame] | 412 | bits_per_sample); |
Rajeshwari Shinde | cd93e9b | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 413 | |
| 414 | if (ret == 0) { |
Simon Glass | 860b11c | 2018-12-03 04:37:32 -0700 | [diff] [blame] | 415 | ret = max98095_set_fmt(priv, SND_SOC_DAIFMT_I2S | |
Dani Krishna Mohan | 6e19490 | 2013-09-11 16:38:50 +0530 | [diff] [blame] | 416 | SND_SOC_DAIFMT_NB_NF | |
| 417 | SND_SOC_DAIFMT_CBS_CFS, |
| 418 | aif_id); |
Rajeshwari Shinde | cd93e9b | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | return ret; |
| 422 | } |
| 423 | |
Simon Glass | 4070ba6 | 2018-12-10 10:37:39 -0700 | [diff] [blame] | 424 | static int max98095_set_params(struct udevice *dev, int interface, int rate, |
| 425 | int mclk_freq, int bits_per_sample, |
| 426 | uint channels) |
| 427 | { |
Simon Glass | 504a790 | 2018-12-10 10:37:42 -0700 | [diff] [blame] | 428 | struct maxim_priv *priv = dev_get_priv(dev); |
Simon Glass | 4070ba6 | 2018-12-10 10:37:39 -0700 | [diff] [blame] | 429 | |
| 430 | return max98095_do_init(priv, interface, rate, mclk_freq, |
| 431 | bits_per_sample); |
| 432 | } |
| 433 | |
| 434 | static int max98095_probe(struct udevice *dev) |
| 435 | { |
Simon Glass | 504a790 | 2018-12-10 10:37:42 -0700 | [diff] [blame] | 436 | struct maxim_priv *priv = dev_get_priv(dev); |
Simon Glass | 4070ba6 | 2018-12-10 10:37:39 -0700 | [diff] [blame] | 437 | int ret; |
| 438 | |
| 439 | priv->dev = dev; |
| 440 | ret = max98095_device_init(priv); |
| 441 | if (ret < 0) { |
| 442 | debug("%s: max98095 codec chip init failed\n", __func__); |
| 443 | return ret; |
| 444 | } |
| 445 | |
| 446 | return 0; |
| 447 | } |
| 448 | |
| 449 | static const struct audio_codec_ops max98095_ops = { |
| 450 | .set_params = max98095_set_params, |
| 451 | }; |
| 452 | |
| 453 | static const struct udevice_id max98095_ids[] = { |
| 454 | { .compatible = "maxim,max98095" }, |
| 455 | { } |
| 456 | }; |
| 457 | |
| 458 | U_BOOT_DRIVER(max98095) = { |
| 459 | .name = "max98095", |
| 460 | .id = UCLASS_AUDIO_CODEC, |
| 461 | .of_match = max98095_ids, |
| 462 | .probe = max98095_probe, |
| 463 | .ops = &max98095_ops, |
Simon Glass | 504a790 | 2018-12-10 10:37:42 -0700 | [diff] [blame] | 464 | .priv_auto_alloc_size = sizeof(struct maxim_priv), |
Simon Glass | 4070ba6 | 2018-12-10 10:37:39 -0700 | [diff] [blame] | 465 | }; |