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