Thomas Gleixner | 2b27bdc | 2019-05-29 16:57:50 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 2 | /* |
| 3 | * omap-mcbsp.c -- OMAP ALSA SoC DAI driver using McBSP port |
| 4 | * |
| 5 | * Copyright (C) 2008 Nokia Corporation |
| 6 | * |
Jarkko Nikula | 7ec41ee | 2011-08-11 15:44:57 +0300 | [diff] [blame] | 7 | * Contact: Jarkko Nikula <jarkko.nikula@bitmer.com> |
Peter Ujfalusi | 56a8742 | 2011-05-03 18:14:06 +0300 | [diff] [blame] | 8 | * Peter Ujfalusi <peter.ujfalusi@ti.com> |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include <linux/init.h> |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/device.h> |
Peter Ujfalusi | 2ee6595 | 2012-02-14 14:52:42 +0200 | [diff] [blame] | 14 | #include <linux/pm_runtime.h> |
Peter Ujfalusi | 11dd586 | 2012-08-16 16:41:08 +0300 | [diff] [blame] | 15 | #include <linux/of.h> |
| 16 | #include <linux/of_device.h> |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 17 | #include <sound/core.h> |
| 18 | #include <sound/pcm.h> |
| 19 | #include <sound/pcm_params.h> |
| 20 | #include <sound/initval.h> |
| 21 | #include <sound/soc.h> |
Lars-Peter Clausen | 09ae3aa | 2013-04-03 11:06:05 +0200 | [diff] [blame] | 22 | #include <sound/dmaengine_pcm.h> |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 23 | |
Peter Ujfalusi | 9c34d02 | 2018-11-08 09:29:58 +0200 | [diff] [blame] | 24 | #include "omap-mcbsp-priv.h" |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 25 | #include "omap-mcbsp.h" |
Peter Ujfalusi | 0198d7b | 2018-05-07 11:49:59 +0300 | [diff] [blame] | 26 | #include "sdma-pcm.h" |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 27 | |
Jarkko Nikula | 0b60485 | 2008-11-12 17:05:51 +0200 | [diff] [blame] | 28 | #define OMAP_MCBSP_RATES (SNDRV_PCM_RATE_8000_96000) |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 29 | |
Peter Ujfalusi | 219f431 | 2012-02-03 13:11:47 +0200 | [diff] [blame] | 30 | enum { |
| 31 | OMAP_MCBSP_WORD_8 = 0, |
| 32 | OMAP_MCBSP_WORD_12, |
| 33 | OMAP_MCBSP_WORD_16, |
| 34 | OMAP_MCBSP_WORD_20, |
| 35 | OMAP_MCBSP_WORD_24, |
| 36 | OMAP_MCBSP_WORD_32, |
| 37 | }; |
| 38 | |
Peter Ujfalusi | 9c34d02 | 2018-11-08 09:29:58 +0200 | [diff] [blame] | 39 | static void omap_mcbsp_dump_reg(struct omap_mcbsp *mcbsp) |
| 40 | { |
| 41 | dev_dbg(mcbsp->dev, "**** McBSP%d regs ****\n", mcbsp->id); |
| 42 | dev_dbg(mcbsp->dev, "DRR2: 0x%04x\n", MCBSP_READ(mcbsp, DRR2)); |
| 43 | dev_dbg(mcbsp->dev, "DRR1: 0x%04x\n", MCBSP_READ(mcbsp, DRR1)); |
| 44 | dev_dbg(mcbsp->dev, "DXR2: 0x%04x\n", MCBSP_READ(mcbsp, DXR2)); |
| 45 | dev_dbg(mcbsp->dev, "DXR1: 0x%04x\n", MCBSP_READ(mcbsp, DXR1)); |
| 46 | dev_dbg(mcbsp->dev, "SPCR2: 0x%04x\n", MCBSP_READ(mcbsp, SPCR2)); |
| 47 | dev_dbg(mcbsp->dev, "SPCR1: 0x%04x\n", MCBSP_READ(mcbsp, SPCR1)); |
| 48 | dev_dbg(mcbsp->dev, "RCR2: 0x%04x\n", MCBSP_READ(mcbsp, RCR2)); |
| 49 | dev_dbg(mcbsp->dev, "RCR1: 0x%04x\n", MCBSP_READ(mcbsp, RCR1)); |
| 50 | dev_dbg(mcbsp->dev, "XCR2: 0x%04x\n", MCBSP_READ(mcbsp, XCR2)); |
| 51 | dev_dbg(mcbsp->dev, "XCR1: 0x%04x\n", MCBSP_READ(mcbsp, XCR1)); |
| 52 | dev_dbg(mcbsp->dev, "SRGR2: 0x%04x\n", MCBSP_READ(mcbsp, SRGR2)); |
| 53 | dev_dbg(mcbsp->dev, "SRGR1: 0x%04x\n", MCBSP_READ(mcbsp, SRGR1)); |
| 54 | dev_dbg(mcbsp->dev, "PCR0: 0x%04x\n", MCBSP_READ(mcbsp, PCR0)); |
| 55 | dev_dbg(mcbsp->dev, "***********************\n"); |
| 56 | } |
| 57 | |
| 58 | static int omap2_mcbsp_set_clks_src(struct omap_mcbsp *mcbsp, u8 fck_src_id) |
| 59 | { |
| 60 | struct clk *fck_src; |
| 61 | const char *src; |
| 62 | int r; |
| 63 | |
| 64 | if (fck_src_id == MCBSP_CLKS_PAD_SRC) |
| 65 | src = "pad_fck"; |
| 66 | else if (fck_src_id == MCBSP_CLKS_PRCM_SRC) |
| 67 | src = "prcm_fck"; |
| 68 | else |
| 69 | return -EINVAL; |
| 70 | |
| 71 | fck_src = clk_get(mcbsp->dev, src); |
| 72 | if (IS_ERR(fck_src)) { |
| 73 | dev_err(mcbsp->dev, "CLKS: could not clk_get() %s\n", src); |
| 74 | return -EINVAL; |
| 75 | } |
| 76 | |
| 77 | pm_runtime_put_sync(mcbsp->dev); |
| 78 | |
| 79 | r = clk_set_parent(mcbsp->fclk, fck_src); |
| 80 | if (r) { |
| 81 | dev_err(mcbsp->dev, "CLKS: could not clk_set_parent() to %s\n", |
| 82 | src); |
| 83 | clk_put(fck_src); |
| 84 | return r; |
| 85 | } |
| 86 | |
| 87 | pm_runtime_get_sync(mcbsp->dev); |
| 88 | |
| 89 | clk_put(fck_src); |
| 90 | |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | static irqreturn_t omap_mcbsp_irq_handler(int irq, void *data) |
| 95 | { |
| 96 | struct omap_mcbsp *mcbsp = data; |
| 97 | u16 irqst; |
| 98 | |
| 99 | irqst = MCBSP_READ(mcbsp, IRQST); |
| 100 | dev_dbg(mcbsp->dev, "IRQ callback : 0x%x\n", irqst); |
| 101 | |
| 102 | if (irqst & RSYNCERREN) |
| 103 | dev_err(mcbsp->dev, "RX Frame Sync Error!\n"); |
| 104 | if (irqst & RFSREN) |
| 105 | dev_dbg(mcbsp->dev, "RX Frame Sync\n"); |
| 106 | if (irqst & REOFEN) |
| 107 | dev_dbg(mcbsp->dev, "RX End Of Frame\n"); |
| 108 | if (irqst & RRDYEN) |
| 109 | dev_dbg(mcbsp->dev, "RX Buffer Threshold Reached\n"); |
| 110 | if (irqst & RUNDFLEN) |
| 111 | dev_err(mcbsp->dev, "RX Buffer Underflow!\n"); |
| 112 | if (irqst & ROVFLEN) |
| 113 | dev_err(mcbsp->dev, "RX Buffer Overflow!\n"); |
| 114 | |
| 115 | if (irqst & XSYNCERREN) |
| 116 | dev_err(mcbsp->dev, "TX Frame Sync Error!\n"); |
| 117 | if (irqst & XFSXEN) |
| 118 | dev_dbg(mcbsp->dev, "TX Frame Sync\n"); |
| 119 | if (irqst & XEOFEN) |
| 120 | dev_dbg(mcbsp->dev, "TX End Of Frame\n"); |
| 121 | if (irqst & XRDYEN) |
| 122 | dev_dbg(mcbsp->dev, "TX Buffer threshold Reached\n"); |
| 123 | if (irqst & XUNDFLEN) |
| 124 | dev_err(mcbsp->dev, "TX Buffer Underflow!\n"); |
| 125 | if (irqst & XOVFLEN) |
| 126 | dev_err(mcbsp->dev, "TX Buffer Overflow!\n"); |
| 127 | if (irqst & XEMPTYEOFEN) |
| 128 | dev_dbg(mcbsp->dev, "TX Buffer empty at end of frame\n"); |
| 129 | |
| 130 | MCBSP_WRITE(mcbsp, IRQST, irqst); |
| 131 | |
| 132 | return IRQ_HANDLED; |
| 133 | } |
| 134 | |
| 135 | static irqreturn_t omap_mcbsp_tx_irq_handler(int irq, void *data) |
| 136 | { |
| 137 | struct omap_mcbsp *mcbsp = data; |
| 138 | u16 irqst_spcr2; |
| 139 | |
| 140 | irqst_spcr2 = MCBSP_READ(mcbsp, SPCR2); |
| 141 | dev_dbg(mcbsp->dev, "TX IRQ callback : 0x%x\n", irqst_spcr2); |
| 142 | |
| 143 | if (irqst_spcr2 & XSYNC_ERR) { |
| 144 | dev_err(mcbsp->dev, "TX Frame Sync Error! : 0x%x\n", |
| 145 | irqst_spcr2); |
| 146 | /* Writing zero to XSYNC_ERR clears the IRQ */ |
| 147 | MCBSP_WRITE(mcbsp, SPCR2, MCBSP_READ_CACHE(mcbsp, SPCR2)); |
| 148 | } |
| 149 | |
| 150 | return IRQ_HANDLED; |
| 151 | } |
| 152 | |
| 153 | static irqreturn_t omap_mcbsp_rx_irq_handler(int irq, void *data) |
| 154 | { |
| 155 | struct omap_mcbsp *mcbsp = data; |
| 156 | u16 irqst_spcr1; |
| 157 | |
| 158 | irqst_spcr1 = MCBSP_READ(mcbsp, SPCR1); |
| 159 | dev_dbg(mcbsp->dev, "RX IRQ callback : 0x%x\n", irqst_spcr1); |
| 160 | |
| 161 | if (irqst_spcr1 & RSYNC_ERR) { |
| 162 | dev_err(mcbsp->dev, "RX Frame Sync Error! : 0x%x\n", |
| 163 | irqst_spcr1); |
| 164 | /* Writing zero to RSYNC_ERR clears the IRQ */ |
| 165 | MCBSP_WRITE(mcbsp, SPCR1, MCBSP_READ_CACHE(mcbsp, SPCR1)); |
| 166 | } |
| 167 | |
| 168 | return IRQ_HANDLED; |
| 169 | } |
| 170 | |
| 171 | /* |
| 172 | * omap_mcbsp_config simply write a config to the |
| 173 | * appropriate McBSP. |
| 174 | * You either call this function or set the McBSP registers |
| 175 | * by yourself before calling omap_mcbsp_start(). |
| 176 | */ |
| 177 | static void omap_mcbsp_config(struct omap_mcbsp *mcbsp, |
| 178 | const struct omap_mcbsp_reg_cfg *config) |
| 179 | { |
| 180 | dev_dbg(mcbsp->dev, "Configuring McBSP%d phys_base: 0x%08lx\n", |
| 181 | mcbsp->id, mcbsp->phys_base); |
| 182 | |
| 183 | /* We write the given config */ |
| 184 | MCBSP_WRITE(mcbsp, SPCR2, config->spcr2); |
| 185 | MCBSP_WRITE(mcbsp, SPCR1, config->spcr1); |
| 186 | MCBSP_WRITE(mcbsp, RCR2, config->rcr2); |
| 187 | MCBSP_WRITE(mcbsp, RCR1, config->rcr1); |
| 188 | MCBSP_WRITE(mcbsp, XCR2, config->xcr2); |
| 189 | MCBSP_WRITE(mcbsp, XCR1, config->xcr1); |
| 190 | MCBSP_WRITE(mcbsp, SRGR2, config->srgr2); |
| 191 | MCBSP_WRITE(mcbsp, SRGR1, config->srgr1); |
| 192 | MCBSP_WRITE(mcbsp, MCR2, config->mcr2); |
| 193 | MCBSP_WRITE(mcbsp, MCR1, config->mcr1); |
| 194 | MCBSP_WRITE(mcbsp, PCR0, config->pcr0); |
| 195 | if (mcbsp->pdata->has_ccr) { |
| 196 | MCBSP_WRITE(mcbsp, XCCR, config->xccr); |
| 197 | MCBSP_WRITE(mcbsp, RCCR, config->rccr); |
| 198 | } |
| 199 | /* Enable wakeup behavior */ |
| 200 | if (mcbsp->pdata->has_wakeup) |
| 201 | MCBSP_WRITE(mcbsp, WAKEUPEN, XRDYEN | RRDYEN); |
| 202 | |
| 203 | /* Enable TX/RX sync error interrupts by default */ |
| 204 | if (mcbsp->irq) |
| 205 | MCBSP_WRITE(mcbsp, IRQEN, RSYNCERREN | XSYNCERREN | |
| 206 | RUNDFLEN | ROVFLEN | XUNDFLEN | XOVFLEN); |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * omap_mcbsp_dma_reg_params - returns the address of mcbsp data register |
| 211 | * @mcbsp: omap_mcbsp struct for the McBSP instance |
| 212 | * @stream: Stream direction (playback/capture) |
| 213 | * |
| 214 | * Returns the address of mcbsp data transmit register or data receive register |
| 215 | * to be used by DMA for transferring/receiving data |
| 216 | */ |
| 217 | static int omap_mcbsp_dma_reg_params(struct omap_mcbsp *mcbsp, |
| 218 | unsigned int stream) |
| 219 | { |
| 220 | int data_reg; |
| 221 | |
| 222 | if (stream == SNDRV_PCM_STREAM_PLAYBACK) { |
| 223 | if (mcbsp->pdata->reg_size == 2) |
| 224 | data_reg = OMAP_MCBSP_REG_DXR1; |
| 225 | else |
| 226 | data_reg = OMAP_MCBSP_REG_DXR; |
| 227 | } else { |
| 228 | if (mcbsp->pdata->reg_size == 2) |
| 229 | data_reg = OMAP_MCBSP_REG_DRR1; |
| 230 | else |
| 231 | data_reg = OMAP_MCBSP_REG_DRR; |
| 232 | } |
| 233 | |
| 234 | return mcbsp->phys_dma_base + data_reg * mcbsp->pdata->reg_step; |
| 235 | } |
| 236 | |
| 237 | /* |
| 238 | * omap_mcbsp_set_rx_threshold configures the transmit threshold in words. |
| 239 | * The threshold parameter is 1 based, and it is converted (threshold - 1) |
| 240 | * for the THRSH2 register. |
| 241 | */ |
| 242 | static void omap_mcbsp_set_tx_threshold(struct omap_mcbsp *mcbsp, u16 threshold) |
| 243 | { |
| 244 | if (threshold && threshold <= mcbsp->max_tx_thres) |
| 245 | MCBSP_WRITE(mcbsp, THRSH2, threshold - 1); |
| 246 | } |
| 247 | |
| 248 | /* |
| 249 | * omap_mcbsp_set_rx_threshold configures the receive threshold in words. |
| 250 | * The threshold parameter is 1 based, and it is converted (threshold - 1) |
| 251 | * for the THRSH1 register. |
| 252 | */ |
| 253 | static void omap_mcbsp_set_rx_threshold(struct omap_mcbsp *mcbsp, u16 threshold) |
| 254 | { |
| 255 | if (threshold && threshold <= mcbsp->max_rx_thres) |
| 256 | MCBSP_WRITE(mcbsp, THRSH1, threshold - 1); |
| 257 | } |
| 258 | |
| 259 | /* |
| 260 | * omap_mcbsp_get_tx_delay returns the number of used slots in the McBSP FIFO |
| 261 | */ |
| 262 | static u16 omap_mcbsp_get_tx_delay(struct omap_mcbsp *mcbsp) |
| 263 | { |
| 264 | u16 buffstat; |
| 265 | |
| 266 | /* Returns the number of free locations in the buffer */ |
| 267 | buffstat = MCBSP_READ(mcbsp, XBUFFSTAT); |
| 268 | |
| 269 | /* Number of slots are different in McBSP ports */ |
| 270 | return mcbsp->pdata->buffer_size - buffstat; |
| 271 | } |
| 272 | |
| 273 | /* |
| 274 | * omap_mcbsp_get_rx_delay returns the number of free slots in the McBSP FIFO |
| 275 | * to reach the threshold value (when the DMA will be triggered to read it) |
| 276 | */ |
| 277 | static u16 omap_mcbsp_get_rx_delay(struct omap_mcbsp *mcbsp) |
| 278 | { |
| 279 | u16 buffstat, threshold; |
| 280 | |
| 281 | /* Returns the number of used locations in the buffer */ |
| 282 | buffstat = MCBSP_READ(mcbsp, RBUFFSTAT); |
| 283 | /* RX threshold */ |
| 284 | threshold = MCBSP_READ(mcbsp, THRSH1); |
| 285 | |
| 286 | /* Return the number of location till we reach the threshold limit */ |
| 287 | if (threshold <= buffstat) |
| 288 | return 0; |
| 289 | else |
| 290 | return threshold - buffstat; |
| 291 | } |
| 292 | |
| 293 | static int omap_mcbsp_request(struct omap_mcbsp *mcbsp) |
| 294 | { |
| 295 | void *reg_cache; |
| 296 | int err; |
| 297 | |
| 298 | reg_cache = kzalloc(mcbsp->reg_cache_size, GFP_KERNEL); |
| 299 | if (!reg_cache) |
| 300 | return -ENOMEM; |
| 301 | |
| 302 | spin_lock(&mcbsp->lock); |
| 303 | if (!mcbsp->free) { |
| 304 | dev_err(mcbsp->dev, "McBSP%d is currently in use\n", mcbsp->id); |
| 305 | err = -EBUSY; |
| 306 | goto err_kfree; |
| 307 | } |
| 308 | |
| 309 | mcbsp->free = false; |
| 310 | mcbsp->reg_cache = reg_cache; |
| 311 | spin_unlock(&mcbsp->lock); |
| 312 | |
Peter Ujfalusi | 465d85b | 2018-11-08 09:29:59 +0200 | [diff] [blame] | 313 | if(mcbsp->pdata->ops && mcbsp->pdata->ops->request) |
Peter Ujfalusi | 9c34d02 | 2018-11-08 09:29:58 +0200 | [diff] [blame] | 314 | mcbsp->pdata->ops->request(mcbsp->id - 1); |
| 315 | |
| 316 | /* |
| 317 | * Make sure that transmitter, receiver and sample-rate generator are |
| 318 | * not running before activating IRQs. |
| 319 | */ |
| 320 | MCBSP_WRITE(mcbsp, SPCR1, 0); |
| 321 | MCBSP_WRITE(mcbsp, SPCR2, 0); |
| 322 | |
| 323 | if (mcbsp->irq) { |
| 324 | err = request_irq(mcbsp->irq, omap_mcbsp_irq_handler, 0, |
| 325 | "McBSP", (void *)mcbsp); |
| 326 | if (err != 0) { |
| 327 | dev_err(mcbsp->dev, "Unable to request IRQ\n"); |
| 328 | goto err_clk_disable; |
| 329 | } |
| 330 | } else { |
| 331 | err = request_irq(mcbsp->tx_irq, omap_mcbsp_tx_irq_handler, 0, |
| 332 | "McBSP TX", (void *)mcbsp); |
| 333 | if (err != 0) { |
| 334 | dev_err(mcbsp->dev, "Unable to request TX IRQ\n"); |
| 335 | goto err_clk_disable; |
| 336 | } |
| 337 | |
| 338 | err = request_irq(mcbsp->rx_irq, omap_mcbsp_rx_irq_handler, 0, |
| 339 | "McBSP RX", (void *)mcbsp); |
| 340 | if (err != 0) { |
| 341 | dev_err(mcbsp->dev, "Unable to request RX IRQ\n"); |
| 342 | goto err_free_irq; |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | return 0; |
| 347 | err_free_irq: |
| 348 | free_irq(mcbsp->tx_irq, (void *)mcbsp); |
| 349 | err_clk_disable: |
Peter Ujfalusi | 465d85b | 2018-11-08 09:29:59 +0200 | [diff] [blame] | 350 | if(mcbsp->pdata->ops && mcbsp->pdata->ops->free) |
Peter Ujfalusi | 9c34d02 | 2018-11-08 09:29:58 +0200 | [diff] [blame] | 351 | mcbsp->pdata->ops->free(mcbsp->id - 1); |
| 352 | |
| 353 | /* Disable wakeup behavior */ |
| 354 | if (mcbsp->pdata->has_wakeup) |
| 355 | MCBSP_WRITE(mcbsp, WAKEUPEN, 0); |
| 356 | |
| 357 | spin_lock(&mcbsp->lock); |
| 358 | mcbsp->free = true; |
| 359 | mcbsp->reg_cache = NULL; |
| 360 | err_kfree: |
| 361 | spin_unlock(&mcbsp->lock); |
| 362 | kfree(reg_cache); |
| 363 | |
| 364 | return err; |
| 365 | } |
| 366 | |
| 367 | static void omap_mcbsp_free(struct omap_mcbsp *mcbsp) |
| 368 | { |
| 369 | void *reg_cache; |
| 370 | |
Peter Ujfalusi | 465d85b | 2018-11-08 09:29:59 +0200 | [diff] [blame] | 371 | if(mcbsp->pdata->ops && mcbsp->pdata->ops->free) |
Peter Ujfalusi | 9c34d02 | 2018-11-08 09:29:58 +0200 | [diff] [blame] | 372 | mcbsp->pdata->ops->free(mcbsp->id - 1); |
| 373 | |
| 374 | /* Disable wakeup behavior */ |
| 375 | if (mcbsp->pdata->has_wakeup) |
| 376 | MCBSP_WRITE(mcbsp, WAKEUPEN, 0); |
| 377 | |
| 378 | /* Disable interrupt requests */ |
| 379 | if (mcbsp->irq) |
| 380 | MCBSP_WRITE(mcbsp, IRQEN, 0); |
| 381 | |
| 382 | if (mcbsp->irq) { |
| 383 | free_irq(mcbsp->irq, (void *)mcbsp); |
| 384 | } else { |
| 385 | free_irq(mcbsp->rx_irq, (void *)mcbsp); |
| 386 | free_irq(mcbsp->tx_irq, (void *)mcbsp); |
| 387 | } |
| 388 | |
| 389 | reg_cache = mcbsp->reg_cache; |
| 390 | |
| 391 | /* |
| 392 | * Select CLKS source from internal source unconditionally before |
| 393 | * marking the McBSP port as free. |
| 394 | * If the external clock source via MCBSP_CLKS pin has been selected the |
| 395 | * system will refuse to enter idle if the CLKS pin source is not reset |
| 396 | * back to internal source. |
| 397 | */ |
| 398 | if (!mcbsp_omap1()) |
| 399 | omap2_mcbsp_set_clks_src(mcbsp, MCBSP_CLKS_PRCM_SRC); |
| 400 | |
| 401 | spin_lock(&mcbsp->lock); |
| 402 | if (mcbsp->free) |
| 403 | dev_err(mcbsp->dev, "McBSP%d was not reserved\n", mcbsp->id); |
| 404 | else |
| 405 | mcbsp->free = true; |
| 406 | mcbsp->reg_cache = NULL; |
| 407 | spin_unlock(&mcbsp->lock); |
| 408 | |
| 409 | kfree(reg_cache); |
| 410 | } |
| 411 | |
| 412 | /* |
| 413 | * Here we start the McBSP, by enabling transmitter, receiver or both. |
| 414 | * If no transmitter or receiver is active prior calling, then sample-rate |
| 415 | * generator and frame sync are started. |
| 416 | */ |
| 417 | static void omap_mcbsp_start(struct omap_mcbsp *mcbsp, int stream) |
| 418 | { |
| 419 | int tx = (stream == SNDRV_PCM_STREAM_PLAYBACK); |
| 420 | int rx = !tx; |
| 421 | int enable_srg = 0; |
| 422 | u16 w; |
| 423 | |
| 424 | if (mcbsp->st_data) |
| 425 | omap_mcbsp_st_start(mcbsp); |
| 426 | |
| 427 | /* Only enable SRG, if McBSP is master */ |
| 428 | w = MCBSP_READ_CACHE(mcbsp, PCR0); |
| 429 | if (w & (FSXM | FSRM | CLKXM | CLKRM)) |
| 430 | enable_srg = !((MCBSP_READ_CACHE(mcbsp, SPCR2) | |
| 431 | MCBSP_READ_CACHE(mcbsp, SPCR1)) & 1); |
| 432 | |
| 433 | if (enable_srg) { |
| 434 | /* Start the sample generator */ |
| 435 | w = MCBSP_READ_CACHE(mcbsp, SPCR2); |
| 436 | MCBSP_WRITE(mcbsp, SPCR2, w | (1 << 6)); |
| 437 | } |
| 438 | |
| 439 | /* Enable transmitter and receiver */ |
| 440 | tx &= 1; |
| 441 | w = MCBSP_READ_CACHE(mcbsp, SPCR2); |
| 442 | MCBSP_WRITE(mcbsp, SPCR2, w | tx); |
| 443 | |
| 444 | rx &= 1; |
| 445 | w = MCBSP_READ_CACHE(mcbsp, SPCR1); |
| 446 | MCBSP_WRITE(mcbsp, SPCR1, w | rx); |
| 447 | |
| 448 | /* |
| 449 | * Worst case: CLKSRG*2 = 8000khz: (1/8000) * 2 * 2 usec |
| 450 | * REVISIT: 100us may give enough time for two CLKSRG, however |
| 451 | * due to some unknown PM related, clock gating etc. reason it |
| 452 | * is now at 500us. |
| 453 | */ |
| 454 | udelay(500); |
| 455 | |
| 456 | if (enable_srg) { |
| 457 | /* Start frame sync */ |
| 458 | w = MCBSP_READ_CACHE(mcbsp, SPCR2); |
| 459 | MCBSP_WRITE(mcbsp, SPCR2, w | (1 << 7)); |
| 460 | } |
| 461 | |
| 462 | if (mcbsp->pdata->has_ccr) { |
| 463 | /* Release the transmitter and receiver */ |
| 464 | w = MCBSP_READ_CACHE(mcbsp, XCCR); |
| 465 | w &= ~(tx ? XDISABLE : 0); |
| 466 | MCBSP_WRITE(mcbsp, XCCR, w); |
| 467 | w = MCBSP_READ_CACHE(mcbsp, RCCR); |
| 468 | w &= ~(rx ? RDISABLE : 0); |
| 469 | MCBSP_WRITE(mcbsp, RCCR, w); |
| 470 | } |
| 471 | |
| 472 | /* Dump McBSP Regs */ |
| 473 | omap_mcbsp_dump_reg(mcbsp); |
| 474 | } |
| 475 | |
| 476 | static void omap_mcbsp_stop(struct omap_mcbsp *mcbsp, int stream) |
| 477 | { |
| 478 | int tx = (stream == SNDRV_PCM_STREAM_PLAYBACK); |
| 479 | int rx = !tx; |
| 480 | int idle; |
| 481 | u16 w; |
| 482 | |
| 483 | /* Reset transmitter */ |
| 484 | tx &= 1; |
| 485 | if (mcbsp->pdata->has_ccr) { |
| 486 | w = MCBSP_READ_CACHE(mcbsp, XCCR); |
| 487 | w |= (tx ? XDISABLE : 0); |
| 488 | MCBSP_WRITE(mcbsp, XCCR, w); |
| 489 | } |
| 490 | w = MCBSP_READ_CACHE(mcbsp, SPCR2); |
| 491 | MCBSP_WRITE(mcbsp, SPCR2, w & ~tx); |
| 492 | |
| 493 | /* Reset receiver */ |
| 494 | rx &= 1; |
| 495 | if (mcbsp->pdata->has_ccr) { |
| 496 | w = MCBSP_READ_CACHE(mcbsp, RCCR); |
| 497 | w |= (rx ? RDISABLE : 0); |
| 498 | MCBSP_WRITE(mcbsp, RCCR, w); |
| 499 | } |
| 500 | w = MCBSP_READ_CACHE(mcbsp, SPCR1); |
| 501 | MCBSP_WRITE(mcbsp, SPCR1, w & ~rx); |
| 502 | |
| 503 | idle = !((MCBSP_READ_CACHE(mcbsp, SPCR2) | |
| 504 | MCBSP_READ_CACHE(mcbsp, SPCR1)) & 1); |
| 505 | |
| 506 | if (idle) { |
| 507 | /* Reset the sample rate generator */ |
| 508 | w = MCBSP_READ_CACHE(mcbsp, SPCR2); |
| 509 | MCBSP_WRITE(mcbsp, SPCR2, w & ~(1 << 6)); |
| 510 | } |
| 511 | |
| 512 | if (mcbsp->st_data) |
| 513 | omap_mcbsp_st_stop(mcbsp); |
| 514 | } |
| 515 | |
| 516 | #define max_thres(m) (mcbsp->pdata->buffer_size) |
| 517 | #define valid_threshold(m, val) ((val) <= max_thres(m)) |
| 518 | #define THRESHOLD_PROP_BUILDER(prop) \ |
| 519 | static ssize_t prop##_show(struct device *dev, \ |
| 520 | struct device_attribute *attr, char *buf) \ |
| 521 | { \ |
| 522 | struct omap_mcbsp *mcbsp = dev_get_drvdata(dev); \ |
| 523 | \ |
| 524 | return sprintf(buf, "%u\n", mcbsp->prop); \ |
| 525 | } \ |
| 526 | \ |
| 527 | static ssize_t prop##_store(struct device *dev, \ |
| 528 | struct device_attribute *attr, \ |
| 529 | const char *buf, size_t size) \ |
| 530 | { \ |
| 531 | struct omap_mcbsp *mcbsp = dev_get_drvdata(dev); \ |
| 532 | unsigned long val; \ |
| 533 | int status; \ |
| 534 | \ |
| 535 | status = kstrtoul(buf, 0, &val); \ |
| 536 | if (status) \ |
| 537 | return status; \ |
| 538 | \ |
| 539 | if (!valid_threshold(mcbsp, val)) \ |
| 540 | return -EDOM; \ |
| 541 | \ |
| 542 | mcbsp->prop = val; \ |
| 543 | return size; \ |
| 544 | } \ |
| 545 | \ |
| 546 | static DEVICE_ATTR(prop, 0644, prop##_show, prop##_store) |
| 547 | |
| 548 | THRESHOLD_PROP_BUILDER(max_tx_thres); |
| 549 | THRESHOLD_PROP_BUILDER(max_rx_thres); |
| 550 | |
| 551 | static const char * const dma_op_modes[] = { |
| 552 | "element", "threshold", |
| 553 | }; |
| 554 | |
| 555 | static ssize_t dma_op_mode_show(struct device *dev, |
| 556 | struct device_attribute *attr, char *buf) |
| 557 | { |
| 558 | struct omap_mcbsp *mcbsp = dev_get_drvdata(dev); |
| 559 | int dma_op_mode, i = 0; |
| 560 | ssize_t len = 0; |
| 561 | const char * const *s; |
| 562 | |
| 563 | dma_op_mode = mcbsp->dma_op_mode; |
| 564 | |
| 565 | for (s = &dma_op_modes[i]; i < ARRAY_SIZE(dma_op_modes); s++, i++) { |
| 566 | if (dma_op_mode == i) |
| 567 | len += sprintf(buf + len, "[%s] ", *s); |
| 568 | else |
| 569 | len += sprintf(buf + len, "%s ", *s); |
| 570 | } |
| 571 | len += sprintf(buf + len, "\n"); |
| 572 | |
| 573 | return len; |
| 574 | } |
| 575 | |
| 576 | static ssize_t dma_op_mode_store(struct device *dev, |
| 577 | struct device_attribute *attr, const char *buf, |
| 578 | size_t size) |
| 579 | { |
| 580 | struct omap_mcbsp *mcbsp = dev_get_drvdata(dev); |
| 581 | int i; |
| 582 | |
| 583 | i = sysfs_match_string(dma_op_modes, buf); |
| 584 | if (i < 0) |
| 585 | return i; |
| 586 | |
| 587 | spin_lock_irq(&mcbsp->lock); |
| 588 | if (!mcbsp->free) { |
| 589 | size = -EBUSY; |
| 590 | goto unlock; |
| 591 | } |
| 592 | mcbsp->dma_op_mode = i; |
| 593 | |
| 594 | unlock: |
| 595 | spin_unlock_irq(&mcbsp->lock); |
| 596 | |
| 597 | return size; |
| 598 | } |
| 599 | |
| 600 | static DEVICE_ATTR_RW(dma_op_mode); |
| 601 | |
| 602 | static const struct attribute *additional_attrs[] = { |
| 603 | &dev_attr_max_tx_thres.attr, |
| 604 | &dev_attr_max_rx_thres.attr, |
| 605 | &dev_attr_dma_op_mode.attr, |
| 606 | NULL, |
| 607 | }; |
| 608 | |
| 609 | static const struct attribute_group additional_attr_group = { |
| 610 | .attrs = (struct attribute **)additional_attrs, |
| 611 | }; |
| 612 | |
| 613 | /* |
| 614 | * McBSP1 and McBSP3 are directly mapped on 1610 and 1510. |
| 615 | * 730 has only 2 McBSP, and both of them are MPU peripherals. |
| 616 | */ |
| 617 | static int omap_mcbsp_init(struct platform_device *pdev) |
| 618 | { |
| 619 | struct omap_mcbsp *mcbsp = platform_get_drvdata(pdev); |
| 620 | struct resource *res; |
| 621 | int ret = 0; |
| 622 | |
| 623 | spin_lock_init(&mcbsp->lock); |
| 624 | mcbsp->free = true; |
| 625 | |
| 626 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mpu"); |
| 627 | if (!res) |
| 628 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 629 | |
| 630 | mcbsp->io_base = devm_ioremap_resource(&pdev->dev, res); |
| 631 | if (IS_ERR(mcbsp->io_base)) |
| 632 | return PTR_ERR(mcbsp->io_base); |
| 633 | |
| 634 | mcbsp->phys_base = res->start; |
| 635 | mcbsp->reg_cache_size = resource_size(res); |
| 636 | |
| 637 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dma"); |
| 638 | if (!res) |
| 639 | mcbsp->phys_dma_base = mcbsp->phys_base; |
| 640 | else |
| 641 | mcbsp->phys_dma_base = res->start; |
| 642 | |
| 643 | /* |
| 644 | * OMAP1, 2 uses two interrupt lines: TX, RX |
| 645 | * OMAP2430, OMAP3 SoC have combined IRQ line as well. |
| 646 | * OMAP4 and newer SoC only have the combined IRQ line. |
| 647 | * Use the combined IRQ if available since it gives better debugging |
| 648 | * possibilities. |
| 649 | */ |
| 650 | mcbsp->irq = platform_get_irq_byname(pdev, "common"); |
| 651 | if (mcbsp->irq == -ENXIO) { |
| 652 | mcbsp->tx_irq = platform_get_irq_byname(pdev, "tx"); |
| 653 | |
| 654 | if (mcbsp->tx_irq == -ENXIO) { |
| 655 | mcbsp->irq = platform_get_irq(pdev, 0); |
| 656 | mcbsp->tx_irq = 0; |
| 657 | } else { |
| 658 | mcbsp->rx_irq = platform_get_irq_byname(pdev, "rx"); |
| 659 | mcbsp->irq = 0; |
| 660 | } |
| 661 | } |
| 662 | |
| 663 | if (!pdev->dev.of_node) { |
| 664 | res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "tx"); |
| 665 | if (!res) { |
| 666 | dev_err(&pdev->dev, "invalid tx DMA channel\n"); |
| 667 | return -ENODEV; |
| 668 | } |
| 669 | mcbsp->dma_req[0] = res->start; |
| 670 | mcbsp->dma_data[0].filter_data = &mcbsp->dma_req[0]; |
| 671 | |
| 672 | res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "rx"); |
| 673 | if (!res) { |
| 674 | dev_err(&pdev->dev, "invalid rx DMA channel\n"); |
| 675 | return -ENODEV; |
| 676 | } |
| 677 | mcbsp->dma_req[1] = res->start; |
| 678 | mcbsp->dma_data[1].filter_data = &mcbsp->dma_req[1]; |
| 679 | } else { |
| 680 | mcbsp->dma_data[0].filter_data = "tx"; |
| 681 | mcbsp->dma_data[1].filter_data = "rx"; |
| 682 | } |
| 683 | |
| 684 | mcbsp->dma_data[0].addr = omap_mcbsp_dma_reg_params(mcbsp, |
| 685 | SNDRV_PCM_STREAM_PLAYBACK); |
| 686 | mcbsp->dma_data[1].addr = omap_mcbsp_dma_reg_params(mcbsp, |
| 687 | SNDRV_PCM_STREAM_CAPTURE); |
| 688 | |
| 689 | mcbsp->fclk = clk_get(&pdev->dev, "fck"); |
| 690 | if (IS_ERR(mcbsp->fclk)) { |
| 691 | ret = PTR_ERR(mcbsp->fclk); |
| 692 | dev_err(mcbsp->dev, "unable to get fck: %d\n", ret); |
| 693 | return ret; |
| 694 | } |
| 695 | |
| 696 | mcbsp->dma_op_mode = MCBSP_DMA_MODE_ELEMENT; |
| 697 | if (mcbsp->pdata->buffer_size) { |
| 698 | /* |
| 699 | * Initially configure the maximum thresholds to a safe value. |
| 700 | * The McBSP FIFO usage with these values should not go under |
| 701 | * 16 locations. |
| 702 | * If the whole FIFO without safety buffer is used, than there |
| 703 | * is a possibility that the DMA will be not able to push the |
| 704 | * new data on time, causing channel shifts in runtime. |
| 705 | */ |
| 706 | mcbsp->max_tx_thres = max_thres(mcbsp) - 0x10; |
| 707 | mcbsp->max_rx_thres = max_thres(mcbsp) - 0x10; |
| 708 | |
| 709 | ret = sysfs_create_group(&mcbsp->dev->kobj, |
| 710 | &additional_attr_group); |
| 711 | if (ret) { |
| 712 | dev_err(mcbsp->dev, |
| 713 | "Unable to create additional controls\n"); |
| 714 | goto err_thres; |
| 715 | } |
Peter Ujfalusi | 9c34d02 | 2018-11-08 09:29:58 +0200 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | ret = omap_mcbsp_st_init(pdev); |
| 719 | if (ret) |
| 720 | goto err_st; |
| 721 | |
| 722 | return 0; |
| 723 | |
| 724 | err_st: |
| 725 | if (mcbsp->pdata->buffer_size) |
| 726 | sysfs_remove_group(&mcbsp->dev->kobj, &additional_attr_group); |
| 727 | err_thres: |
| 728 | clk_put(mcbsp->fclk); |
| 729 | return ret; |
| 730 | } |
| 731 | |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 732 | /* |
| 733 | * Stream DMA parameters. DMA request line and port address are set runtime |
| 734 | * since they are different between OMAP1 and later OMAPs |
| 735 | */ |
Lars-Peter Clausen | abe9937 | 2013-03-25 16:58:16 +0100 | [diff] [blame] | 736 | static void omap_mcbsp_set_threshold(struct snd_pcm_substream *substream, |
| 737 | unsigned int packet_size) |
Eduardo Valentin | caebc0c | 2009-08-20 16:18:25 +0300 | [diff] [blame] | 738 | { |
| 739 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 740 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; |
Peter Ujfalusi | 45656b4 | 2012-02-14 18:20:58 +0200 | [diff] [blame] | 741 | struct omap_mcbsp *mcbsp = snd_soc_dai_get_drvdata(cpu_dai); |
Peter Ujfalusi | 3f02403 | 2010-06-03 07:39:35 +0300 | [diff] [blame] | 742 | int words; |
Eduardo Valentin | a0a499c | 2009-08-20 16:18:26 +0300 | [diff] [blame] | 743 | |
Peter Ujfalusi | be51c57 | 2018-11-08 09:29:57 +0200 | [diff] [blame] | 744 | /* No need to proceed further if McBSP does not have FIFO */ |
| 745 | if (mcbsp->pdata->buffer_size == 0) |
| 746 | return; |
| 747 | |
Peter Ujfalusi | 778a17c | 2012-03-15 12:20:32 +0200 | [diff] [blame] | 748 | /* |
| 749 | * Configure McBSP threshold based on either: |
| 750 | * packet_size, when the sDMA is in packet mode, or based on the |
| 751 | * period size in THRESHOLD mode, otherwise use McBSP threshold = 1 |
| 752 | * for mono streams. |
| 753 | */ |
Lars-Peter Clausen | abe9937 | 2013-03-25 16:58:16 +0100 | [diff] [blame] | 754 | if (packet_size) |
| 755 | words = packet_size; |
Eduardo Valentin | a0a499c | 2009-08-20 16:18:26 +0300 | [diff] [blame] | 756 | else |
Peter Ujfalusi | 3f02403 | 2010-06-03 07:39:35 +0300 | [diff] [blame] | 757 | words = 1; |
Eduardo Valentin | caebc0c | 2009-08-20 16:18:25 +0300 | [diff] [blame] | 758 | |
| 759 | /* Configure McBSP internal buffer usage */ |
| 760 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
Peter Ujfalusi | 45656b4 | 2012-02-14 18:20:58 +0200 | [diff] [blame] | 761 | omap_mcbsp_set_tx_threshold(mcbsp, words); |
Eduardo Valentin | caebc0c | 2009-08-20 16:18:25 +0300 | [diff] [blame] | 762 | else |
Peter Ujfalusi | 45656b4 | 2012-02-14 18:20:58 +0200 | [diff] [blame] | 763 | omap_mcbsp_set_rx_threshold(mcbsp, words); |
Eduardo Valentin | caebc0c | 2009-08-20 16:18:25 +0300 | [diff] [blame] | 764 | } |
| 765 | |
Peter Ujfalusi | ddc29b0 | 2010-06-03 07:39:36 +0300 | [diff] [blame] | 766 | static int omap_mcbsp_hwrule_min_buffersize(struct snd_pcm_hw_params *params, |
| 767 | struct snd_pcm_hw_rule *rule) |
| 768 | { |
| 769 | struct snd_interval *buffer_size = hw_param_interval(params, |
| 770 | SNDRV_PCM_HW_PARAM_BUFFER_SIZE); |
| 771 | struct snd_interval *channels = hw_param_interval(params, |
| 772 | SNDRV_PCM_HW_PARAM_CHANNELS); |
Peter Ujfalusi | 45656b4 | 2012-02-14 18:20:58 +0200 | [diff] [blame] | 773 | struct omap_mcbsp *mcbsp = rule->private; |
Peter Ujfalusi | ddc29b0 | 2010-06-03 07:39:36 +0300 | [diff] [blame] | 774 | struct snd_interval frames; |
| 775 | int size; |
| 776 | |
| 777 | snd_interval_any(&frames); |
Peter Ujfalusi | cb40b63 | 2012-02-13 16:26:54 +0200 | [diff] [blame] | 778 | size = mcbsp->pdata->buffer_size; |
Peter Ujfalusi | ddc29b0 | 2010-06-03 07:39:36 +0300 | [diff] [blame] | 779 | |
| 780 | frames.min = size / channels->min; |
| 781 | frames.integer = 1; |
| 782 | return snd_interval_refine(buffer_size, &frames); |
| 783 | } |
| 784 | |
Mark Brown | dee89c4 | 2008-11-18 22:11:38 +0000 | [diff] [blame] | 785 | static int omap_mcbsp_dai_startup(struct snd_pcm_substream *substream, |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 786 | struct snd_soc_dai *cpu_dai) |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 787 | { |
Peter Ujfalusi | 45656b4 | 2012-02-14 18:20:58 +0200 | [diff] [blame] | 788 | struct omap_mcbsp *mcbsp = snd_soc_dai_get_drvdata(cpu_dai); |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 789 | int err = 0; |
| 790 | |
Eduardo Valentin | caebc0c | 2009-08-20 16:18:25 +0300 | [diff] [blame] | 791 | if (!cpu_dai->active) |
Peter Ujfalusi | 45656b4 | 2012-02-14 18:20:58 +0200 | [diff] [blame] | 792 | err = omap_mcbsp_request(mcbsp); |
Eduardo Valentin | caebc0c | 2009-08-20 16:18:25 +0300 | [diff] [blame] | 793 | |
Peter Ujfalusi | ddc29b0 | 2010-06-03 07:39:36 +0300 | [diff] [blame] | 794 | /* |
| 795 | * OMAP3 McBSP FIFO is word structured. |
| 796 | * McBSP2 has 1024 + 256 = 1280 word long buffer, |
| 797 | * McBSP1,3,4,5 has 128 word long buffer |
| 798 | * This means that the size of the FIFO depends on the sample format. |
| 799 | * For example on McBSP3: |
| 800 | * 16bit samples: size is 128 * 2 = 256 bytes |
| 801 | * 32bit samples: size is 128 * 4 = 512 bytes |
| 802 | * It is simpler to place constraint for buffer and period based on |
| 803 | * channels. |
| 804 | * McBSP3 as example again (16 or 32 bit samples): |
| 805 | * 1 channel (mono): size is 128 frames (128 words) |
| 806 | * 2 channels (stereo): size is 128 / 2 = 64 frames (2 * 64 words) |
| 807 | * 4 channels: size is 128 / 4 = 32 frames (4 * 32 words) |
| 808 | */ |
Peter Ujfalusi | 45656b4 | 2012-02-14 18:20:58 +0200 | [diff] [blame] | 809 | if (mcbsp->pdata->buffer_size) { |
Jarkko Nikula | 6984992 | 2009-03-27 15:32:01 +0200 | [diff] [blame] | 810 | /* |
Peter Ujfalusi | 998a8a6 | 2010-07-29 09:51:28 +0300 | [diff] [blame] | 811 | * Rule for the buffer size. We should not allow |
Peter Ujfalusi | ce37f5e | 2012-03-20 11:47:36 +0200 | [diff] [blame] | 812 | * smaller buffer than the FIFO size to avoid underruns. |
| 813 | * This applies only for the playback stream. |
Peter Ujfalusi | ddc29b0 | 2010-06-03 07:39:36 +0300 | [diff] [blame] | 814 | */ |
Peter Ujfalusi | ce37f5e | 2012-03-20 11:47:36 +0200 | [diff] [blame] | 815 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 816 | snd_pcm_hw_rule_add(substream->runtime, 0, |
| 817 | SNDRV_PCM_HW_PARAM_BUFFER_SIZE, |
| 818 | omap_mcbsp_hwrule_min_buffersize, |
| 819 | mcbsp, |
| 820 | SNDRV_PCM_HW_PARAM_CHANNELS, -1); |
Peter Ujfalusi | ddc29b0 | 2010-06-03 07:39:36 +0300 | [diff] [blame] | 821 | |
Peter Ujfalusi | 998a8a6 | 2010-07-29 09:51:28 +0300 | [diff] [blame] | 822 | /* Make sure, that the period size is always even */ |
| 823 | snd_pcm_hw_constraint_step(substream->runtime, 0, |
| 824 | SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 2); |
Eduardo Valentin | caebc0c | 2009-08-20 16:18:25 +0300 | [diff] [blame] | 825 | } |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 826 | |
| 827 | return err; |
| 828 | } |
| 829 | |
Mark Brown | dee89c4 | 2008-11-18 22:11:38 +0000 | [diff] [blame] | 830 | static void omap_mcbsp_dai_shutdown(struct snd_pcm_substream *substream, |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 831 | struct snd_soc_dai *cpu_dai) |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 832 | { |
Peter Ujfalusi | 45656b4 | 2012-02-14 18:20:58 +0200 | [diff] [blame] | 833 | struct omap_mcbsp *mcbsp = snd_soc_dai_get_drvdata(cpu_dai); |
Matt Ranostay | 9834ffd | 2017-01-31 13:21:43 -0800 | [diff] [blame] | 834 | int tx = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK); |
| 835 | int stream1 = tx ? SNDRV_PCM_STREAM_PLAYBACK : SNDRV_PCM_STREAM_CAPTURE; |
| 836 | int stream2 = tx ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK; |
| 837 | |
| 838 | if (mcbsp->latency[stream2]) |
| 839 | pm_qos_update_request(&mcbsp->pm_qos_req, |
| 840 | mcbsp->latency[stream2]); |
| 841 | else if (mcbsp->latency[stream1]) |
| 842 | pm_qos_remove_request(&mcbsp->pm_qos_req); |
| 843 | |
| 844 | mcbsp->latency[stream1] = 0; |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 845 | |
| 846 | if (!cpu_dai->active) { |
Peter Ujfalusi | 45656b4 | 2012-02-14 18:20:58 +0200 | [diff] [blame] | 847 | omap_mcbsp_free(mcbsp); |
Peter Ujfalusi | 256d9c2 | 2012-02-14 15:23:15 +0200 | [diff] [blame] | 848 | mcbsp->configured = 0; |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 849 | } |
| 850 | } |
| 851 | |
Matt Ranostay | 9834ffd | 2017-01-31 13:21:43 -0800 | [diff] [blame] | 852 | static int omap_mcbsp_dai_prepare(struct snd_pcm_substream *substream, |
| 853 | struct snd_soc_dai *cpu_dai) |
| 854 | { |
| 855 | struct omap_mcbsp *mcbsp = snd_soc_dai_get_drvdata(cpu_dai); |
| 856 | struct pm_qos_request *pm_qos_req = &mcbsp->pm_qos_req; |
| 857 | int tx = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK); |
| 858 | int stream1 = tx ? SNDRV_PCM_STREAM_PLAYBACK : SNDRV_PCM_STREAM_CAPTURE; |
| 859 | int stream2 = tx ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK; |
| 860 | int latency = mcbsp->latency[stream2]; |
| 861 | |
| 862 | /* Prevent omap hardware from hitting off between FIFO fills */ |
| 863 | if (!latency || mcbsp->latency[stream1] < latency) |
| 864 | latency = mcbsp->latency[stream1]; |
| 865 | |
| 866 | if (pm_qos_request_active(pm_qos_req)) |
| 867 | pm_qos_update_request(pm_qos_req, latency); |
| 868 | else if (latency) |
| 869 | pm_qos_add_request(pm_qos_req, PM_QOS_CPU_DMA_LATENCY, latency); |
| 870 | |
| 871 | return 0; |
| 872 | } |
| 873 | |
Mark Brown | dee89c4 | 2008-11-18 22:11:38 +0000 | [diff] [blame] | 874 | static int omap_mcbsp_dai_trigger(struct snd_pcm_substream *substream, int cmd, |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 875 | struct snd_soc_dai *cpu_dai) |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 876 | { |
Peter Ujfalusi | 45656b4 | 2012-02-14 18:20:58 +0200 | [diff] [blame] | 877 | struct omap_mcbsp *mcbsp = snd_soc_dai_get_drvdata(cpu_dai); |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 878 | |
| 879 | switch (cmd) { |
| 880 | case SNDRV_PCM_TRIGGER_START: |
| 881 | case SNDRV_PCM_TRIGGER_RESUME: |
| 882 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: |
Peter Ujfalusi | 256d9c2 | 2012-02-14 15:23:15 +0200 | [diff] [blame] | 883 | mcbsp->active++; |
Peter Ujfalusi | 59d177f | 2018-11-08 09:29:56 +0200 | [diff] [blame] | 884 | omap_mcbsp_start(mcbsp, substream->stream); |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 885 | break; |
| 886 | |
| 887 | case SNDRV_PCM_TRIGGER_STOP: |
| 888 | case SNDRV_PCM_TRIGGER_SUSPEND: |
| 889 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
Peter Ujfalusi | 59d177f | 2018-11-08 09:29:56 +0200 | [diff] [blame] | 890 | omap_mcbsp_stop(mcbsp, substream->stream); |
Peter Ujfalusi | 256d9c2 | 2012-02-14 15:23:15 +0200 | [diff] [blame] | 891 | mcbsp->active--; |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 892 | break; |
| 893 | default: |
Peter Ujfalusi | 59d177f | 2018-11-08 09:29:56 +0200 | [diff] [blame] | 894 | return -EINVAL; |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 895 | } |
| 896 | |
Peter Ujfalusi | 59d177f | 2018-11-08 09:29:56 +0200 | [diff] [blame] | 897 | return 0; |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 898 | } |
| 899 | |
Peter Ujfalusi | 75581d2 | 2010-03-03 15:08:09 +0200 | [diff] [blame] | 900 | static snd_pcm_sframes_t omap_mcbsp_dai_delay( |
| 901 | struct snd_pcm_substream *substream, |
| 902 | struct snd_soc_dai *dai) |
| 903 | { |
| 904 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 905 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; |
Peter Ujfalusi | 45656b4 | 2012-02-14 18:20:58 +0200 | [diff] [blame] | 906 | struct omap_mcbsp *mcbsp = snd_soc_dai_get_drvdata(cpu_dai); |
Peter Ujfalusi | 75581d2 | 2010-03-03 15:08:09 +0200 | [diff] [blame] | 907 | u16 fifo_use; |
| 908 | snd_pcm_sframes_t delay; |
| 909 | |
Peter Ujfalusi | be51c57 | 2018-11-08 09:29:57 +0200 | [diff] [blame] | 910 | /* No need to proceed further if McBSP does not have FIFO */ |
| 911 | if (mcbsp->pdata->buffer_size == 0) |
| 912 | return 0; |
| 913 | |
Peter Ujfalusi | 75581d2 | 2010-03-03 15:08:09 +0200 | [diff] [blame] | 914 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
Peter Ujfalusi | 45656b4 | 2012-02-14 18:20:58 +0200 | [diff] [blame] | 915 | fifo_use = omap_mcbsp_get_tx_delay(mcbsp); |
Peter Ujfalusi | 75581d2 | 2010-03-03 15:08:09 +0200 | [diff] [blame] | 916 | else |
Peter Ujfalusi | 45656b4 | 2012-02-14 18:20:58 +0200 | [diff] [blame] | 917 | fifo_use = omap_mcbsp_get_rx_delay(mcbsp); |
Peter Ujfalusi | 75581d2 | 2010-03-03 15:08:09 +0200 | [diff] [blame] | 918 | |
| 919 | /* |
| 920 | * Divide the used locations with the channel count to get the |
| 921 | * FIFO usage in samples (don't care about partial samples in the |
| 922 | * buffer). |
| 923 | */ |
| 924 | delay = fifo_use / substream->runtime->channels; |
| 925 | |
| 926 | return delay; |
| 927 | } |
| 928 | |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 929 | static int omap_mcbsp_dai_hw_params(struct snd_pcm_substream *substream, |
Mark Brown | dee89c4 | 2008-11-18 22:11:38 +0000 | [diff] [blame] | 930 | struct snd_pcm_hw_params *params, |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 931 | struct snd_soc_dai *cpu_dai) |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 932 | { |
Peter Ujfalusi | 45656b4 | 2012-02-14 18:20:58 +0200 | [diff] [blame] | 933 | struct omap_mcbsp *mcbsp = snd_soc_dai_get_drvdata(cpu_dai); |
Peter Ujfalusi | 256d9c2 | 2012-02-14 15:23:15 +0200 | [diff] [blame] | 934 | struct omap_mcbsp_reg_cfg *regs = &mcbsp->cfg_regs; |
Lars-Peter Clausen | 09ae3aa | 2013-04-03 11:06:05 +0200 | [diff] [blame] | 935 | struct snd_dmaengine_dai_dma_data *dma_data; |
Peter Ujfalusi | 061fb36 | 2012-09-14 15:05:51 +0300 | [diff] [blame] | 936 | int wlen, channels, wpf; |
Peter Ujfalusi | cf80e15 | 2010-07-29 09:51:27 +0300 | [diff] [blame] | 937 | int pkt_size = 0; |
Graeme Gregory | 5f63ef9 | 2009-11-09 19:02:15 +0000 | [diff] [blame] | 938 | unsigned int format, div, framesize, master; |
Matt Ranostay | 9834ffd | 2017-01-31 13:21:43 -0800 | [diff] [blame] | 939 | unsigned int buffer_size = mcbsp->pdata->buffer_size; |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 940 | |
Peter Ujfalusi | bcd6da7 | 2012-09-14 15:05:57 +0300 | [diff] [blame] | 941 | dma_data = snd_soc_dai_get_dma_data(cpu_dai, substream); |
Peter Ujfalusi | 778a17c | 2012-03-15 12:20:32 +0200 | [diff] [blame] | 942 | channels = params_channels(params); |
Kishon Vijay Abraham I | 2686e07 | 2011-02-24 15:16:56 +0530 | [diff] [blame] | 943 | |
Sergey Lapin | d98508a | 2010-05-13 19:48:16 +0400 | [diff] [blame] | 944 | switch (params_format(params)) { |
| 945 | case SNDRV_PCM_FORMAT_S16_LE: |
Peter Ujfalusi | cf80e15 | 2010-07-29 09:51:27 +0300 | [diff] [blame] | 946 | wlen = 16; |
Sergey Lapin | d98508a | 2010-05-13 19:48:16 +0400 | [diff] [blame] | 947 | break; |
| 948 | case SNDRV_PCM_FORMAT_S32_LE: |
Peter Ujfalusi | cf80e15 | 2010-07-29 09:51:27 +0300 | [diff] [blame] | 949 | wlen = 32; |
Sergey Lapin | d98508a | 2010-05-13 19:48:16 +0400 | [diff] [blame] | 950 | break; |
| 951 | default: |
| 952 | return -EINVAL; |
| 953 | } |
Matt Ranostay | 9834ffd | 2017-01-31 13:21:43 -0800 | [diff] [blame] | 954 | if (buffer_size) { |
| 955 | int latency; |
| 956 | |
Peter Ujfalusi | cb40b63 | 2012-02-13 16:26:54 +0200 | [diff] [blame] | 957 | if (mcbsp->dma_op_mode == MCBSP_DMA_MODE_THRESHOLD) { |
Peter Ujfalusi | cf80e15 | 2010-07-29 09:51:27 +0300 | [diff] [blame] | 958 | int period_words, max_thrsh; |
Peter Ujfalusi | dffb360 | 2012-09-14 15:05:49 +0300 | [diff] [blame] | 959 | int divider = 0; |
Peter Ujfalusi | cf80e15 | 2010-07-29 09:51:27 +0300 | [diff] [blame] | 960 | |
| 961 | period_words = params_period_bytes(params) / (wlen / 8); |
| 962 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
Peter Ujfalusi | cb40b63 | 2012-02-13 16:26:54 +0200 | [diff] [blame] | 963 | max_thrsh = mcbsp->max_tx_thres; |
Peter Ujfalusi | cf80e15 | 2010-07-29 09:51:27 +0300 | [diff] [blame] | 964 | else |
Peter Ujfalusi | cb40b63 | 2012-02-13 16:26:54 +0200 | [diff] [blame] | 965 | max_thrsh = mcbsp->max_rx_thres; |
Peter Ujfalusi | cf80e15 | 2010-07-29 09:51:27 +0300 | [diff] [blame] | 966 | /* |
Peter Ujfalusi | dffb360 | 2012-09-14 15:05:49 +0300 | [diff] [blame] | 967 | * Use sDMA packet mode if McBSP is in threshold mode: |
| 968 | * If period words less than the FIFO size the packet |
| 969 | * size is set to the number of period words, otherwise |
| 970 | * Look for the biggest threshold value which divides |
| 971 | * the period size evenly. |
Peter Ujfalusi | cf80e15 | 2010-07-29 09:51:27 +0300 | [diff] [blame] | 972 | */ |
Peter Ujfalusi | dffb360 | 2012-09-14 15:05:49 +0300 | [diff] [blame] | 973 | divider = period_words / max_thrsh; |
| 974 | if (period_words % max_thrsh) |
| 975 | divider++; |
| 976 | while (period_words % divider && |
| 977 | divider < period_words) |
| 978 | divider++; |
| 979 | if (divider == period_words) |
| 980 | return -EINVAL; |
Peter Ujfalusi | cf80e15 | 2010-07-29 09:51:27 +0300 | [diff] [blame] | 981 | |
Peter Ujfalusi | dffb360 | 2012-09-14 15:05:49 +0300 | [diff] [blame] | 982 | pkt_size = period_words / divider; |
Peter Ujfalusi | 778a17c | 2012-03-15 12:20:32 +0200 | [diff] [blame] | 983 | } else if (channels > 1) { |
| 984 | /* Use packet mode for non mono streams */ |
| 985 | pkt_size = channels; |
Peter Ujfalusi | cf80e15 | 2010-07-29 09:51:27 +0300 | [diff] [blame] | 986 | } |
Matt Ranostay | 9834ffd | 2017-01-31 13:21:43 -0800 | [diff] [blame] | 987 | |
Peter Ujfalusi | dd2f52d | 2018-11-14 13:06:21 +0200 | [diff] [blame] | 988 | latency = (buffer_size - pkt_size) / channels; |
| 989 | latency = latency * USEC_PER_SEC / |
| 990 | (params->rate_num / params->rate_den); |
Matt Ranostay | 9834ffd | 2017-01-31 13:21:43 -0800 | [diff] [blame] | 991 | mcbsp->latency[substream->stream] = latency; |
| 992 | |
Lars-Peter Clausen | abe9937 | 2013-03-25 16:58:16 +0100 | [diff] [blame] | 993 | omap_mcbsp_set_threshold(substream, pkt_size); |
Peter Ujfalusi | 15d0143 | 2010-07-29 09:51:25 +0300 | [diff] [blame] | 994 | } |
| 995 | |
Lars-Peter Clausen | 09ae3aa | 2013-04-03 11:06:05 +0200 | [diff] [blame] | 996 | dma_data->maxburst = pkt_size; |
Daniel Mack | fd23b7d | 2010-03-19 14:52:55 +0000 | [diff] [blame] | 997 | |
Peter Ujfalusi | 256d9c2 | 2012-02-14 15:23:15 +0200 | [diff] [blame] | 998 | if (mcbsp->configured) { |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 999 | /* McBSP already configured by another stream */ |
| 1000 | return 0; |
| 1001 | } |
| 1002 | |
Jarkko Nikula | 4dd0417 | 2011-09-30 16:07:44 +0300 | [diff] [blame] | 1003 | regs->rcr2 &= ~(RPHASE | RFRLEN2(0x7f) | RWDLEN2(7)); |
| 1004 | regs->xcr2 &= ~(RPHASE | XFRLEN2(0x7f) | XWDLEN2(7)); |
| 1005 | regs->rcr1 &= ~(RFRLEN1(0x7f) | RWDLEN1(7)); |
| 1006 | regs->xcr1 &= ~(XFRLEN1(0x7f) | XWDLEN1(7)); |
Peter Ujfalusi | 256d9c2 | 2012-02-14 15:23:15 +0200 | [diff] [blame] | 1007 | format = mcbsp->fmt & SND_SOC_DAIFMT_FORMAT_MASK; |
Peter Ujfalusi | 778a17c | 2012-03-15 12:20:32 +0200 | [diff] [blame] | 1008 | wpf = channels; |
Peter Ujfalusi | 299a151 | 2010-03-19 12:27:31 +0200 | [diff] [blame] | 1009 | if (channels == 2 && (format == SND_SOC_DAIFMT_I2S || |
| 1010 | format == SND_SOC_DAIFMT_LEFT_J)) { |
Graeme Gregory | 5f63ef9 | 2009-11-09 19:02:15 +0000 | [diff] [blame] | 1011 | /* Use dual-phase frames */ |
| 1012 | regs->rcr2 |= RPHASE; |
| 1013 | regs->xcr2 |= XPHASE; |
| 1014 | /* Set 1 word per (McBSP) frame for phase1 and phase2 */ |
| 1015 | wpf--; |
| 1016 | regs->rcr2 |= RFRLEN2(wpf - 1); |
| 1017 | regs->xcr2 |= XFRLEN2(wpf - 1); |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 1018 | } |
| 1019 | |
Graeme Gregory | 5f63ef9 | 2009-11-09 19:02:15 +0000 | [diff] [blame] | 1020 | regs->rcr1 |= RFRLEN1(wpf - 1); |
| 1021 | regs->xcr1 |= XFRLEN1(wpf - 1); |
| 1022 | |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 1023 | switch (params_format(params)) { |
| 1024 | case SNDRV_PCM_FORMAT_S16_LE: |
| 1025 | /* Set word lengths */ |
| 1026 | regs->rcr2 |= RWDLEN2(OMAP_MCBSP_WORD_16); |
| 1027 | regs->rcr1 |= RWDLEN1(OMAP_MCBSP_WORD_16); |
| 1028 | regs->xcr2 |= XWDLEN2(OMAP_MCBSP_WORD_16); |
| 1029 | regs->xcr1 |= XWDLEN1(OMAP_MCBSP_WORD_16); |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 1030 | break; |
Sergey Lapin | d98508a | 2010-05-13 19:48:16 +0400 | [diff] [blame] | 1031 | case SNDRV_PCM_FORMAT_S32_LE: |
| 1032 | /* Set word lengths */ |
Sergey Lapin | d98508a | 2010-05-13 19:48:16 +0400 | [diff] [blame] | 1033 | regs->rcr2 |= RWDLEN2(OMAP_MCBSP_WORD_32); |
| 1034 | regs->rcr1 |= RWDLEN1(OMAP_MCBSP_WORD_32); |
| 1035 | regs->xcr2 |= XWDLEN2(OMAP_MCBSP_WORD_32); |
| 1036 | regs->xcr1 |= XWDLEN1(OMAP_MCBSP_WORD_32); |
| 1037 | break; |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 1038 | default: |
| 1039 | /* Unsupported PCM format */ |
| 1040 | return -EINVAL; |
| 1041 | } |
| 1042 | |
Graeme Gregory | 5f63ef9 | 2009-11-09 19:02:15 +0000 | [diff] [blame] | 1043 | /* In McBSP master modes, FRAME (i.e. sample rate) is generated |
| 1044 | * by _counting_ BCLKs. Calculate frame size in BCLKs */ |
Peter Ujfalusi | 256d9c2 | 2012-02-14 15:23:15 +0200 | [diff] [blame] | 1045 | master = mcbsp->fmt & SND_SOC_DAIFMT_MASTER_MASK; |
Graeme Gregory | 5f63ef9 | 2009-11-09 19:02:15 +0000 | [diff] [blame] | 1046 | if (master == SND_SOC_DAIFMT_CBS_CFS) { |
Peter Ujfalusi | 256d9c2 | 2012-02-14 15:23:15 +0200 | [diff] [blame] | 1047 | div = mcbsp->clk_div ? mcbsp->clk_div : 1; |
| 1048 | framesize = (mcbsp->in_freq / div) / params_rate(params); |
Graeme Gregory | 5f63ef9 | 2009-11-09 19:02:15 +0000 | [diff] [blame] | 1049 | |
| 1050 | if (framesize < wlen * channels) { |
| 1051 | printk(KERN_ERR "%s: not enough bandwidth for desired rate and " |
| 1052 | "channels\n", __func__); |
| 1053 | return -EINVAL; |
| 1054 | } |
| 1055 | } else |
| 1056 | framesize = wlen * channels; |
| 1057 | |
Jarkko Nikula | ba9d0fd | 2008-10-20 15:29:59 +0300 | [diff] [blame] | 1058 | /* Set FS period and length in terms of bit clock periods */ |
Jarkko Nikula | 4dd0417 | 2011-09-30 16:07:44 +0300 | [diff] [blame] | 1059 | regs->srgr2 &= ~FPER(0xfff); |
| 1060 | regs->srgr1 &= ~FWID(0xff); |
Peter Ujfalusi | c29b206 | 2009-04-15 15:38:55 +0300 | [diff] [blame] | 1061 | switch (format) { |
Jarkko Nikula | ba9d0fd | 2008-10-20 15:29:59 +0300 | [diff] [blame] | 1062 | case SND_SOC_DAIFMT_I2S: |
Peter Ujfalusi | 299a151 | 2010-03-19 12:27:31 +0200 | [diff] [blame] | 1063 | case SND_SOC_DAIFMT_LEFT_J: |
Graeme Gregory | 5f63ef9 | 2009-11-09 19:02:15 +0000 | [diff] [blame] | 1064 | regs->srgr2 |= FPER(framesize - 1); |
| 1065 | regs->srgr1 |= FWID((framesize >> 1) - 1); |
Jarkko Nikula | ba9d0fd | 2008-10-20 15:29:59 +0300 | [diff] [blame] | 1066 | break; |
Peter Ujfalusi | 3ba191c | 2009-04-15 15:38:56 +0300 | [diff] [blame] | 1067 | case SND_SOC_DAIFMT_DSP_A: |
Jarkko Nikula | bd25867 | 2008-12-22 10:21:36 +0200 | [diff] [blame] | 1068 | case SND_SOC_DAIFMT_DSP_B: |
Graeme Gregory | 5f63ef9 | 2009-11-09 19:02:15 +0000 | [diff] [blame] | 1069 | regs->srgr2 |= FPER(framesize - 1); |
Jarkko Nikula | 36ce858 | 2009-04-15 13:48:16 +0300 | [diff] [blame] | 1070 | regs->srgr1 |= FWID(0); |
Jarkko Nikula | ba9d0fd | 2008-10-20 15:29:59 +0300 | [diff] [blame] | 1071 | break; |
| 1072 | } |
| 1073 | |
Peter Ujfalusi | 256d9c2 | 2012-02-14 15:23:15 +0200 | [diff] [blame] | 1074 | omap_mcbsp_config(mcbsp, &mcbsp->cfg_regs); |
| 1075 | mcbsp->wlen = wlen; |
| 1076 | mcbsp->configured = 1; |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 1077 | |
| 1078 | return 0; |
| 1079 | } |
| 1080 | |
| 1081 | /* |
| 1082 | * This must be called before _set_clkdiv and _set_sysclk since McBSP register |
| 1083 | * cache is initialized here |
| 1084 | */ |
Liam Girdwood | 8687eb8 | 2008-07-07 16:08:07 +0100 | [diff] [blame] | 1085 | static int omap_mcbsp_dai_set_dai_fmt(struct snd_soc_dai *cpu_dai, |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 1086 | unsigned int fmt) |
| 1087 | { |
Peter Ujfalusi | 45656b4 | 2012-02-14 18:20:58 +0200 | [diff] [blame] | 1088 | struct omap_mcbsp *mcbsp = snd_soc_dai_get_drvdata(cpu_dai); |
Peter Ujfalusi | 256d9c2 | 2012-02-14 15:23:15 +0200 | [diff] [blame] | 1089 | struct omap_mcbsp_reg_cfg *regs = &mcbsp->cfg_regs; |
Jarkko Nikula | 91a18ae | 2011-09-30 10:55:32 +0300 | [diff] [blame] | 1090 | bool inv_fs = false; |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 1091 | |
Peter Ujfalusi | 256d9c2 | 2012-02-14 15:23:15 +0200 | [diff] [blame] | 1092 | if (mcbsp->configured) |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 1093 | return 0; |
| 1094 | |
Peter Ujfalusi | 256d9c2 | 2012-02-14 15:23:15 +0200 | [diff] [blame] | 1095 | mcbsp->fmt = fmt; |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 1096 | memset(regs, 0, sizeof(*regs)); |
| 1097 | /* Generic McBSP register settings */ |
| 1098 | regs->spcr2 |= XINTM(3) | FREE; |
| 1099 | regs->spcr1 |= RINTM(3); |
Peter Ujfalusi | dc26df5 | 2012-08-16 16:41:06 +0300 | [diff] [blame] | 1100 | /* RFIG and XFIG are not defined in 2430 and on OMAP3+ */ |
| 1101 | if (!mcbsp->pdata->has_ccr) { |
Eero Nurkkala | c721bbd | 2009-08-20 16:18:23 +0300 | [diff] [blame] | 1102 | regs->rcr2 |= RFIG; |
| 1103 | regs->xcr2 |= XFIG; |
| 1104 | } |
Peter Ujfalusi | dc26df5 | 2012-08-16 16:41:06 +0300 | [diff] [blame] | 1105 | |
| 1106 | /* Configure XCCR/RCCR only for revisions which have ccr registers */ |
| 1107 | if (mcbsp->pdata->has_ccr) { |
Jarkko Nikula | 32080af | 2009-08-23 12:24:26 +0300 | [diff] [blame] | 1108 | regs->xccr = DXENDLY(1) | XDMAEN | XDISABLE; |
| 1109 | regs->rccr = RFULL_CYCLE | RDMAEN | RDISABLE; |
Misael Lopez Cruz | ef390c0 | 2009-01-29 13:29:46 +0200 | [diff] [blame] | 1110 | } |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 1111 | |
| 1112 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { |
| 1113 | case SND_SOC_DAIFMT_I2S: |
| 1114 | /* 1-bit data delay */ |
| 1115 | regs->rcr2 |= RDATDLY(1); |
| 1116 | regs->xcr2 |= XDATDLY(1); |
| 1117 | break; |
Peter Ujfalusi | 299a151 | 2010-03-19 12:27:31 +0200 | [diff] [blame] | 1118 | case SND_SOC_DAIFMT_LEFT_J: |
| 1119 | /* 0-bit data delay */ |
| 1120 | regs->rcr2 |= RDATDLY(0); |
| 1121 | regs->xcr2 |= XDATDLY(0); |
| 1122 | regs->spcr1 |= RJUST(2); |
| 1123 | /* Invert FS polarity configuration */ |
Jarkko Nikula | 91a18ae | 2011-09-30 10:55:32 +0300 | [diff] [blame] | 1124 | inv_fs = true; |
Peter Ujfalusi | 299a151 | 2010-03-19 12:27:31 +0200 | [diff] [blame] | 1125 | break; |
Peter Ujfalusi | 3ba191c | 2009-04-15 15:38:56 +0300 | [diff] [blame] | 1126 | case SND_SOC_DAIFMT_DSP_A: |
| 1127 | /* 1-bit data delay */ |
| 1128 | regs->rcr2 |= RDATDLY(1); |
| 1129 | regs->xcr2 |= XDATDLY(1); |
| 1130 | /* Invert FS polarity configuration */ |
Jarkko Nikula | 91a18ae | 2011-09-30 10:55:32 +0300 | [diff] [blame] | 1131 | inv_fs = true; |
Peter Ujfalusi | 3ba191c | 2009-04-15 15:38:56 +0300 | [diff] [blame] | 1132 | break; |
Jarkko Nikula | bd25867 | 2008-12-22 10:21:36 +0200 | [diff] [blame] | 1133 | case SND_SOC_DAIFMT_DSP_B: |
Arun KS | 3336c5b | 2008-10-02 15:07:06 +0530 | [diff] [blame] | 1134 | /* 0-bit data delay */ |
| 1135 | regs->rcr2 |= RDATDLY(0); |
| 1136 | regs->xcr2 |= XDATDLY(0); |
Jarkko Nikula | 36ce858 | 2009-04-15 13:48:16 +0300 | [diff] [blame] | 1137 | /* Invert FS polarity configuration */ |
Jarkko Nikula | 91a18ae | 2011-09-30 10:55:32 +0300 | [diff] [blame] | 1138 | inv_fs = true; |
Arun KS | 3336c5b | 2008-10-02 15:07:06 +0530 | [diff] [blame] | 1139 | break; |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 1140 | default: |
| 1141 | /* Unsupported data format */ |
| 1142 | return -EINVAL; |
| 1143 | } |
| 1144 | |
| 1145 | switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { |
| 1146 | case SND_SOC_DAIFMT_CBS_CFS: |
| 1147 | /* McBSP master. Set FS and bit clocks as outputs */ |
| 1148 | regs->pcr0 |= FSXM | FSRM | |
| 1149 | CLKXM | CLKRM; |
| 1150 | /* Sample rate generator drives the FS */ |
| 1151 | regs->srgr2 |= FSGM; |
| 1152 | break; |
Michael Trimarchi | 6e20b0d | 2013-07-21 18:24:01 +0200 | [diff] [blame] | 1153 | case SND_SOC_DAIFMT_CBM_CFS: |
| 1154 | /* McBSP slave. FS clock as output */ |
| 1155 | regs->srgr2 |= FSGM; |
Peter Ujfalusi | 20602e3 | 2015-01-16 11:20:25 +0200 | [diff] [blame] | 1156 | regs->pcr0 |= FSXM | FSRM; |
Michael Trimarchi | 6e20b0d | 2013-07-21 18:24:01 +0200 | [diff] [blame] | 1157 | break; |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 1158 | case SND_SOC_DAIFMT_CBM_CFM: |
| 1159 | /* McBSP slave */ |
| 1160 | break; |
| 1161 | default: |
| 1162 | /* Unsupported master/slave configuration */ |
| 1163 | return -EINVAL; |
| 1164 | } |
| 1165 | |
| 1166 | /* Set bit clock (CLKX/CLKR) and FS polarities */ |
Jarkko Nikula | 91a18ae | 2011-09-30 10:55:32 +0300 | [diff] [blame] | 1167 | switch (fmt & SND_SOC_DAIFMT_INV_MASK) { |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 1168 | case SND_SOC_DAIFMT_NB_NF: |
| 1169 | /* |
| 1170 | * Normal BCLK + FS. |
| 1171 | * FS active low. TX data driven on falling edge of bit clock |
| 1172 | * and RX data sampled on rising edge of bit clock. |
| 1173 | */ |
| 1174 | regs->pcr0 |= FSXP | FSRP | |
| 1175 | CLKXP | CLKRP; |
| 1176 | break; |
| 1177 | case SND_SOC_DAIFMT_NB_IF: |
| 1178 | regs->pcr0 |= CLKXP | CLKRP; |
| 1179 | break; |
| 1180 | case SND_SOC_DAIFMT_IB_NF: |
| 1181 | regs->pcr0 |= FSXP | FSRP; |
| 1182 | break; |
| 1183 | case SND_SOC_DAIFMT_IB_IF: |
| 1184 | break; |
| 1185 | default: |
| 1186 | return -EINVAL; |
| 1187 | } |
Jarkko Nikula | 91a18ae | 2011-09-30 10:55:32 +0300 | [diff] [blame] | 1188 | if (inv_fs == true) |
| 1189 | regs->pcr0 ^= FSXP | FSRP; |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 1190 | |
| 1191 | return 0; |
| 1192 | } |
| 1193 | |
Liam Girdwood | 8687eb8 | 2008-07-07 16:08:07 +0100 | [diff] [blame] | 1194 | static int omap_mcbsp_dai_set_clkdiv(struct snd_soc_dai *cpu_dai, |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 1195 | int div_id, int div) |
| 1196 | { |
Peter Ujfalusi | 45656b4 | 2012-02-14 18:20:58 +0200 | [diff] [blame] | 1197 | struct omap_mcbsp *mcbsp = snd_soc_dai_get_drvdata(cpu_dai); |
Peter Ujfalusi | 256d9c2 | 2012-02-14 15:23:15 +0200 | [diff] [blame] | 1198 | struct omap_mcbsp_reg_cfg *regs = &mcbsp->cfg_regs; |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 1199 | |
| 1200 | if (div_id != OMAP_MCBSP_CLKGDV) |
| 1201 | return -ENODEV; |
| 1202 | |
Peter Ujfalusi | 256d9c2 | 2012-02-14 15:23:15 +0200 | [diff] [blame] | 1203 | mcbsp->clk_div = div; |
Jarkko Nikula | 4dd0417 | 2011-09-30 16:07:44 +0300 | [diff] [blame] | 1204 | regs->srgr1 &= ~CLKGDV(0xff); |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 1205 | regs->srgr1 |= CLKGDV(div - 1); |
| 1206 | |
| 1207 | return 0; |
| 1208 | } |
| 1209 | |
Liam Girdwood | 8687eb8 | 2008-07-07 16:08:07 +0100 | [diff] [blame] | 1210 | static int omap_mcbsp_dai_set_dai_sysclk(struct snd_soc_dai *cpu_dai, |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 1211 | int clk_id, unsigned int freq, |
| 1212 | int dir) |
| 1213 | { |
Peter Ujfalusi | 45656b4 | 2012-02-14 18:20:58 +0200 | [diff] [blame] | 1214 | struct omap_mcbsp *mcbsp = snd_soc_dai_get_drvdata(cpu_dai); |
Peter Ujfalusi | 256d9c2 | 2012-02-14 15:23:15 +0200 | [diff] [blame] | 1215 | struct omap_mcbsp_reg_cfg *regs = &mcbsp->cfg_regs; |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 1216 | int err = 0; |
| 1217 | |
Peter Ujfalusi | 256d9c2 | 2012-02-14 15:23:15 +0200 | [diff] [blame] | 1218 | if (mcbsp->active) { |
| 1219 | if (freq == mcbsp->in_freq) |
Jarkko Nikula | 34c8698 | 2011-09-23 11:19:13 +0300 | [diff] [blame] | 1220 | return 0; |
| 1221 | else |
| 1222 | return -EBUSY; |
Peter Ujfalusi | 141947e | 2011-09-26 10:56:42 +0300 | [diff] [blame] | 1223 | } |
Jarkko Nikula | 34c8698 | 2011-09-23 11:19:13 +0300 | [diff] [blame] | 1224 | |
Peter Ujfalusi | 8fef626 | 2012-08-16 16:41:04 +0300 | [diff] [blame] | 1225 | mcbsp->in_freq = freq; |
| 1226 | regs->srgr2 &= ~CLKSM; |
| 1227 | regs->pcr0 &= ~SCLKME; |
Graeme Gregory | 5f63ef9 | 2009-11-09 19:02:15 +0000 | [diff] [blame] | 1228 | |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 1229 | switch (clk_id) { |
| 1230 | case OMAP_MCBSP_SYSCLK_CLK: |
| 1231 | regs->srgr2 |= CLKSM; |
| 1232 | break; |
| 1233 | case OMAP_MCBSP_SYSCLK_CLKS_FCLK: |
Tony Lindgren | e650794 | 2012-11-21 09:42:25 -0800 | [diff] [blame] | 1234 | if (mcbsp_omap1()) { |
Paul Walmsley | d1358657 | 2010-10-08 11:40:19 -0600 | [diff] [blame] | 1235 | err = -EINVAL; |
| 1236 | break; |
| 1237 | } |
Peter Ujfalusi | 45656b4 | 2012-02-14 18:20:58 +0200 | [diff] [blame] | 1238 | err = omap2_mcbsp_set_clks_src(mcbsp, |
Paul Walmsley | d1358657 | 2010-10-08 11:40:19 -0600 | [diff] [blame] | 1239 | MCBSP_CLKS_PRCM_SRC); |
| 1240 | break; |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 1241 | case OMAP_MCBSP_SYSCLK_CLKS_EXT: |
Tony Lindgren | e650794 | 2012-11-21 09:42:25 -0800 | [diff] [blame] | 1242 | if (mcbsp_omap1()) { |
Paul Walmsley | d1358657 | 2010-10-08 11:40:19 -0600 | [diff] [blame] | 1243 | err = 0; |
| 1244 | break; |
| 1245 | } |
Peter Ujfalusi | 45656b4 | 2012-02-14 18:20:58 +0200 | [diff] [blame] | 1246 | err = omap2_mcbsp_set_clks_src(mcbsp, |
Paul Walmsley | d1358657 | 2010-10-08 11:40:19 -0600 | [diff] [blame] | 1247 | MCBSP_CLKS_PAD_SRC); |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 1248 | break; |
| 1249 | |
| 1250 | case OMAP_MCBSP_SYSCLK_CLKX_EXT: |
| 1251 | regs->srgr2 |= CLKSM; |
Thomas Niederprüm | 8af4baa | 2015-02-21 18:11:29 +0100 | [diff] [blame] | 1252 | regs->pcr0 |= SCLKME; |
| 1253 | /* |
| 1254 | * If McBSP is master but yet the CLKX/CLKR pin drives the SRG, |
| 1255 | * disable output on those pins. This enables to inject the |
| 1256 | * reference clock through CLKX/CLKR. For this to work |
| 1257 | * set_dai_sysclk() _needs_ to be called after set_dai_fmt(). |
| 1258 | */ |
| 1259 | regs->pcr0 &= ~CLKXM; |
| 1260 | break; |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 1261 | case OMAP_MCBSP_SYSCLK_CLKR_EXT: |
| 1262 | regs->pcr0 |= SCLKME; |
Thomas Niederprüm | 8af4baa | 2015-02-21 18:11:29 +0100 | [diff] [blame] | 1263 | /* Disable ouput on CLKR pin in master mode */ |
| 1264 | regs->pcr0 &= ~CLKRM; |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 1265 | break; |
| 1266 | default: |
| 1267 | err = -ENODEV; |
| 1268 | } |
| 1269 | |
| 1270 | return err; |
| 1271 | } |
| 1272 | |
Lars-Peter Clausen | 85e7652 | 2011-11-23 11:40:40 +0100 | [diff] [blame] | 1273 | static const struct snd_soc_dai_ops mcbsp_dai_ops = { |
Eric Miao | 6335d05 | 2009-03-03 09:41:00 +0800 | [diff] [blame] | 1274 | .startup = omap_mcbsp_dai_startup, |
| 1275 | .shutdown = omap_mcbsp_dai_shutdown, |
Matt Ranostay | 9834ffd | 2017-01-31 13:21:43 -0800 | [diff] [blame] | 1276 | .prepare = omap_mcbsp_dai_prepare, |
Eric Miao | 6335d05 | 2009-03-03 09:41:00 +0800 | [diff] [blame] | 1277 | .trigger = omap_mcbsp_dai_trigger, |
Peter Ujfalusi | 75581d2 | 2010-03-03 15:08:09 +0200 | [diff] [blame] | 1278 | .delay = omap_mcbsp_dai_delay, |
Eric Miao | 6335d05 | 2009-03-03 09:41:00 +0800 | [diff] [blame] | 1279 | .hw_params = omap_mcbsp_dai_hw_params, |
| 1280 | .set_fmt = omap_mcbsp_dai_set_dai_fmt, |
| 1281 | .set_clkdiv = omap_mcbsp_dai_set_clkdiv, |
| 1282 | .set_sysclk = omap_mcbsp_dai_set_dai_sysclk, |
| 1283 | }; |
| 1284 | |
Peter Ujfalusi | 2ee6595 | 2012-02-14 14:52:42 +0200 | [diff] [blame] | 1285 | static int omap_mcbsp_probe(struct snd_soc_dai *dai) |
| 1286 | { |
| 1287 | struct omap_mcbsp *mcbsp = snd_soc_dai_get_drvdata(dai); |
| 1288 | |
| 1289 | pm_runtime_enable(mcbsp->dev); |
| 1290 | |
Peter Ujfalusi | 3fe856b | 2014-04-16 15:46:15 +0300 | [diff] [blame] | 1291 | snd_soc_dai_init_dma_data(dai, |
| 1292 | &mcbsp->dma_data[SNDRV_PCM_STREAM_PLAYBACK], |
| 1293 | &mcbsp->dma_data[SNDRV_PCM_STREAM_CAPTURE]); |
| 1294 | |
Peter Ujfalusi | 2ee6595 | 2012-02-14 14:52:42 +0200 | [diff] [blame] | 1295 | return 0; |
| 1296 | } |
| 1297 | |
| 1298 | static int omap_mcbsp_remove(struct snd_soc_dai *dai) |
| 1299 | { |
| 1300 | struct omap_mcbsp *mcbsp = snd_soc_dai_get_drvdata(dai); |
| 1301 | |
| 1302 | pm_runtime_disable(mcbsp->dev); |
| 1303 | |
| 1304 | return 0; |
| 1305 | } |
| 1306 | |
Michael Opdenacker | 6179b77 | 2011-10-10 07:07:08 +0200 | [diff] [blame] | 1307 | static struct snd_soc_dai_driver omap_mcbsp_dai = { |
Peter Ujfalusi | 2ee6595 | 2012-02-14 14:52:42 +0200 | [diff] [blame] | 1308 | .probe = omap_mcbsp_probe, |
| 1309 | .remove = omap_mcbsp_remove, |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1310 | .playback = { |
| 1311 | .channels_min = 1, |
| 1312 | .channels_max = 16, |
| 1313 | .rates = OMAP_MCBSP_RATES, |
| 1314 | .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE, |
| 1315 | }, |
| 1316 | .capture = { |
| 1317 | .channels_min = 1, |
| 1318 | .channels_max = 16, |
| 1319 | .rates = OMAP_MCBSP_RATES, |
| 1320 | .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE, |
| 1321 | }, |
| 1322 | .ops = &mcbsp_dai_ops, |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 1323 | }; |
Jarkko Nikula | 8def464 | 2008-10-09 15:57:22 +0300 | [diff] [blame] | 1324 | |
Kuninori Morimoto | 43cd814 | 2013-03-21 03:33:25 -0700 | [diff] [blame] | 1325 | static const struct snd_soc_component_driver omap_mcbsp_component = { |
| 1326 | .name = "omap-mcbsp", |
| 1327 | }; |
| 1328 | |
Peter Ujfalusi | 11dd586 | 2012-08-16 16:41:08 +0300 | [diff] [blame] | 1329 | static struct omap_mcbsp_platform_data omap2420_pdata = { |
| 1330 | .reg_step = 4, |
| 1331 | .reg_size = 2, |
| 1332 | }; |
| 1333 | |
| 1334 | static struct omap_mcbsp_platform_data omap2430_pdata = { |
| 1335 | .reg_step = 4, |
| 1336 | .reg_size = 4, |
| 1337 | .has_ccr = true, |
| 1338 | }; |
| 1339 | |
| 1340 | static struct omap_mcbsp_platform_data omap3_pdata = { |
| 1341 | .reg_step = 4, |
| 1342 | .reg_size = 4, |
| 1343 | .has_ccr = true, |
| 1344 | .has_wakeup = true, |
| 1345 | }; |
| 1346 | |
| 1347 | static struct omap_mcbsp_platform_data omap4_pdata = { |
| 1348 | .reg_step = 4, |
| 1349 | .reg_size = 4, |
| 1350 | .has_ccr = true, |
| 1351 | .has_wakeup = true, |
| 1352 | }; |
| 1353 | |
| 1354 | static const struct of_device_id omap_mcbsp_of_match[] = { |
| 1355 | { |
| 1356 | .compatible = "ti,omap2420-mcbsp", |
| 1357 | .data = &omap2420_pdata, |
| 1358 | }, |
| 1359 | { |
| 1360 | .compatible = "ti,omap2430-mcbsp", |
| 1361 | .data = &omap2430_pdata, |
| 1362 | }, |
| 1363 | { |
| 1364 | .compatible = "ti,omap3-mcbsp", |
| 1365 | .data = &omap3_pdata, |
| 1366 | }, |
| 1367 | { |
| 1368 | .compatible = "ti,omap4-mcbsp", |
| 1369 | .data = &omap4_pdata, |
| 1370 | }, |
| 1371 | { }, |
| 1372 | }; |
| 1373 | MODULE_DEVICE_TABLE(of, omap_mcbsp_of_match); |
| 1374 | |
Bill Pemberton | 7ff6000 | 2012-12-07 09:26:29 -0500 | [diff] [blame] | 1375 | static int asoc_mcbsp_probe(struct platform_device *pdev) |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1376 | { |
Peter Ujfalusi | 2ee6595 | 2012-02-14 14:52:42 +0200 | [diff] [blame] | 1377 | struct omap_mcbsp_platform_data *pdata = dev_get_platdata(&pdev->dev); |
| 1378 | struct omap_mcbsp *mcbsp; |
Peter Ujfalusi | 11dd586 | 2012-08-16 16:41:08 +0300 | [diff] [blame] | 1379 | const struct of_device_id *match; |
Peter Ujfalusi | 45656b4 | 2012-02-14 18:20:58 +0200 | [diff] [blame] | 1380 | int ret; |
| 1381 | |
Peter Ujfalusi | 11dd586 | 2012-08-16 16:41:08 +0300 | [diff] [blame] | 1382 | match = of_match_device(omap_mcbsp_of_match, &pdev->dev); |
| 1383 | if (match) { |
| 1384 | struct device_node *node = pdev->dev.of_node; |
Peter Ujfalusi | bbfa26c | 2016-05-30 11:23:49 +0300 | [diff] [blame] | 1385 | struct omap_mcbsp_platform_data *pdata_quirk = pdata; |
Peter Ujfalusi | 11dd586 | 2012-08-16 16:41:08 +0300 | [diff] [blame] | 1386 | int buffer_size; |
| 1387 | |
| 1388 | pdata = devm_kzalloc(&pdev->dev, |
| 1389 | sizeof(struct omap_mcbsp_platform_data), |
| 1390 | GFP_KERNEL); |
| 1391 | if (!pdata) |
| 1392 | return -ENOMEM; |
| 1393 | |
| 1394 | memcpy(pdata, match->data, sizeof(*pdata)); |
| 1395 | if (!of_property_read_u32(node, "ti,buffer-size", &buffer_size)) |
| 1396 | pdata->buffer_size = buffer_size; |
Peter Ujfalusi | bbfa26c | 2016-05-30 11:23:49 +0300 | [diff] [blame] | 1397 | if (pdata_quirk) |
| 1398 | pdata->force_ick_on = pdata_quirk->force_ick_on; |
Peter Ujfalusi | 11dd586 | 2012-08-16 16:41:08 +0300 | [diff] [blame] | 1399 | } else if (!pdata) { |
Peter Ujfalusi | 2ee6595 | 2012-02-14 14:52:42 +0200 | [diff] [blame] | 1400 | dev_err(&pdev->dev, "missing platform data.\n"); |
| 1401 | return -EINVAL; |
| 1402 | } |
| 1403 | mcbsp = devm_kzalloc(&pdev->dev, sizeof(struct omap_mcbsp), GFP_KERNEL); |
| 1404 | if (!mcbsp) |
| 1405 | return -ENOMEM; |
| 1406 | |
| 1407 | mcbsp->id = pdev->id; |
| 1408 | mcbsp->pdata = pdata; |
| 1409 | mcbsp->dev = &pdev->dev; |
| 1410 | platform_set_drvdata(pdev, mcbsp); |
| 1411 | |
| 1412 | ret = omap_mcbsp_init(pdev); |
Peter Ujfalusi | 6424142 | 2014-04-16 15:46:16 +0300 | [diff] [blame] | 1413 | if (ret) |
| 1414 | return ret; |
Peter Ujfalusi | 45656b4 | 2012-02-14 18:20:58 +0200 | [diff] [blame] | 1415 | |
Peter Ujfalusi | 61f18dc | 2018-10-25 16:48:24 +0300 | [diff] [blame] | 1416 | if (mcbsp->pdata->reg_size == 2) { |
| 1417 | omap_mcbsp_dai.playback.formats = SNDRV_PCM_FMTBIT_S16_LE; |
| 1418 | omap_mcbsp_dai.capture.formats = SNDRV_PCM_FMTBIT_S16_LE; |
| 1419 | } |
| 1420 | |
Manish Badarkhe | 36765c9 | 2014-07-08 21:55:23 +0530 | [diff] [blame] | 1421 | ret = devm_snd_soc_register_component(&pdev->dev, |
| 1422 | &omap_mcbsp_component, |
| 1423 | &omap_mcbsp_dai, 1); |
Peter Ujfalusi | 6424142 | 2014-04-16 15:46:16 +0300 | [diff] [blame] | 1424 | if (ret) |
| 1425 | return ret; |
| 1426 | |
Peter Ujfalusi | 0198d7b | 2018-05-07 11:49:59 +0300 | [diff] [blame] | 1427 | return sdma_pcm_platform_register(&pdev->dev, NULL, NULL); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1428 | } |
| 1429 | |
Bill Pemberton | 7ff6000 | 2012-12-07 09:26:29 -0500 | [diff] [blame] | 1430 | static int asoc_mcbsp_remove(struct platform_device *pdev) |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1431 | { |
Peter Ujfalusi | 2ee6595 | 2012-02-14 14:52:42 +0200 | [diff] [blame] | 1432 | struct omap_mcbsp *mcbsp = platform_get_drvdata(pdev); |
| 1433 | |
Peter Ujfalusi | 2ee6595 | 2012-02-14 14:52:42 +0200 | [diff] [blame] | 1434 | if (mcbsp->pdata->ops && mcbsp->pdata->ops->free) |
| 1435 | mcbsp->pdata->ops->free(mcbsp->id); |
| 1436 | |
Matt Ranostay | 9834ffd | 2017-01-31 13:21:43 -0800 | [diff] [blame] | 1437 | if (pm_qos_request_active(&mcbsp->pm_qos_req)) |
| 1438 | pm_qos_remove_request(&mcbsp->pm_qos_req); |
| 1439 | |
Peter Ujfalusi | 9c34d02 | 2018-11-08 09:29:58 +0200 | [diff] [blame] | 1440 | if (mcbsp->pdata->buffer_size) |
| 1441 | sysfs_remove_group(&mcbsp->dev->kobj, &additional_attr_group); |
| 1442 | |
| 1443 | omap_mcbsp_st_cleanup(pdev); |
Peter Ujfalusi | 2ee6595 | 2012-02-14 14:52:42 +0200 | [diff] [blame] | 1444 | |
| 1445 | clk_put(mcbsp->fclk); |
| 1446 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1447 | return 0; |
| 1448 | } |
| 1449 | |
| 1450 | static struct platform_driver asoc_mcbsp_driver = { |
| 1451 | .driver = { |
Peter Ujfalusi | 45656b4 | 2012-02-14 18:20:58 +0200 | [diff] [blame] | 1452 | .name = "omap-mcbsp", |
Peter Ujfalusi | 11dd586 | 2012-08-16 16:41:08 +0300 | [diff] [blame] | 1453 | .of_match_table = omap_mcbsp_of_match, |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1454 | }, |
| 1455 | |
| 1456 | .probe = asoc_mcbsp_probe, |
Bill Pemberton | 7ff6000 | 2012-12-07 09:26:29 -0500 | [diff] [blame] | 1457 | .remove = asoc_mcbsp_remove, |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1458 | }; |
| 1459 | |
Axel Lin | beda5bf5 | 2011-11-25 10:12:16 +0800 | [diff] [blame] | 1460 | module_platform_driver(asoc_mcbsp_driver); |
Mark Brown | 3f4b783 | 2008-12-03 19:26:35 +0000 | [diff] [blame] | 1461 | |
Jarkko Nikula | 7ec41ee | 2011-08-11 15:44:57 +0300 | [diff] [blame] | 1462 | MODULE_AUTHOR("Jarkko Nikula <jarkko.nikula@bitmer.com>"); |
Jarkko Nikula | 2e74796 | 2008-04-25 13:55:19 +0200 | [diff] [blame] | 1463 | MODULE_DESCRIPTION("OMAP I2S SoC Interface"); |
| 1464 | MODULE_LICENSE("GPL"); |
Guillaume Gardet | 5e70b7fc | 2012-07-12 15:08:16 +0200 | [diff] [blame] | 1465 | MODULE_ALIAS("platform:omap-mcbsp"); |