Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Freescale SSI ALSA SoC Digital Audio Interface (DAI) driver |
| 3 | * |
| 4 | * Author: Timur Tabi <timur@freescale.com> |
| 5 | * |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 6 | * Copyright 2007-2010 Freescale Semiconductor, Inc. |
| 7 | * |
| 8 | * This file is licensed under the terms of the GNU General Public License |
| 9 | * version 2. This program is licensed "as is" without any warranty of any |
| 10 | * kind, whether express or implied. |
Markus Pargmann | de623ec | 2013-07-27 13:31:53 +0200 | [diff] [blame] | 11 | * |
| 12 | * |
| 13 | * Some notes why imx-pcm-fiq is used instead of DMA on some boards: |
| 14 | * |
| 15 | * The i.MX SSI core has some nasty limitations in AC97 mode. While most |
| 16 | * sane processor vendors have a FIFO per AC97 slot, the i.MX has only |
| 17 | * one FIFO which combines all valid receive slots. We cannot even select |
| 18 | * which slots we want to receive. The WM9712 with which this driver |
| 19 | * was developed with always sends GPIO status data in slot 12 which |
| 20 | * we receive in our (PCM-) data stream. The only chance we have is to |
| 21 | * manually skip this data in the FIQ handler. With sampling rates different |
| 22 | * from 48000Hz not every frame has valid receive data, so the ratio |
| 23 | * between pcm data and GPIO status data changes. Our FIQ handler is not |
| 24 | * able to handle this, hence this driver only works with 48000Hz sampling |
| 25 | * rate. |
| 26 | * Reading and writing AC97 registers is another challenge. The core |
| 27 | * provides us status bits when the read register is updated with *another* |
| 28 | * value. When we read the same register two times (and the register still |
| 29 | * contains the same value) these status bits are not set. We work |
| 30 | * around this by not polling these bits but only wait a fixed delay. |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 31 | */ |
| 32 | |
| 33 | #include <linux/init.h> |
Shawn Guo | dfa1a10 | 2012-03-16 16:56:42 +0800 | [diff] [blame] | 34 | #include <linux/io.h> |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 35 | #include <linux/module.h> |
| 36 | #include <linux/interrupt.h> |
Shawn Guo | 95cd98f | 2012-03-29 10:53:41 +0800 | [diff] [blame] | 37 | #include <linux/clk.h> |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 38 | #include <linux/device.h> |
| 39 | #include <linux/delay.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 40 | #include <linux/slab.h> |
Nicolin Chen | aafa85e | 2013-12-12 18:44:45 +0800 | [diff] [blame] | 41 | #include <linux/spinlock.h> |
Mark Brown | 9c72a04 | 2014-04-15 12:02:02 +0100 | [diff] [blame] | 42 | #include <linux/of.h> |
Shawn Guo | dfa1a10 | 2012-03-16 16:56:42 +0800 | [diff] [blame] | 43 | #include <linux/of_address.h> |
| 44 | #include <linux/of_irq.h> |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 45 | #include <linux/of_platform.h> |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 46 | |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 47 | #include <sound/core.h> |
| 48 | #include <sound/pcm.h> |
| 49 | #include <sound/pcm_params.h> |
| 50 | #include <sound/initval.h> |
| 51 | #include <sound/soc.h> |
Lars-Peter Clausen | a8909c9 | 2013-04-03 11:06:04 +0200 | [diff] [blame] | 52 | #include <sound/dmaengine_pcm.h> |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 53 | |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 54 | #include "fsl_ssi.h" |
Shawn Guo | 09ce111 | 2012-03-16 16:56:43 +0800 | [diff] [blame] | 55 | #include "imx-pcm.h" |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 56 | |
| 57 | /** |
| 58 | * FSLSSI_I2S_RATES: sample rates supported by the I2S |
| 59 | * |
| 60 | * This driver currently only supports the SSI running in I2S slave mode, |
| 61 | * which means the codec determines the sample rate. Therefore, we tell |
| 62 | * ALSA that we support all rates and let the codec driver decide what rates |
| 63 | * are really supported. |
| 64 | */ |
Lars-Peter Clausen | 24710c9 | 2014-01-11 10:24:41 +0100 | [diff] [blame] | 65 | #define FSLSSI_I2S_RATES SNDRV_PCM_RATE_CONTINUOUS |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 66 | |
| 67 | /** |
| 68 | * FSLSSI_I2S_FORMATS: audio formats supported by the SSI |
| 69 | * |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 70 | * The SSI has a limitation in that the samples must be in the same byte |
| 71 | * order as the host CPU. This is because when multiple bytes are written |
| 72 | * to the STX register, the bytes and bits must be written in the same |
| 73 | * order. The STX is a shift register, so all the bits need to be aligned |
| 74 | * (bit-endianness must match byte-endianness). Processors typically write |
| 75 | * the bits within a byte in the same order that the bytes of a word are |
| 76 | * written in. So if the host CPU is big-endian, then only big-endian |
| 77 | * samples will be written to STX properly. |
| 78 | */ |
| 79 | #ifdef __BIG_ENDIAN |
| 80 | #define FSLSSI_I2S_FORMATS (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_BE | \ |
| 81 | SNDRV_PCM_FMTBIT_S18_3BE | SNDRV_PCM_FMTBIT_S20_3BE | \ |
| 82 | SNDRV_PCM_FMTBIT_S24_3BE | SNDRV_PCM_FMTBIT_S24_BE) |
| 83 | #else |
| 84 | #define FSLSSI_I2S_FORMATS (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_LE | \ |
| 85 | SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S20_3LE | \ |
| 86 | SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_LE) |
| 87 | #endif |
| 88 | |
Markus Pargmann | 9368acc | 2013-12-20 14:11:29 +0100 | [diff] [blame] | 89 | #define FSLSSI_SIER_DBG_RX_FLAGS (CCSR_SSI_SIER_RFF0_EN | \ |
| 90 | CCSR_SSI_SIER_RLS_EN | CCSR_SSI_SIER_RFS_EN | \ |
| 91 | CCSR_SSI_SIER_ROE0_EN | CCSR_SSI_SIER_RFRC_EN) |
| 92 | #define FSLSSI_SIER_DBG_TX_FLAGS (CCSR_SSI_SIER_TFE0_EN | \ |
| 93 | CCSR_SSI_SIER_TLS_EN | CCSR_SSI_SIER_TFS_EN | \ |
| 94 | CCSR_SSI_SIER_TUE0_EN | CCSR_SSI_SIER_TFRC_EN) |
Markus Pargmann | c1953bf | 2013-12-20 14:11:30 +0100 | [diff] [blame] | 95 | |
| 96 | enum fsl_ssi_type { |
| 97 | FSL_SSI_MCP8610, |
| 98 | FSL_SSI_MX21, |
Markus Pargmann | 0888efd | 2013-12-20 14:11:31 +0100 | [diff] [blame] | 99 | FSL_SSI_MX35, |
Markus Pargmann | c1953bf | 2013-12-20 14:11:30 +0100 | [diff] [blame] | 100 | FSL_SSI_MX51, |
| 101 | }; |
| 102 | |
Markus Pargmann | 4e6ec0d | 2013-12-20 14:11:33 +0100 | [diff] [blame] | 103 | struct fsl_ssi_reg_val { |
| 104 | u32 sier; |
| 105 | u32 srcr; |
| 106 | u32 stcr; |
| 107 | u32 scr; |
| 108 | }; |
| 109 | |
| 110 | struct fsl_ssi_rxtx_reg_val { |
| 111 | struct fsl_ssi_reg_val rx; |
| 112 | struct fsl_ssi_reg_val tx; |
| 113 | }; |
Zidan Wang | 05cf237 | 2015-09-18 11:09:12 +0800 | [diff] [blame] | 114 | |
Zidan Wang | 05cf237 | 2015-09-18 11:09:12 +0800 | [diff] [blame] | 115 | static bool fsl_ssi_readable_reg(struct device *dev, unsigned int reg) |
| 116 | { |
| 117 | switch (reg) { |
| 118 | case CCSR_SSI_SACCEN: |
| 119 | case CCSR_SSI_SACCDIS: |
| 120 | return false; |
| 121 | default: |
| 122 | return true; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | static bool fsl_ssi_volatile_reg(struct device *dev, unsigned int reg) |
| 127 | { |
| 128 | switch (reg) { |
| 129 | case CCSR_SSI_STX0: |
| 130 | case CCSR_SSI_STX1: |
| 131 | case CCSR_SSI_SRX0: |
| 132 | case CCSR_SSI_SRX1: |
| 133 | case CCSR_SSI_SISR: |
| 134 | case CCSR_SSI_SFCSR: |
Maciej S. Szmigiero | 3f1c241 | 2015-12-20 21:30:25 +0100 | [diff] [blame] | 135 | case CCSR_SSI_SACNT: |
Zidan Wang | 05cf237 | 2015-09-18 11:09:12 +0800 | [diff] [blame] | 136 | case CCSR_SSI_SACADD: |
| 137 | case CCSR_SSI_SACDAT: |
| 138 | case CCSR_SSI_SATAG: |
| 139 | case CCSR_SSI_SACCST: |
Caleb Crome | 3cc6185 | 2016-04-25 11:36:18 -0700 | [diff] [blame] | 140 | case CCSR_SSI_SOR: |
Zidan Wang | 05cf237 | 2015-09-18 11:09:12 +0800 | [diff] [blame] | 141 | return true; |
| 142 | default: |
| 143 | return false; |
| 144 | } |
| 145 | } |
| 146 | |
Maciej S. Szmigiero | f51e3d5 | 2015-12-20 21:31:48 +0100 | [diff] [blame] | 147 | static bool fsl_ssi_precious_reg(struct device *dev, unsigned int reg) |
| 148 | { |
| 149 | switch (reg) { |
| 150 | case CCSR_SSI_SRX0: |
| 151 | case CCSR_SSI_SRX1: |
| 152 | case CCSR_SSI_SISR: |
| 153 | case CCSR_SSI_SACADD: |
| 154 | case CCSR_SSI_SACDAT: |
| 155 | case CCSR_SSI_SATAG: |
| 156 | return true; |
| 157 | default: |
| 158 | return false; |
| 159 | } |
| 160 | } |
| 161 | |
Zidan Wang | 05cf237 | 2015-09-18 11:09:12 +0800 | [diff] [blame] | 162 | static bool fsl_ssi_writeable_reg(struct device *dev, unsigned int reg) |
| 163 | { |
| 164 | switch (reg) { |
| 165 | case CCSR_SSI_SRX0: |
| 166 | case CCSR_SSI_SRX1: |
| 167 | case CCSR_SSI_SACCST: |
| 168 | return false; |
| 169 | default: |
| 170 | return true; |
| 171 | } |
| 172 | } |
| 173 | |
Markus Pargmann | 4324812 | 2014-05-27 10:24:25 +0200 | [diff] [blame] | 174 | static const struct regmap_config fsl_ssi_regconfig = { |
| 175 | .max_register = CCSR_SSI_SACCDIS, |
| 176 | .reg_bits = 32, |
| 177 | .val_bits = 32, |
| 178 | .reg_stride = 4, |
| 179 | .val_format_endian = REGMAP_ENDIAN_NATIVE, |
Maciej S. Szmigiero | 6139b1b | 2016-01-18 20:07:44 +0100 | [diff] [blame] | 180 | .num_reg_defaults_raw = CCSR_SSI_SACCDIS / sizeof(uint32_t) + 1, |
Zidan Wang | 05cf237 | 2015-09-18 11:09:12 +0800 | [diff] [blame] | 181 | .readable_reg = fsl_ssi_readable_reg, |
| 182 | .volatile_reg = fsl_ssi_volatile_reg, |
Maciej S. Szmigiero | f51e3d5 | 2015-12-20 21:31:48 +0100 | [diff] [blame] | 183 | .precious_reg = fsl_ssi_precious_reg, |
Zidan Wang | 05cf237 | 2015-09-18 11:09:12 +0800 | [diff] [blame] | 184 | .writeable_reg = fsl_ssi_writeable_reg, |
| 185 | .cache_type = REGCACHE_RBTREE, |
Markus Pargmann | 4324812 | 2014-05-27 10:24:25 +0200 | [diff] [blame] | 186 | }; |
Timur Tabi | d5a908b | 2009-03-26 11:42:38 -0500 | [diff] [blame] | 187 | |
Sascha Hauer | fcdbade | 2014-05-27 10:24:18 +0200 | [diff] [blame] | 188 | struct fsl_ssi_soc_data { |
| 189 | bool imx; |
Maciej S. Szmigiero | 6139b1b | 2016-01-18 20:07:44 +0100 | [diff] [blame] | 190 | bool imx21regs; /* imx21-class SSI - no SACC{ST,EN,DIS} regs */ |
Sascha Hauer | fcdbade | 2014-05-27 10:24:18 +0200 | [diff] [blame] | 191 | bool offline_config; |
| 192 | u32 sisr_write_mask; |
| 193 | }; |
| 194 | |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 195 | /** |
| 196 | * fsl_ssi_private: per-SSI private data |
| 197 | * |
Markus Pargmann | 4324812 | 2014-05-27 10:24:25 +0200 | [diff] [blame] | 198 | * @reg: Pointer to the regmap registers |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 199 | * @irq: IRQ of this SSI |
Markus Pargmann | 737a6b4 | 2014-05-27 10:24:24 +0200 | [diff] [blame] | 200 | * @cpu_dai_drv: CPU DAI driver for this device |
| 201 | * |
| 202 | * @dai_fmt: DAI configuration this device is currently used with |
| 203 | * @i2s_mode: i2s and network mode configuration of the device. Is used to |
| 204 | * switch between normal and i2s/network mode |
| 205 | * mode depending on the number of channels |
| 206 | * @use_dma: DMA is used or FIQ with stream filter |
| 207 | * @use_dual_fifo: DMA with support for both FIFOs used |
| 208 | * @fifo_deph: Depth of the SSI FIFOs |
| 209 | * @rxtx_reg_val: Specific register settings for receive/transmit configuration |
| 210 | * |
| 211 | * @clk: SSI clock |
| 212 | * @baudclk: SSI baud clock for master mode |
| 213 | * @baudclk_streams: Active streams that are using baudclk |
| 214 | * @bitclk_freq: bitclock frequency set by .set_dai_sysclk |
| 215 | * |
| 216 | * @dma_params_tx: DMA transmit parameters |
| 217 | * @dma_params_rx: DMA receive parameters |
| 218 | * @ssi_phys: physical address of the SSI registers |
| 219 | * |
| 220 | * @fiq_params: FIQ stream filtering parameters |
| 221 | * |
| 222 | * @pdev: Pointer to pdev used for deprecated fsl-ssi sound card |
| 223 | * |
| 224 | * @dbg_stats: Debugging statistics |
| 225 | * |
Xiubo Li | dcfcf2c | 2015-08-12 14:38:18 +0800 | [diff] [blame] | 226 | * @soc: SoC specific data |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 227 | */ |
| 228 | struct fsl_ssi_private { |
Markus Pargmann | 4324812 | 2014-05-27 10:24:25 +0200 | [diff] [blame] | 229 | struct regmap *regs; |
Fabio Estevam | 9e446ad | 2015-01-14 10:48:59 -0200 | [diff] [blame] | 230 | int irq; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 231 | struct snd_soc_dai_driver cpu_dai_drv; |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 232 | |
Markus Pargmann | 737a6b4 | 2014-05-27 10:24:24 +0200 | [diff] [blame] | 233 | unsigned int dai_fmt; |
| 234 | u8 i2s_mode; |
Markus Pargmann | de623ec | 2013-07-27 13:31:53 +0200 | [diff] [blame] | 235 | bool use_dma; |
Nicolin Chen | 0da9e55 | 2013-11-13 22:55:26 +0800 | [diff] [blame] | 236 | bool use_dual_fifo; |
Shengjiu Wang | f4a43ca | 2014-09-16 10:13:16 +0800 | [diff] [blame] | 237 | bool has_ipg_clk_name; |
Markus Pargmann | 737a6b4 | 2014-05-27 10:24:24 +0200 | [diff] [blame] | 238 | unsigned int fifo_depth; |
| 239 | struct fsl_ssi_rxtx_reg_val rxtx_reg_val; |
| 240 | |
Shawn Guo | 95cd98f | 2012-03-29 10:53:41 +0800 | [diff] [blame] | 241 | struct clk *clk; |
Markus Pargmann | 737a6b4 | 2014-05-27 10:24:24 +0200 | [diff] [blame] | 242 | struct clk *baudclk; |
Markus Pargmann | d429d8e | 2014-05-27 10:24:23 +0200 | [diff] [blame] | 243 | unsigned int baudclk_streams; |
Sascha Hauer | 8dd51e2 | 2014-05-27 10:24:20 +0200 | [diff] [blame] | 244 | unsigned int bitclk_freq; |
Markus Pargmann | 737a6b4 | 2014-05-27 10:24:24 +0200 | [diff] [blame] | 245 | |
Maciej S. Szmigiero | 3f1c241 | 2015-12-20 21:30:25 +0100 | [diff] [blame] | 246 | /* regcache for volatile regs */ |
Zidan Wang | 05cf237 | 2015-09-18 11:09:12 +0800 | [diff] [blame] | 247 | u32 regcache_sfcsr; |
Maciej S. Szmigiero | 3f1c241 | 2015-12-20 21:30:25 +0100 | [diff] [blame] | 248 | u32 regcache_sacnt; |
Zidan Wang | 05cf237 | 2015-09-18 11:09:12 +0800 | [diff] [blame] | 249 | |
Markus Pargmann | 737a6b4 | 2014-05-27 10:24:24 +0200 | [diff] [blame] | 250 | /* DMA params */ |
Lars-Peter Clausen | a8909c9 | 2013-04-03 11:06:04 +0200 | [diff] [blame] | 251 | struct snd_dmaengine_dai_dma_data dma_params_tx; |
| 252 | struct snd_dmaengine_dai_dma_data dma_params_rx; |
Markus Pargmann | 737a6b4 | 2014-05-27 10:24:24 +0200 | [diff] [blame] | 253 | dma_addr_t ssi_phys; |
| 254 | |
| 255 | /* params for non-dma FIQ stream filtered mode */ |
Markus Pargmann | de623ec | 2013-07-27 13:31:53 +0200 | [diff] [blame] | 256 | struct imx_pcm_fiq_params fiq_params; |
Markus Pargmann | 737a6b4 | 2014-05-27 10:24:24 +0200 | [diff] [blame] | 257 | |
| 258 | /* Used when using fsl-ssi as sound-card. This is only used by ppc and |
| 259 | * should be replaced with simple-sound-card. */ |
| 260 | struct platform_device *pdev; |
Shawn Guo | 09ce111 | 2012-03-16 16:56:43 +0800 | [diff] [blame] | 261 | |
Markus Pargmann | f138e62 | 2014-04-28 12:54:43 +0200 | [diff] [blame] | 262 | struct fsl_ssi_dbg dbg_stats; |
Sascha Hauer | fcdbade | 2014-05-27 10:24:18 +0200 | [diff] [blame] | 263 | |
| 264 | const struct fsl_ssi_soc_data *soc; |
Arnaud Mouiche | 0096b69 | 2016-05-03 14:13:57 +0200 | [diff] [blame] | 265 | struct device *dev; |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 266 | }; |
| 267 | |
Markus Pargmann | 171d683 | 2014-04-28 12:54:48 +0200 | [diff] [blame] | 268 | /* |
| 269 | * imx51 and later SoCs have a slightly different IP that allows the |
| 270 | * SSI configuration while the SSI unit is running. |
| 271 | * |
| 272 | * More important, it is necessary on those SoCs to configure the |
| 273 | * sperate TX/RX DMA bits just before starting the stream |
| 274 | * (fsl_ssi_trigger). The SDMA unit has to be configured before fsl_ssi |
| 275 | * sends any DMA requests to the SDMA unit, otherwise it is not defined |
| 276 | * how the SDMA unit handles the DMA request. |
| 277 | * |
| 278 | * SDMA units are present on devices starting at imx35 but the imx35 |
| 279 | * reference manual states that the DMA bits should not be changed |
| 280 | * while the SSI unit is running (SSIEN). So we support the necessary |
| 281 | * online configuration of fsl-ssi starting at imx51. |
| 282 | */ |
Markus Pargmann | 171d683 | 2014-04-28 12:54:48 +0200 | [diff] [blame] | 283 | |
Sascha Hauer | fcdbade | 2014-05-27 10:24:18 +0200 | [diff] [blame] | 284 | static struct fsl_ssi_soc_data fsl_ssi_mpc8610 = { |
| 285 | .imx = false, |
| 286 | .offline_config = true, |
| 287 | .sisr_write_mask = CCSR_SSI_SISR_RFRC | CCSR_SSI_SISR_TFRC | |
| 288 | CCSR_SSI_SISR_ROE0 | CCSR_SSI_SISR_ROE1 | |
| 289 | CCSR_SSI_SISR_TUE0 | CCSR_SSI_SISR_TUE1, |
| 290 | }; |
| 291 | |
| 292 | static struct fsl_ssi_soc_data fsl_ssi_imx21 = { |
| 293 | .imx = true, |
Maciej S. Szmigiero | 6139b1b | 2016-01-18 20:07:44 +0100 | [diff] [blame] | 294 | .imx21regs = true, |
Sascha Hauer | fcdbade | 2014-05-27 10:24:18 +0200 | [diff] [blame] | 295 | .offline_config = true, |
| 296 | .sisr_write_mask = 0, |
| 297 | }; |
| 298 | |
| 299 | static struct fsl_ssi_soc_data fsl_ssi_imx35 = { |
| 300 | .imx = true, |
| 301 | .offline_config = true, |
| 302 | .sisr_write_mask = CCSR_SSI_SISR_RFRC | CCSR_SSI_SISR_TFRC | |
| 303 | CCSR_SSI_SISR_ROE0 | CCSR_SSI_SISR_ROE1 | |
| 304 | CCSR_SSI_SISR_TUE0 | CCSR_SSI_SISR_TUE1, |
| 305 | }; |
| 306 | |
| 307 | static struct fsl_ssi_soc_data fsl_ssi_imx51 = { |
| 308 | .imx = true, |
| 309 | .offline_config = false, |
| 310 | .sisr_write_mask = CCSR_SSI_SISR_ROE0 | CCSR_SSI_SISR_ROE1 | |
| 311 | CCSR_SSI_SISR_TUE0 | CCSR_SSI_SISR_TUE1, |
| 312 | }; |
| 313 | |
| 314 | static const struct of_device_id fsl_ssi_ids[] = { |
| 315 | { .compatible = "fsl,mpc8610-ssi", .data = &fsl_ssi_mpc8610 }, |
| 316 | { .compatible = "fsl,imx51-ssi", .data = &fsl_ssi_imx51 }, |
| 317 | { .compatible = "fsl,imx35-ssi", .data = &fsl_ssi_imx35 }, |
| 318 | { .compatible = "fsl,imx21-ssi", .data = &fsl_ssi_imx21 }, |
| 319 | {} |
| 320 | }; |
| 321 | MODULE_DEVICE_TABLE(of, fsl_ssi_ids); |
| 322 | |
| 323 | static bool fsl_ssi_is_ac97(struct fsl_ssi_private *ssi_private) |
| 324 | { |
Adam Thomson | 5b64c17 | 2015-09-16 10:13:19 +0100 | [diff] [blame] | 325 | return (ssi_private->dai_fmt & SND_SOC_DAIFMT_FORMAT_MASK) == |
| 326 | SND_SOC_DAIFMT_AC97; |
Markus Pargmann | 171d683 | 2014-04-28 12:54:48 +0200 | [diff] [blame] | 327 | } |
| 328 | |
Sascha Hauer | 8dd51e2 | 2014-05-27 10:24:20 +0200 | [diff] [blame] | 329 | static bool fsl_ssi_is_i2s_master(struct fsl_ssi_private *ssi_private) |
| 330 | { |
| 331 | return (ssi_private->dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) == |
| 332 | SND_SOC_DAIFMT_CBS_CFS; |
| 333 | } |
| 334 | |
Fabio Falzoi | cf4f7fc | 2014-08-04 17:08:07 +0200 | [diff] [blame] | 335 | static bool fsl_ssi_is_i2s_cbm_cfs(struct fsl_ssi_private *ssi_private) |
| 336 | { |
| 337 | return (ssi_private->dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) == |
| 338 | SND_SOC_DAIFMT_CBM_CFS; |
| 339 | } |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 340 | /** |
| 341 | * fsl_ssi_isr: SSI interrupt handler |
| 342 | * |
| 343 | * Although it's possible to use the interrupt handler to send and receive |
| 344 | * data to/from the SSI, we use the DMA instead. Programming is more |
| 345 | * complicated, but the performance is much better. |
| 346 | * |
| 347 | * This interrupt handler is used only to gather statistics. |
| 348 | * |
| 349 | * @irq: IRQ of the SSI device |
| 350 | * @dev_id: pointer to the ssi_private structure for this SSI device |
| 351 | */ |
| 352 | static irqreturn_t fsl_ssi_isr(int irq, void *dev_id) |
| 353 | { |
| 354 | struct fsl_ssi_private *ssi_private = dev_id; |
Markus Pargmann | 4324812 | 2014-05-27 10:24:25 +0200 | [diff] [blame] | 355 | struct regmap *regs = ssi_private->regs; |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 356 | __be32 sisr; |
Markus Pargmann | 0888efd | 2013-12-20 14:11:31 +0100 | [diff] [blame] | 357 | __be32 sisr2; |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 358 | |
| 359 | /* We got an interrupt, so read the status register to see what we |
| 360 | were interrupted for. We mask it with the Interrupt Enable register |
| 361 | so that we only check for events that we're interested in. |
| 362 | */ |
Markus Pargmann | 4324812 | 2014-05-27 10:24:25 +0200 | [diff] [blame] | 363 | regmap_read(regs, CCSR_SSI_SISR, &sisr); |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 364 | |
Sascha Hauer | fcdbade | 2014-05-27 10:24:18 +0200 | [diff] [blame] | 365 | sisr2 = sisr & ssi_private->soc->sisr_write_mask; |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 366 | /* Clear the bits that we set */ |
| 367 | if (sisr2) |
Markus Pargmann | 4324812 | 2014-05-27 10:24:25 +0200 | [diff] [blame] | 368 | regmap_write(regs, CCSR_SSI_SISR, sisr2); |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 369 | |
Markus Pargmann | f138e62 | 2014-04-28 12:54:43 +0200 | [diff] [blame] | 370 | fsl_ssi_dbg_isr(&ssi_private->dbg_stats, sisr); |
| 371 | |
| 372 | return IRQ_HANDLED; |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 373 | } |
| 374 | |
Markus Pargmann | 4e6ec0d | 2013-12-20 14:11:33 +0100 | [diff] [blame] | 375 | /* |
| 376 | * Enable/Disable all rx/tx config flags at once. |
| 377 | */ |
| 378 | static void fsl_ssi_rxtx_config(struct fsl_ssi_private *ssi_private, |
| 379 | bool enable) |
| 380 | { |
Markus Pargmann | 4324812 | 2014-05-27 10:24:25 +0200 | [diff] [blame] | 381 | struct regmap *regs = ssi_private->regs; |
Markus Pargmann | 4e6ec0d | 2013-12-20 14:11:33 +0100 | [diff] [blame] | 382 | struct fsl_ssi_rxtx_reg_val *vals = &ssi_private->rxtx_reg_val; |
| 383 | |
| 384 | if (enable) { |
Markus Pargmann | 4324812 | 2014-05-27 10:24:25 +0200 | [diff] [blame] | 385 | regmap_update_bits(regs, CCSR_SSI_SIER, |
| 386 | vals->rx.sier | vals->tx.sier, |
| 387 | vals->rx.sier | vals->tx.sier); |
| 388 | regmap_update_bits(regs, CCSR_SSI_SRCR, |
| 389 | vals->rx.srcr | vals->tx.srcr, |
| 390 | vals->rx.srcr | vals->tx.srcr); |
| 391 | regmap_update_bits(regs, CCSR_SSI_STCR, |
| 392 | vals->rx.stcr | vals->tx.stcr, |
| 393 | vals->rx.stcr | vals->tx.stcr); |
Markus Pargmann | 4e6ec0d | 2013-12-20 14:11:33 +0100 | [diff] [blame] | 394 | } else { |
Markus Pargmann | 4324812 | 2014-05-27 10:24:25 +0200 | [diff] [blame] | 395 | regmap_update_bits(regs, CCSR_SSI_SRCR, |
| 396 | vals->rx.srcr | vals->tx.srcr, 0); |
| 397 | regmap_update_bits(regs, CCSR_SSI_STCR, |
| 398 | vals->rx.stcr | vals->tx.stcr, 0); |
| 399 | regmap_update_bits(regs, CCSR_SSI_SIER, |
| 400 | vals->rx.sier | vals->tx.sier, 0); |
Markus Pargmann | 4e6ec0d | 2013-12-20 14:11:33 +0100 | [diff] [blame] | 401 | } |
| 402 | } |
| 403 | |
| 404 | /* |
Arnaud Mouiche | 027db2e | 2016-05-03 14:14:00 +0200 | [diff] [blame] | 405 | * Clear RX or TX FIFO to remove samples from the previous |
| 406 | * stream session which may be still present in the FIFO and |
| 407 | * may introduce bad samples and/or channel slipping. |
| 408 | * |
| 409 | * Note: The SOR is not documented in recent IMX datasheet, but |
| 410 | * is described in IMX51 reference manual at section 56.3.3.15. |
| 411 | */ |
| 412 | static void fsl_ssi_fifo_clear(struct fsl_ssi_private *ssi_private, |
| 413 | bool is_rx) |
| 414 | { |
| 415 | if (is_rx) { |
| 416 | regmap_update_bits(ssi_private->regs, CCSR_SSI_SOR, |
| 417 | CCSR_SSI_SOR_RX_CLR, CCSR_SSI_SOR_RX_CLR); |
| 418 | } else { |
| 419 | regmap_update_bits(ssi_private->regs, CCSR_SSI_SOR, |
| 420 | CCSR_SSI_SOR_TX_CLR, CCSR_SSI_SOR_TX_CLR); |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | /* |
Markus Pargmann | 65c961c | 2014-04-28 12:54:42 +0200 | [diff] [blame] | 425 | * Calculate the bits that have to be disabled for the current stream that is |
| 426 | * getting disabled. This keeps the bits enabled that are necessary for the |
| 427 | * second stream to work if 'stream_active' is true. |
| 428 | * |
| 429 | * Detailed calculation: |
| 430 | * These are the values that need to be active after disabling. For non-active |
| 431 | * second stream, this is 0: |
| 432 | * vals_stream * !!stream_active |
| 433 | * |
| 434 | * The following computes the overall differences between the setup for the |
| 435 | * to-disable stream and the active stream, a simple XOR: |
| 436 | * vals_disable ^ (vals_stream * !!(stream_active)) |
| 437 | * |
| 438 | * The full expression adds a mask on all values we care about |
| 439 | */ |
| 440 | #define fsl_ssi_disable_val(vals_disable, vals_stream, stream_active) \ |
| 441 | ((vals_disable) & \ |
| 442 | ((vals_disable) ^ ((vals_stream) * (u32)!!(stream_active)))) |
| 443 | |
| 444 | /* |
Markus Pargmann | 4e6ec0d | 2013-12-20 14:11:33 +0100 | [diff] [blame] | 445 | * Enable/Disable a ssi configuration. You have to pass either |
| 446 | * ssi_private->rxtx_reg_val.rx or tx as vals parameter. |
| 447 | */ |
| 448 | static void fsl_ssi_config(struct fsl_ssi_private *ssi_private, bool enable, |
| 449 | struct fsl_ssi_reg_val *vals) |
| 450 | { |
Markus Pargmann | 4324812 | 2014-05-27 10:24:25 +0200 | [diff] [blame] | 451 | struct regmap *regs = ssi_private->regs; |
Markus Pargmann | 4e6ec0d | 2013-12-20 14:11:33 +0100 | [diff] [blame] | 452 | struct fsl_ssi_reg_val *avals; |
Markus Pargmann | 4324812 | 2014-05-27 10:24:25 +0200 | [diff] [blame] | 453 | int nr_active_streams; |
| 454 | u32 scr_val; |
Markus Pargmann | 65c961c | 2014-04-28 12:54:42 +0200 | [diff] [blame] | 455 | int keep_active; |
| 456 | |
Markus Pargmann | 4324812 | 2014-05-27 10:24:25 +0200 | [diff] [blame] | 457 | regmap_read(regs, CCSR_SSI_SCR, &scr_val); |
| 458 | |
| 459 | nr_active_streams = !!(scr_val & CCSR_SSI_SCR_TE) + |
| 460 | !!(scr_val & CCSR_SSI_SCR_RE); |
| 461 | |
Markus Pargmann | 65c961c | 2014-04-28 12:54:42 +0200 | [diff] [blame] | 462 | if (nr_active_streams - 1 > 0) |
| 463 | keep_active = 1; |
| 464 | else |
| 465 | keep_active = 0; |
Markus Pargmann | 4e6ec0d | 2013-12-20 14:11:33 +0100 | [diff] [blame] | 466 | |
| 467 | /* Find the other direction values rx or tx which we do not want to |
| 468 | * modify */ |
| 469 | if (&ssi_private->rxtx_reg_val.rx == vals) |
| 470 | avals = &ssi_private->rxtx_reg_val.tx; |
| 471 | else |
| 472 | avals = &ssi_private->rxtx_reg_val.rx; |
| 473 | |
| 474 | /* If vals should be disabled, start with disabling the unit */ |
| 475 | if (!enable) { |
Markus Pargmann | 65c961c | 2014-04-28 12:54:42 +0200 | [diff] [blame] | 476 | u32 scr = fsl_ssi_disable_val(vals->scr, avals->scr, |
| 477 | keep_active); |
Markus Pargmann | 4324812 | 2014-05-27 10:24:25 +0200 | [diff] [blame] | 478 | regmap_update_bits(regs, CCSR_SSI_SCR, scr, 0); |
Markus Pargmann | 4e6ec0d | 2013-12-20 14:11:33 +0100 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | /* |
| 482 | * We are running on a SoC which does not support online SSI |
| 483 | * reconfiguration, so we have to enable all necessary flags at once |
| 484 | * even if we do not use them later (capture and playback configuration) |
| 485 | */ |
Sascha Hauer | fcdbade | 2014-05-27 10:24:18 +0200 | [diff] [blame] | 486 | if (ssi_private->soc->offline_config) { |
Markus Pargmann | 4e6ec0d | 2013-12-20 14:11:33 +0100 | [diff] [blame] | 487 | if ((enable && !nr_active_streams) || |
Markus Pargmann | 65c961c | 2014-04-28 12:54:42 +0200 | [diff] [blame] | 488 | (!enable && !keep_active)) |
Markus Pargmann | 4e6ec0d | 2013-12-20 14:11:33 +0100 | [diff] [blame] | 489 | fsl_ssi_rxtx_config(ssi_private, enable); |
| 490 | |
| 491 | goto config_done; |
| 492 | } |
| 493 | |
| 494 | /* |
| 495 | * Configure single direction units while the SSI unit is running |
| 496 | * (online configuration) |
| 497 | */ |
| 498 | if (enable) { |
Arnaud Mouiche | 027db2e | 2016-05-03 14:14:00 +0200 | [diff] [blame] | 499 | fsl_ssi_fifo_clear(ssi_private, vals->scr & CCSR_SSI_SCR_RE); |
| 500 | |
Markus Pargmann | 4324812 | 2014-05-27 10:24:25 +0200 | [diff] [blame] | 501 | regmap_update_bits(regs, CCSR_SSI_SRCR, vals->srcr, vals->srcr); |
| 502 | regmap_update_bits(regs, CCSR_SSI_STCR, vals->stcr, vals->stcr); |
Arnaud Mouiche | d9f2a20 | 2016-05-03 14:13:58 +0200 | [diff] [blame] | 503 | regmap_update_bits(regs, CCSR_SSI_SIER, vals->sier, vals->sier); |
Markus Pargmann | 4e6ec0d | 2013-12-20 14:11:33 +0100 | [diff] [blame] | 504 | } else { |
| 505 | u32 sier; |
| 506 | u32 srcr; |
| 507 | u32 stcr; |
| 508 | |
| 509 | /* |
| 510 | * Disabling the necessary flags for one of rx/tx while the |
| 511 | * other stream is active is a little bit more difficult. We |
| 512 | * have to disable only those flags that differ between both |
| 513 | * streams (rx XOR tx) and that are set in the stream that is |
| 514 | * disabled now. Otherwise we could alter flags of the other |
| 515 | * stream |
| 516 | */ |
| 517 | |
| 518 | /* These assignments are simply vals without bits set in avals*/ |
Markus Pargmann | 65c961c | 2014-04-28 12:54:42 +0200 | [diff] [blame] | 519 | sier = fsl_ssi_disable_val(vals->sier, avals->sier, |
| 520 | keep_active); |
| 521 | srcr = fsl_ssi_disable_val(vals->srcr, avals->srcr, |
| 522 | keep_active); |
| 523 | stcr = fsl_ssi_disable_val(vals->stcr, avals->stcr, |
| 524 | keep_active); |
Markus Pargmann | 4e6ec0d | 2013-12-20 14:11:33 +0100 | [diff] [blame] | 525 | |
Markus Pargmann | 4324812 | 2014-05-27 10:24:25 +0200 | [diff] [blame] | 526 | regmap_update_bits(regs, CCSR_SSI_SRCR, srcr, 0); |
| 527 | regmap_update_bits(regs, CCSR_SSI_STCR, stcr, 0); |
| 528 | regmap_update_bits(regs, CCSR_SSI_SIER, sier, 0); |
Markus Pargmann | 4e6ec0d | 2013-12-20 14:11:33 +0100 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | config_done: |
| 532 | /* Enabling of subunits is done after configuration */ |
Arnaud Mouiche | 61fcf10 | 2016-05-03 14:13:59 +0200 | [diff] [blame] | 533 | if (enable) { |
| 534 | if (ssi_private->use_dma && (vals->scr & CCSR_SSI_SCR_TE)) { |
| 535 | /* |
| 536 | * Be sure the Tx FIFO is filled when TE is set. |
| 537 | * Otherwise, there are some chances to start the |
| 538 | * playback with some void samples inserted first, |
| 539 | * generating a channel slip. |
| 540 | * |
| 541 | * First, SSIEN must be set, to let the FIFO be filled. |
| 542 | * |
| 543 | * Notes: |
| 544 | * - Limit this fix to the DMA case until FIQ cases can |
| 545 | * be tested. |
| 546 | * - Limit the length of the busy loop to not lock the |
| 547 | * system too long, even if 1-2 loops are sufficient |
| 548 | * in general. |
| 549 | */ |
| 550 | int i; |
| 551 | int max_loop = 100; |
| 552 | regmap_update_bits(regs, CCSR_SSI_SCR, |
| 553 | CCSR_SSI_SCR_SSIEN, CCSR_SSI_SCR_SSIEN); |
| 554 | for (i = 0; i < max_loop; i++) { |
| 555 | u32 sfcsr; |
| 556 | regmap_read(regs, CCSR_SSI_SFCSR, &sfcsr); |
| 557 | if (CCSR_SSI_SFCSR_TFCNT0(sfcsr)) |
| 558 | break; |
| 559 | } |
| 560 | if (i == max_loop) { |
| 561 | dev_err(ssi_private->dev, |
| 562 | "Timeout waiting TX FIFO filling\n"); |
| 563 | } |
| 564 | } |
Markus Pargmann | 4324812 | 2014-05-27 10:24:25 +0200 | [diff] [blame] | 565 | regmap_update_bits(regs, CCSR_SSI_SCR, vals->scr, vals->scr); |
Arnaud Mouiche | 61fcf10 | 2016-05-03 14:13:59 +0200 | [diff] [blame] | 566 | } |
Markus Pargmann | 4e6ec0d | 2013-12-20 14:11:33 +0100 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | |
| 570 | static void fsl_ssi_rx_config(struct fsl_ssi_private *ssi_private, bool enable) |
| 571 | { |
| 572 | fsl_ssi_config(ssi_private, enable, &ssi_private->rxtx_reg_val.rx); |
| 573 | } |
| 574 | |
| 575 | static void fsl_ssi_tx_config(struct fsl_ssi_private *ssi_private, bool enable) |
| 576 | { |
| 577 | fsl_ssi_config(ssi_private, enable, &ssi_private->rxtx_reg_val.tx); |
| 578 | } |
| 579 | |
Markus Pargmann | 6de8387 | 2013-12-20 14:11:34 +0100 | [diff] [blame] | 580 | /* |
| 581 | * Setup rx/tx register values used to enable/disable the streams. These will |
| 582 | * be used later in fsl_ssi_config to setup the streams without the need to |
| 583 | * check for all different SSI modes. |
| 584 | */ |
| 585 | static void fsl_ssi_setup_reg_vals(struct fsl_ssi_private *ssi_private) |
| 586 | { |
| 587 | struct fsl_ssi_rxtx_reg_val *reg = &ssi_private->rxtx_reg_val; |
| 588 | |
| 589 | reg->rx.sier = CCSR_SSI_SIER_RFF0_EN; |
| 590 | reg->rx.srcr = CCSR_SSI_SRCR_RFEN0; |
| 591 | reg->rx.scr = 0; |
| 592 | reg->tx.sier = CCSR_SSI_SIER_TFE0_EN; |
| 593 | reg->tx.stcr = CCSR_SSI_STCR_TFEN0; |
| 594 | reg->tx.scr = 0; |
| 595 | |
Markus Pargmann | 171d683 | 2014-04-28 12:54:48 +0200 | [diff] [blame] | 596 | if (!fsl_ssi_is_ac97(ssi_private)) { |
Markus Pargmann | 6de8387 | 2013-12-20 14:11:34 +0100 | [diff] [blame] | 597 | reg->rx.scr = CCSR_SSI_SCR_SSIEN | CCSR_SSI_SCR_RE; |
| 598 | reg->rx.sier |= CCSR_SSI_SIER_RFF0_EN; |
| 599 | reg->tx.scr = CCSR_SSI_SCR_SSIEN | CCSR_SSI_SCR_TE; |
| 600 | reg->tx.sier |= CCSR_SSI_SIER_TFE0_EN; |
| 601 | } |
| 602 | |
| 603 | if (ssi_private->use_dma) { |
| 604 | reg->rx.sier |= CCSR_SSI_SIER_RDMAE; |
| 605 | reg->tx.sier |= CCSR_SSI_SIER_TDMAE; |
| 606 | } else { |
| 607 | reg->rx.sier |= CCSR_SSI_SIER_RIE; |
| 608 | reg->tx.sier |= CCSR_SSI_SIER_TIE; |
| 609 | } |
| 610 | |
| 611 | reg->rx.sier |= FSLSSI_SIER_DBG_RX_FLAGS; |
| 612 | reg->tx.sier |= FSLSSI_SIER_DBG_TX_FLAGS; |
| 613 | } |
| 614 | |
Markus Pargmann | d876464 | 2013-11-20 10:04:15 +0100 | [diff] [blame] | 615 | static void fsl_ssi_setup_ac97(struct fsl_ssi_private *ssi_private) |
| 616 | { |
Markus Pargmann | 4324812 | 2014-05-27 10:24:25 +0200 | [diff] [blame] | 617 | struct regmap *regs = ssi_private->regs; |
Markus Pargmann | d876464 | 2013-11-20 10:04:15 +0100 | [diff] [blame] | 618 | |
| 619 | /* |
| 620 | * Setup the clock control register |
| 621 | */ |
Markus Pargmann | 4324812 | 2014-05-27 10:24:25 +0200 | [diff] [blame] | 622 | regmap_write(regs, CCSR_SSI_STCCR, |
| 623 | CCSR_SSI_SxCCR_WL(17) | CCSR_SSI_SxCCR_DC(13)); |
| 624 | regmap_write(regs, CCSR_SSI_SRCCR, |
| 625 | CCSR_SSI_SxCCR_WL(17) | CCSR_SSI_SxCCR_DC(13)); |
Markus Pargmann | d876464 | 2013-11-20 10:04:15 +0100 | [diff] [blame] | 626 | |
| 627 | /* |
| 628 | * Enable AC97 mode and startup the SSI |
| 629 | */ |
Markus Pargmann | 4324812 | 2014-05-27 10:24:25 +0200 | [diff] [blame] | 630 | regmap_write(regs, CCSR_SSI_SACNT, |
| 631 | CCSR_SSI_SACNT_AC97EN | CCSR_SSI_SACNT_FV); |
Maciej S. Szmigiero | 6139b1b | 2016-01-18 20:07:44 +0100 | [diff] [blame] | 632 | |
| 633 | /* no SACC{ST,EN,DIS} regs on imx21-class SSI */ |
| 634 | if (!ssi_private->soc->imx21regs) { |
| 635 | regmap_write(regs, CCSR_SSI_SACCDIS, 0xff); |
| 636 | regmap_write(regs, CCSR_SSI_SACCEN, 0x300); |
| 637 | } |
Markus Pargmann | d876464 | 2013-11-20 10:04:15 +0100 | [diff] [blame] | 638 | |
| 639 | /* |
| 640 | * Enable SSI, Transmit and Receive. AC97 has to communicate with the |
| 641 | * codec before a stream is started. |
| 642 | */ |
Markus Pargmann | 4324812 | 2014-05-27 10:24:25 +0200 | [diff] [blame] | 643 | regmap_update_bits(regs, CCSR_SSI_SCR, |
| 644 | CCSR_SSI_SCR_SSIEN | CCSR_SSI_SCR_TE | CCSR_SSI_SCR_RE, |
| 645 | CCSR_SSI_SCR_SSIEN | CCSR_SSI_SCR_TE | CCSR_SSI_SCR_RE); |
Markus Pargmann | d876464 | 2013-11-20 10:04:15 +0100 | [diff] [blame] | 646 | |
Markus Pargmann | 4324812 | 2014-05-27 10:24:25 +0200 | [diff] [blame] | 647 | regmap_write(regs, CCSR_SSI_SOR, CCSR_SSI_SOR_WAIT(3)); |
Markus Pargmann | d876464 | 2013-11-20 10:04:15 +0100 | [diff] [blame] | 648 | } |
| 649 | |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 650 | /** |
| 651 | * fsl_ssi_startup: create a new substream |
| 652 | * |
| 653 | * This is the first function called when a stream is opened. |
| 654 | * |
| 655 | * If this is the first stream open, then grab the IRQ and program most of |
| 656 | * the SSI registers. |
| 657 | */ |
Mark Brown | dee89c4 | 2008-11-18 22:11:38 +0000 | [diff] [blame] | 658 | static int fsl_ssi_startup(struct snd_pcm_substream *substream, |
| 659 | struct snd_soc_dai *dai) |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 660 | { |
| 661 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
Timur Tabi | 5e538ec | 2011-09-13 12:59:37 -0500 | [diff] [blame] | 662 | struct fsl_ssi_private *ssi_private = |
| 663 | snd_soc_dai_get_drvdata(rtd->cpu_dai); |
Shengjiu Wang | f4a43ca | 2014-09-16 10:13:16 +0800 | [diff] [blame] | 664 | int ret; |
| 665 | |
| 666 | ret = clk_prepare_enable(ssi_private->clk); |
| 667 | if (ret) |
| 668 | return ret; |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 669 | |
Nicolin Chen | 0da9e55 | 2013-11-13 22:55:26 +0800 | [diff] [blame] | 670 | /* When using dual fifo mode, it is safer to ensure an even period |
| 671 | * size. If appearing to an odd number while DMA always starts its |
| 672 | * task from fifo0, fifo1 would be neglected at the end of each |
| 673 | * period. But SSI would still access fifo1 with an invalid data. |
| 674 | */ |
| 675 | if (ssi_private->use_dual_fifo) |
| 676 | snd_pcm_hw_constraint_step(substream->runtime, 0, |
| 677 | SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 2); |
| 678 | |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 679 | return 0; |
| 680 | } |
| 681 | |
| 682 | /** |
Shengjiu Wang | f4a43ca | 2014-09-16 10:13:16 +0800 | [diff] [blame] | 683 | * fsl_ssi_shutdown: shutdown the SSI |
| 684 | * |
| 685 | */ |
| 686 | static void fsl_ssi_shutdown(struct snd_pcm_substream *substream, |
| 687 | struct snd_soc_dai *dai) |
| 688 | { |
| 689 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 690 | struct fsl_ssi_private *ssi_private = |
| 691 | snd_soc_dai_get_drvdata(rtd->cpu_dai); |
| 692 | |
| 693 | clk_disable_unprepare(ssi_private->clk); |
| 694 | |
| 695 | } |
| 696 | |
| 697 | /** |
Sascha Hauer | 8dd51e2 | 2014-05-27 10:24:20 +0200 | [diff] [blame] | 698 | * fsl_ssi_set_bclk - configure Digital Audio Interface bit clock |
Sascha Hauer | ee9daad | 2014-04-28 12:54:52 +0200 | [diff] [blame] | 699 | * |
| 700 | * Note: This function can be only called when using SSI as DAI master |
| 701 | * |
| 702 | * Quick instruction for parameters: |
| 703 | * freq: Output BCLK frequency = samplerate * 32 (fixed) * channels |
| 704 | * dir: SND_SOC_CLOCK_OUT -> TxBCLK, SND_SOC_CLOCK_IN -> RxBCLK. |
| 705 | */ |
Sascha Hauer | 8dd51e2 | 2014-05-27 10:24:20 +0200 | [diff] [blame] | 706 | static int fsl_ssi_set_bclk(struct snd_pcm_substream *substream, |
| 707 | struct snd_soc_dai *cpu_dai, |
| 708 | struct snd_pcm_hw_params *hw_params) |
Sascha Hauer | ee9daad | 2014-04-28 12:54:52 +0200 | [diff] [blame] | 709 | { |
| 710 | struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(cpu_dai); |
Markus Pargmann | 4324812 | 2014-05-27 10:24:25 +0200 | [diff] [blame] | 711 | struct regmap *regs = ssi_private->regs; |
Sascha Hauer | ee9daad | 2014-04-28 12:54:52 +0200 | [diff] [blame] | 712 | int synchronous = ssi_private->cpu_dai_drv.symmetric_rates, ret; |
| 713 | u32 pm = 999, div2, psr, stccr, mask, afreq, factor, i; |
Sascha Hauer | d8ced47 | 2014-05-27 10:24:21 +0200 | [diff] [blame] | 714 | unsigned long clkrate, baudrate, tmprate; |
Sascha Hauer | ee9daad | 2014-04-28 12:54:52 +0200 | [diff] [blame] | 715 | u64 sub, savesub = 100000; |
Sascha Hauer | 8dd51e2 | 2014-05-27 10:24:20 +0200 | [diff] [blame] | 716 | unsigned int freq; |
Markus Pargmann | d429d8e | 2014-05-27 10:24:23 +0200 | [diff] [blame] | 717 | bool baudclk_is_used; |
Sascha Hauer | 8dd51e2 | 2014-05-27 10:24:20 +0200 | [diff] [blame] | 718 | |
| 719 | /* Prefer the explicitly set bitclock frequency */ |
| 720 | if (ssi_private->bitclk_freq) |
| 721 | freq = ssi_private->bitclk_freq; |
| 722 | else |
| 723 | freq = params_channels(hw_params) * 32 * params_rate(hw_params); |
Sascha Hauer | ee9daad | 2014-04-28 12:54:52 +0200 | [diff] [blame] | 724 | |
| 725 | /* Don't apply it to any non-baudclk circumstance */ |
| 726 | if (IS_ERR(ssi_private->baudclk)) |
| 727 | return -EINVAL; |
| 728 | |
Arnaud Mouiche | e09745f | 2016-05-03 14:13:56 +0200 | [diff] [blame] | 729 | /* |
| 730 | * Hardware limitation: The bclk rate must be |
| 731 | * never greater than 1/5 IPG clock rate |
| 732 | */ |
| 733 | if (freq * 5 > clk_get_rate(ssi_private->clk)) { |
| 734 | dev_err(cpu_dai->dev, "bitclk > ipgclk/5\n"); |
| 735 | return -EINVAL; |
| 736 | } |
| 737 | |
Markus Pargmann | d429d8e | 2014-05-27 10:24:23 +0200 | [diff] [blame] | 738 | baudclk_is_used = ssi_private->baudclk_streams & ~(BIT(substream->stream)); |
| 739 | |
Sascha Hauer | ee9daad | 2014-04-28 12:54:52 +0200 | [diff] [blame] | 740 | /* It should be already enough to divide clock by setting pm alone */ |
| 741 | psr = 0; |
| 742 | div2 = 0; |
| 743 | |
| 744 | factor = (div2 + 1) * (7 * psr + 1) * 2; |
| 745 | |
| 746 | for (i = 0; i < 255; i++) { |
Nicolin Chen | 6c8ca30 | 2015-03-04 21:05:04 -0800 | [diff] [blame] | 747 | tmprate = freq * factor * (i + 1); |
Markus Pargmann | d429d8e | 2014-05-27 10:24:23 +0200 | [diff] [blame] | 748 | |
| 749 | if (baudclk_is_used) |
| 750 | clkrate = clk_get_rate(ssi_private->baudclk); |
| 751 | else |
| 752 | clkrate = clk_round_rate(ssi_private->baudclk, tmprate); |
Sascha Hauer | ee9daad | 2014-04-28 12:54:52 +0200 | [diff] [blame] | 753 | |
Timur Tabi | acf2c60 | 2014-06-13 07:42:40 -0500 | [diff] [blame] | 754 | clkrate /= factor; |
| 755 | afreq = clkrate / (i + 1); |
Sascha Hauer | ee9daad | 2014-04-28 12:54:52 +0200 | [diff] [blame] | 756 | |
| 757 | if (freq == afreq) |
| 758 | sub = 0; |
| 759 | else if (freq / afreq == 1) |
| 760 | sub = freq - afreq; |
| 761 | else if (afreq / freq == 1) |
| 762 | sub = afreq - freq; |
| 763 | else |
| 764 | continue; |
| 765 | |
| 766 | /* Calculate the fraction */ |
| 767 | sub *= 100000; |
| 768 | do_div(sub, freq); |
| 769 | |
Juergen Borleis | ebac95a | 2015-07-03 12:39:36 +0200 | [diff] [blame] | 770 | if (sub < savesub && !(i == 0 && psr == 0 && div2 == 0)) { |
Sascha Hauer | ee9daad | 2014-04-28 12:54:52 +0200 | [diff] [blame] | 771 | baudrate = tmprate; |
| 772 | savesub = sub; |
| 773 | pm = i; |
| 774 | } |
| 775 | |
| 776 | /* We are lucky */ |
| 777 | if (savesub == 0) |
| 778 | break; |
| 779 | } |
| 780 | |
| 781 | /* No proper pm found if it is still remaining the initial value */ |
| 782 | if (pm == 999) { |
| 783 | dev_err(cpu_dai->dev, "failed to handle the required sysclk\n"); |
| 784 | return -EINVAL; |
| 785 | } |
| 786 | |
| 787 | stccr = CCSR_SSI_SxCCR_PM(pm + 1) | (div2 ? CCSR_SSI_SxCCR_DIV2 : 0) | |
| 788 | (psr ? CCSR_SSI_SxCCR_PSR : 0); |
| 789 | mask = CCSR_SSI_SxCCR_PM_MASK | CCSR_SSI_SxCCR_DIV2 | |
| 790 | CCSR_SSI_SxCCR_PSR; |
| 791 | |
Sascha Hauer | 8dd51e2 | 2014-05-27 10:24:20 +0200 | [diff] [blame] | 792 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK || synchronous) |
Markus Pargmann | 4324812 | 2014-05-27 10:24:25 +0200 | [diff] [blame] | 793 | regmap_update_bits(regs, CCSR_SSI_STCCR, mask, stccr); |
Sascha Hauer | ee9daad | 2014-04-28 12:54:52 +0200 | [diff] [blame] | 794 | else |
Markus Pargmann | 4324812 | 2014-05-27 10:24:25 +0200 | [diff] [blame] | 795 | regmap_update_bits(regs, CCSR_SSI_SRCCR, mask, stccr); |
Sascha Hauer | ee9daad | 2014-04-28 12:54:52 +0200 | [diff] [blame] | 796 | |
Markus Pargmann | d429d8e | 2014-05-27 10:24:23 +0200 | [diff] [blame] | 797 | if (!baudclk_is_used) { |
Sascha Hauer | ee9daad | 2014-04-28 12:54:52 +0200 | [diff] [blame] | 798 | ret = clk_set_rate(ssi_private->baudclk, baudrate); |
| 799 | if (ret) { |
Sascha Hauer | ee9daad | 2014-04-28 12:54:52 +0200 | [diff] [blame] | 800 | dev_err(cpu_dai->dev, "failed to set baudclk rate\n"); |
| 801 | return -EINVAL; |
| 802 | } |
Sascha Hauer | ee9daad | 2014-04-28 12:54:52 +0200 | [diff] [blame] | 803 | } |
Sascha Hauer | ee9daad | 2014-04-28 12:54:52 +0200 | [diff] [blame] | 804 | |
| 805 | return 0; |
| 806 | } |
| 807 | |
Sascha Hauer | 8dd51e2 | 2014-05-27 10:24:20 +0200 | [diff] [blame] | 808 | static int fsl_ssi_set_dai_sysclk(struct snd_soc_dai *cpu_dai, |
| 809 | int clk_id, unsigned int freq, int dir) |
| 810 | { |
| 811 | struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(cpu_dai); |
| 812 | |
| 813 | ssi_private->bitclk_freq = freq; |
| 814 | |
| 815 | return 0; |
| 816 | } |
| 817 | |
Sascha Hauer | ee9daad | 2014-04-28 12:54:52 +0200 | [diff] [blame] | 818 | /** |
Timur Tabi | 85ef237 | 2009-02-05 17:56:02 -0600 | [diff] [blame] | 819 | * fsl_ssi_hw_params - program the sample size |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 820 | * |
| 821 | * Most of the SSI registers have been programmed in the startup function, |
| 822 | * but the word length must be programmed here. Unfortunately, programming |
| 823 | * the SxCCR.WL bits requires the SSI to be temporarily disabled. This can |
| 824 | * cause a problem with supporting simultaneous playback and capture. If |
| 825 | * the SSI is already playing a stream, then that stream may be temporarily |
| 826 | * stopped when you start capture. |
| 827 | * |
| 828 | * Note: The SxCCR.DC and SxCCR.PM bits are only used if the SSI is the |
| 829 | * clock master. |
| 830 | */ |
Timur Tabi | 85ef237 | 2009-02-05 17:56:02 -0600 | [diff] [blame] | 831 | static int fsl_ssi_hw_params(struct snd_pcm_substream *substream, |
| 832 | struct snd_pcm_hw_params *hw_params, struct snd_soc_dai *cpu_dai) |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 833 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 834 | struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(cpu_dai); |
Markus Pargmann | 4324812 | 2014-05-27 10:24:25 +0200 | [diff] [blame] | 835 | struct regmap *regs = ssi_private->regs; |
Nicolin Chen | 2924a99 | 2013-12-02 23:29:03 +0800 | [diff] [blame] | 836 | unsigned int channels = params_channels(hw_params); |
Zidan Wang | 4ca7304 | 2015-11-24 15:32:09 +0800 | [diff] [blame] | 837 | unsigned int sample_size = params_width(hw_params); |
Timur Tabi | 5e538ec | 2011-09-13 12:59:37 -0500 | [diff] [blame] | 838 | u32 wl = CCSR_SSI_SxCCR_WL(sample_size); |
Sascha Hauer | 8dd51e2 | 2014-05-27 10:24:20 +0200 | [diff] [blame] | 839 | int ret; |
Markus Pargmann | 4324812 | 2014-05-27 10:24:25 +0200 | [diff] [blame] | 840 | u32 scr_val; |
| 841 | int enabled; |
| 842 | |
| 843 | regmap_read(regs, CCSR_SSI_SCR, &scr_val); |
| 844 | enabled = scr_val & CCSR_SSI_SCR_SSIEN; |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 845 | |
Timur Tabi | 5e538ec | 2011-09-13 12:59:37 -0500 | [diff] [blame] | 846 | /* |
| 847 | * If we're in synchronous mode, and the SSI is already enabled, |
| 848 | * then STCCR is already set properly. |
| 849 | */ |
| 850 | if (enabled && ssi_private->cpu_dai_drv.symmetric_rates) |
| 851 | return 0; |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 852 | |
Sascha Hauer | 8dd51e2 | 2014-05-27 10:24:20 +0200 | [diff] [blame] | 853 | if (fsl_ssi_is_i2s_master(ssi_private)) { |
| 854 | ret = fsl_ssi_set_bclk(substream, cpu_dai, hw_params); |
| 855 | if (ret) |
| 856 | return ret; |
Markus Pargmann | d429d8e | 2014-05-27 10:24:23 +0200 | [diff] [blame] | 857 | |
| 858 | /* Do not enable the clock if it is already enabled */ |
| 859 | if (!(ssi_private->baudclk_streams & BIT(substream->stream))) { |
| 860 | ret = clk_prepare_enable(ssi_private->baudclk); |
| 861 | if (ret) |
| 862 | return ret; |
| 863 | |
| 864 | ssi_private->baudclk_streams |= BIT(substream->stream); |
| 865 | } |
Sascha Hauer | 8dd51e2 | 2014-05-27 10:24:20 +0200 | [diff] [blame] | 866 | } |
| 867 | |
Fabio Falzoi | cf4f7fc | 2014-08-04 17:08:07 +0200 | [diff] [blame] | 868 | if (!fsl_ssi_is_ac97(ssi_private)) { |
| 869 | u8 i2smode; |
| 870 | /* |
| 871 | * Switch to normal net mode in order to have a frame sync |
| 872 | * signal every 32 bits instead of 16 bits |
| 873 | */ |
| 874 | if (fsl_ssi_is_i2s_cbm_cfs(ssi_private) && sample_size == 16) |
| 875 | i2smode = CCSR_SSI_SCR_I2S_MODE_NORMAL | |
| 876 | CCSR_SSI_SCR_NET; |
| 877 | else |
| 878 | i2smode = ssi_private->i2s_mode; |
| 879 | |
| 880 | regmap_update_bits(regs, CCSR_SSI_SCR, |
| 881 | CCSR_SSI_SCR_NET | CCSR_SSI_SCR_I2S_MODE_MASK, |
| 882 | channels == 1 ? 0 : i2smode); |
| 883 | } |
| 884 | |
Timur Tabi | 5e538ec | 2011-09-13 12:59:37 -0500 | [diff] [blame] | 885 | /* |
| 886 | * FIXME: The documentation says that SxCCR[WL] should not be |
| 887 | * modified while the SSI is enabled. The only time this can |
| 888 | * happen is if we're trying to do simultaneous playback and |
| 889 | * capture in asynchronous mode. Unfortunately, I have been enable |
| 890 | * to get that to work at all on the P1022DS. Therefore, we don't |
| 891 | * bother to disable/enable the SSI when setting SxCCR[WL], because |
| 892 | * the SSI will stop anyway. Maybe one day, this will get fixed. |
| 893 | */ |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 894 | |
Timur Tabi | 5e538ec | 2011-09-13 12:59:37 -0500 | [diff] [blame] | 895 | /* In synchronous mode, the SSI uses STCCR for capture */ |
| 896 | if ((substream->stream == SNDRV_PCM_STREAM_PLAYBACK) || |
| 897 | ssi_private->cpu_dai_drv.symmetric_rates) |
Markus Pargmann | 4324812 | 2014-05-27 10:24:25 +0200 | [diff] [blame] | 898 | regmap_update_bits(regs, CCSR_SSI_STCCR, CCSR_SSI_SxCCR_WL_MASK, |
| 899 | wl); |
Timur Tabi | 5e538ec | 2011-09-13 12:59:37 -0500 | [diff] [blame] | 900 | else |
Markus Pargmann | 4324812 | 2014-05-27 10:24:25 +0200 | [diff] [blame] | 901 | regmap_update_bits(regs, CCSR_SSI_SRCCR, CCSR_SSI_SxCCR_WL_MASK, |
| 902 | wl); |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 903 | |
| 904 | return 0; |
| 905 | } |
| 906 | |
Markus Pargmann | d429d8e | 2014-05-27 10:24:23 +0200 | [diff] [blame] | 907 | static int fsl_ssi_hw_free(struct snd_pcm_substream *substream, |
| 908 | struct snd_soc_dai *cpu_dai) |
| 909 | { |
| 910 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 911 | struct fsl_ssi_private *ssi_private = |
| 912 | snd_soc_dai_get_drvdata(rtd->cpu_dai); |
| 913 | |
| 914 | if (fsl_ssi_is_i2s_master(ssi_private) && |
| 915 | ssi_private->baudclk_streams & BIT(substream->stream)) { |
| 916 | clk_disable_unprepare(ssi_private->baudclk); |
| 917 | ssi_private->baudclk_streams &= ~BIT(substream->stream); |
| 918 | } |
| 919 | |
| 920 | return 0; |
| 921 | } |
| 922 | |
Michael Trimarchi | 8515146 | 2014-09-18 20:38:09 +0200 | [diff] [blame] | 923 | static int _fsl_ssi_set_dai_fmt(struct device *dev, |
| 924 | struct fsl_ssi_private *ssi_private, |
| 925 | unsigned int fmt) |
Nicolin Chen | aafa85e | 2013-12-12 18:44:45 +0800 | [diff] [blame] | 926 | { |
Markus Pargmann | 4324812 | 2014-05-27 10:24:25 +0200 | [diff] [blame] | 927 | struct regmap *regs = ssi_private->regs; |
Nicolin Chen | aafa85e | 2013-12-12 18:44:45 +0800 | [diff] [blame] | 928 | u32 strcr = 0, stcr, srcr, scr, mask; |
Markus Pargmann | 2b0db99 | 2014-03-15 13:44:09 +0100 | [diff] [blame] | 929 | u8 wm; |
| 930 | |
Markus Pargmann | 171d683 | 2014-04-28 12:54:48 +0200 | [diff] [blame] | 931 | ssi_private->dai_fmt = fmt; |
| 932 | |
Markus Pargmann | d429d8e | 2014-05-27 10:24:23 +0200 | [diff] [blame] | 933 | if (fsl_ssi_is_i2s_master(ssi_private) && IS_ERR(ssi_private->baudclk)) { |
Michael Trimarchi | 8515146 | 2014-09-18 20:38:09 +0200 | [diff] [blame] | 934 | dev_err(dev, "baudclk is missing which is necessary for master mode\n"); |
Markus Pargmann | d429d8e | 2014-05-27 10:24:23 +0200 | [diff] [blame] | 935 | return -EINVAL; |
| 936 | } |
| 937 | |
Markus Pargmann | 2b0db99 | 2014-03-15 13:44:09 +0100 | [diff] [blame] | 938 | fsl_ssi_setup_reg_vals(ssi_private); |
Nicolin Chen | aafa85e | 2013-12-12 18:44:45 +0800 | [diff] [blame] | 939 | |
Markus Pargmann | 4324812 | 2014-05-27 10:24:25 +0200 | [diff] [blame] | 940 | regmap_read(regs, CCSR_SSI_SCR, &scr); |
| 941 | scr &= ~(CCSR_SSI_SCR_SYN | CCSR_SSI_SCR_I2S_MODE_MASK); |
Markus Pargmann | 5048947 | 2014-04-28 12:54:51 +0200 | [diff] [blame] | 942 | scr |= CCSR_SSI_SCR_SYNC_TX_FS; |
Nicolin Chen | aafa85e | 2013-12-12 18:44:45 +0800 | [diff] [blame] | 943 | |
| 944 | mask = CCSR_SSI_STCR_TXBIT0 | CCSR_SSI_STCR_TFDIR | CCSR_SSI_STCR_TXDIR | |
| 945 | CCSR_SSI_STCR_TSCKP | CCSR_SSI_STCR_TFSI | CCSR_SSI_STCR_TFSL | |
| 946 | CCSR_SSI_STCR_TEFS; |
Markus Pargmann | 4324812 | 2014-05-27 10:24:25 +0200 | [diff] [blame] | 947 | regmap_read(regs, CCSR_SSI_STCR, &stcr); |
| 948 | regmap_read(regs, CCSR_SSI_SRCR, &srcr); |
| 949 | stcr &= ~mask; |
| 950 | srcr &= ~mask; |
Nicolin Chen | aafa85e | 2013-12-12 18:44:45 +0800 | [diff] [blame] | 951 | |
Markus Pargmann | 07a28db | 2014-03-15 13:44:10 +0100 | [diff] [blame] | 952 | ssi_private->i2s_mode = CCSR_SSI_SCR_NET; |
Nicolin Chen | aafa85e | 2013-12-12 18:44:45 +0800 | [diff] [blame] | 953 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { |
| 954 | case SND_SOC_DAIFMT_I2S: |
Alexander Shiyan | 4f14f5c | 2016-06-25 07:59:22 +0300 | [diff] [blame] | 955 | regmap_update_bits(regs, CCSR_SSI_STCCR, |
| 956 | CCSR_SSI_SxCCR_DC_MASK, |
| 957 | CCSR_SSI_SxCCR_DC(2)); |
| 958 | regmap_update_bits(regs, CCSR_SSI_SRCCR, |
| 959 | CCSR_SSI_SxCCR_DC_MASK, |
| 960 | CCSR_SSI_SxCCR_DC(2)); |
Nicolin Chen | aafa85e | 2013-12-12 18:44:45 +0800 | [diff] [blame] | 961 | switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { |
Fabio Falzoi | cf4f7fc | 2014-08-04 17:08:07 +0200 | [diff] [blame] | 962 | case SND_SOC_DAIFMT_CBM_CFS: |
Nicolin Chen | aafa85e | 2013-12-12 18:44:45 +0800 | [diff] [blame] | 963 | case SND_SOC_DAIFMT_CBS_CFS: |
Markus Pargmann | 07a28db | 2014-03-15 13:44:10 +0100 | [diff] [blame] | 964 | ssi_private->i2s_mode |= CCSR_SSI_SCR_I2S_MODE_MASTER; |
Nicolin Chen | aafa85e | 2013-12-12 18:44:45 +0800 | [diff] [blame] | 965 | break; |
| 966 | case SND_SOC_DAIFMT_CBM_CFM: |
Markus Pargmann | 07a28db | 2014-03-15 13:44:10 +0100 | [diff] [blame] | 967 | ssi_private->i2s_mode |= CCSR_SSI_SCR_I2S_MODE_SLAVE; |
Nicolin Chen | aafa85e | 2013-12-12 18:44:45 +0800 | [diff] [blame] | 968 | break; |
| 969 | default: |
| 970 | return -EINVAL; |
| 971 | } |
Nicolin Chen | aafa85e | 2013-12-12 18:44:45 +0800 | [diff] [blame] | 972 | |
| 973 | /* Data on rising edge of bclk, frame low, 1clk before data */ |
| 974 | strcr |= CCSR_SSI_STCR_TFSI | CCSR_SSI_STCR_TSCKP | |
| 975 | CCSR_SSI_STCR_TXBIT0 | CCSR_SSI_STCR_TEFS; |
| 976 | break; |
| 977 | case SND_SOC_DAIFMT_LEFT_J: |
| 978 | /* Data on rising edge of bclk, frame high */ |
| 979 | strcr |= CCSR_SSI_STCR_TXBIT0 | CCSR_SSI_STCR_TSCKP; |
| 980 | break; |
| 981 | case SND_SOC_DAIFMT_DSP_A: |
| 982 | /* Data on rising edge of bclk, frame high, 1clk before data */ |
| 983 | strcr |= CCSR_SSI_STCR_TFSL | CCSR_SSI_STCR_TSCKP | |
| 984 | CCSR_SSI_STCR_TXBIT0 | CCSR_SSI_STCR_TEFS; |
| 985 | break; |
| 986 | case SND_SOC_DAIFMT_DSP_B: |
| 987 | /* Data on rising edge of bclk, frame high */ |
| 988 | strcr |= CCSR_SSI_STCR_TFSL | CCSR_SSI_STCR_TSCKP | |
| 989 | CCSR_SSI_STCR_TXBIT0; |
| 990 | break; |
Markus Pargmann | 2b0db99 | 2014-03-15 13:44:09 +0100 | [diff] [blame] | 991 | case SND_SOC_DAIFMT_AC97: |
Markus Pargmann | 07a28db | 2014-03-15 13:44:10 +0100 | [diff] [blame] | 992 | ssi_private->i2s_mode |= CCSR_SSI_SCR_I2S_MODE_NORMAL; |
Markus Pargmann | 2b0db99 | 2014-03-15 13:44:09 +0100 | [diff] [blame] | 993 | break; |
Nicolin Chen | aafa85e | 2013-12-12 18:44:45 +0800 | [diff] [blame] | 994 | default: |
| 995 | return -EINVAL; |
| 996 | } |
Markus Pargmann | 2b0db99 | 2014-03-15 13:44:09 +0100 | [diff] [blame] | 997 | scr |= ssi_private->i2s_mode; |
Nicolin Chen | aafa85e | 2013-12-12 18:44:45 +0800 | [diff] [blame] | 998 | |
| 999 | /* DAI clock inversion */ |
| 1000 | switch (fmt & SND_SOC_DAIFMT_INV_MASK) { |
| 1001 | case SND_SOC_DAIFMT_NB_NF: |
| 1002 | /* Nothing to do for both normal cases */ |
| 1003 | break; |
| 1004 | case SND_SOC_DAIFMT_IB_NF: |
| 1005 | /* Invert bit clock */ |
| 1006 | strcr ^= CCSR_SSI_STCR_TSCKP; |
| 1007 | break; |
| 1008 | case SND_SOC_DAIFMT_NB_IF: |
| 1009 | /* Invert frame clock */ |
| 1010 | strcr ^= CCSR_SSI_STCR_TFSI; |
| 1011 | break; |
| 1012 | case SND_SOC_DAIFMT_IB_IF: |
| 1013 | /* Invert both clocks */ |
| 1014 | strcr ^= CCSR_SSI_STCR_TSCKP; |
| 1015 | strcr ^= CCSR_SSI_STCR_TFSI; |
| 1016 | break; |
| 1017 | default: |
| 1018 | return -EINVAL; |
| 1019 | } |
| 1020 | |
| 1021 | /* DAI clock master masks */ |
| 1022 | switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { |
| 1023 | case SND_SOC_DAIFMT_CBS_CFS: |
| 1024 | strcr |= CCSR_SSI_STCR_TFDIR | CCSR_SSI_STCR_TXDIR; |
| 1025 | scr |= CCSR_SSI_SCR_SYS_CLK_EN; |
| 1026 | break; |
| 1027 | case SND_SOC_DAIFMT_CBM_CFM: |
| 1028 | scr &= ~CCSR_SSI_SCR_SYS_CLK_EN; |
| 1029 | break; |
Fabio Falzoi | cf4f7fc | 2014-08-04 17:08:07 +0200 | [diff] [blame] | 1030 | case SND_SOC_DAIFMT_CBM_CFS: |
| 1031 | strcr &= ~CCSR_SSI_STCR_TXDIR; |
| 1032 | strcr |= CCSR_SSI_STCR_TFDIR; |
| 1033 | scr &= ~CCSR_SSI_SCR_SYS_CLK_EN; |
| 1034 | break; |
Nicolin Chen | aafa85e | 2013-12-12 18:44:45 +0800 | [diff] [blame] | 1035 | default: |
Maciej S. Szmigiero | dce0332 | 2015-08-05 17:29:02 +0200 | [diff] [blame] | 1036 | if (!fsl_ssi_is_ac97(ssi_private)) |
| 1037 | return -EINVAL; |
Nicolin Chen | aafa85e | 2013-12-12 18:44:45 +0800 | [diff] [blame] | 1038 | } |
| 1039 | |
| 1040 | stcr |= strcr; |
| 1041 | srcr |= strcr; |
| 1042 | |
Maciej S. Szmigiero | dce0332 | 2015-08-05 17:29:02 +0200 | [diff] [blame] | 1043 | if (ssi_private->cpu_dai_drv.symmetric_rates |
| 1044 | || fsl_ssi_is_ac97(ssi_private)) { |
| 1045 | /* Need to clear RXDIR when using SYNC or AC97 mode */ |
Nicolin Chen | aafa85e | 2013-12-12 18:44:45 +0800 | [diff] [blame] | 1046 | srcr &= ~CCSR_SSI_SRCR_RXDIR; |
| 1047 | scr |= CCSR_SSI_SCR_SYN; |
| 1048 | } |
| 1049 | |
Markus Pargmann | 4324812 | 2014-05-27 10:24:25 +0200 | [diff] [blame] | 1050 | regmap_write(regs, CCSR_SSI_STCR, stcr); |
| 1051 | regmap_write(regs, CCSR_SSI_SRCR, srcr); |
| 1052 | regmap_write(regs, CCSR_SSI_SCR, scr); |
Nicolin Chen | aafa85e | 2013-12-12 18:44:45 +0800 | [diff] [blame] | 1053 | |
Markus Pargmann | 2b0db99 | 2014-03-15 13:44:09 +0100 | [diff] [blame] | 1054 | /* |
| 1055 | * Set the watermark for transmit FIFI 0 and receive FIFO 0. We don't |
| 1056 | * use FIFO 1. We program the transmit water to signal a DMA transfer |
| 1057 | * if there are only two (or fewer) elements left in the FIFO. Two |
| 1058 | * elements equals one frame (left channel, right channel). This value, |
| 1059 | * however, depends on the depth of the transmit buffer. |
| 1060 | * |
| 1061 | * We set the watermark on the same level as the DMA burstsize. For |
| 1062 | * fiq it is probably better to use the biggest possible watermark |
| 1063 | * size. |
| 1064 | */ |
| 1065 | if (ssi_private->use_dma) |
| 1066 | wm = ssi_private->fifo_depth - 2; |
| 1067 | else |
| 1068 | wm = ssi_private->fifo_depth; |
| 1069 | |
Markus Pargmann | 4324812 | 2014-05-27 10:24:25 +0200 | [diff] [blame] | 1070 | regmap_write(regs, CCSR_SSI_SFCSR, |
| 1071 | CCSR_SSI_SFCSR_TFWM0(wm) | CCSR_SSI_SFCSR_RFWM0(wm) | |
| 1072 | CCSR_SSI_SFCSR_TFWM1(wm) | CCSR_SSI_SFCSR_RFWM1(wm)); |
Markus Pargmann | 2b0db99 | 2014-03-15 13:44:09 +0100 | [diff] [blame] | 1073 | |
| 1074 | if (ssi_private->use_dual_fifo) { |
Markus Pargmann | 4324812 | 2014-05-27 10:24:25 +0200 | [diff] [blame] | 1075 | regmap_update_bits(regs, CCSR_SSI_SRCR, CCSR_SSI_SRCR_RFEN1, |
Markus Pargmann | 2b0db99 | 2014-03-15 13:44:09 +0100 | [diff] [blame] | 1076 | CCSR_SSI_SRCR_RFEN1); |
Markus Pargmann | 4324812 | 2014-05-27 10:24:25 +0200 | [diff] [blame] | 1077 | regmap_update_bits(regs, CCSR_SSI_STCR, CCSR_SSI_STCR_TFEN1, |
Markus Pargmann | 2b0db99 | 2014-03-15 13:44:09 +0100 | [diff] [blame] | 1078 | CCSR_SSI_STCR_TFEN1); |
Markus Pargmann | 4324812 | 2014-05-27 10:24:25 +0200 | [diff] [blame] | 1079 | regmap_update_bits(regs, CCSR_SSI_SCR, CCSR_SSI_SCR_TCH_EN, |
Markus Pargmann | 2b0db99 | 2014-03-15 13:44:09 +0100 | [diff] [blame] | 1080 | CCSR_SSI_SCR_TCH_EN); |
| 1081 | } |
| 1082 | |
Adam Thomson | 5b64c17 | 2015-09-16 10:13:19 +0100 | [diff] [blame] | 1083 | if ((fmt & SND_SOC_DAIFMT_FORMAT_MASK) == SND_SOC_DAIFMT_AC97) |
Markus Pargmann | 2b0db99 | 2014-03-15 13:44:09 +0100 | [diff] [blame] | 1084 | fsl_ssi_setup_ac97(ssi_private); |
| 1085 | |
Nicolin Chen | aafa85e | 2013-12-12 18:44:45 +0800 | [diff] [blame] | 1086 | return 0; |
Markus Pargmann | 85e59af2 | 2014-05-27 10:24:19 +0200 | [diff] [blame] | 1087 | |
| 1088 | } |
| 1089 | |
| 1090 | /** |
| 1091 | * fsl_ssi_set_dai_fmt - configure Digital Audio Interface Format. |
| 1092 | */ |
| 1093 | static int fsl_ssi_set_dai_fmt(struct snd_soc_dai *cpu_dai, unsigned int fmt) |
| 1094 | { |
| 1095 | struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(cpu_dai); |
| 1096 | |
Michael Trimarchi | 8515146 | 2014-09-18 20:38:09 +0200 | [diff] [blame] | 1097 | return _fsl_ssi_set_dai_fmt(cpu_dai->dev, ssi_private, fmt); |
Nicolin Chen | aafa85e | 2013-12-12 18:44:45 +0800 | [diff] [blame] | 1098 | } |
| 1099 | |
| 1100 | /** |
Nicolin Chen | aafa85e | 2013-12-12 18:44:45 +0800 | [diff] [blame] | 1101 | * fsl_ssi_set_dai_tdm_slot - set TDM slot number |
| 1102 | * |
| 1103 | * Note: This function can be only called when using SSI as DAI master |
| 1104 | */ |
| 1105 | static int fsl_ssi_set_dai_tdm_slot(struct snd_soc_dai *cpu_dai, u32 tx_mask, |
| 1106 | u32 rx_mask, int slots, int slot_width) |
| 1107 | { |
| 1108 | struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(cpu_dai); |
Markus Pargmann | 4324812 | 2014-05-27 10:24:25 +0200 | [diff] [blame] | 1109 | struct regmap *regs = ssi_private->regs; |
Nicolin Chen | aafa85e | 2013-12-12 18:44:45 +0800 | [diff] [blame] | 1110 | u32 val; |
| 1111 | |
| 1112 | /* The slot number should be >= 2 if using Network mode or I2S mode */ |
Markus Pargmann | 4324812 | 2014-05-27 10:24:25 +0200 | [diff] [blame] | 1113 | regmap_read(regs, CCSR_SSI_SCR, &val); |
| 1114 | val &= CCSR_SSI_SCR_I2S_MODE_MASK | CCSR_SSI_SCR_NET; |
Nicolin Chen | aafa85e | 2013-12-12 18:44:45 +0800 | [diff] [blame] | 1115 | if (val && slots < 2) { |
| 1116 | dev_err(cpu_dai->dev, "slot number should be >= 2 in I2S or NET\n"); |
| 1117 | return -EINVAL; |
| 1118 | } |
| 1119 | |
Markus Pargmann | 4324812 | 2014-05-27 10:24:25 +0200 | [diff] [blame] | 1120 | regmap_update_bits(regs, CCSR_SSI_STCCR, CCSR_SSI_SxCCR_DC_MASK, |
Nicolin Chen | aafa85e | 2013-12-12 18:44:45 +0800 | [diff] [blame] | 1121 | CCSR_SSI_SxCCR_DC(slots)); |
Markus Pargmann | 4324812 | 2014-05-27 10:24:25 +0200 | [diff] [blame] | 1122 | regmap_update_bits(regs, CCSR_SSI_SRCCR, CCSR_SSI_SxCCR_DC_MASK, |
Nicolin Chen | aafa85e | 2013-12-12 18:44:45 +0800 | [diff] [blame] | 1123 | CCSR_SSI_SxCCR_DC(slots)); |
| 1124 | |
| 1125 | /* The register SxMSKs needs SSI to provide essential clock due to |
| 1126 | * hardware design. So we here temporarily enable SSI to set them. |
| 1127 | */ |
Markus Pargmann | 4324812 | 2014-05-27 10:24:25 +0200 | [diff] [blame] | 1128 | regmap_read(regs, CCSR_SSI_SCR, &val); |
| 1129 | val &= CCSR_SSI_SCR_SSIEN; |
| 1130 | regmap_update_bits(regs, CCSR_SSI_SCR, CCSR_SSI_SCR_SSIEN, |
| 1131 | CCSR_SSI_SCR_SSIEN); |
Nicolin Chen | aafa85e | 2013-12-12 18:44:45 +0800 | [diff] [blame] | 1132 | |
Lars-Peter Clausen | d0077aa | 2015-01-12 10:27:18 +0100 | [diff] [blame] | 1133 | regmap_write(regs, CCSR_SSI_STMSK, ~tx_mask); |
| 1134 | regmap_write(regs, CCSR_SSI_SRMSK, ~rx_mask); |
Nicolin Chen | aafa85e | 2013-12-12 18:44:45 +0800 | [diff] [blame] | 1135 | |
Markus Pargmann | 4324812 | 2014-05-27 10:24:25 +0200 | [diff] [blame] | 1136 | regmap_update_bits(regs, CCSR_SSI_SCR, CCSR_SSI_SCR_SSIEN, val); |
Nicolin Chen | aafa85e | 2013-12-12 18:44:45 +0800 | [diff] [blame] | 1137 | |
| 1138 | return 0; |
| 1139 | } |
| 1140 | |
| 1141 | /** |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 1142 | * fsl_ssi_trigger: start and stop the DMA transfer. |
| 1143 | * |
| 1144 | * This function is called by ALSA to start, stop, pause, and resume the DMA |
| 1145 | * transfer of data. |
| 1146 | * |
| 1147 | * The DMA channel is in external master start and pause mode, which |
| 1148 | * means the SSI completely controls the flow of data. |
| 1149 | */ |
Mark Brown | dee89c4 | 2008-11-18 22:11:38 +0000 | [diff] [blame] | 1150 | static int fsl_ssi_trigger(struct snd_pcm_substream *substream, int cmd, |
| 1151 | struct snd_soc_dai *dai) |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 1152 | { |
| 1153 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1154 | struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(rtd->cpu_dai); |
Markus Pargmann | 4324812 | 2014-05-27 10:24:25 +0200 | [diff] [blame] | 1155 | struct regmap *regs = ssi_private->regs; |
Michael Grzeschik | 9b443e3 | 2013-08-19 17:06:00 +0200 | [diff] [blame] | 1156 | |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 1157 | switch (cmd) { |
| 1158 | case SNDRV_PCM_TRIGGER_START: |
Fabio Estevam | b20e53a | 2014-05-23 02:38:56 -0300 | [diff] [blame] | 1159 | case SNDRV_PCM_TRIGGER_RESUME: |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 1160 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: |
Timur Tabi | a4d11fe | 2009-03-25 18:20:37 -0500 | [diff] [blame] | 1161 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
Markus Pargmann | 6de8387 | 2013-12-20 14:11:34 +0100 | [diff] [blame] | 1162 | fsl_ssi_tx_config(ssi_private, true); |
Timur Tabi | a4d11fe | 2009-03-25 18:20:37 -0500 | [diff] [blame] | 1163 | else |
Markus Pargmann | 6de8387 | 2013-12-20 14:11:34 +0100 | [diff] [blame] | 1164 | fsl_ssi_rx_config(ssi_private, true); |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 1165 | break; |
| 1166 | |
| 1167 | case SNDRV_PCM_TRIGGER_STOP: |
Fabio Estevam | b20e53a | 2014-05-23 02:38:56 -0300 | [diff] [blame] | 1168 | case SNDRV_PCM_TRIGGER_SUSPEND: |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 1169 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
| 1170 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
Markus Pargmann | 6de8387 | 2013-12-20 14:11:34 +0100 | [diff] [blame] | 1171 | fsl_ssi_tx_config(ssi_private, false); |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 1172 | else |
Markus Pargmann | 6de8387 | 2013-12-20 14:11:34 +0100 | [diff] [blame] | 1173 | fsl_ssi_rx_config(ssi_private, false); |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 1174 | break; |
| 1175 | |
| 1176 | default: |
| 1177 | return -EINVAL; |
| 1178 | } |
| 1179 | |
Markus Pargmann | 171d683 | 2014-04-28 12:54:48 +0200 | [diff] [blame] | 1180 | if (fsl_ssi_is_ac97(ssi_private)) { |
Markus Pargmann | a5a7ee7 | 2013-12-20 14:11:35 +0100 | [diff] [blame] | 1181 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
Markus Pargmann | 4324812 | 2014-05-27 10:24:25 +0200 | [diff] [blame] | 1182 | regmap_write(regs, CCSR_SSI_SOR, CCSR_SSI_SOR_TX_CLR); |
Markus Pargmann | a5a7ee7 | 2013-12-20 14:11:35 +0100 | [diff] [blame] | 1183 | else |
Markus Pargmann | 4324812 | 2014-05-27 10:24:25 +0200 | [diff] [blame] | 1184 | regmap_write(regs, CCSR_SSI_SOR, CCSR_SSI_SOR_RX_CLR); |
Markus Pargmann | a5a7ee7 | 2013-12-20 14:11:35 +0100 | [diff] [blame] | 1185 | } |
Michael Grzeschik | 9b443e3 | 2013-08-19 17:06:00 +0200 | [diff] [blame] | 1186 | |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 1187 | return 0; |
| 1188 | } |
| 1189 | |
Lars-Peter Clausen | fc8ba7f | 2013-04-15 19:19:58 +0200 | [diff] [blame] | 1190 | static int fsl_ssi_dai_probe(struct snd_soc_dai *dai) |
| 1191 | { |
| 1192 | struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(dai); |
| 1193 | |
Sascha Hauer | fcdbade | 2014-05-27 10:24:18 +0200 | [diff] [blame] | 1194 | if (ssi_private->soc->imx && ssi_private->use_dma) { |
Lars-Peter Clausen | fc8ba7f | 2013-04-15 19:19:58 +0200 | [diff] [blame] | 1195 | dai->playback_dma_data = &ssi_private->dma_params_tx; |
| 1196 | dai->capture_dma_data = &ssi_private->dma_params_rx; |
| 1197 | } |
| 1198 | |
| 1199 | return 0; |
| 1200 | } |
| 1201 | |
Lars-Peter Clausen | 85e7652 | 2011-11-23 11:40:40 +0100 | [diff] [blame] | 1202 | static const struct snd_soc_dai_ops fsl_ssi_dai_ops = { |
Eric Miao | 6335d05 | 2009-03-03 09:41:00 +0800 | [diff] [blame] | 1203 | .startup = fsl_ssi_startup, |
Shengjiu Wang | f4a43ca | 2014-09-16 10:13:16 +0800 | [diff] [blame] | 1204 | .shutdown = fsl_ssi_shutdown, |
Eric Miao | 6335d05 | 2009-03-03 09:41:00 +0800 | [diff] [blame] | 1205 | .hw_params = fsl_ssi_hw_params, |
Markus Pargmann | d429d8e | 2014-05-27 10:24:23 +0200 | [diff] [blame] | 1206 | .hw_free = fsl_ssi_hw_free, |
Nicolin Chen | aafa85e | 2013-12-12 18:44:45 +0800 | [diff] [blame] | 1207 | .set_fmt = fsl_ssi_set_dai_fmt, |
| 1208 | .set_sysclk = fsl_ssi_set_dai_sysclk, |
| 1209 | .set_tdm_slot = fsl_ssi_set_dai_tdm_slot, |
Eric Miao | 6335d05 | 2009-03-03 09:41:00 +0800 | [diff] [blame] | 1210 | .trigger = fsl_ssi_trigger, |
Eric Miao | 6335d05 | 2009-03-03 09:41:00 +0800 | [diff] [blame] | 1211 | }; |
| 1212 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1213 | /* Template for the CPU dai driver structure */ |
| 1214 | static struct snd_soc_dai_driver fsl_ssi_dai_template = { |
Lars-Peter Clausen | fc8ba7f | 2013-04-15 19:19:58 +0200 | [diff] [blame] | 1215 | .probe = fsl_ssi_dai_probe, |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 1216 | .playback = { |
Nicolin Chen | e365500 | 2014-07-30 11:10:29 +0800 | [diff] [blame] | 1217 | .stream_name = "CPU-Playback", |
Nicolin Chen | 2924a99 | 2013-12-02 23:29:03 +0800 | [diff] [blame] | 1218 | .channels_min = 1, |
Arnaud Mouiche | 48a260e | 2016-05-03 14:13:55 +0200 | [diff] [blame] | 1219 | .channels_max = 32, |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 1220 | .rates = FSLSSI_I2S_RATES, |
| 1221 | .formats = FSLSSI_I2S_FORMATS, |
| 1222 | }, |
| 1223 | .capture = { |
Nicolin Chen | e365500 | 2014-07-30 11:10:29 +0800 | [diff] [blame] | 1224 | .stream_name = "CPU-Capture", |
Nicolin Chen | 2924a99 | 2013-12-02 23:29:03 +0800 | [diff] [blame] | 1225 | .channels_min = 1, |
Arnaud Mouiche | 48a260e | 2016-05-03 14:13:55 +0200 | [diff] [blame] | 1226 | .channels_max = 32, |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 1227 | .rates = FSLSSI_I2S_RATES, |
| 1228 | .formats = FSLSSI_I2S_FORMATS, |
| 1229 | }, |
Eric Miao | 6335d05 | 2009-03-03 09:41:00 +0800 | [diff] [blame] | 1230 | .ops = &fsl_ssi_dai_ops, |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 1231 | }; |
| 1232 | |
Kuninori Morimoto | 3580aa1 | 2013-03-21 03:32:04 -0700 | [diff] [blame] | 1233 | static const struct snd_soc_component_driver fsl_ssi_component = { |
| 1234 | .name = "fsl-ssi", |
| 1235 | }; |
| 1236 | |
Markus Pargmann | cd7f029 | 2013-08-19 17:05:58 +0200 | [diff] [blame] | 1237 | static struct snd_soc_dai_driver fsl_ssi_ac97_dai = { |
Lars-Peter Clausen | bc26321 | 2014-11-10 22:41:52 +0100 | [diff] [blame] | 1238 | .bus_control = true, |
Maciej S. Szmigiero | 793e3e9 | 2015-08-05 17:22:53 +0200 | [diff] [blame] | 1239 | .probe = fsl_ssi_dai_probe, |
Markus Pargmann | cd7f029 | 2013-08-19 17:05:58 +0200 | [diff] [blame] | 1240 | .playback = { |
| 1241 | .stream_name = "AC97 Playback", |
| 1242 | .channels_min = 2, |
| 1243 | .channels_max = 2, |
| 1244 | .rates = SNDRV_PCM_RATE_8000_48000, |
| 1245 | .formats = SNDRV_PCM_FMTBIT_S16_LE, |
| 1246 | }, |
| 1247 | .capture = { |
| 1248 | .stream_name = "AC97 Capture", |
| 1249 | .channels_min = 2, |
| 1250 | .channels_max = 2, |
| 1251 | .rates = SNDRV_PCM_RATE_48000, |
| 1252 | .formats = SNDRV_PCM_FMTBIT_S16_LE, |
| 1253 | }, |
Markus Pargmann | a5a7ee7 | 2013-12-20 14:11:35 +0100 | [diff] [blame] | 1254 | .ops = &fsl_ssi_dai_ops, |
Markus Pargmann | cd7f029 | 2013-08-19 17:05:58 +0200 | [diff] [blame] | 1255 | }; |
| 1256 | |
| 1257 | |
| 1258 | static struct fsl_ssi_private *fsl_ac97_data; |
| 1259 | |
Sachin Kamat | a851a2b | 2013-09-13 15:22:17 +0530 | [diff] [blame] | 1260 | static void fsl_ssi_ac97_write(struct snd_ac97 *ac97, unsigned short reg, |
Markus Pargmann | cd7f029 | 2013-08-19 17:05:58 +0200 | [diff] [blame] | 1261 | unsigned short val) |
| 1262 | { |
Markus Pargmann | 4324812 | 2014-05-27 10:24:25 +0200 | [diff] [blame] | 1263 | struct regmap *regs = fsl_ac97_data->regs; |
Markus Pargmann | cd7f029 | 2013-08-19 17:05:58 +0200 | [diff] [blame] | 1264 | unsigned int lreg; |
| 1265 | unsigned int lval; |
Maciej S. Szmigiero | 8277df3 | 2015-08-05 17:21:35 +0200 | [diff] [blame] | 1266 | int ret; |
Markus Pargmann | cd7f029 | 2013-08-19 17:05:58 +0200 | [diff] [blame] | 1267 | |
| 1268 | if (reg > 0x7f) |
| 1269 | return; |
| 1270 | |
Maciej S. Szmigiero | 8277df3 | 2015-08-05 17:21:35 +0200 | [diff] [blame] | 1271 | ret = clk_prepare_enable(fsl_ac97_data->clk); |
| 1272 | if (ret) { |
| 1273 | pr_err("ac97 write clk_prepare_enable failed: %d\n", |
| 1274 | ret); |
| 1275 | return; |
| 1276 | } |
Markus Pargmann | cd7f029 | 2013-08-19 17:05:58 +0200 | [diff] [blame] | 1277 | |
| 1278 | lreg = reg << 12; |
Markus Pargmann | 4324812 | 2014-05-27 10:24:25 +0200 | [diff] [blame] | 1279 | regmap_write(regs, CCSR_SSI_SACADD, lreg); |
Markus Pargmann | cd7f029 | 2013-08-19 17:05:58 +0200 | [diff] [blame] | 1280 | |
| 1281 | lval = val << 4; |
Markus Pargmann | 4324812 | 2014-05-27 10:24:25 +0200 | [diff] [blame] | 1282 | regmap_write(regs, CCSR_SSI_SACDAT, lval); |
Markus Pargmann | cd7f029 | 2013-08-19 17:05:58 +0200 | [diff] [blame] | 1283 | |
Markus Pargmann | 4324812 | 2014-05-27 10:24:25 +0200 | [diff] [blame] | 1284 | regmap_update_bits(regs, CCSR_SSI_SACNT, CCSR_SSI_SACNT_RDWR_MASK, |
Markus Pargmann | cd7f029 | 2013-08-19 17:05:58 +0200 | [diff] [blame] | 1285 | CCSR_SSI_SACNT_WR); |
| 1286 | udelay(100); |
Maciej S. Szmigiero | 8277df3 | 2015-08-05 17:21:35 +0200 | [diff] [blame] | 1287 | |
| 1288 | clk_disable_unprepare(fsl_ac97_data->clk); |
Markus Pargmann | cd7f029 | 2013-08-19 17:05:58 +0200 | [diff] [blame] | 1289 | } |
| 1290 | |
Sachin Kamat | a851a2b | 2013-09-13 15:22:17 +0530 | [diff] [blame] | 1291 | static unsigned short fsl_ssi_ac97_read(struct snd_ac97 *ac97, |
Markus Pargmann | cd7f029 | 2013-08-19 17:05:58 +0200 | [diff] [blame] | 1292 | unsigned short reg) |
| 1293 | { |
Markus Pargmann | 4324812 | 2014-05-27 10:24:25 +0200 | [diff] [blame] | 1294 | struct regmap *regs = fsl_ac97_data->regs; |
Markus Pargmann | cd7f029 | 2013-08-19 17:05:58 +0200 | [diff] [blame] | 1295 | |
| 1296 | unsigned short val = -1; |
Markus Pargmann | 4324812 | 2014-05-27 10:24:25 +0200 | [diff] [blame] | 1297 | u32 reg_val; |
Markus Pargmann | cd7f029 | 2013-08-19 17:05:58 +0200 | [diff] [blame] | 1298 | unsigned int lreg; |
Maciej S. Szmigiero | 8277df3 | 2015-08-05 17:21:35 +0200 | [diff] [blame] | 1299 | int ret; |
| 1300 | |
| 1301 | ret = clk_prepare_enable(fsl_ac97_data->clk); |
| 1302 | if (ret) { |
| 1303 | pr_err("ac97 read clk_prepare_enable failed: %d\n", |
| 1304 | ret); |
| 1305 | return -1; |
| 1306 | } |
Markus Pargmann | cd7f029 | 2013-08-19 17:05:58 +0200 | [diff] [blame] | 1307 | |
| 1308 | lreg = (reg & 0x7f) << 12; |
Markus Pargmann | 4324812 | 2014-05-27 10:24:25 +0200 | [diff] [blame] | 1309 | regmap_write(regs, CCSR_SSI_SACADD, lreg); |
| 1310 | regmap_update_bits(regs, CCSR_SSI_SACNT, CCSR_SSI_SACNT_RDWR_MASK, |
Markus Pargmann | cd7f029 | 2013-08-19 17:05:58 +0200 | [diff] [blame] | 1311 | CCSR_SSI_SACNT_RD); |
| 1312 | |
| 1313 | udelay(100); |
| 1314 | |
Markus Pargmann | 4324812 | 2014-05-27 10:24:25 +0200 | [diff] [blame] | 1315 | regmap_read(regs, CCSR_SSI_SACDAT, ®_val); |
| 1316 | val = (reg_val >> 4) & 0xffff; |
Markus Pargmann | cd7f029 | 2013-08-19 17:05:58 +0200 | [diff] [blame] | 1317 | |
Maciej S. Szmigiero | 8277df3 | 2015-08-05 17:21:35 +0200 | [diff] [blame] | 1318 | clk_disable_unprepare(fsl_ac97_data->clk); |
| 1319 | |
Markus Pargmann | cd7f029 | 2013-08-19 17:05:58 +0200 | [diff] [blame] | 1320 | return val; |
| 1321 | } |
| 1322 | |
| 1323 | static struct snd_ac97_bus_ops fsl_ssi_ac97_ops = { |
| 1324 | .read = fsl_ssi_ac97_read, |
| 1325 | .write = fsl_ssi_ac97_write, |
| 1326 | }; |
| 1327 | |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 1328 | /** |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1329 | * Make every character in a string lower-case |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 1330 | */ |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1331 | static void make_lowercase(char *s) |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 1332 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1333 | char *p = s; |
| 1334 | char c; |
| 1335 | |
| 1336 | while ((c = *p)) { |
| 1337 | if ((c >= 'A') && (c <= 'Z')) |
| 1338 | *p = c + ('a' - 'A'); |
| 1339 | p++; |
| 1340 | } |
| 1341 | } |
| 1342 | |
Markus Pargmann | 49da09e | 2014-04-28 12:54:45 +0200 | [diff] [blame] | 1343 | static int fsl_ssi_imx_probe(struct platform_device *pdev, |
Markus Pargmann | 4d9b792 | 2014-04-28 12:54:47 +0200 | [diff] [blame] | 1344 | struct fsl_ssi_private *ssi_private, void __iomem *iomem) |
Markus Pargmann | 49da09e | 2014-04-28 12:54:45 +0200 | [diff] [blame] | 1345 | { |
| 1346 | struct device_node *np = pdev->dev.of_node; |
Markus Pargmann | ed0f1604 | 2014-04-28 12:54:46 +0200 | [diff] [blame] | 1347 | u32 dmas[4]; |
Markus Pargmann | 49da09e | 2014-04-28 12:54:45 +0200 | [diff] [blame] | 1348 | int ret; |
| 1349 | |
Shengjiu Wang | f4a43ca | 2014-09-16 10:13:16 +0800 | [diff] [blame] | 1350 | if (ssi_private->has_ipg_clk_name) |
| 1351 | ssi_private->clk = devm_clk_get(&pdev->dev, "ipg"); |
| 1352 | else |
| 1353 | ssi_private->clk = devm_clk_get(&pdev->dev, NULL); |
Markus Pargmann | 49da09e | 2014-04-28 12:54:45 +0200 | [diff] [blame] | 1354 | if (IS_ERR(ssi_private->clk)) { |
| 1355 | ret = PTR_ERR(ssi_private->clk); |
| 1356 | dev_err(&pdev->dev, "could not get clock: %d\n", ret); |
| 1357 | return ret; |
| 1358 | } |
| 1359 | |
Shengjiu Wang | f4a43ca | 2014-09-16 10:13:16 +0800 | [diff] [blame] | 1360 | if (!ssi_private->has_ipg_clk_name) { |
| 1361 | ret = clk_prepare_enable(ssi_private->clk); |
| 1362 | if (ret) { |
| 1363 | dev_err(&pdev->dev, "clk_prepare_enable failed: %d\n", ret); |
| 1364 | return ret; |
| 1365 | } |
Markus Pargmann | 49da09e | 2014-04-28 12:54:45 +0200 | [diff] [blame] | 1366 | } |
| 1367 | |
Xiubo Li | dcfcf2c | 2015-08-12 14:38:18 +0800 | [diff] [blame] | 1368 | /* For those SLAVE implementations, we ignore non-baudclk cases |
Markus Pargmann | 49da09e | 2014-04-28 12:54:45 +0200 | [diff] [blame] | 1369 | * and, instead, abandon MASTER mode that needs baud clock. |
| 1370 | */ |
| 1371 | ssi_private->baudclk = devm_clk_get(&pdev->dev, "baud"); |
| 1372 | if (IS_ERR(ssi_private->baudclk)) |
| 1373 | dev_dbg(&pdev->dev, "could not get baud clock: %ld\n", |
| 1374 | PTR_ERR(ssi_private->baudclk)); |
Markus Pargmann | 49da09e | 2014-04-28 12:54:45 +0200 | [diff] [blame] | 1375 | |
| 1376 | /* |
| 1377 | * We have burstsize be "fifo_depth - 2" to match the SSI |
| 1378 | * watermark setting in fsl_ssi_startup(). |
| 1379 | */ |
| 1380 | ssi_private->dma_params_tx.maxburst = ssi_private->fifo_depth - 2; |
| 1381 | ssi_private->dma_params_rx.maxburst = ssi_private->fifo_depth - 2; |
Markus Pargmann | 4324812 | 2014-05-27 10:24:25 +0200 | [diff] [blame] | 1382 | ssi_private->dma_params_tx.addr = ssi_private->ssi_phys + CCSR_SSI_STX0; |
| 1383 | ssi_private->dma_params_rx.addr = ssi_private->ssi_phys + CCSR_SSI_SRX0; |
Markus Pargmann | 49da09e | 2014-04-28 12:54:45 +0200 | [diff] [blame] | 1384 | |
Maciej S. Szmigiero | 90aff15 | 2015-03-04 22:48:30 +0100 | [diff] [blame] | 1385 | ret = of_property_read_u32_array(np, "dmas", dmas, 4); |
Markus Pargmann | ed0f1604 | 2014-04-28 12:54:46 +0200 | [diff] [blame] | 1386 | if (ssi_private->use_dma && !ret && dmas[2] == IMX_DMATYPE_SSI_DUAL) { |
Markus Pargmann | 49da09e | 2014-04-28 12:54:45 +0200 | [diff] [blame] | 1387 | ssi_private->use_dual_fifo = true; |
| 1388 | /* When using dual fifo mode, we need to keep watermark |
| 1389 | * as even numbers due to dma script limitation. |
| 1390 | */ |
| 1391 | ssi_private->dma_params_tx.maxburst &= ~0x1; |
| 1392 | ssi_private->dma_params_rx.maxburst &= ~0x1; |
| 1393 | } |
| 1394 | |
Markus Pargmann | 4d9b792 | 2014-04-28 12:54:47 +0200 | [diff] [blame] | 1395 | if (!ssi_private->use_dma) { |
| 1396 | |
| 1397 | /* |
| 1398 | * Some boards use an incompatible codec. To get it |
| 1399 | * working, we are using imx-fiq-pcm-audio, that |
| 1400 | * can handle those codecs. DMA is not possible in this |
| 1401 | * situation. |
| 1402 | */ |
| 1403 | |
| 1404 | ssi_private->fiq_params.irq = ssi_private->irq; |
| 1405 | ssi_private->fiq_params.base = iomem; |
| 1406 | ssi_private->fiq_params.dma_params_rx = |
| 1407 | &ssi_private->dma_params_rx; |
| 1408 | ssi_private->fiq_params.dma_params_tx = |
| 1409 | &ssi_private->dma_params_tx; |
| 1410 | |
| 1411 | ret = imx_pcm_fiq_init(pdev, &ssi_private->fiq_params); |
| 1412 | if (ret) |
| 1413 | goto error_pcm; |
| 1414 | } else { |
Shengjiu Wang | 0d69e0d | 2015-06-23 18:23:53 +0800 | [diff] [blame] | 1415 | ret = imx_pcm_dma_init(pdev, IMX_SSI_DMABUF_SIZE); |
Markus Pargmann | 4d9b792 | 2014-04-28 12:54:47 +0200 | [diff] [blame] | 1416 | if (ret) |
| 1417 | goto error_pcm; |
| 1418 | } |
| 1419 | |
Markus Pargmann | 49da09e | 2014-04-28 12:54:45 +0200 | [diff] [blame] | 1420 | return 0; |
Markus Pargmann | 4d9b792 | 2014-04-28 12:54:47 +0200 | [diff] [blame] | 1421 | |
| 1422 | error_pcm: |
Markus Pargmann | 4d9b792 | 2014-04-28 12:54:47 +0200 | [diff] [blame] | 1423 | |
Shengjiu Wang | f4a43ca | 2014-09-16 10:13:16 +0800 | [diff] [blame] | 1424 | if (!ssi_private->has_ipg_clk_name) |
| 1425 | clk_disable_unprepare(ssi_private->clk); |
Markus Pargmann | 4d9b792 | 2014-04-28 12:54:47 +0200 | [diff] [blame] | 1426 | return ret; |
Markus Pargmann | 49da09e | 2014-04-28 12:54:45 +0200 | [diff] [blame] | 1427 | } |
| 1428 | |
| 1429 | static void fsl_ssi_imx_clean(struct platform_device *pdev, |
| 1430 | struct fsl_ssi_private *ssi_private) |
| 1431 | { |
Markus Pargmann | 4d9b792 | 2014-04-28 12:54:47 +0200 | [diff] [blame] | 1432 | if (!ssi_private->use_dma) |
| 1433 | imx_pcm_fiq_exit(pdev); |
Shengjiu Wang | f4a43ca | 2014-09-16 10:13:16 +0800 | [diff] [blame] | 1434 | if (!ssi_private->has_ipg_clk_name) |
| 1435 | clk_disable_unprepare(ssi_private->clk); |
Markus Pargmann | 49da09e | 2014-04-28 12:54:45 +0200 | [diff] [blame] | 1436 | } |
| 1437 | |
Bill Pemberton | a0a3d51 | 2012-12-07 09:26:16 -0500 | [diff] [blame] | 1438 | static int fsl_ssi_probe(struct platform_device *pdev) |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1439 | { |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 1440 | struct fsl_ssi_private *ssi_private; |
| 1441 | int ret = 0; |
Timur Tabi | 38fec72 | 2010-08-19 15:26:58 -0500 | [diff] [blame] | 1442 | struct device_node *np = pdev->dev.of_node; |
Markus Pargmann | c1953bf | 2013-12-20 14:11:30 +0100 | [diff] [blame] | 1443 | const struct of_device_id *of_id; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1444 | const char *p, *sprop; |
Timur Tabi | 8e9d869 | 2010-08-06 12:16:12 -0500 | [diff] [blame] | 1445 | const uint32_t *iprop; |
Fabio Estevam | ca26418 | 2015-04-10 07:12:29 -0300 | [diff] [blame] | 1446 | struct resource *res; |
Markus Pargmann | 4324812 | 2014-05-27 10:24:25 +0200 | [diff] [blame] | 1447 | void __iomem *iomem; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1448 | char name[64]; |
Maciej S. Szmigiero | 6139b1b | 2016-01-18 20:07:44 +0100 | [diff] [blame] | 1449 | struct regmap_config regconfig = fsl_ssi_regconfig; |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 1450 | |
Markus Pargmann | c1953bf | 2013-12-20 14:11:30 +0100 | [diff] [blame] | 1451 | of_id = of_match_device(fsl_ssi_ids, &pdev->dev); |
Sascha Hauer | fcdbade | 2014-05-27 10:24:18 +0200 | [diff] [blame] | 1452 | if (!of_id || !of_id->data) |
Markus Pargmann | c1953bf | 2013-12-20 14:11:30 +0100 | [diff] [blame] | 1453 | return -EINVAL; |
Markus Pargmann | c1953bf | 2013-12-20 14:11:30 +0100 | [diff] [blame] | 1454 | |
Markus Pargmann | 2a1d102 | 2014-04-28 12:54:44 +0200 | [diff] [blame] | 1455 | ssi_private = devm_kzalloc(&pdev->dev, sizeof(*ssi_private), |
| 1456 | GFP_KERNEL); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1457 | if (!ssi_private) { |
Timur Tabi | 38fec72 | 2010-08-19 15:26:58 -0500 | [diff] [blame] | 1458 | dev_err(&pdev->dev, "could not allocate DAI object\n"); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1459 | return -ENOMEM; |
| 1460 | } |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 1461 | |
Sascha Hauer | fcdbade | 2014-05-27 10:24:18 +0200 | [diff] [blame] | 1462 | ssi_private->soc = of_id->data; |
Arnaud Mouiche | 0096b69 | 2016-05-03 14:13:57 +0200 | [diff] [blame] | 1463 | ssi_private->dev = &pdev->dev; |
Sascha Hauer | fcdbade | 2014-05-27 10:24:18 +0200 | [diff] [blame] | 1464 | |
Markus Pargmann | 85e59af2 | 2014-05-27 10:24:19 +0200 | [diff] [blame] | 1465 | sprop = of_get_property(np, "fsl,mode", NULL); |
| 1466 | if (sprop) { |
| 1467 | if (!strcmp(sprop, "ac97-slave")) |
| 1468 | ssi_private->dai_fmt = SND_SOC_DAIFMT_AC97; |
Markus Pargmann | 85e59af2 | 2014-05-27 10:24:19 +0200 | [diff] [blame] | 1469 | } |
| 1470 | |
Markus Pargmann | de623ec | 2013-07-27 13:31:53 +0200 | [diff] [blame] | 1471 | ssi_private->use_dma = !of_property_read_bool(np, |
| 1472 | "fsl,fiq-stream-filter"); |
| 1473 | |
Markus Pargmann | 85e59af2 | 2014-05-27 10:24:19 +0200 | [diff] [blame] | 1474 | if (fsl_ssi_is_ac97(ssi_private)) { |
Markus Pargmann | cd7f029 | 2013-08-19 17:05:58 +0200 | [diff] [blame] | 1475 | memcpy(&ssi_private->cpu_dai_drv, &fsl_ssi_ac97_dai, |
| 1476 | sizeof(fsl_ssi_ac97_dai)); |
| 1477 | |
| 1478 | fsl_ac97_data = ssi_private; |
Markus Pargmann | cd7f029 | 2013-08-19 17:05:58 +0200 | [diff] [blame] | 1479 | |
Maciej S. Szmigiero | 04143d6 | 2015-08-05 17:25:31 +0200 | [diff] [blame] | 1480 | ret = snd_soc_set_ac97_ops_of_reset(&fsl_ssi_ac97_ops, pdev); |
| 1481 | if (ret) { |
| 1482 | dev_err(&pdev->dev, "could not set AC'97 ops\n"); |
| 1483 | return ret; |
| 1484 | } |
Markus Pargmann | cd7f029 | 2013-08-19 17:05:58 +0200 | [diff] [blame] | 1485 | } else { |
| 1486 | /* Initialize this copy of the CPU DAI driver structure */ |
| 1487 | memcpy(&ssi_private->cpu_dai_drv, &fsl_ssi_dai_template, |
| 1488 | sizeof(fsl_ssi_dai_template)); |
| 1489 | } |
Markus Pargmann | 2a1d102 | 2014-04-28 12:54:44 +0200 | [diff] [blame] | 1490 | ssi_private->cpu_dai_drv.name = dev_name(&pdev->dev); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1491 | |
Fabio Estevam | ca26418 | 2015-04-10 07:12:29 -0300 | [diff] [blame] | 1492 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1493 | iomem = devm_ioremap_resource(&pdev->dev, res); |
| 1494 | if (IS_ERR(iomem)) |
| 1495 | return PTR_ERR(iomem); |
| 1496 | ssi_private->ssi_phys = res->start; |
Markus Pargmann | 4324812 | 2014-05-27 10:24:25 +0200 | [diff] [blame] | 1497 | |
Maciej S. Szmigiero | 6139b1b | 2016-01-18 20:07:44 +0100 | [diff] [blame] | 1498 | if (ssi_private->soc->imx21regs) { |
| 1499 | /* |
| 1500 | * According to datasheet imx21-class SSI |
| 1501 | * don't have SACC{ST,EN,DIS} regs. |
| 1502 | */ |
| 1503 | regconfig.max_register = CCSR_SSI_SRMSK; |
| 1504 | regconfig.num_reg_defaults_raw = |
| 1505 | CCSR_SSI_SRMSK / sizeof(uint32_t) + 1; |
| 1506 | } |
| 1507 | |
Shengjiu Wang | f4a43ca | 2014-09-16 10:13:16 +0800 | [diff] [blame] | 1508 | ret = of_property_match_string(np, "clock-names", "ipg"); |
| 1509 | if (ret < 0) { |
| 1510 | ssi_private->has_ipg_clk_name = false; |
| 1511 | ssi_private->regs = devm_regmap_init_mmio(&pdev->dev, iomem, |
Maciej S. Szmigiero | 6139b1b | 2016-01-18 20:07:44 +0100 | [diff] [blame] | 1512 | ®config); |
Shengjiu Wang | f4a43ca | 2014-09-16 10:13:16 +0800 | [diff] [blame] | 1513 | } else { |
| 1514 | ssi_private->has_ipg_clk_name = true; |
| 1515 | ssi_private->regs = devm_regmap_init_mmio_clk(&pdev->dev, |
Maciej S. Szmigiero | 6139b1b | 2016-01-18 20:07:44 +0100 | [diff] [blame] | 1516 | "ipg", iomem, ®config); |
Shengjiu Wang | f4a43ca | 2014-09-16 10:13:16 +0800 | [diff] [blame] | 1517 | } |
Markus Pargmann | 4324812 | 2014-05-27 10:24:25 +0200 | [diff] [blame] | 1518 | if (IS_ERR(ssi_private->regs)) { |
| 1519 | dev_err(&pdev->dev, "Failed to init register map\n"); |
| 1520 | return PTR_ERR(ssi_private->regs); |
| 1521 | } |
Timur Tabi | 1fab6ca | 2011-08-16 18:47:45 -0400 | [diff] [blame] | 1522 | |
Fabio Estevam | 2ffa531 | 2014-12-01 19:57:14 -0200 | [diff] [blame] | 1523 | ssi_private->irq = platform_get_irq(pdev, 0); |
Fabio Estevam | 28ecc0b | 2015-04-15 00:08:15 -0300 | [diff] [blame] | 1524 | if (ssi_private->irq < 0) { |
Fabio Estevam | 0c12325 | 2015-01-07 13:44:32 -0200 | [diff] [blame] | 1525 | dev_err(&pdev->dev, "no irq for node %s\n", pdev->name); |
Fabio Estevam | 64aa5f5 | 2015-01-07 19:45:40 -0200 | [diff] [blame] | 1526 | return ssi_private->irq; |
Timur Tabi | 1fab6ca | 2011-08-16 18:47:45 -0400 | [diff] [blame] | 1527 | } |
| 1528 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1529 | /* Are the RX and the TX clocks locked? */ |
Nicolin Chen | 07a9483 | 2013-12-03 18:38:07 +0800 | [diff] [blame] | 1530 | if (!of_find_property(np, "fsl,ssi-asynchronous", NULL)) { |
Maciej S. Szmigiero | 06cb373 | 2015-08-05 17:24:10 +0200 | [diff] [blame] | 1531 | if (!fsl_ssi_is_ac97(ssi_private)) |
| 1532 | ssi_private->cpu_dai_drv.symmetric_rates = 1; |
| 1533 | |
Nicolin Chen | 07a9483 | 2013-12-03 18:38:07 +0800 | [diff] [blame] | 1534 | ssi_private->cpu_dai_drv.symmetric_channels = 1; |
| 1535 | ssi_private->cpu_dai_drv.symmetric_samplebits = 1; |
| 1536 | } |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 1537 | |
Timur Tabi | 8e9d869 | 2010-08-06 12:16:12 -0500 | [diff] [blame] | 1538 | /* Determine the FIFO depth. */ |
| 1539 | iprop = of_get_property(np, "fsl,fifo-depth", NULL); |
| 1540 | if (iprop) |
Timur Tabi | 147dfe9 | 2011-06-08 15:02:55 -0500 | [diff] [blame] | 1541 | ssi_private->fifo_depth = be32_to_cpup(iprop); |
Timur Tabi | 8e9d869 | 2010-08-06 12:16:12 -0500 | [diff] [blame] | 1542 | else |
| 1543 | /* Older 8610 DTs didn't have the fifo-depth property */ |
| 1544 | ssi_private->fifo_depth = 8; |
| 1545 | |
Markus Pargmann | 4d9b792 | 2014-04-28 12:54:47 +0200 | [diff] [blame] | 1546 | dev_set_drvdata(&pdev->dev, ssi_private); |
| 1547 | |
Sascha Hauer | fcdbade | 2014-05-27 10:24:18 +0200 | [diff] [blame] | 1548 | if (ssi_private->soc->imx) { |
Markus Pargmann | 4324812 | 2014-05-27 10:24:25 +0200 | [diff] [blame] | 1549 | ret = fsl_ssi_imx_probe(pdev, ssi_private, iomem); |
Markus Pargmann | 49da09e | 2014-04-28 12:54:45 +0200 | [diff] [blame] | 1550 | if (ret) |
Fabio Estevam | 2ffa531 | 2014-12-01 19:57:14 -0200 | [diff] [blame] | 1551 | return ret; |
Markus Pargmann | 0888efd | 2013-12-20 14:11:31 +0100 | [diff] [blame] | 1552 | } |
| 1553 | |
Fabio Estevam | 299e7e9 | 2015-04-09 14:56:41 -0300 | [diff] [blame] | 1554 | ret = devm_snd_soc_register_component(&pdev->dev, &fsl_ssi_component, |
| 1555 | &ssi_private->cpu_dai_drv, 1); |
Markus Pargmann | 4d9b792 | 2014-04-28 12:54:47 +0200 | [diff] [blame] | 1556 | if (ret) { |
| 1557 | dev_err(&pdev->dev, "failed to register DAI: %d\n", ret); |
| 1558 | goto error_asoc_register; |
| 1559 | } |
| 1560 | |
Markus Pargmann | 0888efd | 2013-12-20 14:11:31 +0100 | [diff] [blame] | 1561 | if (ssi_private->use_dma) { |
Michael Grzeschik | f037708 | 2013-08-19 17:06:01 +0200 | [diff] [blame] | 1562 | ret = devm_request_irq(&pdev->dev, ssi_private->irq, |
Markus Pargmann | 171d683 | 2014-04-28 12:54:48 +0200 | [diff] [blame] | 1563 | fsl_ssi_isr, 0, dev_name(&pdev->dev), |
Michael Grzeschik | f037708 | 2013-08-19 17:06:01 +0200 | [diff] [blame] | 1564 | ssi_private); |
| 1565 | if (ret < 0) { |
| 1566 | dev_err(&pdev->dev, "could not claim irq %u\n", |
| 1567 | ssi_private->irq); |
Fabio Estevam | 299e7e9 | 2015-04-09 14:56:41 -0300 | [diff] [blame] | 1568 | goto error_asoc_register; |
Michael Grzeschik | f037708 | 2013-08-19 17:06:01 +0200 | [diff] [blame] | 1569 | } |
Shawn Guo | 09ce111 | 2012-03-16 16:56:43 +0800 | [diff] [blame] | 1570 | } |
| 1571 | |
Markus Pargmann | f138e62 | 2014-04-28 12:54:43 +0200 | [diff] [blame] | 1572 | ret = fsl_ssi_debugfs_create(&ssi_private->dbg_stats, &pdev->dev); |
Markus Pargmann | 9368acc | 2013-12-20 14:11:29 +0100 | [diff] [blame] | 1573 | if (ret) |
Fabio Estevam | 299e7e9 | 2015-04-09 14:56:41 -0300 | [diff] [blame] | 1574 | goto error_asoc_register; |
Shawn Guo | 09ce111 | 2012-03-16 16:56:43 +0800 | [diff] [blame] | 1575 | |
| 1576 | /* |
| 1577 | * If codec-handle property is missing from SSI node, we assume |
| 1578 | * that the machine driver uses new binding which does not require |
| 1579 | * SSI driver to trigger machine driver's probe. |
| 1580 | */ |
Markus Pargmann | 171d683 | 2014-04-28 12:54:48 +0200 | [diff] [blame] | 1581 | if (!of_get_property(np, "codec-handle", NULL)) |
Shawn Guo | 09ce111 | 2012-03-16 16:56:43 +0800 | [diff] [blame] | 1582 | goto done; |
Shawn Guo | 09ce111 | 2012-03-16 16:56:43 +0800 | [diff] [blame] | 1583 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1584 | /* Trigger the machine driver's probe function. The platform driver |
Shawn Guo | 2b81ec6 | 2012-03-09 00:59:46 +0800 | [diff] [blame] | 1585 | * name of the machine driver is taken from /compatible property of the |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1586 | * device tree. We also pass the address of the CPU DAI driver |
| 1587 | * structure. |
| 1588 | */ |
Shawn Guo | 2b81ec6 | 2012-03-09 00:59:46 +0800 | [diff] [blame] | 1589 | sprop = of_get_property(of_find_node_by_path("/"), "compatible", NULL); |
| 1590 | /* Sometimes the compatible name has a "fsl," prefix, so we strip it. */ |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1591 | p = strrchr(sprop, ','); |
| 1592 | if (p) |
| 1593 | sprop = p + 1; |
| 1594 | snprintf(name, sizeof(name), "snd-soc-%s", sprop); |
| 1595 | make_lowercase(name); |
| 1596 | |
| 1597 | ssi_private->pdev = |
Timur Tabi | 38fec72 | 2010-08-19 15:26:58 -0500 | [diff] [blame] | 1598 | platform_device_register_data(&pdev->dev, name, 0, NULL, 0); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1599 | if (IS_ERR(ssi_private->pdev)) { |
| 1600 | ret = PTR_ERR(ssi_private->pdev); |
Timur Tabi | 38fec72 | 2010-08-19 15:26:58 -0500 | [diff] [blame] | 1601 | dev_err(&pdev->dev, "failed to register platform: %d\n", ret); |
Markus Pargmann | 4d9b792 | 2014-04-28 12:54:47 +0200 | [diff] [blame] | 1602 | goto error_sound_card; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1603 | } |
| 1604 | |
Shawn Guo | 09ce111 | 2012-03-16 16:56:43 +0800 | [diff] [blame] | 1605 | done: |
Markus Pargmann | 85e59af2 | 2014-05-27 10:24:19 +0200 | [diff] [blame] | 1606 | if (ssi_private->dai_fmt) |
Michael Trimarchi | 8515146 | 2014-09-18 20:38:09 +0200 | [diff] [blame] | 1607 | _fsl_ssi_set_dai_fmt(&pdev->dev, ssi_private, |
| 1608 | ssi_private->dai_fmt); |
Markus Pargmann | 85e59af2 | 2014-05-27 10:24:19 +0200 | [diff] [blame] | 1609 | |
Maciej S. Szmigiero | 8ed0c84 | 2015-08-05 17:26:44 +0200 | [diff] [blame] | 1610 | if (fsl_ssi_is_ac97(ssi_private)) { |
| 1611 | u32 ssi_idx; |
| 1612 | |
| 1613 | ret = of_property_read_u32(np, "cell-index", &ssi_idx); |
| 1614 | if (ret) { |
| 1615 | dev_err(&pdev->dev, "cannot get SSI index property\n"); |
| 1616 | goto error_sound_card; |
| 1617 | } |
| 1618 | |
| 1619 | ssi_private->pdev = |
| 1620 | platform_device_register_data(NULL, |
| 1621 | "ac97-codec", ssi_idx, NULL, 0); |
| 1622 | if (IS_ERR(ssi_private->pdev)) { |
| 1623 | ret = PTR_ERR(ssi_private->pdev); |
| 1624 | dev_err(&pdev->dev, |
| 1625 | "failed to register AC97 codec platform: %d\n", |
| 1626 | ret); |
| 1627 | goto error_sound_card; |
| 1628 | } |
| 1629 | } |
| 1630 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1631 | return 0; |
Timur Tabi | 87a0632 | 2010-08-03 17:55:28 -0500 | [diff] [blame] | 1632 | |
Markus Pargmann | 4d9b792 | 2014-04-28 12:54:47 +0200 | [diff] [blame] | 1633 | error_sound_card: |
Markus Pargmann | f138e62 | 2014-04-28 12:54:43 +0200 | [diff] [blame] | 1634 | fsl_ssi_debugfs_remove(&ssi_private->dbg_stats); |
Markus Pargmann | 9368acc | 2013-12-20 14:11:29 +0100 | [diff] [blame] | 1635 | |
Markus Pargmann | 4d9b792 | 2014-04-28 12:54:47 +0200 | [diff] [blame] | 1636 | error_asoc_register: |
Sascha Hauer | fcdbade | 2014-05-27 10:24:18 +0200 | [diff] [blame] | 1637 | if (ssi_private->soc->imx) |
Markus Pargmann | 49da09e | 2014-04-28 12:54:45 +0200 | [diff] [blame] | 1638 | fsl_ssi_imx_clean(pdev, ssi_private); |
Timur Tabi | 1fab6ca | 2011-08-16 18:47:45 -0400 | [diff] [blame] | 1639 | |
Timur Tabi | 87a0632 | 2010-08-03 17:55:28 -0500 | [diff] [blame] | 1640 | return ret; |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 1641 | } |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 1642 | |
Timur Tabi | 38fec72 | 2010-08-19 15:26:58 -0500 | [diff] [blame] | 1643 | static int fsl_ssi_remove(struct platform_device *pdev) |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 1644 | { |
Timur Tabi | 38fec72 | 2010-08-19 15:26:58 -0500 | [diff] [blame] | 1645 | struct fsl_ssi_private *ssi_private = dev_get_drvdata(&pdev->dev); |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 1646 | |
Markus Pargmann | f138e62 | 2014-04-28 12:54:43 +0200 | [diff] [blame] | 1647 | fsl_ssi_debugfs_remove(&ssi_private->dbg_stats); |
Markus Pargmann | 9368acc | 2013-12-20 14:11:29 +0100 | [diff] [blame] | 1648 | |
Markus Pargmann | 171d683 | 2014-04-28 12:54:48 +0200 | [diff] [blame] | 1649 | if (ssi_private->pdev) |
Shawn Guo | 09ce111 | 2012-03-16 16:56:43 +0800 | [diff] [blame] | 1650 | platform_device_unregister(ssi_private->pdev); |
Markus Pargmann | 49da09e | 2014-04-28 12:54:45 +0200 | [diff] [blame] | 1651 | |
Sascha Hauer | fcdbade | 2014-05-27 10:24:18 +0200 | [diff] [blame] | 1652 | if (ssi_private->soc->imx) |
Markus Pargmann | 49da09e | 2014-04-28 12:54:45 +0200 | [diff] [blame] | 1653 | fsl_ssi_imx_clean(pdev, ssi_private); |
| 1654 | |
Maciej S. Szmigiero | 04143d6 | 2015-08-05 17:25:31 +0200 | [diff] [blame] | 1655 | if (fsl_ssi_is_ac97(ssi_private)) |
| 1656 | snd_soc_set_ac97_ops(NULL); |
| 1657 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1658 | return 0; |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 1659 | } |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1660 | |
Zidan Wang | 05cf237 | 2015-09-18 11:09:12 +0800 | [diff] [blame] | 1661 | #ifdef CONFIG_PM_SLEEP |
| 1662 | static int fsl_ssi_suspend(struct device *dev) |
| 1663 | { |
| 1664 | struct fsl_ssi_private *ssi_private = dev_get_drvdata(dev); |
| 1665 | struct regmap *regs = ssi_private->regs; |
| 1666 | |
| 1667 | regmap_read(regs, CCSR_SSI_SFCSR, |
| 1668 | &ssi_private->regcache_sfcsr); |
Maciej S. Szmigiero | 3f1c241 | 2015-12-20 21:30:25 +0100 | [diff] [blame] | 1669 | regmap_read(regs, CCSR_SSI_SACNT, |
| 1670 | &ssi_private->regcache_sacnt); |
Zidan Wang | 05cf237 | 2015-09-18 11:09:12 +0800 | [diff] [blame] | 1671 | |
| 1672 | regcache_cache_only(regs, true); |
| 1673 | regcache_mark_dirty(regs); |
| 1674 | |
| 1675 | return 0; |
| 1676 | } |
| 1677 | |
| 1678 | static int fsl_ssi_resume(struct device *dev) |
| 1679 | { |
| 1680 | struct fsl_ssi_private *ssi_private = dev_get_drvdata(dev); |
| 1681 | struct regmap *regs = ssi_private->regs; |
| 1682 | |
| 1683 | regcache_cache_only(regs, false); |
| 1684 | |
| 1685 | regmap_update_bits(regs, CCSR_SSI_SFCSR, |
| 1686 | CCSR_SSI_SFCSR_RFWM1_MASK | CCSR_SSI_SFCSR_TFWM1_MASK | |
| 1687 | CCSR_SSI_SFCSR_RFWM0_MASK | CCSR_SSI_SFCSR_TFWM0_MASK, |
| 1688 | ssi_private->regcache_sfcsr); |
Maciej S. Szmigiero | 3f1c241 | 2015-12-20 21:30:25 +0100 | [diff] [blame] | 1689 | regmap_write(regs, CCSR_SSI_SACNT, |
| 1690 | ssi_private->regcache_sacnt); |
Zidan Wang | 05cf237 | 2015-09-18 11:09:12 +0800 | [diff] [blame] | 1691 | |
| 1692 | return regcache_sync(regs); |
| 1693 | } |
| 1694 | #endif /* CONFIG_PM_SLEEP */ |
| 1695 | |
| 1696 | static const struct dev_pm_ops fsl_ssi_pm = { |
| 1697 | SET_SYSTEM_SLEEP_PM_OPS(fsl_ssi_suspend, fsl_ssi_resume) |
| 1698 | }; |
| 1699 | |
Grant Likely | f07eb22 | 2011-02-22 21:05:04 -0700 | [diff] [blame] | 1700 | static struct platform_driver fsl_ssi_driver = { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1701 | .driver = { |
| 1702 | .name = "fsl-ssi-dai", |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1703 | .of_match_table = fsl_ssi_ids, |
Zidan Wang | 05cf237 | 2015-09-18 11:09:12 +0800 | [diff] [blame] | 1704 | .pm = &fsl_ssi_pm, |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1705 | }, |
| 1706 | .probe = fsl_ssi_probe, |
| 1707 | .remove = fsl_ssi_remove, |
| 1708 | }; |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 1709 | |
Axel Lin | ba0a7e0 | 2011-11-25 10:10:55 +0800 | [diff] [blame] | 1710 | module_platform_driver(fsl_ssi_driver); |
Timur Tabi | a454dad | 2009-03-05 17:23:37 -0600 | [diff] [blame] | 1711 | |
Fabio Estevam | f314280 | 2013-07-20 16:16:01 -0300 | [diff] [blame] | 1712 | MODULE_ALIAS("platform:fsl-ssi-dai"); |
Timur Tabi | 17467f2 | 2008-01-11 18:15:26 +0100 | [diff] [blame] | 1713 | MODULE_AUTHOR("Timur Tabi <timur@freescale.com>"); |
| 1714 | MODULE_DESCRIPTION("Freescale Synchronous Serial Interface (SSI) ASoC Driver"); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1715 | MODULE_LICENSE("GPL v2"); |