15033f43cSJassi Brar /* sound/soc/samsung/i2s.c 25033f43cSJassi Brar * 35033f43cSJassi Brar * ALSA SoC Audio Layer - Samsung I2S Controller driver 45033f43cSJassi Brar * 55033f43cSJassi Brar * Copyright (c) 2010 Samsung Electronics Co. Ltd. 65033f43cSJassi Brar * Jaswinder Singh <jassi.brar@samsung.com> 75033f43cSJassi Brar * 85033f43cSJassi Brar * This program is free software; you can redistribute it and/or modify 95033f43cSJassi Brar * it under the terms of the GNU General Public License version 2 as 105033f43cSJassi Brar * published by the Free Software Foundation. 115033f43cSJassi Brar */ 125033f43cSJassi Brar 135033f43cSJassi Brar #include <linux/delay.h> 145033f43cSJassi Brar #include <linux/slab.h> 155033f43cSJassi Brar #include <linux/clk.h> 165033f43cSJassi Brar #include <linux/io.h> 17da155d5bSPaul Gortmaker #include <linux/module.h> 185033f43cSJassi Brar 195033f43cSJassi Brar #include <sound/soc.h> 200378b6acSSeungwhan Youn #include <sound/pcm_params.h> 215033f43cSJassi Brar 225033f43cSJassi Brar #include <plat/audio.h> 235033f43cSJassi Brar 245033f43cSJassi Brar #include "dma.h" 2561100f40SSangbeom Kim #include "idma.h" 265033f43cSJassi Brar #include "i2s.h" 27172a453dSSangbeom Kim #include "i2s-regs.h" 285033f43cSJassi Brar 295033f43cSJassi Brar #define msecs_to_loops(t) (loops_per_jiffy / 1000 * HZ * t) 305033f43cSJassi Brar 315033f43cSJassi Brar struct i2s_dai { 325033f43cSJassi Brar /* Platform device for this DAI */ 335033f43cSJassi Brar struct platform_device *pdev; 345033f43cSJassi Brar /* IOREMAP'd SFRs */ 355033f43cSJassi Brar void __iomem *addr; 365033f43cSJassi Brar /* Physical base address of SFRs */ 375033f43cSJassi Brar u32 base; 385033f43cSJassi Brar /* Rate of RCLK source clock */ 395033f43cSJassi Brar unsigned long rclk_srcrate; 405033f43cSJassi Brar /* Frame Clock */ 415033f43cSJassi Brar unsigned frmclk; 425033f43cSJassi Brar /* 435033f43cSJassi Brar * Specifically requested RCLK,BCLK by MACHINE Driver. 445033f43cSJassi Brar * 0 indicates CPU driver is free to choose any value. 455033f43cSJassi Brar */ 465033f43cSJassi Brar unsigned rfs, bfs; 475033f43cSJassi Brar /* I2S Controller's core clock */ 485033f43cSJassi Brar struct clk *clk; 495033f43cSJassi Brar /* Clock for generating I2S signals */ 505033f43cSJassi Brar struct clk *op_clk; 515033f43cSJassi Brar /* Array of clock names for op_clk */ 525033f43cSJassi Brar const char **src_clk; 535033f43cSJassi Brar /* Pointer to the Primary_Fifo if this is Sec_Fifo, NULL otherwise */ 545033f43cSJassi Brar struct i2s_dai *pri_dai; 555033f43cSJassi Brar /* Pointer to the Secondary_Fifo if it has one, NULL otherwise */ 565033f43cSJassi Brar struct i2s_dai *sec_dai; 575033f43cSJassi Brar #define DAI_OPENED (1 << 0) /* Dai is opened */ 585033f43cSJassi Brar #define DAI_MANAGER (1 << 1) /* Dai is the manager */ 595033f43cSJassi Brar unsigned mode; 605033f43cSJassi Brar /* Driver for this DAI */ 615033f43cSJassi Brar struct snd_soc_dai_driver i2s_dai_drv; 625033f43cSJassi Brar /* DMA parameters */ 635033f43cSJassi Brar struct s3c_dma_params dma_playback; 645033f43cSJassi Brar struct s3c_dma_params dma_capture; 6561100f40SSangbeom Kim struct s3c_dma_params idma_playback; 665033f43cSJassi Brar u32 quirks; 675033f43cSJassi Brar u32 suspend_i2smod; 685033f43cSJassi Brar u32 suspend_i2scon; 695033f43cSJassi Brar u32 suspend_i2spsr; 705033f43cSJassi Brar }; 715033f43cSJassi Brar 725033f43cSJassi Brar /* Lock for cross i/f checks */ 735033f43cSJassi Brar static DEFINE_SPINLOCK(lock); 745033f43cSJassi Brar 755033f43cSJassi Brar /* If this is the 'overlay' stereo DAI */ 765033f43cSJassi Brar static inline bool is_secondary(struct i2s_dai *i2s) 775033f43cSJassi Brar { 785033f43cSJassi Brar return i2s->pri_dai ? true : false; 795033f43cSJassi Brar } 805033f43cSJassi Brar 815033f43cSJassi Brar /* If operating in SoC-Slave mode */ 825033f43cSJassi Brar static inline bool is_slave(struct i2s_dai *i2s) 835033f43cSJassi Brar { 845033f43cSJassi Brar return (readl(i2s->addr + I2SMOD) & MOD_SLAVE) ? true : false; 855033f43cSJassi Brar } 865033f43cSJassi Brar 875033f43cSJassi Brar /* If this interface of the controller is transmitting data */ 885033f43cSJassi Brar static inline bool tx_active(struct i2s_dai *i2s) 895033f43cSJassi Brar { 905033f43cSJassi Brar u32 active; 915033f43cSJassi Brar 925033f43cSJassi Brar if (!i2s) 935033f43cSJassi Brar return false; 945033f43cSJassi Brar 9533195500SSangbeom Kim active = readl(i2s->addr + I2SCON); 965033f43cSJassi Brar 975033f43cSJassi Brar if (is_secondary(i2s)) 985033f43cSJassi Brar active &= CON_TXSDMA_ACTIVE; 995033f43cSJassi Brar else 1005033f43cSJassi Brar active &= CON_TXDMA_ACTIVE; 1015033f43cSJassi Brar 1025033f43cSJassi Brar return active ? true : false; 1035033f43cSJassi Brar } 1045033f43cSJassi Brar 1055033f43cSJassi Brar /* If the other interface of the controller is transmitting data */ 1065033f43cSJassi Brar static inline bool other_tx_active(struct i2s_dai *i2s) 1075033f43cSJassi Brar { 1085033f43cSJassi Brar struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai; 1095033f43cSJassi Brar 1105033f43cSJassi Brar return tx_active(other); 1115033f43cSJassi Brar } 1125033f43cSJassi Brar 1135033f43cSJassi Brar /* If any interface of the controller is transmitting data */ 1145033f43cSJassi Brar static inline bool any_tx_active(struct i2s_dai *i2s) 1155033f43cSJassi Brar { 1165033f43cSJassi Brar return tx_active(i2s) || other_tx_active(i2s); 1175033f43cSJassi Brar } 1185033f43cSJassi Brar 1195033f43cSJassi Brar /* If this interface of the controller is receiving data */ 1205033f43cSJassi Brar static inline bool rx_active(struct i2s_dai *i2s) 1215033f43cSJassi Brar { 1225033f43cSJassi Brar u32 active; 1235033f43cSJassi Brar 1245033f43cSJassi Brar if (!i2s) 1255033f43cSJassi Brar return false; 1265033f43cSJassi Brar 12733195500SSangbeom Kim active = readl(i2s->addr + I2SCON) & CON_RXDMA_ACTIVE; 1285033f43cSJassi Brar 1295033f43cSJassi Brar return active ? true : false; 1305033f43cSJassi Brar } 1315033f43cSJassi Brar 1325033f43cSJassi Brar /* If the other interface of the controller is receiving data */ 1335033f43cSJassi Brar static inline bool other_rx_active(struct i2s_dai *i2s) 1345033f43cSJassi Brar { 1355033f43cSJassi Brar struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai; 1365033f43cSJassi Brar 1375033f43cSJassi Brar return rx_active(other); 1385033f43cSJassi Brar } 1395033f43cSJassi Brar 1405033f43cSJassi Brar /* If any interface of the controller is receiving data */ 1415033f43cSJassi Brar static inline bool any_rx_active(struct i2s_dai *i2s) 1425033f43cSJassi Brar { 1435033f43cSJassi Brar return rx_active(i2s) || other_rx_active(i2s); 1445033f43cSJassi Brar } 1455033f43cSJassi Brar 1465033f43cSJassi Brar /* If the other DAI is transmitting or receiving data */ 1475033f43cSJassi Brar static inline bool other_active(struct i2s_dai *i2s) 1485033f43cSJassi Brar { 1495033f43cSJassi Brar return other_rx_active(i2s) || other_tx_active(i2s); 1505033f43cSJassi Brar } 1515033f43cSJassi Brar 1525033f43cSJassi Brar /* If this DAI is transmitting or receiving data */ 1535033f43cSJassi Brar static inline bool this_active(struct i2s_dai *i2s) 1545033f43cSJassi Brar { 1555033f43cSJassi Brar return tx_active(i2s) || rx_active(i2s); 1565033f43cSJassi Brar } 1575033f43cSJassi Brar 1585033f43cSJassi Brar /* If the controller is active anyway */ 1595033f43cSJassi Brar static inline bool any_active(struct i2s_dai *i2s) 1605033f43cSJassi Brar { 1615033f43cSJassi Brar return this_active(i2s) || other_active(i2s); 1625033f43cSJassi Brar } 1635033f43cSJassi Brar 1645033f43cSJassi Brar static inline struct i2s_dai *to_info(struct snd_soc_dai *dai) 1655033f43cSJassi Brar { 1665033f43cSJassi Brar return snd_soc_dai_get_drvdata(dai); 1675033f43cSJassi Brar } 1685033f43cSJassi Brar 1695033f43cSJassi Brar static inline bool is_opened(struct i2s_dai *i2s) 1705033f43cSJassi Brar { 1715033f43cSJassi Brar if (i2s && (i2s->mode & DAI_OPENED)) 1725033f43cSJassi Brar return true; 1735033f43cSJassi Brar else 1745033f43cSJassi Brar return false; 1755033f43cSJassi Brar } 1765033f43cSJassi Brar 1775033f43cSJassi Brar static inline bool is_manager(struct i2s_dai *i2s) 1785033f43cSJassi Brar { 1795033f43cSJassi Brar if (is_opened(i2s) && (i2s->mode & DAI_MANAGER)) 1805033f43cSJassi Brar return true; 1815033f43cSJassi Brar else 1825033f43cSJassi Brar return false; 1835033f43cSJassi Brar } 1845033f43cSJassi Brar 1855033f43cSJassi Brar /* Read RCLK of I2S (in multiples of LRCLK) */ 1865033f43cSJassi Brar static inline unsigned get_rfs(struct i2s_dai *i2s) 1875033f43cSJassi Brar { 1885033f43cSJassi Brar u32 rfs = (readl(i2s->addr + I2SMOD) >> 3) & 0x3; 1895033f43cSJassi Brar 1905033f43cSJassi Brar switch (rfs) { 1915033f43cSJassi Brar case 3: return 768; 1925033f43cSJassi Brar case 2: return 384; 1935033f43cSJassi Brar case 1: return 512; 1945033f43cSJassi Brar default: return 256; 1955033f43cSJassi Brar } 1965033f43cSJassi Brar } 1975033f43cSJassi Brar 1985033f43cSJassi Brar /* Write RCLK of I2S (in multiples of LRCLK) */ 1995033f43cSJassi Brar static inline void set_rfs(struct i2s_dai *i2s, unsigned rfs) 2005033f43cSJassi Brar { 2015033f43cSJassi Brar u32 mod = readl(i2s->addr + I2SMOD); 2025033f43cSJassi Brar 2035033f43cSJassi Brar mod &= ~MOD_RCLK_MASK; 2045033f43cSJassi Brar 2055033f43cSJassi Brar switch (rfs) { 2065033f43cSJassi Brar case 768: 2075033f43cSJassi Brar mod |= MOD_RCLK_768FS; 2085033f43cSJassi Brar break; 2095033f43cSJassi Brar case 512: 2105033f43cSJassi Brar mod |= MOD_RCLK_512FS; 2115033f43cSJassi Brar break; 2125033f43cSJassi Brar case 384: 2135033f43cSJassi Brar mod |= MOD_RCLK_384FS; 2145033f43cSJassi Brar break; 2155033f43cSJassi Brar default: 2165033f43cSJassi Brar mod |= MOD_RCLK_256FS; 2175033f43cSJassi Brar break; 2185033f43cSJassi Brar } 2195033f43cSJassi Brar 2205033f43cSJassi Brar writel(mod, i2s->addr + I2SMOD); 2215033f43cSJassi Brar } 2225033f43cSJassi Brar 2235033f43cSJassi Brar /* Read Bit-Clock of I2S (in multiples of LRCLK) */ 2245033f43cSJassi Brar static inline unsigned get_bfs(struct i2s_dai *i2s) 2255033f43cSJassi Brar { 2265033f43cSJassi Brar u32 bfs = (readl(i2s->addr + I2SMOD) >> 1) & 0x3; 2275033f43cSJassi Brar 2285033f43cSJassi Brar switch (bfs) { 2295033f43cSJassi Brar case 3: return 24; 2305033f43cSJassi Brar case 2: return 16; 2315033f43cSJassi Brar case 1: return 48; 2325033f43cSJassi Brar default: return 32; 2335033f43cSJassi Brar } 2345033f43cSJassi Brar } 2355033f43cSJassi Brar 2365033f43cSJassi Brar /* Write Bit-Clock of I2S (in multiples of LRCLK) */ 2375033f43cSJassi Brar static inline void set_bfs(struct i2s_dai *i2s, unsigned bfs) 2385033f43cSJassi Brar { 2395033f43cSJassi Brar u32 mod = readl(i2s->addr + I2SMOD); 2405033f43cSJassi Brar 2415033f43cSJassi Brar mod &= ~MOD_BCLK_MASK; 2425033f43cSJassi Brar 2435033f43cSJassi Brar switch (bfs) { 2445033f43cSJassi Brar case 48: 2455033f43cSJassi Brar mod |= MOD_BCLK_48FS; 2465033f43cSJassi Brar break; 2475033f43cSJassi Brar case 32: 2485033f43cSJassi Brar mod |= MOD_BCLK_32FS; 2495033f43cSJassi Brar break; 2505033f43cSJassi Brar case 24: 2515033f43cSJassi Brar mod |= MOD_BCLK_24FS; 2525033f43cSJassi Brar break; 2535033f43cSJassi Brar case 16: 2545033f43cSJassi Brar mod |= MOD_BCLK_16FS; 2555033f43cSJassi Brar break; 2565033f43cSJassi Brar default: 2575033f43cSJassi Brar dev_err(&i2s->pdev->dev, "Wrong BCLK Divider!\n"); 2585033f43cSJassi Brar return; 2595033f43cSJassi Brar } 2605033f43cSJassi Brar 2615033f43cSJassi Brar writel(mod, i2s->addr + I2SMOD); 2625033f43cSJassi Brar } 2635033f43cSJassi Brar 2645033f43cSJassi Brar /* Sample-Size */ 2655033f43cSJassi Brar static inline int get_blc(struct i2s_dai *i2s) 2665033f43cSJassi Brar { 2675033f43cSJassi Brar int blc = readl(i2s->addr + I2SMOD); 2685033f43cSJassi Brar 2695033f43cSJassi Brar blc = (blc >> 13) & 0x3; 2705033f43cSJassi Brar 2715033f43cSJassi Brar switch (blc) { 2725033f43cSJassi Brar case 2: return 24; 2735033f43cSJassi Brar case 1: return 8; 2745033f43cSJassi Brar default: return 16; 2755033f43cSJassi Brar } 2765033f43cSJassi Brar } 2775033f43cSJassi Brar 2785033f43cSJassi Brar /* TX Channel Control */ 2795033f43cSJassi Brar static void i2s_txctrl(struct i2s_dai *i2s, int on) 2805033f43cSJassi Brar { 2815033f43cSJassi Brar void __iomem *addr = i2s->addr; 2825033f43cSJassi Brar u32 con = readl(addr + I2SCON); 2835033f43cSJassi Brar u32 mod = readl(addr + I2SMOD) & ~MOD_MASK; 2845033f43cSJassi Brar 2855033f43cSJassi Brar if (on) { 2865033f43cSJassi Brar con |= CON_ACTIVE; 2875033f43cSJassi Brar con &= ~CON_TXCH_PAUSE; 2885033f43cSJassi Brar 2895033f43cSJassi Brar if (is_secondary(i2s)) { 2905033f43cSJassi Brar con |= CON_TXSDMA_ACTIVE; 2915033f43cSJassi Brar con &= ~CON_TXSDMA_PAUSE; 2925033f43cSJassi Brar } else { 2935033f43cSJassi Brar con |= CON_TXDMA_ACTIVE; 2945033f43cSJassi Brar con &= ~CON_TXDMA_PAUSE; 2955033f43cSJassi Brar } 2965033f43cSJassi Brar 2975033f43cSJassi Brar if (any_rx_active(i2s)) 2985033f43cSJassi Brar mod |= MOD_TXRX; 2995033f43cSJassi Brar else 3005033f43cSJassi Brar mod |= MOD_TXONLY; 3015033f43cSJassi Brar } else { 3025033f43cSJassi Brar if (is_secondary(i2s)) { 3035033f43cSJassi Brar con |= CON_TXSDMA_PAUSE; 3045033f43cSJassi Brar con &= ~CON_TXSDMA_ACTIVE; 3055033f43cSJassi Brar } else { 3065033f43cSJassi Brar con |= CON_TXDMA_PAUSE; 3075033f43cSJassi Brar con &= ~CON_TXDMA_ACTIVE; 3085033f43cSJassi Brar } 3095033f43cSJassi Brar 3105033f43cSJassi Brar if (other_tx_active(i2s)) { 3115033f43cSJassi Brar writel(con, addr + I2SCON); 3125033f43cSJassi Brar return; 3135033f43cSJassi Brar } 3145033f43cSJassi Brar 3155033f43cSJassi Brar con |= CON_TXCH_PAUSE; 3165033f43cSJassi Brar 3175033f43cSJassi Brar if (any_rx_active(i2s)) 3185033f43cSJassi Brar mod |= MOD_RXONLY; 3195033f43cSJassi Brar else 3205033f43cSJassi Brar con &= ~CON_ACTIVE; 3215033f43cSJassi Brar } 3225033f43cSJassi Brar 3235033f43cSJassi Brar writel(mod, addr + I2SMOD); 3245033f43cSJassi Brar writel(con, addr + I2SCON); 3255033f43cSJassi Brar } 3265033f43cSJassi Brar 3275033f43cSJassi Brar /* RX Channel Control */ 3285033f43cSJassi Brar static void i2s_rxctrl(struct i2s_dai *i2s, int on) 3295033f43cSJassi Brar { 3305033f43cSJassi Brar void __iomem *addr = i2s->addr; 3315033f43cSJassi Brar u32 con = readl(addr + I2SCON); 3325033f43cSJassi Brar u32 mod = readl(addr + I2SMOD) & ~MOD_MASK; 3335033f43cSJassi Brar 3345033f43cSJassi Brar if (on) { 3355033f43cSJassi Brar con |= CON_RXDMA_ACTIVE | CON_ACTIVE; 3365033f43cSJassi Brar con &= ~(CON_RXDMA_PAUSE | CON_RXCH_PAUSE); 3375033f43cSJassi Brar 3385033f43cSJassi Brar if (any_tx_active(i2s)) 3395033f43cSJassi Brar mod |= MOD_TXRX; 3405033f43cSJassi Brar else 3415033f43cSJassi Brar mod |= MOD_RXONLY; 3425033f43cSJassi Brar } else { 3435033f43cSJassi Brar con |= CON_RXDMA_PAUSE | CON_RXCH_PAUSE; 3445033f43cSJassi Brar con &= ~CON_RXDMA_ACTIVE; 3455033f43cSJassi Brar 3465033f43cSJassi Brar if (any_tx_active(i2s)) 3475033f43cSJassi Brar mod |= MOD_TXONLY; 3485033f43cSJassi Brar else 3495033f43cSJassi Brar con &= ~CON_ACTIVE; 3505033f43cSJassi Brar } 3515033f43cSJassi Brar 3525033f43cSJassi Brar writel(mod, addr + I2SMOD); 3535033f43cSJassi Brar writel(con, addr + I2SCON); 3545033f43cSJassi Brar } 3555033f43cSJassi Brar 3565033f43cSJassi Brar /* Flush FIFO of an interface */ 3575033f43cSJassi Brar static inline void i2s_fifo(struct i2s_dai *i2s, u32 flush) 3585033f43cSJassi Brar { 3595033f43cSJassi Brar void __iomem *fic; 3605033f43cSJassi Brar u32 val; 3615033f43cSJassi Brar 3625033f43cSJassi Brar if (!i2s) 3635033f43cSJassi Brar return; 3645033f43cSJassi Brar 3655033f43cSJassi Brar if (is_secondary(i2s)) 3665033f43cSJassi Brar fic = i2s->addr + I2SFICS; 3675033f43cSJassi Brar else 3685033f43cSJassi Brar fic = i2s->addr + I2SFIC; 3695033f43cSJassi Brar 3705033f43cSJassi Brar /* Flush the FIFO */ 3715033f43cSJassi Brar writel(readl(fic) | flush, fic); 3725033f43cSJassi Brar 3735033f43cSJassi Brar /* Be patient */ 3745033f43cSJassi Brar val = msecs_to_loops(1) / 1000; /* 1 usec */ 3755033f43cSJassi Brar while (--val) 3765033f43cSJassi Brar cpu_relax(); 3775033f43cSJassi Brar 3785033f43cSJassi Brar writel(readl(fic) & ~flush, fic); 3795033f43cSJassi Brar } 3805033f43cSJassi Brar 3815033f43cSJassi Brar static int i2s_set_sysclk(struct snd_soc_dai *dai, 3825033f43cSJassi Brar int clk_id, unsigned int rfs, int dir) 3835033f43cSJassi Brar { 3845033f43cSJassi Brar struct i2s_dai *i2s = to_info(dai); 3855033f43cSJassi Brar struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai; 3865033f43cSJassi Brar u32 mod = readl(i2s->addr + I2SMOD); 3875033f43cSJassi Brar 3885033f43cSJassi Brar switch (clk_id) { 3895033f43cSJassi Brar case SAMSUNG_I2S_CDCLK: 3905033f43cSJassi Brar /* Shouldn't matter in GATING(CLOCK_IN) mode */ 3915033f43cSJassi Brar if (dir == SND_SOC_CLOCK_IN) 3925033f43cSJassi Brar rfs = 0; 3935033f43cSJassi Brar 3945033f43cSJassi Brar if ((rfs && other->rfs && (other->rfs != rfs)) || 3955033f43cSJassi Brar (any_active(i2s) && 3965033f43cSJassi Brar (((dir == SND_SOC_CLOCK_IN) 3975033f43cSJassi Brar && !(mod & MOD_CDCLKCON)) || 3985033f43cSJassi Brar ((dir == SND_SOC_CLOCK_OUT) 3995033f43cSJassi Brar && (mod & MOD_CDCLKCON))))) { 4005033f43cSJassi Brar dev_err(&i2s->pdev->dev, 4015033f43cSJassi Brar "%s:%d Other DAI busy\n", __func__, __LINE__); 4025033f43cSJassi Brar return -EAGAIN; 4035033f43cSJassi Brar } 4045033f43cSJassi Brar 4055033f43cSJassi Brar if (dir == SND_SOC_CLOCK_IN) 4065033f43cSJassi Brar mod |= MOD_CDCLKCON; 4075033f43cSJassi Brar else 4085033f43cSJassi Brar mod &= ~MOD_CDCLKCON; 4095033f43cSJassi Brar 4105033f43cSJassi Brar i2s->rfs = rfs; 4115033f43cSJassi Brar break; 4125033f43cSJassi Brar 4135033f43cSJassi Brar case SAMSUNG_I2S_RCLKSRC_0: /* clock corrsponding to IISMOD[10] := 0 */ 4145033f43cSJassi Brar case SAMSUNG_I2S_RCLKSRC_1: /* clock corrsponding to IISMOD[10] := 1 */ 4155033f43cSJassi Brar if ((i2s->quirks & QUIRK_NO_MUXPSR) 4165033f43cSJassi Brar || (clk_id == SAMSUNG_I2S_RCLKSRC_0)) 4175033f43cSJassi Brar clk_id = 0; 4185033f43cSJassi Brar else 4195033f43cSJassi Brar clk_id = 1; 4205033f43cSJassi Brar 4215033f43cSJassi Brar if (!any_active(i2s)) { 4225033f43cSJassi Brar if (i2s->op_clk) { 4235033f43cSJassi Brar if ((clk_id && !(mod & MOD_IMS_SYSMUX)) || 4245033f43cSJassi Brar (!clk_id && (mod & MOD_IMS_SYSMUX))) { 4255033f43cSJassi Brar clk_disable(i2s->op_clk); 4265033f43cSJassi Brar clk_put(i2s->op_clk); 4275033f43cSJassi Brar } else { 4286ce534aaSJassi Brar i2s->rclk_srcrate = 4296ce534aaSJassi Brar clk_get_rate(i2s->op_clk); 4305033f43cSJassi Brar return 0; 4315033f43cSJassi Brar } 4325033f43cSJassi Brar } 4335033f43cSJassi Brar 4345033f43cSJassi Brar i2s->op_clk = clk_get(&i2s->pdev->dev, 4355033f43cSJassi Brar i2s->src_clk[clk_id]); 4365033f43cSJassi Brar clk_enable(i2s->op_clk); 4375033f43cSJassi Brar i2s->rclk_srcrate = clk_get_rate(i2s->op_clk); 4385033f43cSJassi Brar 4395033f43cSJassi Brar /* Over-ride the other's */ 4405033f43cSJassi Brar if (other) { 4415033f43cSJassi Brar other->op_clk = i2s->op_clk; 4425033f43cSJassi Brar other->rclk_srcrate = i2s->rclk_srcrate; 4435033f43cSJassi Brar } 4445033f43cSJassi Brar } else if ((!clk_id && (mod & MOD_IMS_SYSMUX)) 4455033f43cSJassi Brar || (clk_id && !(mod & MOD_IMS_SYSMUX))) { 4465033f43cSJassi Brar dev_err(&i2s->pdev->dev, 4475033f43cSJassi Brar "%s:%d Other DAI busy\n", __func__, __LINE__); 4485033f43cSJassi Brar return -EAGAIN; 4495033f43cSJassi Brar } else { 4505033f43cSJassi Brar /* Call can't be on the active DAI */ 4515033f43cSJassi Brar i2s->op_clk = other->op_clk; 4525033f43cSJassi Brar i2s->rclk_srcrate = other->rclk_srcrate; 4535033f43cSJassi Brar return 0; 4545033f43cSJassi Brar } 4555033f43cSJassi Brar 4565033f43cSJassi Brar if (clk_id == 0) 4575033f43cSJassi Brar mod &= ~MOD_IMS_SYSMUX; 4585033f43cSJassi Brar else 4595033f43cSJassi Brar mod |= MOD_IMS_SYSMUX; 4605033f43cSJassi Brar break; 4615033f43cSJassi Brar 4625033f43cSJassi Brar default: 4635033f43cSJassi Brar dev_err(&i2s->pdev->dev, "We don't serve that!\n"); 4645033f43cSJassi Brar return -EINVAL; 4655033f43cSJassi Brar } 4665033f43cSJassi Brar 4675033f43cSJassi Brar writel(mod, i2s->addr + I2SMOD); 4685033f43cSJassi Brar 4695033f43cSJassi Brar return 0; 4705033f43cSJassi Brar } 4715033f43cSJassi Brar 4725033f43cSJassi Brar static int i2s_set_fmt(struct snd_soc_dai *dai, 4735033f43cSJassi Brar unsigned int fmt) 4745033f43cSJassi Brar { 4755033f43cSJassi Brar struct i2s_dai *i2s = to_info(dai); 4765033f43cSJassi Brar u32 mod = readl(i2s->addr + I2SMOD); 4775033f43cSJassi Brar u32 tmp = 0; 4785033f43cSJassi Brar 4795033f43cSJassi Brar /* Format is priority */ 4805033f43cSJassi Brar switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { 4815033f43cSJassi Brar case SND_SOC_DAIFMT_RIGHT_J: 4825033f43cSJassi Brar tmp |= MOD_LR_RLOW; 4835033f43cSJassi Brar tmp |= MOD_SDF_MSB; 4845033f43cSJassi Brar break; 4855033f43cSJassi Brar case SND_SOC_DAIFMT_LEFT_J: 4865033f43cSJassi Brar tmp |= MOD_LR_RLOW; 4875033f43cSJassi Brar tmp |= MOD_SDF_LSB; 4885033f43cSJassi Brar break; 4895033f43cSJassi Brar case SND_SOC_DAIFMT_I2S: 4905033f43cSJassi Brar tmp |= MOD_SDF_IIS; 4915033f43cSJassi Brar break; 4925033f43cSJassi Brar default: 4935033f43cSJassi Brar dev_err(&i2s->pdev->dev, "Format not supported\n"); 4945033f43cSJassi Brar return -EINVAL; 4955033f43cSJassi Brar } 4965033f43cSJassi Brar 4975033f43cSJassi Brar /* 4985033f43cSJassi Brar * INV flag is relative to the FORMAT flag - if set it simply 4995033f43cSJassi Brar * flips the polarity specified by the Standard 5005033f43cSJassi Brar */ 5015033f43cSJassi Brar switch (fmt & SND_SOC_DAIFMT_INV_MASK) { 5025033f43cSJassi Brar case SND_SOC_DAIFMT_NB_NF: 5035033f43cSJassi Brar break; 5045033f43cSJassi Brar case SND_SOC_DAIFMT_NB_IF: 5055033f43cSJassi Brar if (tmp & MOD_LR_RLOW) 5065033f43cSJassi Brar tmp &= ~MOD_LR_RLOW; 5075033f43cSJassi Brar else 5085033f43cSJassi Brar tmp |= MOD_LR_RLOW; 5095033f43cSJassi Brar break; 5105033f43cSJassi Brar default: 5115033f43cSJassi Brar dev_err(&i2s->pdev->dev, "Polarity not supported\n"); 5125033f43cSJassi Brar return -EINVAL; 5135033f43cSJassi Brar } 5145033f43cSJassi Brar 5155033f43cSJassi Brar switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { 5165033f43cSJassi Brar case SND_SOC_DAIFMT_CBM_CFM: 5175033f43cSJassi Brar tmp |= MOD_SLAVE; 5185033f43cSJassi Brar break; 5195033f43cSJassi Brar case SND_SOC_DAIFMT_CBS_CFS: 5205033f43cSJassi Brar /* Set default source clock in Master mode */ 5215033f43cSJassi Brar if (i2s->rclk_srcrate == 0) 5225033f43cSJassi Brar i2s_set_sysclk(dai, SAMSUNG_I2S_RCLKSRC_0, 5235033f43cSJassi Brar 0, SND_SOC_CLOCK_IN); 5245033f43cSJassi Brar break; 5255033f43cSJassi Brar default: 5265033f43cSJassi Brar dev_err(&i2s->pdev->dev, "master/slave format not supported\n"); 5275033f43cSJassi Brar return -EINVAL; 5285033f43cSJassi Brar } 5295033f43cSJassi Brar 5305033f43cSJassi Brar if (any_active(i2s) && 5315033f43cSJassi Brar ((mod & (MOD_SDF_MASK | MOD_LR_RLOW 5325033f43cSJassi Brar | MOD_SLAVE)) != tmp)) { 5335033f43cSJassi Brar dev_err(&i2s->pdev->dev, 5345033f43cSJassi Brar "%s:%d Other DAI busy\n", __func__, __LINE__); 5355033f43cSJassi Brar return -EAGAIN; 5365033f43cSJassi Brar } 5375033f43cSJassi Brar 5385033f43cSJassi Brar mod &= ~(MOD_SDF_MASK | MOD_LR_RLOW | MOD_SLAVE); 5395033f43cSJassi Brar mod |= tmp; 5405033f43cSJassi Brar writel(mod, i2s->addr + I2SMOD); 5415033f43cSJassi Brar 5425033f43cSJassi Brar return 0; 5435033f43cSJassi Brar } 5445033f43cSJassi Brar 5455033f43cSJassi Brar static int i2s_hw_params(struct snd_pcm_substream *substream, 5465033f43cSJassi Brar struct snd_pcm_hw_params *params, struct snd_soc_dai *dai) 5475033f43cSJassi Brar { 5485033f43cSJassi Brar struct i2s_dai *i2s = to_info(dai); 5495033f43cSJassi Brar u32 mod = readl(i2s->addr + I2SMOD); 5505033f43cSJassi Brar 5515033f43cSJassi Brar if (!is_secondary(i2s)) 5525033f43cSJassi Brar mod &= ~(MOD_DC2_EN | MOD_DC1_EN); 5535033f43cSJassi Brar 5545033f43cSJassi Brar switch (params_channels(params)) { 5555033f43cSJassi Brar case 6: 5565033f43cSJassi Brar mod |= MOD_DC2_EN; 5575033f43cSJassi Brar case 4: 5585033f43cSJassi Brar mod |= MOD_DC1_EN; 5595033f43cSJassi Brar break; 5605033f43cSJassi Brar case 2: 5615033f43cSJassi Brar break; 5625033f43cSJassi Brar default: 5635033f43cSJassi Brar dev_err(&i2s->pdev->dev, "%d channels not supported\n", 5645033f43cSJassi Brar params_channels(params)); 5655033f43cSJassi Brar return -EINVAL; 5665033f43cSJassi Brar } 5675033f43cSJassi Brar 5685033f43cSJassi Brar if (is_secondary(i2s)) 5695033f43cSJassi Brar mod &= ~MOD_BLCS_MASK; 5705033f43cSJassi Brar else 5715033f43cSJassi Brar mod &= ~MOD_BLCP_MASK; 5725033f43cSJassi Brar 5735033f43cSJassi Brar if (is_manager(i2s)) 5745033f43cSJassi Brar mod &= ~MOD_BLC_MASK; 5755033f43cSJassi Brar 5765033f43cSJassi Brar switch (params_format(params)) { 5775033f43cSJassi Brar case SNDRV_PCM_FORMAT_S8: 5785033f43cSJassi Brar if (is_secondary(i2s)) 5795033f43cSJassi Brar mod |= MOD_BLCS_8BIT; 5805033f43cSJassi Brar else 5815033f43cSJassi Brar mod |= MOD_BLCP_8BIT; 5825033f43cSJassi Brar if (is_manager(i2s)) 5835033f43cSJassi Brar mod |= MOD_BLC_8BIT; 5845033f43cSJassi Brar break; 5855033f43cSJassi Brar case SNDRV_PCM_FORMAT_S16_LE: 5865033f43cSJassi Brar if (is_secondary(i2s)) 5875033f43cSJassi Brar mod |= MOD_BLCS_16BIT; 5885033f43cSJassi Brar else 5895033f43cSJassi Brar mod |= MOD_BLCP_16BIT; 5905033f43cSJassi Brar if (is_manager(i2s)) 5915033f43cSJassi Brar mod |= MOD_BLC_16BIT; 5925033f43cSJassi Brar break; 5935033f43cSJassi Brar case SNDRV_PCM_FORMAT_S24_LE: 5945033f43cSJassi Brar if (is_secondary(i2s)) 5955033f43cSJassi Brar mod |= MOD_BLCS_24BIT; 5965033f43cSJassi Brar else 5975033f43cSJassi Brar mod |= MOD_BLCP_24BIT; 5985033f43cSJassi Brar if (is_manager(i2s)) 5995033f43cSJassi Brar mod |= MOD_BLC_24BIT; 6005033f43cSJassi Brar break; 6015033f43cSJassi Brar default: 6025033f43cSJassi Brar dev_err(&i2s->pdev->dev, "Format(%d) not supported\n", 6035033f43cSJassi Brar params_format(params)); 6045033f43cSJassi Brar return -EINVAL; 6055033f43cSJassi Brar } 6065033f43cSJassi Brar writel(mod, i2s->addr + I2SMOD); 6075033f43cSJassi Brar 6085033f43cSJassi Brar if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 6095033f43cSJassi Brar snd_soc_dai_set_dma_data(dai, substream, 6105033f43cSJassi Brar (void *)&i2s->dma_playback); 6115033f43cSJassi Brar else 6125033f43cSJassi Brar snd_soc_dai_set_dma_data(dai, substream, 6135033f43cSJassi Brar (void *)&i2s->dma_capture); 6145033f43cSJassi Brar 6155033f43cSJassi Brar i2s->frmclk = params_rate(params); 6165033f43cSJassi Brar 6175033f43cSJassi Brar return 0; 6185033f43cSJassi Brar } 6195033f43cSJassi Brar 6205033f43cSJassi Brar /* We set constraints on the substream acc to the version of I2S */ 6215033f43cSJassi Brar static int i2s_startup(struct snd_pcm_substream *substream, 6225033f43cSJassi Brar struct snd_soc_dai *dai) 6235033f43cSJassi Brar { 6245033f43cSJassi Brar struct i2s_dai *i2s = to_info(dai); 6255033f43cSJassi Brar struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai; 6265033f43cSJassi Brar unsigned long flags; 6275033f43cSJassi Brar 6285033f43cSJassi Brar spin_lock_irqsave(&lock, flags); 6295033f43cSJassi Brar 6305033f43cSJassi Brar i2s->mode |= DAI_OPENED; 6315033f43cSJassi Brar 6325033f43cSJassi Brar if (is_manager(other)) 6335033f43cSJassi Brar i2s->mode &= ~DAI_MANAGER; 6345033f43cSJassi Brar else 6355033f43cSJassi Brar i2s->mode |= DAI_MANAGER; 6365033f43cSJassi Brar 6375033f43cSJassi Brar /* Enforce set_sysclk in Master mode */ 6385033f43cSJassi Brar i2s->rclk_srcrate = 0; 6395033f43cSJassi Brar 6405033f43cSJassi Brar spin_unlock_irqrestore(&lock, flags); 6415033f43cSJassi Brar 6425033f43cSJassi Brar return 0; 6435033f43cSJassi Brar } 6445033f43cSJassi Brar 6455033f43cSJassi Brar static void i2s_shutdown(struct snd_pcm_substream *substream, 6465033f43cSJassi Brar struct snd_soc_dai *dai) 6475033f43cSJassi Brar { 6485033f43cSJassi Brar struct i2s_dai *i2s = to_info(dai); 6495033f43cSJassi Brar struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai; 6505033f43cSJassi Brar unsigned long flags; 6515033f43cSJassi Brar 6525033f43cSJassi Brar spin_lock_irqsave(&lock, flags); 6535033f43cSJassi Brar 6545033f43cSJassi Brar i2s->mode &= ~DAI_OPENED; 6555033f43cSJassi Brar i2s->mode &= ~DAI_MANAGER; 6565033f43cSJassi Brar 6575033f43cSJassi Brar if (is_opened(other)) 6585033f43cSJassi Brar other->mode |= DAI_MANAGER; 6595033f43cSJassi Brar 6605033f43cSJassi Brar /* Reset any constraint on RFS and BFS */ 6615033f43cSJassi Brar i2s->rfs = 0; 6625033f43cSJassi Brar i2s->bfs = 0; 6635033f43cSJassi Brar 6645033f43cSJassi Brar spin_unlock_irqrestore(&lock, flags); 6655033f43cSJassi Brar 6665033f43cSJassi Brar /* Gate CDCLK by default */ 6675033f43cSJassi Brar if (!is_opened(other)) 6685033f43cSJassi Brar i2s_set_sysclk(dai, SAMSUNG_I2S_CDCLK, 6695033f43cSJassi Brar 0, SND_SOC_CLOCK_IN); 6705033f43cSJassi Brar } 6715033f43cSJassi Brar 6725033f43cSJassi Brar static int config_setup(struct i2s_dai *i2s) 6735033f43cSJassi Brar { 6745033f43cSJassi Brar struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai; 6755033f43cSJassi Brar unsigned rfs, bfs, blc; 6765033f43cSJassi Brar u32 psr; 6775033f43cSJassi Brar 6785033f43cSJassi Brar blc = get_blc(i2s); 6795033f43cSJassi Brar 6805033f43cSJassi Brar bfs = i2s->bfs; 6815033f43cSJassi Brar 6825033f43cSJassi Brar if (!bfs && other) 6835033f43cSJassi Brar bfs = other->bfs; 6845033f43cSJassi Brar 6855033f43cSJassi Brar /* Select least possible multiple(2) if no constraint set */ 6865033f43cSJassi Brar if (!bfs) 6875033f43cSJassi Brar bfs = blc * 2; 6885033f43cSJassi Brar 6895033f43cSJassi Brar rfs = i2s->rfs; 6905033f43cSJassi Brar 6915033f43cSJassi Brar if (!rfs && other) 6925033f43cSJassi Brar rfs = other->rfs; 6935033f43cSJassi Brar 6945033f43cSJassi Brar if ((rfs == 256 || rfs == 512) && (blc == 24)) { 6955033f43cSJassi Brar dev_err(&i2s->pdev->dev, 6965033f43cSJassi Brar "%d-RFS not supported for 24-blc\n", rfs); 6975033f43cSJassi Brar return -EINVAL; 6985033f43cSJassi Brar } 6995033f43cSJassi Brar 7005033f43cSJassi Brar if (!rfs) { 7015033f43cSJassi Brar if (bfs == 16 || bfs == 32) 7025033f43cSJassi Brar rfs = 256; 7035033f43cSJassi Brar else 7045033f43cSJassi Brar rfs = 384; 7055033f43cSJassi Brar } 7065033f43cSJassi Brar 7075033f43cSJassi Brar /* If already setup and running */ 7085033f43cSJassi Brar if (any_active(i2s) && (get_rfs(i2s) != rfs || get_bfs(i2s) != bfs)) { 7095033f43cSJassi Brar dev_err(&i2s->pdev->dev, 7105033f43cSJassi Brar "%s:%d Other DAI busy\n", __func__, __LINE__); 7115033f43cSJassi Brar return -EAGAIN; 7125033f43cSJassi Brar } 7135033f43cSJassi Brar 7145033f43cSJassi Brar /* Don't bother RFS, BFS & PSR in Slave mode */ 7155033f43cSJassi Brar if (is_slave(i2s)) 7165033f43cSJassi Brar return 0; 7175033f43cSJassi Brar 7185033f43cSJassi Brar set_bfs(i2s, bfs); 7195033f43cSJassi Brar set_rfs(i2s, rfs); 7205033f43cSJassi Brar 7215033f43cSJassi Brar if (!(i2s->quirks & QUIRK_NO_MUXPSR)) { 7225033f43cSJassi Brar psr = i2s->rclk_srcrate / i2s->frmclk / rfs; 7235033f43cSJassi Brar writel(((psr - 1) << 8) | PSR_PSREN, i2s->addr + I2SPSR); 7245033f43cSJassi Brar dev_dbg(&i2s->pdev->dev, 7255033f43cSJassi Brar "RCLK_SRC=%luHz PSR=%u, RCLK=%dfs, BCLK=%dfs\n", 7265033f43cSJassi Brar i2s->rclk_srcrate, psr, rfs, bfs); 7275033f43cSJassi Brar } 7285033f43cSJassi Brar 7295033f43cSJassi Brar return 0; 7305033f43cSJassi Brar } 7315033f43cSJassi Brar 7325033f43cSJassi Brar static int i2s_trigger(struct snd_pcm_substream *substream, 7335033f43cSJassi Brar int cmd, struct snd_soc_dai *dai) 7345033f43cSJassi Brar { 7355033f43cSJassi Brar int capture = (substream->stream == SNDRV_PCM_STREAM_CAPTURE); 7365033f43cSJassi Brar struct snd_soc_pcm_runtime *rtd = substream->private_data; 7375033f43cSJassi Brar struct i2s_dai *i2s = to_info(rtd->cpu_dai); 7385033f43cSJassi Brar unsigned long flags; 7395033f43cSJassi Brar 7405033f43cSJassi Brar switch (cmd) { 7415033f43cSJassi Brar case SNDRV_PCM_TRIGGER_START: 7425033f43cSJassi Brar case SNDRV_PCM_TRIGGER_RESUME: 7435033f43cSJassi Brar case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 7445033f43cSJassi Brar local_irq_save(flags); 7455033f43cSJassi Brar 7465033f43cSJassi Brar if (config_setup(i2s)) { 7475033f43cSJassi Brar local_irq_restore(flags); 7485033f43cSJassi Brar return -EINVAL; 7495033f43cSJassi Brar } 7505033f43cSJassi Brar 7515033f43cSJassi Brar if (capture) 7525033f43cSJassi Brar i2s_rxctrl(i2s, 1); 7535033f43cSJassi Brar else 7545033f43cSJassi Brar i2s_txctrl(i2s, 1); 7555033f43cSJassi Brar 7565033f43cSJassi Brar local_irq_restore(flags); 7575033f43cSJassi Brar break; 7585033f43cSJassi Brar case SNDRV_PCM_TRIGGER_STOP: 7595033f43cSJassi Brar case SNDRV_PCM_TRIGGER_SUSPEND: 7605033f43cSJassi Brar case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 7615033f43cSJassi Brar local_irq_save(flags); 7625033f43cSJassi Brar 7635033f43cSJassi Brar if (capture) 7645033f43cSJassi Brar i2s_rxctrl(i2s, 0); 7655033f43cSJassi Brar else 7665033f43cSJassi Brar i2s_txctrl(i2s, 0); 7675033f43cSJassi Brar 768775bc971SJassi Brar if (capture) 769775bc971SJassi Brar i2s_fifo(i2s, FIC_RXFLUSH); 770775bc971SJassi Brar else 771775bc971SJassi Brar i2s_fifo(i2s, FIC_TXFLUSH); 772775bc971SJassi Brar 7735033f43cSJassi Brar local_irq_restore(flags); 7745033f43cSJassi Brar break; 7755033f43cSJassi Brar } 7765033f43cSJassi Brar 7775033f43cSJassi Brar return 0; 7785033f43cSJassi Brar } 7795033f43cSJassi Brar 7805033f43cSJassi Brar static int i2s_set_clkdiv(struct snd_soc_dai *dai, 7815033f43cSJassi Brar int div_id, int div) 7825033f43cSJassi Brar { 7835033f43cSJassi Brar struct i2s_dai *i2s = to_info(dai); 7845033f43cSJassi Brar struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai; 7855033f43cSJassi Brar 7865033f43cSJassi Brar switch (div_id) { 7875033f43cSJassi Brar case SAMSUNG_I2S_DIV_BCLK: 7885033f43cSJassi Brar if ((any_active(i2s) && div && (get_bfs(i2s) != div)) 7895033f43cSJassi Brar || (other && other->bfs && (other->bfs != div))) { 7905033f43cSJassi Brar dev_err(&i2s->pdev->dev, 7915033f43cSJassi Brar "%s:%d Other DAI busy\n", __func__, __LINE__); 7925033f43cSJassi Brar return -EAGAIN; 7935033f43cSJassi Brar } 7945033f43cSJassi Brar i2s->bfs = div; 7955033f43cSJassi Brar break; 7965033f43cSJassi Brar default: 7975033f43cSJassi Brar dev_err(&i2s->pdev->dev, 7985033f43cSJassi Brar "Invalid clock divider(%d)\n", div_id); 7995033f43cSJassi Brar return -EINVAL; 8005033f43cSJassi Brar } 8015033f43cSJassi Brar 8025033f43cSJassi Brar return 0; 8035033f43cSJassi Brar } 8045033f43cSJassi Brar 8055033f43cSJassi Brar static snd_pcm_sframes_t 8065033f43cSJassi Brar i2s_delay(struct snd_pcm_substream *substream, struct snd_soc_dai *dai) 8075033f43cSJassi Brar { 8085033f43cSJassi Brar struct i2s_dai *i2s = to_info(dai); 8095033f43cSJassi Brar u32 reg = readl(i2s->addr + I2SFIC); 8105033f43cSJassi Brar snd_pcm_sframes_t delay; 8115033f43cSJassi Brar 8125033f43cSJassi Brar if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) 8135033f43cSJassi Brar delay = FIC_RXCOUNT(reg); 8145033f43cSJassi Brar else if (is_secondary(i2s)) 8155033f43cSJassi Brar delay = FICS_TXCOUNT(readl(i2s->addr + I2SFICS)); 8165033f43cSJassi Brar else 8175033f43cSJassi Brar delay = FIC_TXCOUNT(reg); 8185033f43cSJassi Brar 8195033f43cSJassi Brar return delay; 8205033f43cSJassi Brar } 8215033f43cSJassi Brar 8225033f43cSJassi Brar #ifdef CONFIG_PM 8235033f43cSJassi Brar static int i2s_suspend(struct snd_soc_dai *dai) 8245033f43cSJassi Brar { 8255033f43cSJassi Brar struct i2s_dai *i2s = to_info(dai); 8265033f43cSJassi Brar 8275033f43cSJassi Brar if (dai->active) { 8285033f43cSJassi Brar i2s->suspend_i2smod = readl(i2s->addr + I2SMOD); 8295033f43cSJassi Brar i2s->suspend_i2scon = readl(i2s->addr + I2SCON); 8305033f43cSJassi Brar i2s->suspend_i2spsr = readl(i2s->addr + I2SPSR); 8315033f43cSJassi Brar } 8325033f43cSJassi Brar 8335033f43cSJassi Brar return 0; 8345033f43cSJassi Brar } 8355033f43cSJassi Brar 8365033f43cSJassi Brar static int i2s_resume(struct snd_soc_dai *dai) 8375033f43cSJassi Brar { 8385033f43cSJassi Brar struct i2s_dai *i2s = to_info(dai); 8395033f43cSJassi Brar 8405033f43cSJassi Brar if (dai->active) { 8415033f43cSJassi Brar writel(i2s->suspend_i2scon, i2s->addr + I2SCON); 8425033f43cSJassi Brar writel(i2s->suspend_i2smod, i2s->addr + I2SMOD); 8435033f43cSJassi Brar writel(i2s->suspend_i2spsr, i2s->addr + I2SPSR); 8445033f43cSJassi Brar } 8455033f43cSJassi Brar 8465033f43cSJassi Brar return 0; 8475033f43cSJassi Brar } 8485033f43cSJassi Brar #else 8495033f43cSJassi Brar #define i2s_suspend NULL 8505033f43cSJassi Brar #define i2s_resume NULL 8515033f43cSJassi Brar #endif 8525033f43cSJassi Brar 8535033f43cSJassi Brar static int samsung_i2s_dai_probe(struct snd_soc_dai *dai) 8545033f43cSJassi Brar { 8555033f43cSJassi Brar struct i2s_dai *i2s = to_info(dai); 8565033f43cSJassi Brar struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai; 8575033f43cSJassi Brar 8585033f43cSJassi Brar if (other && other->clk) /* If this is probe on secondary */ 8595033f43cSJassi Brar goto probe_exit; 8605033f43cSJassi Brar 8615033f43cSJassi Brar i2s->addr = ioremap(i2s->base, 0x100); 8625033f43cSJassi Brar if (i2s->addr == NULL) { 8635033f43cSJassi Brar dev_err(&i2s->pdev->dev, "cannot ioremap registers\n"); 8645033f43cSJassi Brar return -ENXIO; 8655033f43cSJassi Brar } 8665033f43cSJassi Brar 8675033f43cSJassi Brar i2s->clk = clk_get(&i2s->pdev->dev, "iis"); 8685033f43cSJassi Brar if (IS_ERR(i2s->clk)) { 8695033f43cSJassi Brar dev_err(&i2s->pdev->dev, "failed to get i2s_clock\n"); 8705033f43cSJassi Brar iounmap(i2s->addr); 8715033f43cSJassi Brar return -ENOENT; 8725033f43cSJassi Brar } 8735033f43cSJassi Brar clk_enable(i2s->clk); 8745033f43cSJassi Brar 8755033f43cSJassi Brar if (other) { 8765033f43cSJassi Brar other->addr = i2s->addr; 8775033f43cSJassi Brar other->clk = i2s->clk; 8785033f43cSJassi Brar } 8795033f43cSJassi Brar 8805033f43cSJassi Brar if (i2s->quirks & QUIRK_NEED_RSTCLR) 8815033f43cSJassi Brar writel(CON_RSTCLR, i2s->addr + I2SCON); 8825033f43cSJassi Brar 88361100f40SSangbeom Kim if (i2s->quirks & QUIRK_SEC_DAI) 8849b8f5695SMark Brown idma_reg_addr_init(i2s->addr, 88561100f40SSangbeom Kim i2s->sec_dai->idma_playback.dma_addr); 88661100f40SSangbeom Kim 8875033f43cSJassi Brar probe_exit: 8885033f43cSJassi Brar /* Reset any constraint on RFS and BFS */ 8895033f43cSJassi Brar i2s->rfs = 0; 8905033f43cSJassi Brar i2s->bfs = 0; 8915033f43cSJassi Brar i2s_txctrl(i2s, 0); 8925033f43cSJassi Brar i2s_rxctrl(i2s, 0); 8935033f43cSJassi Brar i2s_fifo(i2s, FIC_TXFLUSH); 8945033f43cSJassi Brar i2s_fifo(other, FIC_TXFLUSH); 8955033f43cSJassi Brar i2s_fifo(i2s, FIC_RXFLUSH); 8965033f43cSJassi Brar 8975033f43cSJassi Brar /* Gate CDCLK by default */ 8985033f43cSJassi Brar if (!is_opened(other)) 8995033f43cSJassi Brar i2s_set_sysclk(dai, SAMSUNG_I2S_CDCLK, 9005033f43cSJassi Brar 0, SND_SOC_CLOCK_IN); 9015033f43cSJassi Brar 9025033f43cSJassi Brar return 0; 9035033f43cSJassi Brar } 9045033f43cSJassi Brar 9055033f43cSJassi Brar static int samsung_i2s_dai_remove(struct snd_soc_dai *dai) 9065033f43cSJassi Brar { 9075033f43cSJassi Brar struct i2s_dai *i2s = snd_soc_dai_get_drvdata(dai); 9085033f43cSJassi Brar struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai; 9095033f43cSJassi Brar 9105033f43cSJassi Brar if (!other || !other->clk) { 9115033f43cSJassi Brar 9125033f43cSJassi Brar if (i2s->quirks & QUIRK_NEED_RSTCLR) 9135033f43cSJassi Brar writel(0, i2s->addr + I2SCON); 9145033f43cSJassi Brar 9155033f43cSJassi Brar clk_disable(i2s->clk); 9165033f43cSJassi Brar clk_put(i2s->clk); 9175033f43cSJassi Brar 9185033f43cSJassi Brar iounmap(i2s->addr); 9195033f43cSJassi Brar } 9205033f43cSJassi Brar 9215033f43cSJassi Brar i2s->clk = NULL; 9225033f43cSJassi Brar 9235033f43cSJassi Brar return 0; 9245033f43cSJassi Brar } 9255033f43cSJassi Brar 92685e7652dSLars-Peter Clausen static const struct snd_soc_dai_ops samsung_i2s_dai_ops = { 9275033f43cSJassi Brar .trigger = i2s_trigger, 9285033f43cSJassi Brar .hw_params = i2s_hw_params, 9295033f43cSJassi Brar .set_fmt = i2s_set_fmt, 9305033f43cSJassi Brar .set_clkdiv = i2s_set_clkdiv, 9315033f43cSJassi Brar .set_sysclk = i2s_set_sysclk, 9325033f43cSJassi Brar .startup = i2s_startup, 9335033f43cSJassi Brar .shutdown = i2s_shutdown, 9345033f43cSJassi Brar .delay = i2s_delay, 9355033f43cSJassi Brar }; 9365033f43cSJassi Brar 9375033f43cSJassi Brar #define SAMSUNG_I2S_RATES SNDRV_PCM_RATE_8000_96000 9385033f43cSJassi Brar 9395033f43cSJassi Brar #define SAMSUNG_I2S_FMTS (SNDRV_PCM_FMTBIT_S8 | \ 9405033f43cSJassi Brar SNDRV_PCM_FMTBIT_S16_LE | \ 9415033f43cSJassi Brar SNDRV_PCM_FMTBIT_S24_LE) 9425033f43cSJassi Brar 9435033f43cSJassi Brar static __devinit 9445033f43cSJassi Brar struct i2s_dai *i2s_alloc_dai(struct platform_device *pdev, bool sec) 9455033f43cSJassi Brar { 9465033f43cSJassi Brar struct i2s_dai *i2s; 9475033f43cSJassi Brar 948*b960ce74SMark Brown i2s = devm_kzalloc(&pdev->dev, sizeof(struct i2s_dai), GFP_KERNEL); 9495033f43cSJassi Brar if (i2s == NULL) 9505033f43cSJassi Brar return NULL; 9515033f43cSJassi Brar 9525033f43cSJassi Brar i2s->pdev = pdev; 9535033f43cSJassi Brar i2s->pri_dai = NULL; 9545033f43cSJassi Brar i2s->sec_dai = NULL; 9555033f43cSJassi Brar i2s->i2s_dai_drv.symmetric_rates = 1; 9565033f43cSJassi Brar i2s->i2s_dai_drv.probe = samsung_i2s_dai_probe; 9575033f43cSJassi Brar i2s->i2s_dai_drv.remove = samsung_i2s_dai_remove; 9585033f43cSJassi Brar i2s->i2s_dai_drv.ops = &samsung_i2s_dai_ops; 9595033f43cSJassi Brar i2s->i2s_dai_drv.suspend = i2s_suspend; 9605033f43cSJassi Brar i2s->i2s_dai_drv.resume = i2s_resume; 9615033f43cSJassi Brar i2s->i2s_dai_drv.playback.channels_min = 2; 9625033f43cSJassi Brar i2s->i2s_dai_drv.playback.channels_max = 2; 9635033f43cSJassi Brar i2s->i2s_dai_drv.playback.rates = SAMSUNG_I2S_RATES; 9645033f43cSJassi Brar i2s->i2s_dai_drv.playback.formats = SAMSUNG_I2S_FMTS; 9655033f43cSJassi Brar 9665033f43cSJassi Brar if (!sec) { 9675033f43cSJassi Brar i2s->i2s_dai_drv.capture.channels_min = 2; 9685033f43cSJassi Brar i2s->i2s_dai_drv.capture.channels_max = 2; 9695033f43cSJassi Brar i2s->i2s_dai_drv.capture.rates = SAMSUNG_I2S_RATES; 9705033f43cSJassi Brar i2s->i2s_dai_drv.capture.formats = SAMSUNG_I2S_FMTS; 9715033f43cSJassi Brar } else { /* Create a new platform_device for Secondary */ 9725033f43cSJassi Brar i2s->pdev = platform_device_register_resndata(NULL, 9735033f43cSJassi Brar pdev->name, pdev->id + SAMSUNG_I2S_SECOFF, 9745033f43cSJassi Brar NULL, 0, NULL, 0); 975*b960ce74SMark Brown if (IS_ERR(i2s->pdev)) 9765033f43cSJassi Brar return NULL; 9775033f43cSJassi Brar } 9785033f43cSJassi Brar 9795033f43cSJassi Brar /* Pre-assign snd_soc_dai_set_drvdata */ 9805033f43cSJassi Brar dev_set_drvdata(&i2s->pdev->dev, i2s); 9815033f43cSJassi Brar 9825033f43cSJassi Brar return i2s; 9835033f43cSJassi Brar } 9845033f43cSJassi Brar 9855033f43cSJassi Brar static __devinit int samsung_i2s_probe(struct platform_device *pdev) 9865033f43cSJassi Brar { 9875033f43cSJassi Brar u32 dma_pl_chan, dma_cp_chan, dma_pl_sec_chan; 9885033f43cSJassi Brar struct i2s_dai *pri_dai, *sec_dai = NULL; 9895033f43cSJassi Brar struct s3c_audio_pdata *i2s_pdata; 9905033f43cSJassi Brar struct samsung_i2s *i2s_cfg; 9915033f43cSJassi Brar struct resource *res; 9925033f43cSJassi Brar u32 regs_base, quirks; 9935033f43cSJassi Brar int ret = 0; 9945033f43cSJassi Brar 9955033f43cSJassi Brar /* Call during Seconday interface registration */ 9965033f43cSJassi Brar if (pdev->id >= SAMSUNG_I2S_SECOFF) { 9975033f43cSJassi Brar sec_dai = dev_get_drvdata(&pdev->dev); 9985033f43cSJassi Brar snd_soc_register_dai(&sec_dai->pdev->dev, 9995033f43cSJassi Brar &sec_dai->i2s_dai_drv); 10005033f43cSJassi Brar return 0; 10015033f43cSJassi Brar } 10025033f43cSJassi Brar 10035033f43cSJassi Brar i2s_pdata = pdev->dev.platform_data; 10045033f43cSJassi Brar if (i2s_pdata == NULL) { 10055033f43cSJassi Brar dev_err(&pdev->dev, "Can't work without s3c_audio_pdata\n"); 10065033f43cSJassi Brar return -EINVAL; 10075033f43cSJassi Brar } 10085033f43cSJassi Brar 10095033f43cSJassi Brar res = platform_get_resource(pdev, IORESOURCE_DMA, 0); 10105033f43cSJassi Brar if (!res) { 10115033f43cSJassi Brar dev_err(&pdev->dev, "Unable to get I2S-TX dma resource\n"); 10125033f43cSJassi Brar return -ENXIO; 10135033f43cSJassi Brar } 10145033f43cSJassi Brar dma_pl_chan = res->start; 10155033f43cSJassi Brar 10165033f43cSJassi Brar res = platform_get_resource(pdev, IORESOURCE_DMA, 1); 10175033f43cSJassi Brar if (!res) { 10185033f43cSJassi Brar dev_err(&pdev->dev, "Unable to get I2S-RX dma resource\n"); 10195033f43cSJassi Brar return -ENXIO; 10205033f43cSJassi Brar } 10215033f43cSJassi Brar dma_cp_chan = res->start; 10225033f43cSJassi Brar 10235033f43cSJassi Brar res = platform_get_resource(pdev, IORESOURCE_DMA, 2); 10245033f43cSJassi Brar if (res) 10255033f43cSJassi Brar dma_pl_sec_chan = res->start; 10265033f43cSJassi Brar else 10275033f43cSJassi Brar dma_pl_sec_chan = 0; 10285033f43cSJassi Brar 10295033f43cSJassi Brar res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 10305033f43cSJassi Brar if (!res) { 10315033f43cSJassi Brar dev_err(&pdev->dev, "Unable to get I2S SFR address\n"); 10325033f43cSJassi Brar return -ENXIO; 10335033f43cSJassi Brar } 10345033f43cSJassi Brar 10355033f43cSJassi Brar if (!request_mem_region(res->start, resource_size(res), 10365033f43cSJassi Brar "samsung-i2s")) { 10375033f43cSJassi Brar dev_err(&pdev->dev, "Unable to request SFR region\n"); 10385033f43cSJassi Brar return -EBUSY; 10395033f43cSJassi Brar } 10405033f43cSJassi Brar regs_base = res->start; 10415033f43cSJassi Brar 10425033f43cSJassi Brar i2s_cfg = &i2s_pdata->type.i2s; 10435033f43cSJassi Brar quirks = i2s_cfg->quirks; 10445033f43cSJassi Brar 10455033f43cSJassi Brar pri_dai = i2s_alloc_dai(pdev, false); 10465033f43cSJassi Brar if (!pri_dai) { 10475033f43cSJassi Brar dev_err(&pdev->dev, "Unable to alloc I2S_pri\n"); 10485033f43cSJassi Brar ret = -ENOMEM; 1049*b960ce74SMark Brown goto err; 10505033f43cSJassi Brar } 10515033f43cSJassi Brar 10525033f43cSJassi Brar pri_dai->dma_playback.dma_addr = regs_base + I2STXD; 10535033f43cSJassi Brar pri_dai->dma_capture.dma_addr = regs_base + I2SRXD; 10545033f43cSJassi Brar pri_dai->dma_playback.client = 10555033f43cSJassi Brar (struct s3c2410_dma_client *)&pri_dai->dma_playback; 10565033f43cSJassi Brar pri_dai->dma_capture.client = 10575033f43cSJassi Brar (struct s3c2410_dma_client *)&pri_dai->dma_capture; 10585033f43cSJassi Brar pri_dai->dma_playback.channel = dma_pl_chan; 10595033f43cSJassi Brar pri_dai->dma_capture.channel = dma_cp_chan; 10605033f43cSJassi Brar pri_dai->src_clk = i2s_cfg->src_clk; 10615033f43cSJassi Brar pri_dai->dma_playback.dma_size = 4; 10625033f43cSJassi Brar pri_dai->dma_capture.dma_size = 4; 10635033f43cSJassi Brar pri_dai->base = regs_base; 10645033f43cSJassi Brar pri_dai->quirks = quirks; 10655033f43cSJassi Brar 10665033f43cSJassi Brar if (quirks & QUIRK_PRI_6CHAN) 10675033f43cSJassi Brar pri_dai->i2s_dai_drv.playback.channels_max = 6; 10685033f43cSJassi Brar 10695033f43cSJassi Brar if (quirks & QUIRK_SEC_DAI) { 10705033f43cSJassi Brar sec_dai = i2s_alloc_dai(pdev, true); 10715033f43cSJassi Brar if (!sec_dai) { 10725033f43cSJassi Brar dev_err(&pdev->dev, "Unable to alloc I2S_sec\n"); 10735033f43cSJassi Brar ret = -ENOMEM; 1074*b960ce74SMark Brown goto err; 10755033f43cSJassi Brar } 10765033f43cSJassi Brar sec_dai->dma_playback.dma_addr = regs_base + I2STXDS; 10775033f43cSJassi Brar sec_dai->dma_playback.client = 10785033f43cSJassi Brar (struct s3c2410_dma_client *)&sec_dai->dma_playback; 10795033f43cSJassi Brar /* Use iDMA always if SysDMA not provided */ 10805033f43cSJassi Brar sec_dai->dma_playback.channel = dma_pl_sec_chan ? : -1; 10815033f43cSJassi Brar sec_dai->src_clk = i2s_cfg->src_clk; 10825033f43cSJassi Brar sec_dai->dma_playback.dma_size = 4; 10835033f43cSJassi Brar sec_dai->base = regs_base; 10845033f43cSJassi Brar sec_dai->quirks = quirks; 108561100f40SSangbeom Kim sec_dai->idma_playback.dma_addr = i2s_cfg->idma_addr; 10865033f43cSJassi Brar sec_dai->pri_dai = pri_dai; 10875033f43cSJassi Brar pri_dai->sec_dai = sec_dai; 10885033f43cSJassi Brar } 10895033f43cSJassi Brar 10905033f43cSJassi Brar if (i2s_pdata->cfg_gpio && i2s_pdata->cfg_gpio(pdev)) { 10915033f43cSJassi Brar dev_err(&pdev->dev, "Unable to configure gpio\n"); 10925033f43cSJassi Brar ret = -EINVAL; 1093*b960ce74SMark Brown goto err; 10945033f43cSJassi Brar } 10955033f43cSJassi Brar 10965033f43cSJassi Brar snd_soc_register_dai(&pri_dai->pdev->dev, &pri_dai->i2s_dai_drv); 10975033f43cSJassi Brar 10985033f43cSJassi Brar return 0; 1099*b960ce74SMark Brown err: 11005033f43cSJassi Brar release_mem_region(regs_base, resource_size(res)); 11015033f43cSJassi Brar 11025033f43cSJassi Brar return ret; 11035033f43cSJassi Brar } 11045033f43cSJassi Brar 11055033f43cSJassi Brar static __devexit int samsung_i2s_remove(struct platform_device *pdev) 11065033f43cSJassi Brar { 11075033f43cSJassi Brar struct i2s_dai *i2s, *other; 11085033f43cSJassi Brar 11095033f43cSJassi Brar i2s = dev_get_drvdata(&pdev->dev); 11105033f43cSJassi Brar other = i2s->pri_dai ? : i2s->sec_dai; 11115033f43cSJassi Brar 11125033f43cSJassi Brar if (other) { 11135033f43cSJassi Brar other->pri_dai = NULL; 11145033f43cSJassi Brar other->sec_dai = NULL; 11155033f43cSJassi Brar } else { 11165033f43cSJassi Brar struct resource *res; 11175033f43cSJassi Brar res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 11185033f43cSJassi Brar if (res) 11195033f43cSJassi Brar release_mem_region(res->start, resource_size(res)); 11205033f43cSJassi Brar } 11215033f43cSJassi Brar 11225033f43cSJassi Brar i2s->pri_dai = NULL; 11235033f43cSJassi Brar i2s->sec_dai = NULL; 11245033f43cSJassi Brar 11255033f43cSJassi Brar snd_soc_unregister_dai(&pdev->dev); 11265033f43cSJassi Brar 11275033f43cSJassi Brar return 0; 11285033f43cSJassi Brar } 11295033f43cSJassi Brar 11305033f43cSJassi Brar static struct platform_driver samsung_i2s_driver = { 11315033f43cSJassi Brar .probe = samsung_i2s_probe, 1132c4c5839fSAxel Lin .remove = __devexit_p(samsung_i2s_remove), 11335033f43cSJassi Brar .driver = { 11345033f43cSJassi Brar .name = "samsung-i2s", 11355033f43cSJassi Brar .owner = THIS_MODULE, 11365033f43cSJassi Brar }, 11375033f43cSJassi Brar }; 11385033f43cSJassi Brar 1139e00c3f55SMark Brown module_platform_driver(samsung_i2s_driver); 11405033f43cSJassi Brar 11415033f43cSJassi Brar /* Module information */ 11425033f43cSJassi Brar MODULE_AUTHOR("Jaswinder Singh, <jassi.brar@samsung.com>"); 11435033f43cSJassi Brar MODULE_DESCRIPTION("Samsung I2S Interface"); 11445033f43cSJassi Brar MODULE_ALIAS("platform:samsung-i2s"); 11455033f43cSJassi Brar MODULE_LICENSE("GPL"); 1146