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>
36 #define SOUND_400_HZ 400
37 #define SOUND_BITS_IN_BYTE 8
39 static struct i2stx_info g_i2stx_pri;
42 * get_sound_i2s_values gets values for i2s parameters
44 * @param i2stx_info i2s transmitter transfer param structure
45 * @param blob FDT blob if enabled else NULL
47 static int get_sound_i2s_values(struct i2stx_info *i2s, const void *blob)
49 #ifdef CONFIG_OF_CONTROL
54 node = fdtdec_next_compatible(blob, 0,
55 COMPAT_SAMSUNG_EXYNOS5_SOUND);
57 debug("EXYNOS_SOUND: No node for sound in device tree\n");
62 * Get the pre-defined sound specific values from FDT.
63 * All of these are expected to be correct otherwise
64 * wrong register values in i2s setup parameters
65 * may result in no sound play.
67 base = fdtdec_get_addr(blob, node, "reg");
68 if (base == FDT_ADDR_T_NONE) {
69 debug("%s: Missing i2s base\n", __func__);
72 i2s->base_address = base;
74 i2s->audio_pll_clk = fdtdec_get_int(blob,
75 node, "samsung,i2s-epll-clock-frequency", -1);
76 error |= i2s->audio_pll_clk;
77 debug("audio_pll_clk = %d\n", i2s->audio_pll_clk);
78 i2s->samplingrate = fdtdec_get_int(blob,
79 node, "samsung,i2s-sampling-rate", -1);
80 error |= i2s->samplingrate;
81 debug("samplingrate = %d\n", i2s->samplingrate);
82 i2s->bitspersample = fdtdec_get_int(blob,
83 node, "samsung,i2s-bits-per-sample", -1);
84 error |= i2s->bitspersample;
85 debug("bitspersample = %d\n", i2s->bitspersample);
86 i2s->channels = fdtdec_get_int(blob,
87 node, "samsung,i2s-channels", -1);
88 error |= i2s->channels;
89 debug("channels = %d\n", i2s->channels);
90 i2s->rfs = fdtdec_get_int(blob,
91 node, "samsung,i2s-lr-clk-framesize", -1);
93 debug("rfs = %d\n", i2s->rfs);
94 i2s->bfs = fdtdec_get_int(blob,
95 node, "samsung,i2s-bit-clk-framesize", -1);
97 debug("bfs = %d\n", i2s->bfs);
99 debug("fail to get sound i2s node properties\n");
103 i2s->base_address = samsung_get_base_i2s();
104 i2s->audio_pll_clk = I2S_PLL_CLK;
105 i2s->samplingrate = I2S_SAMPLING_RATE;
106 i2s->bitspersample = I2S_BITS_PER_SAMPLE;
107 i2s->channels = I2S_CHANNELS;
117 * @param blob FDT blob
118 * @param pi2s_tx i2s parameters required by codec
119 * @return int value, 0 for success
121 static int codec_init(const void *blob, struct i2stx_info *pi2s_tx)
124 const char *codectype;
125 #ifdef CONFIG_OF_CONTROL
128 /* Get the node from FDT for sound */
129 node = fdtdec_next_compatible(blob, 0, COMPAT_SAMSUNG_EXYNOS5_SOUND);
131 debug("EXYNOS_SOUND: No node for sound in device tree\n");
132 debug("node = %d\n", node);
137 * Get the pre-defined sound codec specific values from FDT.
138 * All of these are expected to be correct otherwise sound
141 codectype = fdt_getprop(blob, node, "samsung,codec-type", NULL);
142 debug("device = %s\n", codectype);
144 codectype = AUDIO_CODEC;
146 if (!strcmp(codectype, "wm8994")) {
147 /* Check the codec type and initialise the same */
148 ret = wm8994_init(blob, WM8994_AIF2,
149 pi2s_tx->samplingrate,
150 (pi2s_tx->samplingrate * (pi2s_tx->rfs)),
151 pi2s_tx->bitspersample, pi2s_tx->channels);
153 debug("%s: Unknown code type %s\n", __func__,
158 debug("%s: Codec init failed\n", __func__);
165 int sound_init(const void *blob)
168 struct i2stx_info *pi2s_tx = &g_i2stx_pri;
170 /* Get the I2S Values */
171 if (get_sound_i2s_values(pi2s_tx, blob) < 0) {
172 debug(" FDT I2S values failed\n");
176 if (codec_init(blob, pi2s_tx) < 0) {
177 debug(" Codec init failed\n");
181 ret = i2s_tx_init(pi2s_tx);
183 debug("%s: Failed to init i2c transmit: ret=%d\n", __func__,
193 * Generates square wave sound data for 1 second
195 * @param data data buffer pointer
196 * @param size size of the buffer
197 * @param freq frequency of the wave
199 static void sound_prepare_buffer(unsigned short *data, int size, uint32_t freq)
201 const int sample = 48000;
202 const unsigned short amplitude = 16000; /* between 1 and 32767 */
203 const int period = freq ? sample / freq : 0;
204 const int half = period / 2;
208 /* Make sure we don't overflow our buffer */
214 for (i = 0; size && i < half; i++) {
219 for (i = 0; size && i < period - half; i++) {
221 *data++ = -amplitude;
222 *data++ = -amplitude;
227 int sound_play(uint32_t msec, uint32_t frequency)
230 unsigned long data_size;
231 unsigned int ret = 0;
233 /*Buffer length computation */
234 data_size = g_i2stx_pri.samplingrate * g_i2stx_pri.channels;
235 data_size *= (g_i2stx_pri.bitspersample / SOUND_BITS_IN_BYTE);
236 data = malloc(data_size);
239 debug("%s: malloc failed\n", __func__);
243 sound_prepare_buffer((unsigned short *)data,
244 data_size / sizeof(unsigned short), frequency);
246 while (msec >= 1000) {
247 ret = i2s_transfer_tx_data(&g_i2stx_pri, data,
248 (data_size / sizeof(int)));
253 (data_size * msec) / (sizeof(int) * 1000);
255 ret = i2s_transfer_tx_data(&g_i2stx_pri, data, size);