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