Rajeshwari Shinde | 836a66c | 2012-10-25 19:49:22 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 Samsung Electronics |
| 3 | * R. Chandrasekar <rcsekar@samsung.com> |
| 4 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame^] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
Rajeshwari Shinde | 836a66c | 2012-10-25 19:49:22 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <malloc.h> |
| 9 | #include <common.h> |
| 10 | #include <asm/io.h> |
Rajeshwari Shinde | 7376621 | 2012-12-26 20:03:17 +0000 | [diff] [blame] | 11 | #include <libfdt.h> |
| 12 | #include <fdtdec.h> |
Rajeshwari Shinde | 836a66c | 2012-10-25 19:49:22 +0000 | [diff] [blame] | 13 | #include <i2c.h> |
| 14 | #include <i2s.h> |
| 15 | #include <sound.h> |
Rajeshwari Shinde | 836a66c | 2012-10-25 19:49:22 +0000 | [diff] [blame] | 16 | #include <asm/arch/sound.h> |
Rajeshwari Shinde | 7376621 | 2012-12-26 20:03:17 +0000 | [diff] [blame] | 17 | #include "wm8994.h" |
Rajeshwari Shinde | 1bf5825 | 2013-02-14 19:46:13 +0000 | [diff] [blame] | 18 | #include "max98095.h" |
Rajeshwari Shinde | 836a66c | 2012-10-25 19:49:22 +0000 | [diff] [blame] | 19 | |
| 20 | /* defines */ |
| 21 | #define SOUND_400_HZ 400 |
| 22 | #define SOUND_BITS_IN_BYTE 8 |
| 23 | |
| 24 | static struct i2stx_info g_i2stx_pri; |
Rajeshwari Shinde | 836a66c | 2012-10-25 19:49:22 +0000 | [diff] [blame] | 25 | |
| 26 | /* |
Rajeshwari Shinde | 7376621 | 2012-12-26 20:03:17 +0000 | [diff] [blame] | 27 | * get_sound_i2s_values gets values for i2s parameters |
Rajeshwari Shinde | 836a66c | 2012-10-25 19:49:22 +0000 | [diff] [blame] | 28 | * |
| 29 | * @param i2stx_info i2s transmitter transfer param structure |
Rajeshwari Shinde | 7376621 | 2012-12-26 20:03:17 +0000 | [diff] [blame] | 30 | * @param blob FDT blob if enabled else NULL |
Rajeshwari Shinde | 836a66c | 2012-10-25 19:49:22 +0000 | [diff] [blame] | 31 | */ |
Rajeshwari Shinde | 7376621 | 2012-12-26 20:03:17 +0000 | [diff] [blame] | 32 | static int get_sound_i2s_values(struct i2stx_info *i2s, const void *blob) |
Rajeshwari Shinde | 836a66c | 2012-10-25 19:49:22 +0000 | [diff] [blame] | 33 | { |
Rajeshwari Shinde | 7376621 | 2012-12-26 20:03:17 +0000 | [diff] [blame] | 34 | #ifdef CONFIG_OF_CONTROL |
| 35 | int node; |
Rajeshwari Shinde | 836a66c | 2012-10-25 19:49:22 +0000 | [diff] [blame] | 36 | int error = 0; |
Rajeshwari Shinde | 7376621 | 2012-12-26 20:03:17 +0000 | [diff] [blame] | 37 | int base; |
Rajeshwari Shinde | 836a66c | 2012-10-25 19:49:22 +0000 | [diff] [blame] | 38 | |
Rajeshwari Shinde | 7376621 | 2012-12-26 20:03:17 +0000 | [diff] [blame] | 39 | node = fdtdec_next_compatible(blob, 0, |
| 40 | COMPAT_SAMSUNG_EXYNOS5_SOUND); |
| 41 | if (node <= 0) { |
| 42 | debug("EXYNOS_SOUND: No node for sound in device tree\n"); |
Rajeshwari Shinde | 836a66c | 2012-10-25 19:49:22 +0000 | [diff] [blame] | 43 | return -1; |
Rajeshwari Shinde | 7376621 | 2012-12-26 20:03:17 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | /* |
| 47 | * Get the pre-defined sound specific values from FDT. |
| 48 | * All of these are expected to be correct otherwise |
| 49 | * wrong register values in i2s setup parameters |
| 50 | * may result in no sound play. |
| 51 | */ |
| 52 | base = fdtdec_get_addr(blob, node, "reg"); |
| 53 | if (base == FDT_ADDR_T_NONE) { |
| 54 | debug("%s: Missing i2s base\n", __func__); |
Rajeshwari Shinde | 836a66c | 2012-10-25 19:49:22 +0000 | [diff] [blame] | 55 | return -1; |
| 56 | } |
Rajeshwari Shinde | 7376621 | 2012-12-26 20:03:17 +0000 | [diff] [blame] | 57 | i2s->base_address = base; |
Rajeshwari Shinde | 836a66c | 2012-10-25 19:49:22 +0000 | [diff] [blame] | 58 | |
Rajeshwari Shinde | 7376621 | 2012-12-26 20:03:17 +0000 | [diff] [blame] | 59 | i2s->audio_pll_clk = fdtdec_get_int(blob, |
| 60 | node, "samsung,i2s-epll-clock-frequency", -1); |
| 61 | error |= i2s->audio_pll_clk; |
| 62 | debug("audio_pll_clk = %d\n", i2s->audio_pll_clk); |
| 63 | i2s->samplingrate = fdtdec_get_int(blob, |
| 64 | node, "samsung,i2s-sampling-rate", -1); |
| 65 | error |= i2s->samplingrate; |
| 66 | debug("samplingrate = %d\n", i2s->samplingrate); |
| 67 | i2s->bitspersample = fdtdec_get_int(blob, |
| 68 | node, "samsung,i2s-bits-per-sample", -1); |
| 69 | error |= i2s->bitspersample; |
| 70 | debug("bitspersample = %d\n", i2s->bitspersample); |
| 71 | i2s->channels = fdtdec_get_int(blob, |
| 72 | node, "samsung,i2s-channels", -1); |
| 73 | error |= i2s->channels; |
| 74 | debug("channels = %d\n", i2s->channels); |
| 75 | i2s->rfs = fdtdec_get_int(blob, |
| 76 | node, "samsung,i2s-lr-clk-framesize", -1); |
| 77 | error |= i2s->rfs; |
| 78 | debug("rfs = %d\n", i2s->rfs); |
| 79 | i2s->bfs = fdtdec_get_int(blob, |
| 80 | node, "samsung,i2s-bit-clk-framesize", -1); |
| 81 | error |= i2s->bfs; |
| 82 | debug("bfs = %d\n", i2s->bfs); |
Rajeshwari Shinde | 836a66c | 2012-10-25 19:49:22 +0000 | [diff] [blame] | 83 | if (error == -1) { |
Rajeshwari Shinde | 7376621 | 2012-12-26 20:03:17 +0000 | [diff] [blame] | 84 | debug("fail to get sound i2s node properties\n"); |
Rajeshwari Shinde | 836a66c | 2012-10-25 19:49:22 +0000 | [diff] [blame] | 85 | return -1; |
| 86 | } |
Rajeshwari Shinde | 7376621 | 2012-12-26 20:03:17 +0000 | [diff] [blame] | 87 | #else |
| 88 | i2s->base_address = samsung_get_base_i2s(); |
| 89 | i2s->audio_pll_clk = I2S_PLL_CLK; |
| 90 | i2s->samplingrate = I2S_SAMPLING_RATE; |
| 91 | i2s->bitspersample = I2S_BITS_PER_SAMPLE; |
| 92 | i2s->channels = I2S_CHANNELS; |
| 93 | i2s->rfs = I2S_RFS; |
| 94 | i2s->bfs = I2S_BFS; |
| 95 | #endif |
Rajeshwari Shinde | 836a66c | 2012-10-25 19:49:22 +0000 | [diff] [blame] | 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | /* |
Rajeshwari Shinde | 7376621 | 2012-12-26 20:03:17 +0000 | [diff] [blame] | 100 | * Init codec |
Rajeshwari Shinde | 836a66c | 2012-10-25 19:49:22 +0000 | [diff] [blame] | 101 | * |
Rajeshwari Shinde | 7376621 | 2012-12-26 20:03:17 +0000 | [diff] [blame] | 102 | * @param blob FDT blob |
| 103 | * @param pi2s_tx i2s parameters required by codec |
| 104 | * @return int value, 0 for success |
Rajeshwari Shinde | 836a66c | 2012-10-25 19:49:22 +0000 | [diff] [blame] | 105 | */ |
Rajeshwari Shinde | 7376621 | 2012-12-26 20:03:17 +0000 | [diff] [blame] | 106 | static int codec_init(const void *blob, struct i2stx_info *pi2s_tx) |
Rajeshwari Shinde | 836a66c | 2012-10-25 19:49:22 +0000 | [diff] [blame] | 107 | { |
Rajeshwari Shinde | 7376621 | 2012-12-26 20:03:17 +0000 | [diff] [blame] | 108 | int ret; |
Rajeshwari Shinde | 836a66c | 2012-10-25 19:49:22 +0000 | [diff] [blame] | 109 | const char *codectype; |
Rajeshwari Shinde | 7376621 | 2012-12-26 20:03:17 +0000 | [diff] [blame] | 110 | #ifdef CONFIG_OF_CONTROL |
| 111 | int node; |
Rajeshwari Shinde | 836a66c | 2012-10-25 19:49:22 +0000 | [diff] [blame] | 112 | |
Rajeshwari Shinde | 7376621 | 2012-12-26 20:03:17 +0000 | [diff] [blame] | 113 | /* Get the node from FDT for sound */ |
| 114 | node = fdtdec_next_compatible(blob, 0, COMPAT_SAMSUNG_EXYNOS5_SOUND); |
| 115 | if (node <= 0) { |
| 116 | debug("EXYNOS_SOUND: No node for sound in device tree\n"); |
| 117 | debug("node = %d\n", node); |
| 118 | return -1; |
| 119 | } |
Rajeshwari Shinde | 836a66c | 2012-10-25 19:49:22 +0000 | [diff] [blame] | 120 | |
Rajeshwari Shinde | 7376621 | 2012-12-26 20:03:17 +0000 | [diff] [blame] | 121 | /* |
| 122 | * Get the pre-defined sound codec specific values from FDT. |
| 123 | * All of these are expected to be correct otherwise sound |
| 124 | * can not be played |
| 125 | */ |
| 126 | codectype = fdt_getprop(blob, node, "samsung,codec-type", NULL); |
| 127 | debug("device = %s\n", codectype); |
| 128 | #else |
| 129 | codectype = AUDIO_CODEC; |
| 130 | #endif |
Rajeshwari Shinde | 836a66c | 2012-10-25 19:49:22 +0000 | [diff] [blame] | 131 | if (!strcmp(codectype, "wm8994")) { |
Rajeshwari Shinde | 7376621 | 2012-12-26 20:03:17 +0000 | [diff] [blame] | 132 | /* Check the codec type and initialise the same */ |
| 133 | ret = wm8994_init(blob, WM8994_AIF2, |
| 134 | pi2s_tx->samplingrate, |
| 135 | (pi2s_tx->samplingrate * (pi2s_tx->rfs)), |
| 136 | pi2s_tx->bitspersample, pi2s_tx->channels); |
Rajeshwari Shinde | 1bf5825 | 2013-02-14 19:46:13 +0000 | [diff] [blame] | 137 | } else if (!strcmp(codectype, "max98095")) { |
| 138 | ret = max98095_init(blob, pi2s_tx->samplingrate, |
| 139 | (pi2s_tx->samplingrate * (pi2s_tx->rfs)), |
| 140 | pi2s_tx->bitspersample); |
Rajeshwari Shinde | 836a66c | 2012-10-25 19:49:22 +0000 | [diff] [blame] | 141 | } else { |
Rajeshwari Shinde | 1bf5825 | 2013-02-14 19:46:13 +0000 | [diff] [blame] | 142 | debug("%s: Unknown codec type %s\n", __func__, codectype); |
Rajeshwari Shinde | 7376621 | 2012-12-26 20:03:17 +0000 | [diff] [blame] | 143 | return -1; |
Rajeshwari Shinde | 836a66c | 2012-10-25 19:49:22 +0000 | [diff] [blame] | 144 | } |
Rajeshwari Shinde | 1bf5825 | 2013-02-14 19:46:13 +0000 | [diff] [blame] | 145 | |
Rajeshwari Shinde | 7376621 | 2012-12-26 20:03:17 +0000 | [diff] [blame] | 146 | if (ret) { |
| 147 | debug("%s: Codec init failed\n", __func__); |
Rajeshwari Shinde | 836a66c | 2012-10-25 19:49:22 +0000 | [diff] [blame] | 148 | return -1; |
| 149 | } |
| 150 | |
| 151 | return 0; |
| 152 | } |
| 153 | |
Rajeshwari Shinde | 7376621 | 2012-12-26 20:03:17 +0000 | [diff] [blame] | 154 | int sound_init(const void *blob) |
Rajeshwari Shinde | 836a66c | 2012-10-25 19:49:22 +0000 | [diff] [blame] | 155 | { |
| 156 | int ret; |
| 157 | struct i2stx_info *pi2s_tx = &g_i2stx_pri; |
Rajeshwari Shinde | 836a66c | 2012-10-25 19:49:22 +0000 | [diff] [blame] | 158 | |
| 159 | /* Get the I2S Values */ |
Rajeshwari Shinde | 7376621 | 2012-12-26 20:03:17 +0000 | [diff] [blame] | 160 | if (get_sound_i2s_values(pi2s_tx, blob) < 0) { |
| 161 | debug(" FDT I2S values failed\n"); |
| 162 | return -1; |
| 163 | } |
Rajeshwari Shinde | 836a66c | 2012-10-25 19:49:22 +0000 | [diff] [blame] | 164 | |
Rajeshwari Shinde | 7376621 | 2012-12-26 20:03:17 +0000 | [diff] [blame] | 165 | if (codec_init(blob, pi2s_tx) < 0) { |
| 166 | debug(" Codec init failed\n"); |
Rajeshwari Shinde | 836a66c | 2012-10-25 19:49:22 +0000 | [diff] [blame] | 167 | return -1; |
Rajeshwari Shinde | 7376621 | 2012-12-26 20:03:17 +0000 | [diff] [blame] | 168 | } |
Rajeshwari Shinde | 836a66c | 2012-10-25 19:49:22 +0000 | [diff] [blame] | 169 | |
| 170 | ret = i2s_tx_init(pi2s_tx); |
| 171 | if (ret) { |
| 172 | debug("%s: Failed to init i2c transmit: ret=%d\n", __func__, |
| 173 | ret); |
| 174 | return ret; |
| 175 | } |
| 176 | |
Rajeshwari Shinde | 836a66c | 2012-10-25 19:49:22 +0000 | [diff] [blame] | 177 | |
| 178 | return ret; |
| 179 | } |
| 180 | |
| 181 | /* |
| 182 | * Generates square wave sound data for 1 second |
| 183 | * |
| 184 | * @param data data buffer pointer |
| 185 | * @param size size of the buffer |
| 186 | * @param freq frequency of the wave |
| 187 | */ |
| 188 | static void sound_prepare_buffer(unsigned short *data, int size, uint32_t freq) |
| 189 | { |
| 190 | const int sample = 48000; |
| 191 | const unsigned short amplitude = 16000; /* between 1 and 32767 */ |
| 192 | const int period = freq ? sample / freq : 0; |
| 193 | const int half = period / 2; |
| 194 | |
| 195 | assert(freq); |
| 196 | |
| 197 | /* Make sure we don't overflow our buffer */ |
| 198 | if (size % 2) |
| 199 | size--; |
| 200 | |
| 201 | while (size) { |
| 202 | int i; |
| 203 | for (i = 0; size && i < half; i++) { |
| 204 | size -= 2; |
| 205 | *data++ = amplitude; |
| 206 | *data++ = amplitude; |
| 207 | } |
| 208 | for (i = 0; size && i < period - half; i++) { |
| 209 | size -= 2; |
| 210 | *data++ = -amplitude; |
| 211 | *data++ = -amplitude; |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | int sound_play(uint32_t msec, uint32_t frequency) |
| 217 | { |
| 218 | unsigned int *data; |
| 219 | unsigned long data_size; |
| 220 | unsigned int ret = 0; |
| 221 | |
| 222 | /*Buffer length computation */ |
| 223 | data_size = g_i2stx_pri.samplingrate * g_i2stx_pri.channels; |
| 224 | data_size *= (g_i2stx_pri.bitspersample / SOUND_BITS_IN_BYTE); |
| 225 | data = malloc(data_size); |
| 226 | |
| 227 | if (data == NULL) { |
| 228 | debug("%s: malloc failed\n", __func__); |
| 229 | return -1; |
| 230 | } |
| 231 | |
| 232 | sound_prepare_buffer((unsigned short *)data, |
| 233 | data_size / sizeof(unsigned short), frequency); |
| 234 | |
| 235 | while (msec >= 1000) { |
| 236 | ret = i2s_transfer_tx_data(&g_i2stx_pri, data, |
| 237 | (data_size / sizeof(int))); |
| 238 | msec -= 1000; |
| 239 | } |
| 240 | if (msec) { |
| 241 | unsigned long size = |
| 242 | (data_size * msec) / (sizeof(int) * 1000); |
| 243 | |
| 244 | ret = i2s_transfer_tx_data(&g_i2stx_pri, data, size); |
| 245 | } |
| 246 | |
| 247 | free(data); |
| 248 | |
| 249 | return ret; |
| 250 | } |