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