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