blob: 9b8ce5a9efb752c78466f2b461385e448a49e2f9 [file] [log] [blame]
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +00001/*
2 * Copyright (C) 2012 Samsung Electronics
3 * R. Chandrasekar <rcsekar@samsung.com>
4 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +00006 */
7
8#include <malloc.h>
9#include <common.h>
10#include <asm/io.h>
Rajeshwari Shinde73766212012-12-26 20:03:17 +000011#include <libfdt.h>
12#include <fdtdec.h>
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +000013#include <i2c.h>
14#include <i2s.h>
15#include <sound.h>
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +000016#include <asm/arch/sound.h>
Rajeshwari Shinde73766212012-12-26 20:03:17 +000017#include "wm8994.h"
Rajeshwari Shinde1bf58252013-02-14 19:46:13 +000018#include "max98095.h"
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +000019
20/* defines */
21#define SOUND_400_HZ 400
22#define SOUND_BITS_IN_BYTE 8
23
24static struct i2stx_info g_i2stx_pri;
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +000025
26/*
Rajeshwari Shinde73766212012-12-26 20:03:17 +000027 * get_sound_i2s_values gets values for i2s parameters
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +000028 *
29 * @param i2stx_info i2s transmitter transfer param structure
Rajeshwari Shinde73766212012-12-26 20:03:17 +000030 * @param blob FDT blob if enabled else NULL
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +000031 */
Rajeshwari Shinde73766212012-12-26 20:03:17 +000032static int get_sound_i2s_values(struct i2stx_info *i2s, const void *blob)
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +000033{
Rajeshwari Shinde73766212012-12-26 20:03:17 +000034#ifdef CONFIG_OF_CONTROL
35 int node;
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +000036 int error = 0;
Rajeshwari Shinde73766212012-12-26 20:03:17 +000037 int base;
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +000038
Dani Krishna Mohan962e08c2013-09-11 16:38:46 +053039 node = fdt_path_offset(blob, "i2s");
Rajeshwari Shinde73766212012-12-26 20:03:17 +000040 if (node <= 0) {
41 debug("EXYNOS_SOUND: No node for sound in device tree\n");
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +000042 return -1;
Rajeshwari Shinde73766212012-12-26 20:03:17 +000043 }
44
45 /*
46 * Get the pre-defined sound specific values from FDT.
47 * All of these are expected to be correct otherwise
48 * wrong register values in i2s setup parameters
49 * may result in no sound play.
50 */
51 base = fdtdec_get_addr(blob, node, "reg");
52 if (base == FDT_ADDR_T_NONE) {
53 debug("%s: Missing i2s base\n", __func__);
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +000054 return -1;
55 }
Rajeshwari Shinde73766212012-12-26 20:03:17 +000056 i2s->base_address = base;
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +000057
Rajeshwari Shinde73766212012-12-26 20:03:17 +000058 i2s->audio_pll_clk = fdtdec_get_int(blob,
59 node, "samsung,i2s-epll-clock-frequency", -1);
60 error |= i2s->audio_pll_clk;
61 debug("audio_pll_clk = %d\n", i2s->audio_pll_clk);
62 i2s->samplingrate = fdtdec_get_int(blob,
63 node, "samsung,i2s-sampling-rate", -1);
64 error |= i2s->samplingrate;
65 debug("samplingrate = %d\n", i2s->samplingrate);
66 i2s->bitspersample = fdtdec_get_int(blob,
67 node, "samsung,i2s-bits-per-sample", -1);
68 error |= i2s->bitspersample;
69 debug("bitspersample = %d\n", i2s->bitspersample);
70 i2s->channels = fdtdec_get_int(blob,
71 node, "samsung,i2s-channels", -1);
72 error |= i2s->channels;
73 debug("channels = %d\n", i2s->channels);
74 i2s->rfs = fdtdec_get_int(blob,
75 node, "samsung,i2s-lr-clk-framesize", -1);
76 error |= i2s->rfs;
77 debug("rfs = %d\n", i2s->rfs);
78 i2s->bfs = fdtdec_get_int(blob,
79 node, "samsung,i2s-bit-clk-framesize", -1);
80 error |= i2s->bfs;
81 debug("bfs = %d\n", i2s->bfs);
Dani Krishna Mohan962e08c2013-09-11 16:38:46 +053082
83 i2s->id = fdtdec_get_int(blob, node, "samsung,i2s-id", -1);
84 error |= i2s->id;
85 debug("id = %d\n", i2s->id);
86
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +000087 if (error == -1) {
Rajeshwari Shinde73766212012-12-26 20:03:17 +000088 debug("fail to get sound i2s node properties\n");
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +000089 return -1;
90 }
Rajeshwari Shinde73766212012-12-26 20:03:17 +000091#else
92 i2s->base_address = samsung_get_base_i2s();
93 i2s->audio_pll_clk = I2S_PLL_CLK;
94 i2s->samplingrate = I2S_SAMPLING_RATE;
95 i2s->bitspersample = I2S_BITS_PER_SAMPLE;
96 i2s->channels = I2S_CHANNELS;
97 i2s->rfs = I2S_RFS;
98 i2s->bfs = I2S_BFS;
Dani Krishna Mohan962e08c2013-09-11 16:38:46 +053099 i2s->id = 0;
Rajeshwari Shinde73766212012-12-26 20:03:17 +0000100#endif
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +0000101 return 0;
102}
103
104/*
Rajeshwari Shinde73766212012-12-26 20:03:17 +0000105 * Init codec
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +0000106 *
Rajeshwari Shinde73766212012-12-26 20:03:17 +0000107 * @param blob FDT blob
108 * @param pi2s_tx i2s parameters required by codec
109 * @return int value, 0 for success
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +0000110 */
Rajeshwari Shinde73766212012-12-26 20:03:17 +0000111static int codec_init(const void *blob, struct i2stx_info *pi2s_tx)
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +0000112{
Rajeshwari Shinde73766212012-12-26 20:03:17 +0000113 int ret;
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +0000114 const char *codectype;
Rajeshwari Shinde73766212012-12-26 20:03:17 +0000115#ifdef CONFIG_OF_CONTROL
116 int node;
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +0000117
Rajeshwari Shinde73766212012-12-26 20:03:17 +0000118 /* Get the node from FDT for sound */
Dani Krishna Mohan6e194902013-09-11 16:38:50 +0530119 node = fdt_path_offset(blob, "i2s");
Rajeshwari Shinde73766212012-12-26 20:03:17 +0000120 if (node <= 0) {
121 debug("EXYNOS_SOUND: No node for sound in device tree\n");
122 debug("node = %d\n", node);
123 return -1;
124 }
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +0000125
Rajeshwari Shinde73766212012-12-26 20:03:17 +0000126 /*
127 * Get the pre-defined sound codec specific values from FDT.
128 * All of these are expected to be correct otherwise sound
129 * can not be played
130 */
131 codectype = fdt_getprop(blob, node, "samsung,codec-type", NULL);
132 debug("device = %s\n", codectype);
133#else
134 codectype = AUDIO_CODEC;
135#endif
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +0000136 if (!strcmp(codectype, "wm8994")) {
Rajeshwari Shinde73766212012-12-26 20:03:17 +0000137 /* Check the codec type and initialise the same */
Dani Krishna Mohan962e08c2013-09-11 16:38:46 +0530138 ret = wm8994_init(blob, pi2s_tx->id + 1,
Dani Krishna Mohan6e194902013-09-11 16:38:50 +0530139 pi2s_tx->samplingrate,
140 (pi2s_tx->samplingrate * (pi2s_tx->rfs)),
141 pi2s_tx->bitspersample, pi2s_tx->channels);
Rajeshwari Shinde1bf58252013-02-14 19:46:13 +0000142 } else if (!strcmp(codectype, "max98095")) {
Dani Krishna Mohan6e194902013-09-11 16:38:50 +0530143 ret = max98095_init(blob, pi2s_tx->id + 1,
144 pi2s_tx->samplingrate,
145 (pi2s_tx->samplingrate * (pi2s_tx->rfs)),
146 pi2s_tx->bitspersample);
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +0000147 } else {
Rajeshwari Shinde1bf58252013-02-14 19:46:13 +0000148 debug("%s: Unknown codec type %s\n", __func__, codectype);
Rajeshwari Shinde73766212012-12-26 20:03:17 +0000149 return -1;
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +0000150 }
Rajeshwari Shinde1bf58252013-02-14 19:46:13 +0000151
Rajeshwari Shinde73766212012-12-26 20:03:17 +0000152 if (ret) {
153 debug("%s: Codec init failed\n", __func__);
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +0000154 return -1;
155 }
156
157 return 0;
158}
159
Rajeshwari Shinde73766212012-12-26 20:03:17 +0000160int sound_init(const void *blob)
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +0000161{
162 int ret;
163 struct i2stx_info *pi2s_tx = &g_i2stx_pri;
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +0000164
165 /* Get the I2S Values */
Rajeshwari Shinde73766212012-12-26 20:03:17 +0000166 if (get_sound_i2s_values(pi2s_tx, blob) < 0) {
167 debug(" FDT I2S values failed\n");
168 return -1;
169 }
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +0000170
Rajeshwari Shinde73766212012-12-26 20:03:17 +0000171 if (codec_init(blob, pi2s_tx) < 0) {
172 debug(" Codec init failed\n");
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +0000173 return -1;
Rajeshwari Shinde73766212012-12-26 20:03:17 +0000174 }
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +0000175
176 ret = i2s_tx_init(pi2s_tx);
177 if (ret) {
178 debug("%s: Failed to init i2c transmit: ret=%d\n", __func__,
179 ret);
180 return ret;
181 }
182
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +0000183
184 return ret;
185}
186
187/*
188 * Generates square wave sound data for 1 second
189 *
190 * @param data data buffer pointer
191 * @param size size of the buffer
192 * @param freq frequency of the wave
193 */
194static void sound_prepare_buffer(unsigned short *data, int size, uint32_t freq)
195{
196 const int sample = 48000;
197 const unsigned short amplitude = 16000; /* between 1 and 32767 */
198 const int period = freq ? sample / freq : 0;
199 const int half = period / 2;
200
201 assert(freq);
202
203 /* Make sure we don't overflow our buffer */
204 if (size % 2)
205 size--;
206
207 while (size) {
208 int i;
209 for (i = 0; size && i < half; i++) {
210 size -= 2;
211 *data++ = amplitude;
212 *data++ = amplitude;
213 }
214 for (i = 0; size && i < period - half; i++) {
215 size -= 2;
216 *data++ = -amplitude;
217 *data++ = -amplitude;
218 }
219 }
220}
221
222int sound_play(uint32_t msec, uint32_t frequency)
223{
224 unsigned int *data;
225 unsigned long data_size;
226 unsigned int ret = 0;
227
228 /*Buffer length computation */
229 data_size = g_i2stx_pri.samplingrate * g_i2stx_pri.channels;
230 data_size *= (g_i2stx_pri.bitspersample / SOUND_BITS_IN_BYTE);
231 data = malloc(data_size);
232
233 if (data == NULL) {
234 debug("%s: malloc failed\n", __func__);
235 return -1;
236 }
237
238 sound_prepare_buffer((unsigned short *)data,
Dani Krishna Mohan6e194902013-09-11 16:38:50 +0530239 data_size / sizeof(unsigned short), frequency);
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +0000240
241 while (msec >= 1000) {
242 ret = i2s_transfer_tx_data(&g_i2stx_pri, data,
243 (data_size / sizeof(int)));
244 msec -= 1000;
245 }
246 if (msec) {
247 unsigned long size =
248 (data_size * msec) / (sizeof(int) * 1000);
249
250 ret = i2s_transfer_tx_data(&g_i2stx_pri, data, size);
251 }
252
253 free(data);
254
255 return ret;
256}