Xiubo Li | 4355082 | 2013-12-17 11:24:38 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Freescale ALSA SoC Digital Audio Interface (SAI) driver. |
| 3 | * |
Zidan Wang | c3ecef2 | 2015-05-11 18:24:41 +0800 | [diff] [blame] | 4 | * Copyright 2012-2015 Freescale Semiconductor, Inc. |
Xiubo Li | 4355082 | 2013-12-17 11:24:38 +0800 | [diff] [blame] | 5 | * |
| 6 | * This program is free software, you can redistribute it and/or modify it |
| 7 | * under the terms of the GNU General Public License as published by the |
| 8 | * Free Software Foundation, either version 2 of the License, or(at your |
| 9 | * option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #include <linux/clk.h> |
| 14 | #include <linux/delay.h> |
| 15 | #include <linux/dmaengine.h> |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/of_address.h> |
Xiubo Li | 78957fc | 2014-02-08 14:38:28 +0800 | [diff] [blame] | 18 | #include <linux/regmap.h> |
Xiubo Li | 4355082 | 2013-12-17 11:24:38 +0800 | [diff] [blame] | 19 | #include <linux/slab.h> |
| 20 | #include <sound/core.h> |
| 21 | #include <sound/dmaengine_pcm.h> |
| 22 | #include <sound/pcm_params.h> |
| 23 | |
| 24 | #include "fsl_sai.h" |
Nicolin Chen | c754064 | 2014-04-01 19:34:09 +0800 | [diff] [blame] | 25 | #include "imx-pcm.h" |
Xiubo Li | 4355082 | 2013-12-17 11:24:38 +0800 | [diff] [blame] | 26 | |
Nicolin Chen | e2681a1 | 2014-03-27 19:06:59 +0800 | [diff] [blame] | 27 | #define FSL_SAI_FLAGS (FSL_SAI_CSR_SEIE |\ |
| 28 | FSL_SAI_CSR_FEIE) |
| 29 | |
Zidan Wang | c5f4823 | 2015-05-11 18:24:43 +0800 | [diff] [blame^] | 30 | static u32 fsl_sai_rates[] = { |
| 31 | 8000, 11025, 12000, 16000, 22050, |
| 32 | 24000, 32000, 44100, 48000, 64000, |
| 33 | 88200, 96000, 176400, 192000 |
| 34 | }; |
| 35 | |
| 36 | static struct snd_pcm_hw_constraint_list fsl_sai_rate_constraints = { |
| 37 | .count = ARRAY_SIZE(fsl_sai_rates), |
| 38 | .list = fsl_sai_rates, |
| 39 | }; |
| 40 | |
Nicolin Chen | e2681a1 | 2014-03-27 19:06:59 +0800 | [diff] [blame] | 41 | static irqreturn_t fsl_sai_isr(int irq, void *devid) |
| 42 | { |
| 43 | struct fsl_sai *sai = (struct fsl_sai *)devid; |
| 44 | struct device *dev = &sai->pdev->dev; |
Nicolin Chen | 413312a | 2014-03-28 19:39:25 +0800 | [diff] [blame] | 45 | u32 flags, xcsr, mask; |
| 46 | bool irq_none = true; |
Nicolin Chen | e2681a1 | 2014-03-27 19:06:59 +0800 | [diff] [blame] | 47 | |
Nicolin Chen | 413312a | 2014-03-28 19:39:25 +0800 | [diff] [blame] | 48 | /* |
| 49 | * Both IRQ status bits and IRQ mask bits are in the xCSR but |
| 50 | * different shifts. And we here create a mask only for those |
| 51 | * IRQs that we activated. |
| 52 | */ |
Nicolin Chen | e2681a1 | 2014-03-27 19:06:59 +0800 | [diff] [blame] | 53 | mask = (FSL_SAI_FLAGS >> FSL_SAI_CSR_xIE_SHIFT) << FSL_SAI_CSR_xF_SHIFT; |
| 54 | |
| 55 | /* Tx IRQ */ |
| 56 | regmap_read(sai->regmap, FSL_SAI_TCSR, &xcsr); |
Nicolin Chen | 413312a | 2014-03-28 19:39:25 +0800 | [diff] [blame] | 57 | flags = xcsr & mask; |
Nicolin Chen | e2681a1 | 2014-03-27 19:06:59 +0800 | [diff] [blame] | 58 | |
Nicolin Chen | 413312a | 2014-03-28 19:39:25 +0800 | [diff] [blame] | 59 | if (flags) |
| 60 | irq_none = false; |
| 61 | else |
| 62 | goto irq_rx; |
| 63 | |
| 64 | if (flags & FSL_SAI_CSR_WSF) |
Nicolin Chen | e2681a1 | 2014-03-27 19:06:59 +0800 | [diff] [blame] | 65 | dev_dbg(dev, "isr: Start of Tx word detected\n"); |
| 66 | |
Nicolin Chen | 413312a | 2014-03-28 19:39:25 +0800 | [diff] [blame] | 67 | if (flags & FSL_SAI_CSR_SEF) |
Nicolin Chen | e2681a1 | 2014-03-27 19:06:59 +0800 | [diff] [blame] | 68 | dev_warn(dev, "isr: Tx Frame sync error detected\n"); |
| 69 | |
Nicolin Chen | 413312a | 2014-03-28 19:39:25 +0800 | [diff] [blame] | 70 | if (flags & FSL_SAI_CSR_FEF) { |
Nicolin Chen | e2681a1 | 2014-03-27 19:06:59 +0800 | [diff] [blame] | 71 | dev_warn(dev, "isr: Transmit underrun detected\n"); |
| 72 | /* FIFO reset for safety */ |
| 73 | xcsr |= FSL_SAI_CSR_FR; |
| 74 | } |
| 75 | |
Nicolin Chen | 413312a | 2014-03-28 19:39:25 +0800 | [diff] [blame] | 76 | if (flags & FSL_SAI_CSR_FWF) |
Nicolin Chen | e2681a1 | 2014-03-27 19:06:59 +0800 | [diff] [blame] | 77 | dev_dbg(dev, "isr: Enabled transmit FIFO is empty\n"); |
| 78 | |
Nicolin Chen | 413312a | 2014-03-28 19:39:25 +0800 | [diff] [blame] | 79 | if (flags & FSL_SAI_CSR_FRF) |
Nicolin Chen | e2681a1 | 2014-03-27 19:06:59 +0800 | [diff] [blame] | 80 | dev_dbg(dev, "isr: Transmit FIFO watermark has been reached\n"); |
| 81 | |
Nicolin Chen | 413312a | 2014-03-28 19:39:25 +0800 | [diff] [blame] | 82 | flags &= FSL_SAI_CSR_xF_W_MASK; |
| 83 | xcsr &= ~FSL_SAI_CSR_xF_MASK; |
Nicolin Chen | e2681a1 | 2014-03-27 19:06:59 +0800 | [diff] [blame] | 84 | |
Nicolin Chen | 413312a | 2014-03-28 19:39:25 +0800 | [diff] [blame] | 85 | if (flags) |
| 86 | regmap_write(sai->regmap, FSL_SAI_TCSR, flags | xcsr); |
| 87 | |
| 88 | irq_rx: |
Nicolin Chen | e2681a1 | 2014-03-27 19:06:59 +0800 | [diff] [blame] | 89 | /* Rx IRQ */ |
| 90 | regmap_read(sai->regmap, FSL_SAI_RCSR, &xcsr); |
Nicolin Chen | 413312a | 2014-03-28 19:39:25 +0800 | [diff] [blame] | 91 | flags = xcsr & mask; |
Nicolin Chen | e2681a1 | 2014-03-27 19:06:59 +0800 | [diff] [blame] | 92 | |
Nicolin Chen | 413312a | 2014-03-28 19:39:25 +0800 | [diff] [blame] | 93 | if (flags) |
| 94 | irq_none = false; |
| 95 | else |
| 96 | goto out; |
| 97 | |
| 98 | if (flags & FSL_SAI_CSR_WSF) |
Nicolin Chen | e2681a1 | 2014-03-27 19:06:59 +0800 | [diff] [blame] | 99 | dev_dbg(dev, "isr: Start of Rx word detected\n"); |
| 100 | |
Nicolin Chen | 413312a | 2014-03-28 19:39:25 +0800 | [diff] [blame] | 101 | if (flags & FSL_SAI_CSR_SEF) |
Nicolin Chen | e2681a1 | 2014-03-27 19:06:59 +0800 | [diff] [blame] | 102 | dev_warn(dev, "isr: Rx Frame sync error detected\n"); |
| 103 | |
Nicolin Chen | 413312a | 2014-03-28 19:39:25 +0800 | [diff] [blame] | 104 | if (flags & FSL_SAI_CSR_FEF) { |
Nicolin Chen | e2681a1 | 2014-03-27 19:06:59 +0800 | [diff] [blame] | 105 | dev_warn(dev, "isr: Receive overflow detected\n"); |
| 106 | /* FIFO reset for safety */ |
| 107 | xcsr |= FSL_SAI_CSR_FR; |
| 108 | } |
| 109 | |
Nicolin Chen | 413312a | 2014-03-28 19:39:25 +0800 | [diff] [blame] | 110 | if (flags & FSL_SAI_CSR_FWF) |
Nicolin Chen | e2681a1 | 2014-03-27 19:06:59 +0800 | [diff] [blame] | 111 | dev_dbg(dev, "isr: Enabled receive FIFO is full\n"); |
| 112 | |
Nicolin Chen | 413312a | 2014-03-28 19:39:25 +0800 | [diff] [blame] | 113 | if (flags & FSL_SAI_CSR_FRF) |
Nicolin Chen | e2681a1 | 2014-03-27 19:06:59 +0800 | [diff] [blame] | 114 | dev_dbg(dev, "isr: Receive FIFO watermark has been reached\n"); |
| 115 | |
Nicolin Chen | 413312a | 2014-03-28 19:39:25 +0800 | [diff] [blame] | 116 | flags &= FSL_SAI_CSR_xF_W_MASK; |
| 117 | xcsr &= ~FSL_SAI_CSR_xF_MASK; |
Nicolin Chen | e2681a1 | 2014-03-27 19:06:59 +0800 | [diff] [blame] | 118 | |
Nicolin Chen | 413312a | 2014-03-28 19:39:25 +0800 | [diff] [blame] | 119 | if (flags) |
Nicolin Chen | 4800f88 | 2014-07-17 21:21:38 +0800 | [diff] [blame] | 120 | regmap_write(sai->regmap, FSL_SAI_RCSR, flags | xcsr); |
Nicolin Chen | 413312a | 2014-03-28 19:39:25 +0800 | [diff] [blame] | 121 | |
| 122 | out: |
| 123 | if (irq_none) |
| 124 | return IRQ_NONE; |
| 125 | else |
| 126 | return IRQ_HANDLED; |
Nicolin Chen | e2681a1 | 2014-03-27 19:06:59 +0800 | [diff] [blame] | 127 | } |
| 128 | |
Xiubo Li | 4355082 | 2013-12-17 11:24:38 +0800 | [diff] [blame] | 129 | static int fsl_sai_set_dai_sysclk_tr(struct snd_soc_dai *cpu_dai, |
| 130 | int clk_id, unsigned int freq, int fsl_dir) |
| 131 | { |
Xiubo Li | 4355082 | 2013-12-17 11:24:38 +0800 | [diff] [blame] | 132 | struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai); |
Nicolin Chen | 2a266f8 | 2014-04-11 18:30:09 +0800 | [diff] [blame] | 133 | bool tx = fsl_dir == FSL_FMT_TRANSMITTER; |
| 134 | u32 val_cr2 = 0; |
Xiubo Li | 633ff8f | 2014-01-08 16:13:05 +0800 | [diff] [blame] | 135 | |
Xiubo Li | 4355082 | 2013-12-17 11:24:38 +0800 | [diff] [blame] | 136 | switch (clk_id) { |
| 137 | case FSL_SAI_CLK_BUS: |
Xiubo Li | 4355082 | 2013-12-17 11:24:38 +0800 | [diff] [blame] | 138 | val_cr2 |= FSL_SAI_CR2_MSEL_BUS; |
| 139 | break; |
| 140 | case FSL_SAI_CLK_MAST1: |
Xiubo Li | 4355082 | 2013-12-17 11:24:38 +0800 | [diff] [blame] | 141 | val_cr2 |= FSL_SAI_CR2_MSEL_MCLK1; |
| 142 | break; |
| 143 | case FSL_SAI_CLK_MAST2: |
Xiubo Li | 4355082 | 2013-12-17 11:24:38 +0800 | [diff] [blame] | 144 | val_cr2 |= FSL_SAI_CR2_MSEL_MCLK2; |
| 145 | break; |
| 146 | case FSL_SAI_CLK_MAST3: |
Xiubo Li | 4355082 | 2013-12-17 11:24:38 +0800 | [diff] [blame] | 147 | val_cr2 |= FSL_SAI_CR2_MSEL_MCLK3; |
| 148 | break; |
| 149 | default: |
| 150 | return -EINVAL; |
| 151 | } |
Xiubo Li | 633ff8f | 2014-01-08 16:13:05 +0800 | [diff] [blame] | 152 | |
Nicolin Chen | 2a266f8 | 2014-04-11 18:30:09 +0800 | [diff] [blame] | 153 | regmap_update_bits(sai->regmap, FSL_SAI_xCR2(tx), |
| 154 | FSL_SAI_CR2_MSEL_MASK, val_cr2); |
Xiubo Li | 4355082 | 2013-12-17 11:24:38 +0800 | [diff] [blame] | 155 | |
| 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | static int fsl_sai_set_dai_sysclk(struct snd_soc_dai *cpu_dai, |
| 160 | int clk_id, unsigned int freq, int dir) |
| 161 | { |
Nicolin Chen | 4e3a99f | 2013-12-20 16:41:05 +0800 | [diff] [blame] | 162 | int ret; |
Xiubo Li | 4355082 | 2013-12-17 11:24:38 +0800 | [diff] [blame] | 163 | |
| 164 | if (dir == SND_SOC_CLOCK_IN) |
| 165 | return 0; |
| 166 | |
Xiubo Li | 4355082 | 2013-12-17 11:24:38 +0800 | [diff] [blame] | 167 | ret = fsl_sai_set_dai_sysclk_tr(cpu_dai, clk_id, freq, |
| 168 | FSL_FMT_TRANSMITTER); |
| 169 | if (ret) { |
Nicolin Chen | 190af12 | 2013-12-20 16:41:04 +0800 | [diff] [blame] | 170 | dev_err(cpu_dai->dev, "Cannot set tx sysclk: %d\n", ret); |
Xiubo Li | 78957fc | 2014-02-08 14:38:28 +0800 | [diff] [blame] | 171 | return ret; |
Xiubo Li | 4355082 | 2013-12-17 11:24:38 +0800 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | ret = fsl_sai_set_dai_sysclk_tr(cpu_dai, clk_id, freq, |
| 175 | FSL_FMT_RECEIVER); |
Xiubo Li | 78957fc | 2014-02-08 14:38:28 +0800 | [diff] [blame] | 176 | if (ret) |
Nicolin Chen | 190af12 | 2013-12-20 16:41:04 +0800 | [diff] [blame] | 177 | dev_err(cpu_dai->dev, "Cannot set rx sysclk: %d\n", ret); |
Xiubo Li | 4355082 | 2013-12-17 11:24:38 +0800 | [diff] [blame] | 178 | |
Nicolin Chen | 1fb2d9d | 2013-12-20 16:41:00 +0800 | [diff] [blame] | 179 | return ret; |
Xiubo Li | 4355082 | 2013-12-17 11:24:38 +0800 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | static int fsl_sai_set_dai_fmt_tr(struct snd_soc_dai *cpu_dai, |
| 183 | unsigned int fmt, int fsl_dir) |
| 184 | { |
Xiubo Li | 4355082 | 2013-12-17 11:24:38 +0800 | [diff] [blame] | 185 | struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai); |
Nicolin Chen | 2a266f8 | 2014-04-11 18:30:09 +0800 | [diff] [blame] | 186 | bool tx = fsl_dir == FSL_FMT_TRANSMITTER; |
| 187 | u32 val_cr2 = 0, val_cr4 = 0; |
Xiubo Li | 4355082 | 2013-12-17 11:24:38 +0800 | [diff] [blame] | 188 | |
Xiubo Li | eadb001 | 2014-08-29 15:12:12 +0800 | [diff] [blame] | 189 | if (!sai->is_lsb_first) |
Xiubo Li | 72aa62b | 2013-12-31 15:33:22 +0800 | [diff] [blame] | 190 | val_cr4 |= FSL_SAI_CR4_MF; |
Xiubo Li | 4355082 | 2013-12-17 11:24:38 +0800 | [diff] [blame] | 191 | |
Xiubo Li | 13cde09 | 2014-02-25 17:54:51 +0800 | [diff] [blame] | 192 | /* DAI mode */ |
Xiubo Li | 4355082 | 2013-12-17 11:24:38 +0800 | [diff] [blame] | 193 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { |
| 194 | case SND_SOC_DAIFMT_I2S: |
Xiubo Li | a3f7dcc | 2014-02-27 08:45:01 +0800 | [diff] [blame] | 195 | /* |
| 196 | * Frame low, 1clk before data, one word length for frame sync, |
| 197 | * frame sync starts one serial clock cycle earlier, |
| 198 | * that is, together with the last bit of the previous |
| 199 | * data word. |
| 200 | */ |
Nicolin Chen | ef33bc3 | 2014-04-04 15:09:47 +0800 | [diff] [blame] | 201 | val_cr2 |= FSL_SAI_CR2_BCP; |
Xiubo Li | 13cde09 | 2014-02-25 17:54:51 +0800 | [diff] [blame] | 202 | val_cr4 |= FSL_SAI_CR4_FSE | FSL_SAI_CR4_FSP; |
Xiubo Li | 4355082 | 2013-12-17 11:24:38 +0800 | [diff] [blame] | 203 | break; |
Xiubo Li | 13cde09 | 2014-02-25 17:54:51 +0800 | [diff] [blame] | 204 | case SND_SOC_DAIFMT_LEFT_J: |
Xiubo Li | a3f7dcc | 2014-02-27 08:45:01 +0800 | [diff] [blame] | 205 | /* |
| 206 | * Frame high, one word length for frame sync, |
| 207 | * frame sync asserts with the first bit of the frame. |
| 208 | */ |
Nicolin Chen | ef33bc3 | 2014-04-04 15:09:47 +0800 | [diff] [blame] | 209 | val_cr2 |= FSL_SAI_CR2_BCP; |
Xiubo Li | 13cde09 | 2014-02-25 17:54:51 +0800 | [diff] [blame] | 210 | break; |
Xiubo Li | a3f7dcc | 2014-02-27 08:45:01 +0800 | [diff] [blame] | 211 | case SND_SOC_DAIFMT_DSP_A: |
| 212 | /* |
| 213 | * Frame high, 1clk before data, one bit for frame sync, |
| 214 | * frame sync starts one serial clock cycle earlier, |
| 215 | * that is, together with the last bit of the previous |
| 216 | * data word. |
| 217 | */ |
Nicolin Chen | ef33bc3 | 2014-04-04 15:09:47 +0800 | [diff] [blame] | 218 | val_cr2 |= FSL_SAI_CR2_BCP; |
Xiubo Li | a3f7dcc | 2014-02-27 08:45:01 +0800 | [diff] [blame] | 219 | val_cr4 |= FSL_SAI_CR4_FSE; |
| 220 | sai->is_dsp_mode = true; |
| 221 | break; |
| 222 | case SND_SOC_DAIFMT_DSP_B: |
| 223 | /* |
| 224 | * Frame high, one bit for frame sync, |
| 225 | * frame sync asserts with the first bit of the frame. |
| 226 | */ |
Nicolin Chen | ef33bc3 | 2014-04-04 15:09:47 +0800 | [diff] [blame] | 227 | val_cr2 |= FSL_SAI_CR2_BCP; |
Xiubo Li | a3f7dcc | 2014-02-27 08:45:01 +0800 | [diff] [blame] | 228 | sai->is_dsp_mode = true; |
| 229 | break; |
Xiubo Li | 13cde09 | 2014-02-25 17:54:51 +0800 | [diff] [blame] | 230 | case SND_SOC_DAIFMT_RIGHT_J: |
| 231 | /* To be done */ |
Xiubo Li | 4355082 | 2013-12-17 11:24:38 +0800 | [diff] [blame] | 232 | default: |
| 233 | return -EINVAL; |
| 234 | } |
| 235 | |
Xiubo Li | 13cde09 | 2014-02-25 17:54:51 +0800 | [diff] [blame] | 236 | /* DAI clock inversion */ |
Xiubo Li | 4355082 | 2013-12-17 11:24:38 +0800 | [diff] [blame] | 237 | switch (fmt & SND_SOC_DAIFMT_INV_MASK) { |
| 238 | case SND_SOC_DAIFMT_IB_IF: |
Xiubo Li | 13cde09 | 2014-02-25 17:54:51 +0800 | [diff] [blame] | 239 | /* Invert both clocks */ |
| 240 | val_cr2 ^= FSL_SAI_CR2_BCP; |
| 241 | val_cr4 ^= FSL_SAI_CR4_FSP; |
Xiubo Li | 4355082 | 2013-12-17 11:24:38 +0800 | [diff] [blame] | 242 | break; |
| 243 | case SND_SOC_DAIFMT_IB_NF: |
Xiubo Li | 13cde09 | 2014-02-25 17:54:51 +0800 | [diff] [blame] | 244 | /* Invert bit clock */ |
| 245 | val_cr2 ^= FSL_SAI_CR2_BCP; |
Xiubo Li | 4355082 | 2013-12-17 11:24:38 +0800 | [diff] [blame] | 246 | break; |
| 247 | case SND_SOC_DAIFMT_NB_IF: |
Xiubo Li | 13cde09 | 2014-02-25 17:54:51 +0800 | [diff] [blame] | 248 | /* Invert frame clock */ |
| 249 | val_cr4 ^= FSL_SAI_CR4_FSP; |
Xiubo Li | 4355082 | 2013-12-17 11:24:38 +0800 | [diff] [blame] | 250 | break; |
| 251 | case SND_SOC_DAIFMT_NB_NF: |
Xiubo Li | 13cde09 | 2014-02-25 17:54:51 +0800 | [diff] [blame] | 252 | /* Nothing to do for both normal cases */ |
Xiubo Li | 4355082 | 2013-12-17 11:24:38 +0800 | [diff] [blame] | 253 | break; |
| 254 | default: |
| 255 | return -EINVAL; |
| 256 | } |
| 257 | |
Xiubo Li | 13cde09 | 2014-02-25 17:54:51 +0800 | [diff] [blame] | 258 | /* DAI clock master masks */ |
Xiubo Li | 4355082 | 2013-12-17 11:24:38 +0800 | [diff] [blame] | 259 | switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { |
| 260 | case SND_SOC_DAIFMT_CBS_CFS: |
| 261 | val_cr2 |= FSL_SAI_CR2_BCD_MSTR; |
| 262 | val_cr4 |= FSL_SAI_CR4_FSD_MSTR; |
| 263 | break; |
| 264 | case SND_SOC_DAIFMT_CBM_CFM: |
Zidan Wang | c3ecef2 | 2015-05-11 18:24:41 +0800 | [diff] [blame] | 265 | sai->is_slave_mode = true; |
Xiubo Li | 4355082 | 2013-12-17 11:24:38 +0800 | [diff] [blame] | 266 | break; |
Xiubo Li | 13cde09 | 2014-02-25 17:54:51 +0800 | [diff] [blame] | 267 | case SND_SOC_DAIFMT_CBS_CFM: |
| 268 | val_cr2 |= FSL_SAI_CR2_BCD_MSTR; |
Xiubo Li | 13cde09 | 2014-02-25 17:54:51 +0800 | [diff] [blame] | 269 | break; |
| 270 | case SND_SOC_DAIFMT_CBM_CFS: |
Xiubo Li | 13cde09 | 2014-02-25 17:54:51 +0800 | [diff] [blame] | 271 | val_cr4 |= FSL_SAI_CR4_FSD_MSTR; |
Zidan Wang | c3ecef2 | 2015-05-11 18:24:41 +0800 | [diff] [blame] | 272 | sai->is_slave_mode = true; |
Xiubo Li | 13cde09 | 2014-02-25 17:54:51 +0800 | [diff] [blame] | 273 | break; |
Xiubo Li | 4355082 | 2013-12-17 11:24:38 +0800 | [diff] [blame] | 274 | default: |
| 275 | return -EINVAL; |
| 276 | } |
| 277 | |
Nicolin Chen | 2a266f8 | 2014-04-11 18:30:09 +0800 | [diff] [blame] | 278 | regmap_update_bits(sai->regmap, FSL_SAI_xCR2(tx), |
| 279 | FSL_SAI_CR2_BCP | FSL_SAI_CR2_BCD_MSTR, val_cr2); |
| 280 | regmap_update_bits(sai->regmap, FSL_SAI_xCR4(tx), |
| 281 | FSL_SAI_CR4_MF | FSL_SAI_CR4_FSE | |
| 282 | FSL_SAI_CR4_FSP | FSL_SAI_CR4_FSD_MSTR, val_cr4); |
Xiubo Li | 4355082 | 2013-12-17 11:24:38 +0800 | [diff] [blame] | 283 | |
| 284 | return 0; |
| 285 | } |
| 286 | |
| 287 | static int fsl_sai_set_dai_fmt(struct snd_soc_dai *cpu_dai, unsigned int fmt) |
| 288 | { |
Nicolin Chen | 4e3a99f | 2013-12-20 16:41:05 +0800 | [diff] [blame] | 289 | int ret; |
Xiubo Li | 4355082 | 2013-12-17 11:24:38 +0800 | [diff] [blame] | 290 | |
Xiubo Li | 4355082 | 2013-12-17 11:24:38 +0800 | [diff] [blame] | 291 | ret = fsl_sai_set_dai_fmt_tr(cpu_dai, fmt, FSL_FMT_TRANSMITTER); |
| 292 | if (ret) { |
Nicolin Chen | 190af12 | 2013-12-20 16:41:04 +0800 | [diff] [blame] | 293 | dev_err(cpu_dai->dev, "Cannot set tx format: %d\n", ret); |
Xiubo Li | 78957fc | 2014-02-08 14:38:28 +0800 | [diff] [blame] | 294 | return ret; |
Xiubo Li | 4355082 | 2013-12-17 11:24:38 +0800 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | ret = fsl_sai_set_dai_fmt_tr(cpu_dai, fmt, FSL_FMT_RECEIVER); |
Xiubo Li | 78957fc | 2014-02-08 14:38:28 +0800 | [diff] [blame] | 298 | if (ret) |
Nicolin Chen | 190af12 | 2013-12-20 16:41:04 +0800 | [diff] [blame] | 299 | dev_err(cpu_dai->dev, "Cannot set rx format: %d\n", ret); |
Xiubo Li | 4355082 | 2013-12-17 11:24:38 +0800 | [diff] [blame] | 300 | |
Nicolin Chen | 1fb2d9d | 2013-12-20 16:41:00 +0800 | [diff] [blame] | 301 | return ret; |
Xiubo Li | 4355082 | 2013-12-17 11:24:38 +0800 | [diff] [blame] | 302 | } |
| 303 | |
Zidan Wang | c3ecef2 | 2015-05-11 18:24:41 +0800 | [diff] [blame] | 304 | static int fsl_sai_set_bclk(struct snd_soc_dai *dai, bool tx, u32 freq) |
| 305 | { |
| 306 | struct fsl_sai *sai = snd_soc_dai_get_drvdata(dai); |
| 307 | unsigned long clk_rate; |
| 308 | u32 savediv = 0, ratio, savesub = freq; |
| 309 | u32 id; |
| 310 | int ret = 0; |
| 311 | |
| 312 | /* Don't apply to slave mode */ |
| 313 | if (sai->is_slave_mode) |
| 314 | return 0; |
| 315 | |
| 316 | for (id = 0; id < FSL_SAI_MCLK_MAX; id++) { |
| 317 | clk_rate = clk_get_rate(sai->mclk_clk[id]); |
| 318 | if (!clk_rate) |
| 319 | continue; |
| 320 | |
| 321 | ratio = clk_rate / freq; |
| 322 | |
| 323 | ret = clk_rate - ratio * freq; |
| 324 | |
| 325 | /* |
| 326 | * Drop the source that can not be |
| 327 | * divided into the required rate. |
| 328 | */ |
| 329 | if (ret != 0 && clk_rate / ret < 1000) |
| 330 | continue; |
| 331 | |
| 332 | dev_dbg(dai->dev, |
| 333 | "ratio %d for freq %dHz based on clock %ldHz\n", |
| 334 | ratio, freq, clk_rate); |
| 335 | |
| 336 | if (ratio % 2 == 0 && ratio >= 2 && ratio <= 512) |
| 337 | ratio /= 2; |
| 338 | else |
| 339 | continue; |
| 340 | |
| 341 | if (ret < savesub) { |
| 342 | savediv = ratio; |
| 343 | sai->mclk_id[tx] = id; |
| 344 | savesub = ret; |
| 345 | } |
| 346 | |
| 347 | if (ret == 0) |
| 348 | break; |
| 349 | } |
| 350 | |
| 351 | if (savediv == 0) { |
| 352 | dev_err(dai->dev, "failed to derive required %cx rate: %d\n", |
| 353 | tx ? 'T' : 'R', freq); |
| 354 | return -EINVAL; |
| 355 | } |
| 356 | |
| 357 | if ((tx && sai->synchronous[TX]) || (!tx && !sai->synchronous[RX])) { |
| 358 | regmap_update_bits(sai->regmap, FSL_SAI_RCR2, |
| 359 | FSL_SAI_CR2_MSEL_MASK, |
| 360 | FSL_SAI_CR2_MSEL(sai->mclk_id[tx])); |
| 361 | regmap_update_bits(sai->regmap, FSL_SAI_RCR2, |
| 362 | FSL_SAI_CR2_DIV_MASK, savediv - 1); |
| 363 | } else { |
| 364 | regmap_update_bits(sai->regmap, FSL_SAI_TCR2, |
| 365 | FSL_SAI_CR2_MSEL_MASK, |
| 366 | FSL_SAI_CR2_MSEL(sai->mclk_id[tx])); |
| 367 | regmap_update_bits(sai->regmap, FSL_SAI_TCR2, |
| 368 | FSL_SAI_CR2_DIV_MASK, savediv - 1); |
| 369 | } |
| 370 | |
| 371 | dev_dbg(dai->dev, "best fit: clock id=%d, div=%d, deviation =%d\n", |
| 372 | sai->mclk_id[tx], savediv, savesub); |
| 373 | |
| 374 | return 0; |
| 375 | } |
| 376 | |
Xiubo Li | 4355082 | 2013-12-17 11:24:38 +0800 | [diff] [blame] | 377 | static int fsl_sai_hw_params(struct snd_pcm_substream *substream, |
| 378 | struct snd_pcm_hw_params *params, |
| 379 | struct snd_soc_dai *cpu_dai) |
| 380 | { |
Nicolin Chen | 4e3a99f | 2013-12-20 16:41:05 +0800 | [diff] [blame] | 381 | struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai); |
Nicolin Chen | 2a266f8 | 2014-04-11 18:30:09 +0800 | [diff] [blame] | 382 | bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK; |
Xiubo Li | 4355082 | 2013-12-17 11:24:38 +0800 | [diff] [blame] | 383 | unsigned int channels = params_channels(params); |
Nicolin Chen | 1d70030 | 2013-12-20 16:41:01 +0800 | [diff] [blame] | 384 | u32 word_width = snd_pcm_format_width(params_format(params)); |
Nicolin Chen | 2a266f8 | 2014-04-11 18:30:09 +0800 | [diff] [blame] | 385 | u32 val_cr4 = 0, val_cr5 = 0; |
Zidan Wang | c3ecef2 | 2015-05-11 18:24:41 +0800 | [diff] [blame] | 386 | int ret; |
| 387 | |
| 388 | if (!sai->is_slave_mode) { |
| 389 | ret = fsl_sai_set_bclk(cpu_dai, tx, |
| 390 | 2 * word_width * params_rate(params)); |
| 391 | if (ret) |
| 392 | return ret; |
| 393 | |
| 394 | /* Do not enable the clock if it is already enabled */ |
| 395 | if (!(sai->mclk_streams & BIT(substream->stream))) { |
| 396 | ret = clk_prepare_enable(sai->mclk_clk[sai->mclk_id[tx]]); |
| 397 | if (ret) |
| 398 | return ret; |
| 399 | |
| 400 | sai->mclk_streams |= BIT(substream->stream); |
| 401 | } |
| 402 | |
| 403 | } |
Xiubo Li | 4355082 | 2013-12-17 11:24:38 +0800 | [diff] [blame] | 404 | |
Xiubo Li | a3f7dcc | 2014-02-27 08:45:01 +0800 | [diff] [blame] | 405 | if (!sai->is_dsp_mode) |
| 406 | val_cr4 |= FSL_SAI_CR4_SYWD(word_width); |
| 407 | |
Xiubo Li | 4355082 | 2013-12-17 11:24:38 +0800 | [diff] [blame] | 408 | val_cr5 |= FSL_SAI_CR5_WNW(word_width); |
| 409 | val_cr5 |= FSL_SAI_CR5_W0W(word_width); |
| 410 | |
Xiubo Li | eadb001 | 2014-08-29 15:12:12 +0800 | [diff] [blame] | 411 | if (sai->is_lsb_first) |
Xiubo Li | 4355082 | 2013-12-17 11:24:38 +0800 | [diff] [blame] | 412 | val_cr5 |= FSL_SAI_CR5_FBT(0); |
Xiubo Li | 72aa62b | 2013-12-31 15:33:22 +0800 | [diff] [blame] | 413 | else |
| 414 | val_cr5 |= FSL_SAI_CR5_FBT(word_width - 1); |
Xiubo Li | 4355082 | 2013-12-17 11:24:38 +0800 | [diff] [blame] | 415 | |
| 416 | val_cr4 |= FSL_SAI_CR4_FRSZ(channels); |
Xiubo Li | 4355082 | 2013-12-17 11:24:38 +0800 | [diff] [blame] | 417 | |
Nicolin Chen | 2a266f8 | 2014-04-11 18:30:09 +0800 | [diff] [blame] | 418 | regmap_update_bits(sai->regmap, FSL_SAI_xCR4(tx), |
| 419 | FSL_SAI_CR4_SYWD_MASK | FSL_SAI_CR4_FRSZ_MASK, |
| 420 | val_cr4); |
| 421 | regmap_update_bits(sai->regmap, FSL_SAI_xCR5(tx), |
| 422 | FSL_SAI_CR5_WNW_MASK | FSL_SAI_CR5_W0W_MASK | |
| 423 | FSL_SAI_CR5_FBT_MASK, val_cr5); |
| 424 | regmap_write(sai->regmap, FSL_SAI_xMR(tx), ~0UL - ((1 << channels) - 1)); |
Xiubo Li | 4355082 | 2013-12-17 11:24:38 +0800 | [diff] [blame] | 425 | |
| 426 | return 0; |
| 427 | } |
| 428 | |
Zidan Wang | c3ecef2 | 2015-05-11 18:24:41 +0800 | [diff] [blame] | 429 | static int fsl_sai_hw_free(struct snd_pcm_substream *substream, |
| 430 | struct snd_soc_dai *cpu_dai) |
| 431 | { |
| 432 | struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai); |
| 433 | bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK; |
| 434 | |
| 435 | if (!sai->is_slave_mode && |
| 436 | sai->mclk_streams & BIT(substream->stream)) { |
| 437 | clk_disable_unprepare(sai->mclk_clk[sai->mclk_id[tx]]); |
| 438 | sai->mclk_streams &= ~BIT(substream->stream); |
| 439 | } |
| 440 | |
| 441 | return 0; |
| 442 | } |
| 443 | |
| 444 | |
Xiubo Li | 4355082 | 2013-12-17 11:24:38 +0800 | [diff] [blame] | 445 | static int fsl_sai_trigger(struct snd_pcm_substream *substream, int cmd, |
| 446 | struct snd_soc_dai *cpu_dai) |
| 447 | { |
| 448 | struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai); |
Nicolin Chen | e6b3984 | 2014-04-01 11:17:06 +0800 | [diff] [blame] | 449 | bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK; |
Nicolin Chen | c44b56a | 2014-07-23 19:23:39 +0800 | [diff] [blame] | 450 | u32 xcsr, count = 100; |
Xiubo Li | 496a39d | 2013-12-31 15:33:21 +0800 | [diff] [blame] | 451 | |
Xiubo Li | a3f7dcc | 2014-02-27 08:45:01 +0800 | [diff] [blame] | 452 | /* |
Nicolin Chen | 08fdf65 | 2014-08-05 15:32:05 +0800 | [diff] [blame] | 453 | * Asynchronous mode: Clear SYNC for both Tx and Rx. |
| 454 | * Rx sync with Tx clocks: Clear SYNC for Tx, set it for Rx. |
| 455 | * Tx sync with Rx clocks: Clear SYNC for Rx, set it for Tx. |
Xiubo Li | a3f7dcc | 2014-02-27 08:45:01 +0800 | [diff] [blame] | 456 | */ |
Nicolin Chen | 855675f | 2014-08-04 15:07:25 +0800 | [diff] [blame] | 457 | regmap_update_bits(sai->regmap, FSL_SAI_TCR2, FSL_SAI_CR2_SYNC, 0); |
Xiubo Li | 78957fc | 2014-02-08 14:38:28 +0800 | [diff] [blame] | 458 | regmap_update_bits(sai->regmap, FSL_SAI_RCR2, FSL_SAI_CR2_SYNC, |
Nicolin Chen | 08fdf65 | 2014-08-05 15:32:05 +0800 | [diff] [blame] | 459 | sai->synchronous[RX] ? FSL_SAI_CR2_SYNC : 0); |
Xiubo Li | 496a39d | 2013-12-31 15:33:21 +0800 | [diff] [blame] | 460 | |
Xiubo Li | a3f7dcc | 2014-02-27 08:45:01 +0800 | [diff] [blame] | 461 | /* |
| 462 | * It is recommended that the transmitter is the last enabled |
| 463 | * and the first disabled. |
| 464 | */ |
Xiubo Li | 4355082 | 2013-12-17 11:24:38 +0800 | [diff] [blame] | 465 | switch (cmd) { |
| 466 | case SNDRV_PCM_TRIGGER_START: |
| 467 | case SNDRV_PCM_TRIGGER_RESUME: |
| 468 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: |
Nicolin Chen | a3fdc67 | 2014-07-23 19:23:40 +0800 | [diff] [blame] | 469 | regmap_update_bits(sai->regmap, FSL_SAI_xCSR(tx), |
| 470 | FSL_SAI_CSR_FRDE, FSL_SAI_CSR_FRDE); |
| 471 | |
Nicolin Chen | f4075a8 | 2014-07-23 19:23:38 +0800 | [diff] [blame] | 472 | regmap_update_bits(sai->regmap, FSL_SAI_RCSR, |
| 473 | FSL_SAI_CSR_TERE, FSL_SAI_CSR_TERE); |
| 474 | regmap_update_bits(sai->regmap, FSL_SAI_TCSR, |
| 475 | FSL_SAI_CSR_TERE, FSL_SAI_CSR_TERE); |
Xiubo Li | e5d0fa9 | 2013-12-25 12:40:04 +0800 | [diff] [blame] | 476 | |
Nicolin Chen | e6b3984 | 2014-04-01 11:17:06 +0800 | [diff] [blame] | 477 | regmap_update_bits(sai->regmap, FSL_SAI_xCSR(tx), |
Nicolin Chen | 8abba5d | 2014-04-01 11:17:07 +0800 | [diff] [blame] | 478 | FSL_SAI_CSR_xIE_MASK, FSL_SAI_FLAGS); |
Xiubo Li | 4355082 | 2013-12-17 11:24:38 +0800 | [diff] [blame] | 479 | break; |
Xiubo Li | 4355082 | 2013-12-17 11:24:38 +0800 | [diff] [blame] | 480 | case SNDRV_PCM_TRIGGER_STOP: |
| 481 | case SNDRV_PCM_TRIGGER_SUSPEND: |
| 482 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
Nicolin Chen | e6b3984 | 2014-04-01 11:17:06 +0800 | [diff] [blame] | 483 | regmap_update_bits(sai->regmap, FSL_SAI_xCSR(tx), |
| 484 | FSL_SAI_CSR_FRDE, 0); |
Nicolin Chen | 8abba5d | 2014-04-01 11:17:07 +0800 | [diff] [blame] | 485 | regmap_update_bits(sai->regmap, FSL_SAI_xCSR(tx), |
| 486 | FSL_SAI_CSR_xIE_MASK, 0); |
Xiubo Li | e5d0fa9 | 2013-12-25 12:40:04 +0800 | [diff] [blame] | 487 | |
Nicolin Chen | f84526c | 2014-04-11 22:10:00 +0800 | [diff] [blame] | 488 | /* Check if the opposite FRDE is also disabled */ |
Nicolin Chen | f4075a8 | 2014-07-23 19:23:38 +0800 | [diff] [blame] | 489 | regmap_read(sai->regmap, FSL_SAI_xCSR(!tx), &xcsr); |
| 490 | if (!(xcsr & FSL_SAI_CSR_FRDE)) { |
Nicolin Chen | eff952b | 2014-07-17 21:21:37 +0800 | [diff] [blame] | 491 | /* Disable both directions and reset their FIFOs */ |
Nicolin Chen | e6b3984 | 2014-04-01 11:17:06 +0800 | [diff] [blame] | 492 | regmap_update_bits(sai->regmap, FSL_SAI_TCSR, |
Nicolin Chen | c44b56a | 2014-07-23 19:23:39 +0800 | [diff] [blame] | 493 | FSL_SAI_CSR_TERE, 0); |
Nicolin Chen | e6b3984 | 2014-04-01 11:17:06 +0800 | [diff] [blame] | 494 | regmap_update_bits(sai->regmap, FSL_SAI_RCSR, |
Nicolin Chen | c44b56a | 2014-07-23 19:23:39 +0800 | [diff] [blame] | 495 | FSL_SAI_CSR_TERE, 0); |
| 496 | |
| 497 | /* TERE will remain set till the end of current frame */ |
| 498 | do { |
| 499 | udelay(10); |
| 500 | regmap_read(sai->regmap, FSL_SAI_xCSR(tx), &xcsr); |
| 501 | } while (--count && xcsr & FSL_SAI_CSR_TERE); |
| 502 | |
| 503 | regmap_update_bits(sai->regmap, FSL_SAI_TCSR, |
| 504 | FSL_SAI_CSR_FR, FSL_SAI_CSR_FR); |
| 505 | regmap_update_bits(sai->regmap, FSL_SAI_RCSR, |
| 506 | FSL_SAI_CSR_FR, FSL_SAI_CSR_FR); |
Nicolin Chen | e6b3984 | 2014-04-01 11:17:06 +0800 | [diff] [blame] | 507 | } |
Xiubo Li | 4355082 | 2013-12-17 11:24:38 +0800 | [diff] [blame] | 508 | break; |
| 509 | default: |
| 510 | return -EINVAL; |
| 511 | } |
| 512 | |
| 513 | return 0; |
| 514 | } |
| 515 | |
| 516 | static int fsl_sai_startup(struct snd_pcm_substream *substream, |
| 517 | struct snd_soc_dai *cpu_dai) |
| 518 | { |
Xiubo Li | 4355082 | 2013-12-17 11:24:38 +0800 | [diff] [blame] | 519 | struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai); |
Nicolin Chen | 2a266f8 | 2014-04-11 18:30:09 +0800 | [diff] [blame] | 520 | bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK; |
Nicolin Chen | ca3e35c | 2014-04-10 23:26:15 +0800 | [diff] [blame] | 521 | struct device *dev = &sai->pdev->dev; |
Nicolin Chen | ca3e35c | 2014-04-10 23:26:15 +0800 | [diff] [blame] | 522 | int ret; |
| 523 | |
| 524 | ret = clk_prepare_enable(sai->bus_clk); |
| 525 | if (ret) { |
| 526 | dev_err(dev, "failed to enable bus clock: %d\n", ret); |
| 527 | return ret; |
| 528 | } |
Xiubo Li | 4355082 | 2013-12-17 11:24:38 +0800 | [diff] [blame] | 529 | |
Nicolin Chen | 2a266f8 | 2014-04-11 18:30:09 +0800 | [diff] [blame] | 530 | regmap_update_bits(sai->regmap, FSL_SAI_xCR3(tx), FSL_SAI_CR3_TRCE, |
Xiubo Li | 78957fc | 2014-02-08 14:38:28 +0800 | [diff] [blame] | 531 | FSL_SAI_CR3_TRCE); |
| 532 | |
Zidan Wang | c5f4823 | 2015-05-11 18:24:43 +0800 | [diff] [blame^] | 533 | ret = snd_pcm_hw_constraint_list(substream->runtime, 0, |
| 534 | SNDRV_PCM_HW_PARAM_RATE, &fsl_sai_rate_constraints); |
| 535 | |
| 536 | return ret; |
Xiubo Li | 4355082 | 2013-12-17 11:24:38 +0800 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | static void fsl_sai_shutdown(struct snd_pcm_substream *substream, |
| 540 | struct snd_soc_dai *cpu_dai) |
| 541 | { |
| 542 | struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai); |
Nicolin Chen | 2a266f8 | 2014-04-11 18:30:09 +0800 | [diff] [blame] | 543 | bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK; |
Xiubo Li | 4355082 | 2013-12-17 11:24:38 +0800 | [diff] [blame] | 544 | |
Nicolin Chen | 2a266f8 | 2014-04-11 18:30:09 +0800 | [diff] [blame] | 545 | regmap_update_bits(sai->regmap, FSL_SAI_xCR3(tx), FSL_SAI_CR3_TRCE, 0); |
Nicolin Chen | ca3e35c | 2014-04-10 23:26:15 +0800 | [diff] [blame] | 546 | |
| 547 | clk_disable_unprepare(sai->bus_clk); |
Xiubo Li | 4355082 | 2013-12-17 11:24:38 +0800 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | static const struct snd_soc_dai_ops fsl_sai_pcm_dai_ops = { |
| 551 | .set_sysclk = fsl_sai_set_dai_sysclk, |
| 552 | .set_fmt = fsl_sai_set_dai_fmt, |
| 553 | .hw_params = fsl_sai_hw_params, |
Zidan Wang | c3ecef2 | 2015-05-11 18:24:41 +0800 | [diff] [blame] | 554 | .hw_free = fsl_sai_hw_free, |
Xiubo Li | 4355082 | 2013-12-17 11:24:38 +0800 | [diff] [blame] | 555 | .trigger = fsl_sai_trigger, |
| 556 | .startup = fsl_sai_startup, |
| 557 | .shutdown = fsl_sai_shutdown, |
| 558 | }; |
| 559 | |
| 560 | static int fsl_sai_dai_probe(struct snd_soc_dai *cpu_dai) |
| 561 | { |
| 562 | struct fsl_sai *sai = dev_get_drvdata(cpu_dai->dev); |
Xiubo Li | e6dc12d | 2013-12-25 11:20:14 +0800 | [diff] [blame] | 563 | |
Nicolin Chen | 376d1a9 | 2014-08-05 17:20:21 +0800 | [diff] [blame] | 564 | /* Software Reset for both Tx and Rx */ |
| 565 | regmap_write(sai->regmap, FSL_SAI_TCSR, FSL_SAI_CSR_SR); |
| 566 | regmap_write(sai->regmap, FSL_SAI_RCSR, FSL_SAI_CSR_SR); |
| 567 | /* Clear SR bit to finish the reset */ |
| 568 | regmap_write(sai->regmap, FSL_SAI_TCSR, 0); |
| 569 | regmap_write(sai->regmap, FSL_SAI_RCSR, 0); |
| 570 | |
Xiubo Li | 78957fc | 2014-02-08 14:38:28 +0800 | [diff] [blame] | 571 | regmap_update_bits(sai->regmap, FSL_SAI_TCR1, FSL_SAI_CR1_RFW_MASK, |
| 572 | FSL_SAI_MAXBURST_TX * 2); |
| 573 | regmap_update_bits(sai->regmap, FSL_SAI_RCR1, FSL_SAI_CR1_RFW_MASK, |
| 574 | FSL_SAI_MAXBURST_RX - 1); |
Xiubo Li | 4355082 | 2013-12-17 11:24:38 +0800 | [diff] [blame] | 575 | |
Xiubo Li | dd9f406 | 2013-12-20 12:35:33 +0800 | [diff] [blame] | 576 | snd_soc_dai_init_dma_data(cpu_dai, &sai->dma_params_tx, |
| 577 | &sai->dma_params_rx); |
Xiubo Li | 4355082 | 2013-12-17 11:24:38 +0800 | [diff] [blame] | 578 | |
| 579 | snd_soc_dai_set_drvdata(cpu_dai, sai); |
| 580 | |
| 581 | return 0; |
| 582 | } |
| 583 | |
Xiubo Li | 4355082 | 2013-12-17 11:24:38 +0800 | [diff] [blame] | 584 | static struct snd_soc_dai_driver fsl_sai_dai = { |
| 585 | .probe = fsl_sai_dai_probe, |
Xiubo Li | 4355082 | 2013-12-17 11:24:38 +0800 | [diff] [blame] | 586 | .playback = { |
Nicolin Chen | 20d5b76 | 2014-07-30 11:10:27 +0800 | [diff] [blame] | 587 | .stream_name = "CPU-Playback", |
Xiubo Li | 4355082 | 2013-12-17 11:24:38 +0800 | [diff] [blame] | 588 | .channels_min = 1, |
| 589 | .channels_max = 2, |
Zidan Wang | c5f4823 | 2015-05-11 18:24:43 +0800 | [diff] [blame^] | 590 | .rate_min = 8000, |
| 591 | .rate_max = 192000, |
| 592 | .rates = SNDRV_PCM_RATE_KNOT, |
Xiubo Li | 4355082 | 2013-12-17 11:24:38 +0800 | [diff] [blame] | 593 | .formats = FSL_SAI_FORMATS, |
| 594 | }, |
| 595 | .capture = { |
Nicolin Chen | 20d5b76 | 2014-07-30 11:10:27 +0800 | [diff] [blame] | 596 | .stream_name = "CPU-Capture", |
Xiubo Li | 4355082 | 2013-12-17 11:24:38 +0800 | [diff] [blame] | 597 | .channels_min = 1, |
| 598 | .channels_max = 2, |
Zidan Wang | c5f4823 | 2015-05-11 18:24:43 +0800 | [diff] [blame^] | 599 | .rate_min = 8000, |
| 600 | .rate_max = 192000, |
| 601 | .rates = SNDRV_PCM_RATE_KNOT, |
Xiubo Li | 4355082 | 2013-12-17 11:24:38 +0800 | [diff] [blame] | 602 | .formats = FSL_SAI_FORMATS, |
| 603 | }, |
| 604 | .ops = &fsl_sai_pcm_dai_ops, |
| 605 | }; |
| 606 | |
| 607 | static const struct snd_soc_component_driver fsl_component = { |
| 608 | .name = "fsl-sai", |
| 609 | }; |
| 610 | |
Xiubo Li | 78957fc | 2014-02-08 14:38:28 +0800 | [diff] [blame] | 611 | static bool fsl_sai_readable_reg(struct device *dev, unsigned int reg) |
| 612 | { |
| 613 | switch (reg) { |
| 614 | case FSL_SAI_TCSR: |
| 615 | case FSL_SAI_TCR1: |
| 616 | case FSL_SAI_TCR2: |
| 617 | case FSL_SAI_TCR3: |
| 618 | case FSL_SAI_TCR4: |
| 619 | case FSL_SAI_TCR5: |
| 620 | case FSL_SAI_TFR: |
| 621 | case FSL_SAI_TMR: |
| 622 | case FSL_SAI_RCSR: |
| 623 | case FSL_SAI_RCR1: |
| 624 | case FSL_SAI_RCR2: |
| 625 | case FSL_SAI_RCR3: |
| 626 | case FSL_SAI_RCR4: |
| 627 | case FSL_SAI_RCR5: |
| 628 | case FSL_SAI_RDR: |
| 629 | case FSL_SAI_RFR: |
| 630 | case FSL_SAI_RMR: |
| 631 | return true; |
| 632 | default: |
| 633 | return false; |
| 634 | } |
| 635 | } |
| 636 | |
| 637 | static bool fsl_sai_volatile_reg(struct device *dev, unsigned int reg) |
| 638 | { |
| 639 | switch (reg) { |
| 640 | case FSL_SAI_TFR: |
| 641 | case FSL_SAI_RFR: |
| 642 | case FSL_SAI_TDR: |
| 643 | case FSL_SAI_RDR: |
| 644 | return true; |
| 645 | default: |
| 646 | return false; |
| 647 | } |
| 648 | |
| 649 | } |
| 650 | |
| 651 | static bool fsl_sai_writeable_reg(struct device *dev, unsigned int reg) |
| 652 | { |
| 653 | switch (reg) { |
| 654 | case FSL_SAI_TCSR: |
| 655 | case FSL_SAI_TCR1: |
| 656 | case FSL_SAI_TCR2: |
| 657 | case FSL_SAI_TCR3: |
| 658 | case FSL_SAI_TCR4: |
| 659 | case FSL_SAI_TCR5: |
| 660 | case FSL_SAI_TDR: |
| 661 | case FSL_SAI_TMR: |
| 662 | case FSL_SAI_RCSR: |
| 663 | case FSL_SAI_RCR1: |
| 664 | case FSL_SAI_RCR2: |
| 665 | case FSL_SAI_RCR3: |
| 666 | case FSL_SAI_RCR4: |
| 667 | case FSL_SAI_RCR5: |
| 668 | case FSL_SAI_RMR: |
| 669 | return true; |
| 670 | default: |
| 671 | return false; |
| 672 | } |
| 673 | } |
| 674 | |
Xiubo Li | 014fd22 | 2014-08-25 11:31:02 +0800 | [diff] [blame] | 675 | static const struct regmap_config fsl_sai_regmap_config = { |
Xiubo Li | 78957fc | 2014-02-08 14:38:28 +0800 | [diff] [blame] | 676 | .reg_bits = 32, |
| 677 | .reg_stride = 4, |
| 678 | .val_bits = 32, |
| 679 | |
| 680 | .max_register = FSL_SAI_RMR, |
| 681 | .readable_reg = fsl_sai_readable_reg, |
| 682 | .volatile_reg = fsl_sai_volatile_reg, |
| 683 | .writeable_reg = fsl_sai_writeable_reg, |
| 684 | }; |
| 685 | |
Xiubo Li | 4355082 | 2013-12-17 11:24:38 +0800 | [diff] [blame] | 686 | static int fsl_sai_probe(struct platform_device *pdev) |
| 687 | { |
Nicolin Chen | 4e3a99f | 2013-12-20 16:41:05 +0800 | [diff] [blame] | 688 | struct device_node *np = pdev->dev.of_node; |
Xiubo Li | 4355082 | 2013-12-17 11:24:38 +0800 | [diff] [blame] | 689 | struct fsl_sai *sai; |
| 690 | struct resource *res; |
Xiubo Li | 78957fc | 2014-02-08 14:38:28 +0800 | [diff] [blame] | 691 | void __iomem *base; |
Nicolin Chen | ca3e35c | 2014-04-10 23:26:15 +0800 | [diff] [blame] | 692 | char tmp[8]; |
| 693 | int irq, ret, i; |
Xiubo Li | 4355082 | 2013-12-17 11:24:38 +0800 | [diff] [blame] | 694 | |
| 695 | sai = devm_kzalloc(&pdev->dev, sizeof(*sai), GFP_KERNEL); |
| 696 | if (!sai) |
| 697 | return -ENOMEM; |
| 698 | |
Nicolin Chen | e2681a1 | 2014-03-27 19:06:59 +0800 | [diff] [blame] | 699 | sai->pdev = pdev; |
| 700 | |
Nicolin Chen | c754064 | 2014-04-01 19:34:09 +0800 | [diff] [blame] | 701 | if (of_device_is_compatible(pdev->dev.of_node, "fsl,imx6sx-sai")) |
| 702 | sai->sai_on_imx = true; |
| 703 | |
Xiubo Li | eadb001 | 2014-08-29 15:12:12 +0800 | [diff] [blame] | 704 | sai->is_lsb_first = of_property_read_bool(np, "lsb-first"); |
Xiubo Li | 78957fc | 2014-02-08 14:38:28 +0800 | [diff] [blame] | 705 | |
| 706 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 707 | base = devm_ioremap_resource(&pdev->dev, res); |
| 708 | if (IS_ERR(base)) |
| 709 | return PTR_ERR(base); |
| 710 | |
| 711 | sai->regmap = devm_regmap_init_mmio_clk(&pdev->dev, |
Nicolin Chen | ca3e35c | 2014-04-10 23:26:15 +0800 | [diff] [blame] | 712 | "bus", base, &fsl_sai_regmap_config); |
| 713 | |
| 714 | /* Compatible with old DTB cases */ |
| 715 | if (IS_ERR(sai->regmap)) |
| 716 | sai->regmap = devm_regmap_init_mmio_clk(&pdev->dev, |
| 717 | "sai", base, &fsl_sai_regmap_config); |
Xiubo Li | 78957fc | 2014-02-08 14:38:28 +0800 | [diff] [blame] | 718 | if (IS_ERR(sai->regmap)) { |
| 719 | dev_err(&pdev->dev, "regmap init failed\n"); |
| 720 | return PTR_ERR(sai->regmap); |
Xiubo Li | 4355082 | 2013-12-17 11:24:38 +0800 | [diff] [blame] | 721 | } |
| 722 | |
Nicolin Chen | ca3e35c | 2014-04-10 23:26:15 +0800 | [diff] [blame] | 723 | /* No error out for old DTB cases but only mark the clock NULL */ |
| 724 | sai->bus_clk = devm_clk_get(&pdev->dev, "bus"); |
| 725 | if (IS_ERR(sai->bus_clk)) { |
| 726 | dev_err(&pdev->dev, "failed to get bus clock: %ld\n", |
| 727 | PTR_ERR(sai->bus_clk)); |
| 728 | sai->bus_clk = NULL; |
| 729 | } |
| 730 | |
Zidan Wang | c3ecef2 | 2015-05-11 18:24:41 +0800 | [diff] [blame] | 731 | sai->mclk_clk[0] = sai->bus_clk; |
| 732 | for (i = 1; i < FSL_SAI_MCLK_MAX; i++) { |
| 733 | sprintf(tmp, "mclk%d", i); |
Nicolin Chen | ca3e35c | 2014-04-10 23:26:15 +0800 | [diff] [blame] | 734 | sai->mclk_clk[i] = devm_clk_get(&pdev->dev, tmp); |
| 735 | if (IS_ERR(sai->mclk_clk[i])) { |
| 736 | dev_err(&pdev->dev, "failed to get mclk%d clock: %ld\n", |
| 737 | i + 1, PTR_ERR(sai->mclk_clk[i])); |
| 738 | sai->mclk_clk[i] = NULL; |
| 739 | } |
| 740 | } |
| 741 | |
Nicolin Chen | e2681a1 | 2014-03-27 19:06:59 +0800 | [diff] [blame] | 742 | irq = platform_get_irq(pdev, 0); |
| 743 | if (irq < 0) { |
Fabio Estevam | 0954237 | 2015-01-07 13:44:34 -0200 | [diff] [blame] | 744 | dev_err(&pdev->dev, "no irq for node %s\n", pdev->name); |
Nicolin Chen | e2681a1 | 2014-03-27 19:06:59 +0800 | [diff] [blame] | 745 | return irq; |
| 746 | } |
| 747 | |
| 748 | ret = devm_request_irq(&pdev->dev, irq, fsl_sai_isr, 0, np->name, sai); |
| 749 | if (ret) { |
| 750 | dev_err(&pdev->dev, "failed to claim irq %u\n", irq); |
| 751 | return ret; |
| 752 | } |
| 753 | |
Nicolin Chen | 08fdf65 | 2014-08-05 15:32:05 +0800 | [diff] [blame] | 754 | /* Sync Tx with Rx as default by following old DT binding */ |
| 755 | sai->synchronous[RX] = true; |
| 756 | sai->synchronous[TX] = false; |
| 757 | fsl_sai_dai.symmetric_rates = 1; |
| 758 | fsl_sai_dai.symmetric_channels = 1; |
| 759 | fsl_sai_dai.symmetric_samplebits = 1; |
| 760 | |
Nicolin Chen | ce7344a | 2014-08-08 18:41:19 +0800 | [diff] [blame] | 761 | if (of_find_property(np, "fsl,sai-synchronous-rx", NULL) && |
| 762 | of_find_property(np, "fsl,sai-asynchronous", NULL)) { |
| 763 | /* error out if both synchronous and asynchronous are present */ |
| 764 | dev_err(&pdev->dev, "invalid binding for synchronous mode\n"); |
| 765 | return -EINVAL; |
| 766 | } |
| 767 | |
Nicolin Chen | 08fdf65 | 2014-08-05 15:32:05 +0800 | [diff] [blame] | 768 | if (of_find_property(np, "fsl,sai-synchronous-rx", NULL)) { |
| 769 | /* Sync Rx with Tx */ |
| 770 | sai->synchronous[RX] = false; |
| 771 | sai->synchronous[TX] = true; |
| 772 | } else if (of_find_property(np, "fsl,sai-asynchronous", NULL)) { |
| 773 | /* Discard all settings for asynchronous mode */ |
| 774 | sai->synchronous[RX] = false; |
| 775 | sai->synchronous[TX] = false; |
| 776 | fsl_sai_dai.symmetric_rates = 0; |
| 777 | fsl_sai_dai.symmetric_channels = 0; |
| 778 | fsl_sai_dai.symmetric_samplebits = 0; |
| 779 | } |
| 780 | |
Xiubo Li | 4355082 | 2013-12-17 11:24:38 +0800 | [diff] [blame] | 781 | sai->dma_params_rx.addr = res->start + FSL_SAI_RDR; |
| 782 | sai->dma_params_tx.addr = res->start + FSL_SAI_TDR; |
| 783 | sai->dma_params_rx.maxburst = FSL_SAI_MAXBURST_RX; |
| 784 | sai->dma_params_tx.maxburst = FSL_SAI_MAXBURST_TX; |
| 785 | |
Xiubo Li | 4355082 | 2013-12-17 11:24:38 +0800 | [diff] [blame] | 786 | platform_set_drvdata(pdev, sai); |
| 787 | |
| 788 | ret = devm_snd_soc_register_component(&pdev->dev, &fsl_component, |
| 789 | &fsl_sai_dai, 1); |
| 790 | if (ret) |
| 791 | return ret; |
| 792 | |
Nicolin Chen | c754064 | 2014-04-01 19:34:09 +0800 | [diff] [blame] | 793 | if (sai->sai_on_imx) |
| 794 | return imx_pcm_dma_init(pdev); |
| 795 | else |
| 796 | return devm_snd_dmaengine_pcm_register(&pdev->dev, NULL, |
| 797 | SND_DMAENGINE_PCM_FLAG_NO_RESIDUE); |
Xiubo Li | 4355082 | 2013-12-17 11:24:38 +0800 | [diff] [blame] | 798 | } |
| 799 | |
| 800 | static const struct of_device_id fsl_sai_ids[] = { |
| 801 | { .compatible = "fsl,vf610-sai", }, |
Nicolin Chen | c754064 | 2014-04-01 19:34:09 +0800 | [diff] [blame] | 802 | { .compatible = "fsl,imx6sx-sai", }, |
Xiubo Li | 4355082 | 2013-12-17 11:24:38 +0800 | [diff] [blame] | 803 | { /* sentinel */ } |
| 804 | }; |
| 805 | |
| 806 | static struct platform_driver fsl_sai_driver = { |
| 807 | .probe = fsl_sai_probe, |
Xiubo Li | 4355082 | 2013-12-17 11:24:38 +0800 | [diff] [blame] | 808 | .driver = { |
| 809 | .name = "fsl-sai", |
Xiubo Li | 4355082 | 2013-12-17 11:24:38 +0800 | [diff] [blame] | 810 | .of_match_table = fsl_sai_ids, |
| 811 | }, |
| 812 | }; |
| 813 | module_platform_driver(fsl_sai_driver); |
| 814 | |
| 815 | MODULE_DESCRIPTION("Freescale Soc SAI Interface"); |
| 816 | MODULE_AUTHOR("Xiubo Li, <Li.Xiubo@freescale.com>"); |
| 817 | MODULE_ALIAS("platform:fsl-sai"); |
| 818 | MODULE_LICENSE("GPL"); |