xref: /linux/sound/soc/bcm/bcm2835-i2s.c (revision a1c613ae4c322ddd58d5a8539dbfba2a0380a8c0)
11802d0beSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2c6aeb7deSFlorian Meier /*
3c6aeb7deSFlorian Meier  * ALSA SoC I2S Audio Layer for Broadcom BCM2835 SoC
4c6aeb7deSFlorian Meier  *
5c6aeb7deSFlorian Meier  * Author:	Florian Meier <florian.meier@koalo.de>
6c6aeb7deSFlorian Meier  *		Copyright 2013
7c6aeb7deSFlorian Meier  *
8c6aeb7deSFlorian Meier  * Based on
9c6aeb7deSFlorian Meier  *	Raspberry Pi PCM I2S ALSA Driver
10c6aeb7deSFlorian Meier  *	Copyright (c) by Phil Poole 2013
11c6aeb7deSFlorian Meier  *
12c6aeb7deSFlorian Meier  *	ALSA SoC I2S (McBSP) Audio Layer for TI DAVINCI processor
13c6aeb7deSFlorian Meier  *      Vladimir Barinov, <vbarinov@embeddedalley.com>
14c6aeb7deSFlorian Meier  *	Copyright (C) 2007 MontaVista Software, Inc., <source@mvista.com>
15c6aeb7deSFlorian Meier  *
16c6aeb7deSFlorian Meier  *	OMAP ALSA SoC DAI driver using McBSP port
17c6aeb7deSFlorian Meier  *	Copyright (C) 2008 Nokia Corporation
18c6aeb7deSFlorian Meier  *	Contact: Jarkko Nikula <jarkko.nikula@bitmer.com>
19c6aeb7deSFlorian Meier  *		 Peter Ujfalusi <peter.ujfalusi@ti.com>
20c6aeb7deSFlorian Meier  *
21c6aeb7deSFlorian Meier  *	Freescale SSI ALSA SoC Digital Audio Interface (DAI) driver
22c6aeb7deSFlorian Meier  *	Author: Timur Tabi <timur@freescale.com>
23c6aeb7deSFlorian Meier  *	Copyright 2007-2010 Freescale Semiconductor, Inc.
24c6aeb7deSFlorian Meier  */
25c6aeb7deSFlorian Meier 
269448572dSMatthias Reichl #include <linux/bitops.h>
27c6aeb7deSFlorian Meier #include <linux/clk.h>
287905f082SMartin Sperl #include <linux/delay.h>
297905f082SMartin Sperl #include <linux/device.h>
307905f082SMartin Sperl #include <linux/init.h>
317905f082SMartin Sperl #include <linux/io.h>
327905f082SMartin Sperl #include <linux/module.h>
33517e7a15SMartin Sperl #include <linux/of_address.h>
347905f082SMartin Sperl #include <linux/slab.h>
35c6aeb7deSFlorian Meier 
36c6aeb7deSFlorian Meier #include <sound/core.h>
377905f082SMartin Sperl #include <sound/dmaengine_pcm.h>
387905f082SMartin Sperl #include <sound/initval.h>
39c6aeb7deSFlorian Meier #include <sound/pcm.h>
40c6aeb7deSFlorian Meier #include <sound/pcm_params.h>
41c6aeb7deSFlorian Meier #include <sound/soc.h>
42c6aeb7deSFlorian Meier 
43c6aeb7deSFlorian Meier /* I2S registers */
44c6aeb7deSFlorian Meier #define BCM2835_I2S_CS_A_REG		0x00
45c6aeb7deSFlorian Meier #define BCM2835_I2S_FIFO_A_REG		0x04
46c6aeb7deSFlorian Meier #define BCM2835_I2S_MODE_A_REG		0x08
47c6aeb7deSFlorian Meier #define BCM2835_I2S_RXC_A_REG		0x0c
48c6aeb7deSFlorian Meier #define BCM2835_I2S_TXC_A_REG		0x10
49c6aeb7deSFlorian Meier #define BCM2835_I2S_DREQ_A_REG		0x14
50c6aeb7deSFlorian Meier #define BCM2835_I2S_INTEN_A_REG	0x18
51c6aeb7deSFlorian Meier #define BCM2835_I2S_INTSTC_A_REG	0x1c
52c6aeb7deSFlorian Meier #define BCM2835_I2S_GRAY_REG		0x20
53c6aeb7deSFlorian Meier 
54c6aeb7deSFlorian Meier /* I2S register settings */
55c6aeb7deSFlorian Meier #define BCM2835_I2S_STBY		BIT(25)
56c6aeb7deSFlorian Meier #define BCM2835_I2S_SYNC		BIT(24)
57c6aeb7deSFlorian Meier #define BCM2835_I2S_RXSEX		BIT(23)
58c6aeb7deSFlorian Meier #define BCM2835_I2S_RXF		BIT(22)
59c6aeb7deSFlorian Meier #define BCM2835_I2S_TXE		BIT(21)
60c6aeb7deSFlorian Meier #define BCM2835_I2S_RXD		BIT(20)
61c6aeb7deSFlorian Meier #define BCM2835_I2S_TXD		BIT(19)
62c6aeb7deSFlorian Meier #define BCM2835_I2S_RXR		BIT(18)
63c6aeb7deSFlorian Meier #define BCM2835_I2S_TXW		BIT(17)
64c6aeb7deSFlorian Meier #define BCM2835_I2S_CS_RXERR		BIT(16)
65c6aeb7deSFlorian Meier #define BCM2835_I2S_CS_TXERR		BIT(15)
66c6aeb7deSFlorian Meier #define BCM2835_I2S_RXSYNC		BIT(14)
67c6aeb7deSFlorian Meier #define BCM2835_I2S_TXSYNC		BIT(13)
68c6aeb7deSFlorian Meier #define BCM2835_I2S_DMAEN		BIT(9)
69c6aeb7deSFlorian Meier #define BCM2835_I2S_RXTHR(v)		((v) << 7)
70c6aeb7deSFlorian Meier #define BCM2835_I2S_TXTHR(v)		((v) << 5)
71c6aeb7deSFlorian Meier #define BCM2835_I2S_RXCLR		BIT(4)
72c6aeb7deSFlorian Meier #define BCM2835_I2S_TXCLR		BIT(3)
73c6aeb7deSFlorian Meier #define BCM2835_I2S_TXON		BIT(2)
74c6aeb7deSFlorian Meier #define BCM2835_I2S_RXON		BIT(1)
75c6aeb7deSFlorian Meier #define BCM2835_I2S_EN			(1)
76c6aeb7deSFlorian Meier 
77c6aeb7deSFlorian Meier #define BCM2835_I2S_CLKDIS		BIT(28)
78c6aeb7deSFlorian Meier #define BCM2835_I2S_PDMN		BIT(27)
79c6aeb7deSFlorian Meier #define BCM2835_I2S_PDME		BIT(26)
80c6aeb7deSFlorian Meier #define BCM2835_I2S_FRXP		BIT(25)
81c6aeb7deSFlorian Meier #define BCM2835_I2S_FTXP		BIT(24)
82c6aeb7deSFlorian Meier #define BCM2835_I2S_CLKM		BIT(23)
83c6aeb7deSFlorian Meier #define BCM2835_I2S_CLKI		BIT(22)
84c6aeb7deSFlorian Meier #define BCM2835_I2S_FSM		BIT(21)
85c6aeb7deSFlorian Meier #define BCM2835_I2S_FSI		BIT(20)
86c6aeb7deSFlorian Meier #define BCM2835_I2S_FLEN(v)		((v) << 10)
87c6aeb7deSFlorian Meier #define BCM2835_I2S_FSLEN(v)		(v)
88c6aeb7deSFlorian Meier 
89c6aeb7deSFlorian Meier #define BCM2835_I2S_CHWEX		BIT(15)
90c6aeb7deSFlorian Meier #define BCM2835_I2S_CHEN		BIT(14)
91c6aeb7deSFlorian Meier #define BCM2835_I2S_CHPOS(v)		((v) << 4)
92c6aeb7deSFlorian Meier #define BCM2835_I2S_CHWID(v)		(v)
93c6aeb7deSFlorian Meier #define BCM2835_I2S_CH1(v)		((v) << 16)
94c6aeb7deSFlorian Meier #define BCM2835_I2S_CH2(v)		(v)
959448572dSMatthias Reichl #define BCM2835_I2S_CH1_POS(v)		BCM2835_I2S_CH1(BCM2835_I2S_CHPOS(v))
969448572dSMatthias Reichl #define BCM2835_I2S_CH2_POS(v)		BCM2835_I2S_CH2(BCM2835_I2S_CHPOS(v))
97c6aeb7deSFlorian Meier 
98c6aeb7deSFlorian Meier #define BCM2835_I2S_TX_PANIC(v)	((v) << 24)
99c6aeb7deSFlorian Meier #define BCM2835_I2S_RX_PANIC(v)	((v) << 16)
100c6aeb7deSFlorian Meier #define BCM2835_I2S_TX(v)		((v) << 8)
101c6aeb7deSFlorian Meier #define BCM2835_I2S_RX(v)		(v)
102c6aeb7deSFlorian Meier 
103c6aeb7deSFlorian Meier #define BCM2835_I2S_INT_RXERR		BIT(3)
104c6aeb7deSFlorian Meier #define BCM2835_I2S_INT_TXERR		BIT(2)
105c6aeb7deSFlorian Meier #define BCM2835_I2S_INT_RXR		BIT(1)
106c6aeb7deSFlorian Meier #define BCM2835_I2S_INT_TXW		BIT(0)
107c6aeb7deSFlorian Meier 
1089448572dSMatthias Reichl /* Frame length register is 10 bit, maximum length 1024 */
1099448572dSMatthias Reichl #define BCM2835_I2S_MAX_FRAME_LENGTH	1024
1109448572dSMatthias Reichl 
111c6aeb7deSFlorian Meier /* General device struct */
112c6aeb7deSFlorian Meier struct bcm2835_i2s_dev {
113c6aeb7deSFlorian Meier 	struct device				*dev;
114c6aeb7deSFlorian Meier 	struct snd_dmaengine_dai_dma_data	dma_data[2];
115c6aeb7deSFlorian Meier 	unsigned int				fmt;
1169448572dSMatthias Reichl 	unsigned int				tdm_slots;
1179448572dSMatthias Reichl 	unsigned int				rx_mask;
1189448572dSMatthias Reichl 	unsigned int				tx_mask;
1199448572dSMatthias Reichl 	unsigned int				slot_width;
1209448572dSMatthias Reichl 	unsigned int				frame_length;
121c6aeb7deSFlorian Meier 
122c6aeb7deSFlorian Meier 	struct regmap				*i2s_regmap;
123517e7a15SMartin Sperl 	struct clk				*clk;
124517e7a15SMartin Sperl 	bool					clk_prepared;
1258d5737a5SMatthias Reichl 	int					clk_rate;
126c6aeb7deSFlorian Meier };
127c6aeb7deSFlorian Meier 
bcm2835_i2s_start_clock(struct bcm2835_i2s_dev * dev)128c6aeb7deSFlorian Meier static void bcm2835_i2s_start_clock(struct bcm2835_i2s_dev *dev)
129c6aeb7deSFlorian Meier {
130a91b0e5bSMark Brown 	unsigned int provider = dev->fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK;
131c6aeb7deSFlorian Meier 
132517e7a15SMartin Sperl 	if (dev->clk_prepared)
133517e7a15SMartin Sperl 		return;
134517e7a15SMartin Sperl 
135a91b0e5bSMark Brown 	switch (provider) {
13604ea2404SCharles Keepax 	case SND_SOC_DAIFMT_BP_FP:
13704ea2404SCharles Keepax 	case SND_SOC_DAIFMT_BP_FC:
138517e7a15SMartin Sperl 		clk_prepare_enable(dev->clk);
139517e7a15SMartin Sperl 		dev->clk_prepared = true;
140c6aeb7deSFlorian Meier 		break;
141c6aeb7deSFlorian Meier 	default:
142c6aeb7deSFlorian Meier 		break;
143c6aeb7deSFlorian Meier 	}
144c6aeb7deSFlorian Meier }
145c6aeb7deSFlorian Meier 
bcm2835_i2s_stop_clock(struct bcm2835_i2s_dev * dev)146c6aeb7deSFlorian Meier static void bcm2835_i2s_stop_clock(struct bcm2835_i2s_dev *dev)
147c6aeb7deSFlorian Meier {
148517e7a15SMartin Sperl 	if (dev->clk_prepared)
149517e7a15SMartin Sperl 		clk_disable_unprepare(dev->clk);
150517e7a15SMartin Sperl 	dev->clk_prepared = false;
151c6aeb7deSFlorian Meier }
152c6aeb7deSFlorian Meier 
bcm2835_i2s_clear_fifos(struct bcm2835_i2s_dev * dev,bool tx,bool rx)153c6aeb7deSFlorian Meier static void bcm2835_i2s_clear_fifos(struct bcm2835_i2s_dev *dev,
154c6aeb7deSFlorian Meier 				    bool tx, bool rx)
155c6aeb7deSFlorian Meier {
156c6aeb7deSFlorian Meier 	int timeout = 1000;
157c6aeb7deSFlorian Meier 	uint32_t syncval;
158c6aeb7deSFlorian Meier 	uint32_t csreg;
159c6aeb7deSFlorian Meier 	uint32_t i2s_active_state;
160517e7a15SMartin Sperl 	bool clk_was_prepared;
161c6aeb7deSFlorian Meier 	uint32_t off;
162c6aeb7deSFlorian Meier 	uint32_t clr;
163c6aeb7deSFlorian Meier 
164c6aeb7deSFlorian Meier 	off =  tx ? BCM2835_I2S_TXON : 0;
165c6aeb7deSFlorian Meier 	off |= rx ? BCM2835_I2S_RXON : 0;
166c6aeb7deSFlorian Meier 
167c6aeb7deSFlorian Meier 	clr =  tx ? BCM2835_I2S_TXCLR : 0;
168c6aeb7deSFlorian Meier 	clr |= rx ? BCM2835_I2S_RXCLR : 0;
169c6aeb7deSFlorian Meier 
170c6aeb7deSFlorian Meier 	/* Backup the current state */
171c6aeb7deSFlorian Meier 	regmap_read(dev->i2s_regmap, BCM2835_I2S_CS_A_REG, &csreg);
172c6aeb7deSFlorian Meier 	i2s_active_state = csreg & (BCM2835_I2S_RXON | BCM2835_I2S_TXON);
173c6aeb7deSFlorian Meier 
174c6aeb7deSFlorian Meier 	/* Start clock if not running */
175517e7a15SMartin Sperl 	clk_was_prepared = dev->clk_prepared;
176517e7a15SMartin Sperl 	if (!clk_was_prepared)
177517e7a15SMartin Sperl 		bcm2835_i2s_start_clock(dev);
178c6aeb7deSFlorian Meier 
179c6aeb7deSFlorian Meier 	/* Stop I2S module */
180c6aeb7deSFlorian Meier 	regmap_update_bits(dev->i2s_regmap, BCM2835_I2S_CS_A_REG, off, 0);
181c6aeb7deSFlorian Meier 
182c6aeb7deSFlorian Meier 	/*
183c6aeb7deSFlorian Meier 	 * Clear the FIFOs
184c6aeb7deSFlorian Meier 	 * Requires at least 2 PCM clock cycles to take effect
185c6aeb7deSFlorian Meier 	 */
186c6aeb7deSFlorian Meier 	regmap_update_bits(dev->i2s_regmap, BCM2835_I2S_CS_A_REG, clr, clr);
187c6aeb7deSFlorian Meier 
188c6aeb7deSFlorian Meier 	/* Wait for 2 PCM clock cycles */
189c6aeb7deSFlorian Meier 
190c6aeb7deSFlorian Meier 	/*
191c6aeb7deSFlorian Meier 	 * Toggle the SYNC flag. After 2 PCM clock cycles it can be read back
192c6aeb7deSFlorian Meier 	 * FIXME: This does not seem to work for slave mode!
193c6aeb7deSFlorian Meier 	 */
194c6aeb7deSFlorian Meier 	regmap_read(dev->i2s_regmap, BCM2835_I2S_CS_A_REG, &syncval);
195c6aeb7deSFlorian Meier 	syncval &= BCM2835_I2S_SYNC;
196c6aeb7deSFlorian Meier 
197c6aeb7deSFlorian Meier 	regmap_update_bits(dev->i2s_regmap, BCM2835_I2S_CS_A_REG,
198c6aeb7deSFlorian Meier 			BCM2835_I2S_SYNC, ~syncval);
199c6aeb7deSFlorian Meier 
200c6aeb7deSFlorian Meier 	/* Wait for the SYNC flag changing it's state */
201c6aeb7deSFlorian Meier 	while (--timeout) {
202c6aeb7deSFlorian Meier 		regmap_read(dev->i2s_regmap, BCM2835_I2S_CS_A_REG, &csreg);
203c6aeb7deSFlorian Meier 		if ((csreg & BCM2835_I2S_SYNC) != syncval)
204c6aeb7deSFlorian Meier 			break;
205c6aeb7deSFlorian Meier 	}
206c6aeb7deSFlorian Meier 
207c6aeb7deSFlorian Meier 	if (!timeout)
208c6aeb7deSFlorian Meier 		dev_err(dev->dev, "I2S SYNC error!\n");
209c6aeb7deSFlorian Meier 
210c6aeb7deSFlorian Meier 	/* Stop clock if it was not running before */
211517e7a15SMartin Sperl 	if (!clk_was_prepared)
212c6aeb7deSFlorian Meier 		bcm2835_i2s_stop_clock(dev);
213c6aeb7deSFlorian Meier 
214c6aeb7deSFlorian Meier 	/* Restore I2S state */
215c6aeb7deSFlorian Meier 	regmap_update_bits(dev->i2s_regmap, BCM2835_I2S_CS_A_REG,
216c6aeb7deSFlorian Meier 			BCM2835_I2S_RXON | BCM2835_I2S_TXON, i2s_active_state);
217c6aeb7deSFlorian Meier }
218c6aeb7deSFlorian Meier 
bcm2835_i2s_set_dai_fmt(struct snd_soc_dai * dai,unsigned int fmt)219c6aeb7deSFlorian Meier static int bcm2835_i2s_set_dai_fmt(struct snd_soc_dai *dai,
220c6aeb7deSFlorian Meier 				      unsigned int fmt)
221c6aeb7deSFlorian Meier {
222c6aeb7deSFlorian Meier 	struct bcm2835_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
223c6aeb7deSFlorian Meier 	dev->fmt = fmt;
224c6aeb7deSFlorian Meier 	return 0;
225c6aeb7deSFlorian Meier }
226c6aeb7deSFlorian Meier 
bcm2835_i2s_set_dai_bclk_ratio(struct snd_soc_dai * dai,unsigned int ratio)227c6aeb7deSFlorian Meier static int bcm2835_i2s_set_dai_bclk_ratio(struct snd_soc_dai *dai,
228c6aeb7deSFlorian Meier 				      unsigned int ratio)
229c6aeb7deSFlorian Meier {
230c6aeb7deSFlorian Meier 	struct bcm2835_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
2319448572dSMatthias Reichl 
2329448572dSMatthias Reichl 	if (!ratio) {
2339448572dSMatthias Reichl 		dev->tdm_slots = 0;
234c6aeb7deSFlorian Meier 		return 0;
235c6aeb7deSFlorian Meier 	}
236c6aeb7deSFlorian Meier 
2379448572dSMatthias Reichl 	if (ratio > BCM2835_I2S_MAX_FRAME_LENGTH)
2389448572dSMatthias Reichl 		return -EINVAL;
2399448572dSMatthias Reichl 
2409448572dSMatthias Reichl 	dev->tdm_slots = 2;
2419448572dSMatthias Reichl 	dev->rx_mask = 0x03;
2429448572dSMatthias Reichl 	dev->tx_mask = 0x03;
2439448572dSMatthias Reichl 	dev->slot_width = ratio / 2;
2449448572dSMatthias Reichl 	dev->frame_length = ratio;
2459448572dSMatthias Reichl 
2469448572dSMatthias Reichl 	return 0;
2479448572dSMatthias Reichl }
2489448572dSMatthias Reichl 
bcm2835_i2s_set_dai_tdm_slot(struct snd_soc_dai * dai,unsigned int tx_mask,unsigned int rx_mask,int slots,int width)2499448572dSMatthias Reichl static int bcm2835_i2s_set_dai_tdm_slot(struct snd_soc_dai *dai,
2509448572dSMatthias Reichl 	unsigned int tx_mask, unsigned int rx_mask,
2519448572dSMatthias Reichl 	int slots, int width)
2529448572dSMatthias Reichl {
2539448572dSMatthias Reichl 	struct bcm2835_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
2549448572dSMatthias Reichl 
2559448572dSMatthias Reichl 	if (slots) {
2569448572dSMatthias Reichl 		if (slots < 0 || width < 0)
2579448572dSMatthias Reichl 			return -EINVAL;
2589448572dSMatthias Reichl 
2599448572dSMatthias Reichl 		/* Limit masks to available slots */
2609448572dSMatthias Reichl 		rx_mask &= GENMASK(slots - 1, 0);
2619448572dSMatthias Reichl 		tx_mask &= GENMASK(slots - 1, 0);
2629448572dSMatthias Reichl 
2639448572dSMatthias Reichl 		/*
2649448572dSMatthias Reichl 		 * The driver is limited to 2-channel setups.
2659448572dSMatthias Reichl 		 * Check that exactly 2 bits are set in the masks.
2669448572dSMatthias Reichl 		 */
2679448572dSMatthias Reichl 		if (hweight_long((unsigned long) rx_mask) != 2
2689448572dSMatthias Reichl 		    || hweight_long((unsigned long) tx_mask) != 2)
2699448572dSMatthias Reichl 			return -EINVAL;
2709448572dSMatthias Reichl 
2719448572dSMatthias Reichl 		if (slots * width > BCM2835_I2S_MAX_FRAME_LENGTH)
2729448572dSMatthias Reichl 			return -EINVAL;
2739448572dSMatthias Reichl 	}
2749448572dSMatthias Reichl 
2759448572dSMatthias Reichl 	dev->tdm_slots = slots;
2769448572dSMatthias Reichl 
2779448572dSMatthias Reichl 	dev->rx_mask = rx_mask;
2789448572dSMatthias Reichl 	dev->tx_mask = tx_mask;
2799448572dSMatthias Reichl 	dev->slot_width = width;
2809448572dSMatthias Reichl 	dev->frame_length = slots * width;
2819448572dSMatthias Reichl 
2829448572dSMatthias Reichl 	return 0;
2839448572dSMatthias Reichl }
2849448572dSMatthias Reichl 
2859448572dSMatthias Reichl /*
2869448572dSMatthias Reichl  * Convert logical slot number into physical slot number.
2879448572dSMatthias Reichl  *
2889448572dSMatthias Reichl  * If odd_offset is 0 sequential number is identical to logical number.
2899448572dSMatthias Reichl  * This is used for DSP modes with slot numbering 0 1 2 3 ...
2909448572dSMatthias Reichl  *
2919448572dSMatthias Reichl  * Otherwise odd_offset defines the physical offset for odd numbered
2929448572dSMatthias Reichl  * slots. This is used for I2S and left/right justified modes to
2939448572dSMatthias Reichl  * translate from logical slot numbers 0 1 2 3 ... into physical slot
2949448572dSMatthias Reichl  * numbers 0 2 ... 3 4 ...
2959448572dSMatthias Reichl  */
bcm2835_i2s_convert_slot(unsigned int slot,unsigned int odd_offset)2969448572dSMatthias Reichl static int bcm2835_i2s_convert_slot(unsigned int slot, unsigned int odd_offset)
2979448572dSMatthias Reichl {
2989448572dSMatthias Reichl 	if (!odd_offset)
2999448572dSMatthias Reichl 		return slot;
3009448572dSMatthias Reichl 
3019448572dSMatthias Reichl 	if (slot & 1)
3029448572dSMatthias Reichl 		return (slot >> 1) + odd_offset;
3039448572dSMatthias Reichl 
3049448572dSMatthias Reichl 	return slot >> 1;
3059448572dSMatthias Reichl }
3069448572dSMatthias Reichl 
3079448572dSMatthias Reichl /*
3089448572dSMatthias Reichl  * Calculate channel position from mask and slot width.
3099448572dSMatthias Reichl  *
3109448572dSMatthias Reichl  * Mask must contain exactly 2 set bits.
3119448572dSMatthias Reichl  * Lowest set bit is channel 1 position, highest set bit channel 2.
3129448572dSMatthias Reichl  * The constant offset is added to both channel positions.
3139448572dSMatthias Reichl  *
3149448572dSMatthias Reichl  * If odd_offset is > 0 slot positions are translated to
3159448572dSMatthias Reichl  * I2S-style TDM slot numbering ( 0 2 ... 3 4 ...) with odd
3169448572dSMatthias Reichl  * logical slot numbers starting at physical slot odd_offset.
3179448572dSMatthias Reichl  */
bcm2835_i2s_calc_channel_pos(unsigned int * ch1_pos,unsigned int * ch2_pos,unsigned int mask,unsigned int width,unsigned int bit_offset,unsigned int odd_offset)3189448572dSMatthias Reichl static void bcm2835_i2s_calc_channel_pos(
3199448572dSMatthias Reichl 	unsigned int *ch1_pos, unsigned int *ch2_pos,
3209448572dSMatthias Reichl 	unsigned int mask, unsigned int width,
3219448572dSMatthias Reichl 	unsigned int bit_offset, unsigned int odd_offset)
3229448572dSMatthias Reichl {
3239448572dSMatthias Reichl 	*ch1_pos = bcm2835_i2s_convert_slot((ffs(mask) - 1), odd_offset)
3249448572dSMatthias Reichl 			* width + bit_offset;
3259448572dSMatthias Reichl 	*ch2_pos = bcm2835_i2s_convert_slot((fls(mask) - 1), odd_offset)
3269448572dSMatthias Reichl 			* width + bit_offset;
3279448572dSMatthias Reichl }
3289448572dSMatthias Reichl 
bcm2835_i2s_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params,struct snd_soc_dai * dai)329c6aeb7deSFlorian Meier static int bcm2835_i2s_hw_params(struct snd_pcm_substream *substream,
330c6aeb7deSFlorian Meier 				 struct snd_pcm_hw_params *params,
331c6aeb7deSFlorian Meier 				 struct snd_soc_dai *dai)
332c6aeb7deSFlorian Meier {
333c6aeb7deSFlorian Meier 	struct bcm2835_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
3349448572dSMatthias Reichl 	unsigned int data_length, data_delay, framesync_length;
3359448572dSMatthias Reichl 	unsigned int slots, slot_width, odd_slot_offset;
3369448572dSMatthias Reichl 	int frame_length, bclk_rate;
3379448572dSMatthias Reichl 	unsigned int rx_mask, tx_mask;
3389448572dSMatthias Reichl 	unsigned int rx_ch1_pos, rx_ch2_pos, tx_ch1_pos, tx_ch2_pos;
3399448572dSMatthias Reichl 	unsigned int mode, format;
340a91b0e5bSMark Brown 	bool bit_clock_provider = false;
341a91b0e5bSMark Brown 	bool frame_sync_provider = false;
342abd4f0e1SMatthias Reichl 	bool frame_start_falling_edge = false;
343c6aeb7deSFlorian Meier 	uint32_t csreg;
3449448572dSMatthias Reichl 	int ret = 0;
345c6aeb7deSFlorian Meier 
346c6aeb7deSFlorian Meier 	/*
347c6aeb7deSFlorian Meier 	 * If a stream is already enabled,
348c6aeb7deSFlorian Meier 	 * the registers are already set properly.
349c6aeb7deSFlorian Meier 	 */
350c6aeb7deSFlorian Meier 	regmap_read(dev->i2s_regmap, BCM2835_I2S_CS_A_REG, &csreg);
351c6aeb7deSFlorian Meier 
352c6aeb7deSFlorian Meier 	if (csreg & (BCM2835_I2S_TXON | BCM2835_I2S_RXON))
353c6aeb7deSFlorian Meier 		return 0;
354c6aeb7deSFlorian Meier 
3559448572dSMatthias Reichl 	data_length = params_width(params);
3569448572dSMatthias Reichl 	data_delay = 0;
3579448572dSMatthias Reichl 	odd_slot_offset = 0;
3589448572dSMatthias Reichl 	mode = 0;
3599448572dSMatthias Reichl 
3609448572dSMatthias Reichl 	if (dev->tdm_slots) {
3619448572dSMatthias Reichl 		slots = dev->tdm_slots;
3629448572dSMatthias Reichl 		slot_width = dev->slot_width;
3639448572dSMatthias Reichl 		frame_length = dev->frame_length;
3649448572dSMatthias Reichl 		rx_mask = dev->rx_mask;
3659448572dSMatthias Reichl 		tx_mask = dev->tx_mask;
3669448572dSMatthias Reichl 		bclk_rate = dev->frame_length * params_rate(params);
3679448572dSMatthias Reichl 	} else {
3689448572dSMatthias Reichl 		slots = 2;
3699448572dSMatthias Reichl 		slot_width = params_width(params);
3709448572dSMatthias Reichl 		rx_mask = 0x03;
3719448572dSMatthias Reichl 		tx_mask = 0x03;
3729448572dSMatthias Reichl 
3739448572dSMatthias Reichl 		frame_length = snd_soc_params_to_frame_size(params);
3749448572dSMatthias Reichl 		if (frame_length < 0)
3759448572dSMatthias Reichl 			return frame_length;
3769448572dSMatthias Reichl 
3779448572dSMatthias Reichl 		bclk_rate = snd_soc_params_to_bclk(params);
3789448572dSMatthias Reichl 		if (bclk_rate < 0)
3799448572dSMatthias Reichl 			return bclk_rate;
380c6aeb7deSFlorian Meier 	}
381c6aeb7deSFlorian Meier 
3829448572dSMatthias Reichl 	/* Check if data fits into slots */
3839448572dSMatthias Reichl 	if (data_length > slot_width)
3849448572dSMatthias Reichl 		return -EINVAL;
385c6aeb7deSFlorian Meier 
386a91b0e5bSMark Brown 	/* Check if CPU is bit clock provider */
387a91b0e5bSMark Brown 	switch (dev->fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
38804ea2404SCharles Keepax 	case SND_SOC_DAIFMT_BP_FP:
38904ea2404SCharles Keepax 	case SND_SOC_DAIFMT_BP_FC:
390a91b0e5bSMark Brown 		bit_clock_provider = true;
391abd4f0e1SMatthias Reichl 		break;
39204ea2404SCharles Keepax 	case SND_SOC_DAIFMT_BC_FP:
39304ea2404SCharles Keepax 	case SND_SOC_DAIFMT_BC_FC:
394a91b0e5bSMark Brown 		bit_clock_provider = false;
395abd4f0e1SMatthias Reichl 		break;
396abd4f0e1SMatthias Reichl 	default:
397abd4f0e1SMatthias Reichl 		return -EINVAL;
398abd4f0e1SMatthias Reichl 	}
399abd4f0e1SMatthias Reichl 
400a91b0e5bSMark Brown 	/* Check if CPU is frame sync provider */
401a91b0e5bSMark Brown 	switch (dev->fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
40204ea2404SCharles Keepax 	case SND_SOC_DAIFMT_BP_FP:
40304ea2404SCharles Keepax 	case SND_SOC_DAIFMT_BC_FP:
404a91b0e5bSMark Brown 		frame_sync_provider = true;
405abd4f0e1SMatthias Reichl 		break;
40604ea2404SCharles Keepax 	case SND_SOC_DAIFMT_BP_FC:
40704ea2404SCharles Keepax 	case SND_SOC_DAIFMT_BC_FC:
408a91b0e5bSMark Brown 		frame_sync_provider = false;
409abd4f0e1SMatthias Reichl 		break;
410abd4f0e1SMatthias Reichl 	default:
411abd4f0e1SMatthias Reichl 		return -EINVAL;
412abd4f0e1SMatthias Reichl 	}
413abd4f0e1SMatthias Reichl 
414abd4f0e1SMatthias Reichl 	/* Clock should only be set up here if CPU is clock master */
415a91b0e5bSMark Brown 	if (bit_clock_provider &&
4168d5737a5SMatthias Reichl 	    (!dev->clk_prepared || dev->clk_rate != bclk_rate)) {
4178d5737a5SMatthias Reichl 		if (dev->clk_prepared)
4188d5737a5SMatthias Reichl 			bcm2835_i2s_stop_clock(dev);
4198d5737a5SMatthias Reichl 
4208d5737a5SMatthias Reichl 		if (dev->clk_rate != bclk_rate) {
4219448572dSMatthias Reichl 			ret = clk_set_rate(dev->clk, bclk_rate);
4229448572dSMatthias Reichl 			if (ret)
4239448572dSMatthias Reichl 				return ret;
4248d5737a5SMatthias Reichl 			dev->clk_rate = bclk_rate;
4258d5737a5SMatthias Reichl 		}
4268d5737a5SMatthias Reichl 
4278d5737a5SMatthias Reichl 		bcm2835_i2s_start_clock(dev);
42860507fe1SMatthias Reichl 	}
429c6aeb7deSFlorian Meier 
430c6aeb7deSFlorian Meier 	/* Setup the frame format */
431c6aeb7deSFlorian Meier 	format = BCM2835_I2S_CHEN;
432c6aeb7deSFlorian Meier 
433a34b027dSMatthias Reichl 	if (data_length >= 24)
434c6aeb7deSFlorian Meier 		format |= BCM2835_I2S_CHWEX;
435c6aeb7deSFlorian Meier 
436c6aeb7deSFlorian Meier 	format |= BCM2835_I2S_CHWID((data_length-8)&0xf);
437c6aeb7deSFlorian Meier 
4389448572dSMatthias Reichl 	/* CH2 format is the same as for CH1 */
4399448572dSMatthias Reichl 	format = BCM2835_I2S_CH1(format) | BCM2835_I2S_CH2(format);
4409448572dSMatthias Reichl 
441c6aeb7deSFlorian Meier 	switch (dev->fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
442c6aeb7deSFlorian Meier 	case SND_SOC_DAIFMT_I2S:
4439448572dSMatthias Reichl 		/* I2S mode needs an even number of slots */
4449448572dSMatthias Reichl 		if (slots & 1)
4459448572dSMatthias Reichl 			return -EINVAL;
4469448572dSMatthias Reichl 
4479448572dSMatthias Reichl 		/*
4489448572dSMatthias Reichl 		 * Use I2S-style logical slot numbering: even slots
4499448572dSMatthias Reichl 		 * are in first half of frame, odd slots in second half.
4509448572dSMatthias Reichl 		 */
4519448572dSMatthias Reichl 		odd_slot_offset = slots >> 1;
4529448572dSMatthias Reichl 
4539448572dSMatthias Reichl 		/* MSB starts one cycle after frame start */
454c6aeb7deSFlorian Meier 		data_delay = 1;
4559448572dSMatthias Reichl 
4569448572dSMatthias Reichl 		/* Setup frame sync signal for 50% duty cycle */
4579448572dSMatthias Reichl 		framesync_length = frame_length / 2;
458abd4f0e1SMatthias Reichl 		frame_start_falling_edge = true;
459abd4f0e1SMatthias Reichl 		break;
460abd4f0e1SMatthias Reichl 	case SND_SOC_DAIFMT_LEFT_J:
461abd4f0e1SMatthias Reichl 		if (slots & 1)
462abd4f0e1SMatthias Reichl 			return -EINVAL;
463abd4f0e1SMatthias Reichl 
464abd4f0e1SMatthias Reichl 		odd_slot_offset = slots >> 1;
465abd4f0e1SMatthias Reichl 		data_delay = 0;
466abd4f0e1SMatthias Reichl 		framesync_length = frame_length / 2;
467abd4f0e1SMatthias Reichl 		frame_start_falling_edge = false;
468abd4f0e1SMatthias Reichl 		break;
469abd4f0e1SMatthias Reichl 	case SND_SOC_DAIFMT_RIGHT_J:
470abd4f0e1SMatthias Reichl 		if (slots & 1)
471abd4f0e1SMatthias Reichl 			return -EINVAL;
472abd4f0e1SMatthias Reichl 
473abd4f0e1SMatthias Reichl 		/* Odd frame lengths aren't supported */
474abd4f0e1SMatthias Reichl 		if (frame_length & 1)
475abd4f0e1SMatthias Reichl 			return -EINVAL;
476abd4f0e1SMatthias Reichl 
477abd4f0e1SMatthias Reichl 		odd_slot_offset = slots >> 1;
478abd4f0e1SMatthias Reichl 		data_delay = slot_width - data_length;
479abd4f0e1SMatthias Reichl 		framesync_length = frame_length / 2;
480abd4f0e1SMatthias Reichl 		frame_start_falling_edge = false;
481abd4f0e1SMatthias Reichl 		break;
482abd4f0e1SMatthias Reichl 	case SND_SOC_DAIFMT_DSP_A:
483abd4f0e1SMatthias Reichl 		data_delay = 1;
484abd4f0e1SMatthias Reichl 		framesync_length = 1;
485abd4f0e1SMatthias Reichl 		frame_start_falling_edge = false;
486abd4f0e1SMatthias Reichl 		break;
487abd4f0e1SMatthias Reichl 	case SND_SOC_DAIFMT_DSP_B:
488abd4f0e1SMatthias Reichl 		data_delay = 0;
489abd4f0e1SMatthias Reichl 		framesync_length = 1;
490abd4f0e1SMatthias Reichl 		frame_start_falling_edge = false;
491c6aeb7deSFlorian Meier 		break;
492c6aeb7deSFlorian Meier 	default:
493c6aeb7deSFlorian Meier 		return -EINVAL;
494c6aeb7deSFlorian Meier 	}
495c6aeb7deSFlorian Meier 
4969448572dSMatthias Reichl 	bcm2835_i2s_calc_channel_pos(&rx_ch1_pos, &rx_ch2_pos,
4979448572dSMatthias Reichl 		rx_mask, slot_width, data_delay, odd_slot_offset);
4989448572dSMatthias Reichl 	bcm2835_i2s_calc_channel_pos(&tx_ch1_pos, &tx_ch2_pos,
4999448572dSMatthias Reichl 		tx_mask, slot_width, data_delay, odd_slot_offset);
500c6aeb7deSFlorian Meier 
501c6aeb7deSFlorian Meier 	/*
502abd4f0e1SMatthias Reichl 	 * Transmitting data immediately after frame start, eg
503abd4f0e1SMatthias Reichl 	 * in left-justified or DSP mode A, only works stable
504a91b0e5bSMark Brown 	 * if bcm2835 is the frame clock provider.
505abd4f0e1SMatthias Reichl 	 */
506a91b0e5bSMark Brown 	if ((!rx_ch1_pos || !tx_ch1_pos) && !frame_sync_provider)
507abd4f0e1SMatthias Reichl 		dev_warn(dev->dev,
508a91b0e5bSMark Brown 			"Unstable consumer config detected, L/R may be swapped");
509abd4f0e1SMatthias Reichl 
510abd4f0e1SMatthias Reichl 	/*
511c6aeb7deSFlorian Meier 	 * Set format for both streams.
512c6aeb7deSFlorian Meier 	 * We cannot set another frame length
513c6aeb7deSFlorian Meier 	 * (and therefore word length) anyway,
514c6aeb7deSFlorian Meier 	 * so the format will be the same.
515c6aeb7deSFlorian Meier 	 */
5169448572dSMatthias Reichl 	regmap_write(dev->i2s_regmap, BCM2835_I2S_RXC_A_REG,
5179448572dSMatthias Reichl 		  format
5189448572dSMatthias Reichl 		| BCM2835_I2S_CH1_POS(rx_ch1_pos)
5199448572dSMatthias Reichl 		| BCM2835_I2S_CH2_POS(rx_ch2_pos));
5209448572dSMatthias Reichl 	regmap_write(dev->i2s_regmap, BCM2835_I2S_TXC_A_REG,
5219448572dSMatthias Reichl 		  format
5229448572dSMatthias Reichl 		| BCM2835_I2S_CH1_POS(tx_ch1_pos)
5239448572dSMatthias Reichl 		| BCM2835_I2S_CH2_POS(tx_ch2_pos));
524c6aeb7deSFlorian Meier 
525c6aeb7deSFlorian Meier 	/* Setup the I2S mode */
526c6aeb7deSFlorian Meier 
527c6aeb7deSFlorian Meier 	if (data_length <= 16) {
528c6aeb7deSFlorian Meier 		/*
529c6aeb7deSFlorian Meier 		 * Use frame packed mode (2 channels per 32 bit word)
530c6aeb7deSFlorian Meier 		 * We cannot set another frame length in the second stream
531c6aeb7deSFlorian Meier 		 * (and therefore word length) anyway,
532c6aeb7deSFlorian Meier 		 * so the format will be the same.
533c6aeb7deSFlorian Meier 		 */
534c6aeb7deSFlorian Meier 		mode |= BCM2835_I2S_FTXP | BCM2835_I2S_FRXP;
535c6aeb7deSFlorian Meier 	}
536c6aeb7deSFlorian Meier 
5379448572dSMatthias Reichl 	mode |= BCM2835_I2S_FLEN(frame_length - 1);
5389448572dSMatthias Reichl 	mode |= BCM2835_I2S_FSLEN(framesync_length);
539c6aeb7deSFlorian Meier 
540abd4f0e1SMatthias Reichl 	/* CLKM selects bcm2835 clock slave mode */
541a91b0e5bSMark Brown 	if (!bit_clock_provider)
542c6aeb7deSFlorian Meier 		mode |= BCM2835_I2S_CLKM;
543c6aeb7deSFlorian Meier 
544abd4f0e1SMatthias Reichl 	/* FSM selects bcm2835 frame sync slave mode */
545a91b0e5bSMark Brown 	if (!frame_sync_provider)
546abd4f0e1SMatthias Reichl 		mode |= BCM2835_I2S_FSM;
547abd4f0e1SMatthias Reichl 
548abd4f0e1SMatthias Reichl 	/* CLKI selects normal clocking mode, sampling on rising edge */
549c6aeb7deSFlorian Meier         switch (dev->fmt & SND_SOC_DAIFMT_INV_MASK) {
550c6aeb7deSFlorian Meier 	case SND_SOC_DAIFMT_NB_NF:
551c6aeb7deSFlorian Meier 	case SND_SOC_DAIFMT_NB_IF:
552c6aeb7deSFlorian Meier 		mode |= BCM2835_I2S_CLKI;
553c6aeb7deSFlorian Meier 		break;
554c6aeb7deSFlorian Meier 	case SND_SOC_DAIFMT_IB_NF:
555abd4f0e1SMatthias Reichl 	case SND_SOC_DAIFMT_IB_IF:
556abd4f0e1SMatthias Reichl 		break;
557abd4f0e1SMatthias Reichl 	default:
558abd4f0e1SMatthias Reichl 		return -EINVAL;
559abd4f0e1SMatthias Reichl 	}
560abd4f0e1SMatthias Reichl 
561abd4f0e1SMatthias Reichl 	/* FSI selects frame start on falling edge */
562abd4f0e1SMatthias Reichl 	switch (dev->fmt & SND_SOC_DAIFMT_INV_MASK) {
563abd4f0e1SMatthias Reichl 	case SND_SOC_DAIFMT_NB_NF:
564abd4f0e1SMatthias Reichl 	case SND_SOC_DAIFMT_IB_NF:
565abd4f0e1SMatthias Reichl 		if (frame_start_falling_edge)
566abd4f0e1SMatthias Reichl 			mode |= BCM2835_I2S_FSI;
567abd4f0e1SMatthias Reichl 		break;
568abd4f0e1SMatthias Reichl 	case SND_SOC_DAIFMT_NB_IF:
569abd4f0e1SMatthias Reichl 	case SND_SOC_DAIFMT_IB_IF:
570abd4f0e1SMatthias Reichl 		if (!frame_start_falling_edge)
571c6aeb7deSFlorian Meier 			mode |= BCM2835_I2S_FSI;
572c6aeb7deSFlorian Meier 		break;
573c6aeb7deSFlorian Meier 	default:
574c6aeb7deSFlorian Meier 		return -EINVAL;
575c6aeb7deSFlorian Meier 	}
576c6aeb7deSFlorian Meier 
577c6aeb7deSFlorian Meier 	regmap_write(dev->i2s_regmap, BCM2835_I2S_MODE_A_REG, mode);
578c6aeb7deSFlorian Meier 
579c6aeb7deSFlorian Meier 	/* Setup the DMA parameters */
580c6aeb7deSFlorian Meier 	regmap_update_bits(dev->i2s_regmap, BCM2835_I2S_CS_A_REG,
581c6aeb7deSFlorian Meier 			BCM2835_I2S_RXTHR(1)
582c6aeb7deSFlorian Meier 			| BCM2835_I2S_TXTHR(1)
583c6aeb7deSFlorian Meier 			| BCM2835_I2S_DMAEN, 0xffffffff);
584c6aeb7deSFlorian Meier 
585c6aeb7deSFlorian Meier 	regmap_update_bits(dev->i2s_regmap, BCM2835_I2S_DREQ_A_REG,
586c6aeb7deSFlorian Meier 			  BCM2835_I2S_TX_PANIC(0x10)
587c6aeb7deSFlorian Meier 			| BCM2835_I2S_RX_PANIC(0x30)
588c6aeb7deSFlorian Meier 			| BCM2835_I2S_TX(0x30)
589c6aeb7deSFlorian Meier 			| BCM2835_I2S_RX(0x20), 0xffffffff);
590c6aeb7deSFlorian Meier 
591c6aeb7deSFlorian Meier 	/* Clear FIFOs */
592c6aeb7deSFlorian Meier 	bcm2835_i2s_clear_fifos(dev, true, true);
593c6aeb7deSFlorian Meier 
5949448572dSMatthias Reichl 	dev_dbg(dev->dev,
5959448572dSMatthias Reichl 		"slots: %d width: %d rx mask: 0x%02x tx_mask: 0x%02x\n",
5969448572dSMatthias Reichl 		slots, slot_width, rx_mask, tx_mask);
5979448572dSMatthias Reichl 
5989448572dSMatthias Reichl 	dev_dbg(dev->dev, "frame len: %d sync len: %d data len: %d\n",
5999448572dSMatthias Reichl 		frame_length, framesync_length, data_length);
6009448572dSMatthias Reichl 
6019448572dSMatthias Reichl 	dev_dbg(dev->dev, "rx pos: %d,%d tx pos: %d,%d\n",
6029448572dSMatthias Reichl 		rx_ch1_pos, rx_ch2_pos, tx_ch1_pos, tx_ch2_pos);
6039448572dSMatthias Reichl 
6049448572dSMatthias Reichl 	dev_dbg(dev->dev, "sampling rate: %d bclk rate: %d\n",
6059448572dSMatthias Reichl 		params_rate(params), bclk_rate);
6069448572dSMatthias Reichl 
607abd4f0e1SMatthias Reichl 	dev_dbg(dev->dev, "CLKM: %d CLKI: %d FSM: %d FSI: %d frame start: %s edge\n",
608abd4f0e1SMatthias Reichl 		!!(mode & BCM2835_I2S_CLKM),
609abd4f0e1SMatthias Reichl 		!!(mode & BCM2835_I2S_CLKI),
610abd4f0e1SMatthias Reichl 		!!(mode & BCM2835_I2S_FSM),
611abd4f0e1SMatthias Reichl 		!!(mode & BCM2835_I2S_FSI),
612abd4f0e1SMatthias Reichl 		(mode & BCM2835_I2S_FSI) ? "falling" : "rising");
613abd4f0e1SMatthias Reichl 
6149448572dSMatthias Reichl 	return ret;
615c6aeb7deSFlorian Meier }
616c6aeb7deSFlorian Meier 
bcm2835_i2s_prepare(struct snd_pcm_substream * substream,struct snd_soc_dai * dai)617c6aeb7deSFlorian Meier static int bcm2835_i2s_prepare(struct snd_pcm_substream *substream,
618c6aeb7deSFlorian Meier 		struct snd_soc_dai *dai)
619c6aeb7deSFlorian Meier {
620c6aeb7deSFlorian Meier 	struct bcm2835_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
621c6aeb7deSFlorian Meier 	uint32_t cs_reg;
622c6aeb7deSFlorian Meier 
623c6aeb7deSFlorian Meier 	/*
624c6aeb7deSFlorian Meier 	 * Clear both FIFOs if the one that should be started
625c6aeb7deSFlorian Meier 	 * is not empty at the moment. This should only happen
626c6aeb7deSFlorian Meier 	 * after overrun. Otherwise, hw_params would have cleared
627c6aeb7deSFlorian Meier 	 * the FIFO.
628c6aeb7deSFlorian Meier 	 */
629c6aeb7deSFlorian Meier 	regmap_read(dev->i2s_regmap, BCM2835_I2S_CS_A_REG, &cs_reg);
630c6aeb7deSFlorian Meier 
631c6aeb7deSFlorian Meier 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK
632c6aeb7deSFlorian Meier 			&& !(cs_reg & BCM2835_I2S_TXE))
633c6aeb7deSFlorian Meier 		bcm2835_i2s_clear_fifos(dev, true, false);
634c6aeb7deSFlorian Meier 	else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE
635c6aeb7deSFlorian Meier 			&& (cs_reg & BCM2835_I2S_RXD))
636c6aeb7deSFlorian Meier 		bcm2835_i2s_clear_fifos(dev, false, true);
637c6aeb7deSFlorian Meier 
638c6aeb7deSFlorian Meier 	return 0;
639c6aeb7deSFlorian Meier }
640c6aeb7deSFlorian Meier 
bcm2835_i2s_stop(struct bcm2835_i2s_dev * dev,struct snd_pcm_substream * substream,struct snd_soc_dai * dai)641c6aeb7deSFlorian Meier static void bcm2835_i2s_stop(struct bcm2835_i2s_dev *dev,
642c6aeb7deSFlorian Meier 		struct snd_pcm_substream *substream,
643c6aeb7deSFlorian Meier 		struct snd_soc_dai *dai)
644c6aeb7deSFlorian Meier {
645c6aeb7deSFlorian Meier 	uint32_t mask;
646c6aeb7deSFlorian Meier 
647c6aeb7deSFlorian Meier 	if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
648c6aeb7deSFlorian Meier 		mask = BCM2835_I2S_RXON;
649c6aeb7deSFlorian Meier 	else
650c6aeb7deSFlorian Meier 		mask = BCM2835_I2S_TXON;
651c6aeb7deSFlorian Meier 
652c6aeb7deSFlorian Meier 	regmap_update_bits(dev->i2s_regmap,
653c6aeb7deSFlorian Meier 			BCM2835_I2S_CS_A_REG, mask, 0);
654c6aeb7deSFlorian Meier 
655c6aeb7deSFlorian Meier 	/* Stop also the clock when not SND_SOC_DAIFMT_CONT */
6568ca4602dSKuninori Morimoto 	if (!snd_soc_dai_active(dai) && !(dev->fmt & SND_SOC_DAIFMT_CONT))
657c6aeb7deSFlorian Meier 		bcm2835_i2s_stop_clock(dev);
658c6aeb7deSFlorian Meier }
659c6aeb7deSFlorian Meier 
bcm2835_i2s_trigger(struct snd_pcm_substream * substream,int cmd,struct snd_soc_dai * dai)660c6aeb7deSFlorian Meier static int bcm2835_i2s_trigger(struct snd_pcm_substream *substream, int cmd,
661c6aeb7deSFlorian Meier 			       struct snd_soc_dai *dai)
662c6aeb7deSFlorian Meier {
663c6aeb7deSFlorian Meier 	struct bcm2835_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
664c6aeb7deSFlorian Meier 	uint32_t mask;
665c6aeb7deSFlorian Meier 
666c6aeb7deSFlorian Meier 	switch (cmd) {
667c6aeb7deSFlorian Meier 	case SNDRV_PCM_TRIGGER_START:
668c6aeb7deSFlorian Meier 	case SNDRV_PCM_TRIGGER_RESUME:
669c6aeb7deSFlorian Meier 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
670c6aeb7deSFlorian Meier 		bcm2835_i2s_start_clock(dev);
671c6aeb7deSFlorian Meier 
672c6aeb7deSFlorian Meier 		if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
673c6aeb7deSFlorian Meier 			mask = BCM2835_I2S_RXON;
674c6aeb7deSFlorian Meier 		else
675c6aeb7deSFlorian Meier 			mask = BCM2835_I2S_TXON;
676c6aeb7deSFlorian Meier 
677c6aeb7deSFlorian Meier 		regmap_update_bits(dev->i2s_regmap,
678c6aeb7deSFlorian Meier 				BCM2835_I2S_CS_A_REG, mask, mask);
679c6aeb7deSFlorian Meier 		break;
680c6aeb7deSFlorian Meier 
681c6aeb7deSFlorian Meier 	case SNDRV_PCM_TRIGGER_STOP:
682c6aeb7deSFlorian Meier 	case SNDRV_PCM_TRIGGER_SUSPEND:
683c6aeb7deSFlorian Meier 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
684c6aeb7deSFlorian Meier 		bcm2835_i2s_stop(dev, substream, dai);
685c6aeb7deSFlorian Meier 		break;
686c6aeb7deSFlorian Meier 	default:
687c6aeb7deSFlorian Meier 		return -EINVAL;
688c6aeb7deSFlorian Meier 	}
689c6aeb7deSFlorian Meier 
690c6aeb7deSFlorian Meier 	return 0;
691c6aeb7deSFlorian Meier }
692c6aeb7deSFlorian Meier 
bcm2835_i2s_startup(struct snd_pcm_substream * substream,struct snd_soc_dai * dai)693c6aeb7deSFlorian Meier static int bcm2835_i2s_startup(struct snd_pcm_substream *substream,
694c6aeb7deSFlorian Meier 			       struct snd_soc_dai *dai)
695c6aeb7deSFlorian Meier {
696c6aeb7deSFlorian Meier 	struct bcm2835_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
697c6aeb7deSFlorian Meier 
6988ca4602dSKuninori Morimoto 	if (snd_soc_dai_active(dai))
699c6aeb7deSFlorian Meier 		return 0;
700c6aeb7deSFlorian Meier 
701c6aeb7deSFlorian Meier 	/* Should this still be running stop it */
702c6aeb7deSFlorian Meier 	bcm2835_i2s_stop_clock(dev);
703c6aeb7deSFlorian Meier 
704c6aeb7deSFlorian Meier 	/* Enable PCM block */
705c6aeb7deSFlorian Meier 	regmap_update_bits(dev->i2s_regmap, BCM2835_I2S_CS_A_REG,
706c6aeb7deSFlorian Meier 			BCM2835_I2S_EN, BCM2835_I2S_EN);
707c6aeb7deSFlorian Meier 
708c6aeb7deSFlorian Meier 	/*
709c6aeb7deSFlorian Meier 	 * Disable STBY.
710c6aeb7deSFlorian Meier 	 * Requires at least 4 PCM clock cycles to take effect.
711c6aeb7deSFlorian Meier 	 */
712c6aeb7deSFlorian Meier 	regmap_update_bits(dev->i2s_regmap, BCM2835_I2S_CS_A_REG,
713c6aeb7deSFlorian Meier 			BCM2835_I2S_STBY, BCM2835_I2S_STBY);
714c6aeb7deSFlorian Meier 
715c6aeb7deSFlorian Meier 	return 0;
716c6aeb7deSFlorian Meier }
717c6aeb7deSFlorian Meier 
bcm2835_i2s_shutdown(struct snd_pcm_substream * substream,struct snd_soc_dai * dai)718c6aeb7deSFlorian Meier static void bcm2835_i2s_shutdown(struct snd_pcm_substream *substream,
719c6aeb7deSFlorian Meier 		struct snd_soc_dai *dai)
720c6aeb7deSFlorian Meier {
721c6aeb7deSFlorian Meier 	struct bcm2835_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
722c6aeb7deSFlorian Meier 
723c6aeb7deSFlorian Meier 	bcm2835_i2s_stop(dev, substream, dai);
724c6aeb7deSFlorian Meier 
725c6aeb7deSFlorian Meier 	/* If both streams are stopped, disable module and clock */
7268ca4602dSKuninori Morimoto 	if (snd_soc_dai_active(dai))
727c6aeb7deSFlorian Meier 		return;
728c6aeb7deSFlorian Meier 
729c6aeb7deSFlorian Meier 	/* Disable the module */
730c6aeb7deSFlorian Meier 	regmap_update_bits(dev->i2s_regmap, BCM2835_I2S_CS_A_REG,
731c6aeb7deSFlorian Meier 			BCM2835_I2S_EN, 0);
732c6aeb7deSFlorian Meier 
733c6aeb7deSFlorian Meier 	/*
734c6aeb7deSFlorian Meier 	 * Stopping clock is necessary, because stop does
735c6aeb7deSFlorian Meier 	 * not stop the clock when SND_SOC_DAIFMT_CONT
736c6aeb7deSFlorian Meier 	 */
737c6aeb7deSFlorian Meier 	bcm2835_i2s_stop_clock(dev);
738c6aeb7deSFlorian Meier }
739c6aeb7deSFlorian Meier 
bcm2835_i2s_dai_probe(struct snd_soc_dai * dai)740c6aeb7deSFlorian Meier static int bcm2835_i2s_dai_probe(struct snd_soc_dai *dai)
741c6aeb7deSFlorian Meier {
742c6aeb7deSFlorian Meier 	struct bcm2835_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
743c6aeb7deSFlorian Meier 
744c6aeb7deSFlorian Meier 	snd_soc_dai_init_dma_data(dai,
745c6aeb7deSFlorian Meier 				  &dev->dma_data[SNDRV_PCM_STREAM_PLAYBACK],
746c6aeb7deSFlorian Meier 				  &dev->dma_data[SNDRV_PCM_STREAM_CAPTURE]);
747c6aeb7deSFlorian Meier 
748c6aeb7deSFlorian Meier 	return 0;
749c6aeb7deSFlorian Meier }
750c6aeb7deSFlorian Meier 
751*755ecb00SKuninori Morimoto static const struct snd_soc_dai_ops bcm2835_i2s_dai_ops = {
752*755ecb00SKuninori Morimoto 	.probe		= bcm2835_i2s_dai_probe,
753*755ecb00SKuninori Morimoto 	.startup	= bcm2835_i2s_startup,
754*755ecb00SKuninori Morimoto 	.shutdown	= bcm2835_i2s_shutdown,
755*755ecb00SKuninori Morimoto 	.prepare	= bcm2835_i2s_prepare,
756*755ecb00SKuninori Morimoto 	.trigger	= bcm2835_i2s_trigger,
757*755ecb00SKuninori Morimoto 	.hw_params	= bcm2835_i2s_hw_params,
758*755ecb00SKuninori Morimoto 	.set_fmt	= bcm2835_i2s_set_dai_fmt,
759*755ecb00SKuninori Morimoto 	.set_bclk_ratio	= bcm2835_i2s_set_dai_bclk_ratio,
760*755ecb00SKuninori Morimoto 	.set_tdm_slot	= bcm2835_i2s_set_dai_tdm_slot,
761*755ecb00SKuninori Morimoto };
762*755ecb00SKuninori Morimoto 
763c6aeb7deSFlorian Meier static struct snd_soc_dai_driver bcm2835_i2s_dai = {
764c6aeb7deSFlorian Meier 	.name	= "bcm2835-i2s",
765c6aeb7deSFlorian Meier 	.playback = {
766c6aeb7deSFlorian Meier 		.channels_min = 2,
767c6aeb7deSFlorian Meier 		.channels_max = 2,
768675c0ee5SMatthias Reichl 		.rates =	SNDRV_PCM_RATE_CONTINUOUS,
769675c0ee5SMatthias Reichl 		.rate_min =	8000,
770675c0ee5SMatthias Reichl 		.rate_max =	384000,
771c6aeb7deSFlorian Meier 		.formats =	SNDRV_PCM_FMTBIT_S16_LE
772a34b027dSMatthias Reichl 				| SNDRV_PCM_FMTBIT_S24_LE
773c6aeb7deSFlorian Meier 				| SNDRV_PCM_FMTBIT_S32_LE
774c6aeb7deSFlorian Meier 		},
775c6aeb7deSFlorian Meier 	.capture = {
776c6aeb7deSFlorian Meier 		.channels_min = 2,
777c6aeb7deSFlorian Meier 		.channels_max = 2,
778675c0ee5SMatthias Reichl 		.rates =	SNDRV_PCM_RATE_CONTINUOUS,
779675c0ee5SMatthias Reichl 		.rate_min =	8000,
780675c0ee5SMatthias Reichl 		.rate_max =	384000,
781c6aeb7deSFlorian Meier 		.formats =	SNDRV_PCM_FMTBIT_S16_LE
782a34b027dSMatthias Reichl 				| SNDRV_PCM_FMTBIT_S24_LE
783c6aeb7deSFlorian Meier 				| SNDRV_PCM_FMTBIT_S32_LE
784c6aeb7deSFlorian Meier 		},
785c6aeb7deSFlorian Meier 	.ops = &bcm2835_i2s_dai_ops,
786a8e94022SKuninori Morimoto 	.symmetric_rate = 1,
787a8e94022SKuninori Morimoto 	.symmetric_sample_bits = 1,
788c6aeb7deSFlorian Meier };
789c6aeb7deSFlorian Meier 
bcm2835_i2s_volatile_reg(struct device * dev,unsigned int reg)790c6aeb7deSFlorian Meier static bool bcm2835_i2s_volatile_reg(struct device *dev, unsigned int reg)
791c6aeb7deSFlorian Meier {
792c6aeb7deSFlorian Meier 	switch (reg) {
793c6aeb7deSFlorian Meier 	case BCM2835_I2S_CS_A_REG:
794c6aeb7deSFlorian Meier 	case BCM2835_I2S_FIFO_A_REG:
795c6aeb7deSFlorian Meier 	case BCM2835_I2S_INTSTC_A_REG:
796c6aeb7deSFlorian Meier 	case BCM2835_I2S_GRAY_REG:
797c6aeb7deSFlorian Meier 		return true;
798c6aeb7deSFlorian Meier 	default:
799c6aeb7deSFlorian Meier 		return false;
80046713ed2STom Rix 	}
801c6aeb7deSFlorian Meier }
802c6aeb7deSFlorian Meier 
bcm2835_i2s_precious_reg(struct device * dev,unsigned int reg)803c6aeb7deSFlorian Meier static bool bcm2835_i2s_precious_reg(struct device *dev, unsigned int reg)
804c6aeb7deSFlorian Meier {
805c6aeb7deSFlorian Meier 	switch (reg) {
806c6aeb7deSFlorian Meier 	case BCM2835_I2S_FIFO_A_REG:
807c6aeb7deSFlorian Meier 		return true;
808c6aeb7deSFlorian Meier 	default:
809c6aeb7deSFlorian Meier 		return false;
81046713ed2STom Rix 	}
811c6aeb7deSFlorian Meier }
812c6aeb7deSFlorian Meier 
813517e7a15SMartin Sperl static const struct regmap_config bcm2835_regmap_config = {
814c6aeb7deSFlorian Meier 	.reg_bits = 32,
815c6aeb7deSFlorian Meier 	.reg_stride = 4,
816c6aeb7deSFlorian Meier 	.val_bits = 32,
817c6aeb7deSFlorian Meier 	.max_register = BCM2835_I2S_GRAY_REG,
818c6aeb7deSFlorian Meier 	.precious_reg = bcm2835_i2s_precious_reg,
819c6aeb7deSFlorian Meier 	.volatile_reg = bcm2835_i2s_volatile_reg,
820c6aeb7deSFlorian Meier 	.cache_type = REGCACHE_RBTREE,
821c6aeb7deSFlorian Meier };
822c6aeb7deSFlorian Meier 
823c6aeb7deSFlorian Meier static const struct snd_soc_component_driver bcm2835_i2s_component = {
824c6aeb7deSFlorian Meier 	.name			= "bcm2835-i2s-comp",
825b9a0db0aSCharles Keepax 	.legacy_dai_naming	= 1,
826c6aeb7deSFlorian Meier };
827c6aeb7deSFlorian Meier 
bcm2835_i2s_probe(struct platform_device * pdev)828c6aeb7deSFlorian Meier static int bcm2835_i2s_probe(struct platform_device *pdev)
829c6aeb7deSFlorian Meier {
830c6aeb7deSFlorian Meier 	struct bcm2835_i2s_dev *dev;
831c6aeb7deSFlorian Meier 	int ret;
832c6aeb7deSFlorian Meier 	void __iomem *base;
833517e7a15SMartin Sperl 	const __be32 *addr;
834517e7a15SMartin Sperl 	dma_addr_t dma_base;
835c6aeb7deSFlorian Meier 
836c6aeb7deSFlorian Meier 	dev = devm_kzalloc(&pdev->dev, sizeof(*dev),
837c6aeb7deSFlorian Meier 			   GFP_KERNEL);
838c6aeb7deSFlorian Meier 	if (!dev)
839c6aeb7deSFlorian Meier 		return -ENOMEM;
840c6aeb7deSFlorian Meier 
841517e7a15SMartin Sperl 	/* get the clock */
842517e7a15SMartin Sperl 	dev->clk_prepared = false;
843517e7a15SMartin Sperl 	dev->clk = devm_clk_get(&pdev->dev, NULL);
844b4075895SYang Yingliang 	if (IS_ERR(dev->clk))
845b4075895SYang Yingliang 		return dev_err_probe(&pdev->dev, PTR_ERR(dev->clk),
846b4075895SYang Yingliang 				     "could not get clk\n");
847c6aeb7deSFlorian Meier 
848517e7a15SMartin Sperl 	/* Request ioarea */
849d400b1b3SYueHaibing 	base = devm_platform_ioremap_resource(pdev, 0);
850517e7a15SMartin Sperl 	if (IS_ERR(base))
851517e7a15SMartin Sperl 		return PTR_ERR(base);
852517e7a15SMartin Sperl 
853517e7a15SMartin Sperl 	dev->i2s_regmap = devm_regmap_init_mmio(&pdev->dev, base,
854517e7a15SMartin Sperl 				&bcm2835_regmap_config);
855517e7a15SMartin Sperl 	if (IS_ERR(dev->i2s_regmap))
856517e7a15SMartin Sperl 		return PTR_ERR(dev->i2s_regmap);
857517e7a15SMartin Sperl 
858517e7a15SMartin Sperl 	/* Set the DMA address - we have to parse DT ourselves */
859517e7a15SMartin Sperl 	addr = of_get_address(pdev->dev.of_node, 0, NULL, NULL);
860517e7a15SMartin Sperl 	if (!addr) {
861517e7a15SMartin Sperl 		dev_err(&pdev->dev, "could not get DMA-register address\n");
862517e7a15SMartin Sperl 		return -EINVAL;
863517e7a15SMartin Sperl 	}
864517e7a15SMartin Sperl 	dma_base = be32_to_cpup(addr);
865517e7a15SMartin Sperl 
866c6aeb7deSFlorian Meier 	dev->dma_data[SNDRV_PCM_STREAM_PLAYBACK].addr =
867517e7a15SMartin Sperl 		dma_base + BCM2835_I2S_FIFO_A_REG;
868c6aeb7deSFlorian Meier 
869c6aeb7deSFlorian Meier 	dev->dma_data[SNDRV_PCM_STREAM_CAPTURE].addr =
870517e7a15SMartin Sperl 		dma_base + BCM2835_I2S_FIFO_A_REG;
871c6aeb7deSFlorian Meier 
872c6aeb7deSFlorian Meier 	/* Set the bus width */
873c6aeb7deSFlorian Meier 	dev->dma_data[SNDRV_PCM_STREAM_PLAYBACK].addr_width =
874c6aeb7deSFlorian Meier 		DMA_SLAVE_BUSWIDTH_4_BYTES;
875c6aeb7deSFlorian Meier 	dev->dma_data[SNDRV_PCM_STREAM_CAPTURE].addr_width =
876c6aeb7deSFlorian Meier 		DMA_SLAVE_BUSWIDTH_4_BYTES;
877c6aeb7deSFlorian Meier 
878c6aeb7deSFlorian Meier 	/* Set burst */
879c6aeb7deSFlorian Meier 	dev->dma_data[SNDRV_PCM_STREAM_PLAYBACK].maxburst = 2;
880c6aeb7deSFlorian Meier 	dev->dma_data[SNDRV_PCM_STREAM_CAPTURE].maxburst = 2;
881c6aeb7deSFlorian Meier 
882beff053cSMatthias Reichl 	/*
883beff053cSMatthias Reichl 	 * Set the PACK flag to enable S16_LE support (2 S16_LE values
884beff053cSMatthias Reichl 	 * packed into 32-bit transfers).
885beff053cSMatthias Reichl 	 */
886beff053cSMatthias Reichl 	dev->dma_data[SNDRV_PCM_STREAM_PLAYBACK].flags =
887beff053cSMatthias Reichl 		SND_DMAENGINE_PCM_DAI_FLAG_PACK;
888beff053cSMatthias Reichl 	dev->dma_data[SNDRV_PCM_STREAM_CAPTURE].flags =
889beff053cSMatthias Reichl 		SND_DMAENGINE_PCM_DAI_FLAG_PACK;
890beff053cSMatthias Reichl 
891c6aeb7deSFlorian Meier 	/* Store the pdev */
892c6aeb7deSFlorian Meier 	dev->dev = &pdev->dev;
893c6aeb7deSFlorian Meier 	dev_set_drvdata(&pdev->dev, dev);
894c6aeb7deSFlorian Meier 
895c6aeb7deSFlorian Meier 	ret = devm_snd_soc_register_component(&pdev->dev,
896c6aeb7deSFlorian Meier 			&bcm2835_i2s_component, &bcm2835_i2s_dai, 1);
897c6aeb7deSFlorian Meier 	if (ret) {
898c6aeb7deSFlorian Meier 		dev_err(&pdev->dev, "Could not register DAI: %d\n", ret);
899c6aeb7deSFlorian Meier 		return ret;
900c6aeb7deSFlorian Meier 	}
901c6aeb7deSFlorian Meier 
9023990c516SLars-Peter Clausen 	ret = devm_snd_dmaengine_pcm_register(&pdev->dev, NULL, 0);
903c6aeb7deSFlorian Meier 	if (ret) {
904c6aeb7deSFlorian Meier 		dev_err(&pdev->dev, "Could not register PCM: %d\n", ret);
905c6aeb7deSFlorian Meier 		return ret;
906c6aeb7deSFlorian Meier 	}
907c6aeb7deSFlorian Meier 
908c6aeb7deSFlorian Meier 	return 0;
909c6aeb7deSFlorian Meier }
910c6aeb7deSFlorian Meier 
911c6aeb7deSFlorian Meier static const struct of_device_id bcm2835_i2s_of_match[] = {
912c6aeb7deSFlorian Meier 	{ .compatible = "brcm,bcm2835-i2s", },
913c6aeb7deSFlorian Meier 	{},
914c6aeb7deSFlorian Meier };
915c6aeb7deSFlorian Meier 
916054bc835SLuis de Bethencourt MODULE_DEVICE_TABLE(of, bcm2835_i2s_of_match);
917054bc835SLuis de Bethencourt 
918c6aeb7deSFlorian Meier static struct platform_driver bcm2835_i2s_driver = {
919c6aeb7deSFlorian Meier 	.probe		= bcm2835_i2s_probe,
920c6aeb7deSFlorian Meier 	.driver		= {
921c6aeb7deSFlorian Meier 		.name	= "bcm2835-i2s",
922c6aeb7deSFlorian Meier 		.of_match_table = bcm2835_i2s_of_match,
923c6aeb7deSFlorian Meier 	},
924c6aeb7deSFlorian Meier };
925c6aeb7deSFlorian Meier 
926c6aeb7deSFlorian Meier module_platform_driver(bcm2835_i2s_driver);
927c6aeb7deSFlorian Meier 
928c6aeb7deSFlorian Meier MODULE_ALIAS("platform:bcm2835-i2s");
929c6aeb7deSFlorian Meier MODULE_DESCRIPTION("BCM2835 I2S interface");
930c6aeb7deSFlorian Meier MODULE_AUTHOR("Florian Meier <florian.meier@koalo.de>");
931c6aeb7deSFlorian Meier MODULE_LICENSE("GPL v2");
932