]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - drivers/sound/sound.c
video: Fix splash screen alignment
[karo-tx-uboot.git] / drivers / sound / sound.c
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 <libfdt.h>
28 #include <fdtdec.h>
29 #include <i2c.h>
30 #include <i2s.h>
31 #include <sound.h>
32 #include <asm/arch/sound.h>
33 #include "wm8994.h"
34
35 /* defines */
36 #define SOUND_400_HZ 400
37 #define SOUND_BITS_IN_BYTE 8
38
39 static struct i2stx_info g_i2stx_pri;
40
41 /*
42  * get_sound_i2s_values gets values for i2s parameters
43  *
44  * @param i2stx_info    i2s transmitter transfer param structure
45  * @param blob          FDT blob if enabled else NULL
46  */
47 static int get_sound_i2s_values(struct i2stx_info *i2s, const void *blob)
48 {
49 #ifdef CONFIG_OF_CONTROL
50         int node;
51         int error = 0;
52         int base;
53
54         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");
58                 return -1;
59         }
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__);
70                 return -1;
71         }
72         i2s->base_address = base;
73
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);
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);
98         if (error == -1) {
99                 debug("fail to get sound i2s node properties\n");
100                 return -1;
101         }
102 #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
111         return 0;
112 }
113
114 /*
115  * Init codec
116  *
117  * @param blob          FDT blob
118  * @param pi2s_tx       i2s parameters required by codec
119  * @return              int value, 0 for success
120  */
121 static int codec_init(const void *blob, struct i2stx_info *pi2s_tx)
122 {
123         int ret;
124         const char *codectype;
125 #ifdef CONFIG_OF_CONTROL
126         int node;
127
128         /* 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         }
135
136         /*
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
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);
152         } else {
153                 debug("%s: Unknown code type %s\n", __func__,
154                       codectype);
155                 return -1;
156         }
157         if (ret) {
158                 debug("%s: Codec init failed\n", __func__);
159                 return -1;
160         }
161
162         return 0;
163 }
164
165 int sound_init(const void *blob)
166 {
167         int ret;
168         struct i2stx_info *pi2s_tx = &g_i2stx_pri;
169
170         /* Get the I2S Values */
171         if (get_sound_i2s_values(pi2s_tx, blob) < 0) {
172                 debug(" FDT I2S values failed\n");
173                 return -1;
174         }
175
176         if (codec_init(blob, pi2s_tx) < 0) {
177                 debug(" Codec init failed\n");
178                 return -1;
179         }
180
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
188
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  */
199 static 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
227 int 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 }