blob: 30d1470102c29c4821d20bbd18c075ccc1d75e66 [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 <asm/arch/clk.h>
9#include <asm/arch/pinmux.h>
10#include <asm/arch/i2s-regs.h>
11#include <asm/io.h>
12#include <common.h>
13#include <sound.h>
14#include <i2s.h>
15
16#define FIC_TX2COUNT(x) (((x) >> 24) & 0xf)
17#define FIC_TX1COUNT(x) (((x) >> 16) & 0xf)
18#define FIC_TXCOUNT(x) (((x) >> 8) & 0xf)
19#define FIC_RXCOUNT(x) (((x) >> 0) & 0xf)
20#define FICS_TXCOUNT(x) (((x) >> 8) & 0x7f)
21
22#define TIMEOUT_I2S_TX 100 /* i2s transfer timeout */
23
24/*
25 * Sets the frame size for I2S LR clock
26 *
27 * @param i2s_reg i2s regiter address
28 * @param rfs Frame Size
29 */
30static void i2s_set_lr_framesize(struct i2s_reg *i2s_reg, unsigned int rfs)
31{
32 unsigned int mod = readl(&i2s_reg->mod);
33
34 mod &= ~MOD_RCLK_MASK;
35
36 switch (rfs) {
37 case 768:
38 mod |= MOD_RCLK_768FS;
39 break;
40 case 512:
41 mod |= MOD_RCLK_512FS;
42 break;
43 case 384:
44 mod |= MOD_RCLK_384FS;
45 break;
46 default:
47 mod |= MOD_RCLK_256FS;
48 break;
49 }
50
51 writel(mod, &i2s_reg->mod);
52}
53
54/*
55 * Sets the i2s transfer control
56 *
57 * @param i2s_reg i2s regiter address
58 * @param on 1 enable tx , 0 disable tx transfer
59 */
60static void i2s_txctrl(struct i2s_reg *i2s_reg, int on)
61{
62 unsigned int con = readl(&i2s_reg->con);
63 unsigned int mod = readl(&i2s_reg->mod) & ~MOD_MASK;
64
65 if (on) {
66 con |= CON_ACTIVE;
67 con &= ~CON_TXCH_PAUSE;
68
69 } else {
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +000070 con |= CON_TXCH_PAUSE;
71 con &= ~CON_ACTIVE;
72 }
73
74 writel(mod, &i2s_reg->mod);
75 writel(con, &i2s_reg->con);
76}
77
78/*
79 * set the bit clock frame size (in multiples of LRCLK)
80 *
81 * @param i2s_reg i2s regiter address
82 * @param bfs bit Frame Size
83 */
84static void i2s_set_bitclk_framesize(struct i2s_reg *i2s_reg, unsigned bfs)
85{
86 unsigned int mod = readl(&i2s_reg->mod);
87
88 mod &= ~MOD_BCLK_MASK;
89
90 switch (bfs) {
91 case 48:
92 mod |= MOD_BCLK_48FS;
93 break;
94 case 32:
95 mod |= MOD_BCLK_32FS;
96 break;
97 case 24:
98 mod |= MOD_BCLK_24FS;
99 break;
100 case 16:
101 mod |= MOD_BCLK_16FS;
102 break;
103 default:
104 return;
105 }
106 writel(mod, &i2s_reg->mod);
107}
108
109/*
110 * flushes the i2stx fifo
111 *
112 * @param i2s_reg i2s regiter address
113 * @param flush Tx fifo flush command (0x00 - do not flush
114 * 0x80 - flush tx fifo)
115 */
116void i2s_fifo(struct i2s_reg *i2s_reg, unsigned int flush)
117{
118 /* Flush the FIFO */
119 setbits_le32(&i2s_reg->fic, flush);
120 clrbits_le32(&i2s_reg->fic, flush);
121}
122
123/*
124 * Set System Clock direction
125 *
126 * @param i2s_reg i2s regiter address
127 * @param dir Clock direction
128 *
129 * @return int value 0 for success, -1 in case of error
130 */
131int i2s_set_sysclk_dir(struct i2s_reg *i2s_reg, int dir)
132{
133 unsigned int mod = readl(&i2s_reg->mod);
134
135 if (dir == SND_SOC_CLOCK_IN)
136 mod |= MOD_CDCLKCON;
137 else
138 mod &= ~MOD_CDCLKCON;
139
140 writel(mod, &i2s_reg->mod);
141
142 return 0;
143}
144
145/*
146 * Sets I2S Clcok format
147 *
148 * @param fmt i2s clock properties
149 * @param i2s_reg i2s regiter address
150 *
151 * @return int value 0 for success, -1 in case of error
152 */
153int i2s_set_fmt(struct i2s_reg *i2s_reg, unsigned int fmt)
154{
155 unsigned int mod = readl(&i2s_reg->mod);
156 unsigned int tmp = 0;
157 unsigned int ret = 0;
158
159 /* Format is priority */
160 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
161 case SND_SOC_DAIFMT_RIGHT_J:
162 tmp |= MOD_LR_RLOW;
163 tmp |= MOD_SDF_MSB;
164 break;
165 case SND_SOC_DAIFMT_LEFT_J:
166 tmp |= MOD_LR_RLOW;
167 tmp |= MOD_SDF_LSB;
168 break;
169 case SND_SOC_DAIFMT_I2S:
170 tmp |= MOD_SDF_IIS;
171 break;
172 default:
173 debug("%s: Invalid format priority [0x%x]\n", __func__,
Dani Krishna Mohan65c7ee62013-09-11 16:38:48 +0530174 (fmt & SND_SOC_DAIFMT_FORMAT_MASK));
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +0000175 return -1;
176 }
177
178 /*
179 * INV flag is relative to the FORMAT flag - if set it simply
180 * flips the polarity specified by the Standard
181 */
182 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
183 case SND_SOC_DAIFMT_NB_NF:
184 break;
185 case SND_SOC_DAIFMT_NB_IF:
186 if (tmp & MOD_LR_RLOW)
187 tmp &= ~MOD_LR_RLOW;
188 else
189 tmp |= MOD_LR_RLOW;
190 break;
191 default:
192 debug("%s: Invalid clock ploarity input [0x%x]\n", __func__,
Dani Krishna Mohan65c7ee62013-09-11 16:38:48 +0530193 (fmt & SND_SOC_DAIFMT_INV_MASK));
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +0000194 return -1;
195 }
196
197 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
198 case SND_SOC_DAIFMT_CBS_CFS:
199 tmp |= MOD_SLAVE;
200 break;
201 case SND_SOC_DAIFMT_CBM_CFM:
202 /* Set default source clock in Master mode */
203 ret = i2s_set_sysclk_dir(i2s_reg, SND_SOC_CLOCK_OUT);
204 if (ret != 0) {
205 debug("%s:set i2s clock direction failed\n", __func__);
206 return -1;
207 }
208 break;
209 default:
210 debug("%s: Invalid master selection [0x%x]\n", __func__,
Dani Krishna Mohan65c7ee62013-09-11 16:38:48 +0530211 (fmt & SND_SOC_DAIFMT_MASTER_MASK));
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +0000212 return -1;
213 }
214
215 mod &= ~(MOD_SDF_MASK | MOD_LR_RLOW | MOD_SLAVE);
216 mod |= tmp;
217 writel(mod, &i2s_reg->mod);
218
219 return 0;
220}
221
222/*
223 * Sets the sample width in bits
224 *
225 * @param blc samplewidth (size of sample in bits)
226 * @param i2s_reg i2s regiter address
227 *
228 * @return int value 0 for success, -1 in case of error
229 */
230int i2s_set_samplesize(struct i2s_reg *i2s_reg, unsigned int blc)
231{
232 unsigned int mod = readl(&i2s_reg->mod);
233
234 mod &= ~MOD_BLCP_MASK;
235 mod &= ~MOD_BLC_MASK;
236
237 switch (blc) {
238 case 8:
239 mod |= MOD_BLCP_8BIT;
240 mod |= MOD_BLC_8BIT;
241 break;
242 case 16:
243 mod |= MOD_BLCP_16BIT;
244 mod |= MOD_BLC_16BIT;
245 break;
246 case 24:
247 mod |= MOD_BLCP_24BIT;
248 mod |= MOD_BLC_24BIT;
249 break;
250 default:
251 debug("%s: Invalid sample size input [0x%x]\n",
Dani Krishna Mohan65c7ee62013-09-11 16:38:48 +0530252 __func__, blc);
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +0000253 return -1;
254 }
255 writel(mod, &i2s_reg->mod);
256
257 return 0;
258}
259
260int i2s_transfer_tx_data(struct i2stx_info *pi2s_tx, unsigned int *data,
261 unsigned long data_size)
262{
263 int i;
264 int start;
265 struct i2s_reg *i2s_reg =
266 (struct i2s_reg *)pi2s_tx->base_address;
267
268 if (data_size < FIFO_LENGTH) {
269 debug("%s : Invalid data size\n", __func__);
270 return -1; /* invalid pcm data size */
271 }
272
273 /* fill the tx buffer before stating the tx transmit */
274 for (i = 0; i < FIFO_LENGTH; i++)
275 writel(*data++, &i2s_reg->txd);
276
277 data_size -= FIFO_LENGTH;
278 i2s_txctrl(i2s_reg, I2S_TX_ON);
279
280 while (data_size > 0) {
281 start = get_timer(0);
282 if (!(CON_TXFIFO_FULL & (readl(&i2s_reg->con)))) {
283 writel(*data++, &i2s_reg->txd);
284 data_size--;
285 } else {
286 if (get_timer(start) > TIMEOUT_I2S_TX) {
287 i2s_txctrl(i2s_reg, I2S_TX_OFF);
288 debug("%s: I2S Transfer Timeout\n", __func__);
289 return -1;
290 }
291 }
292 }
293 i2s_txctrl(i2s_reg, I2S_TX_OFF);
294
295 return 0;
296}
297
298int i2s_tx_init(struct i2stx_info *pi2s_tx)
299{
300 int ret;
301 struct i2s_reg *i2s_reg =
302 (struct i2s_reg *)pi2s_tx->base_address;
303
304 /* Initialize GPIO for I2s */
305 exynos_pinmux_config(PERIPH_ID_I2S1, 0);
306
307 /* Set EPLL Clock */
308 ret = set_epll_clk(pi2s_tx->audio_pll_clk);
309 if (ret != 0) {
310 debug("%s: epll clock set rate falied\n", __func__);
311 return -1;
312 }
313
314 /* Select Clk Source for Audio1 */
Dani Krishna Mohan65c7ee62013-09-11 16:38:48 +0530315 ret = set_i2s_clk_source(pi2s_tx->id);
316 if (ret == -1) {
317 debug("%s: unsupported clock for i2s-%d\n", __func__,
318 pi2s_tx->id);
319 return -1;
320 }
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +0000321
322 /* Set Prescaler to get MCLK */
Dani Krishna Mohan65c7ee62013-09-11 16:38:48 +0530323 ret = set_i2s_clk_prescaler(pi2s_tx->audio_pll_clk,
324 (pi2s_tx->samplingrate * (pi2s_tx->rfs)),
325 pi2s_tx->id);
326 if (ret == -1) {
327 debug("%s: unsupported prescalar for i2s-%d\n", __func__,
328 pi2s_tx->id);
329 return -1;
330 }
Rajeshwari Shinde836a66c2012-10-25 19:49:22 +0000331
332 /* Configure I2s format */
333 ret = i2s_set_fmt(i2s_reg, (SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
334 SND_SOC_DAIFMT_CBM_CFM));
335 if (ret == 0) {
336 i2s_set_lr_framesize(i2s_reg, pi2s_tx->rfs);
337 ret = i2s_set_samplesize(i2s_reg, pi2s_tx->bitspersample);
338 if (ret != 0) {
339 debug("%s:set sample rate failed\n", __func__);
340 return -1;
341 }
342
343 i2s_set_bitclk_framesize(i2s_reg, pi2s_tx->bfs);
344 /* disable i2s transfer flag and flush the fifo */
345 i2s_txctrl(i2s_reg, I2S_TX_OFF);
346 i2s_fifo(i2s_reg, FIC_TXFLUSH);
347 } else {
348 debug("%s: failed\n", __func__);
349 }
350
351 return ret;
352}