blob: 8cab1151d3bb358e085bee898dbef90ba2ebef57 [file] [log] [blame]
Jarkko Nikula2e747962008-04-25 13:55:19 +02001/*
2 * omap-mcbsp.c -- OMAP ALSA SoC DAI driver using McBSP port
3 *
4 * Copyright (C) 2008 Nokia Corporation
5 *
Jarkko Nikula7ec41ee2011-08-11 15:44:57 +03006 * Contact: Jarkko Nikula <jarkko.nikula@bitmer.com>
Peter Ujfalusi56a87422011-05-03 18:14:06 +03007 * Peter Ujfalusi <peter.ujfalusi@ti.com>
Jarkko Nikula2e747962008-04-25 13:55:19 +02008 *
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 Ujfalusi2ee65952012-02-14 14:52:42 +020028#include <linux/pm_runtime.h>
Peter Ujfalusi11dd5862012-08-16 16:41:08 +030029#include <linux/of.h>
30#include <linux/of_device.h>
Jarkko Nikula2e747962008-04-25 13:55:19 +020031#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 Clausen09ae3aa2013-04-03 11:06:05 +020036#include <sound/dmaengine_pcm.h>
Jarkko Nikula2e747962008-04-25 13:55:19 +020037
Peter Ujfalusi9c34d022018-11-08 09:29:58 +020038#include "omap-mcbsp-priv.h"
Jarkko Nikula2e747962008-04-25 13:55:19 +020039#include "omap-mcbsp.h"
Peter Ujfalusi0198d7b2018-05-07 11:49:59 +030040#include "sdma-pcm.h"
Jarkko Nikula2e747962008-04-25 13:55:19 +020041
Jarkko Nikula0b604852008-11-12 17:05:51 +020042#define OMAP_MCBSP_RATES (SNDRV_PCM_RATE_8000_96000)
Jarkko Nikula2e747962008-04-25 13:55:19 +020043
Peter Ujfalusi219f4312012-02-03 13:11:47 +020044enum {
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 Ujfalusi9c34d022018-11-08 09:29:58 +020053static 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
72static 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
108static 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
149static 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
167static 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 */
191static 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 */
231static 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 */
256static 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 */
267static 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 */
276static 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 */
291static 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
307static 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 Ujfalusi465d85b2018-11-08 09:29:59 +0200327 if(mcbsp->pdata->ops && mcbsp->pdata->ops->request)
Peter Ujfalusi9c34d022018-11-08 09:29:58 +0200328 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;
361err_free_irq:
362 free_irq(mcbsp->tx_irq, (void *)mcbsp);
363err_clk_disable:
Peter Ujfalusi465d85b2018-11-08 09:29:59 +0200364 if(mcbsp->pdata->ops && mcbsp->pdata->ops->free)
Peter Ujfalusi9c34d022018-11-08 09:29:58 +0200365 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;
374err_kfree:
375 spin_unlock(&mcbsp->lock);
376 kfree(reg_cache);
377
378 return err;
379}
380
381static void omap_mcbsp_free(struct omap_mcbsp *mcbsp)
382{
383 void *reg_cache;
384
Peter Ujfalusi465d85b2018-11-08 09:29:59 +0200385 if(mcbsp->pdata->ops && mcbsp->pdata->ops->free)
Peter Ujfalusi9c34d022018-11-08 09:29:58 +0200386 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 */
431static 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
490static 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) \
533static 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 \
541static 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 \
560static DEVICE_ATTR(prop, 0644, prop##_show, prop##_store)
561
562THRESHOLD_PROP_BUILDER(max_tx_thres);
563THRESHOLD_PROP_BUILDER(max_rx_thres);
564
565static const char * const dma_op_modes[] = {
566 "element", "threshold",
567};
568
569static 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
590static 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
608unlock:
609 spin_unlock_irq(&mcbsp->lock);
610
611 return size;
612}
613
614static DEVICE_ATTR_RW(dma_op_mode);
615
616static 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
623static 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 */
631static 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
741err_st:
742 if (mcbsp->pdata->buffer_size)
743 sysfs_remove_group(&mcbsp->dev->kobj, &additional_attr_group);
744err_thres:
745 clk_put(mcbsp->fclk);
746 return ret;
747}
748
Jarkko Nikula2e747962008-04-25 13:55:19 +0200749/*
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 Clausenabe99372013-03-25 16:58:16 +0100753static void omap_mcbsp_set_threshold(struct snd_pcm_substream *substream,
754 unsigned int packet_size)
Eduardo Valentincaebc0c2009-08-20 16:18:25 +0300755{
756 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000757 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Peter Ujfalusi45656b42012-02-14 18:20:58 +0200758 struct omap_mcbsp *mcbsp = snd_soc_dai_get_drvdata(cpu_dai);
Peter Ujfalusi3f024032010-06-03 07:39:35 +0300759 int words;
Eduardo Valentina0a499c2009-08-20 16:18:26 +0300760
Peter Ujfalusibe51c572018-11-08 09:29:57 +0200761 /* No need to proceed further if McBSP does not have FIFO */
762 if (mcbsp->pdata->buffer_size == 0)
763 return;
764
Peter Ujfalusi778a17c2012-03-15 12:20:32 +0200765 /*
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 Clausenabe99372013-03-25 16:58:16 +0100771 if (packet_size)
772 words = packet_size;
Eduardo Valentina0a499c2009-08-20 16:18:26 +0300773 else
Peter Ujfalusi3f024032010-06-03 07:39:35 +0300774 words = 1;
Eduardo Valentincaebc0c2009-08-20 16:18:25 +0300775
776 /* Configure McBSP internal buffer usage */
777 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Peter Ujfalusi45656b42012-02-14 18:20:58 +0200778 omap_mcbsp_set_tx_threshold(mcbsp, words);
Eduardo Valentincaebc0c2009-08-20 16:18:25 +0300779 else
Peter Ujfalusi45656b42012-02-14 18:20:58 +0200780 omap_mcbsp_set_rx_threshold(mcbsp, words);
Eduardo Valentincaebc0c2009-08-20 16:18:25 +0300781}
782
Peter Ujfalusiddc29b02010-06-03 07:39:36 +0300783static 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 Ujfalusi45656b42012-02-14 18:20:58 +0200790 struct omap_mcbsp *mcbsp = rule->private;
Peter Ujfalusiddc29b02010-06-03 07:39:36 +0300791 struct snd_interval frames;
792 int size;
793
794 snd_interval_any(&frames);
Peter Ujfalusicb40b632012-02-13 16:26:54 +0200795 size = mcbsp->pdata->buffer_size;
Peter Ujfalusiddc29b02010-06-03 07:39:36 +0300796
797 frames.min = size / channels->min;
798 frames.integer = 1;
799 return snd_interval_refine(buffer_size, &frames);
800}
801
Mark Browndee89c42008-11-18 22:11:38 +0000802static int omap_mcbsp_dai_startup(struct snd_pcm_substream *substream,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000803 struct snd_soc_dai *cpu_dai)
Jarkko Nikula2e747962008-04-25 13:55:19 +0200804{
Peter Ujfalusi45656b42012-02-14 18:20:58 +0200805 struct omap_mcbsp *mcbsp = snd_soc_dai_get_drvdata(cpu_dai);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200806 int err = 0;
807
Eduardo Valentincaebc0c2009-08-20 16:18:25 +0300808 if (!cpu_dai->active)
Peter Ujfalusi45656b42012-02-14 18:20:58 +0200809 err = omap_mcbsp_request(mcbsp);
Eduardo Valentincaebc0c2009-08-20 16:18:25 +0300810
Peter Ujfalusiddc29b02010-06-03 07:39:36 +0300811 /*
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 Ujfalusi45656b42012-02-14 18:20:58 +0200826 if (mcbsp->pdata->buffer_size) {
Jarkko Nikula69849922009-03-27 15:32:01 +0200827 /*
Peter Ujfalusi998a8a62010-07-29 09:51:28 +0300828 * Rule for the buffer size. We should not allow
Peter Ujfalusice37f5e2012-03-20 11:47:36 +0200829 * smaller buffer than the FIFO size to avoid underruns.
830 * This applies only for the playback stream.
Peter Ujfalusiddc29b02010-06-03 07:39:36 +0300831 */
Peter Ujfalusice37f5e2012-03-20 11:47:36 +0200832 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 Ujfalusiddc29b02010-06-03 07:39:36 +0300838
Peter Ujfalusi998a8a62010-07-29 09:51:28 +0300839 /* 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 Valentincaebc0c2009-08-20 16:18:25 +0300842 }
Jarkko Nikula2e747962008-04-25 13:55:19 +0200843
844 return err;
845}
846
Mark Browndee89c42008-11-18 22:11:38 +0000847static void omap_mcbsp_dai_shutdown(struct snd_pcm_substream *substream,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000848 struct snd_soc_dai *cpu_dai)
Jarkko Nikula2e747962008-04-25 13:55:19 +0200849{
Peter Ujfalusi45656b42012-02-14 18:20:58 +0200850 struct omap_mcbsp *mcbsp = snd_soc_dai_get_drvdata(cpu_dai);
Matt Ranostay9834ffd2017-01-31 13:21:43 -0800851 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 Nikula2e747962008-04-25 13:55:19 +0200862
863 if (!cpu_dai->active) {
Peter Ujfalusi45656b42012-02-14 18:20:58 +0200864 omap_mcbsp_free(mcbsp);
Peter Ujfalusi256d9c22012-02-14 15:23:15 +0200865 mcbsp->configured = 0;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200866 }
867}
868
Matt Ranostay9834ffd2017-01-31 13:21:43 -0800869static 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 Browndee89c42008-11-18 22:11:38 +0000891static int omap_mcbsp_dai_trigger(struct snd_pcm_substream *substream, int cmd,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000892 struct snd_soc_dai *cpu_dai)
Jarkko Nikula2e747962008-04-25 13:55:19 +0200893{
Peter Ujfalusi45656b42012-02-14 18:20:58 +0200894 struct omap_mcbsp *mcbsp = snd_soc_dai_get_drvdata(cpu_dai);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200895
896 switch (cmd) {
897 case SNDRV_PCM_TRIGGER_START:
898 case SNDRV_PCM_TRIGGER_RESUME:
899 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Peter Ujfalusi256d9c22012-02-14 15:23:15 +0200900 mcbsp->active++;
Peter Ujfalusi59d177f2018-11-08 09:29:56 +0200901 omap_mcbsp_start(mcbsp, substream->stream);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200902 break;
903
904 case SNDRV_PCM_TRIGGER_STOP:
905 case SNDRV_PCM_TRIGGER_SUSPEND:
906 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Peter Ujfalusi59d177f2018-11-08 09:29:56 +0200907 omap_mcbsp_stop(mcbsp, substream->stream);
Peter Ujfalusi256d9c22012-02-14 15:23:15 +0200908 mcbsp->active--;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200909 break;
910 default:
Peter Ujfalusi59d177f2018-11-08 09:29:56 +0200911 return -EINVAL;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200912 }
913
Peter Ujfalusi59d177f2018-11-08 09:29:56 +0200914 return 0;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200915}
916
Peter Ujfalusi75581d22010-03-03 15:08:09 +0200917static 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 Girdwoodf0fba2a2010-03-17 20:15:21 +0000922 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Peter Ujfalusi45656b42012-02-14 18:20:58 +0200923 struct omap_mcbsp *mcbsp = snd_soc_dai_get_drvdata(cpu_dai);
Peter Ujfalusi75581d22010-03-03 15:08:09 +0200924 u16 fifo_use;
925 snd_pcm_sframes_t delay;
926
Peter Ujfalusibe51c572018-11-08 09:29:57 +0200927 /* No need to proceed further if McBSP does not have FIFO */
928 if (mcbsp->pdata->buffer_size == 0)
929 return 0;
930
Peter Ujfalusi75581d22010-03-03 15:08:09 +0200931 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Peter Ujfalusi45656b42012-02-14 18:20:58 +0200932 fifo_use = omap_mcbsp_get_tx_delay(mcbsp);
Peter Ujfalusi75581d22010-03-03 15:08:09 +0200933 else
Peter Ujfalusi45656b42012-02-14 18:20:58 +0200934 fifo_use = omap_mcbsp_get_rx_delay(mcbsp);
Peter Ujfalusi75581d22010-03-03 15:08:09 +0200935
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 Nikula2e747962008-04-25 13:55:19 +0200946static int omap_mcbsp_dai_hw_params(struct snd_pcm_substream *substream,
Mark Browndee89c42008-11-18 22:11:38 +0000947 struct snd_pcm_hw_params *params,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000948 struct snd_soc_dai *cpu_dai)
Jarkko Nikula2e747962008-04-25 13:55:19 +0200949{
Peter Ujfalusi45656b42012-02-14 18:20:58 +0200950 struct omap_mcbsp *mcbsp = snd_soc_dai_get_drvdata(cpu_dai);
Peter Ujfalusi256d9c22012-02-14 15:23:15 +0200951 struct omap_mcbsp_reg_cfg *regs = &mcbsp->cfg_regs;
Lars-Peter Clausen09ae3aa2013-04-03 11:06:05 +0200952 struct snd_dmaengine_dai_dma_data *dma_data;
Peter Ujfalusi061fb362012-09-14 15:05:51 +0300953 int wlen, channels, wpf;
Peter Ujfalusicf80e152010-07-29 09:51:27 +0300954 int pkt_size = 0;
Graeme Gregory5f63ef92009-11-09 19:02:15 +0000955 unsigned int format, div, framesize, master;
Matt Ranostay9834ffd2017-01-31 13:21:43 -0800956 unsigned int buffer_size = mcbsp->pdata->buffer_size;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200957
Peter Ujfalusibcd6da72012-09-14 15:05:57 +0300958 dma_data = snd_soc_dai_get_dma_data(cpu_dai, substream);
Peter Ujfalusi778a17c2012-03-15 12:20:32 +0200959 channels = params_channels(params);
Kishon Vijay Abraham I2686e072011-02-24 15:16:56 +0530960
Sergey Lapind98508a2010-05-13 19:48:16 +0400961 switch (params_format(params)) {
962 case SNDRV_PCM_FORMAT_S16_LE:
Peter Ujfalusicf80e152010-07-29 09:51:27 +0300963 wlen = 16;
Sergey Lapind98508a2010-05-13 19:48:16 +0400964 break;
965 case SNDRV_PCM_FORMAT_S32_LE:
Peter Ujfalusicf80e152010-07-29 09:51:27 +0300966 wlen = 32;
Sergey Lapind98508a2010-05-13 19:48:16 +0400967 break;
968 default:
969 return -EINVAL;
970 }
Matt Ranostay9834ffd2017-01-31 13:21:43 -0800971 if (buffer_size) {
972 int latency;
973
Peter Ujfalusicb40b632012-02-13 16:26:54 +0200974 if (mcbsp->dma_op_mode == MCBSP_DMA_MODE_THRESHOLD) {
Peter Ujfalusicf80e152010-07-29 09:51:27 +0300975 int period_words, max_thrsh;
Peter Ujfalusidffb3602012-09-14 15:05:49 +0300976 int divider = 0;
Peter Ujfalusicf80e152010-07-29 09:51:27 +0300977
978 period_words = params_period_bytes(params) / (wlen / 8);
979 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Peter Ujfalusicb40b632012-02-13 16:26:54 +0200980 max_thrsh = mcbsp->max_tx_thres;
Peter Ujfalusicf80e152010-07-29 09:51:27 +0300981 else
Peter Ujfalusicb40b632012-02-13 16:26:54 +0200982 max_thrsh = mcbsp->max_rx_thres;
Peter Ujfalusicf80e152010-07-29 09:51:27 +0300983 /*
Peter Ujfalusidffb3602012-09-14 15:05:49 +0300984 * 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 Ujfalusicf80e152010-07-29 09:51:27 +0300989 */
Peter Ujfalusidffb3602012-09-14 15:05:49 +0300990 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 Ujfalusicf80e152010-07-29 09:51:27 +0300998
Peter Ujfalusidffb3602012-09-14 15:05:49 +0300999 pkt_size = period_words / divider;
Peter Ujfalusi778a17c2012-03-15 12:20:32 +02001000 } else if (channels > 1) {
1001 /* Use packet mode for non mono streams */
1002 pkt_size = channels;
Peter Ujfalusicf80e152010-07-29 09:51:27 +03001003 }
Matt Ranostay9834ffd2017-01-31 13:21:43 -08001004
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 Clausenabe99372013-03-25 16:58:16 +01001010 omap_mcbsp_set_threshold(substream, pkt_size);
Peter Ujfalusi15d01432010-07-29 09:51:25 +03001011 }
1012
Lars-Peter Clausen09ae3aa2013-04-03 11:06:05 +02001013 dma_data->maxburst = pkt_size;
Daniel Mackfd23b7d2010-03-19 14:52:55 +00001014
Peter Ujfalusi256d9c22012-02-14 15:23:15 +02001015 if (mcbsp->configured) {
Jarkko Nikula2e747962008-04-25 13:55:19 +02001016 /* McBSP already configured by another stream */
1017 return 0;
1018 }
1019
Jarkko Nikula4dd04172011-09-30 16:07:44 +03001020 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 Ujfalusi256d9c22012-02-14 15:23:15 +02001024 format = mcbsp->fmt & SND_SOC_DAIFMT_FORMAT_MASK;
Peter Ujfalusi778a17c2012-03-15 12:20:32 +02001025 wpf = channels;
Peter Ujfalusi299a1512010-03-19 12:27:31 +02001026 if (channels == 2 && (format == SND_SOC_DAIFMT_I2S ||
1027 format == SND_SOC_DAIFMT_LEFT_J)) {
Graeme Gregory5f63ef92009-11-09 19:02:15 +00001028 /* 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 Nikula2e747962008-04-25 13:55:19 +02001035 }
1036
Graeme Gregory5f63ef92009-11-09 19:02:15 +00001037 regs->rcr1 |= RFRLEN1(wpf - 1);
1038 regs->xcr1 |= XFRLEN1(wpf - 1);
1039
Jarkko Nikula2e747962008-04-25 13:55:19 +02001040 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 Nikula2e747962008-04-25 13:55:19 +02001047 break;
Sergey Lapind98508a2010-05-13 19:48:16 +04001048 case SNDRV_PCM_FORMAT_S32_LE:
1049 /* Set word lengths */
Sergey Lapind98508a2010-05-13 19:48:16 +04001050 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 Nikula2e747962008-04-25 13:55:19 +02001055 default:
1056 /* Unsupported PCM format */
1057 return -EINVAL;
1058 }
1059
Graeme Gregory5f63ef92009-11-09 19:02:15 +00001060 /* In McBSP master modes, FRAME (i.e. sample rate) is generated
1061 * by _counting_ BCLKs. Calculate frame size in BCLKs */
Peter Ujfalusi256d9c22012-02-14 15:23:15 +02001062 master = mcbsp->fmt & SND_SOC_DAIFMT_MASTER_MASK;
Graeme Gregory5f63ef92009-11-09 19:02:15 +00001063 if (master == SND_SOC_DAIFMT_CBS_CFS) {
Peter Ujfalusi256d9c22012-02-14 15:23:15 +02001064 div = mcbsp->clk_div ? mcbsp->clk_div : 1;
1065 framesize = (mcbsp->in_freq / div) / params_rate(params);
Graeme Gregory5f63ef92009-11-09 19:02:15 +00001066
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 Nikulaba9d0fd2008-10-20 15:29:59 +03001075 /* Set FS period and length in terms of bit clock periods */
Jarkko Nikula4dd04172011-09-30 16:07:44 +03001076 regs->srgr2 &= ~FPER(0xfff);
1077 regs->srgr1 &= ~FWID(0xff);
Peter Ujfalusic29b2062009-04-15 15:38:55 +03001078 switch (format) {
Jarkko Nikulaba9d0fd2008-10-20 15:29:59 +03001079 case SND_SOC_DAIFMT_I2S:
Peter Ujfalusi299a1512010-03-19 12:27:31 +02001080 case SND_SOC_DAIFMT_LEFT_J:
Graeme Gregory5f63ef92009-11-09 19:02:15 +00001081 regs->srgr2 |= FPER(framesize - 1);
1082 regs->srgr1 |= FWID((framesize >> 1) - 1);
Jarkko Nikulaba9d0fd2008-10-20 15:29:59 +03001083 break;
Peter Ujfalusi3ba191c2009-04-15 15:38:56 +03001084 case SND_SOC_DAIFMT_DSP_A:
Jarkko Nikulabd258672008-12-22 10:21:36 +02001085 case SND_SOC_DAIFMT_DSP_B:
Graeme Gregory5f63ef92009-11-09 19:02:15 +00001086 regs->srgr2 |= FPER(framesize - 1);
Jarkko Nikula36ce8582009-04-15 13:48:16 +03001087 regs->srgr1 |= FWID(0);
Jarkko Nikulaba9d0fd2008-10-20 15:29:59 +03001088 break;
1089 }
1090
Peter Ujfalusi256d9c22012-02-14 15:23:15 +02001091 omap_mcbsp_config(mcbsp, &mcbsp->cfg_regs);
1092 mcbsp->wlen = wlen;
1093 mcbsp->configured = 1;
Jarkko Nikula2e747962008-04-25 13:55:19 +02001094
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 Girdwood8687eb82008-07-07 16:08:07 +01001102static int omap_mcbsp_dai_set_dai_fmt(struct snd_soc_dai *cpu_dai,
Jarkko Nikula2e747962008-04-25 13:55:19 +02001103 unsigned int fmt)
1104{
Peter Ujfalusi45656b42012-02-14 18:20:58 +02001105 struct omap_mcbsp *mcbsp = snd_soc_dai_get_drvdata(cpu_dai);
Peter Ujfalusi256d9c22012-02-14 15:23:15 +02001106 struct omap_mcbsp_reg_cfg *regs = &mcbsp->cfg_regs;
Jarkko Nikula91a18ae2011-09-30 10:55:32 +03001107 bool inv_fs = false;
Jarkko Nikula2e747962008-04-25 13:55:19 +02001108
Peter Ujfalusi256d9c22012-02-14 15:23:15 +02001109 if (mcbsp->configured)
Jarkko Nikula2e747962008-04-25 13:55:19 +02001110 return 0;
1111
Peter Ujfalusi256d9c22012-02-14 15:23:15 +02001112 mcbsp->fmt = fmt;
Jarkko Nikula2e747962008-04-25 13:55:19 +02001113 memset(regs, 0, sizeof(*regs));
1114 /* Generic McBSP register settings */
1115 regs->spcr2 |= XINTM(3) | FREE;
1116 regs->spcr1 |= RINTM(3);
Peter Ujfalusidc26df52012-08-16 16:41:06 +03001117 /* RFIG and XFIG are not defined in 2430 and on OMAP3+ */
1118 if (!mcbsp->pdata->has_ccr) {
Eero Nurkkalac721bbd2009-08-20 16:18:23 +03001119 regs->rcr2 |= RFIG;
1120 regs->xcr2 |= XFIG;
1121 }
Peter Ujfalusidc26df52012-08-16 16:41:06 +03001122
1123 /* Configure XCCR/RCCR only for revisions which have ccr registers */
1124 if (mcbsp->pdata->has_ccr) {
Jarkko Nikula32080af2009-08-23 12:24:26 +03001125 regs->xccr = DXENDLY(1) | XDMAEN | XDISABLE;
1126 regs->rccr = RFULL_CYCLE | RDMAEN | RDISABLE;
Misael Lopez Cruzef390c02009-01-29 13:29:46 +02001127 }
Jarkko Nikula2e747962008-04-25 13:55:19 +02001128
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 Ujfalusi299a1512010-03-19 12:27:31 +02001135 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 Nikula91a18ae2011-09-30 10:55:32 +03001141 inv_fs = true;
Peter Ujfalusi299a1512010-03-19 12:27:31 +02001142 break;
Peter Ujfalusi3ba191c2009-04-15 15:38:56 +03001143 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 Nikula91a18ae2011-09-30 10:55:32 +03001148 inv_fs = true;
Peter Ujfalusi3ba191c2009-04-15 15:38:56 +03001149 break;
Jarkko Nikulabd258672008-12-22 10:21:36 +02001150 case SND_SOC_DAIFMT_DSP_B:
Arun KS3336c5b2008-10-02 15:07:06 +05301151 /* 0-bit data delay */
1152 regs->rcr2 |= RDATDLY(0);
1153 regs->xcr2 |= XDATDLY(0);
Jarkko Nikula36ce8582009-04-15 13:48:16 +03001154 /* Invert FS polarity configuration */
Jarkko Nikula91a18ae2011-09-30 10:55:32 +03001155 inv_fs = true;
Arun KS3336c5b2008-10-02 15:07:06 +05301156 break;
Jarkko Nikula2e747962008-04-25 13:55:19 +02001157 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 Trimarchi6e20b0d2013-07-21 18:24:01 +02001170 case SND_SOC_DAIFMT_CBM_CFS:
1171 /* McBSP slave. FS clock as output */
1172 regs->srgr2 |= FSGM;
Peter Ujfalusi20602e32015-01-16 11:20:25 +02001173 regs->pcr0 |= FSXM | FSRM;
Michael Trimarchi6e20b0d2013-07-21 18:24:01 +02001174 break;
Jarkko Nikula2e747962008-04-25 13:55:19 +02001175 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 Nikula91a18ae2011-09-30 10:55:32 +03001184 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
Jarkko Nikula2e747962008-04-25 13:55:19 +02001185 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 Nikula91a18ae2011-09-30 10:55:32 +03001205 if (inv_fs == true)
1206 regs->pcr0 ^= FSXP | FSRP;
Jarkko Nikula2e747962008-04-25 13:55:19 +02001207
1208 return 0;
1209}
1210
Liam Girdwood8687eb82008-07-07 16:08:07 +01001211static int omap_mcbsp_dai_set_clkdiv(struct snd_soc_dai *cpu_dai,
Jarkko Nikula2e747962008-04-25 13:55:19 +02001212 int div_id, int div)
1213{
Peter Ujfalusi45656b42012-02-14 18:20:58 +02001214 struct omap_mcbsp *mcbsp = snd_soc_dai_get_drvdata(cpu_dai);
Peter Ujfalusi256d9c22012-02-14 15:23:15 +02001215 struct omap_mcbsp_reg_cfg *regs = &mcbsp->cfg_regs;
Jarkko Nikula2e747962008-04-25 13:55:19 +02001216
1217 if (div_id != OMAP_MCBSP_CLKGDV)
1218 return -ENODEV;
1219
Peter Ujfalusi256d9c22012-02-14 15:23:15 +02001220 mcbsp->clk_div = div;
Jarkko Nikula4dd04172011-09-30 16:07:44 +03001221 regs->srgr1 &= ~CLKGDV(0xff);
Jarkko Nikula2e747962008-04-25 13:55:19 +02001222 regs->srgr1 |= CLKGDV(div - 1);
1223
1224 return 0;
1225}
1226
Liam Girdwood8687eb82008-07-07 16:08:07 +01001227static int omap_mcbsp_dai_set_dai_sysclk(struct snd_soc_dai *cpu_dai,
Jarkko Nikula2e747962008-04-25 13:55:19 +02001228 int clk_id, unsigned int freq,
1229 int dir)
1230{
Peter Ujfalusi45656b42012-02-14 18:20:58 +02001231 struct omap_mcbsp *mcbsp = snd_soc_dai_get_drvdata(cpu_dai);
Peter Ujfalusi256d9c22012-02-14 15:23:15 +02001232 struct omap_mcbsp_reg_cfg *regs = &mcbsp->cfg_regs;
Jarkko Nikula2e747962008-04-25 13:55:19 +02001233 int err = 0;
1234
Peter Ujfalusi256d9c22012-02-14 15:23:15 +02001235 if (mcbsp->active) {
1236 if (freq == mcbsp->in_freq)
Jarkko Nikula34c86982011-09-23 11:19:13 +03001237 return 0;
1238 else
1239 return -EBUSY;
Peter Ujfalusi141947e2011-09-26 10:56:42 +03001240 }
Jarkko Nikula34c86982011-09-23 11:19:13 +03001241
Peter Ujfalusi8fef6262012-08-16 16:41:04 +03001242 mcbsp->in_freq = freq;
1243 regs->srgr2 &= ~CLKSM;
1244 regs->pcr0 &= ~SCLKME;
Graeme Gregory5f63ef92009-11-09 19:02:15 +00001245
Jarkko Nikula2e747962008-04-25 13:55:19 +02001246 switch (clk_id) {
1247 case OMAP_MCBSP_SYSCLK_CLK:
1248 regs->srgr2 |= CLKSM;
1249 break;
1250 case OMAP_MCBSP_SYSCLK_CLKS_FCLK:
Tony Lindgrene6507942012-11-21 09:42:25 -08001251 if (mcbsp_omap1()) {
Paul Walmsleyd13586572010-10-08 11:40:19 -06001252 err = -EINVAL;
1253 break;
1254 }
Peter Ujfalusi45656b42012-02-14 18:20:58 +02001255 err = omap2_mcbsp_set_clks_src(mcbsp,
Paul Walmsleyd13586572010-10-08 11:40:19 -06001256 MCBSP_CLKS_PRCM_SRC);
1257 break;
Jarkko Nikula2e747962008-04-25 13:55:19 +02001258 case OMAP_MCBSP_SYSCLK_CLKS_EXT:
Tony Lindgrene6507942012-11-21 09:42:25 -08001259 if (mcbsp_omap1()) {
Paul Walmsleyd13586572010-10-08 11:40:19 -06001260 err = 0;
1261 break;
1262 }
Peter Ujfalusi45656b42012-02-14 18:20:58 +02001263 err = omap2_mcbsp_set_clks_src(mcbsp,
Paul Walmsleyd13586572010-10-08 11:40:19 -06001264 MCBSP_CLKS_PAD_SRC);
Jarkko Nikula2e747962008-04-25 13:55:19 +02001265 break;
1266
1267 case OMAP_MCBSP_SYSCLK_CLKX_EXT:
1268 regs->srgr2 |= CLKSM;
Thomas Niederprüm8af4baa2015-02-21 18:11:29 +01001269 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 Nikula2e747962008-04-25 13:55:19 +02001278 case OMAP_MCBSP_SYSCLK_CLKR_EXT:
1279 regs->pcr0 |= SCLKME;
Thomas Niederprüm8af4baa2015-02-21 18:11:29 +01001280 /* Disable ouput on CLKR pin in master mode */
1281 regs->pcr0 &= ~CLKRM;
Jarkko Nikula2e747962008-04-25 13:55:19 +02001282 break;
1283 default:
1284 err = -ENODEV;
1285 }
1286
1287 return err;
1288}
1289
Lars-Peter Clausen85e76522011-11-23 11:40:40 +01001290static const struct snd_soc_dai_ops mcbsp_dai_ops = {
Eric Miao6335d052009-03-03 09:41:00 +08001291 .startup = omap_mcbsp_dai_startup,
1292 .shutdown = omap_mcbsp_dai_shutdown,
Matt Ranostay9834ffd2017-01-31 13:21:43 -08001293 .prepare = omap_mcbsp_dai_prepare,
Eric Miao6335d052009-03-03 09:41:00 +08001294 .trigger = omap_mcbsp_dai_trigger,
Peter Ujfalusi75581d22010-03-03 15:08:09 +02001295 .delay = omap_mcbsp_dai_delay,
Eric Miao6335d052009-03-03 09:41:00 +08001296 .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 Ujfalusi2ee65952012-02-14 14:52:42 +02001302static 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 Ujfalusi3fe856b2014-04-16 15:46:15 +03001308 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 Ujfalusi2ee65952012-02-14 14:52:42 +02001312 return 0;
1313}
1314
1315static 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 Opdenacker6179b772011-10-10 07:07:08 +02001324static struct snd_soc_dai_driver omap_mcbsp_dai = {
Peter Ujfalusi2ee65952012-02-14 14:52:42 +02001325 .probe = omap_mcbsp_probe,
1326 .remove = omap_mcbsp_remove,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001327 .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 Nikula2e747962008-04-25 13:55:19 +02001340};
Jarkko Nikula8def4642008-10-09 15:57:22 +03001341
Kuninori Morimoto43cd8142013-03-21 03:33:25 -07001342static const struct snd_soc_component_driver omap_mcbsp_component = {
1343 .name = "omap-mcbsp",
1344};
1345
Peter Ujfalusi11dd5862012-08-16 16:41:08 +03001346static struct omap_mcbsp_platform_data omap2420_pdata = {
1347 .reg_step = 4,
1348 .reg_size = 2,
1349};
1350
1351static struct omap_mcbsp_platform_data omap2430_pdata = {
1352 .reg_step = 4,
1353 .reg_size = 4,
1354 .has_ccr = true,
1355};
1356
1357static 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
1364static 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
1371static 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};
1390MODULE_DEVICE_TABLE(of, omap_mcbsp_of_match);
1391
Bill Pemberton7ff60002012-12-07 09:26:29 -05001392static int asoc_mcbsp_probe(struct platform_device *pdev)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001393{
Peter Ujfalusi2ee65952012-02-14 14:52:42 +02001394 struct omap_mcbsp_platform_data *pdata = dev_get_platdata(&pdev->dev);
1395 struct omap_mcbsp *mcbsp;
Peter Ujfalusi11dd5862012-08-16 16:41:08 +03001396 const struct of_device_id *match;
Peter Ujfalusi45656b42012-02-14 18:20:58 +02001397 int ret;
1398
Peter Ujfalusi11dd5862012-08-16 16:41:08 +03001399 match = of_match_device(omap_mcbsp_of_match, &pdev->dev);
1400 if (match) {
1401 struct device_node *node = pdev->dev.of_node;
Peter Ujfalusibbfa26c2016-05-30 11:23:49 +03001402 struct omap_mcbsp_platform_data *pdata_quirk = pdata;
Peter Ujfalusi11dd5862012-08-16 16:41:08 +03001403 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 Ujfalusibbfa26c2016-05-30 11:23:49 +03001414 if (pdata_quirk)
1415 pdata->force_ick_on = pdata_quirk->force_ick_on;
Peter Ujfalusi11dd5862012-08-16 16:41:08 +03001416 } else if (!pdata) {
Peter Ujfalusi2ee65952012-02-14 14:52:42 +02001417 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 Ujfalusi64241422014-04-16 15:46:16 +03001430 if (ret)
1431 return ret;
Peter Ujfalusi45656b42012-02-14 18:20:58 +02001432
Peter Ujfalusi61f18dc2018-10-25 16:48:24 +03001433 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 Badarkhe36765c92014-07-08 21:55:23 +05301438 ret = devm_snd_soc_register_component(&pdev->dev,
1439 &omap_mcbsp_component,
1440 &omap_mcbsp_dai, 1);
Peter Ujfalusi64241422014-04-16 15:46:16 +03001441 if (ret)
1442 return ret;
1443
Peter Ujfalusi0198d7b2018-05-07 11:49:59 +03001444 return sdma_pcm_platform_register(&pdev->dev, NULL, NULL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001445}
1446
Bill Pemberton7ff60002012-12-07 09:26:29 -05001447static int asoc_mcbsp_remove(struct platform_device *pdev)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001448{
Peter Ujfalusi2ee65952012-02-14 14:52:42 +02001449 struct omap_mcbsp *mcbsp = platform_get_drvdata(pdev);
1450
Peter Ujfalusi2ee65952012-02-14 14:52:42 +02001451 if (mcbsp->pdata->ops && mcbsp->pdata->ops->free)
1452 mcbsp->pdata->ops->free(mcbsp->id);
1453
Matt Ranostay9834ffd2017-01-31 13:21:43 -08001454 if (pm_qos_request_active(&mcbsp->pm_qos_req))
1455 pm_qos_remove_request(&mcbsp->pm_qos_req);
1456
Peter Ujfalusi9c34d022018-11-08 09:29:58 +02001457 if (mcbsp->pdata->buffer_size)
1458 sysfs_remove_group(&mcbsp->dev->kobj, &additional_attr_group);
1459
1460 omap_mcbsp_st_cleanup(pdev);
Peter Ujfalusi2ee65952012-02-14 14:52:42 +02001461
1462 clk_put(mcbsp->fclk);
1463
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001464 return 0;
1465}
1466
1467static struct platform_driver asoc_mcbsp_driver = {
1468 .driver = {
Peter Ujfalusi45656b42012-02-14 18:20:58 +02001469 .name = "omap-mcbsp",
Peter Ujfalusi11dd5862012-08-16 16:41:08 +03001470 .of_match_table = omap_mcbsp_of_match,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001471 },
1472
1473 .probe = asoc_mcbsp_probe,
Bill Pemberton7ff60002012-12-07 09:26:29 -05001474 .remove = asoc_mcbsp_remove,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001475};
1476
Axel Linbeda5bf52011-11-25 10:12:16 +08001477module_platform_driver(asoc_mcbsp_driver);
Mark Brown3f4b7832008-12-03 19:26:35 +00001478
Jarkko Nikula7ec41ee2011-08-11 15:44:57 +03001479MODULE_AUTHOR("Jarkko Nikula <jarkko.nikula@bitmer.com>");
Jarkko Nikula2e747962008-04-25 13:55:19 +02001480MODULE_DESCRIPTION("OMAP I2S SoC Interface");
1481MODULE_LICENSE("GPL");
Guillaume Gardet5e70b7fc2012-07-12 15:08:16 +02001482MODULE_ALIAS("platform:omap-mcbsp");