blob: 1ab3c7df4f8b4e8880b48f306be53187e4b18709 [file] [log] [blame]
Thomas Gleixner2b27bdc2019-05-29 16:57:50 -07001// SPDX-License-Identifier: GPL-2.0-only
Jarkko Nikula2e747962008-04-25 13:55:19 +02002/*
3 * omap-mcbsp.c -- OMAP ALSA SoC DAI driver using McBSP port
4 *
5 * Copyright (C) 2008 Nokia Corporation
6 *
Jarkko Nikula7ec41ee2011-08-11 15:44:57 +03007 * Contact: Jarkko Nikula <jarkko.nikula@bitmer.com>
Peter Ujfalusi56a87422011-05-03 18:14:06 +03008 * Peter Ujfalusi <peter.ujfalusi@ti.com>
Jarkko Nikula2e747962008-04-25 13:55:19 +02009 */
10
11#include <linux/init.h>
12#include <linux/module.h>
13#include <linux/device.h>
Peter Ujfalusi2ee65952012-02-14 14:52:42 +020014#include <linux/pm_runtime.h>
Peter Ujfalusi11dd5862012-08-16 16:41:08 +030015#include <linux/of.h>
16#include <linux/of_device.h>
Jarkko Nikula2e747962008-04-25 13:55:19 +020017#include <sound/core.h>
18#include <sound/pcm.h>
19#include <sound/pcm_params.h>
20#include <sound/initval.h>
21#include <sound/soc.h>
Lars-Peter Clausen09ae3aa2013-04-03 11:06:05 +020022#include <sound/dmaengine_pcm.h>
Jarkko Nikula2e747962008-04-25 13:55:19 +020023
Peter Ujfalusi9c34d022018-11-08 09:29:58 +020024#include "omap-mcbsp-priv.h"
Jarkko Nikula2e747962008-04-25 13:55:19 +020025#include "omap-mcbsp.h"
Peter Ujfalusi0198d7b2018-05-07 11:49:59 +030026#include "sdma-pcm.h"
Jarkko Nikula2e747962008-04-25 13:55:19 +020027
Jarkko Nikula0b604852008-11-12 17:05:51 +020028#define OMAP_MCBSP_RATES (SNDRV_PCM_RATE_8000_96000)
Jarkko Nikula2e747962008-04-25 13:55:19 +020029
Peter Ujfalusi219f4312012-02-03 13:11:47 +020030enum {
31 OMAP_MCBSP_WORD_8 = 0,
32 OMAP_MCBSP_WORD_12,
33 OMAP_MCBSP_WORD_16,
34 OMAP_MCBSP_WORD_20,
35 OMAP_MCBSP_WORD_24,
36 OMAP_MCBSP_WORD_32,
37};
38
Peter Ujfalusi9c34d022018-11-08 09:29:58 +020039static void omap_mcbsp_dump_reg(struct omap_mcbsp *mcbsp)
40{
41 dev_dbg(mcbsp->dev, "**** McBSP%d regs ****\n", mcbsp->id);
42 dev_dbg(mcbsp->dev, "DRR2: 0x%04x\n", MCBSP_READ(mcbsp, DRR2));
43 dev_dbg(mcbsp->dev, "DRR1: 0x%04x\n", MCBSP_READ(mcbsp, DRR1));
44 dev_dbg(mcbsp->dev, "DXR2: 0x%04x\n", MCBSP_READ(mcbsp, DXR2));
45 dev_dbg(mcbsp->dev, "DXR1: 0x%04x\n", MCBSP_READ(mcbsp, DXR1));
46 dev_dbg(mcbsp->dev, "SPCR2: 0x%04x\n", MCBSP_READ(mcbsp, SPCR2));
47 dev_dbg(mcbsp->dev, "SPCR1: 0x%04x\n", MCBSP_READ(mcbsp, SPCR1));
48 dev_dbg(mcbsp->dev, "RCR2: 0x%04x\n", MCBSP_READ(mcbsp, RCR2));
49 dev_dbg(mcbsp->dev, "RCR1: 0x%04x\n", MCBSP_READ(mcbsp, RCR1));
50 dev_dbg(mcbsp->dev, "XCR2: 0x%04x\n", MCBSP_READ(mcbsp, XCR2));
51 dev_dbg(mcbsp->dev, "XCR1: 0x%04x\n", MCBSP_READ(mcbsp, XCR1));
52 dev_dbg(mcbsp->dev, "SRGR2: 0x%04x\n", MCBSP_READ(mcbsp, SRGR2));
53 dev_dbg(mcbsp->dev, "SRGR1: 0x%04x\n", MCBSP_READ(mcbsp, SRGR1));
54 dev_dbg(mcbsp->dev, "PCR0: 0x%04x\n", MCBSP_READ(mcbsp, PCR0));
55 dev_dbg(mcbsp->dev, "***********************\n");
56}
57
58static int omap2_mcbsp_set_clks_src(struct omap_mcbsp *mcbsp, u8 fck_src_id)
59{
60 struct clk *fck_src;
61 const char *src;
62 int r;
63
64 if (fck_src_id == MCBSP_CLKS_PAD_SRC)
65 src = "pad_fck";
66 else if (fck_src_id == MCBSP_CLKS_PRCM_SRC)
67 src = "prcm_fck";
68 else
69 return -EINVAL;
70
71 fck_src = clk_get(mcbsp->dev, src);
72 if (IS_ERR(fck_src)) {
73 dev_err(mcbsp->dev, "CLKS: could not clk_get() %s\n", src);
74 return -EINVAL;
75 }
76
77 pm_runtime_put_sync(mcbsp->dev);
78
79 r = clk_set_parent(mcbsp->fclk, fck_src);
80 if (r) {
81 dev_err(mcbsp->dev, "CLKS: could not clk_set_parent() to %s\n",
82 src);
83 clk_put(fck_src);
84 return r;
85 }
86
87 pm_runtime_get_sync(mcbsp->dev);
88
89 clk_put(fck_src);
90
91 return 0;
92}
93
94static irqreturn_t omap_mcbsp_irq_handler(int irq, void *data)
95{
96 struct omap_mcbsp *mcbsp = data;
97 u16 irqst;
98
99 irqst = MCBSP_READ(mcbsp, IRQST);
100 dev_dbg(mcbsp->dev, "IRQ callback : 0x%x\n", irqst);
101
102 if (irqst & RSYNCERREN)
103 dev_err(mcbsp->dev, "RX Frame Sync Error!\n");
104 if (irqst & RFSREN)
105 dev_dbg(mcbsp->dev, "RX Frame Sync\n");
106 if (irqst & REOFEN)
107 dev_dbg(mcbsp->dev, "RX End Of Frame\n");
108 if (irqst & RRDYEN)
109 dev_dbg(mcbsp->dev, "RX Buffer Threshold Reached\n");
110 if (irqst & RUNDFLEN)
111 dev_err(mcbsp->dev, "RX Buffer Underflow!\n");
112 if (irqst & ROVFLEN)
113 dev_err(mcbsp->dev, "RX Buffer Overflow!\n");
114
115 if (irqst & XSYNCERREN)
116 dev_err(mcbsp->dev, "TX Frame Sync Error!\n");
117 if (irqst & XFSXEN)
118 dev_dbg(mcbsp->dev, "TX Frame Sync\n");
119 if (irqst & XEOFEN)
120 dev_dbg(mcbsp->dev, "TX End Of Frame\n");
121 if (irqst & XRDYEN)
122 dev_dbg(mcbsp->dev, "TX Buffer threshold Reached\n");
123 if (irqst & XUNDFLEN)
124 dev_err(mcbsp->dev, "TX Buffer Underflow!\n");
125 if (irqst & XOVFLEN)
126 dev_err(mcbsp->dev, "TX Buffer Overflow!\n");
127 if (irqst & XEMPTYEOFEN)
128 dev_dbg(mcbsp->dev, "TX Buffer empty at end of frame\n");
129
130 MCBSP_WRITE(mcbsp, IRQST, irqst);
131
132 return IRQ_HANDLED;
133}
134
135static irqreturn_t omap_mcbsp_tx_irq_handler(int irq, void *data)
136{
137 struct omap_mcbsp *mcbsp = data;
138 u16 irqst_spcr2;
139
140 irqst_spcr2 = MCBSP_READ(mcbsp, SPCR2);
141 dev_dbg(mcbsp->dev, "TX IRQ callback : 0x%x\n", irqst_spcr2);
142
143 if (irqst_spcr2 & XSYNC_ERR) {
144 dev_err(mcbsp->dev, "TX Frame Sync Error! : 0x%x\n",
145 irqst_spcr2);
146 /* Writing zero to XSYNC_ERR clears the IRQ */
147 MCBSP_WRITE(mcbsp, SPCR2, MCBSP_READ_CACHE(mcbsp, SPCR2));
148 }
149
150 return IRQ_HANDLED;
151}
152
153static irqreturn_t omap_mcbsp_rx_irq_handler(int irq, void *data)
154{
155 struct omap_mcbsp *mcbsp = data;
156 u16 irqst_spcr1;
157
158 irqst_spcr1 = MCBSP_READ(mcbsp, SPCR1);
159 dev_dbg(mcbsp->dev, "RX IRQ callback : 0x%x\n", irqst_spcr1);
160
161 if (irqst_spcr1 & RSYNC_ERR) {
162 dev_err(mcbsp->dev, "RX Frame Sync Error! : 0x%x\n",
163 irqst_spcr1);
164 /* Writing zero to RSYNC_ERR clears the IRQ */
165 MCBSP_WRITE(mcbsp, SPCR1, MCBSP_READ_CACHE(mcbsp, SPCR1));
166 }
167
168 return IRQ_HANDLED;
169}
170
171/*
172 * omap_mcbsp_config simply write a config to the
173 * appropriate McBSP.
174 * You either call this function or set the McBSP registers
175 * by yourself before calling omap_mcbsp_start().
176 */
177static void omap_mcbsp_config(struct omap_mcbsp *mcbsp,
178 const struct omap_mcbsp_reg_cfg *config)
179{
180 dev_dbg(mcbsp->dev, "Configuring McBSP%d phys_base: 0x%08lx\n",
181 mcbsp->id, mcbsp->phys_base);
182
183 /* We write the given config */
184 MCBSP_WRITE(mcbsp, SPCR2, config->spcr2);
185 MCBSP_WRITE(mcbsp, SPCR1, config->spcr1);
186 MCBSP_WRITE(mcbsp, RCR2, config->rcr2);
187 MCBSP_WRITE(mcbsp, RCR1, config->rcr1);
188 MCBSP_WRITE(mcbsp, XCR2, config->xcr2);
189 MCBSP_WRITE(mcbsp, XCR1, config->xcr1);
190 MCBSP_WRITE(mcbsp, SRGR2, config->srgr2);
191 MCBSP_WRITE(mcbsp, SRGR1, config->srgr1);
192 MCBSP_WRITE(mcbsp, MCR2, config->mcr2);
193 MCBSP_WRITE(mcbsp, MCR1, config->mcr1);
194 MCBSP_WRITE(mcbsp, PCR0, config->pcr0);
195 if (mcbsp->pdata->has_ccr) {
196 MCBSP_WRITE(mcbsp, XCCR, config->xccr);
197 MCBSP_WRITE(mcbsp, RCCR, config->rccr);
198 }
199 /* Enable wakeup behavior */
200 if (mcbsp->pdata->has_wakeup)
201 MCBSP_WRITE(mcbsp, WAKEUPEN, XRDYEN | RRDYEN);
202
203 /* Enable TX/RX sync error interrupts by default */
204 if (mcbsp->irq)
205 MCBSP_WRITE(mcbsp, IRQEN, RSYNCERREN | XSYNCERREN |
206 RUNDFLEN | ROVFLEN | XUNDFLEN | XOVFLEN);
207}
208
209/**
210 * omap_mcbsp_dma_reg_params - returns the address of mcbsp data register
211 * @mcbsp: omap_mcbsp struct for the McBSP instance
212 * @stream: Stream direction (playback/capture)
213 *
214 * Returns the address of mcbsp data transmit register or data receive register
215 * to be used by DMA for transferring/receiving data
216 */
217static int omap_mcbsp_dma_reg_params(struct omap_mcbsp *mcbsp,
218 unsigned int stream)
219{
220 int data_reg;
221
222 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
223 if (mcbsp->pdata->reg_size == 2)
224 data_reg = OMAP_MCBSP_REG_DXR1;
225 else
226 data_reg = OMAP_MCBSP_REG_DXR;
227 } else {
228 if (mcbsp->pdata->reg_size == 2)
229 data_reg = OMAP_MCBSP_REG_DRR1;
230 else
231 data_reg = OMAP_MCBSP_REG_DRR;
232 }
233
234 return mcbsp->phys_dma_base + data_reg * mcbsp->pdata->reg_step;
235}
236
237/*
238 * omap_mcbsp_set_rx_threshold configures the transmit threshold in words.
239 * The threshold parameter is 1 based, and it is converted (threshold - 1)
240 * for the THRSH2 register.
241 */
242static void omap_mcbsp_set_tx_threshold(struct omap_mcbsp *mcbsp, u16 threshold)
243{
244 if (threshold && threshold <= mcbsp->max_tx_thres)
245 MCBSP_WRITE(mcbsp, THRSH2, threshold - 1);
246}
247
248/*
249 * omap_mcbsp_set_rx_threshold configures the receive threshold in words.
250 * The threshold parameter is 1 based, and it is converted (threshold - 1)
251 * for the THRSH1 register.
252 */
253static void omap_mcbsp_set_rx_threshold(struct omap_mcbsp *mcbsp, u16 threshold)
254{
255 if (threshold && threshold <= mcbsp->max_rx_thres)
256 MCBSP_WRITE(mcbsp, THRSH1, threshold - 1);
257}
258
259/*
260 * omap_mcbsp_get_tx_delay returns the number of used slots in the McBSP FIFO
261 */
262static u16 omap_mcbsp_get_tx_delay(struct omap_mcbsp *mcbsp)
263{
264 u16 buffstat;
265
266 /* Returns the number of free locations in the buffer */
267 buffstat = MCBSP_READ(mcbsp, XBUFFSTAT);
268
269 /* Number of slots are different in McBSP ports */
270 return mcbsp->pdata->buffer_size - buffstat;
271}
272
273/*
274 * omap_mcbsp_get_rx_delay returns the number of free slots in the McBSP FIFO
275 * to reach the threshold value (when the DMA will be triggered to read it)
276 */
277static u16 omap_mcbsp_get_rx_delay(struct omap_mcbsp *mcbsp)
278{
279 u16 buffstat, threshold;
280
281 /* Returns the number of used locations in the buffer */
282 buffstat = MCBSP_READ(mcbsp, RBUFFSTAT);
283 /* RX threshold */
284 threshold = MCBSP_READ(mcbsp, THRSH1);
285
286 /* Return the number of location till we reach the threshold limit */
287 if (threshold <= buffstat)
288 return 0;
289 else
290 return threshold - buffstat;
291}
292
293static int omap_mcbsp_request(struct omap_mcbsp *mcbsp)
294{
295 void *reg_cache;
296 int err;
297
298 reg_cache = kzalloc(mcbsp->reg_cache_size, GFP_KERNEL);
299 if (!reg_cache)
300 return -ENOMEM;
301
302 spin_lock(&mcbsp->lock);
303 if (!mcbsp->free) {
304 dev_err(mcbsp->dev, "McBSP%d is currently in use\n", mcbsp->id);
305 err = -EBUSY;
306 goto err_kfree;
307 }
308
309 mcbsp->free = false;
310 mcbsp->reg_cache = reg_cache;
311 spin_unlock(&mcbsp->lock);
312
Peter Ujfalusi465d85b2018-11-08 09:29:59 +0200313 if(mcbsp->pdata->ops && mcbsp->pdata->ops->request)
Peter Ujfalusi9c34d022018-11-08 09:29:58 +0200314 mcbsp->pdata->ops->request(mcbsp->id - 1);
315
316 /*
317 * Make sure that transmitter, receiver and sample-rate generator are
318 * not running before activating IRQs.
319 */
320 MCBSP_WRITE(mcbsp, SPCR1, 0);
321 MCBSP_WRITE(mcbsp, SPCR2, 0);
322
323 if (mcbsp->irq) {
324 err = request_irq(mcbsp->irq, omap_mcbsp_irq_handler, 0,
325 "McBSP", (void *)mcbsp);
326 if (err != 0) {
327 dev_err(mcbsp->dev, "Unable to request IRQ\n");
328 goto err_clk_disable;
329 }
330 } else {
331 err = request_irq(mcbsp->tx_irq, omap_mcbsp_tx_irq_handler, 0,
332 "McBSP TX", (void *)mcbsp);
333 if (err != 0) {
334 dev_err(mcbsp->dev, "Unable to request TX IRQ\n");
335 goto err_clk_disable;
336 }
337
338 err = request_irq(mcbsp->rx_irq, omap_mcbsp_rx_irq_handler, 0,
339 "McBSP RX", (void *)mcbsp);
340 if (err != 0) {
341 dev_err(mcbsp->dev, "Unable to request RX IRQ\n");
342 goto err_free_irq;
343 }
344 }
345
346 return 0;
347err_free_irq:
348 free_irq(mcbsp->tx_irq, (void *)mcbsp);
349err_clk_disable:
Peter Ujfalusi465d85b2018-11-08 09:29:59 +0200350 if(mcbsp->pdata->ops && mcbsp->pdata->ops->free)
Peter Ujfalusi9c34d022018-11-08 09:29:58 +0200351 mcbsp->pdata->ops->free(mcbsp->id - 1);
352
353 /* Disable wakeup behavior */
354 if (mcbsp->pdata->has_wakeup)
355 MCBSP_WRITE(mcbsp, WAKEUPEN, 0);
356
357 spin_lock(&mcbsp->lock);
358 mcbsp->free = true;
359 mcbsp->reg_cache = NULL;
360err_kfree:
361 spin_unlock(&mcbsp->lock);
362 kfree(reg_cache);
363
364 return err;
365}
366
367static void omap_mcbsp_free(struct omap_mcbsp *mcbsp)
368{
369 void *reg_cache;
370
Peter Ujfalusi465d85b2018-11-08 09:29:59 +0200371 if(mcbsp->pdata->ops && mcbsp->pdata->ops->free)
Peter Ujfalusi9c34d022018-11-08 09:29:58 +0200372 mcbsp->pdata->ops->free(mcbsp->id - 1);
373
374 /* Disable wakeup behavior */
375 if (mcbsp->pdata->has_wakeup)
376 MCBSP_WRITE(mcbsp, WAKEUPEN, 0);
377
378 /* Disable interrupt requests */
379 if (mcbsp->irq)
380 MCBSP_WRITE(mcbsp, IRQEN, 0);
381
382 if (mcbsp->irq) {
383 free_irq(mcbsp->irq, (void *)mcbsp);
384 } else {
385 free_irq(mcbsp->rx_irq, (void *)mcbsp);
386 free_irq(mcbsp->tx_irq, (void *)mcbsp);
387 }
388
389 reg_cache = mcbsp->reg_cache;
390
391 /*
392 * Select CLKS source from internal source unconditionally before
393 * marking the McBSP port as free.
394 * If the external clock source via MCBSP_CLKS pin has been selected the
395 * system will refuse to enter idle if the CLKS pin source is not reset
396 * back to internal source.
397 */
398 if (!mcbsp_omap1())
399 omap2_mcbsp_set_clks_src(mcbsp, MCBSP_CLKS_PRCM_SRC);
400
401 spin_lock(&mcbsp->lock);
402 if (mcbsp->free)
403 dev_err(mcbsp->dev, "McBSP%d was not reserved\n", mcbsp->id);
404 else
405 mcbsp->free = true;
406 mcbsp->reg_cache = NULL;
407 spin_unlock(&mcbsp->lock);
408
409 kfree(reg_cache);
410}
411
412/*
413 * Here we start the McBSP, by enabling transmitter, receiver or both.
414 * If no transmitter or receiver is active prior calling, then sample-rate
415 * generator and frame sync are started.
416 */
417static void omap_mcbsp_start(struct omap_mcbsp *mcbsp, int stream)
418{
419 int tx = (stream == SNDRV_PCM_STREAM_PLAYBACK);
420 int rx = !tx;
421 int enable_srg = 0;
422 u16 w;
423
424 if (mcbsp->st_data)
425 omap_mcbsp_st_start(mcbsp);
426
427 /* Only enable SRG, if McBSP is master */
428 w = MCBSP_READ_CACHE(mcbsp, PCR0);
429 if (w & (FSXM | FSRM | CLKXM | CLKRM))
430 enable_srg = !((MCBSP_READ_CACHE(mcbsp, SPCR2) |
431 MCBSP_READ_CACHE(mcbsp, SPCR1)) & 1);
432
433 if (enable_srg) {
434 /* Start the sample generator */
435 w = MCBSP_READ_CACHE(mcbsp, SPCR2);
436 MCBSP_WRITE(mcbsp, SPCR2, w | (1 << 6));
437 }
438
439 /* Enable transmitter and receiver */
440 tx &= 1;
441 w = MCBSP_READ_CACHE(mcbsp, SPCR2);
442 MCBSP_WRITE(mcbsp, SPCR2, w | tx);
443
444 rx &= 1;
445 w = MCBSP_READ_CACHE(mcbsp, SPCR1);
446 MCBSP_WRITE(mcbsp, SPCR1, w | rx);
447
448 /*
449 * Worst case: CLKSRG*2 = 8000khz: (1/8000) * 2 * 2 usec
450 * REVISIT: 100us may give enough time for two CLKSRG, however
451 * due to some unknown PM related, clock gating etc. reason it
452 * is now at 500us.
453 */
454 udelay(500);
455
456 if (enable_srg) {
457 /* Start frame sync */
458 w = MCBSP_READ_CACHE(mcbsp, SPCR2);
459 MCBSP_WRITE(mcbsp, SPCR2, w | (1 << 7));
460 }
461
462 if (mcbsp->pdata->has_ccr) {
463 /* Release the transmitter and receiver */
464 w = MCBSP_READ_CACHE(mcbsp, XCCR);
465 w &= ~(tx ? XDISABLE : 0);
466 MCBSP_WRITE(mcbsp, XCCR, w);
467 w = MCBSP_READ_CACHE(mcbsp, RCCR);
468 w &= ~(rx ? RDISABLE : 0);
469 MCBSP_WRITE(mcbsp, RCCR, w);
470 }
471
472 /* Dump McBSP Regs */
473 omap_mcbsp_dump_reg(mcbsp);
474}
475
476static void omap_mcbsp_stop(struct omap_mcbsp *mcbsp, int stream)
477{
478 int tx = (stream == SNDRV_PCM_STREAM_PLAYBACK);
479 int rx = !tx;
480 int idle;
481 u16 w;
482
483 /* Reset transmitter */
484 tx &= 1;
485 if (mcbsp->pdata->has_ccr) {
486 w = MCBSP_READ_CACHE(mcbsp, XCCR);
487 w |= (tx ? XDISABLE : 0);
488 MCBSP_WRITE(mcbsp, XCCR, w);
489 }
490 w = MCBSP_READ_CACHE(mcbsp, SPCR2);
491 MCBSP_WRITE(mcbsp, SPCR2, w & ~tx);
492
493 /* Reset receiver */
494 rx &= 1;
495 if (mcbsp->pdata->has_ccr) {
496 w = MCBSP_READ_CACHE(mcbsp, RCCR);
497 w |= (rx ? RDISABLE : 0);
498 MCBSP_WRITE(mcbsp, RCCR, w);
499 }
500 w = MCBSP_READ_CACHE(mcbsp, SPCR1);
501 MCBSP_WRITE(mcbsp, SPCR1, w & ~rx);
502
503 idle = !((MCBSP_READ_CACHE(mcbsp, SPCR2) |
504 MCBSP_READ_CACHE(mcbsp, SPCR1)) & 1);
505
506 if (idle) {
507 /* Reset the sample rate generator */
508 w = MCBSP_READ_CACHE(mcbsp, SPCR2);
509 MCBSP_WRITE(mcbsp, SPCR2, w & ~(1 << 6));
510 }
511
512 if (mcbsp->st_data)
513 omap_mcbsp_st_stop(mcbsp);
514}
515
516#define max_thres(m) (mcbsp->pdata->buffer_size)
517#define valid_threshold(m, val) ((val) <= max_thres(m))
518#define THRESHOLD_PROP_BUILDER(prop) \
519static ssize_t prop##_show(struct device *dev, \
520 struct device_attribute *attr, char *buf) \
521{ \
522 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev); \
523 \
524 return sprintf(buf, "%u\n", mcbsp->prop); \
525} \
526 \
527static ssize_t prop##_store(struct device *dev, \
528 struct device_attribute *attr, \
529 const char *buf, size_t size) \
530{ \
531 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev); \
532 unsigned long val; \
533 int status; \
534 \
535 status = kstrtoul(buf, 0, &val); \
536 if (status) \
537 return status; \
538 \
539 if (!valid_threshold(mcbsp, val)) \
540 return -EDOM; \
541 \
542 mcbsp->prop = val; \
543 return size; \
544} \
545 \
546static DEVICE_ATTR(prop, 0644, prop##_show, prop##_store)
547
548THRESHOLD_PROP_BUILDER(max_tx_thres);
549THRESHOLD_PROP_BUILDER(max_rx_thres);
550
551static const char * const dma_op_modes[] = {
552 "element", "threshold",
553};
554
555static ssize_t dma_op_mode_show(struct device *dev,
556 struct device_attribute *attr, char *buf)
557{
558 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
559 int dma_op_mode, i = 0;
560 ssize_t len = 0;
561 const char * const *s;
562
563 dma_op_mode = mcbsp->dma_op_mode;
564
565 for (s = &dma_op_modes[i]; i < ARRAY_SIZE(dma_op_modes); s++, i++) {
566 if (dma_op_mode == i)
567 len += sprintf(buf + len, "[%s] ", *s);
568 else
569 len += sprintf(buf + len, "%s ", *s);
570 }
571 len += sprintf(buf + len, "\n");
572
573 return len;
574}
575
576static ssize_t dma_op_mode_store(struct device *dev,
577 struct device_attribute *attr, const char *buf,
578 size_t size)
579{
580 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
581 int i;
582
583 i = sysfs_match_string(dma_op_modes, buf);
584 if (i < 0)
585 return i;
586
587 spin_lock_irq(&mcbsp->lock);
588 if (!mcbsp->free) {
589 size = -EBUSY;
590 goto unlock;
591 }
592 mcbsp->dma_op_mode = i;
593
594unlock:
595 spin_unlock_irq(&mcbsp->lock);
596
597 return size;
598}
599
600static DEVICE_ATTR_RW(dma_op_mode);
601
602static const struct attribute *additional_attrs[] = {
603 &dev_attr_max_tx_thres.attr,
604 &dev_attr_max_rx_thres.attr,
605 &dev_attr_dma_op_mode.attr,
606 NULL,
607};
608
609static const struct attribute_group additional_attr_group = {
610 .attrs = (struct attribute **)additional_attrs,
611};
612
613/*
614 * McBSP1 and McBSP3 are directly mapped on 1610 and 1510.
615 * 730 has only 2 McBSP, and both of them are MPU peripherals.
616 */
617static int omap_mcbsp_init(struct platform_device *pdev)
618{
619 struct omap_mcbsp *mcbsp = platform_get_drvdata(pdev);
620 struct resource *res;
621 int ret = 0;
622
623 spin_lock_init(&mcbsp->lock);
624 mcbsp->free = true;
625
626 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mpu");
627 if (!res)
628 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
629
630 mcbsp->io_base = devm_ioremap_resource(&pdev->dev, res);
631 if (IS_ERR(mcbsp->io_base))
632 return PTR_ERR(mcbsp->io_base);
633
634 mcbsp->phys_base = res->start;
635 mcbsp->reg_cache_size = resource_size(res);
636
637 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dma");
638 if (!res)
639 mcbsp->phys_dma_base = mcbsp->phys_base;
640 else
641 mcbsp->phys_dma_base = res->start;
642
643 /*
644 * OMAP1, 2 uses two interrupt lines: TX, RX
645 * OMAP2430, OMAP3 SoC have combined IRQ line as well.
646 * OMAP4 and newer SoC only have the combined IRQ line.
647 * Use the combined IRQ if available since it gives better debugging
648 * possibilities.
649 */
650 mcbsp->irq = platform_get_irq_byname(pdev, "common");
651 if (mcbsp->irq == -ENXIO) {
652 mcbsp->tx_irq = platform_get_irq_byname(pdev, "tx");
653
654 if (mcbsp->tx_irq == -ENXIO) {
655 mcbsp->irq = platform_get_irq(pdev, 0);
656 mcbsp->tx_irq = 0;
657 } else {
658 mcbsp->rx_irq = platform_get_irq_byname(pdev, "rx");
659 mcbsp->irq = 0;
660 }
661 }
662
663 if (!pdev->dev.of_node) {
664 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "tx");
665 if (!res) {
666 dev_err(&pdev->dev, "invalid tx DMA channel\n");
667 return -ENODEV;
668 }
669 mcbsp->dma_req[0] = res->start;
670 mcbsp->dma_data[0].filter_data = &mcbsp->dma_req[0];
671
672 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "rx");
673 if (!res) {
674 dev_err(&pdev->dev, "invalid rx DMA channel\n");
675 return -ENODEV;
676 }
677 mcbsp->dma_req[1] = res->start;
678 mcbsp->dma_data[1].filter_data = &mcbsp->dma_req[1];
679 } else {
680 mcbsp->dma_data[0].filter_data = "tx";
681 mcbsp->dma_data[1].filter_data = "rx";
682 }
683
684 mcbsp->dma_data[0].addr = omap_mcbsp_dma_reg_params(mcbsp,
685 SNDRV_PCM_STREAM_PLAYBACK);
686 mcbsp->dma_data[1].addr = omap_mcbsp_dma_reg_params(mcbsp,
687 SNDRV_PCM_STREAM_CAPTURE);
688
689 mcbsp->fclk = clk_get(&pdev->dev, "fck");
690 if (IS_ERR(mcbsp->fclk)) {
691 ret = PTR_ERR(mcbsp->fclk);
692 dev_err(mcbsp->dev, "unable to get fck: %d\n", ret);
693 return ret;
694 }
695
696 mcbsp->dma_op_mode = MCBSP_DMA_MODE_ELEMENT;
697 if (mcbsp->pdata->buffer_size) {
698 /*
699 * Initially configure the maximum thresholds to a safe value.
700 * The McBSP FIFO usage with these values should not go under
701 * 16 locations.
702 * If the whole FIFO without safety buffer is used, than there
703 * is a possibility that the DMA will be not able to push the
704 * new data on time, causing channel shifts in runtime.
705 */
706 mcbsp->max_tx_thres = max_thres(mcbsp) - 0x10;
707 mcbsp->max_rx_thres = max_thres(mcbsp) - 0x10;
708
709 ret = sysfs_create_group(&mcbsp->dev->kobj,
710 &additional_attr_group);
711 if (ret) {
712 dev_err(mcbsp->dev,
713 "Unable to create additional controls\n");
714 goto err_thres;
715 }
Peter Ujfalusi9c34d022018-11-08 09:29:58 +0200716 }
717
718 ret = omap_mcbsp_st_init(pdev);
719 if (ret)
720 goto err_st;
721
722 return 0;
723
724err_st:
725 if (mcbsp->pdata->buffer_size)
726 sysfs_remove_group(&mcbsp->dev->kobj, &additional_attr_group);
727err_thres:
728 clk_put(mcbsp->fclk);
729 return ret;
730}
731
Jarkko Nikula2e747962008-04-25 13:55:19 +0200732/*
733 * Stream DMA parameters. DMA request line and port address are set runtime
734 * since they are different between OMAP1 and later OMAPs
735 */
Lars-Peter Clausenabe99372013-03-25 16:58:16 +0100736static void omap_mcbsp_set_threshold(struct snd_pcm_substream *substream,
737 unsigned int packet_size)
Eduardo Valentincaebc0c2009-08-20 16:18:25 +0300738{
739 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000740 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Peter Ujfalusi45656b42012-02-14 18:20:58 +0200741 struct omap_mcbsp *mcbsp = snd_soc_dai_get_drvdata(cpu_dai);
Peter Ujfalusi3f024032010-06-03 07:39:35 +0300742 int words;
Eduardo Valentina0a499c2009-08-20 16:18:26 +0300743
Peter Ujfalusibe51c572018-11-08 09:29:57 +0200744 /* No need to proceed further if McBSP does not have FIFO */
745 if (mcbsp->pdata->buffer_size == 0)
746 return;
747
Peter Ujfalusi778a17c2012-03-15 12:20:32 +0200748 /*
749 * Configure McBSP threshold based on either:
750 * packet_size, when the sDMA is in packet mode, or based on the
751 * period size in THRESHOLD mode, otherwise use McBSP threshold = 1
752 * for mono streams.
753 */
Lars-Peter Clausenabe99372013-03-25 16:58:16 +0100754 if (packet_size)
755 words = packet_size;
Eduardo Valentina0a499c2009-08-20 16:18:26 +0300756 else
Peter Ujfalusi3f024032010-06-03 07:39:35 +0300757 words = 1;
Eduardo Valentincaebc0c2009-08-20 16:18:25 +0300758
759 /* Configure McBSP internal buffer usage */
760 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Peter Ujfalusi45656b42012-02-14 18:20:58 +0200761 omap_mcbsp_set_tx_threshold(mcbsp, words);
Eduardo Valentincaebc0c2009-08-20 16:18:25 +0300762 else
Peter Ujfalusi45656b42012-02-14 18:20:58 +0200763 omap_mcbsp_set_rx_threshold(mcbsp, words);
Eduardo Valentincaebc0c2009-08-20 16:18:25 +0300764}
765
Peter Ujfalusiddc29b02010-06-03 07:39:36 +0300766static int omap_mcbsp_hwrule_min_buffersize(struct snd_pcm_hw_params *params,
767 struct snd_pcm_hw_rule *rule)
768{
769 struct snd_interval *buffer_size = hw_param_interval(params,
770 SNDRV_PCM_HW_PARAM_BUFFER_SIZE);
771 struct snd_interval *channels = hw_param_interval(params,
772 SNDRV_PCM_HW_PARAM_CHANNELS);
Peter Ujfalusi45656b42012-02-14 18:20:58 +0200773 struct omap_mcbsp *mcbsp = rule->private;
Peter Ujfalusiddc29b02010-06-03 07:39:36 +0300774 struct snd_interval frames;
775 int size;
776
777 snd_interval_any(&frames);
Peter Ujfalusicb40b632012-02-13 16:26:54 +0200778 size = mcbsp->pdata->buffer_size;
Peter Ujfalusiddc29b02010-06-03 07:39:36 +0300779
780 frames.min = size / channels->min;
781 frames.integer = 1;
782 return snd_interval_refine(buffer_size, &frames);
783}
784
Mark Browndee89c42008-11-18 22:11:38 +0000785static int omap_mcbsp_dai_startup(struct snd_pcm_substream *substream,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000786 struct snd_soc_dai *cpu_dai)
Jarkko Nikula2e747962008-04-25 13:55:19 +0200787{
Peter Ujfalusi45656b42012-02-14 18:20:58 +0200788 struct omap_mcbsp *mcbsp = snd_soc_dai_get_drvdata(cpu_dai);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200789 int err = 0;
790
Eduardo Valentincaebc0c2009-08-20 16:18:25 +0300791 if (!cpu_dai->active)
Peter Ujfalusi45656b42012-02-14 18:20:58 +0200792 err = omap_mcbsp_request(mcbsp);
Eduardo Valentincaebc0c2009-08-20 16:18:25 +0300793
Peter Ujfalusiddc29b02010-06-03 07:39:36 +0300794 /*
795 * OMAP3 McBSP FIFO is word structured.
796 * McBSP2 has 1024 + 256 = 1280 word long buffer,
797 * McBSP1,3,4,5 has 128 word long buffer
798 * This means that the size of the FIFO depends on the sample format.
799 * For example on McBSP3:
800 * 16bit samples: size is 128 * 2 = 256 bytes
801 * 32bit samples: size is 128 * 4 = 512 bytes
802 * It is simpler to place constraint for buffer and period based on
803 * channels.
804 * McBSP3 as example again (16 or 32 bit samples):
805 * 1 channel (mono): size is 128 frames (128 words)
806 * 2 channels (stereo): size is 128 / 2 = 64 frames (2 * 64 words)
807 * 4 channels: size is 128 / 4 = 32 frames (4 * 32 words)
808 */
Peter Ujfalusi45656b42012-02-14 18:20:58 +0200809 if (mcbsp->pdata->buffer_size) {
Jarkko Nikula69849922009-03-27 15:32:01 +0200810 /*
Peter Ujfalusi998a8a62010-07-29 09:51:28 +0300811 * Rule for the buffer size. We should not allow
Peter Ujfalusice37f5e2012-03-20 11:47:36 +0200812 * smaller buffer than the FIFO size to avoid underruns.
813 * This applies only for the playback stream.
Peter Ujfalusiddc29b02010-06-03 07:39:36 +0300814 */
Peter Ujfalusice37f5e2012-03-20 11:47:36 +0200815 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
816 snd_pcm_hw_rule_add(substream->runtime, 0,
817 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
818 omap_mcbsp_hwrule_min_buffersize,
819 mcbsp,
820 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
Peter Ujfalusiddc29b02010-06-03 07:39:36 +0300821
Peter Ujfalusi998a8a62010-07-29 09:51:28 +0300822 /* Make sure, that the period size is always even */
823 snd_pcm_hw_constraint_step(substream->runtime, 0,
824 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 2);
Eduardo Valentincaebc0c2009-08-20 16:18:25 +0300825 }
Jarkko Nikula2e747962008-04-25 13:55:19 +0200826
827 return err;
828}
829
Mark Browndee89c42008-11-18 22:11:38 +0000830static void omap_mcbsp_dai_shutdown(struct snd_pcm_substream *substream,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000831 struct snd_soc_dai *cpu_dai)
Jarkko Nikula2e747962008-04-25 13:55:19 +0200832{
Peter Ujfalusi45656b42012-02-14 18:20:58 +0200833 struct omap_mcbsp *mcbsp = snd_soc_dai_get_drvdata(cpu_dai);
Matt Ranostay9834ffd2017-01-31 13:21:43 -0800834 int tx = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
835 int stream1 = tx ? SNDRV_PCM_STREAM_PLAYBACK : SNDRV_PCM_STREAM_CAPTURE;
836 int stream2 = tx ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
837
838 if (mcbsp->latency[stream2])
839 pm_qos_update_request(&mcbsp->pm_qos_req,
840 mcbsp->latency[stream2]);
841 else if (mcbsp->latency[stream1])
842 pm_qos_remove_request(&mcbsp->pm_qos_req);
843
844 mcbsp->latency[stream1] = 0;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200845
846 if (!cpu_dai->active) {
Peter Ujfalusi45656b42012-02-14 18:20:58 +0200847 omap_mcbsp_free(mcbsp);
Peter Ujfalusi256d9c22012-02-14 15:23:15 +0200848 mcbsp->configured = 0;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200849 }
850}
851
Matt Ranostay9834ffd2017-01-31 13:21:43 -0800852static int omap_mcbsp_dai_prepare(struct snd_pcm_substream *substream,
853 struct snd_soc_dai *cpu_dai)
854{
855 struct omap_mcbsp *mcbsp = snd_soc_dai_get_drvdata(cpu_dai);
856 struct pm_qos_request *pm_qos_req = &mcbsp->pm_qos_req;
857 int tx = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
858 int stream1 = tx ? SNDRV_PCM_STREAM_PLAYBACK : SNDRV_PCM_STREAM_CAPTURE;
859 int stream2 = tx ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
860 int latency = mcbsp->latency[stream2];
861
862 /* Prevent omap hardware from hitting off between FIFO fills */
863 if (!latency || mcbsp->latency[stream1] < latency)
864 latency = mcbsp->latency[stream1];
865
866 if (pm_qos_request_active(pm_qos_req))
867 pm_qos_update_request(pm_qos_req, latency);
868 else if (latency)
869 pm_qos_add_request(pm_qos_req, PM_QOS_CPU_DMA_LATENCY, latency);
870
871 return 0;
872}
873
Mark Browndee89c42008-11-18 22:11:38 +0000874static int omap_mcbsp_dai_trigger(struct snd_pcm_substream *substream, int cmd,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000875 struct snd_soc_dai *cpu_dai)
Jarkko Nikula2e747962008-04-25 13:55:19 +0200876{
Peter Ujfalusi45656b42012-02-14 18:20:58 +0200877 struct omap_mcbsp *mcbsp = snd_soc_dai_get_drvdata(cpu_dai);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200878
879 switch (cmd) {
880 case SNDRV_PCM_TRIGGER_START:
881 case SNDRV_PCM_TRIGGER_RESUME:
882 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Peter Ujfalusi256d9c22012-02-14 15:23:15 +0200883 mcbsp->active++;
Peter Ujfalusi59d177f2018-11-08 09:29:56 +0200884 omap_mcbsp_start(mcbsp, substream->stream);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200885 break;
886
887 case SNDRV_PCM_TRIGGER_STOP:
888 case SNDRV_PCM_TRIGGER_SUSPEND:
889 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Peter Ujfalusi59d177f2018-11-08 09:29:56 +0200890 omap_mcbsp_stop(mcbsp, substream->stream);
Peter Ujfalusi256d9c22012-02-14 15:23:15 +0200891 mcbsp->active--;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200892 break;
893 default:
Peter Ujfalusi59d177f2018-11-08 09:29:56 +0200894 return -EINVAL;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200895 }
896
Peter Ujfalusi59d177f2018-11-08 09:29:56 +0200897 return 0;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200898}
899
Peter Ujfalusi75581d22010-03-03 15:08:09 +0200900static snd_pcm_sframes_t omap_mcbsp_dai_delay(
901 struct snd_pcm_substream *substream,
902 struct snd_soc_dai *dai)
903{
904 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000905 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Peter Ujfalusi45656b42012-02-14 18:20:58 +0200906 struct omap_mcbsp *mcbsp = snd_soc_dai_get_drvdata(cpu_dai);
Peter Ujfalusi75581d22010-03-03 15:08:09 +0200907 u16 fifo_use;
908 snd_pcm_sframes_t delay;
909
Peter Ujfalusibe51c572018-11-08 09:29:57 +0200910 /* No need to proceed further if McBSP does not have FIFO */
911 if (mcbsp->pdata->buffer_size == 0)
912 return 0;
913
Peter Ujfalusi75581d22010-03-03 15:08:09 +0200914 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Peter Ujfalusi45656b42012-02-14 18:20:58 +0200915 fifo_use = omap_mcbsp_get_tx_delay(mcbsp);
Peter Ujfalusi75581d22010-03-03 15:08:09 +0200916 else
Peter Ujfalusi45656b42012-02-14 18:20:58 +0200917 fifo_use = omap_mcbsp_get_rx_delay(mcbsp);
Peter Ujfalusi75581d22010-03-03 15:08:09 +0200918
919 /*
920 * Divide the used locations with the channel count to get the
921 * FIFO usage in samples (don't care about partial samples in the
922 * buffer).
923 */
924 delay = fifo_use / substream->runtime->channels;
925
926 return delay;
927}
928
Jarkko Nikula2e747962008-04-25 13:55:19 +0200929static int omap_mcbsp_dai_hw_params(struct snd_pcm_substream *substream,
Mark Browndee89c42008-11-18 22:11:38 +0000930 struct snd_pcm_hw_params *params,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000931 struct snd_soc_dai *cpu_dai)
Jarkko Nikula2e747962008-04-25 13:55:19 +0200932{
Peter Ujfalusi45656b42012-02-14 18:20:58 +0200933 struct omap_mcbsp *mcbsp = snd_soc_dai_get_drvdata(cpu_dai);
Peter Ujfalusi256d9c22012-02-14 15:23:15 +0200934 struct omap_mcbsp_reg_cfg *regs = &mcbsp->cfg_regs;
Lars-Peter Clausen09ae3aa2013-04-03 11:06:05 +0200935 struct snd_dmaengine_dai_dma_data *dma_data;
Peter Ujfalusi061fb362012-09-14 15:05:51 +0300936 int wlen, channels, wpf;
Peter Ujfalusicf80e152010-07-29 09:51:27 +0300937 int pkt_size = 0;
Graeme Gregory5f63ef92009-11-09 19:02:15 +0000938 unsigned int format, div, framesize, master;
Matt Ranostay9834ffd2017-01-31 13:21:43 -0800939 unsigned int buffer_size = mcbsp->pdata->buffer_size;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200940
Peter Ujfalusibcd6da72012-09-14 15:05:57 +0300941 dma_data = snd_soc_dai_get_dma_data(cpu_dai, substream);
Peter Ujfalusi778a17c2012-03-15 12:20:32 +0200942 channels = params_channels(params);
Kishon Vijay Abraham I2686e072011-02-24 15:16:56 +0530943
Sergey Lapind98508a2010-05-13 19:48:16 +0400944 switch (params_format(params)) {
945 case SNDRV_PCM_FORMAT_S16_LE:
Peter Ujfalusicf80e152010-07-29 09:51:27 +0300946 wlen = 16;
Sergey Lapind98508a2010-05-13 19:48:16 +0400947 break;
948 case SNDRV_PCM_FORMAT_S32_LE:
Peter Ujfalusicf80e152010-07-29 09:51:27 +0300949 wlen = 32;
Sergey Lapind98508a2010-05-13 19:48:16 +0400950 break;
951 default:
952 return -EINVAL;
953 }
Matt Ranostay9834ffd2017-01-31 13:21:43 -0800954 if (buffer_size) {
955 int latency;
956
Peter Ujfalusicb40b632012-02-13 16:26:54 +0200957 if (mcbsp->dma_op_mode == MCBSP_DMA_MODE_THRESHOLD) {
Peter Ujfalusicf80e152010-07-29 09:51:27 +0300958 int period_words, max_thrsh;
Peter Ujfalusidffb3602012-09-14 15:05:49 +0300959 int divider = 0;
Peter Ujfalusicf80e152010-07-29 09:51:27 +0300960
961 period_words = params_period_bytes(params) / (wlen / 8);
962 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Peter Ujfalusicb40b632012-02-13 16:26:54 +0200963 max_thrsh = mcbsp->max_tx_thres;
Peter Ujfalusicf80e152010-07-29 09:51:27 +0300964 else
Peter Ujfalusicb40b632012-02-13 16:26:54 +0200965 max_thrsh = mcbsp->max_rx_thres;
Peter Ujfalusicf80e152010-07-29 09:51:27 +0300966 /*
Peter Ujfalusidffb3602012-09-14 15:05:49 +0300967 * Use sDMA packet mode if McBSP is in threshold mode:
968 * If period words less than the FIFO size the packet
969 * size is set to the number of period words, otherwise
970 * Look for the biggest threshold value which divides
971 * the period size evenly.
Peter Ujfalusicf80e152010-07-29 09:51:27 +0300972 */
Peter Ujfalusidffb3602012-09-14 15:05:49 +0300973 divider = period_words / max_thrsh;
974 if (period_words % max_thrsh)
975 divider++;
976 while (period_words % divider &&
977 divider < period_words)
978 divider++;
979 if (divider == period_words)
980 return -EINVAL;
Peter Ujfalusicf80e152010-07-29 09:51:27 +0300981
Peter Ujfalusidffb3602012-09-14 15:05:49 +0300982 pkt_size = period_words / divider;
Peter Ujfalusi778a17c2012-03-15 12:20:32 +0200983 } else if (channels > 1) {
984 /* Use packet mode for non mono streams */
985 pkt_size = channels;
Peter Ujfalusicf80e152010-07-29 09:51:27 +0300986 }
Matt Ranostay9834ffd2017-01-31 13:21:43 -0800987
Peter Ujfalusidd2f52d2018-11-14 13:06:21 +0200988 latency = (buffer_size - pkt_size) / channels;
989 latency = latency * USEC_PER_SEC /
990 (params->rate_num / params->rate_den);
Matt Ranostay9834ffd2017-01-31 13:21:43 -0800991 mcbsp->latency[substream->stream] = latency;
992
Lars-Peter Clausenabe99372013-03-25 16:58:16 +0100993 omap_mcbsp_set_threshold(substream, pkt_size);
Peter Ujfalusi15d01432010-07-29 09:51:25 +0300994 }
995
Lars-Peter Clausen09ae3aa2013-04-03 11:06:05 +0200996 dma_data->maxburst = pkt_size;
Daniel Mackfd23b7d2010-03-19 14:52:55 +0000997
Peter Ujfalusi256d9c22012-02-14 15:23:15 +0200998 if (mcbsp->configured) {
Jarkko Nikula2e747962008-04-25 13:55:19 +0200999 /* McBSP already configured by another stream */
1000 return 0;
1001 }
1002
Jarkko Nikula4dd04172011-09-30 16:07:44 +03001003 regs->rcr2 &= ~(RPHASE | RFRLEN2(0x7f) | RWDLEN2(7));
1004 regs->xcr2 &= ~(RPHASE | XFRLEN2(0x7f) | XWDLEN2(7));
1005 regs->rcr1 &= ~(RFRLEN1(0x7f) | RWDLEN1(7));
1006 regs->xcr1 &= ~(XFRLEN1(0x7f) | XWDLEN1(7));
Peter Ujfalusi256d9c22012-02-14 15:23:15 +02001007 format = mcbsp->fmt & SND_SOC_DAIFMT_FORMAT_MASK;
Peter Ujfalusi778a17c2012-03-15 12:20:32 +02001008 wpf = channels;
Peter Ujfalusi299a1512010-03-19 12:27:31 +02001009 if (channels == 2 && (format == SND_SOC_DAIFMT_I2S ||
1010 format == SND_SOC_DAIFMT_LEFT_J)) {
Graeme Gregory5f63ef92009-11-09 19:02:15 +00001011 /* Use dual-phase frames */
1012 regs->rcr2 |= RPHASE;
1013 regs->xcr2 |= XPHASE;
1014 /* Set 1 word per (McBSP) frame for phase1 and phase2 */
1015 wpf--;
1016 regs->rcr2 |= RFRLEN2(wpf - 1);
1017 regs->xcr2 |= XFRLEN2(wpf - 1);
Jarkko Nikula2e747962008-04-25 13:55:19 +02001018 }
1019
Graeme Gregory5f63ef92009-11-09 19:02:15 +00001020 regs->rcr1 |= RFRLEN1(wpf - 1);
1021 regs->xcr1 |= XFRLEN1(wpf - 1);
1022
Jarkko Nikula2e747962008-04-25 13:55:19 +02001023 switch (params_format(params)) {
1024 case SNDRV_PCM_FORMAT_S16_LE:
1025 /* Set word lengths */
1026 regs->rcr2 |= RWDLEN2(OMAP_MCBSP_WORD_16);
1027 regs->rcr1 |= RWDLEN1(OMAP_MCBSP_WORD_16);
1028 regs->xcr2 |= XWDLEN2(OMAP_MCBSP_WORD_16);
1029 regs->xcr1 |= XWDLEN1(OMAP_MCBSP_WORD_16);
Jarkko Nikula2e747962008-04-25 13:55:19 +02001030 break;
Sergey Lapind98508a2010-05-13 19:48:16 +04001031 case SNDRV_PCM_FORMAT_S32_LE:
1032 /* Set word lengths */
Sergey Lapind98508a2010-05-13 19:48:16 +04001033 regs->rcr2 |= RWDLEN2(OMAP_MCBSP_WORD_32);
1034 regs->rcr1 |= RWDLEN1(OMAP_MCBSP_WORD_32);
1035 regs->xcr2 |= XWDLEN2(OMAP_MCBSP_WORD_32);
1036 regs->xcr1 |= XWDLEN1(OMAP_MCBSP_WORD_32);
1037 break;
Jarkko Nikula2e747962008-04-25 13:55:19 +02001038 default:
1039 /* Unsupported PCM format */
1040 return -EINVAL;
1041 }
1042
Graeme Gregory5f63ef92009-11-09 19:02:15 +00001043 /* In McBSP master modes, FRAME (i.e. sample rate) is generated
1044 * by _counting_ BCLKs. Calculate frame size in BCLKs */
Peter Ujfalusi256d9c22012-02-14 15:23:15 +02001045 master = mcbsp->fmt & SND_SOC_DAIFMT_MASTER_MASK;
Graeme Gregory5f63ef92009-11-09 19:02:15 +00001046 if (master == SND_SOC_DAIFMT_CBS_CFS) {
Peter Ujfalusi256d9c22012-02-14 15:23:15 +02001047 div = mcbsp->clk_div ? mcbsp->clk_div : 1;
1048 framesize = (mcbsp->in_freq / div) / params_rate(params);
Graeme Gregory5f63ef92009-11-09 19:02:15 +00001049
1050 if (framesize < wlen * channels) {
1051 printk(KERN_ERR "%s: not enough bandwidth for desired rate and "
1052 "channels\n", __func__);
1053 return -EINVAL;
1054 }
1055 } else
1056 framesize = wlen * channels;
1057
Jarkko Nikulaba9d0fd2008-10-20 15:29:59 +03001058 /* Set FS period and length in terms of bit clock periods */
Jarkko Nikula4dd04172011-09-30 16:07:44 +03001059 regs->srgr2 &= ~FPER(0xfff);
1060 regs->srgr1 &= ~FWID(0xff);
Peter Ujfalusic29b2062009-04-15 15:38:55 +03001061 switch (format) {
Jarkko Nikulaba9d0fd2008-10-20 15:29:59 +03001062 case SND_SOC_DAIFMT_I2S:
Peter Ujfalusi299a1512010-03-19 12:27:31 +02001063 case SND_SOC_DAIFMT_LEFT_J:
Graeme Gregory5f63ef92009-11-09 19:02:15 +00001064 regs->srgr2 |= FPER(framesize - 1);
1065 regs->srgr1 |= FWID((framesize >> 1) - 1);
Jarkko Nikulaba9d0fd2008-10-20 15:29:59 +03001066 break;
Peter Ujfalusi3ba191c2009-04-15 15:38:56 +03001067 case SND_SOC_DAIFMT_DSP_A:
Jarkko Nikulabd258672008-12-22 10:21:36 +02001068 case SND_SOC_DAIFMT_DSP_B:
Graeme Gregory5f63ef92009-11-09 19:02:15 +00001069 regs->srgr2 |= FPER(framesize - 1);
Jarkko Nikula36ce8582009-04-15 13:48:16 +03001070 regs->srgr1 |= FWID(0);
Jarkko Nikulaba9d0fd2008-10-20 15:29:59 +03001071 break;
1072 }
1073
Peter Ujfalusi256d9c22012-02-14 15:23:15 +02001074 omap_mcbsp_config(mcbsp, &mcbsp->cfg_regs);
1075 mcbsp->wlen = wlen;
1076 mcbsp->configured = 1;
Jarkko Nikula2e747962008-04-25 13:55:19 +02001077
1078 return 0;
1079}
1080
1081/*
1082 * This must be called before _set_clkdiv and _set_sysclk since McBSP register
1083 * cache is initialized here
1084 */
Liam Girdwood8687eb82008-07-07 16:08:07 +01001085static int omap_mcbsp_dai_set_dai_fmt(struct snd_soc_dai *cpu_dai,
Jarkko Nikula2e747962008-04-25 13:55:19 +02001086 unsigned int fmt)
1087{
Peter Ujfalusi45656b42012-02-14 18:20:58 +02001088 struct omap_mcbsp *mcbsp = snd_soc_dai_get_drvdata(cpu_dai);
Peter Ujfalusi256d9c22012-02-14 15:23:15 +02001089 struct omap_mcbsp_reg_cfg *regs = &mcbsp->cfg_regs;
Jarkko Nikula91a18ae2011-09-30 10:55:32 +03001090 bool inv_fs = false;
Jarkko Nikula2e747962008-04-25 13:55:19 +02001091
Peter Ujfalusi256d9c22012-02-14 15:23:15 +02001092 if (mcbsp->configured)
Jarkko Nikula2e747962008-04-25 13:55:19 +02001093 return 0;
1094
Peter Ujfalusi256d9c22012-02-14 15:23:15 +02001095 mcbsp->fmt = fmt;
Jarkko Nikula2e747962008-04-25 13:55:19 +02001096 memset(regs, 0, sizeof(*regs));
1097 /* Generic McBSP register settings */
1098 regs->spcr2 |= XINTM(3) | FREE;
1099 regs->spcr1 |= RINTM(3);
Peter Ujfalusidc26df52012-08-16 16:41:06 +03001100 /* RFIG and XFIG are not defined in 2430 and on OMAP3+ */
1101 if (!mcbsp->pdata->has_ccr) {
Eero Nurkkalac721bbd2009-08-20 16:18:23 +03001102 regs->rcr2 |= RFIG;
1103 regs->xcr2 |= XFIG;
1104 }
Peter Ujfalusidc26df52012-08-16 16:41:06 +03001105
1106 /* Configure XCCR/RCCR only for revisions which have ccr registers */
1107 if (mcbsp->pdata->has_ccr) {
Jarkko Nikula32080af2009-08-23 12:24:26 +03001108 regs->xccr = DXENDLY(1) | XDMAEN | XDISABLE;
1109 regs->rccr = RFULL_CYCLE | RDMAEN | RDISABLE;
Misael Lopez Cruzef390c02009-01-29 13:29:46 +02001110 }
Jarkko Nikula2e747962008-04-25 13:55:19 +02001111
1112 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
1113 case SND_SOC_DAIFMT_I2S:
1114 /* 1-bit data delay */
1115 regs->rcr2 |= RDATDLY(1);
1116 regs->xcr2 |= XDATDLY(1);
1117 break;
Peter Ujfalusi299a1512010-03-19 12:27:31 +02001118 case SND_SOC_DAIFMT_LEFT_J:
1119 /* 0-bit data delay */
1120 regs->rcr2 |= RDATDLY(0);
1121 regs->xcr2 |= XDATDLY(0);
1122 regs->spcr1 |= RJUST(2);
1123 /* Invert FS polarity configuration */
Jarkko Nikula91a18ae2011-09-30 10:55:32 +03001124 inv_fs = true;
Peter Ujfalusi299a1512010-03-19 12:27:31 +02001125 break;
Peter Ujfalusi3ba191c2009-04-15 15:38:56 +03001126 case SND_SOC_DAIFMT_DSP_A:
1127 /* 1-bit data delay */
1128 regs->rcr2 |= RDATDLY(1);
1129 regs->xcr2 |= XDATDLY(1);
1130 /* Invert FS polarity configuration */
Jarkko Nikula91a18ae2011-09-30 10:55:32 +03001131 inv_fs = true;
Peter Ujfalusi3ba191c2009-04-15 15:38:56 +03001132 break;
Jarkko Nikulabd258672008-12-22 10:21:36 +02001133 case SND_SOC_DAIFMT_DSP_B:
Arun KS3336c5b2008-10-02 15:07:06 +05301134 /* 0-bit data delay */
1135 regs->rcr2 |= RDATDLY(0);
1136 regs->xcr2 |= XDATDLY(0);
Jarkko Nikula36ce8582009-04-15 13:48:16 +03001137 /* Invert FS polarity configuration */
Jarkko Nikula91a18ae2011-09-30 10:55:32 +03001138 inv_fs = true;
Arun KS3336c5b2008-10-02 15:07:06 +05301139 break;
Jarkko Nikula2e747962008-04-25 13:55:19 +02001140 default:
1141 /* Unsupported data format */
1142 return -EINVAL;
1143 }
1144
1145 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1146 case SND_SOC_DAIFMT_CBS_CFS:
1147 /* McBSP master. Set FS and bit clocks as outputs */
1148 regs->pcr0 |= FSXM | FSRM |
1149 CLKXM | CLKRM;
1150 /* Sample rate generator drives the FS */
1151 regs->srgr2 |= FSGM;
1152 break;
Michael Trimarchi6e20b0d2013-07-21 18:24:01 +02001153 case SND_SOC_DAIFMT_CBM_CFS:
1154 /* McBSP slave. FS clock as output */
1155 regs->srgr2 |= FSGM;
Peter Ujfalusi20602e32015-01-16 11:20:25 +02001156 regs->pcr0 |= FSXM | FSRM;
Michael Trimarchi6e20b0d2013-07-21 18:24:01 +02001157 break;
Jarkko Nikula2e747962008-04-25 13:55:19 +02001158 case SND_SOC_DAIFMT_CBM_CFM:
1159 /* McBSP slave */
1160 break;
1161 default:
1162 /* Unsupported master/slave configuration */
1163 return -EINVAL;
1164 }
1165
1166 /* Set bit clock (CLKX/CLKR) and FS polarities */
Jarkko Nikula91a18ae2011-09-30 10:55:32 +03001167 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
Jarkko Nikula2e747962008-04-25 13:55:19 +02001168 case SND_SOC_DAIFMT_NB_NF:
1169 /*
1170 * Normal BCLK + FS.
1171 * FS active low. TX data driven on falling edge of bit clock
1172 * and RX data sampled on rising edge of bit clock.
1173 */
1174 regs->pcr0 |= FSXP | FSRP |
1175 CLKXP | CLKRP;
1176 break;
1177 case SND_SOC_DAIFMT_NB_IF:
1178 regs->pcr0 |= CLKXP | CLKRP;
1179 break;
1180 case SND_SOC_DAIFMT_IB_NF:
1181 regs->pcr0 |= FSXP | FSRP;
1182 break;
1183 case SND_SOC_DAIFMT_IB_IF:
1184 break;
1185 default:
1186 return -EINVAL;
1187 }
Jarkko Nikula91a18ae2011-09-30 10:55:32 +03001188 if (inv_fs == true)
1189 regs->pcr0 ^= FSXP | FSRP;
Jarkko Nikula2e747962008-04-25 13:55:19 +02001190
1191 return 0;
1192}
1193
Liam Girdwood8687eb82008-07-07 16:08:07 +01001194static int omap_mcbsp_dai_set_clkdiv(struct snd_soc_dai *cpu_dai,
Jarkko Nikula2e747962008-04-25 13:55:19 +02001195 int div_id, int div)
1196{
Peter Ujfalusi45656b42012-02-14 18:20:58 +02001197 struct omap_mcbsp *mcbsp = snd_soc_dai_get_drvdata(cpu_dai);
Peter Ujfalusi256d9c22012-02-14 15:23:15 +02001198 struct omap_mcbsp_reg_cfg *regs = &mcbsp->cfg_regs;
Jarkko Nikula2e747962008-04-25 13:55:19 +02001199
1200 if (div_id != OMAP_MCBSP_CLKGDV)
1201 return -ENODEV;
1202
Peter Ujfalusi256d9c22012-02-14 15:23:15 +02001203 mcbsp->clk_div = div;
Jarkko Nikula4dd04172011-09-30 16:07:44 +03001204 regs->srgr1 &= ~CLKGDV(0xff);
Jarkko Nikula2e747962008-04-25 13:55:19 +02001205 regs->srgr1 |= CLKGDV(div - 1);
1206
1207 return 0;
1208}
1209
Liam Girdwood8687eb82008-07-07 16:08:07 +01001210static int omap_mcbsp_dai_set_dai_sysclk(struct snd_soc_dai *cpu_dai,
Jarkko Nikula2e747962008-04-25 13:55:19 +02001211 int clk_id, unsigned int freq,
1212 int dir)
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 int err = 0;
1217
Peter Ujfalusi256d9c22012-02-14 15:23:15 +02001218 if (mcbsp->active) {
1219 if (freq == mcbsp->in_freq)
Jarkko Nikula34c86982011-09-23 11:19:13 +03001220 return 0;
1221 else
1222 return -EBUSY;
Peter Ujfalusi141947e2011-09-26 10:56:42 +03001223 }
Jarkko Nikula34c86982011-09-23 11:19:13 +03001224
Peter Ujfalusi8fef6262012-08-16 16:41:04 +03001225 mcbsp->in_freq = freq;
1226 regs->srgr2 &= ~CLKSM;
1227 regs->pcr0 &= ~SCLKME;
Graeme Gregory5f63ef92009-11-09 19:02:15 +00001228
Jarkko Nikula2e747962008-04-25 13:55:19 +02001229 switch (clk_id) {
1230 case OMAP_MCBSP_SYSCLK_CLK:
1231 regs->srgr2 |= CLKSM;
1232 break;
1233 case OMAP_MCBSP_SYSCLK_CLKS_FCLK:
Tony Lindgrene6507942012-11-21 09:42:25 -08001234 if (mcbsp_omap1()) {
Paul Walmsleyd13586572010-10-08 11:40:19 -06001235 err = -EINVAL;
1236 break;
1237 }
Peter Ujfalusi45656b42012-02-14 18:20:58 +02001238 err = omap2_mcbsp_set_clks_src(mcbsp,
Paul Walmsleyd13586572010-10-08 11:40:19 -06001239 MCBSP_CLKS_PRCM_SRC);
1240 break;
Jarkko Nikula2e747962008-04-25 13:55:19 +02001241 case OMAP_MCBSP_SYSCLK_CLKS_EXT:
Tony Lindgrene6507942012-11-21 09:42:25 -08001242 if (mcbsp_omap1()) {
Paul Walmsleyd13586572010-10-08 11:40:19 -06001243 err = 0;
1244 break;
1245 }
Peter Ujfalusi45656b42012-02-14 18:20:58 +02001246 err = omap2_mcbsp_set_clks_src(mcbsp,
Paul Walmsleyd13586572010-10-08 11:40:19 -06001247 MCBSP_CLKS_PAD_SRC);
Jarkko Nikula2e747962008-04-25 13:55:19 +02001248 break;
1249
1250 case OMAP_MCBSP_SYSCLK_CLKX_EXT:
1251 regs->srgr2 |= CLKSM;
Thomas Niederprüm8af4baa2015-02-21 18:11:29 +01001252 regs->pcr0 |= SCLKME;
1253 /*
1254 * If McBSP is master but yet the CLKX/CLKR pin drives the SRG,
1255 * disable output on those pins. This enables to inject the
1256 * reference clock through CLKX/CLKR. For this to work
1257 * set_dai_sysclk() _needs_ to be called after set_dai_fmt().
1258 */
1259 regs->pcr0 &= ~CLKXM;
1260 break;
Jarkko Nikula2e747962008-04-25 13:55:19 +02001261 case OMAP_MCBSP_SYSCLK_CLKR_EXT:
1262 regs->pcr0 |= SCLKME;
Thomas Niederprüm8af4baa2015-02-21 18:11:29 +01001263 /* Disable ouput on CLKR pin in master mode */
1264 regs->pcr0 &= ~CLKRM;
Jarkko Nikula2e747962008-04-25 13:55:19 +02001265 break;
1266 default:
1267 err = -ENODEV;
1268 }
1269
1270 return err;
1271}
1272
Lars-Peter Clausen85e76522011-11-23 11:40:40 +01001273static const struct snd_soc_dai_ops mcbsp_dai_ops = {
Eric Miao6335d052009-03-03 09:41:00 +08001274 .startup = omap_mcbsp_dai_startup,
1275 .shutdown = omap_mcbsp_dai_shutdown,
Matt Ranostay9834ffd2017-01-31 13:21:43 -08001276 .prepare = omap_mcbsp_dai_prepare,
Eric Miao6335d052009-03-03 09:41:00 +08001277 .trigger = omap_mcbsp_dai_trigger,
Peter Ujfalusi75581d22010-03-03 15:08:09 +02001278 .delay = omap_mcbsp_dai_delay,
Eric Miao6335d052009-03-03 09:41:00 +08001279 .hw_params = omap_mcbsp_dai_hw_params,
1280 .set_fmt = omap_mcbsp_dai_set_dai_fmt,
1281 .set_clkdiv = omap_mcbsp_dai_set_clkdiv,
1282 .set_sysclk = omap_mcbsp_dai_set_dai_sysclk,
1283};
1284
Peter Ujfalusi2ee65952012-02-14 14:52:42 +02001285static int omap_mcbsp_probe(struct snd_soc_dai *dai)
1286{
1287 struct omap_mcbsp *mcbsp = snd_soc_dai_get_drvdata(dai);
1288
1289 pm_runtime_enable(mcbsp->dev);
1290
Peter Ujfalusi3fe856b2014-04-16 15:46:15 +03001291 snd_soc_dai_init_dma_data(dai,
1292 &mcbsp->dma_data[SNDRV_PCM_STREAM_PLAYBACK],
1293 &mcbsp->dma_data[SNDRV_PCM_STREAM_CAPTURE]);
1294
Peter Ujfalusi2ee65952012-02-14 14:52:42 +02001295 return 0;
1296}
1297
1298static int omap_mcbsp_remove(struct snd_soc_dai *dai)
1299{
1300 struct omap_mcbsp *mcbsp = snd_soc_dai_get_drvdata(dai);
1301
1302 pm_runtime_disable(mcbsp->dev);
1303
1304 return 0;
1305}
1306
Michael Opdenacker6179b772011-10-10 07:07:08 +02001307static struct snd_soc_dai_driver omap_mcbsp_dai = {
Peter Ujfalusi2ee65952012-02-14 14:52:42 +02001308 .probe = omap_mcbsp_probe,
1309 .remove = omap_mcbsp_remove,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001310 .playback = {
1311 .channels_min = 1,
1312 .channels_max = 16,
1313 .rates = OMAP_MCBSP_RATES,
1314 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE,
1315 },
1316 .capture = {
1317 .channels_min = 1,
1318 .channels_max = 16,
1319 .rates = OMAP_MCBSP_RATES,
1320 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE,
1321 },
1322 .ops = &mcbsp_dai_ops,
Jarkko Nikula2e747962008-04-25 13:55:19 +02001323};
Jarkko Nikula8def4642008-10-09 15:57:22 +03001324
Kuninori Morimoto43cd8142013-03-21 03:33:25 -07001325static const struct snd_soc_component_driver omap_mcbsp_component = {
1326 .name = "omap-mcbsp",
1327};
1328
Peter Ujfalusi11dd5862012-08-16 16:41:08 +03001329static struct omap_mcbsp_platform_data omap2420_pdata = {
1330 .reg_step = 4,
1331 .reg_size = 2,
1332};
1333
1334static struct omap_mcbsp_platform_data omap2430_pdata = {
1335 .reg_step = 4,
1336 .reg_size = 4,
1337 .has_ccr = true,
1338};
1339
1340static struct omap_mcbsp_platform_data omap3_pdata = {
1341 .reg_step = 4,
1342 .reg_size = 4,
1343 .has_ccr = true,
1344 .has_wakeup = true,
1345};
1346
1347static struct omap_mcbsp_platform_data omap4_pdata = {
1348 .reg_step = 4,
1349 .reg_size = 4,
1350 .has_ccr = true,
1351 .has_wakeup = true,
1352};
1353
1354static const struct of_device_id omap_mcbsp_of_match[] = {
1355 {
1356 .compatible = "ti,omap2420-mcbsp",
1357 .data = &omap2420_pdata,
1358 },
1359 {
1360 .compatible = "ti,omap2430-mcbsp",
1361 .data = &omap2430_pdata,
1362 },
1363 {
1364 .compatible = "ti,omap3-mcbsp",
1365 .data = &omap3_pdata,
1366 },
1367 {
1368 .compatible = "ti,omap4-mcbsp",
1369 .data = &omap4_pdata,
1370 },
1371 { },
1372};
1373MODULE_DEVICE_TABLE(of, omap_mcbsp_of_match);
1374
Bill Pemberton7ff60002012-12-07 09:26:29 -05001375static int asoc_mcbsp_probe(struct platform_device *pdev)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001376{
Peter Ujfalusi2ee65952012-02-14 14:52:42 +02001377 struct omap_mcbsp_platform_data *pdata = dev_get_platdata(&pdev->dev);
1378 struct omap_mcbsp *mcbsp;
Peter Ujfalusi11dd5862012-08-16 16:41:08 +03001379 const struct of_device_id *match;
Peter Ujfalusi45656b42012-02-14 18:20:58 +02001380 int ret;
1381
Peter Ujfalusi11dd5862012-08-16 16:41:08 +03001382 match = of_match_device(omap_mcbsp_of_match, &pdev->dev);
1383 if (match) {
1384 struct device_node *node = pdev->dev.of_node;
Peter Ujfalusibbfa26c2016-05-30 11:23:49 +03001385 struct omap_mcbsp_platform_data *pdata_quirk = pdata;
Peter Ujfalusi11dd5862012-08-16 16:41:08 +03001386 int buffer_size;
1387
1388 pdata = devm_kzalloc(&pdev->dev,
1389 sizeof(struct omap_mcbsp_platform_data),
1390 GFP_KERNEL);
1391 if (!pdata)
1392 return -ENOMEM;
1393
1394 memcpy(pdata, match->data, sizeof(*pdata));
1395 if (!of_property_read_u32(node, "ti,buffer-size", &buffer_size))
1396 pdata->buffer_size = buffer_size;
Peter Ujfalusibbfa26c2016-05-30 11:23:49 +03001397 if (pdata_quirk)
1398 pdata->force_ick_on = pdata_quirk->force_ick_on;
Peter Ujfalusi11dd5862012-08-16 16:41:08 +03001399 } else if (!pdata) {
Peter Ujfalusi2ee65952012-02-14 14:52:42 +02001400 dev_err(&pdev->dev, "missing platform data.\n");
1401 return -EINVAL;
1402 }
1403 mcbsp = devm_kzalloc(&pdev->dev, sizeof(struct omap_mcbsp), GFP_KERNEL);
1404 if (!mcbsp)
1405 return -ENOMEM;
1406
1407 mcbsp->id = pdev->id;
1408 mcbsp->pdata = pdata;
1409 mcbsp->dev = &pdev->dev;
1410 platform_set_drvdata(pdev, mcbsp);
1411
1412 ret = omap_mcbsp_init(pdev);
Peter Ujfalusi64241422014-04-16 15:46:16 +03001413 if (ret)
1414 return ret;
Peter Ujfalusi45656b42012-02-14 18:20:58 +02001415
Peter Ujfalusi61f18dc2018-10-25 16:48:24 +03001416 if (mcbsp->pdata->reg_size == 2) {
1417 omap_mcbsp_dai.playback.formats = SNDRV_PCM_FMTBIT_S16_LE;
1418 omap_mcbsp_dai.capture.formats = SNDRV_PCM_FMTBIT_S16_LE;
1419 }
1420
Manish Badarkhe36765c92014-07-08 21:55:23 +05301421 ret = devm_snd_soc_register_component(&pdev->dev,
1422 &omap_mcbsp_component,
1423 &omap_mcbsp_dai, 1);
Peter Ujfalusi64241422014-04-16 15:46:16 +03001424 if (ret)
1425 return ret;
1426
Peter Ujfalusi0198d7b2018-05-07 11:49:59 +03001427 return sdma_pcm_platform_register(&pdev->dev, NULL, NULL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001428}
1429
Bill Pemberton7ff60002012-12-07 09:26:29 -05001430static int asoc_mcbsp_remove(struct platform_device *pdev)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001431{
Peter Ujfalusi2ee65952012-02-14 14:52:42 +02001432 struct omap_mcbsp *mcbsp = platform_get_drvdata(pdev);
1433
Peter Ujfalusi2ee65952012-02-14 14:52:42 +02001434 if (mcbsp->pdata->ops && mcbsp->pdata->ops->free)
1435 mcbsp->pdata->ops->free(mcbsp->id);
1436
Matt Ranostay9834ffd2017-01-31 13:21:43 -08001437 if (pm_qos_request_active(&mcbsp->pm_qos_req))
1438 pm_qos_remove_request(&mcbsp->pm_qos_req);
1439
Peter Ujfalusi9c34d022018-11-08 09:29:58 +02001440 if (mcbsp->pdata->buffer_size)
1441 sysfs_remove_group(&mcbsp->dev->kobj, &additional_attr_group);
1442
1443 omap_mcbsp_st_cleanup(pdev);
Peter Ujfalusi2ee65952012-02-14 14:52:42 +02001444
1445 clk_put(mcbsp->fclk);
1446
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001447 return 0;
1448}
1449
1450static struct platform_driver asoc_mcbsp_driver = {
1451 .driver = {
Peter Ujfalusi45656b42012-02-14 18:20:58 +02001452 .name = "omap-mcbsp",
Peter Ujfalusi11dd5862012-08-16 16:41:08 +03001453 .of_match_table = omap_mcbsp_of_match,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001454 },
1455
1456 .probe = asoc_mcbsp_probe,
Bill Pemberton7ff60002012-12-07 09:26:29 -05001457 .remove = asoc_mcbsp_remove,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001458};
1459
Axel Linbeda5bf52011-11-25 10:12:16 +08001460module_platform_driver(asoc_mcbsp_driver);
Mark Brown3f4b7832008-12-03 19:26:35 +00001461
Jarkko Nikula7ec41ee2011-08-11 15:44:57 +03001462MODULE_AUTHOR("Jarkko Nikula <jarkko.nikula@bitmer.com>");
Jarkko Nikula2e747962008-04-25 13:55:19 +02001463MODULE_DESCRIPTION("OMAP I2S SoC Interface");
1464MODULE_LICENSE("GPL");
Guillaume Gardet5e70b7fc2012-07-12 15:08:16 +02001465MODULE_ALIAS("platform:omap-mcbsp");