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. 6df8ad335SJaswinder Singh * Jaswinder Singh <jassisinghbrar@gmail.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> 1840476f61SPadmavathi Venna #include <linux/of.h> 1940476f61SPadmavathi Venna #include <linux/of_gpio.h> 20c5cf4dbcSMark Brown #include <linux/pm_runtime.h> 215033f43cSJassi Brar 225033f43cSJassi Brar #include <sound/soc.h> 230378b6acSSeungwhan Youn #include <sound/pcm_params.h> 245033f43cSJassi Brar 25436d42c6SArnd Bergmann #include <linux/platform_data/asoc-s3c.h> 265033f43cSJassi Brar 275033f43cSJassi Brar #include "dma.h" 2861100f40SSangbeom Kim #include "idma.h" 295033f43cSJassi Brar #include "i2s.h" 30172a453dSSangbeom Kim #include "i2s-regs.h" 315033f43cSJassi Brar 325033f43cSJassi Brar #define msecs_to_loops(t) (loops_per_jiffy / 1000 * HZ * t) 335033f43cSJassi Brar 347c62eebbSPadmavathi Venna enum samsung_dai_type { 357c62eebbSPadmavathi Venna TYPE_PRI, 367c62eebbSPadmavathi Venna TYPE_SEC, 377c62eebbSPadmavathi Venna }; 387c62eebbSPadmavathi Venna 39a5a56871SPadmavathi Venna struct samsung_i2s_variant_regs { 40a5a56871SPadmavathi Venna unsigned int bfs_off; 41a5a56871SPadmavathi Venna unsigned int rfs_off; 42a5a56871SPadmavathi Venna unsigned int sdf_off; 43a5a56871SPadmavathi Venna unsigned int txr_off; 44a5a56871SPadmavathi Venna unsigned int rclksrc_off; 45a5a56871SPadmavathi Venna unsigned int mss_off; 46a5a56871SPadmavathi Venna unsigned int cdclkcon_off; 47a5a56871SPadmavathi Venna unsigned int lrp_off; 48a5a56871SPadmavathi Venna unsigned int bfs_mask; 49a5a56871SPadmavathi Venna unsigned int rfs_mask; 50a5a56871SPadmavathi Venna unsigned int ftx0cnt_off; 51a5a56871SPadmavathi Venna }; 52a5a56871SPadmavathi Venna 5340476f61SPadmavathi Venna struct samsung_i2s_dai_data { 5440476f61SPadmavathi Venna int dai_type; 557da493e9SPadmavathi Venna u32 quirks; 56a5a56871SPadmavathi Venna const struct samsung_i2s_variant_regs *i2s_variant_regs; 5740476f61SPadmavathi Venna }; 5840476f61SPadmavathi Venna 595033f43cSJassi Brar struct i2s_dai { 605033f43cSJassi Brar /* Platform device for this DAI */ 615033f43cSJassi Brar struct platform_device *pdev; 625033f43cSJassi Brar /* IOREMAP'd SFRs */ 635033f43cSJassi Brar void __iomem *addr; 645033f43cSJassi Brar /* Physical base address of SFRs */ 655033f43cSJassi Brar u32 base; 665033f43cSJassi Brar /* Rate of RCLK source clock */ 675033f43cSJassi Brar unsigned long rclk_srcrate; 685033f43cSJassi Brar /* Frame Clock */ 695033f43cSJassi Brar unsigned frmclk; 705033f43cSJassi Brar /* 715033f43cSJassi Brar * Specifically requested RCLK,BCLK by MACHINE Driver. 725033f43cSJassi Brar * 0 indicates CPU driver is free to choose any value. 735033f43cSJassi Brar */ 745033f43cSJassi Brar unsigned rfs, bfs; 755033f43cSJassi Brar /* I2S Controller's core clock */ 765033f43cSJassi Brar struct clk *clk; 775033f43cSJassi Brar /* Clock for generating I2S signals */ 785033f43cSJassi Brar struct clk *op_clk; 795033f43cSJassi Brar /* Pointer to the Primary_Fifo if this is Sec_Fifo, NULL otherwise */ 805033f43cSJassi Brar struct i2s_dai *pri_dai; 815033f43cSJassi Brar /* Pointer to the Secondary_Fifo if it has one, NULL otherwise */ 825033f43cSJassi Brar struct i2s_dai *sec_dai; 835033f43cSJassi Brar #define DAI_OPENED (1 << 0) /* Dai is opened */ 845033f43cSJassi Brar #define DAI_MANAGER (1 << 1) /* Dai is the manager */ 855033f43cSJassi Brar unsigned mode; 86b97c60abSSylwester Nawrocki /* CDCLK pin direction: 0 - input, 1 - output */ 87b97c60abSSylwester Nawrocki unsigned int cdclk_out:1; 885033f43cSJassi Brar /* Driver for this DAI */ 895033f43cSJassi Brar struct snd_soc_dai_driver i2s_dai_drv; 905033f43cSJassi Brar /* DMA parameters */ 915033f43cSJassi Brar struct s3c_dma_params dma_playback; 925033f43cSJassi Brar struct s3c_dma_params dma_capture; 9361100f40SSangbeom Kim struct s3c_dma_params idma_playback; 945033f43cSJassi Brar u32 quirks; 955033f43cSJassi Brar u32 suspend_i2smod; 965033f43cSJassi Brar u32 suspend_i2scon; 975033f43cSJassi Brar u32 suspend_i2spsr; 9840476f61SPadmavathi Venna unsigned long gpios[7]; /* i2s gpio line numbers */ 99a5a56871SPadmavathi Venna const struct samsung_i2s_variant_regs *variant_regs; 1005033f43cSJassi Brar }; 1015033f43cSJassi Brar 1025033f43cSJassi Brar /* Lock for cross i/f checks */ 1035033f43cSJassi Brar static DEFINE_SPINLOCK(lock); 1045033f43cSJassi Brar 1055033f43cSJassi Brar /* If this is the 'overlay' stereo DAI */ 1065033f43cSJassi Brar static inline bool is_secondary(struct i2s_dai *i2s) 1075033f43cSJassi Brar { 1085033f43cSJassi Brar return i2s->pri_dai ? true : false; 1095033f43cSJassi Brar } 1105033f43cSJassi Brar 1115033f43cSJassi Brar /* If operating in SoC-Slave mode */ 1125033f43cSJassi Brar static inline bool is_slave(struct i2s_dai *i2s) 1135033f43cSJassi Brar { 114a5a56871SPadmavathi Venna u32 mod = readl(i2s->addr + I2SMOD); 115a5a56871SPadmavathi Venna return (mod & (1 << i2s->variant_regs->mss_off)) ? true : false; 1165033f43cSJassi Brar } 1175033f43cSJassi Brar 1185033f43cSJassi Brar /* If this interface of the controller is transmitting data */ 1195033f43cSJassi Brar static inline bool tx_active(struct i2s_dai *i2s) 1205033f43cSJassi Brar { 1215033f43cSJassi Brar u32 active; 1225033f43cSJassi Brar 1235033f43cSJassi Brar if (!i2s) 1245033f43cSJassi Brar return false; 1255033f43cSJassi Brar 12633195500SSangbeom Kim active = readl(i2s->addr + I2SCON); 1275033f43cSJassi Brar 1285033f43cSJassi Brar if (is_secondary(i2s)) 1295033f43cSJassi Brar active &= CON_TXSDMA_ACTIVE; 1305033f43cSJassi Brar else 1315033f43cSJassi Brar active &= CON_TXDMA_ACTIVE; 1325033f43cSJassi Brar 1335033f43cSJassi Brar return active ? true : false; 1345033f43cSJassi Brar } 1355033f43cSJassi Brar 1365033f43cSJassi Brar /* If the other interface of the controller is transmitting data */ 1375033f43cSJassi Brar static inline bool other_tx_active(struct i2s_dai *i2s) 1385033f43cSJassi Brar { 1395033f43cSJassi Brar struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai; 1405033f43cSJassi Brar 1415033f43cSJassi Brar return tx_active(other); 1425033f43cSJassi Brar } 1435033f43cSJassi Brar 1445033f43cSJassi Brar /* If any interface of the controller is transmitting data */ 1455033f43cSJassi Brar static inline bool any_tx_active(struct i2s_dai *i2s) 1465033f43cSJassi Brar { 1475033f43cSJassi Brar return tx_active(i2s) || other_tx_active(i2s); 1485033f43cSJassi Brar } 1495033f43cSJassi Brar 1505033f43cSJassi Brar /* If this interface of the controller is receiving data */ 1515033f43cSJassi Brar static inline bool rx_active(struct i2s_dai *i2s) 1525033f43cSJassi Brar { 1535033f43cSJassi Brar u32 active; 1545033f43cSJassi Brar 1555033f43cSJassi Brar if (!i2s) 1565033f43cSJassi Brar return false; 1575033f43cSJassi Brar 15833195500SSangbeom Kim active = readl(i2s->addr + I2SCON) & CON_RXDMA_ACTIVE; 1595033f43cSJassi Brar 1605033f43cSJassi Brar return active ? true : false; 1615033f43cSJassi Brar } 1625033f43cSJassi Brar 1635033f43cSJassi Brar /* If the other interface of the controller is receiving data */ 1645033f43cSJassi Brar static inline bool other_rx_active(struct i2s_dai *i2s) 1655033f43cSJassi Brar { 1665033f43cSJassi Brar struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai; 1675033f43cSJassi Brar 1685033f43cSJassi Brar return rx_active(other); 1695033f43cSJassi Brar } 1705033f43cSJassi Brar 1715033f43cSJassi Brar /* If any interface of the controller is receiving data */ 1725033f43cSJassi Brar static inline bool any_rx_active(struct i2s_dai *i2s) 1735033f43cSJassi Brar { 1745033f43cSJassi Brar return rx_active(i2s) || other_rx_active(i2s); 1755033f43cSJassi Brar } 1765033f43cSJassi Brar 1775033f43cSJassi Brar /* If the other DAI is transmitting or receiving data */ 1785033f43cSJassi Brar static inline bool other_active(struct i2s_dai *i2s) 1795033f43cSJassi Brar { 1805033f43cSJassi Brar return other_rx_active(i2s) || other_tx_active(i2s); 1815033f43cSJassi Brar } 1825033f43cSJassi Brar 1835033f43cSJassi Brar /* If this DAI is transmitting or receiving data */ 1845033f43cSJassi Brar static inline bool this_active(struct i2s_dai *i2s) 1855033f43cSJassi Brar { 1865033f43cSJassi Brar return tx_active(i2s) || rx_active(i2s); 1875033f43cSJassi Brar } 1885033f43cSJassi Brar 1895033f43cSJassi Brar /* If the controller is active anyway */ 1905033f43cSJassi Brar static inline bool any_active(struct i2s_dai *i2s) 1915033f43cSJassi Brar { 1925033f43cSJassi Brar return this_active(i2s) || other_active(i2s); 1935033f43cSJassi Brar } 1945033f43cSJassi Brar 1955033f43cSJassi Brar static inline struct i2s_dai *to_info(struct snd_soc_dai *dai) 1965033f43cSJassi Brar { 1975033f43cSJassi Brar return snd_soc_dai_get_drvdata(dai); 1985033f43cSJassi Brar } 1995033f43cSJassi Brar 2005033f43cSJassi Brar static inline bool is_opened(struct i2s_dai *i2s) 2015033f43cSJassi Brar { 2025033f43cSJassi Brar if (i2s && (i2s->mode & DAI_OPENED)) 2035033f43cSJassi Brar return true; 2045033f43cSJassi Brar else 2055033f43cSJassi Brar return false; 2065033f43cSJassi Brar } 2075033f43cSJassi Brar 2085033f43cSJassi Brar static inline bool is_manager(struct i2s_dai *i2s) 2095033f43cSJassi Brar { 2105033f43cSJassi Brar if (is_opened(i2s) && (i2s->mode & DAI_MANAGER)) 2115033f43cSJassi Brar return true; 2125033f43cSJassi Brar else 2135033f43cSJassi Brar return false; 2145033f43cSJassi Brar } 2155033f43cSJassi Brar 2165033f43cSJassi Brar /* Read RCLK of I2S (in multiples of LRCLK) */ 2175033f43cSJassi Brar static inline unsigned get_rfs(struct i2s_dai *i2s) 2185033f43cSJassi Brar { 2194ca0c0d4SPadmavathi Venna u32 rfs; 220a5a56871SPadmavathi Venna rfs = readl(i2s->addr + I2SMOD) >> i2s->variant_regs->rfs_off; 221a5a56871SPadmavathi Venna rfs &= i2s->variant_regs->rfs_mask; 2225033f43cSJassi Brar 2235033f43cSJassi Brar switch (rfs) { 224a5a56871SPadmavathi Venna case 7: return 192; 225a5a56871SPadmavathi Venna case 6: return 96; 226a5a56871SPadmavathi Venna case 5: return 128; 227a5a56871SPadmavathi Venna case 4: return 64; 2285033f43cSJassi Brar case 3: return 768; 2295033f43cSJassi Brar case 2: return 384; 2305033f43cSJassi Brar case 1: return 512; 2315033f43cSJassi Brar default: return 256; 2325033f43cSJassi Brar } 2335033f43cSJassi Brar } 2345033f43cSJassi Brar 2355033f43cSJassi Brar /* Write RCLK of I2S (in multiples of LRCLK) */ 2365033f43cSJassi Brar static inline void set_rfs(struct i2s_dai *i2s, unsigned rfs) 2375033f43cSJassi Brar { 2385033f43cSJassi Brar u32 mod = readl(i2s->addr + I2SMOD); 239a5a56871SPadmavathi Venna int rfs_shift = i2s->variant_regs->rfs_off; 2405033f43cSJassi Brar 241a5a56871SPadmavathi Venna mod &= ~(i2s->variant_regs->rfs_mask << rfs_shift); 2425033f43cSJassi Brar 2435033f43cSJassi Brar switch (rfs) { 244a5a56871SPadmavathi Venna case 192: 245a5a56871SPadmavathi Venna mod |= (EXYNOS7_MOD_RCLK_192FS << rfs_shift); 246a5a56871SPadmavathi Venna break; 247a5a56871SPadmavathi Venna case 96: 248a5a56871SPadmavathi Venna mod |= (EXYNOS7_MOD_RCLK_96FS << rfs_shift); 249a5a56871SPadmavathi Venna break; 250a5a56871SPadmavathi Venna case 128: 251a5a56871SPadmavathi Venna mod |= (EXYNOS7_MOD_RCLK_128FS << rfs_shift); 252a5a56871SPadmavathi Venna break; 253a5a56871SPadmavathi Venna case 64: 254a5a56871SPadmavathi Venna mod |= (EXYNOS7_MOD_RCLK_64FS << rfs_shift); 255a5a56871SPadmavathi Venna break; 2565033f43cSJassi Brar case 768: 257b60be4aaSPadmavathi Venna mod |= (MOD_RCLK_768FS << rfs_shift); 2585033f43cSJassi Brar break; 2595033f43cSJassi Brar case 512: 260b60be4aaSPadmavathi Venna mod |= (MOD_RCLK_512FS << rfs_shift); 2615033f43cSJassi Brar break; 2625033f43cSJassi Brar case 384: 263b60be4aaSPadmavathi Venna mod |= (MOD_RCLK_384FS << rfs_shift); 2645033f43cSJassi Brar break; 2655033f43cSJassi Brar default: 266b60be4aaSPadmavathi Venna mod |= (MOD_RCLK_256FS << rfs_shift); 2675033f43cSJassi Brar break; 2685033f43cSJassi Brar } 2695033f43cSJassi Brar 2705033f43cSJassi Brar writel(mod, i2s->addr + I2SMOD); 2715033f43cSJassi Brar } 2725033f43cSJassi Brar 2735033f43cSJassi Brar /* Read Bit-Clock of I2S (in multiples of LRCLK) */ 2745033f43cSJassi Brar static inline unsigned get_bfs(struct i2s_dai *i2s) 2755033f43cSJassi Brar { 2764ca0c0d4SPadmavathi Venna u32 bfs; 277a5a56871SPadmavathi Venna bfs = readl(i2s->addr + I2SMOD) >> i2s->variant_regs->bfs_off; 278a5a56871SPadmavathi Venna bfs &= i2s->variant_regs->bfs_mask; 2795033f43cSJassi Brar 2805033f43cSJassi Brar switch (bfs) { 2814ca0c0d4SPadmavathi Venna case 8: return 256; 2824ca0c0d4SPadmavathi Venna case 7: return 192; 2834ca0c0d4SPadmavathi Venna case 6: return 128; 2844ca0c0d4SPadmavathi Venna case 5: return 96; 2854ca0c0d4SPadmavathi Venna case 4: return 64; 2865033f43cSJassi Brar case 3: return 24; 2875033f43cSJassi Brar case 2: return 16; 2885033f43cSJassi Brar case 1: return 48; 2895033f43cSJassi Brar default: return 32; 2905033f43cSJassi Brar } 2915033f43cSJassi Brar } 2925033f43cSJassi Brar 2935033f43cSJassi Brar /* Write Bit-Clock of I2S (in multiples of LRCLK) */ 2945033f43cSJassi Brar static inline void set_bfs(struct i2s_dai *i2s, unsigned bfs) 2955033f43cSJassi Brar { 2965033f43cSJassi Brar u32 mod = readl(i2s->addr + I2SMOD); 2974ca0c0d4SPadmavathi Venna int tdm = i2s->quirks & QUIRK_SUPPORTS_TDM; 298a5a56871SPadmavathi Venna int bfs_shift = i2s->variant_regs->bfs_off; 2994ca0c0d4SPadmavathi Venna 3004ca0c0d4SPadmavathi Venna /* Non-TDM I2S controllers do not support BCLK > 48 * FS */ 3014ca0c0d4SPadmavathi Venna if (!tdm && bfs > 48) { 3024ca0c0d4SPadmavathi Venna dev_err(&i2s->pdev->dev, "Unsupported BCLK divider\n"); 3034ca0c0d4SPadmavathi Venna return; 3044ca0c0d4SPadmavathi Venna } 3055033f43cSJassi Brar 306a5a56871SPadmavathi Venna mod &= ~(i2s->variant_regs->bfs_mask << bfs_shift); 307a5a56871SPadmavathi Venna 3085033f43cSJassi Brar switch (bfs) { 3095033f43cSJassi Brar case 48: 310b60be4aaSPadmavathi Venna mod |= (MOD_BCLK_48FS << bfs_shift); 3115033f43cSJassi Brar break; 3125033f43cSJassi Brar case 32: 313b60be4aaSPadmavathi Venna mod |= (MOD_BCLK_32FS << bfs_shift); 3145033f43cSJassi Brar break; 3155033f43cSJassi Brar case 24: 316b60be4aaSPadmavathi Venna mod |= (MOD_BCLK_24FS << bfs_shift); 3175033f43cSJassi Brar break; 3185033f43cSJassi Brar case 16: 319b60be4aaSPadmavathi Venna mod |= (MOD_BCLK_16FS << bfs_shift); 3205033f43cSJassi Brar break; 3214ca0c0d4SPadmavathi Venna case 64: 3224ca0c0d4SPadmavathi Venna mod |= (EXYNOS5420_MOD_BCLK_64FS << bfs_shift); 3234ca0c0d4SPadmavathi Venna break; 3244ca0c0d4SPadmavathi Venna case 96: 3254ca0c0d4SPadmavathi Venna mod |= (EXYNOS5420_MOD_BCLK_96FS << bfs_shift); 3264ca0c0d4SPadmavathi Venna break; 3274ca0c0d4SPadmavathi Venna case 128: 3284ca0c0d4SPadmavathi Venna mod |= (EXYNOS5420_MOD_BCLK_128FS << bfs_shift); 3294ca0c0d4SPadmavathi Venna break; 3304ca0c0d4SPadmavathi Venna case 192: 3314ca0c0d4SPadmavathi Venna mod |= (EXYNOS5420_MOD_BCLK_192FS << bfs_shift); 3324ca0c0d4SPadmavathi Venna break; 3334ca0c0d4SPadmavathi Venna case 256: 3344ca0c0d4SPadmavathi Venna mod |= (EXYNOS5420_MOD_BCLK_256FS << bfs_shift); 3355033f43cSJassi Brar break; 3365033f43cSJassi Brar default: 3375033f43cSJassi Brar dev_err(&i2s->pdev->dev, "Wrong BCLK Divider!\n"); 3385033f43cSJassi Brar return; 3395033f43cSJassi Brar } 3405033f43cSJassi Brar 3415033f43cSJassi Brar writel(mod, i2s->addr + I2SMOD); 3425033f43cSJassi Brar } 3435033f43cSJassi Brar 3445033f43cSJassi Brar /* Sample-Size */ 3455033f43cSJassi Brar static inline int get_blc(struct i2s_dai *i2s) 3465033f43cSJassi Brar { 3475033f43cSJassi Brar int blc = readl(i2s->addr + I2SMOD); 3485033f43cSJassi Brar 3495033f43cSJassi Brar blc = (blc >> 13) & 0x3; 3505033f43cSJassi Brar 3515033f43cSJassi Brar switch (blc) { 3525033f43cSJassi Brar case 2: return 24; 3535033f43cSJassi Brar case 1: return 8; 3545033f43cSJassi Brar default: return 16; 3555033f43cSJassi Brar } 3565033f43cSJassi Brar } 3575033f43cSJassi Brar 3585033f43cSJassi Brar /* TX Channel Control */ 3595033f43cSJassi Brar static void i2s_txctrl(struct i2s_dai *i2s, int on) 3605033f43cSJassi Brar { 3615033f43cSJassi Brar void __iomem *addr = i2s->addr; 362a5a56871SPadmavathi Venna int txr_off = i2s->variant_regs->txr_off; 3635033f43cSJassi Brar u32 con = readl(addr + I2SCON); 364a5a56871SPadmavathi Venna u32 mod = readl(addr + I2SMOD) & ~(3 << txr_off); 3655033f43cSJassi Brar 3665033f43cSJassi Brar if (on) { 3675033f43cSJassi Brar con |= CON_ACTIVE; 3685033f43cSJassi Brar con &= ~CON_TXCH_PAUSE; 3695033f43cSJassi Brar 3705033f43cSJassi Brar if (is_secondary(i2s)) { 3715033f43cSJassi Brar con |= CON_TXSDMA_ACTIVE; 3725033f43cSJassi Brar con &= ~CON_TXSDMA_PAUSE; 3735033f43cSJassi Brar } else { 3745033f43cSJassi Brar con |= CON_TXDMA_ACTIVE; 3755033f43cSJassi Brar con &= ~CON_TXDMA_PAUSE; 3765033f43cSJassi Brar } 3775033f43cSJassi Brar 3785033f43cSJassi Brar if (any_rx_active(i2s)) 379a5a56871SPadmavathi Venna mod |= 2 << txr_off; 3805033f43cSJassi Brar else 381a5a56871SPadmavathi Venna mod |= 0 << txr_off; 3825033f43cSJassi Brar } else { 3835033f43cSJassi Brar if (is_secondary(i2s)) { 3845033f43cSJassi Brar con |= CON_TXSDMA_PAUSE; 3855033f43cSJassi Brar con &= ~CON_TXSDMA_ACTIVE; 3865033f43cSJassi Brar } else { 3875033f43cSJassi Brar con |= CON_TXDMA_PAUSE; 3885033f43cSJassi Brar con &= ~CON_TXDMA_ACTIVE; 3895033f43cSJassi Brar } 3905033f43cSJassi Brar 3915033f43cSJassi Brar if (other_tx_active(i2s)) { 3925033f43cSJassi Brar writel(con, addr + I2SCON); 3935033f43cSJassi Brar return; 3945033f43cSJassi Brar } 3955033f43cSJassi Brar 3965033f43cSJassi Brar con |= CON_TXCH_PAUSE; 3975033f43cSJassi Brar 3985033f43cSJassi Brar if (any_rx_active(i2s)) 399a5a56871SPadmavathi Venna mod |= 1 << txr_off; 4005033f43cSJassi Brar else 4015033f43cSJassi Brar con &= ~CON_ACTIVE; 4025033f43cSJassi Brar } 4035033f43cSJassi Brar 4045033f43cSJassi Brar writel(mod, addr + I2SMOD); 4055033f43cSJassi Brar writel(con, addr + I2SCON); 4065033f43cSJassi Brar } 4075033f43cSJassi Brar 4085033f43cSJassi Brar /* RX Channel Control */ 4095033f43cSJassi Brar static void i2s_rxctrl(struct i2s_dai *i2s, int on) 4105033f43cSJassi Brar { 4115033f43cSJassi Brar void __iomem *addr = i2s->addr; 412a5a56871SPadmavathi Venna int txr_off = i2s->variant_regs->txr_off; 4135033f43cSJassi Brar u32 con = readl(addr + I2SCON); 414a5a56871SPadmavathi Venna u32 mod = readl(addr + I2SMOD) & ~(3 << txr_off); 4155033f43cSJassi Brar 4165033f43cSJassi Brar if (on) { 4175033f43cSJassi Brar con |= CON_RXDMA_ACTIVE | CON_ACTIVE; 4185033f43cSJassi Brar con &= ~(CON_RXDMA_PAUSE | CON_RXCH_PAUSE); 4195033f43cSJassi Brar 4205033f43cSJassi Brar if (any_tx_active(i2s)) 421a5a56871SPadmavathi Venna mod |= 2 << txr_off; 4225033f43cSJassi Brar else 423a5a56871SPadmavathi Venna mod |= 1 << txr_off; 4245033f43cSJassi Brar } else { 4255033f43cSJassi Brar con |= CON_RXDMA_PAUSE | CON_RXCH_PAUSE; 4265033f43cSJassi Brar con &= ~CON_RXDMA_ACTIVE; 4275033f43cSJassi Brar 4285033f43cSJassi Brar if (any_tx_active(i2s)) 429a5a56871SPadmavathi Venna mod |= 0 << txr_off; 4305033f43cSJassi Brar else 4315033f43cSJassi Brar con &= ~CON_ACTIVE; 4325033f43cSJassi Brar } 4335033f43cSJassi Brar 4345033f43cSJassi Brar writel(mod, addr + I2SMOD); 4355033f43cSJassi Brar writel(con, addr + I2SCON); 4365033f43cSJassi Brar } 4375033f43cSJassi Brar 4385033f43cSJassi Brar /* Flush FIFO of an interface */ 4395033f43cSJassi Brar static inline void i2s_fifo(struct i2s_dai *i2s, u32 flush) 4405033f43cSJassi Brar { 4415033f43cSJassi Brar void __iomem *fic; 4425033f43cSJassi Brar u32 val; 4435033f43cSJassi Brar 4445033f43cSJassi Brar if (!i2s) 4455033f43cSJassi Brar return; 4465033f43cSJassi Brar 4475033f43cSJassi Brar if (is_secondary(i2s)) 4485033f43cSJassi Brar fic = i2s->addr + I2SFICS; 4495033f43cSJassi Brar else 4505033f43cSJassi Brar fic = i2s->addr + I2SFIC; 4515033f43cSJassi Brar 4525033f43cSJassi Brar /* Flush the FIFO */ 4535033f43cSJassi Brar writel(readl(fic) | flush, fic); 4545033f43cSJassi Brar 4555033f43cSJassi Brar /* Be patient */ 4565033f43cSJassi Brar val = msecs_to_loops(1) / 1000; /* 1 usec */ 4575033f43cSJassi Brar while (--val) 4585033f43cSJassi Brar cpu_relax(); 4595033f43cSJassi Brar 4605033f43cSJassi Brar writel(readl(fic) & ~flush, fic); 4615033f43cSJassi Brar } 4625033f43cSJassi Brar 4635033f43cSJassi Brar static int i2s_set_sysclk(struct snd_soc_dai *dai, 4645033f43cSJassi Brar int clk_id, unsigned int rfs, int dir) 4655033f43cSJassi Brar { 4665033f43cSJassi Brar struct i2s_dai *i2s = to_info(dai); 4675033f43cSJassi Brar struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai; 4685033f43cSJassi Brar u32 mod = readl(i2s->addr + I2SMOD); 469a5a56871SPadmavathi Venna const struct samsung_i2s_variant_regs *i2s_regs = i2s->variant_regs; 470a5a56871SPadmavathi Venna unsigned int cdcon_mask = 1 << i2s_regs->cdclkcon_off; 471a5a56871SPadmavathi Venna unsigned int rsrc_mask = 1 << i2s_regs->rclksrc_off; 4725033f43cSJassi Brar 4735033f43cSJassi Brar switch (clk_id) { 474c86d50f9SSylwester Nawrocki case SAMSUNG_I2S_OPCLK: 475c86d50f9SSylwester Nawrocki mod &= ~MOD_OPCLK_MASK; 476c86d50f9SSylwester Nawrocki mod |= dir; 477c86d50f9SSylwester Nawrocki break; 4785033f43cSJassi Brar case SAMSUNG_I2S_CDCLK: 4795033f43cSJassi Brar /* Shouldn't matter in GATING(CLOCK_IN) mode */ 4805033f43cSJassi Brar if (dir == SND_SOC_CLOCK_IN) 4815033f43cSJassi Brar rfs = 0; 4825033f43cSJassi Brar 483133c2681SCharles Keepax if ((rfs && other && other->rfs && (other->rfs != rfs)) || 4845033f43cSJassi Brar (any_active(i2s) && 4855033f43cSJassi Brar (((dir == SND_SOC_CLOCK_IN) 486a5a56871SPadmavathi Venna && !(mod & cdcon_mask)) || 4875033f43cSJassi Brar ((dir == SND_SOC_CLOCK_OUT) 488a5a56871SPadmavathi Venna && (mod & cdcon_mask))))) { 4895033f43cSJassi Brar dev_err(&i2s->pdev->dev, 4905033f43cSJassi Brar "%s:%d Other DAI busy\n", __func__, __LINE__); 4915033f43cSJassi Brar return -EAGAIN; 4925033f43cSJassi Brar } 4935033f43cSJassi Brar 4945033f43cSJassi Brar if (dir == SND_SOC_CLOCK_IN) 495a5a56871SPadmavathi Venna mod |= 1 << i2s_regs->cdclkcon_off; 4965033f43cSJassi Brar else 497b2de1d20SPadmavathi Venna mod &= ~(1 << i2s_regs->cdclkcon_off); 4985033f43cSJassi Brar 4995033f43cSJassi Brar i2s->rfs = rfs; 5005033f43cSJassi Brar break; 5015033f43cSJassi Brar 5025033f43cSJassi Brar case SAMSUNG_I2S_RCLKSRC_0: /* clock corrsponding to IISMOD[10] := 0 */ 5035033f43cSJassi Brar case SAMSUNG_I2S_RCLKSRC_1: /* clock corrsponding to IISMOD[10] := 1 */ 5045033f43cSJassi Brar if ((i2s->quirks & QUIRK_NO_MUXPSR) 5055033f43cSJassi Brar || (clk_id == SAMSUNG_I2S_RCLKSRC_0)) 5065033f43cSJassi Brar clk_id = 0; 5075033f43cSJassi Brar else 5085033f43cSJassi Brar clk_id = 1; 5095033f43cSJassi Brar 5105033f43cSJassi Brar if (!any_active(i2s)) { 511a6aba536SSylwester Nawrocki if (i2s->op_clk && !IS_ERR(i2s->op_clk)) { 512a5a56871SPadmavathi Venna if ((clk_id && !(mod & rsrc_mask)) || 513a5a56871SPadmavathi Venna (!clk_id && (mod & rsrc_mask))) { 51498614cf6SThomas Abraham clk_disable_unprepare(i2s->op_clk); 5155033f43cSJassi Brar clk_put(i2s->op_clk); 5165033f43cSJassi Brar } else { 5176ce534aaSJassi Brar i2s->rclk_srcrate = 5186ce534aaSJassi Brar clk_get_rate(i2s->op_clk); 5195033f43cSJassi Brar return 0; 5205033f43cSJassi Brar } 5215033f43cSJassi Brar } 5225033f43cSJassi Brar 5231974a042SPadmavathi Venna if (clk_id) 5245033f43cSJassi Brar i2s->op_clk = clk_get(&i2s->pdev->dev, 5251974a042SPadmavathi Venna "i2s_opclk1"); 5261974a042SPadmavathi Venna else 5271974a042SPadmavathi Venna i2s->op_clk = clk_get(&i2s->pdev->dev, 5281974a042SPadmavathi Venna "i2s_opclk0"); 529a6aba536SSylwester Nawrocki 530a6aba536SSylwester Nawrocki if (WARN_ON(IS_ERR(i2s->op_clk))) 531a6aba536SSylwester Nawrocki return PTR_ERR(i2s->op_clk); 532a6aba536SSylwester Nawrocki 53398614cf6SThomas Abraham clk_prepare_enable(i2s->op_clk); 5345033f43cSJassi Brar i2s->rclk_srcrate = clk_get_rate(i2s->op_clk); 5355033f43cSJassi Brar 5365033f43cSJassi Brar /* Over-ride the other's */ 5375033f43cSJassi Brar if (other) { 5385033f43cSJassi Brar other->op_clk = i2s->op_clk; 5395033f43cSJassi Brar other->rclk_srcrate = i2s->rclk_srcrate; 5405033f43cSJassi Brar } 541a5a56871SPadmavathi Venna } else if ((!clk_id && (mod & rsrc_mask)) 542a5a56871SPadmavathi Venna || (clk_id && !(mod & rsrc_mask))) { 5435033f43cSJassi Brar dev_err(&i2s->pdev->dev, 5445033f43cSJassi Brar "%s:%d Other DAI busy\n", __func__, __LINE__); 5455033f43cSJassi Brar return -EAGAIN; 5465033f43cSJassi Brar } else { 5475033f43cSJassi Brar /* Call can't be on the active DAI */ 5485033f43cSJassi Brar i2s->op_clk = other->op_clk; 5495033f43cSJassi Brar i2s->rclk_srcrate = other->rclk_srcrate; 5505033f43cSJassi Brar return 0; 5515033f43cSJassi Brar } 5525033f43cSJassi Brar 5535033f43cSJassi Brar if (clk_id == 0) 554b2de1d20SPadmavathi Venna mod &= ~(1 << i2s_regs->rclksrc_off); 5555033f43cSJassi Brar else 556a5a56871SPadmavathi Venna mod |= 1 << i2s_regs->rclksrc_off; 5575033f43cSJassi Brar 558b2de1d20SPadmavathi Venna break; 5595033f43cSJassi Brar default: 5605033f43cSJassi Brar dev_err(&i2s->pdev->dev, "We don't serve that!\n"); 5615033f43cSJassi Brar return -EINVAL; 5625033f43cSJassi Brar } 5635033f43cSJassi Brar 5645033f43cSJassi Brar writel(mod, i2s->addr + I2SMOD); 5655033f43cSJassi Brar 5665033f43cSJassi Brar return 0; 5675033f43cSJassi Brar } 5685033f43cSJassi Brar 5695033f43cSJassi Brar static int i2s_set_fmt(struct snd_soc_dai *dai, 5705033f43cSJassi Brar unsigned int fmt) 5715033f43cSJassi Brar { 5725033f43cSJassi Brar struct i2s_dai *i2s = to_info(dai); 5735033f43cSJassi Brar u32 mod = readl(i2s->addr + I2SMOD); 574a5a56871SPadmavathi Venna int lrp_shift, sdf_shift, sdf_mask, lrp_rlow, mod_slave; 5755033f43cSJassi Brar u32 tmp = 0; 5765033f43cSJassi Brar 577a5a56871SPadmavathi Venna lrp_shift = i2s->variant_regs->lrp_off; 578a5a56871SPadmavathi Venna sdf_shift = i2s->variant_regs->sdf_off; 579a5a56871SPadmavathi Venna mod_slave = 1 << i2s->variant_regs->mss_off; 5804ca0c0d4SPadmavathi Venna 581b60be4aaSPadmavathi Venna sdf_mask = MOD_SDF_MASK << sdf_shift; 582b60be4aaSPadmavathi Venna lrp_rlow = MOD_LR_RLOW << lrp_shift; 583b60be4aaSPadmavathi Venna 5845033f43cSJassi Brar /* Format is priority */ 5855033f43cSJassi Brar switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { 5865033f43cSJassi Brar case SND_SOC_DAIFMT_RIGHT_J: 587b60be4aaSPadmavathi Venna tmp |= lrp_rlow; 588b60be4aaSPadmavathi Venna tmp |= (MOD_SDF_MSB << sdf_shift); 5895033f43cSJassi Brar break; 5905033f43cSJassi Brar case SND_SOC_DAIFMT_LEFT_J: 591b60be4aaSPadmavathi Venna tmp |= lrp_rlow; 592b60be4aaSPadmavathi Venna tmp |= (MOD_SDF_LSB << sdf_shift); 5935033f43cSJassi Brar break; 5945033f43cSJassi Brar case SND_SOC_DAIFMT_I2S: 595b60be4aaSPadmavathi Venna tmp |= (MOD_SDF_IIS << sdf_shift); 5965033f43cSJassi Brar break; 5975033f43cSJassi Brar default: 5985033f43cSJassi Brar dev_err(&i2s->pdev->dev, "Format not supported\n"); 5995033f43cSJassi Brar return -EINVAL; 6005033f43cSJassi Brar } 6015033f43cSJassi Brar 6025033f43cSJassi Brar /* 6035033f43cSJassi Brar * INV flag is relative to the FORMAT flag - if set it simply 6045033f43cSJassi Brar * flips the polarity specified by the Standard 6055033f43cSJassi Brar */ 6065033f43cSJassi Brar switch (fmt & SND_SOC_DAIFMT_INV_MASK) { 6075033f43cSJassi Brar case SND_SOC_DAIFMT_NB_NF: 6085033f43cSJassi Brar break; 6095033f43cSJassi Brar case SND_SOC_DAIFMT_NB_IF: 610b60be4aaSPadmavathi Venna if (tmp & lrp_rlow) 611b60be4aaSPadmavathi Venna tmp &= ~lrp_rlow; 6125033f43cSJassi Brar else 613b60be4aaSPadmavathi Venna tmp |= lrp_rlow; 6145033f43cSJassi Brar break; 6155033f43cSJassi Brar default: 6165033f43cSJassi Brar dev_err(&i2s->pdev->dev, "Polarity not supported\n"); 6175033f43cSJassi Brar return -EINVAL; 6185033f43cSJassi Brar } 6195033f43cSJassi Brar 6205033f43cSJassi Brar switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { 6215033f43cSJassi Brar case SND_SOC_DAIFMT_CBM_CFM: 622a5a56871SPadmavathi Venna tmp |= mod_slave; 6235033f43cSJassi Brar break; 6245033f43cSJassi Brar case SND_SOC_DAIFMT_CBS_CFS: 6255033f43cSJassi Brar /* Set default source clock in Master mode */ 6265033f43cSJassi Brar if (i2s->rclk_srcrate == 0) 6275033f43cSJassi Brar i2s_set_sysclk(dai, SAMSUNG_I2S_RCLKSRC_0, 6285033f43cSJassi Brar 0, SND_SOC_CLOCK_IN); 6295033f43cSJassi Brar break; 6305033f43cSJassi Brar default: 6315033f43cSJassi Brar dev_err(&i2s->pdev->dev, "master/slave format not supported\n"); 6325033f43cSJassi Brar return -EINVAL; 6335033f43cSJassi Brar } 6345033f43cSJassi Brar 635b60be4aaSPadmavathi Venna /* 636b60be4aaSPadmavathi Venna * Don't change the I2S mode if any controller is active on this 637b60be4aaSPadmavathi Venna * channel. 638b60be4aaSPadmavathi Venna */ 6395033f43cSJassi Brar if (any_active(i2s) && 640a5a56871SPadmavathi Venna ((mod & (sdf_mask | lrp_rlow | mod_slave)) != tmp)) { 6415033f43cSJassi Brar dev_err(&i2s->pdev->dev, 6425033f43cSJassi Brar "%s:%d Other DAI busy\n", __func__, __LINE__); 6435033f43cSJassi Brar return -EAGAIN; 6445033f43cSJassi Brar } 6455033f43cSJassi Brar 646a5a56871SPadmavathi Venna mod &= ~(sdf_mask | lrp_rlow | mod_slave); 6475033f43cSJassi Brar mod |= tmp; 6485033f43cSJassi Brar writel(mod, i2s->addr + I2SMOD); 6495033f43cSJassi Brar 6505033f43cSJassi Brar return 0; 6515033f43cSJassi Brar } 6525033f43cSJassi Brar 6535033f43cSJassi Brar static int i2s_hw_params(struct snd_pcm_substream *substream, 6545033f43cSJassi Brar struct snd_pcm_hw_params *params, struct snd_soc_dai *dai) 6555033f43cSJassi Brar { 6565033f43cSJassi Brar struct i2s_dai *i2s = to_info(dai); 6575033f43cSJassi Brar u32 mod = readl(i2s->addr + I2SMOD); 6585033f43cSJassi Brar 6595033f43cSJassi Brar if (!is_secondary(i2s)) 6605033f43cSJassi Brar mod &= ~(MOD_DC2_EN | MOD_DC1_EN); 6615033f43cSJassi Brar 6625033f43cSJassi Brar switch (params_channels(params)) { 6635033f43cSJassi Brar case 6: 6645033f43cSJassi Brar mod |= MOD_DC2_EN; 6655033f43cSJassi Brar case 4: 6665033f43cSJassi Brar mod |= MOD_DC1_EN; 6675033f43cSJassi Brar break; 6685033f43cSJassi Brar case 2: 669588fb705SSangsu Park if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 670588fb705SSangsu Park i2s->dma_playback.dma_size = 4; 671588fb705SSangsu Park else 672588fb705SSangsu Park i2s->dma_capture.dma_size = 4; 673588fb705SSangsu Park break; 674588fb705SSangsu Park case 1: 675588fb705SSangsu Park if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 676588fb705SSangsu Park i2s->dma_playback.dma_size = 2; 677588fb705SSangsu Park else 678588fb705SSangsu Park i2s->dma_capture.dma_size = 2; 679588fb705SSangsu Park 6805033f43cSJassi Brar break; 6815033f43cSJassi Brar default: 6825033f43cSJassi Brar dev_err(&i2s->pdev->dev, "%d channels not supported\n", 6835033f43cSJassi Brar params_channels(params)); 6845033f43cSJassi Brar return -EINVAL; 6855033f43cSJassi Brar } 6865033f43cSJassi Brar 6875033f43cSJassi Brar if (is_secondary(i2s)) 6885033f43cSJassi Brar mod &= ~MOD_BLCS_MASK; 6895033f43cSJassi Brar else 6905033f43cSJassi Brar mod &= ~MOD_BLCP_MASK; 6915033f43cSJassi Brar 6925033f43cSJassi Brar if (is_manager(i2s)) 6935033f43cSJassi Brar mod &= ~MOD_BLC_MASK; 6945033f43cSJassi Brar 69588ce1465STushar Behera switch (params_width(params)) { 69688ce1465STushar Behera case 8: 6975033f43cSJassi Brar if (is_secondary(i2s)) 6985033f43cSJassi Brar mod |= MOD_BLCS_8BIT; 6995033f43cSJassi Brar else 7005033f43cSJassi Brar mod |= MOD_BLCP_8BIT; 7015033f43cSJassi Brar if (is_manager(i2s)) 7025033f43cSJassi Brar mod |= MOD_BLC_8BIT; 7035033f43cSJassi Brar break; 70488ce1465STushar Behera case 16: 7055033f43cSJassi Brar if (is_secondary(i2s)) 7065033f43cSJassi Brar mod |= MOD_BLCS_16BIT; 7075033f43cSJassi Brar else 7085033f43cSJassi Brar mod |= MOD_BLCP_16BIT; 7095033f43cSJassi Brar if (is_manager(i2s)) 7105033f43cSJassi Brar mod |= MOD_BLC_16BIT; 7115033f43cSJassi Brar break; 71288ce1465STushar Behera case 24: 7135033f43cSJassi Brar if (is_secondary(i2s)) 7145033f43cSJassi Brar mod |= MOD_BLCS_24BIT; 7155033f43cSJassi Brar else 7165033f43cSJassi Brar mod |= MOD_BLCP_24BIT; 7175033f43cSJassi Brar if (is_manager(i2s)) 7185033f43cSJassi Brar mod |= MOD_BLC_24BIT; 7195033f43cSJassi Brar break; 7205033f43cSJassi Brar default: 7215033f43cSJassi Brar dev_err(&i2s->pdev->dev, "Format(%d) not supported\n", 7225033f43cSJassi Brar params_format(params)); 7235033f43cSJassi Brar return -EINVAL; 7245033f43cSJassi Brar } 7255033f43cSJassi Brar writel(mod, i2s->addr + I2SMOD); 7265033f43cSJassi Brar 727d37bdf73SMark Brown samsung_asoc_init_dma_data(dai, &i2s->dma_playback, &i2s->dma_capture); 728d37bdf73SMark Brown 7295033f43cSJassi Brar i2s->frmclk = params_rate(params); 7305033f43cSJassi Brar 7315033f43cSJassi Brar return 0; 7325033f43cSJassi Brar } 7335033f43cSJassi Brar 7345033f43cSJassi Brar /* We set constraints on the substream acc to the version of I2S */ 7355033f43cSJassi Brar static int i2s_startup(struct snd_pcm_substream *substream, 7365033f43cSJassi Brar struct snd_soc_dai *dai) 7375033f43cSJassi Brar { 7385033f43cSJassi Brar struct i2s_dai *i2s = to_info(dai); 7395033f43cSJassi Brar struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai; 7405033f43cSJassi Brar unsigned long flags; 7415033f43cSJassi Brar 7425033f43cSJassi Brar spin_lock_irqsave(&lock, flags); 7435033f43cSJassi Brar 7445033f43cSJassi Brar i2s->mode |= DAI_OPENED; 7455033f43cSJassi Brar 7465033f43cSJassi Brar if (is_manager(other)) 7475033f43cSJassi Brar i2s->mode &= ~DAI_MANAGER; 7485033f43cSJassi Brar else 7495033f43cSJassi Brar i2s->mode |= DAI_MANAGER; 7505033f43cSJassi Brar 7512d77828dSPadmavathi Venna if (!any_active(i2s) && (i2s->quirks & QUIRK_NEED_RSTCLR)) 7522d77828dSPadmavathi Venna writel(CON_RSTCLR, i2s->addr + I2SCON); 7532d77828dSPadmavathi Venna 7545033f43cSJassi Brar spin_unlock_irqrestore(&lock, flags); 7555033f43cSJassi Brar 756b97c60abSSylwester Nawrocki if (!is_opened(other) && i2s->cdclk_out) 757b97c60abSSylwester Nawrocki i2s_set_sysclk(dai, SAMSUNG_I2S_CDCLK, 758b97c60abSSylwester Nawrocki 0, SND_SOC_CLOCK_OUT); 7595033f43cSJassi Brar return 0; 7605033f43cSJassi Brar } 7615033f43cSJassi Brar 7625033f43cSJassi Brar static void i2s_shutdown(struct snd_pcm_substream *substream, 7635033f43cSJassi Brar struct snd_soc_dai *dai) 7645033f43cSJassi Brar { 7655033f43cSJassi Brar struct i2s_dai *i2s = to_info(dai); 7665033f43cSJassi Brar struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai; 7675033f43cSJassi Brar unsigned long flags; 768a5a56871SPadmavathi Venna const struct samsung_i2s_variant_regs *i2s_regs = i2s->variant_regs; 7695033f43cSJassi Brar 7705033f43cSJassi Brar spin_lock_irqsave(&lock, flags); 7715033f43cSJassi Brar 7725033f43cSJassi Brar i2s->mode &= ~DAI_OPENED; 7735033f43cSJassi Brar i2s->mode &= ~DAI_MANAGER; 7745033f43cSJassi Brar 775b97c60abSSylwester Nawrocki if (is_opened(other)) { 7765033f43cSJassi Brar other->mode |= DAI_MANAGER; 777b97c60abSSylwester Nawrocki } else { 778b97c60abSSylwester Nawrocki u32 mod = readl(i2s->addr + I2SMOD); 779a5a56871SPadmavathi Venna i2s->cdclk_out = !(mod & (1 << i2s_regs->cdclkcon_off)); 780133c2681SCharles Keepax if (other) 781b97c60abSSylwester Nawrocki other->cdclk_out = i2s->cdclk_out; 782b97c60abSSylwester Nawrocki } 7835033f43cSJassi Brar /* Reset any constraint on RFS and BFS */ 7845033f43cSJassi Brar i2s->rfs = 0; 7855033f43cSJassi Brar i2s->bfs = 0; 7865033f43cSJassi Brar 7875033f43cSJassi Brar spin_unlock_irqrestore(&lock, flags); 7885033f43cSJassi Brar 7895033f43cSJassi Brar /* Gate CDCLK by default */ 7905033f43cSJassi Brar if (!is_opened(other)) 7915033f43cSJassi Brar i2s_set_sysclk(dai, SAMSUNG_I2S_CDCLK, 7925033f43cSJassi Brar 0, SND_SOC_CLOCK_IN); 7935033f43cSJassi Brar } 7945033f43cSJassi Brar 7955033f43cSJassi Brar static int config_setup(struct i2s_dai *i2s) 7965033f43cSJassi Brar { 7975033f43cSJassi Brar struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai; 7985033f43cSJassi Brar unsigned rfs, bfs, blc; 7995033f43cSJassi Brar u32 psr; 8005033f43cSJassi Brar 8015033f43cSJassi Brar blc = get_blc(i2s); 8025033f43cSJassi Brar 8035033f43cSJassi Brar bfs = i2s->bfs; 8045033f43cSJassi Brar 8055033f43cSJassi Brar if (!bfs && other) 8065033f43cSJassi Brar bfs = other->bfs; 8075033f43cSJassi Brar 8085033f43cSJassi Brar /* Select least possible multiple(2) if no constraint set */ 8095033f43cSJassi Brar if (!bfs) 8105033f43cSJassi Brar bfs = blc * 2; 8115033f43cSJassi Brar 8125033f43cSJassi Brar rfs = i2s->rfs; 8135033f43cSJassi Brar 8145033f43cSJassi Brar if (!rfs && other) 8155033f43cSJassi Brar rfs = other->rfs; 8165033f43cSJassi Brar 8175033f43cSJassi Brar if ((rfs == 256 || rfs == 512) && (blc == 24)) { 8185033f43cSJassi Brar dev_err(&i2s->pdev->dev, 8195033f43cSJassi Brar "%d-RFS not supported for 24-blc\n", rfs); 8205033f43cSJassi Brar return -EINVAL; 8215033f43cSJassi Brar } 8225033f43cSJassi Brar 8235033f43cSJassi Brar if (!rfs) { 8245033f43cSJassi Brar if (bfs == 16 || bfs == 32) 8255033f43cSJassi Brar rfs = 256; 8265033f43cSJassi Brar else 8275033f43cSJassi Brar rfs = 384; 8285033f43cSJassi Brar } 8295033f43cSJassi Brar 8305033f43cSJassi Brar /* If already setup and running */ 8315033f43cSJassi Brar if (any_active(i2s) && (get_rfs(i2s) != rfs || get_bfs(i2s) != bfs)) { 8325033f43cSJassi Brar dev_err(&i2s->pdev->dev, 8335033f43cSJassi Brar "%s:%d Other DAI busy\n", __func__, __LINE__); 8345033f43cSJassi Brar return -EAGAIN; 8355033f43cSJassi Brar } 8365033f43cSJassi Brar 8375033f43cSJassi Brar set_bfs(i2s, bfs); 8385033f43cSJassi Brar set_rfs(i2s, rfs); 8395033f43cSJassi Brar 84077010010SPadmavathi Venna /* Don't bother with PSR in Slave mode */ 84177010010SPadmavathi Venna if (is_slave(i2s)) 84277010010SPadmavathi Venna return 0; 84377010010SPadmavathi Venna 8445033f43cSJassi Brar if (!(i2s->quirks & QUIRK_NO_MUXPSR)) { 8455033f43cSJassi Brar psr = i2s->rclk_srcrate / i2s->frmclk / rfs; 8465033f43cSJassi Brar writel(((psr - 1) << 8) | PSR_PSREN, i2s->addr + I2SPSR); 8475033f43cSJassi Brar dev_dbg(&i2s->pdev->dev, 8485033f43cSJassi Brar "RCLK_SRC=%luHz PSR=%u, RCLK=%dfs, BCLK=%dfs\n", 8495033f43cSJassi Brar i2s->rclk_srcrate, psr, rfs, bfs); 8505033f43cSJassi Brar } 8515033f43cSJassi Brar 8525033f43cSJassi Brar return 0; 8535033f43cSJassi Brar } 8545033f43cSJassi Brar 8555033f43cSJassi Brar static int i2s_trigger(struct snd_pcm_substream *substream, 8565033f43cSJassi Brar int cmd, struct snd_soc_dai *dai) 8575033f43cSJassi Brar { 8585033f43cSJassi Brar int capture = (substream->stream == SNDRV_PCM_STREAM_CAPTURE); 8595033f43cSJassi Brar struct snd_soc_pcm_runtime *rtd = substream->private_data; 8605033f43cSJassi Brar struct i2s_dai *i2s = to_info(rtd->cpu_dai); 8615033f43cSJassi Brar unsigned long flags; 8625033f43cSJassi Brar 8635033f43cSJassi Brar switch (cmd) { 8645033f43cSJassi Brar case SNDRV_PCM_TRIGGER_START: 8655033f43cSJassi Brar case SNDRV_PCM_TRIGGER_RESUME: 8665033f43cSJassi Brar case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 8675033f43cSJassi Brar local_irq_save(flags); 8685033f43cSJassi Brar 8695033f43cSJassi Brar if (config_setup(i2s)) { 8705033f43cSJassi Brar local_irq_restore(flags); 8715033f43cSJassi Brar return -EINVAL; 8725033f43cSJassi Brar } 8735033f43cSJassi Brar 8745033f43cSJassi Brar if (capture) 8755033f43cSJassi Brar i2s_rxctrl(i2s, 1); 8765033f43cSJassi Brar else 8775033f43cSJassi Brar i2s_txctrl(i2s, 1); 8785033f43cSJassi Brar 8795033f43cSJassi Brar local_irq_restore(flags); 8805033f43cSJassi Brar break; 8815033f43cSJassi Brar case SNDRV_PCM_TRIGGER_STOP: 8825033f43cSJassi Brar case SNDRV_PCM_TRIGGER_SUSPEND: 8835033f43cSJassi Brar case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 8845033f43cSJassi Brar local_irq_save(flags); 8855033f43cSJassi Brar 886c90887feSJassi Brar if (capture) { 8875033f43cSJassi Brar i2s_rxctrl(i2s, 0); 888775bc971SJassi Brar i2s_fifo(i2s, FIC_RXFLUSH); 889c90887feSJassi Brar } else { 890c90887feSJassi Brar i2s_txctrl(i2s, 0); 891775bc971SJassi Brar i2s_fifo(i2s, FIC_TXFLUSH); 892c90887feSJassi Brar } 893775bc971SJassi Brar 8945033f43cSJassi Brar local_irq_restore(flags); 8955033f43cSJassi Brar break; 8965033f43cSJassi Brar } 8975033f43cSJassi Brar 8985033f43cSJassi Brar return 0; 8995033f43cSJassi Brar } 9005033f43cSJassi Brar 9015033f43cSJassi Brar static int i2s_set_clkdiv(struct snd_soc_dai *dai, 9025033f43cSJassi Brar int div_id, int div) 9035033f43cSJassi Brar { 9045033f43cSJassi Brar struct i2s_dai *i2s = to_info(dai); 9055033f43cSJassi Brar struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai; 9065033f43cSJassi Brar 9075033f43cSJassi Brar switch (div_id) { 9085033f43cSJassi Brar case SAMSUNG_I2S_DIV_BCLK: 9095033f43cSJassi Brar if ((any_active(i2s) && div && (get_bfs(i2s) != div)) 9105033f43cSJassi Brar || (other && other->bfs && (other->bfs != div))) { 9115033f43cSJassi Brar dev_err(&i2s->pdev->dev, 9125033f43cSJassi Brar "%s:%d Other DAI busy\n", __func__, __LINE__); 9135033f43cSJassi Brar return -EAGAIN; 9145033f43cSJassi Brar } 9155033f43cSJassi Brar i2s->bfs = div; 9165033f43cSJassi Brar break; 9175033f43cSJassi Brar default: 9185033f43cSJassi Brar dev_err(&i2s->pdev->dev, 9195033f43cSJassi Brar "Invalid clock divider(%d)\n", div_id); 9205033f43cSJassi Brar return -EINVAL; 9215033f43cSJassi Brar } 9225033f43cSJassi Brar 9235033f43cSJassi Brar return 0; 9245033f43cSJassi Brar } 9255033f43cSJassi Brar 9265033f43cSJassi Brar static snd_pcm_sframes_t 9275033f43cSJassi Brar i2s_delay(struct snd_pcm_substream *substream, struct snd_soc_dai *dai) 9285033f43cSJassi Brar { 9295033f43cSJassi Brar struct i2s_dai *i2s = to_info(dai); 9305033f43cSJassi Brar u32 reg = readl(i2s->addr + I2SFIC); 9315033f43cSJassi Brar snd_pcm_sframes_t delay; 932a5a56871SPadmavathi Venna const struct samsung_i2s_variant_regs *i2s_regs = i2s->variant_regs; 9335033f43cSJassi Brar 9345033f43cSJassi Brar if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) 9355033f43cSJassi Brar delay = FIC_RXCOUNT(reg); 9365033f43cSJassi Brar else if (is_secondary(i2s)) 9375033f43cSJassi Brar delay = FICS_TXCOUNT(readl(i2s->addr + I2SFICS)); 9385033f43cSJassi Brar else 939a5a56871SPadmavathi Venna delay = (reg >> i2s_regs->ftx0cnt_off) & 0x7f; 9405033f43cSJassi Brar 9415033f43cSJassi Brar return delay; 9425033f43cSJassi Brar } 9435033f43cSJassi Brar 9445033f43cSJassi Brar #ifdef CONFIG_PM 9455033f43cSJassi Brar static int i2s_suspend(struct snd_soc_dai *dai) 9465033f43cSJassi Brar { 9475033f43cSJassi Brar struct i2s_dai *i2s = to_info(dai); 9485033f43cSJassi Brar 9495033f43cSJassi Brar i2s->suspend_i2smod = readl(i2s->addr + I2SMOD); 9505033f43cSJassi Brar i2s->suspend_i2scon = readl(i2s->addr + I2SCON); 9515033f43cSJassi Brar i2s->suspend_i2spsr = readl(i2s->addr + I2SPSR); 9525033f43cSJassi Brar 9535033f43cSJassi Brar return 0; 9545033f43cSJassi Brar } 9555033f43cSJassi Brar 9565033f43cSJassi Brar static int i2s_resume(struct snd_soc_dai *dai) 9575033f43cSJassi Brar { 9585033f43cSJassi Brar struct i2s_dai *i2s = to_info(dai); 9595033f43cSJassi Brar 9605033f43cSJassi Brar writel(i2s->suspend_i2scon, i2s->addr + I2SCON); 9615033f43cSJassi Brar writel(i2s->suspend_i2smod, i2s->addr + I2SMOD); 9625033f43cSJassi Brar writel(i2s->suspend_i2spsr, i2s->addr + I2SPSR); 9635033f43cSJassi Brar 9645033f43cSJassi Brar return 0; 9655033f43cSJassi Brar } 9665033f43cSJassi Brar #else 9675033f43cSJassi Brar #define i2s_suspend NULL 9685033f43cSJassi Brar #define i2s_resume NULL 9695033f43cSJassi Brar #endif 9705033f43cSJassi Brar 9715033f43cSJassi Brar static int samsung_i2s_dai_probe(struct snd_soc_dai *dai) 9725033f43cSJassi Brar { 9735033f43cSJassi Brar struct i2s_dai *i2s = to_info(dai); 9745033f43cSJassi Brar struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai; 975ba56447cSMark Brown int ret; 9765033f43cSJassi Brar 9773688569eSMark Brown if (other && other->clk) { /* If this is probe on secondary */ 9783688569eSMark Brown samsung_asoc_init_dma_data(dai, &other->sec_dai->dma_playback, 9793688569eSMark Brown NULL); 9805033f43cSJassi Brar goto probe_exit; 9813688569eSMark Brown } 9825033f43cSJassi Brar 9835033f43cSJassi Brar i2s->addr = ioremap(i2s->base, 0x100); 9845033f43cSJassi Brar if (i2s->addr == NULL) { 9855033f43cSJassi Brar dev_err(&i2s->pdev->dev, "cannot ioremap registers\n"); 9865033f43cSJassi Brar return -ENXIO; 9875033f43cSJassi Brar } 9885033f43cSJassi Brar 9895033f43cSJassi Brar i2s->clk = clk_get(&i2s->pdev->dev, "iis"); 9905033f43cSJassi Brar if (IS_ERR(i2s->clk)) { 9915033f43cSJassi Brar dev_err(&i2s->pdev->dev, "failed to get i2s_clock\n"); 9925033f43cSJassi Brar iounmap(i2s->addr); 993ba56447cSMark Brown return PTR_ERR(i2s->clk); 9945033f43cSJassi Brar } 995ba56447cSMark Brown 996ba56447cSMark Brown ret = clk_prepare_enable(i2s->clk); 997ba56447cSMark Brown if (ret != 0) { 998ba56447cSMark Brown dev_err(&i2s->pdev->dev, "failed to enable clock: %d\n", ret); 999ba56447cSMark Brown return ret; 1000ba56447cSMark Brown } 10015033f43cSJassi Brar 10023688569eSMark Brown samsung_asoc_init_dma_data(dai, &i2s->dma_playback, &i2s->dma_capture); 1003511e3033SMark Brown 10045033f43cSJassi Brar if (other) { 10055033f43cSJassi Brar other->addr = i2s->addr; 10065033f43cSJassi Brar other->clk = i2s->clk; 10075033f43cSJassi Brar } 10085033f43cSJassi Brar 10095033f43cSJassi Brar if (i2s->quirks & QUIRK_NEED_RSTCLR) 10105033f43cSJassi Brar writel(CON_RSTCLR, i2s->addr + I2SCON); 10115033f43cSJassi Brar 1012b0759736SPadmavathi Venna if (i2s->quirks & QUIRK_SUPPORTS_IDMA) 10139b8f5695SMark Brown idma_reg_addr_init(i2s->addr, 101461100f40SSangbeom Kim i2s->sec_dai->idma_playback.dma_addr); 101561100f40SSangbeom Kim 10165033f43cSJassi Brar probe_exit: 10175033f43cSJassi Brar /* Reset any constraint on RFS and BFS */ 10185033f43cSJassi Brar i2s->rfs = 0; 10195033f43cSJassi Brar i2s->bfs = 0; 1020d66eac3eSTushar Behera i2s->rclk_srcrate = 0; 10215033f43cSJassi Brar i2s_txctrl(i2s, 0); 10225033f43cSJassi Brar i2s_rxctrl(i2s, 0); 10235033f43cSJassi Brar i2s_fifo(i2s, FIC_TXFLUSH); 10245033f43cSJassi Brar i2s_fifo(other, FIC_TXFLUSH); 10255033f43cSJassi Brar i2s_fifo(i2s, FIC_RXFLUSH); 10265033f43cSJassi Brar 10275033f43cSJassi Brar /* Gate CDCLK by default */ 10285033f43cSJassi Brar if (!is_opened(other)) 10295033f43cSJassi Brar i2s_set_sysclk(dai, SAMSUNG_I2S_CDCLK, 10305033f43cSJassi Brar 0, SND_SOC_CLOCK_IN); 10315033f43cSJassi Brar 10325033f43cSJassi Brar return 0; 10335033f43cSJassi Brar } 10345033f43cSJassi Brar 10355033f43cSJassi Brar static int samsung_i2s_dai_remove(struct snd_soc_dai *dai) 10365033f43cSJassi Brar { 10375033f43cSJassi Brar struct i2s_dai *i2s = snd_soc_dai_get_drvdata(dai); 10385033f43cSJassi Brar struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai; 10395033f43cSJassi Brar 10405033f43cSJassi Brar if (!other || !other->clk) { 10415033f43cSJassi Brar 10425033f43cSJassi Brar if (i2s->quirks & QUIRK_NEED_RSTCLR) 10435033f43cSJassi Brar writel(0, i2s->addr + I2SCON); 10445033f43cSJassi Brar 104598614cf6SThomas Abraham clk_disable_unprepare(i2s->clk); 10465033f43cSJassi Brar clk_put(i2s->clk); 10475033f43cSJassi Brar 10485033f43cSJassi Brar iounmap(i2s->addr); 10495033f43cSJassi Brar } 10505033f43cSJassi Brar 10515033f43cSJassi Brar i2s->clk = NULL; 10525033f43cSJassi Brar 10535033f43cSJassi Brar return 0; 10545033f43cSJassi Brar } 10555033f43cSJassi Brar 105685e7652dSLars-Peter Clausen static const struct snd_soc_dai_ops samsung_i2s_dai_ops = { 10575033f43cSJassi Brar .trigger = i2s_trigger, 10585033f43cSJassi Brar .hw_params = i2s_hw_params, 10595033f43cSJassi Brar .set_fmt = i2s_set_fmt, 10605033f43cSJassi Brar .set_clkdiv = i2s_set_clkdiv, 10615033f43cSJassi Brar .set_sysclk = i2s_set_sysclk, 10625033f43cSJassi Brar .startup = i2s_startup, 10635033f43cSJassi Brar .shutdown = i2s_shutdown, 10645033f43cSJassi Brar .delay = i2s_delay, 10655033f43cSJassi Brar }; 10665033f43cSJassi Brar 10674b828535SKuninori Morimoto static const struct snd_soc_component_driver samsung_i2s_component = { 10684b828535SKuninori Morimoto .name = "samsung-i2s", 10694b828535SKuninori Morimoto }; 10704b828535SKuninori Morimoto 10715033f43cSJassi Brar #define SAMSUNG_I2S_RATES SNDRV_PCM_RATE_8000_96000 10725033f43cSJassi Brar 10735033f43cSJassi Brar #define SAMSUNG_I2S_FMTS (SNDRV_PCM_FMTBIT_S8 | \ 10745033f43cSJassi Brar SNDRV_PCM_FMTBIT_S16_LE | \ 10755033f43cSJassi Brar SNDRV_PCM_FMTBIT_S24_LE) 10765033f43cSJassi Brar 1077fdca21adSBill Pemberton static struct i2s_dai *i2s_alloc_dai(struct platform_device *pdev, bool sec) 10785033f43cSJassi Brar { 10795033f43cSJassi Brar struct i2s_dai *i2s; 1080c6f9b1ebSPrathyush K int ret; 10815033f43cSJassi Brar 1082b960ce74SMark Brown i2s = devm_kzalloc(&pdev->dev, sizeof(struct i2s_dai), GFP_KERNEL); 10835033f43cSJassi Brar if (i2s == NULL) 10845033f43cSJassi Brar return NULL; 10855033f43cSJassi Brar 10865033f43cSJassi Brar i2s->pdev = pdev; 10875033f43cSJassi Brar i2s->pri_dai = NULL; 10885033f43cSJassi Brar i2s->sec_dai = NULL; 10895033f43cSJassi Brar i2s->i2s_dai_drv.symmetric_rates = 1; 10905033f43cSJassi Brar i2s->i2s_dai_drv.probe = samsung_i2s_dai_probe; 10915033f43cSJassi Brar i2s->i2s_dai_drv.remove = samsung_i2s_dai_remove; 10925033f43cSJassi Brar i2s->i2s_dai_drv.ops = &samsung_i2s_dai_ops; 10935033f43cSJassi Brar i2s->i2s_dai_drv.suspend = i2s_suspend; 10945033f43cSJassi Brar i2s->i2s_dai_drv.resume = i2s_resume; 1095a0ff6ea2SCharles Keepax i2s->i2s_dai_drv.playback.channels_min = 1; 10965033f43cSJassi Brar i2s->i2s_dai_drv.playback.channels_max = 2; 10975033f43cSJassi Brar i2s->i2s_dai_drv.playback.rates = SAMSUNG_I2S_RATES; 10985033f43cSJassi Brar i2s->i2s_dai_drv.playback.formats = SAMSUNG_I2S_FMTS; 10995033f43cSJassi Brar 11005033f43cSJassi Brar if (!sec) { 1101588fb705SSangsu Park i2s->i2s_dai_drv.capture.channels_min = 1; 11025033f43cSJassi Brar i2s->i2s_dai_drv.capture.channels_max = 2; 11035033f43cSJassi Brar i2s->i2s_dai_drv.capture.rates = SAMSUNG_I2S_RATES; 11045033f43cSJassi Brar i2s->i2s_dai_drv.capture.formats = SAMSUNG_I2S_FMTS; 1105c6f9b1ebSPrathyush K dev_set_drvdata(&i2s->pdev->dev, i2s); 11065033f43cSJassi Brar } else { /* Create a new platform_device for Secondary */ 1107c6f9b1ebSPrathyush K i2s->pdev = platform_device_alloc("samsung-i2s-sec", -1); 110829ca9c73SWei Yongjun if (!i2s->pdev) 11095033f43cSJassi Brar return NULL; 11105033f43cSJassi Brar 11112f6f0ffbSMark Brown i2s->pdev->dev.parent = &pdev->dev; 11122f6f0ffbSMark Brown 1113c6f9b1ebSPrathyush K platform_set_drvdata(i2s->pdev, i2s); 1114c6f9b1ebSPrathyush K ret = platform_device_add(i2s->pdev); 1115c6f9b1ebSPrathyush K if (ret < 0) 1116c6f9b1ebSPrathyush K return NULL; 1117c6f9b1ebSPrathyush K } 11185033f43cSJassi Brar 11195033f43cSJassi Brar return i2s; 11205033f43cSJassi Brar } 11215033f43cSJassi Brar 112240476f61SPadmavathi Venna static const struct of_device_id exynos_i2s_match[]; 112340476f61SPadmavathi Venna 11247da493e9SPadmavathi Venna static inline const struct samsung_i2s_dai_data *samsung_i2s_get_driver_data( 11257da493e9SPadmavathi Venna struct platform_device *pdev) 11267c62eebbSPadmavathi Venna { 112740476f61SPadmavathi Venna #ifdef CONFIG_OF 112840476f61SPadmavathi Venna if (pdev->dev.of_node) { 112940476f61SPadmavathi Venna const struct of_device_id *match; 113040476f61SPadmavathi Venna match = of_match_node(exynos_i2s_match, pdev->dev.of_node); 11317da493e9SPadmavathi Venna return match->data; 113240476f61SPadmavathi Venna } else 113340476f61SPadmavathi Venna #endif 11347da493e9SPadmavathi Venna return (struct samsung_i2s_dai_data *) 11357da493e9SPadmavathi Venna platform_get_device_id(pdev)->driver_data; 11367c62eebbSPadmavathi Venna } 11377c62eebbSPadmavathi Venna 11385b1d3c34SR. Chandrasekar #ifdef CONFIG_PM_RUNTIME 11395b1d3c34SR. Chandrasekar static int i2s_runtime_suspend(struct device *dev) 11405b1d3c34SR. Chandrasekar { 11415b1d3c34SR. Chandrasekar struct i2s_dai *i2s = dev_get_drvdata(dev); 11425b1d3c34SR. Chandrasekar 11435b1d3c34SR. Chandrasekar clk_disable_unprepare(i2s->clk); 11445b1d3c34SR. Chandrasekar 11455b1d3c34SR. Chandrasekar return 0; 11465b1d3c34SR. Chandrasekar } 11475b1d3c34SR. Chandrasekar 11485b1d3c34SR. Chandrasekar static int i2s_runtime_resume(struct device *dev) 11495b1d3c34SR. Chandrasekar { 11505b1d3c34SR. Chandrasekar struct i2s_dai *i2s = dev_get_drvdata(dev); 11515b1d3c34SR. Chandrasekar 11525b1d3c34SR. Chandrasekar clk_prepare_enable(i2s->clk); 11535b1d3c34SR. Chandrasekar 11545b1d3c34SR. Chandrasekar return 0; 11555b1d3c34SR. Chandrasekar } 11565b1d3c34SR. Chandrasekar #endif /* CONFIG_PM_RUNTIME */ 11575b1d3c34SR. Chandrasekar 1158fdca21adSBill Pemberton static int samsung_i2s_probe(struct platform_device *pdev) 11595033f43cSJassi Brar { 11605033f43cSJassi Brar struct i2s_dai *pri_dai, *sec_dai = NULL; 116140476f61SPadmavathi Venna struct s3c_audio_pdata *i2s_pdata = pdev->dev.platform_data; 116240476f61SPadmavathi Venna struct samsung_i2s *i2s_cfg = NULL; 11635033f43cSJassi Brar struct resource *res; 116440476f61SPadmavathi Venna u32 regs_base, quirks = 0, idma_addr = 0; 116540476f61SPadmavathi Venna struct device_node *np = pdev->dev.of_node; 11667da493e9SPadmavathi Venna const struct samsung_i2s_dai_data *i2s_dai_data; 11675033f43cSJassi Brar int ret = 0; 11685033f43cSJassi Brar 11695033f43cSJassi Brar /* Call during Seconday interface registration */ 11707da493e9SPadmavathi Venna i2s_dai_data = samsung_i2s_get_driver_data(pdev); 11717c62eebbSPadmavathi Venna 11727da493e9SPadmavathi Venna if (i2s_dai_data->dai_type == TYPE_SEC) { 11735033f43cSJassi Brar sec_dai = dev_get_drvdata(&pdev->dev); 1174a9b977ecSPrathyush K if (!sec_dai) { 1175a9b977ecSPrathyush K dev_err(&pdev->dev, "Unable to get drvdata\n"); 1176a9b977ecSPrathyush K return -EFAULT; 1177a9b977ecSPrathyush K } 1178d644a115SMark Brown devm_snd_soc_register_component(&sec_dai->pdev->dev, 11794b828535SKuninori Morimoto &samsung_i2s_component, 11804b828535SKuninori Morimoto &sec_dai->i2s_dai_drv, 1); 118185ff3c29SMark Brown samsung_asoc_dma_platform_register(&pdev->dev); 11825033f43cSJassi Brar return 0; 11835033f43cSJassi Brar } 11845033f43cSJassi Brar 118540476f61SPadmavathi Venna pri_dai = i2s_alloc_dai(pdev, false); 118640476f61SPadmavathi Venna if (!pri_dai) { 118740476f61SPadmavathi Venna dev_err(&pdev->dev, "Unable to alloc I2S_pri\n"); 118840476f61SPadmavathi Venna return -ENOMEM; 118940476f61SPadmavathi Venna } 119040476f61SPadmavathi Venna 119140476f61SPadmavathi Venna if (!np) { 119240476f61SPadmavathi Venna res = platform_get_resource(pdev, IORESOURCE_DMA, 0); 119340476f61SPadmavathi Venna if (!res) { 119440476f61SPadmavathi Venna dev_err(&pdev->dev, 119540476f61SPadmavathi Venna "Unable to get I2S-TX dma resource\n"); 119640476f61SPadmavathi Venna return -ENXIO; 119740476f61SPadmavathi Venna } 119840476f61SPadmavathi Venna pri_dai->dma_playback.channel = res->start; 119940476f61SPadmavathi Venna 120040476f61SPadmavathi Venna res = platform_get_resource(pdev, IORESOURCE_DMA, 1); 120140476f61SPadmavathi Venna if (!res) { 120240476f61SPadmavathi Venna dev_err(&pdev->dev, 120340476f61SPadmavathi Venna "Unable to get I2S-RX dma resource\n"); 120440476f61SPadmavathi Venna return -ENXIO; 120540476f61SPadmavathi Venna } 120640476f61SPadmavathi Venna pri_dai->dma_capture.channel = res->start; 120740476f61SPadmavathi Venna 12085033f43cSJassi Brar if (i2s_pdata == NULL) { 12095033f43cSJassi Brar dev_err(&pdev->dev, "Can't work without s3c_audio_pdata\n"); 12105033f43cSJassi Brar return -EINVAL; 12115033f43cSJassi Brar } 12125033f43cSJassi Brar 121340476f61SPadmavathi Venna if (&i2s_pdata->type) 121440476f61SPadmavathi Venna i2s_cfg = &i2s_pdata->type.i2s; 12155033f43cSJassi Brar 121640476f61SPadmavathi Venna if (i2s_cfg) { 121740476f61SPadmavathi Venna quirks = i2s_cfg->quirks; 121840476f61SPadmavathi Venna idma_addr = i2s_cfg->idma_addr; 12195033f43cSJassi Brar } 122040476f61SPadmavathi Venna } else { 12217da493e9SPadmavathi Venna quirks = i2s_dai_data->quirks; 122240476f61SPadmavathi Venna if (of_property_read_u32(np, "samsung,idma-addr", 122340476f61SPadmavathi Venna &idma_addr)) { 1224b0759736SPadmavathi Venna if (quirks & QUIRK_SUPPORTS_IDMA) { 1225b0759736SPadmavathi Venna dev_info(&pdev->dev, "idma address is not"\ 122640476f61SPadmavathi Venna "specified"); 122740476f61SPadmavathi Venna } 122840476f61SPadmavathi Venna } 122940476f61SPadmavathi Venna } 12305033f43cSJassi Brar 12315033f43cSJassi Brar res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 12325033f43cSJassi Brar if (!res) { 12335033f43cSJassi Brar dev_err(&pdev->dev, "Unable to get I2S SFR address\n"); 12345033f43cSJassi Brar return -ENXIO; 12355033f43cSJassi Brar } 12365033f43cSJassi Brar 12375033f43cSJassi Brar if (!request_mem_region(res->start, resource_size(res), 12385033f43cSJassi Brar "samsung-i2s")) { 12395033f43cSJassi Brar dev_err(&pdev->dev, "Unable to request SFR region\n"); 12405033f43cSJassi Brar return -EBUSY; 12415033f43cSJassi Brar } 12425033f43cSJassi Brar regs_base = res->start; 12435033f43cSJassi Brar 12445033f43cSJassi Brar pri_dai->dma_playback.dma_addr = regs_base + I2STXD; 12455033f43cSJassi Brar pri_dai->dma_capture.dma_addr = regs_base + I2SRXD; 124640476f61SPadmavathi Venna pri_dai->dma_playback.ch_name = "tx"; 124740476f61SPadmavathi Venna pri_dai->dma_capture.ch_name = "rx"; 12485033f43cSJassi Brar pri_dai->dma_playback.dma_size = 4; 12495033f43cSJassi Brar pri_dai->dma_capture.dma_size = 4; 12505033f43cSJassi Brar pri_dai->base = regs_base; 12515033f43cSJassi Brar pri_dai->quirks = quirks; 1252a5a56871SPadmavathi Venna pri_dai->variant_regs = i2s_dai_data->i2s_variant_regs; 12535033f43cSJassi Brar 12545033f43cSJassi Brar if (quirks & QUIRK_PRI_6CHAN) 12555033f43cSJassi Brar pri_dai->i2s_dai_drv.playback.channels_max = 6; 12565033f43cSJassi Brar 12575033f43cSJassi Brar if (quirks & QUIRK_SEC_DAI) { 12585033f43cSJassi Brar sec_dai = i2s_alloc_dai(pdev, true); 12595033f43cSJassi Brar if (!sec_dai) { 12605033f43cSJassi Brar dev_err(&pdev->dev, "Unable to alloc I2S_sec\n"); 12615033f43cSJassi Brar ret = -ENOMEM; 1262b960ce74SMark Brown goto err; 12635033f43cSJassi Brar } 12645033f43cSJassi Brar sec_dai->dma_playback.dma_addr = regs_base + I2STXDS; 126540476f61SPadmavathi Venna sec_dai->dma_playback.ch_name = "tx-sec"; 126640476f61SPadmavathi Venna 126740476f61SPadmavathi Venna if (!np) { 126840476f61SPadmavathi Venna res = platform_get_resource(pdev, IORESOURCE_DMA, 2); 126940476f61SPadmavathi Venna if (res) 127040476f61SPadmavathi Venna sec_dai->dma_playback.channel = res->start; 127140476f61SPadmavathi Venna } 127240476f61SPadmavathi Venna 12735033f43cSJassi Brar sec_dai->dma_playback.dma_size = 4; 12745033f43cSJassi Brar sec_dai->base = regs_base; 12755033f43cSJassi Brar sec_dai->quirks = quirks; 127640476f61SPadmavathi Venna sec_dai->idma_playback.dma_addr = idma_addr; 12775033f43cSJassi Brar sec_dai->pri_dai = pri_dai; 12785033f43cSJassi Brar pri_dai->sec_dai = sec_dai; 12795033f43cSJassi Brar } 12805033f43cSJassi Brar 12810429ffefSMark Brown if (i2s_pdata && i2s_pdata->cfg_gpio && i2s_pdata->cfg_gpio(pdev)) { 128240476f61SPadmavathi Venna dev_err(&pdev->dev, "Unable to configure gpio\n"); 128340476f61SPadmavathi Venna ret = -EINVAL; 128440476f61SPadmavathi Venna goto err; 128540476f61SPadmavathi Venna } 12865033f43cSJassi Brar 1287d644a115SMark Brown devm_snd_soc_register_component(&pri_dai->pdev->dev, 1288d644a115SMark Brown &samsung_i2s_component, 12894b828535SKuninori Morimoto &pri_dai->i2s_dai_drv, 1); 12905033f43cSJassi Brar 1291c5cf4dbcSMark Brown pm_runtime_enable(&pdev->dev); 1292c5cf4dbcSMark Brown 129385ff3c29SMark Brown samsung_asoc_dma_platform_register(&pdev->dev); 1294a08485d8SPadmavathi Venna 12955033f43cSJassi Brar return 0; 1296b960ce74SMark Brown err: 129757e33781SSachin Kamat if (res) 12985033f43cSJassi Brar release_mem_region(regs_base, resource_size(res)); 12995033f43cSJassi Brar 13005033f43cSJassi Brar return ret; 13015033f43cSJassi Brar } 13025033f43cSJassi Brar 1303fdca21adSBill Pemberton static int samsung_i2s_remove(struct platform_device *pdev) 13045033f43cSJassi Brar { 13055033f43cSJassi Brar struct i2s_dai *i2s, *other; 1306c5cf4dbcSMark Brown struct resource *res; 13075033f43cSJassi Brar 13085033f43cSJassi Brar i2s = dev_get_drvdata(&pdev->dev); 13095033f43cSJassi Brar other = i2s->pri_dai ? : i2s->sec_dai; 13105033f43cSJassi Brar 13115033f43cSJassi Brar if (other) { 13125033f43cSJassi Brar other->pri_dai = NULL; 13135033f43cSJassi Brar other->sec_dai = NULL; 13145033f43cSJassi Brar } else { 1315c5cf4dbcSMark Brown pm_runtime_disable(&pdev->dev); 13165033f43cSJassi Brar res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 13175033f43cSJassi Brar if (res) 13185033f43cSJassi Brar release_mem_region(res->start, resource_size(res)); 13195033f43cSJassi Brar } 13205033f43cSJassi Brar 13215033f43cSJassi Brar i2s->pri_dai = NULL; 13225033f43cSJassi Brar i2s->sec_dai = NULL; 13235033f43cSJassi Brar 13245033f43cSJassi Brar return 0; 13255033f43cSJassi Brar } 13265033f43cSJassi Brar 1327a5a56871SPadmavathi Venna static const struct samsung_i2s_variant_regs i2sv3_regs = { 1328a5a56871SPadmavathi Venna .bfs_off = 1, 1329a5a56871SPadmavathi Venna .rfs_off = 3, 1330a5a56871SPadmavathi Venna .sdf_off = 5, 1331a5a56871SPadmavathi Venna .txr_off = 8, 1332a5a56871SPadmavathi Venna .rclksrc_off = 10, 1333a5a56871SPadmavathi Venna .mss_off = 11, 1334a5a56871SPadmavathi Venna .cdclkcon_off = 12, 1335a5a56871SPadmavathi Venna .lrp_off = 7, 1336a5a56871SPadmavathi Venna .bfs_mask = 0x3, 1337a5a56871SPadmavathi Venna .rfs_mask = 0x3, 1338a5a56871SPadmavathi Venna .ftx0cnt_off = 8, 1339a5a56871SPadmavathi Venna }; 1340a5a56871SPadmavathi Venna 1341a5a56871SPadmavathi Venna static const struct samsung_i2s_variant_regs i2sv6_regs = { 1342a5a56871SPadmavathi Venna .bfs_off = 0, 1343a5a56871SPadmavathi Venna .rfs_off = 4, 1344a5a56871SPadmavathi Venna .sdf_off = 6, 1345a5a56871SPadmavathi Venna .txr_off = 8, 1346a5a56871SPadmavathi Venna .rclksrc_off = 10, 1347a5a56871SPadmavathi Venna .mss_off = 11, 1348a5a56871SPadmavathi Venna .cdclkcon_off = 12, 1349a5a56871SPadmavathi Venna .lrp_off = 15, 1350a5a56871SPadmavathi Venna .bfs_mask = 0xf, 1351a5a56871SPadmavathi Venna .rfs_mask = 0x3, 1352a5a56871SPadmavathi Venna .ftx0cnt_off = 8, 1353a5a56871SPadmavathi Venna }; 1354a5a56871SPadmavathi Venna 1355a5a56871SPadmavathi Venna static const struct samsung_i2s_variant_regs i2sv7_regs = { 1356a5a56871SPadmavathi Venna .bfs_off = 0, 1357a5a56871SPadmavathi Venna .rfs_off = 4, 1358a5a56871SPadmavathi Venna .sdf_off = 7, 1359a5a56871SPadmavathi Venna .txr_off = 9, 1360a5a56871SPadmavathi Venna .rclksrc_off = 11, 1361a5a56871SPadmavathi Venna .mss_off = 12, 1362a5a56871SPadmavathi Venna .cdclkcon_off = 22, 1363a5a56871SPadmavathi Venna .lrp_off = 15, 1364a5a56871SPadmavathi Venna .bfs_mask = 0xf, 1365a5a56871SPadmavathi Venna .rfs_mask = 0x7, 1366a5a56871SPadmavathi Venna .ftx0cnt_off = 0, 1367a5a56871SPadmavathi Venna }; 1368a5a56871SPadmavathi Venna 1369a5a56871SPadmavathi Venna static const struct samsung_i2s_variant_regs i2sv5_i2s1_regs = { 1370a5a56871SPadmavathi Venna .bfs_off = 0, 1371a5a56871SPadmavathi Venna .rfs_off = 3, 1372a5a56871SPadmavathi Venna .sdf_off = 6, 1373a5a56871SPadmavathi Venna .txr_off = 8, 1374a5a56871SPadmavathi Venna .rclksrc_off = 10, 1375a5a56871SPadmavathi Venna .mss_off = 11, 1376a5a56871SPadmavathi Venna .cdclkcon_off = 12, 1377a5a56871SPadmavathi Venna .lrp_off = 15, 1378a5a56871SPadmavathi Venna .bfs_mask = 0x7, 1379a5a56871SPadmavathi Venna .rfs_mask = 0x7, 1380a5a56871SPadmavathi Venna .ftx0cnt_off = 8, 1381a5a56871SPadmavathi Venna }; 1382a5a56871SPadmavathi Venna 13837da493e9SPadmavathi Venna static const struct samsung_i2s_dai_data i2sv3_dai_type = { 13847da493e9SPadmavathi Venna .dai_type = TYPE_PRI, 13857da493e9SPadmavathi Venna .quirks = QUIRK_NO_MUXPSR, 1386a5a56871SPadmavathi Venna .i2s_variant_regs = &i2sv3_regs, 13877da493e9SPadmavathi Venna }; 13887da493e9SPadmavathi Venna 13897da493e9SPadmavathi Venna static const struct samsung_i2s_dai_data i2sv5_dai_type = { 13907da493e9SPadmavathi Venna .dai_type = TYPE_PRI, 1391b0759736SPadmavathi Venna .quirks = QUIRK_PRI_6CHAN | QUIRK_SEC_DAI | QUIRK_NEED_RSTCLR | 1392b0759736SPadmavathi Venna QUIRK_SUPPORTS_IDMA, 1393a5a56871SPadmavathi Venna .i2s_variant_regs = &i2sv3_regs, 13947da493e9SPadmavathi Venna }; 13957da493e9SPadmavathi Venna 13964ca0c0d4SPadmavathi Venna static const struct samsung_i2s_dai_data i2sv6_dai_type = { 13974ca0c0d4SPadmavathi Venna .dai_type = TYPE_PRI, 13984ca0c0d4SPadmavathi Venna .quirks = QUIRK_PRI_6CHAN | QUIRK_SEC_DAI | QUIRK_NEED_RSTCLR | 1399b0759736SPadmavathi Venna QUIRK_SUPPORTS_TDM | QUIRK_SUPPORTS_IDMA, 1400a5a56871SPadmavathi Venna .i2s_variant_regs = &i2sv6_regs, 1401a5a56871SPadmavathi Venna }; 1402a5a56871SPadmavathi Venna 1403a5a56871SPadmavathi Venna static const struct samsung_i2s_dai_data i2sv7_dai_type = { 1404a5a56871SPadmavathi Venna .dai_type = TYPE_PRI, 1405a5a56871SPadmavathi Venna .quirks = QUIRK_PRI_6CHAN | QUIRK_SEC_DAI | QUIRK_NEED_RSTCLR | 1406a5a56871SPadmavathi Venna QUIRK_SUPPORTS_TDM, 1407a5a56871SPadmavathi Venna .i2s_variant_regs = &i2sv7_regs, 1408a5a56871SPadmavathi Venna }; 1409a5a56871SPadmavathi Venna 1410a5a56871SPadmavathi Venna static const struct samsung_i2s_dai_data i2sv5_dai_type_i2s1 = { 1411a5a56871SPadmavathi Venna .dai_type = TYPE_PRI, 1412a5a56871SPadmavathi Venna .quirks = QUIRK_PRI_6CHAN | QUIRK_NEED_RSTCLR, 1413a5a56871SPadmavathi Venna .i2s_variant_regs = &i2sv5_i2s1_regs, 14144ca0c0d4SPadmavathi Venna }; 14154ca0c0d4SPadmavathi Venna 14167da493e9SPadmavathi Venna static const struct samsung_i2s_dai_data samsung_dai_type_pri = { 14177da493e9SPadmavathi Venna .dai_type = TYPE_PRI, 14187da493e9SPadmavathi Venna }; 14197da493e9SPadmavathi Venna 14207da493e9SPadmavathi Venna static const struct samsung_i2s_dai_data samsung_dai_type_sec = { 14217da493e9SPadmavathi Venna .dai_type = TYPE_SEC, 14227da493e9SPadmavathi Venna }; 14237da493e9SPadmavathi Venna 14247c62eebbSPadmavathi Venna static struct platform_device_id samsung_i2s_driver_ids[] = { 14257c62eebbSPadmavathi Venna { 14267c62eebbSPadmavathi Venna .name = "samsung-i2s", 1427*3f024980SMark Brown .driver_data = (kernel_ulong_t)&i2sv3_dai_type, 14287c62eebbSPadmavathi Venna }, { 14297c62eebbSPadmavathi Venna .name = "samsung-i2s-sec", 14307da493e9SPadmavathi Venna .driver_data = (kernel_ulong_t)&samsung_dai_type_sec, 1431*3f024980SMark Brown }, { 1432*3f024980SMark Brown .name = "samsung-i2sv4", 1433*3f024980SMark Brown .driver_data = (kernel_ulong_t)&i2sv5_dai_type, 14347c62eebbSPadmavathi Venna }, 14357c62eebbSPadmavathi Venna {}, 14367c62eebbSPadmavathi Venna }; 14372af19558SArnd Bergmann MODULE_DEVICE_TABLE(platform, samsung_i2s_driver_ids); 14387c62eebbSPadmavathi Venna 143940476f61SPadmavathi Venna #ifdef CONFIG_OF 144040476f61SPadmavathi Venna static const struct of_device_id exynos_i2s_match[] = { 14417da493e9SPadmavathi Venna { 14427da493e9SPadmavathi Venna .compatible = "samsung,s3c6410-i2s", 14437da493e9SPadmavathi Venna .data = &i2sv3_dai_type, 14447da493e9SPadmavathi Venna }, { 14457da493e9SPadmavathi Venna .compatible = "samsung,s5pv210-i2s", 14467da493e9SPadmavathi Venna .data = &i2sv5_dai_type, 14474ca0c0d4SPadmavathi Venna }, { 14484ca0c0d4SPadmavathi Venna .compatible = "samsung,exynos5420-i2s", 14494ca0c0d4SPadmavathi Venna .data = &i2sv6_dai_type, 1450a5a56871SPadmavathi Venna }, { 1451a5a56871SPadmavathi Venna .compatible = "samsung,exynos7-i2s", 1452a5a56871SPadmavathi Venna .data = &i2sv7_dai_type, 1453a5a56871SPadmavathi Venna }, { 1454a5a56871SPadmavathi Venna .compatible = "samsung,exynos7-i2s1", 1455a5a56871SPadmavathi Venna .data = &i2sv5_dai_type_i2s1, 145640476f61SPadmavathi Venna }, 145740476f61SPadmavathi Venna {}, 145840476f61SPadmavathi Venna }; 145940476f61SPadmavathi Venna MODULE_DEVICE_TABLE(of, exynos_i2s_match); 146040476f61SPadmavathi Venna #endif 146140476f61SPadmavathi Venna 14625b1d3c34SR. Chandrasekar static const struct dev_pm_ops samsung_i2s_pm = { 14635b1d3c34SR. Chandrasekar SET_RUNTIME_PM_OPS(i2s_runtime_suspend, 14645b1d3c34SR. Chandrasekar i2s_runtime_resume, NULL) 14655b1d3c34SR. Chandrasekar }; 14665b1d3c34SR. Chandrasekar 14675033f43cSJassi Brar static struct platform_driver samsung_i2s_driver = { 14685033f43cSJassi Brar .probe = samsung_i2s_probe, 1469fdca21adSBill Pemberton .remove = samsung_i2s_remove, 14707c62eebbSPadmavathi Venna .id_table = samsung_i2s_driver_ids, 14715033f43cSJassi Brar .driver = { 14725033f43cSJassi Brar .name = "samsung-i2s", 14735033f43cSJassi Brar .owner = THIS_MODULE, 147440476f61SPadmavathi Venna .of_match_table = of_match_ptr(exynos_i2s_match), 14755b1d3c34SR. Chandrasekar .pm = &samsung_i2s_pm, 14765033f43cSJassi Brar }, 14775033f43cSJassi Brar }; 14785033f43cSJassi Brar 1479e00c3f55SMark Brown module_platform_driver(samsung_i2s_driver); 14805033f43cSJassi Brar 14815033f43cSJassi Brar /* Module information */ 1482df8ad335SJaswinder Singh MODULE_AUTHOR("Jaswinder Singh, <jassisinghbrar@gmail.com>"); 14835033f43cSJassi Brar MODULE_DESCRIPTION("Samsung I2S Interface"); 14845033f43cSJassi Brar MODULE_ALIAS("platform:samsung-i2s"); 14855033f43cSJassi Brar MODULE_LICENSE("GPL"); 1486