1 /* 2 * Copyright (C) 2012 Samsung Electronics 3 * R. Chandrasekar <rcsekar@samsung.com> 4 * 5 * See file CREDITS for list of people who contributed to this 6 * project. 7 * 8 * This program is free software; you can redistribute it and/or 9 * modify it under the terms of the GNU General Public License as 10 * published by the Free Software Foundation; either version 2 of 11 * the License, or (at your option) any later version. 12 * 13 * This program is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 * GNU General Public License for more details. 17 * 18 * You should have received a copy of the GNU General Public License 19 * along with this program; if not, write to the Free Software 20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, 21 * MA 02111-1307 USA 22 */ 23 24 #include <malloc.h> 25 #include <common.h> 26 #include <asm/io.h> 27 #include <i2c.h> 28 #include <i2s.h> 29 #include <sound.h> 30 #include "wm8994.h" 31 #include <asm/arch/sound.h> 32 33 /* defines */ 34 #define SOUND_400_HZ 400 35 #define SOUND_BITS_IN_BYTE 8 36 37 static struct i2stx_info g_i2stx_pri; 38 static struct sound_codec_info g_codec_info; 39 40 /* 41 * get_sound_fdt_values gets fdt values for i2s parameters 42 * 43 * @param i2stx_info i2s transmitter transfer param structure 44 * @param blob FDT blob 45 */ 46 static void get_sound_i2s_values(struct i2stx_info *i2s) 47 { 48 i2s->base_address = samsung_get_base_i2s(); 49 i2s->audio_pll_clk = I2S_PLL_CLK; 50 i2s->samplingrate = I2S_SAMPLING_RATE; 51 i2s->bitspersample = I2S_BITS_PER_SAMPLE; 52 i2s->channels = I2S_CHANNELS; 53 i2s->rfs = I2S_RFS; 54 i2s->bfs = I2S_BFS; 55 } 56 57 /* 58 * Gets fdt values for wm8994 config parameters 59 * 60 * @param pcodec_info codec information structure 61 * @param blob FDT blob 62 * @return int value, 0 for success 63 */ 64 static int get_sound_wm8994_values(struct sound_codec_info *pcodec_info) 65 { 66 int error = 0; 67 68 switch (AUDIO_COMPAT) { 69 case AUDIO_COMPAT_SPI: 70 debug("%s: Support not added for SPI interface\n", __func__); 71 return -1; 72 break; 73 case AUDIO_COMPAT_I2C: 74 pcodec_info->i2c_bus = AUDIO_I2C_BUS; 75 pcodec_info->i2c_dev_addr = AUDIO_I2C_REG; 76 debug("i2c dev addr = %d\n", pcodec_info->i2c_dev_addr); 77 break; 78 default: 79 debug("%s: Unknown compat id %d\n", __func__, AUDIO_COMPAT); 80 return -1; 81 } 82 83 if (error == -1) { 84 debug("fail to get wm8994 codec node properties\n"); 85 return -1; 86 } 87 88 return 0; 89 } 90 91 /* 92 * Gets fdt values for codec config parameters 93 * 94 * @param pcodec_info codec information structure 95 * @param blob FDT blob 96 * @return int value, 0 for success 97 */ 98 static int get_sound_codec_values(struct sound_codec_info *pcodec_info) 99 { 100 int error = 0; 101 const char *codectype; 102 103 codectype = AUDIO_CODEC; 104 105 if (!strcmp(codectype, "wm8994")) { 106 pcodec_info->codec_type = CODEC_WM_8994; 107 error = get_sound_wm8994_values(pcodec_info); 108 } else { 109 error = -1; 110 } 111 112 if (error == -1) { 113 debug("fail to get sound codec node properties\n"); 114 return -1; 115 } 116 117 return 0; 118 } 119 120 int sound_init(void) 121 { 122 int ret; 123 struct i2stx_info *pi2s_tx = &g_i2stx_pri; 124 struct sound_codec_info *pcodec_info = &g_codec_info; 125 126 /* Get the I2S Values */ 127 get_sound_i2s_values(pi2s_tx); 128 129 /* Get the codec Values */ 130 if (get_sound_codec_values(pcodec_info) < 0) 131 return -1; 132 133 ret = i2s_tx_init(pi2s_tx); 134 if (ret) { 135 debug("%s: Failed to init i2c transmit: ret=%d\n", __func__, 136 ret); 137 return ret; 138 } 139 140 /* Check the codec type and initialise the same */ 141 if (pcodec_info->codec_type == CODEC_WM_8994) { 142 ret = wm8994_init(pcodec_info, WM8994_AIF2, 143 pi2s_tx->samplingrate, 144 (pi2s_tx->samplingrate * (pi2s_tx->rfs)), 145 pi2s_tx->bitspersample, pi2s_tx->channels); 146 } else { 147 debug("%s: Unknown code type %d\n", __func__, 148 pcodec_info->codec_type); 149 return -1; 150 } 151 if (ret) { 152 debug("%s: Codec init failed\n", __func__); 153 return -1; 154 } 155 156 return ret; 157 } 158 159 /* 160 * Generates square wave sound data for 1 second 161 * 162 * @param data data buffer pointer 163 * @param size size of the buffer 164 * @param freq frequency of the wave 165 */ 166 static void sound_prepare_buffer(unsigned short *data, int size, uint32_t freq) 167 { 168 const int sample = 48000; 169 const unsigned short amplitude = 16000; /* between 1 and 32767 */ 170 const int period = freq ? sample / freq : 0; 171 const int half = period / 2; 172 173 assert(freq); 174 175 /* Make sure we don't overflow our buffer */ 176 if (size % 2) 177 size--; 178 179 while (size) { 180 int i; 181 for (i = 0; size && i < half; i++) { 182 size -= 2; 183 *data++ = amplitude; 184 *data++ = amplitude; 185 } 186 for (i = 0; size && i < period - half; i++) { 187 size -= 2; 188 *data++ = -amplitude; 189 *data++ = -amplitude; 190 } 191 } 192 } 193 194 int sound_play(uint32_t msec, uint32_t frequency) 195 { 196 unsigned int *data; 197 unsigned long data_size; 198 unsigned int ret = 0; 199 200 /*Buffer length computation */ 201 data_size = g_i2stx_pri.samplingrate * g_i2stx_pri.channels; 202 data_size *= (g_i2stx_pri.bitspersample / SOUND_BITS_IN_BYTE); 203 data = malloc(data_size); 204 205 if (data == NULL) { 206 debug("%s: malloc failed\n", __func__); 207 return -1; 208 } 209 210 sound_prepare_buffer((unsigned short *)data, 211 data_size / sizeof(unsigned short), frequency); 212 213 while (msec >= 1000) { 214 ret = i2s_transfer_tx_data(&g_i2stx_pri, data, 215 (data_size / sizeof(int))); 216 msec -= 1000; 217 } 218 if (msec) { 219 unsigned long size = 220 (data_size * msec) / (sizeof(int) * 1000); 221 222 ret = i2s_transfer_tx_data(&g_i2stx_pri, data, size); 223 } 224 225 free(data); 226 227 return ret; 228 } 229