blob: a4bf4adcb38bf744d379f84613006483e22e7119 [file] [log] [blame]
Rajeshwari Shinde511ed5f2012-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 Shindef4823102012-12-26 20:03:17 +000027#include <libfdt.h>
28#include <fdtdec.h>
Rajeshwari Shinde511ed5f2012-10-25 19:49:22 +000029#include <i2c.h>
30#include <i2s.h>
31#include <sound.h>
Rajeshwari Shinde511ed5f2012-10-25 19:49:22 +000032#include <asm/arch/sound.h>
Rajeshwari Shindef4823102012-12-26 20:03:17 +000033#include "wm8994.h"
Rajeshwari Shinde14d2dfc2013-02-14 19:46:13 +000034#include "max98095.h"
Rajeshwari Shinde511ed5f2012-10-25 19:49:22 +000035
36/* defines */
37#define SOUND_400_HZ 400
38#define SOUND_BITS_IN_BYTE 8
39
40static struct i2stx_info g_i2stx_pri;
Rajeshwari Shinde511ed5f2012-10-25 19:49:22 +000041
42/*
Rajeshwari Shindef4823102012-12-26 20:03:17 +000043 * get_sound_i2s_values gets values for i2s parameters
Rajeshwari Shinde511ed5f2012-10-25 19:49:22 +000044 *
45 * @param i2stx_info i2s transmitter transfer param structure
Rajeshwari Shindef4823102012-12-26 20:03:17 +000046 * @param blob FDT blob if enabled else NULL
Rajeshwari Shinde511ed5f2012-10-25 19:49:22 +000047 */
Rajeshwari Shindef4823102012-12-26 20:03:17 +000048static int get_sound_i2s_values(struct i2stx_info *i2s, const void *blob)
Rajeshwari Shinde511ed5f2012-10-25 19:49:22 +000049{
Rajeshwari Shindef4823102012-12-26 20:03:17 +000050#ifdef CONFIG_OF_CONTROL
51 int node;
52 int error = 0;
53 int base;
54
55 node = fdtdec_next_compatible(blob, 0,
56 COMPAT_SAMSUNG_EXYNOS5_SOUND);
57 if (node <= 0) {
58 debug("EXYNOS_SOUND: No node for sound in device tree\n");
59 return -1;
60 }
61
62 /*
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.
67 */
68 base = fdtdec_get_addr(blob, node, "reg");
69 if (base == FDT_ADDR_T_NONE) {
70 debug("%s: Missing i2s base\n", __func__);
71 return -1;
72 }
73 i2s->base_address = base;
74
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);
93 error |= i2s->rfs;
94 debug("rfs = %d\n", i2s->rfs);
95 i2s->bfs = fdtdec_get_int(blob,
96 node, "samsung,i2s-bit-clk-framesize", -1);
97 error |= i2s->bfs;
98 debug("bfs = %d\n", i2s->bfs);
99 if (error == -1) {
100 debug("fail to get sound i2s node properties\n");
101 return -1;
102 }
103#else
Rajeshwari Shinde511ed5f2012-10-25 19:49:22 +0000104 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;
109 i2s->rfs = I2S_RFS;
110 i2s->bfs = I2S_BFS;
Rajeshwari Shindef4823102012-12-26 20:03:17 +0000111#endif
Rajeshwari Shinde511ed5f2012-10-25 19:49:22 +0000112 return 0;
113}
114
115/*
Rajeshwari Shindef4823102012-12-26 20:03:17 +0000116 * Init codec
Rajeshwari Shinde511ed5f2012-10-25 19:49:22 +0000117 *
Rajeshwari Shindef4823102012-12-26 20:03:17 +0000118 * @param blob FDT blob
119 * @param pi2s_tx i2s parameters required by codec
120 * @return int value, 0 for success
Rajeshwari Shinde511ed5f2012-10-25 19:49:22 +0000121 */
Rajeshwari Shindef4823102012-12-26 20:03:17 +0000122static int codec_init(const void *blob, struct i2stx_info *pi2s_tx)
Rajeshwari Shinde511ed5f2012-10-25 19:49:22 +0000123{
Rajeshwari Shindef4823102012-12-26 20:03:17 +0000124 int ret;
Rajeshwari Shinde511ed5f2012-10-25 19:49:22 +0000125 const char *codectype;
Rajeshwari Shindef4823102012-12-26 20:03:17 +0000126#ifdef CONFIG_OF_CONTROL
127 int node;
Rajeshwari Shinde511ed5f2012-10-25 19:49:22 +0000128
Rajeshwari Shindef4823102012-12-26 20:03:17 +0000129 /* Get the node from FDT for sound */
130 node = fdtdec_next_compatible(blob, 0, COMPAT_SAMSUNG_EXYNOS5_SOUND);
131 if (node <= 0) {
132 debug("EXYNOS_SOUND: No node for sound in device tree\n");
133 debug("node = %d\n", node);
134 return -1;
Rajeshwari Shinde511ed5f2012-10-25 19:49:22 +0000135 }
136
Rajeshwari Shindef4823102012-12-26 20:03:17 +0000137 /*
138 * Get the pre-defined sound codec specific values from FDT.
139 * All of these are expected to be correct otherwise sound
140 * can not be played
141 */
142 codectype = fdt_getprop(blob, node, "samsung,codec-type", NULL);
143 debug("device = %s\n", codectype);
144#else
145 codectype = AUDIO_CODEC;
146#endif
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);
Rajeshwari Shinde14d2dfc2013-02-14 19:46:13 +0000153 } else if (!strcmp(codectype, "max98095")) {
154 ret = max98095_init(blob, pi2s_tx->samplingrate,
155 (pi2s_tx->samplingrate * (pi2s_tx->rfs)),
156 pi2s_tx->bitspersample);
Rajeshwari Shindef4823102012-12-26 20:03:17 +0000157 } else {
Rajeshwari Shinde14d2dfc2013-02-14 19:46:13 +0000158 debug("%s: Unknown codec type %s\n", __func__, codectype);
Rajeshwari Shindef4823102012-12-26 20:03:17 +0000159 return -1;
160 }
Rajeshwari Shinde14d2dfc2013-02-14 19:46:13 +0000161
Rajeshwari Shindef4823102012-12-26 20:03:17 +0000162 if (ret) {
163 debug("%s: Codec init failed\n", __func__);
Rajeshwari Shinde511ed5f2012-10-25 19:49:22 +0000164 return -1;
165 }
166
167 return 0;
168}
169
Rajeshwari Shindef4823102012-12-26 20:03:17 +0000170int sound_init(const void *blob)
Rajeshwari Shinde511ed5f2012-10-25 19:49:22 +0000171{
172 int ret;
173 struct i2stx_info *pi2s_tx = &g_i2stx_pri;
Rajeshwari Shinde511ed5f2012-10-25 19:49:22 +0000174
175 /* Get the I2S Values */
Rajeshwari Shindef4823102012-12-26 20:03:17 +0000176 if (get_sound_i2s_values(pi2s_tx, blob) < 0) {
177 debug(" FDT I2S values failed\n");
Rajeshwari Shinde511ed5f2012-10-25 19:49:22 +0000178 return -1;
Rajeshwari Shindef4823102012-12-26 20:03:17 +0000179 }
180
181 if (codec_init(blob, pi2s_tx) < 0) {
182 debug(" Codec init failed\n");
183 return -1;
184 }
Rajeshwari Shinde511ed5f2012-10-25 19:49:22 +0000185
186 ret = i2s_tx_init(pi2s_tx);
187 if (ret) {
188 debug("%s: Failed to init i2c transmit: ret=%d\n", __func__,
189 ret);
190 return ret;
191 }
192
Rajeshwari Shinde511ed5f2012-10-25 19:49:22 +0000193
194 return ret;
195}
196
197/*
198 * Generates square wave sound data for 1 second
199 *
200 * @param data data buffer pointer
201 * @param size size of the buffer
202 * @param freq frequency of the wave
203 */
204static void sound_prepare_buffer(unsigned short *data, int size, uint32_t freq)
205{
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;
210
211 assert(freq);
212
213 /* Make sure we don't overflow our buffer */
214 if (size % 2)
215 size--;
216
217 while (size) {
218 int i;
219 for (i = 0; size && i < half; i++) {
220 size -= 2;
221 *data++ = amplitude;
222 *data++ = amplitude;
223 }
224 for (i = 0; size && i < period - half; i++) {
225 size -= 2;
226 *data++ = -amplitude;
227 *data++ = -amplitude;
228 }
229 }
230}
231
232int sound_play(uint32_t msec, uint32_t frequency)
233{
234 unsigned int *data;
235 unsigned long data_size;
236 unsigned int ret = 0;
237
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);
242
243 if (data == NULL) {
244 debug("%s: malloc failed\n", __func__);
245 return -1;
246 }
247
248 sound_prepare_buffer((unsigned short *)data,
249 data_size / sizeof(unsigned short), frequency);
250
251 while (msec >= 1000) {
252 ret = i2s_transfer_tx_data(&g_i2stx_pri, data,
253 (data_size / sizeof(int)));
254 msec -= 1000;
255 }
256 if (msec) {
257 unsigned long size =
258 (data_size * msec) / (sizeof(int) * 1000);
259
260 ret = i2s_transfer_tx_data(&g_i2stx_pri, data, size);
261 }
262
263 free(data);
264
265 return ret;
266}