blob: fa8432d48acedc2acc0fc341b90fee62eb9e980c [file] [log] [blame]
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +00001/*
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>
Rajeshwari Shinde73766212012-12-26 20:03:17 +000027#include <libfdt.h>
28#include <fdtdec.h>
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +000029#include <i2c.h>
30#include <i2s.h>
31#include <sound.h>
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +000032#include <asm/arch/sound.h>
Rajeshwari Shinde73766212012-12-26 20:03:17 +000033#include "wm8994.h"
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +000034
35/* defines */
36#define SOUND_400_HZ 400
37#define SOUND_BITS_IN_BYTE 8
38
39static struct i2stx_info g_i2stx_pri;
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +000040
41/*
Rajeshwari Shinde73766212012-12-26 20:03:17 +000042 * get_sound_i2s_values gets values for i2s parameters
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +000043 *
44 * @param i2stx_info i2s transmitter transfer param structure
Rajeshwari Shinde73766212012-12-26 20:03:17 +000045 * @param blob FDT blob if enabled else NULL
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +000046 */
Rajeshwari Shinde73766212012-12-26 20:03:17 +000047static int get_sound_i2s_values(struct i2stx_info *i2s, const void *blob)
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +000048{
Rajeshwari Shinde73766212012-12-26 20:03:17 +000049#ifdef CONFIG_OF_CONTROL
50 int node;
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +000051 int error = 0;
Rajeshwari Shinde73766212012-12-26 20:03:17 +000052 int base;
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +000053
Rajeshwari Shinde73766212012-12-26 20:03:17 +000054 node = fdtdec_next_compatible(blob, 0,
55 COMPAT_SAMSUNG_EXYNOS5_SOUND);
56 if (node <= 0) {
57 debug("EXYNOS_SOUND: No node for sound in device tree\n");
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +000058 return -1;
Rajeshwari Shinde73766212012-12-26 20:03:17 +000059 }
60
61 /*
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.
66 */
67 base = fdtdec_get_addr(blob, node, "reg");
68 if (base == FDT_ADDR_T_NONE) {
69 debug("%s: Missing i2s base\n", __func__);
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +000070 return -1;
71 }
Rajeshwari Shinde73766212012-12-26 20:03:17 +000072 i2s->base_address = base;
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +000073
Rajeshwari Shinde73766212012-12-26 20:03:17 +000074 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);
92 error |= i2s->rfs;
93 debug("rfs = %d\n", i2s->rfs);
94 i2s->bfs = fdtdec_get_int(blob,
95 node, "samsung,i2s-bit-clk-framesize", -1);
96 error |= i2s->bfs;
97 debug("bfs = %d\n", i2s->bfs);
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +000098 if (error == -1) {
Rajeshwari Shinde73766212012-12-26 20:03:17 +000099 debug("fail to get sound i2s node properties\n");
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +0000100 return -1;
101 }
Rajeshwari Shinde73766212012-12-26 20:03:17 +0000102#else
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;
108 i2s->rfs = I2S_RFS;
109 i2s->bfs = I2S_BFS;
110#endif
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +0000111 return 0;
112}
113
114/*
Rajeshwari Shinde73766212012-12-26 20:03:17 +0000115 * Init codec
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +0000116 *
Rajeshwari Shinde73766212012-12-26 20:03:17 +0000117 * @param blob FDT blob
118 * @param pi2s_tx i2s parameters required by codec
119 * @return int value, 0 for success
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +0000120 */
Rajeshwari Shinde73766212012-12-26 20:03:17 +0000121static int codec_init(const void *blob, struct i2stx_info *pi2s_tx)
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +0000122{
Rajeshwari Shinde73766212012-12-26 20:03:17 +0000123 int ret;
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +0000124 const char *codectype;
Rajeshwari Shinde73766212012-12-26 20:03:17 +0000125#ifdef CONFIG_OF_CONTROL
126 int node;
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +0000127
Rajeshwari Shinde73766212012-12-26 20:03:17 +0000128 /* Get the node from FDT for sound */
129 node = fdtdec_next_compatible(blob, 0, COMPAT_SAMSUNG_EXYNOS5_SOUND);
130 if (node <= 0) {
131 debug("EXYNOS_SOUND: No node for sound in device tree\n");
132 debug("node = %d\n", node);
133 return -1;
134 }
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +0000135
Rajeshwari Shinde73766212012-12-26 20:03:17 +0000136 /*
137 * Get the pre-defined sound codec specific values from FDT.
138 * All of these are expected to be correct otherwise sound
139 * can not be played
140 */
141 codectype = fdt_getprop(blob, node, "samsung,codec-type", NULL);
142 debug("device = %s\n", codectype);
143#else
144 codectype = AUDIO_CODEC;
145#endif
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +0000146 if (!strcmp(codectype, "wm8994")) {
Rajeshwari Shinde73766212012-12-26 20:03:17 +0000147 /* 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);
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +0000152 } else {
Rajeshwari Shinde73766212012-12-26 20:03:17 +0000153 debug("%s: Unknown code type %s\n", __func__,
154 codectype);
155 return -1;
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +0000156 }
Rajeshwari Shinde73766212012-12-26 20:03:17 +0000157 if (ret) {
158 debug("%s: Codec init failed\n", __func__);
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +0000159 return -1;
160 }
161
162 return 0;
163}
164
Rajeshwari Shinde73766212012-12-26 20:03:17 +0000165int sound_init(const void *blob)
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +0000166{
167 int ret;
168 struct i2stx_info *pi2s_tx = &g_i2stx_pri;
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +0000169
170 /* Get the I2S Values */
Rajeshwari Shinde73766212012-12-26 20:03:17 +0000171 if (get_sound_i2s_values(pi2s_tx, blob) < 0) {
172 debug(" FDT I2S values failed\n");
173 return -1;
174 }
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +0000175
Rajeshwari Shinde73766212012-12-26 20:03:17 +0000176 if (codec_init(blob, pi2s_tx) < 0) {
177 debug(" Codec init failed\n");
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +0000178 return -1;
Rajeshwari Shinde73766212012-12-26 20:03:17 +0000179 }
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +0000180
181 ret = i2s_tx_init(pi2s_tx);
182 if (ret) {
183 debug("%s: Failed to init i2c transmit: ret=%d\n", __func__,
184 ret);
185 return ret;
186 }
187
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +0000188
189 return ret;
190}
191
192/*
193 * Generates square wave sound data for 1 second
194 *
195 * @param data data buffer pointer
196 * @param size size of the buffer
197 * @param freq frequency of the wave
198 */
199static void sound_prepare_buffer(unsigned short *data, int size, uint32_t freq)
200{
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;
205
206 assert(freq);
207
208 /* Make sure we don't overflow our buffer */
209 if (size % 2)
210 size--;
211
212 while (size) {
213 int i;
214 for (i = 0; size && i < half; i++) {
215 size -= 2;
216 *data++ = amplitude;
217 *data++ = amplitude;
218 }
219 for (i = 0; size && i < period - half; i++) {
220 size -= 2;
221 *data++ = -amplitude;
222 *data++ = -amplitude;
223 }
224 }
225}
226
227int sound_play(uint32_t msec, uint32_t frequency)
228{
229 unsigned int *data;
230 unsigned long data_size;
231 unsigned int ret = 0;
232
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);
237
238 if (data == NULL) {
239 debug("%s: malloc failed\n", __func__);
240 return -1;
241 }
242
243 sound_prepare_buffer((unsigned short *)data,
244 data_size / sizeof(unsigned short), frequency);
245
246 while (msec >= 1000) {
247 ret = i2s_transfer_tx_data(&g_i2stx_pri, data,
248 (data_size / sizeof(int)));
249 msec -= 1000;
250 }
251 if (msec) {
252 unsigned long size =
253 (data_size * msec) / (sizeof(int) * 1000);
254
255 ret = i2s_transfer_tx_data(&g_i2stx_pri, data, size);
256 }
257
258 free(data);
259
260 return ret;
261}