2 * Copyright (C) 2012 Samsung Electronics
3 * R. Chandrasekar <rcsekar@samsung.com>
5 * See file CREDITS for list of people who contributed to this
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.
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.
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,
32 #include <asm/arch/sound.h>
37 #define SOUND_400_HZ 400
38 #define SOUND_BITS_IN_BYTE 8
40 static struct i2stx_info g_i2stx_pri;
43 * get_sound_i2s_values gets values for i2s parameters
45 * @param i2stx_info i2s transmitter transfer param structure
46 * @param blob FDT blob if enabled else NULL
48 static int get_sound_i2s_values(struct i2stx_info *i2s, const void *blob)
50 #ifdef CONFIG_OF_CONTROL
55 node = fdtdec_next_compatible(blob, 0,
56 COMPAT_SAMSUNG_EXYNOS5_SOUND);
58 debug("EXYNOS_SOUND: No node for sound in device tree\n");
63 * Get the pre-defined sound specific values from FDT.
64 * All of these are expected to be correct otherwise
65 * wrong register values in i2s setup parameters
66 * may result in no sound play.
68 base = fdtdec_get_addr(blob, node, "reg");
69 if (base == FDT_ADDR_T_NONE) {
70 debug("%s: Missing i2s base\n", __func__);
73 i2s->base_address = base;
75 i2s->audio_pll_clk = fdtdec_get_int(blob,
76 node, "samsung,i2s-epll-clock-frequency", -1);
77 error |= i2s->audio_pll_clk;
78 debug("audio_pll_clk = %d\n", i2s->audio_pll_clk);
79 i2s->samplingrate = fdtdec_get_int(blob,
80 node, "samsung,i2s-sampling-rate", -1);
81 error |= i2s->samplingrate;
82 debug("samplingrate = %d\n", i2s->samplingrate);
83 i2s->bitspersample = fdtdec_get_int(blob,
84 node, "samsung,i2s-bits-per-sample", -1);
85 error |= i2s->bitspersample;
86 debug("bitspersample = %d\n", i2s->bitspersample);
87 i2s->channels = fdtdec_get_int(blob,
88 node, "samsung,i2s-channels", -1);
89 error |= i2s->channels;
90 debug("channels = %d\n", i2s->channels);
91 i2s->rfs = fdtdec_get_int(blob,
92 node, "samsung,i2s-lr-clk-framesize", -1);
94 debug("rfs = %d\n", i2s->rfs);
95 i2s->bfs = fdtdec_get_int(blob,
96 node, "samsung,i2s-bit-clk-framesize", -1);
98 debug("bfs = %d\n", i2s->bfs);
100 debug("fail to get sound i2s node properties\n");
104 i2s->base_address = samsung_get_base_i2s();
105 i2s->audio_pll_clk = I2S_PLL_CLK;
106 i2s->samplingrate = I2S_SAMPLING_RATE;
107 i2s->bitspersample = I2S_BITS_PER_SAMPLE;
108 i2s->channels = I2S_CHANNELS;
118 * @param blob FDT blob
119 * @param pi2s_tx i2s parameters required by codec
120 * @return int value, 0 for success
122 static int codec_init(const void *blob, struct i2stx_info *pi2s_tx)
125 const char *codectype;
126 #ifdef CONFIG_OF_CONTROL
129 /* Get the node from FDT for sound */
130 node = fdtdec_next_compatible(blob, 0, COMPAT_SAMSUNG_EXYNOS5_SOUND);
132 debug("EXYNOS_SOUND: No node for sound in device tree\n");
133 debug("node = %d\n", node);
138 * Get the pre-defined sound codec specific values from FDT.
139 * All of these are expected to be correct otherwise sound
142 codectype = fdt_getprop(blob, node, "samsung,codec-type", NULL);
143 debug("device = %s\n", codectype);
145 codectype = AUDIO_CODEC;
147 if (!strcmp(codectype, "wm8994")) {
148 /* Check the codec type and initialise the same */
149 ret = wm8994_init(blob, WM8994_AIF2,
150 pi2s_tx->samplingrate,
151 (pi2s_tx->samplingrate * (pi2s_tx->rfs)),
152 pi2s_tx->bitspersample, pi2s_tx->channels);
153 } else if (!strcmp(codectype, "max98095")) {
154 ret = max98095_init(blob, pi2s_tx->samplingrate,
155 (pi2s_tx->samplingrate * (pi2s_tx->rfs)),
156 pi2s_tx->bitspersample);
158 debug("%s: Unknown codec type %s\n", __func__, codectype);
163 debug("%s: Codec init failed\n", __func__);
170 int sound_init(const void *blob)
173 struct i2stx_info *pi2s_tx = &g_i2stx_pri;
175 /* Get the I2S Values */
176 if (get_sound_i2s_values(pi2s_tx, blob) < 0) {
177 debug(" FDT I2S values failed\n");
181 if (codec_init(blob, pi2s_tx) < 0) {
182 debug(" Codec init failed\n");
186 ret = i2s_tx_init(pi2s_tx);
188 debug("%s: Failed to init i2c transmit: ret=%d\n", __func__,
198 * Generates square wave sound data for 1 second
200 * @param data data buffer pointer
201 * @param size size of the buffer
202 * @param freq frequency of the wave
204 static void sound_prepare_buffer(unsigned short *data, int size, uint32_t freq)
206 const int sample = 48000;
207 const unsigned short amplitude = 16000; /* between 1 and 32767 */
208 const int period = freq ? sample / freq : 0;
209 const int half = period / 2;
213 /* Make sure we don't overflow our buffer */
219 for (i = 0; size && i < half; i++) {
224 for (i = 0; size && i < period - half; i++) {
226 *data++ = -amplitude;
227 *data++ = -amplitude;
232 int sound_play(uint32_t msec, uint32_t frequency)
235 unsigned long data_size;
236 unsigned int ret = 0;
238 /*Buffer length computation */
239 data_size = g_i2stx_pri.samplingrate * g_i2stx_pri.channels;
240 data_size *= (g_i2stx_pri.bitspersample / SOUND_BITS_IN_BYTE);
241 data = malloc(data_size);
244 debug("%s: malloc failed\n", __func__);
248 sound_prepare_buffer((unsigned short *)data,
249 data_size / sizeof(unsigned short), frequency);
251 while (msec >= 1000) {
252 ret = i2s_transfer_tx_data(&g_i2stx_pri, data,
253 (data_size / sizeof(int)));
258 (data_size * msec) / (sizeof(int) * 1000);
260 ret = i2s_transfer_tx_data(&g_i2stx_pri, data, size);