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