Rajeshwari Shinde | 5febe8d | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 1 | /* |
| 2 | * max98095.c -- MAX98095 ALSA SoC Audio driver |
| 3 | * |
| 4 | * Copyright 2011 Maxim Integrated Products |
| 5 | * |
| 6 | * Modified for uboot by R. Chandrasekar (rcsekar@samsung.com) |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License version 2 as |
| 10 | * published by the Free Software Foundation. |
| 11 | */ |
Simon Glass | 150c5af | 2017-05-30 21:47:09 -0600 | [diff] [blame] | 12 | |
| 13 | #include <common.h> |
Rajeshwari Shinde | 5febe8d | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 14 | #include <asm/arch/clk.h> |
| 15 | #include <asm/arch/cpu.h> |
| 16 | #include <asm/arch/power.h> |
| 17 | #include <asm/gpio.h> |
| 18 | #include <asm/io.h> |
| 19 | #include <common.h> |
| 20 | #include <div64.h> |
| 21 | #include <fdtdec.h> |
| 22 | #include <i2c.h> |
| 23 | #include <sound.h> |
| 24 | #include "i2s.h" |
| 25 | #include "max98095.h" |
| 26 | |
Rajeshwari Shinde | 5febe8d | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 27 | struct max98095_priv { |
Rajeshwari Shinde | 5febe8d | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 28 | unsigned int sysclk; |
| 29 | unsigned int rate; |
| 30 | unsigned int fmt; |
Simon Glass | 372922c | 2018-12-03 04:37:30 -0700 | [diff] [blame] | 31 | int i2c_addr; |
Rajeshwari Shinde | 5febe8d | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 32 | }; |
| 33 | |
| 34 | static struct sound_codec_info g_codec_info; |
| 35 | struct max98095_priv g_max98095_info; |
Rajeshwari Shinde | 5febe8d | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 36 | |
| 37 | /* Index 0 is reserved. */ |
| 38 | int rate_table[] = {0, 8000, 11025, 16000, 22050, 24000, 32000, 44100, 48000, |
| 39 | 88200, 96000}; |
| 40 | |
| 41 | /* |
| 42 | * Writes value to a device register through i2c |
| 43 | * |
Simon Glass | a832a3e | 2018-12-03 04:37:25 -0700 | [diff] [blame] | 44 | * @param priv Private data for driver |
Rajeshwari Shinde | 5febe8d | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 45 | * @param reg reg number to be write |
| 46 | * @param data data to be writen to the above registor |
| 47 | * |
| 48 | * @return int value 1 for change, 0 for no change or negative error code. |
| 49 | */ |
Simon Glass | a832a3e | 2018-12-03 04:37:25 -0700 | [diff] [blame] | 50 | static int max98095_i2c_write(struct max98095_priv *priv, unsigned int reg, |
| 51 | unsigned char data) |
Rajeshwari Shinde | 5febe8d | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 52 | { |
| 53 | debug("%s: Write Addr : 0x%02X, Data : 0x%02X\n", |
Dani Krishna Mohan | 6b40852 | 2013-09-11 16:38:50 +0530 | [diff] [blame] | 54 | __func__, reg, data); |
Simon Glass | 372922c | 2018-12-03 04:37:30 -0700 | [diff] [blame] | 55 | return i2c_write(priv->i2c_addr, reg, 1, &data, 1); |
Rajeshwari Shinde | 5febe8d | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | /* |
| 59 | * Read a value from a device register through i2c |
| 60 | * |
Simon Glass | a832a3e | 2018-12-03 04:37:25 -0700 | [diff] [blame] | 61 | * @param priv Private data for driver |
Rajeshwari Shinde | 5febe8d | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 62 | * @param reg reg number to be read |
| 63 | * @param data address of read data to be stored |
| 64 | * |
| 65 | * @return int value 0 for success, -1 in case of error. |
| 66 | */ |
Simon Glass | a832a3e | 2018-12-03 04:37:25 -0700 | [diff] [blame] | 67 | static unsigned int max98095_i2c_read(struct max98095_priv *priv, |
| 68 | unsigned int reg, unsigned char *data) |
Rajeshwari Shinde | 5febe8d | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 69 | { |
| 70 | int ret; |
| 71 | |
Simon Glass | 372922c | 2018-12-03 04:37:30 -0700 | [diff] [blame] | 72 | ret = i2c_read(priv->i2c_addr, reg, 1, data, 1); |
Rajeshwari Shinde | 5febe8d | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 73 | if (ret != 0) { |
| 74 | debug("%s: Error while reading register %#04x\n", |
Dani Krishna Mohan | 6b40852 | 2013-09-11 16:38:50 +0530 | [diff] [blame] | 75 | __func__, reg); |
Rajeshwari Shinde | 5febe8d | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 76 | return -1; |
| 77 | } |
| 78 | |
| 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | /* |
| 83 | * update device register bits through i2c |
| 84 | * |
Simon Glass | a832a3e | 2018-12-03 04:37:25 -0700 | [diff] [blame] | 85 | * @param priv Private data for driver |
Rajeshwari Shinde | 5febe8d | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 86 | * @param reg codec register |
| 87 | * @param mask register mask |
| 88 | * @param value new value |
| 89 | * |
| 90 | * @return int value 0 for success, non-zero error code. |
| 91 | */ |
Simon Glass | a832a3e | 2018-12-03 04:37:25 -0700 | [diff] [blame] | 92 | static int max98095_bic_or(struct max98095_priv *priv, unsigned int reg, |
| 93 | unsigned char mask, unsigned char value) |
Rajeshwari Shinde | 5febe8d | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 94 | { |
| 95 | int change, ret = 0; |
| 96 | unsigned char old, new; |
| 97 | |
Simon Glass | a832a3e | 2018-12-03 04:37:25 -0700 | [diff] [blame] | 98 | if (max98095_i2c_read(priv, reg, &old) != 0) |
Rajeshwari Shinde | 5febe8d | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 99 | return -1; |
| 100 | new = (old & ~mask) | (value & mask); |
| 101 | change = (old != new) ? 1 : 0; |
| 102 | if (change) |
Simon Glass | a832a3e | 2018-12-03 04:37:25 -0700 | [diff] [blame] | 103 | ret = max98095_i2c_write(priv, reg, new); |
Rajeshwari Shinde | 5febe8d | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 104 | if (ret < 0) |
| 105 | return ret; |
| 106 | |
| 107 | return change; |
| 108 | } |
| 109 | |
| 110 | /* |
| 111 | * codec mclk clock divider coefficients based on sampling rate |
| 112 | * |
| 113 | * @param rate sampling rate |
| 114 | * @param value address of indexvalue to be stored |
| 115 | * |
| 116 | * @return 0 for success or negative error code. |
| 117 | */ |
| 118 | static int rate_value(int rate, u8 *value) |
| 119 | { |
| 120 | int i; |
| 121 | |
| 122 | for (i = 1; i < ARRAY_SIZE(rate_table); i++) { |
| 123 | if (rate_table[i] >= rate) { |
| 124 | *value = i; |
| 125 | return 0; |
| 126 | } |
| 127 | } |
| 128 | *value = 1; |
| 129 | |
| 130 | return -1; |
| 131 | } |
| 132 | |
| 133 | /* |
| 134 | * Sets hw params for max98095 |
| 135 | * |
Simon Glass | a832a3e | 2018-12-03 04:37:25 -0700 | [diff] [blame] | 136 | * @param priv max98095 information pointer |
Rajeshwari Shinde | 5febe8d | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 137 | * @param rate Sampling rate |
| 138 | * @param bits_per_sample Bits per sample |
| 139 | * |
| 140 | * @return -1 for error and 0 Success. |
| 141 | */ |
Simon Glass | a832a3e | 2018-12-03 04:37:25 -0700 | [diff] [blame] | 142 | static int max98095_hw_params(struct max98095_priv *priv, |
Dani Krishna Mohan | 6b40852 | 2013-09-11 16:38:50 +0530 | [diff] [blame] | 143 | enum en_max_audio_interface aif_id, |
| 144 | unsigned int rate, unsigned int bits_per_sample) |
Rajeshwari Shinde | 5febe8d | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 145 | { |
| 146 | u8 regval; |
| 147 | int error; |
Dani Krishna Mohan | 6b40852 | 2013-09-11 16:38:50 +0530 | [diff] [blame] | 148 | unsigned short M98095_DAI_CLKMODE; |
| 149 | unsigned short M98095_DAI_FORMAT; |
| 150 | unsigned short M98095_DAI_FILTERS; |
| 151 | |
| 152 | if (aif_id == AIF1) { |
| 153 | M98095_DAI_CLKMODE = M98095_027_DAI1_CLKMODE; |
| 154 | M98095_DAI_FORMAT = M98095_02A_DAI1_FORMAT; |
| 155 | M98095_DAI_FILTERS = M98095_02E_DAI1_FILTERS; |
| 156 | } else { |
| 157 | M98095_DAI_CLKMODE = M98095_031_DAI2_CLKMODE; |
| 158 | M98095_DAI_FORMAT = M98095_034_DAI2_FORMAT; |
| 159 | M98095_DAI_FILTERS = M98095_038_DAI2_FILTERS; |
| 160 | } |
Rajeshwari Shinde | 5febe8d | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 161 | |
| 162 | switch (bits_per_sample) { |
| 163 | case 16: |
Simon Glass | a832a3e | 2018-12-03 04:37:25 -0700 | [diff] [blame] | 164 | error = max98095_bic_or(priv, M98095_DAI_FORMAT, M98095_DAI_WS, |
| 165 | 0); |
Rajeshwari Shinde | 5febe8d | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 166 | break; |
| 167 | case 24: |
Simon Glass | a832a3e | 2018-12-03 04:37:25 -0700 | [diff] [blame] | 168 | error = max98095_bic_or(priv, M98095_DAI_FORMAT, M98095_DAI_WS, |
| 169 | M98095_DAI_WS); |
Rajeshwari Shinde | 5febe8d | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 170 | break; |
| 171 | default: |
| 172 | debug("%s: Illegal bits per sample %d.\n", |
Dani Krishna Mohan | 6b40852 | 2013-09-11 16:38:50 +0530 | [diff] [blame] | 173 | __func__, bits_per_sample); |
Rajeshwari Shinde | 5febe8d | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 174 | return -1; |
| 175 | } |
| 176 | |
| 177 | if (rate_value(rate, ®val)) { |
| 178 | debug("%s: Failed to set sample rate to %d.\n", |
Dani Krishna Mohan | 6b40852 | 2013-09-11 16:38:50 +0530 | [diff] [blame] | 179 | __func__, rate); |
Rajeshwari Shinde | 5febe8d | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 180 | return -1; |
| 181 | } |
Simon Glass | a832a3e | 2018-12-03 04:37:25 -0700 | [diff] [blame] | 182 | priv->rate = rate; |
Rajeshwari Shinde | 5febe8d | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 183 | |
Simon Glass | a832a3e | 2018-12-03 04:37:25 -0700 | [diff] [blame] | 184 | error |= max98095_bic_or(priv, M98095_DAI_CLKMODE, M98095_CLKMODE_MASK, |
| 185 | regval); |
Rajeshwari Shinde | 5febe8d | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 186 | |
| 187 | /* Update sample rate mode */ |
| 188 | if (rate < 50000) |
Simon Glass | a832a3e | 2018-12-03 04:37:25 -0700 | [diff] [blame] | 189 | error |= max98095_bic_or(priv, M98095_DAI_FILTERS, |
| 190 | M98095_DAI_DHF, 0); |
Rajeshwari Shinde | 5febe8d | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 191 | else |
Simon Glass | a832a3e | 2018-12-03 04:37:25 -0700 | [diff] [blame] | 192 | error |= max98095_bic_or(priv, M98095_DAI_FILTERS, |
| 193 | M98095_DAI_DHF, M98095_DAI_DHF); |
Rajeshwari Shinde | 5febe8d | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 194 | |
| 195 | if (error < 0) { |
| 196 | debug("%s: Error setting hardware params.\n", __func__); |
| 197 | return -1; |
| 198 | } |
| 199 | |
| 200 | return 0; |
| 201 | } |
| 202 | |
| 203 | /* |
| 204 | * Configures Audio interface system clock for the given frequency |
| 205 | * |
Simon Glass | a832a3e | 2018-12-03 04:37:25 -0700 | [diff] [blame] | 206 | * @param priv max98095 information |
Rajeshwari Shinde | 5febe8d | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 207 | * @param freq Sampling frequency in Hz |
| 208 | * |
| 209 | * @return -1 for error and 0 success. |
| 210 | */ |
Simon Glass | a832a3e | 2018-12-03 04:37:25 -0700 | [diff] [blame] | 211 | static int max98095_set_sysclk(struct max98095_priv *priv, unsigned int freq) |
Rajeshwari Shinde | 5febe8d | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 212 | { |
| 213 | int error = 0; |
| 214 | |
| 215 | /* Requested clock frequency is already setup */ |
Simon Glass | a832a3e | 2018-12-03 04:37:25 -0700 | [diff] [blame] | 216 | if (freq == priv->sysclk) |
Rajeshwari Shinde | 5febe8d | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 217 | return 0; |
| 218 | |
| 219 | /* Setup clocks for slave mode, and using the PLL |
| 220 | * PSCLK = 0x01 (when master clk is 10MHz to 20MHz) |
| 221 | * 0x02 (when master clk is 20MHz to 40MHz).. |
| 222 | * 0x03 (when master clk is 40MHz to 60MHz).. |
| 223 | */ |
| 224 | if ((freq >= 10000000) && (freq < 20000000)) { |
Simon Glass | a832a3e | 2018-12-03 04:37:25 -0700 | [diff] [blame] | 225 | error = max98095_i2c_write(priv, M98095_026_SYS_CLK, 0x10); |
Rajeshwari Shinde | 5febe8d | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 226 | } else if ((freq >= 20000000) && (freq < 40000000)) { |
Simon Glass | a832a3e | 2018-12-03 04:37:25 -0700 | [diff] [blame] | 227 | error = max98095_i2c_write(priv, M98095_026_SYS_CLK, 0x20); |
Rajeshwari Shinde | 5febe8d | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 228 | } else if ((freq >= 40000000) && (freq < 60000000)) { |
Simon Glass | a832a3e | 2018-12-03 04:37:25 -0700 | [diff] [blame] | 229 | error = max98095_i2c_write(priv, M98095_026_SYS_CLK, 0x30); |
Rajeshwari Shinde | 5febe8d | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 230 | } else { |
| 231 | debug("%s: Invalid master clock frequency\n", __func__); |
| 232 | return -1; |
| 233 | } |
| 234 | |
| 235 | debug("%s: Clock at %uHz\n", __func__, freq); |
| 236 | |
| 237 | if (error < 0) |
| 238 | return -1; |
| 239 | |
Simon Glass | a832a3e | 2018-12-03 04:37:25 -0700 | [diff] [blame] | 240 | priv->sysclk = freq; |
Rajeshwari Shinde | 5febe8d | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 241 | return 0; |
| 242 | } |
| 243 | |
| 244 | /* |
| 245 | * Sets Max98095 I2S format |
| 246 | * |
Simon Glass | a832a3e | 2018-12-03 04:37:25 -0700 | [diff] [blame] | 247 | * @param priv max98095 information |
Rajeshwari Shinde | 5febe8d | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 248 | * @param fmt i2S format - supports a subset of the options defined |
| 249 | * in i2s.h. |
| 250 | * |
| 251 | * @return -1 for error and 0 Success. |
| 252 | */ |
Simon Glass | a832a3e | 2018-12-03 04:37:25 -0700 | [diff] [blame] | 253 | static int max98095_set_fmt(struct max98095_priv *priv, int fmt, |
Dani Krishna Mohan | 6b40852 | 2013-09-11 16:38:50 +0530 | [diff] [blame] | 254 | enum en_max_audio_interface aif_id) |
Rajeshwari Shinde | 5febe8d | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 255 | { |
| 256 | u8 regval = 0; |
| 257 | int error = 0; |
Dani Krishna Mohan | 6b40852 | 2013-09-11 16:38:50 +0530 | [diff] [blame] | 258 | unsigned short M98095_DAI_CLKCFG_HI; |
| 259 | unsigned short M98095_DAI_CLKCFG_LO; |
| 260 | unsigned short M98095_DAI_FORMAT; |
| 261 | unsigned short M98095_DAI_CLOCK; |
Rajeshwari Shinde | 5febe8d | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 262 | |
Simon Glass | a832a3e | 2018-12-03 04:37:25 -0700 | [diff] [blame] | 263 | if (fmt == priv->fmt) |
Rajeshwari Shinde | 5febe8d | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 264 | return 0; |
| 265 | |
Simon Glass | a832a3e | 2018-12-03 04:37:25 -0700 | [diff] [blame] | 266 | priv->fmt = fmt; |
Rajeshwari Shinde | 5febe8d | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 267 | |
Dani Krishna Mohan | 6b40852 | 2013-09-11 16:38:50 +0530 | [diff] [blame] | 268 | if (aif_id == AIF1) { |
| 269 | M98095_DAI_CLKCFG_HI = M98095_028_DAI1_CLKCFG_HI; |
| 270 | M98095_DAI_CLKCFG_LO = M98095_029_DAI1_CLKCFG_LO; |
| 271 | M98095_DAI_FORMAT = M98095_02A_DAI1_FORMAT; |
| 272 | M98095_DAI_CLOCK = M98095_02B_DAI1_CLOCK; |
| 273 | } else { |
| 274 | M98095_DAI_CLKCFG_HI = M98095_032_DAI2_CLKCFG_HI; |
| 275 | M98095_DAI_CLKCFG_LO = M98095_033_DAI2_CLKCFG_LO; |
| 276 | M98095_DAI_FORMAT = M98095_034_DAI2_FORMAT; |
| 277 | M98095_DAI_CLOCK = M98095_035_DAI2_CLOCK; |
| 278 | } |
| 279 | |
Rajeshwari Shinde | 5febe8d | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 280 | switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { |
| 281 | case SND_SOC_DAIFMT_CBS_CFS: |
| 282 | /* Slave mode PLL */ |
Simon Glass | a832a3e | 2018-12-03 04:37:25 -0700 | [diff] [blame] | 283 | error |= max98095_i2c_write(priv, M98095_DAI_CLKCFG_HI, 0x80); |
| 284 | error |= max98095_i2c_write(priv, M98095_DAI_CLKCFG_LO, 0x00); |
Rajeshwari Shinde | 5febe8d | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 285 | break; |
| 286 | case SND_SOC_DAIFMT_CBM_CFM: |
| 287 | /* Set to master mode */ |
| 288 | regval |= M98095_DAI_MAS; |
| 289 | break; |
| 290 | case SND_SOC_DAIFMT_CBS_CFM: |
| 291 | case SND_SOC_DAIFMT_CBM_CFS: |
| 292 | default: |
| 293 | debug("%s: Clock mode unsupported\n", __func__); |
| 294 | return -1; |
| 295 | } |
| 296 | |
| 297 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { |
| 298 | case SND_SOC_DAIFMT_I2S: |
| 299 | regval |= M98095_DAI_DLY; |
| 300 | break; |
| 301 | case SND_SOC_DAIFMT_LEFT_J: |
| 302 | break; |
| 303 | default: |
| 304 | debug("%s: Unrecognized format.\n", __func__); |
| 305 | return -1; |
| 306 | } |
| 307 | |
| 308 | switch (fmt & SND_SOC_DAIFMT_INV_MASK) { |
| 309 | case SND_SOC_DAIFMT_NB_NF: |
| 310 | break; |
| 311 | case SND_SOC_DAIFMT_NB_IF: |
| 312 | regval |= M98095_DAI_WCI; |
| 313 | break; |
| 314 | case SND_SOC_DAIFMT_IB_NF: |
| 315 | regval |= M98095_DAI_BCI; |
| 316 | break; |
| 317 | case SND_SOC_DAIFMT_IB_IF: |
| 318 | regval |= M98095_DAI_BCI | M98095_DAI_WCI; |
| 319 | break; |
| 320 | default: |
| 321 | debug("%s: Unrecognized inversion settings.\n", __func__); |
| 322 | return -1; |
| 323 | } |
| 324 | |
Simon Glass | a832a3e | 2018-12-03 04:37:25 -0700 | [diff] [blame] | 325 | error |= max98095_bic_or(priv, M98095_DAI_FORMAT, |
| 326 | M98095_DAI_MAS | M98095_DAI_DLY | |
| 327 | M98095_DAI_BCI | M98095_DAI_WCI, regval); |
Rajeshwari Shinde | 5febe8d | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 328 | |
Simon Glass | a832a3e | 2018-12-03 04:37:25 -0700 | [diff] [blame] | 329 | error |= max98095_i2c_write(priv, M98095_DAI_CLOCK, M98095_DAI_BSEL64); |
Rajeshwari Shinde | 5febe8d | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 330 | |
| 331 | if (error < 0) { |
| 332 | debug("%s: Error setting i2s format.\n", __func__); |
| 333 | return -1; |
| 334 | } |
| 335 | |
| 336 | return 0; |
| 337 | } |
| 338 | |
| 339 | /* |
| 340 | * resets the audio codec |
| 341 | * |
Simon Glass | a832a3e | 2018-12-03 04:37:25 -0700 | [diff] [blame] | 342 | * @param priv Private data for driver |
Rajeshwari Shinde | 5febe8d | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 343 | * @return -1 for error and 0 success. |
| 344 | */ |
Simon Glass | a832a3e | 2018-12-03 04:37:25 -0700 | [diff] [blame] | 345 | static int max98095_reset(struct max98095_priv *priv) |
Rajeshwari Shinde | 5febe8d | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 346 | { |
| 347 | int i, ret; |
| 348 | |
| 349 | /* |
| 350 | * Gracefully reset the DSP core and the codec hardware in a proper |
| 351 | * sequence. |
| 352 | */ |
Simon Glass | a832a3e | 2018-12-03 04:37:25 -0700 | [diff] [blame] | 353 | ret = max98095_i2c_write(priv, M98095_00F_HOST_CFG, 0); |
Rajeshwari Shinde | 5febe8d | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 354 | if (ret != 0) { |
| 355 | debug("%s: Failed to reset DSP: %d\n", __func__, ret); |
| 356 | return ret; |
| 357 | } |
| 358 | |
Simon Glass | a832a3e | 2018-12-03 04:37:25 -0700 | [diff] [blame] | 359 | ret = max98095_i2c_write(priv, M98095_097_PWR_SYS, 0); |
Rajeshwari Shinde | 5febe8d | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 360 | if (ret != 0) { |
| 361 | debug("%s: Failed to reset codec: %d\n", __func__, ret); |
| 362 | return ret; |
| 363 | } |
| 364 | |
| 365 | /* |
| 366 | * Reset to hardware default for registers, as there is not a soft |
| 367 | * reset hardware control register. |
| 368 | */ |
| 369 | for (i = M98095_010_HOST_INT_CFG; i < M98095_REG_MAX_CACHED; i++) { |
Simon Glass | a832a3e | 2018-12-03 04:37:25 -0700 | [diff] [blame] | 370 | ret = max98095_i2c_write(priv, i, 0); |
Rajeshwari Shinde | 5febe8d | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 371 | if (ret < 0) { |
| 372 | debug("%s: Failed to reset: %d\n", __func__, ret); |
| 373 | return ret; |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | return 0; |
| 378 | } |
| 379 | |
| 380 | /* |
| 381 | * Intialise max98095 codec device |
| 382 | * |
Simon Glass | a832a3e | 2018-12-03 04:37:25 -0700 | [diff] [blame] | 383 | * @param priv max98095 information |
Rajeshwari Shinde | 5febe8d | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 384 | * |
| 385 | * @returns -1 for error and 0 Success. |
| 386 | */ |
Simon Glass | 82a27d2 | 2018-12-03 04:37:28 -0700 | [diff] [blame] | 387 | static int max98095_device_init(struct max98095_priv *priv) |
Rajeshwari Shinde | 5febe8d | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 388 | { |
| 389 | unsigned char id; |
| 390 | int error = 0; |
| 391 | |
Simon Glass | 82a27d2 | 2018-12-03 04:37:28 -0700 | [diff] [blame] | 392 | /* Enable codec clock */ |
| 393 | set_xclkout(); |
| 394 | |
Rajeshwari Shinde | 5febe8d | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 395 | /* reset the codec, the DSP core, and disable all interrupts */ |
Simon Glass | a832a3e | 2018-12-03 04:37:25 -0700 | [diff] [blame] | 396 | error = max98095_reset(priv); |
Rajeshwari Shinde | 5febe8d | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 397 | if (error != 0) { |
| 398 | debug("Reset\n"); |
| 399 | return error; |
| 400 | } |
| 401 | |
| 402 | /* initialize private data */ |
Simon Glass | a832a3e | 2018-12-03 04:37:25 -0700 | [diff] [blame] | 403 | priv->sysclk = -1U; |
| 404 | priv->rate = -1U; |
| 405 | priv->fmt = -1U; |
Rajeshwari Shinde | 5febe8d | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 406 | |
Simon Glass | a832a3e | 2018-12-03 04:37:25 -0700 | [diff] [blame] | 407 | error = max98095_i2c_read(priv, M98095_0FF_REV_ID, &id); |
Rajeshwari Shinde | 5febe8d | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 408 | if (error < 0) { |
| 409 | debug("%s: Failure reading hardware revision: %d\n", |
Dani Krishna Mohan | 6b40852 | 2013-09-11 16:38:50 +0530 | [diff] [blame] | 410 | __func__, id); |
Simon Glass | 82a27d2 | 2018-12-03 04:37:28 -0700 | [diff] [blame] | 411 | return error; |
Rajeshwari Shinde | 5febe8d | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 412 | } |
| 413 | debug("%s: Hardware revision: %c\n", __func__, (id - 0x40) + 'A'); |
| 414 | |
Simon Glass | 82a27d2 | 2018-12-03 04:37:28 -0700 | [diff] [blame] | 415 | return 0; |
| 416 | } |
| 417 | |
| 418 | static int max98095_setup_interface(struct max98095_priv *priv, |
| 419 | enum en_max_audio_interface aif_id) |
| 420 | { |
| 421 | int error; |
| 422 | |
| 423 | error = max98095_i2c_write(priv, M98095_097_PWR_SYS, M98095_PWRSV); |
Rajeshwari Shinde | 5febe8d | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 424 | |
| 425 | /* |
| 426 | * initialize registers to hardware default configuring audio |
| 427 | * interface2 to DAC |
| 428 | */ |
Dani Krishna Mohan | 6b40852 | 2013-09-11 16:38:50 +0530 | [diff] [blame] | 429 | if (aif_id == AIF1) |
Simon Glass | a832a3e | 2018-12-03 04:37:25 -0700 | [diff] [blame] | 430 | error |= max98095_i2c_write(priv, M98095_048_MIX_DAC_LR, |
Dani Krishna Mohan | 6b40852 | 2013-09-11 16:38:50 +0530 | [diff] [blame] | 431 | M98095_DAI1L_TO_DACL | |
| 432 | M98095_DAI1R_TO_DACR); |
| 433 | else |
Simon Glass | a832a3e | 2018-12-03 04:37:25 -0700 | [diff] [blame] | 434 | error |= max98095_i2c_write(priv, M98095_048_MIX_DAC_LR, |
Dani Krishna Mohan | 6b40852 | 2013-09-11 16:38:50 +0530 | [diff] [blame] | 435 | M98095_DAI2M_TO_DACL | |
| 436 | M98095_DAI2M_TO_DACR); |
Rajeshwari Shinde | 5febe8d | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 437 | |
Simon Glass | a832a3e | 2018-12-03 04:37:25 -0700 | [diff] [blame] | 438 | error |= max98095_i2c_write(priv, M98095_092_PWR_EN_OUT, |
Dani Krishna Mohan | 6b40852 | 2013-09-11 16:38:50 +0530 | [diff] [blame] | 439 | M98095_SPK_SPREADSPECTRUM); |
Simon Glass | a832a3e | 2018-12-03 04:37:25 -0700 | [diff] [blame] | 440 | error |= max98095_i2c_write(priv, M98095_04E_CFG_HP, M98095_HPNORMAL); |
Dani Krishna Mohan | 6b40852 | 2013-09-11 16:38:50 +0530 | [diff] [blame] | 441 | if (aif_id == AIF1) |
Simon Glass | a832a3e | 2018-12-03 04:37:25 -0700 | [diff] [blame] | 442 | error |= max98095_i2c_write(priv, M98095_02C_DAI1_IOCFG, |
Dani Krishna Mohan | 6b40852 | 2013-09-11 16:38:50 +0530 | [diff] [blame] | 443 | M98095_S1NORMAL | M98095_SDATA); |
| 444 | else |
Simon Glass | a832a3e | 2018-12-03 04:37:25 -0700 | [diff] [blame] | 445 | error |= max98095_i2c_write(priv, M98095_036_DAI2_IOCFG, |
Dani Krishna Mohan | 6b40852 | 2013-09-11 16:38:50 +0530 | [diff] [blame] | 446 | M98095_S2NORMAL | M98095_SDATA); |
Rajeshwari Shinde | 5febe8d | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 447 | |
| 448 | /* take the codec out of the shut down */ |
Simon Glass | a832a3e | 2018-12-03 04:37:25 -0700 | [diff] [blame] | 449 | error |= max98095_bic_or(priv, M98095_097_PWR_SYS, M98095_SHDNRUN, |
| 450 | M98095_SHDNRUN); |
| 451 | /* |
| 452 | * route DACL and DACR output to HO and Speakers |
| 453 | * Ordering: DACL, DACR, DACL, DACR |
| 454 | */ |
| 455 | error |= max98095_i2c_write(priv, M98095_050_MIX_SPK_LEFT, 0x01); |
| 456 | error |= max98095_i2c_write(priv, M98095_051_MIX_SPK_RIGHT, 0x01); |
| 457 | error |= max98095_i2c_write(priv, M98095_04C_MIX_HP_LEFT, 0x01); |
| 458 | error |= max98095_i2c_write(priv, M98095_04D_MIX_HP_RIGHT, 0x01); |
Rajeshwari Shinde | 5febe8d | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 459 | |
| 460 | /* power Enable */ |
Simon Glass | a832a3e | 2018-12-03 04:37:25 -0700 | [diff] [blame] | 461 | error |= max98095_i2c_write(priv, M98095_091_PWR_EN_OUT, 0xF3); |
Rajeshwari Shinde | 5febe8d | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 462 | |
| 463 | /* set Volume */ |
Simon Glass | a832a3e | 2018-12-03 04:37:25 -0700 | [diff] [blame] | 464 | error |= max98095_i2c_write(priv, M98095_064_LVL_HP_L, 15); |
| 465 | error |= max98095_i2c_write(priv, M98095_065_LVL_HP_R, 15); |
| 466 | error |= max98095_i2c_write(priv, M98095_067_LVL_SPK_L, 16); |
| 467 | error |= max98095_i2c_write(priv, M98095_068_LVL_SPK_R, 16); |
Rajeshwari Shinde | 5febe8d | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 468 | |
| 469 | /* Enable DAIs */ |
Simon Glass | a832a3e | 2018-12-03 04:37:25 -0700 | [diff] [blame] | 470 | error |= max98095_i2c_write(priv, M98095_093_BIAS_CTRL, 0x30); |
Dani Krishna Mohan | 6b40852 | 2013-09-11 16:38:50 +0530 | [diff] [blame] | 471 | if (aif_id == AIF1) |
Simon Glass | a832a3e | 2018-12-03 04:37:25 -0700 | [diff] [blame] | 472 | error |= max98095_i2c_write(priv, M98095_096_PWR_DAC_CK, 0x01); |
Dani Krishna Mohan | 6b40852 | 2013-09-11 16:38:50 +0530 | [diff] [blame] | 473 | else |
Simon Glass | a832a3e | 2018-12-03 04:37:25 -0700 | [diff] [blame] | 474 | error |= max98095_i2c_write(priv, M98095_096_PWR_DAC_CK, 0x07); |
Rajeshwari Shinde | 5febe8d | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 475 | |
Rajeshwari Shinde | 5febe8d | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 476 | if (error < 0) |
| 477 | return -1; |
| 478 | |
| 479 | return 0; |
| 480 | } |
| 481 | |
| 482 | static int max98095_do_init(struct sound_codec_info *pcodec_info, |
Dani Krishna Mohan | 6b40852 | 2013-09-11 16:38:50 +0530 | [diff] [blame] | 483 | enum en_max_audio_interface aif_id, |
| 484 | int sampling_rate, int mclk_freq, |
| 485 | int bits_per_sample) |
Rajeshwari Shinde | 5febe8d | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 486 | { |
| 487 | int ret = 0; |
| 488 | |
Simon Glass | 82a27d2 | 2018-12-03 04:37:28 -0700 | [diff] [blame] | 489 | ret = max98095_setup_interface(&g_max98095_info, aif_id); |
Rajeshwari Shinde | 5febe8d | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 490 | if (ret < 0) { |
| 491 | debug("%s: max98095 codec chip init failed\n", __func__); |
| 492 | return ret; |
| 493 | } |
| 494 | |
| 495 | ret = max98095_set_sysclk(&g_max98095_info, mclk_freq); |
| 496 | if (ret < 0) { |
| 497 | debug("%s: max98095 codec set sys clock failed\n", __func__); |
| 498 | return ret; |
| 499 | } |
| 500 | |
Dani Krishna Mohan | 6b40852 | 2013-09-11 16:38:50 +0530 | [diff] [blame] | 501 | ret = max98095_hw_params(&g_max98095_info, aif_id, sampling_rate, |
| 502 | bits_per_sample); |
Rajeshwari Shinde | 5febe8d | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 503 | |
| 504 | if (ret == 0) { |
| 505 | ret = max98095_set_fmt(&g_max98095_info, |
Dani Krishna Mohan | 6b40852 | 2013-09-11 16:38:50 +0530 | [diff] [blame] | 506 | SND_SOC_DAIFMT_I2S | |
| 507 | SND_SOC_DAIFMT_NB_NF | |
| 508 | SND_SOC_DAIFMT_CBS_CFS, |
| 509 | aif_id); |
Rajeshwari Shinde | 5febe8d | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | return ret; |
| 513 | } |
| 514 | |
| 515 | static int get_max98095_codec_values(struct sound_codec_info *pcodec_info, |
| 516 | const void *blob) |
| 517 | { |
| 518 | int error = 0; |
Rajeshwari Shinde | 5febe8d | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 519 | enum fdt_compat_id compat; |
| 520 | int node; |
| 521 | int parent; |
| 522 | |
| 523 | /* Get the node from FDT for codec */ |
| 524 | node = fdtdec_next_compatible(blob, 0, COMPAT_MAXIM_98095_CODEC); |
| 525 | if (node <= 0) { |
| 526 | debug("EXYNOS_SOUND: No node for codec in device tree\n"); |
| 527 | debug("node = %d\n", node); |
| 528 | return -1; |
| 529 | } |
| 530 | |
| 531 | parent = fdt_parent_offset(blob, node); |
| 532 | if (parent < 0) { |
| 533 | debug("%s: Cannot find node parent\n", __func__); |
| 534 | return -1; |
| 535 | } |
| 536 | |
| 537 | compat = fdtdec_lookup(blob, parent); |
| 538 | switch (compat) { |
| 539 | case COMPAT_SAMSUNG_S3C2440_I2C: |
| 540 | pcodec_info->i2c_bus = i2c_get_bus_num_fdt(parent); |
| 541 | error |= pcodec_info->i2c_bus; |
| 542 | debug("i2c bus = %d\n", pcodec_info->i2c_bus); |
| 543 | pcodec_info->i2c_dev_addr = fdtdec_get_int(blob, node, |
| 544 | "reg", 0); |
| 545 | error |= pcodec_info->i2c_dev_addr; |
| 546 | debug("i2c dev addr = %x\n", pcodec_info->i2c_dev_addr); |
| 547 | break; |
| 548 | default: |
| 549 | debug("%s: Unknown compat id %d\n", __func__, compat); |
| 550 | return -1; |
| 551 | } |
Rajeshwari Shinde | 5febe8d | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 552 | if (error == -1) { |
| 553 | debug("fail to get max98095 codec node properties\n"); |
| 554 | return -1; |
| 555 | } |
| 556 | |
| 557 | return 0; |
| 558 | } |
| 559 | |
| 560 | /* max98095 Device Initialisation */ |
Dani Krishna Mohan | 6b40852 | 2013-09-11 16:38:50 +0530 | [diff] [blame] | 561 | int max98095_init(const void *blob, enum en_max_audio_interface aif_id, |
| 562 | int sampling_rate, int mclk_freq, |
| 563 | int bits_per_sample) |
Rajeshwari Shinde | 5febe8d | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 564 | { |
| 565 | int ret; |
| 566 | int old_bus = i2c_get_bus_num(); |
| 567 | struct sound_codec_info *pcodec_info = &g_codec_info; |
| 568 | |
| 569 | if (get_max98095_codec_values(pcodec_info, blob) < 0) { |
| 570 | debug("FDT Codec values failed\n"); |
Dani Krishna Mohan | 6b40852 | 2013-09-11 16:38:50 +0530 | [diff] [blame] | 571 | return -1; |
Rajeshwari Shinde | 5febe8d | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | i2c_set_bus_num(pcodec_info->i2c_bus); |
Simon Glass | 82a27d2 | 2018-12-03 04:37:28 -0700 | [diff] [blame] | 575 | |
| 576 | /* shift the device address by 1 for 7 bit addressing */ |
Simon Glass | 372922c | 2018-12-03 04:37:30 -0700 | [diff] [blame] | 577 | g_max98095_info.i2c_addr = pcodec_info->i2c_dev_addr >> 1; |
Simon Glass | 82a27d2 | 2018-12-03 04:37:28 -0700 | [diff] [blame] | 578 | ret = max98095_device_init(&g_max98095_info); |
| 579 | if (ret < 0) { |
| 580 | debug("%s: max98095 codec chip init failed\n", __func__); |
| 581 | return ret; |
| 582 | } |
| 583 | |
Dani Krishna Mohan | 6b40852 | 2013-09-11 16:38:50 +0530 | [diff] [blame] | 584 | ret = max98095_do_init(pcodec_info, aif_id, sampling_rate, mclk_freq, |
| 585 | bits_per_sample); |
Rajeshwari Shinde | 5febe8d | 2013-02-14 19:46:12 +0000 | [diff] [blame] | 586 | i2c_set_bus_num(old_bus); |
| 587 | |
| 588 | return ret; |
| 589 | } |