blob: a2fc78154f176b3b1ff81e403bc222c30cf46652 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassa77bf702014-02-27 13:26:20 -07002/*
3 * Copyright (C) 2012 Samsung Electronics
4 * R. Chandrasekar <rcsekar@samsung.com>
Simon Glassa77bf702014-02-27 13:26:20 -07005 */
6
7#include <malloc.h>
8#include <common.h>
9#include <asm/io.h>
Masahiro Yamadab08c8c42018-03-05 01:20:11 +090010#include <linux/libfdt.h>
Simon Glassa77bf702014-02-27 13:26:20 -070011#include <fdtdec.h>
12#include <i2c.h>
13#include <i2s.h>
14#include <sound.h>
15#include <asm/arch/sound.h>
16#include "wm8994.h"
17#include "max98095.h"
18
19/* defines */
20#define SOUND_400_HZ 400
21#define SOUND_BITS_IN_BYTE 8
22
Simon Glass45863db2018-12-10 10:37:32 -070023static struct i2s_uc_priv g_i2stx_pri;
Simon Glassa77bf702014-02-27 13:26:20 -070024
25/*
26 * get_sound_i2s_values gets values for i2s parameters
27 *
Simon Glass45863db2018-12-10 10:37:32 -070028 * @param i2s_uc_priv i2s transmitter transfer param structure
Simon Glassa77bf702014-02-27 13:26:20 -070029 * @param blob FDT blob if enabled else NULL
30 */
Simon Glass45863db2018-12-10 10:37:32 -070031static int get_sound_i2s_values(struct i2s_uc_priv *i2s, const void *blob)
Simon Glassa77bf702014-02-27 13:26:20 -070032{
33 int node;
34 int error = 0;
35 int base;
36
37 node = fdt_path_offset(blob, "i2s");
38 if (node <= 0) {
39 debug("EXYNOS_SOUND: No node for sound in device tree\n");
40 return -1;
41 }
42
43 /*
44 * Get the pre-defined sound specific values from FDT.
45 * All of these are expected to be correct otherwise
46 * wrong register values in i2s setup parameters
47 * may result in no sound play.
48 */
49 base = fdtdec_get_addr(blob, node, "reg");
50 if (base == FDT_ADDR_T_NONE) {
51 debug("%s: Missing i2s base\n", __func__);
52 return -1;
53 }
54 i2s->base_address = base;
55
56 i2s->audio_pll_clk = fdtdec_get_int(blob,
57 node, "samsung,i2s-epll-clock-frequency", -1);
58 error |= i2s->audio_pll_clk;
59 debug("audio_pll_clk = %d\n", i2s->audio_pll_clk);
60 i2s->samplingrate = fdtdec_get_int(blob,
61 node, "samsung,i2s-sampling-rate", -1);
62 error |= i2s->samplingrate;
63 debug("samplingrate = %d\n", i2s->samplingrate);
64 i2s->bitspersample = fdtdec_get_int(blob,
65 node, "samsung,i2s-bits-per-sample", -1);
66 error |= i2s->bitspersample;
67 debug("bitspersample = %d\n", i2s->bitspersample);
68 i2s->channels = fdtdec_get_int(blob,
69 node, "samsung,i2s-channels", -1);
70 error |= i2s->channels;
71 debug("channels = %d\n", i2s->channels);
72 i2s->rfs = fdtdec_get_int(blob,
73 node, "samsung,i2s-lr-clk-framesize", -1);
74 error |= i2s->rfs;
75 debug("rfs = %d\n", i2s->rfs);
76 i2s->bfs = fdtdec_get_int(blob,
77 node, "samsung,i2s-bit-clk-framesize", -1);
78 error |= i2s->bfs;
79 debug("bfs = %d\n", i2s->bfs);
80
81 i2s->id = fdtdec_get_int(blob, node, "samsung,i2s-id", -1);
82 error |= i2s->id;
83 debug("id = %d\n", i2s->id);
84
85 if (error == -1) {
86 debug("fail to get sound i2s node properties\n");
87 return -1;
88 }
89
90 return 0;
91}
92
93/*
94 * Init codec
95 *
96 * @param blob FDT blob
97 * @param pi2s_tx i2s parameters required by codec
98 * @return int value, 0 for success
99 */
Simon Glass45863db2018-12-10 10:37:32 -0700100static int codec_init(const void *blob, struct i2s_uc_priv *pi2s_tx)
Simon Glassa77bf702014-02-27 13:26:20 -0700101{
102 int ret;
103 const char *codectype;
104 int node;
105
106 /* Get the node from FDT for sound */
107 node = fdt_path_offset(blob, "i2s");
108 if (node <= 0) {
109 debug("EXYNOS_SOUND: No node for sound in device tree\n");
110 debug("node = %d\n", node);
111 return -1;
112 }
113
114 /*
115 * Get the pre-defined sound codec specific values from FDT.
116 * All of these are expected to be correct otherwise sound
117 * can not be played
118 */
119 codectype = fdt_getprop(blob, node, "samsung,codec-type", NULL);
120 debug("device = %s\n", codectype);
121 if (!strcmp(codectype, "wm8994")) {
122 /* Check the codec type and initialise the same */
Simon Glass6c986cf2018-12-10 10:37:38 -0700123 ret = wm8994_init(blob, pi2s_tx->id, pi2s_tx->samplingrate,
Simon Glassa77bf702014-02-27 13:26:20 -0700124 (pi2s_tx->samplingrate * (pi2s_tx->rfs)),
125 pi2s_tx->bitspersample, pi2s_tx->channels);
126 } else if (!strcmp(codectype, "max98095")) {
Simon Glass6c986cf2018-12-10 10:37:38 -0700127 ret = max98095_init(blob, pi2s_tx->id, pi2s_tx->samplingrate,
Simon Glassa77bf702014-02-27 13:26:20 -0700128 (pi2s_tx->samplingrate * (pi2s_tx->rfs)),
129 pi2s_tx->bitspersample);
130 } else {
131 debug("%s: Unknown codec type %s\n", __func__, codectype);
132 return -1;
133 }
134
135 if (ret) {
136 debug("%s: Codec init failed\n", __func__);
137 return -1;
138 }
139
140 return 0;
141}
142
143int sound_init(const void *blob)
144{
145 int ret;
Simon Glass45863db2018-12-10 10:37:32 -0700146 struct i2s_uc_priv *pi2s_tx = &g_i2stx_pri;
Simon Glassa77bf702014-02-27 13:26:20 -0700147
148 /* Get the I2S Values */
149 if (get_sound_i2s_values(pi2s_tx, blob) < 0) {
150 debug(" FDT I2S values failed\n");
151 return -1;
152 }
153
154 if (codec_init(blob, pi2s_tx) < 0) {
155 debug(" Codec init failed\n");
156 return -1;
157 }
158
159 ret = i2s_tx_init(pi2s_tx);
160 if (ret) {
161 debug("%s: Failed to init i2c transmit: ret=%d\n", __func__,
162 ret);
163 return ret;
164 }
165
166
167 return ret;
168}
169
170int sound_play(uint32_t msec, uint32_t frequency)
171{
172 unsigned int *data;
173 unsigned long data_size;
174 unsigned int ret = 0;
175
176 /*Buffer length computation */
177 data_size = g_i2stx_pri.samplingrate * g_i2stx_pri.channels;
178 data_size *= (g_i2stx_pri.bitspersample / SOUND_BITS_IN_BYTE);
179 data = malloc(data_size);
180
181 if (data == NULL) {
182 debug("%s: malloc failed\n", __func__);
183 return -1;
184 }
185
Simon Glass7d92b062018-11-15 19:56:13 -0700186 sound_create_square_wave(g_i2stx_pri.samplingrate,
187 (unsigned short *)data,
Simon Glassa77bf702014-02-27 13:26:20 -0700188 data_size / sizeof(unsigned short),
189 frequency);
190
191 while (msec >= 1000) {
192 ret = i2s_transfer_tx_data(&g_i2stx_pri, data,
193 (data_size / sizeof(int)));
194 msec -= 1000;
195 }
196 if (msec) {
197 unsigned long size =
198 (data_size * msec) / (sizeof(int) * 1000);
199
200 ret = i2s_transfer_tx_data(&g_i2stx_pri, data, size);
201 }
202
203 free(data);
204
205 return ret;
206}