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,
31 #include <asm/arch/sound.h>
34 #define SOUND_400_HZ 400
35 #define SOUND_BITS_IN_BYTE 8
37 static struct i2stx_info g_i2stx_pri
;
38 static struct sound_codec_info g_codec_info
;
41 * get_sound_fdt_values gets fdt values for i2s parameters
43 * @param i2stx_info i2s transmitter transfer param structure
44 * @param blob FDT blob
46 static void get_sound_i2s_values(struct i2stx_info
*i2s
)
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
;
58 * Gets fdt values for wm8994 config parameters
60 * @param pcodec_info codec information structure
61 * @param blob FDT blob
62 * @return int value, 0 for success
64 static int get_sound_wm8994_values(struct sound_codec_info
*pcodec_info
)
68 switch (AUDIO_COMPAT
) {
69 case AUDIO_COMPAT_SPI
:
70 debug("%s: Support not added for SPI interface\n", __func__
);
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
);
79 debug("%s: Unknown compat id %d\n", __func__
, AUDIO_COMPAT
);
84 debug("fail to get wm8994 codec node properties\n");
92 * Gets fdt values for codec config parameters
94 * @param pcodec_info codec information structure
95 * @param blob FDT blob
96 * @return int value, 0 for success
98 static int get_sound_codec_values(struct sound_codec_info
*pcodec_info
)
101 const char *codectype
;
103 codectype
= AUDIO_CODEC
;
105 if (!strcmp(codectype
, "wm8994")) {
106 pcodec_info
->codec_type
= CODEC_WM_8994
;
107 error
= get_sound_wm8994_values(pcodec_info
);
113 debug("fail to get sound codec node properties\n");
123 struct i2stx_info
*pi2s_tx
= &g_i2stx_pri
;
124 struct sound_codec_info
*pcodec_info
= &g_codec_info
;
126 /* Get the I2S Values */
127 get_sound_i2s_values(pi2s_tx
);
129 /* Get the codec Values */
130 if (get_sound_codec_values(pcodec_info
) < 0)
133 ret
= i2s_tx_init(pi2s_tx
);
135 debug("%s: Failed to init i2c transmit: ret=%d\n", __func__
,
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
);
147 debug("%s: Unknown code type %d\n", __func__
,
148 pcodec_info
->codec_type
);
152 debug("%s: Codec init failed\n", __func__
);
160 * Generates square wave sound data for 1 second
162 * @param data data buffer pointer
163 * @param size size of the buffer
164 * @param freq frequency of the wave
166 static void sound_prepare_buffer(unsigned short *data
, int size
, uint32_t freq
)
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;
175 /* Make sure we don't overflow our buffer */
181 for (i
= 0; size
&& i
< half
; i
++) {
186 for (i
= 0; size
&& i
< period
- half
; i
++) {
188 *data
++ = -amplitude
;
189 *data
++ = -amplitude
;
194 int sound_play(uint32_t msec
, uint32_t frequency
)
197 unsigned long data_size
;
198 unsigned int ret
= 0;
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
);
206 debug("%s: malloc failed\n", __func__
);
210 sound_prepare_buffer((unsigned short *)data
,
211 data_size
/ sizeof(unsigned short), frequency
);
213 while (msec
>= 1000) {
214 ret
= i2s_transfer_tx_data(&g_i2stx_pri
, data
,
215 (data_size
/ sizeof(int)));
220 (data_size
* msec
) / (sizeof(int) * 1000);
222 ret
= i2s_transfer_tx_data(&g_i2stx_pri
, data
, size
);