1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2012 Samsung Electronics
12 #include <asm/arch/clk.h>
13 #include <asm/arch/pinmux.h>
14 #include <asm/arch/i2s-regs.h>
17 #define FIC_TX2COUNT(x) (((x) >> 24) & 0xf)
18 #define FIC_TX1COUNT(x) (((x) >> 16) & 0xf)
19 #define FIC_TXCOUNT(x) (((x) >> 8) & 0xf)
20 #define FIC_RXCOUNT(x) (((x) >> 0) & 0xf)
21 #define FICS_TXCOUNT(x) (((x) >> 8) & 0x7f)
23 #define TIMEOUT_I2S_TX 100 /* i2s transfer timeout */
26 * Sets the frame size for I2S LR clock
28 * @param i2s_reg i2s register address
29 * @param rfs Frame Size
31 static void i2s_set_lr_framesize(struct i2s_reg *i2s_reg, unsigned int rfs)
33 unsigned int mod = readl(&i2s_reg->mod);
35 mod &= ~MOD_RCLK_MASK;
39 mod |= MOD_RCLK_768FS;
42 mod |= MOD_RCLK_512FS;
45 mod |= MOD_RCLK_384FS;
48 mod |= MOD_RCLK_256FS;
52 writel(mod, &i2s_reg->mod);
56 * Sets the i2s transfer control
58 * @param i2s_reg i2s register address
59 * @param on 1 enable tx , 0 disable tx transfer
61 static void i2s_txctrl(struct i2s_reg *i2s_reg, int on)
63 unsigned int con = readl(&i2s_reg->con);
64 unsigned int mod = readl(&i2s_reg->mod) & ~MOD_MASK;
68 con &= ~CON_TXCH_PAUSE;
70 con |= CON_TXCH_PAUSE;
74 writel(mod, &i2s_reg->mod);
75 writel(con, &i2s_reg->con);
79 * set the bit clock frame size (in multiples of LRCLK)
81 * @param i2s_reg i2s register address
82 * @param bfs bit Frame Size
84 static void i2s_set_bitclk_framesize(struct i2s_reg *i2s_reg, unsigned bfs)
86 unsigned int mod = readl(&i2s_reg->mod);
88 mod &= ~MOD_BCLK_MASK;
101 mod |= MOD_BCLK_16FS;
106 writel(mod, &i2s_reg->mod);
110 * flushes the i2stx fifo
112 * @param i2s_reg i2s register address
113 * @param flush Tx fifo flush command (0x00 - do not flush
114 * 0x80 - flush tx fifo)
116 static void i2s_fifo(struct i2s_reg *i2s_reg, unsigned int flush)
119 setbits_le32(&i2s_reg->fic, flush);
120 clrbits_le32(&i2s_reg->fic, flush);
124 * Set System Clock direction
126 * @param i2s_reg i2s register address
127 * @param dir Clock direction
129 * @return int value 0 for success, -1 in case of error
131 static int i2s_set_sysclk_dir(struct i2s_reg *i2s_reg, int dir)
133 unsigned int mod = readl(&i2s_reg->mod);
135 if (dir == SND_SOC_CLOCK_IN)
138 mod &= ~MOD_CDCLKCON;
140 writel(mod, &i2s_reg->mod);
146 * Sets I2S Clcok format
148 * @param fmt i2s clock properties
149 * @param i2s_reg i2s register address
151 * @return int value 0 for success, -1 in case of error
153 static int i2s_set_fmt(struct i2s_reg *i2s_reg, unsigned int fmt)
155 unsigned int mod = readl(&i2s_reg->mod);
156 unsigned int tmp = 0;
157 unsigned int ret = 0;
159 /* Format is priority */
160 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
161 case SND_SOC_DAIFMT_RIGHT_J:
165 case SND_SOC_DAIFMT_LEFT_J:
169 case SND_SOC_DAIFMT_I2S:
173 debug("%s: Invalid format priority [0x%x]\n", __func__,
174 (fmt & SND_SOC_DAIFMT_FORMAT_MASK));
179 * INV flag is relative to the FORMAT flag - if set it simply
180 * flips the polarity specified by the Standard
182 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
183 case SND_SOC_DAIFMT_NB_NF:
185 case SND_SOC_DAIFMT_NB_IF:
186 if (tmp & MOD_LR_RLOW)
192 debug("%s: Invalid clock ploarity input [0x%x]\n", __func__,
193 (fmt & SND_SOC_DAIFMT_INV_MASK));
197 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
198 case SND_SOC_DAIFMT_CBS_CFS:
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);
205 debug("%s:set i2s clock direction failed\n", __func__);
210 debug("%s: Invalid master selection [0x%x]\n", __func__,
211 (fmt & SND_SOC_DAIFMT_MASTER_MASK));
215 mod &= ~(MOD_SDF_MASK | MOD_LR_RLOW | MOD_SLAVE);
217 writel(mod, &i2s_reg->mod);
223 * Sets the sample width in bits
225 * @param blc samplewidth (size of sample in bits)
226 * @param i2s_reg i2s register address
228 * @return int value 0 for success, -1 in case of error
230 static int i2s_set_samplesize(struct i2s_reg *i2s_reg, unsigned int blc)
232 unsigned int mod = readl(&i2s_reg->mod);
234 mod &= ~MOD_BLCP_MASK;
235 mod &= ~MOD_BLC_MASK;
239 mod |= MOD_BLCP_8BIT;
243 mod |= MOD_BLCP_16BIT;
244 mod |= MOD_BLC_16BIT;
247 mod |= MOD_BLCP_24BIT;
248 mod |= MOD_BLC_24BIT;
251 debug("%s: Invalid sample size input [0x%x]\n",
255 writel(mod, &i2s_reg->mod);
260 int i2s_transfer_tx_data(struct i2s_uc_priv *pi2s_tx, void *data,
263 struct i2s_reg *i2s_reg = (struct i2s_reg *)pi2s_tx->base_address;
268 if (data_size < FIFO_LENGTH) {
269 debug("%s : Invalid data size\n", __func__);
270 return -ENODATA; /* invalid pcm data size */
273 /* fill the tx buffer before stating the tx transmit */
274 for (i = 0, ptr = data; i < FIFO_LENGTH; i++)
275 writel(*ptr++, &i2s_reg->txd);
277 data_size -= sizeof(*ptr) * FIFO_LENGTH;
278 i2s_txctrl(i2s_reg, I2S_TX_ON);
280 while (data_size > 0) {
281 start = get_timer(0);
282 if (!(CON_TXFIFO_FULL & (readl(&i2s_reg->con)))) {
283 writel(*ptr++, &i2s_reg->txd);
284 data_size -= sizeof(*ptr);
286 if (get_timer(start) > TIMEOUT_I2S_TX) {
287 i2s_txctrl(i2s_reg, I2S_TX_OFF);
288 debug("%s: I2S Transfer Timeout\n", __func__);
293 i2s_txctrl(i2s_reg, I2S_TX_OFF);
298 static int i2s_tx_init(struct i2s_uc_priv *pi2s_tx)
301 struct i2s_reg *i2s_reg = (struct i2s_reg *)pi2s_tx->base_address;
303 if (pi2s_tx->id == 0) {
304 /* Initialize GPIO for I2S-0 */
305 exynos_pinmux_config(PERIPH_ID_I2S0, 0);
308 ret = set_epll_clk(pi2s_tx->samplingrate * pi2s_tx->rfs * 4);
309 } else if (pi2s_tx->id == 1) {
310 /* Initialize GPIO for I2S-1 */
311 exynos_pinmux_config(PERIPH_ID_I2S1, 0);
314 ret = set_epll_clk(pi2s_tx->audio_pll_clk);
316 debug("%s: unsupported i2s-%d bus\n", __func__, pi2s_tx->id);
321 debug("%s: epll clock set rate failed\n", __func__);
325 /* Select Clk Source for Audio 0 or 1 */
326 ret = set_i2s_clk_source(pi2s_tx->id);
328 debug("%s: unsupported clock for i2s-%d\n", __func__,
333 if (pi2s_tx->id == 0) {
334 /*Reset the i2s module */
335 writel(CON_RESET, &i2s_reg->con);
337 writel(MOD_OP_CLK | MOD_RCLKSRC, &i2s_reg->mod);
338 /* set i2s prescaler */
339 writel(PSREN | PSVAL, &i2s_reg->psr);
341 /* Set Prescaler to get MCLK */
342 ret = set_i2s_clk_prescaler(pi2s_tx->audio_pll_clk,
343 (pi2s_tx->samplingrate * (pi2s_tx->rfs)),
347 debug("%s: unsupported prescalar for i2s-%d\n", __func__,
352 /* Configure I2s format */
353 ret = i2s_set_fmt(i2s_reg, SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
354 SND_SOC_DAIFMT_CBM_CFM);
356 i2s_set_lr_framesize(i2s_reg, pi2s_tx->rfs);
357 ret = i2s_set_samplesize(i2s_reg, pi2s_tx->bitspersample);
359 debug("%s:set sample rate failed\n", __func__);
363 i2s_set_bitclk_framesize(i2s_reg, pi2s_tx->bfs);
364 /* disable i2s transfer flag and flush the fifo */
365 i2s_txctrl(i2s_reg, I2S_TX_OFF);
366 i2s_fifo(i2s_reg, FIC_TXFLUSH);
368 debug("%s: failed\n", __func__);
374 static int samsung_i2s_tx_data(struct udevice *dev, void *data, uint data_size)
376 struct i2s_uc_priv *priv = dev_get_uclass_priv(dev);
378 return i2s_transfer_tx_data(priv, data, data_size);
381 static int samsung_i2s_probe(struct udevice *dev)
383 struct i2s_uc_priv *priv = dev_get_uclass_priv(dev);
385 return i2s_tx_init(priv);
388 static int samsung_i2s_ofdata_to_platdata(struct udevice *dev)
390 struct i2s_uc_priv *priv = dev_get_uclass_priv(dev);
394 * Get the pre-defined sound specific values from FDT.
395 * All of these are expected to be correct otherwise
396 * wrong register values in i2s setup parameters
397 * may result in no sound play.
399 base = dev_read_addr(dev);
400 if (base == FDT_ADDR_T_NONE) {
401 debug("%s: Missing i2s base\n", __func__);
404 priv->base_address = base;
406 if (dev_read_u32u(dev, "samsung,i2s-epll-clock-frequency",
407 &priv->audio_pll_clk))
409 debug("audio_pll_clk = %d\n", priv->audio_pll_clk);
410 if (dev_read_u32u(dev, "samsung,i2s-sampling-rate",
411 &priv->samplingrate))
413 debug("samplingrate = %d\n", priv->samplingrate);
414 if (dev_read_u32u(dev, "samsung,i2s-bits-per-sample",
415 &priv->bitspersample))
417 debug("bitspersample = %d\n", priv->bitspersample);
418 if (dev_read_u32u(dev, "samsung,i2s-channels", &priv->channels))
420 debug("channels = %d\n", priv->channels);
421 if (dev_read_u32u(dev, "samsung,i2s-lr-clk-framesize", &priv->rfs))
423 debug("rfs = %d\n", priv->rfs);
424 if (dev_read_u32u(dev, "samsung,i2s-bit-clk-framesize", &priv->bfs))
426 debug("bfs = %d\n", priv->bfs);
428 if (dev_read_u32u(dev, "samsung,i2s-id", &priv->id))
430 debug("id = %d\n", priv->id);
435 debug("fail to get sound i2s node properties\n");
440 static const struct i2s_ops samsung_i2s_ops = {
441 .tx_data = samsung_i2s_tx_data,
444 static const struct udevice_id samsung_i2s_ids[] = {
445 { .compatible = "samsung,s5pv210-i2s" },
449 U_BOOT_DRIVER(samsung_i2s) = {
450 .name = "samsung_i2s",
452 .of_match = samsung_i2s_ids,
453 .probe = samsung_i2s_probe,
454 .ofdata_to_platdata = samsung_i2s_ofdata_to_platdata,
455 .ops = &samsung_i2s_ops,