xref: /linux/sound/soc/atmel/atmel-i2s.c (revision b543e467d1a9451013cd89ddcacde07dda9e7f32)
1*b543e467SCyrille Pitchen /*
2*b543e467SCyrille Pitchen  * Driver for Atmel I2S controller
3*b543e467SCyrille Pitchen  *
4*b543e467SCyrille Pitchen  * Copyright (C) 2015 Atmel Corporation
5*b543e467SCyrille Pitchen  *
6*b543e467SCyrille Pitchen  * Author: Cyrille Pitchen <cyrille.pitchen@atmel.com>
7*b543e467SCyrille Pitchen  *
8*b543e467SCyrille Pitchen  * This program is free software; you can redistribute it and/or modify it
9*b543e467SCyrille Pitchen  * under the terms of the GNU General Public License version 2 as published by
10*b543e467SCyrille Pitchen  * the Free Software Foundation.
11*b543e467SCyrille Pitchen  *
12*b543e467SCyrille Pitchen  * This program is distributed in the hope that it will be useful, but WITHOUT
13*b543e467SCyrille Pitchen  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14*b543e467SCyrille Pitchen  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15*b543e467SCyrille Pitchen  * more details.
16*b543e467SCyrille Pitchen  *
17*b543e467SCyrille Pitchen  * You should have received a copy of the GNU General Public License along with
18*b543e467SCyrille Pitchen  * this program.  If not, see <http://www.gnu.org/licenses/>.
19*b543e467SCyrille Pitchen  */
20*b543e467SCyrille Pitchen 
21*b543e467SCyrille Pitchen #include <linux/init.h>
22*b543e467SCyrille Pitchen #include <linux/module.h>
23*b543e467SCyrille Pitchen #include <linux/device.h>
24*b543e467SCyrille Pitchen #include <linux/slab.h>
25*b543e467SCyrille Pitchen #include <linux/delay.h>
26*b543e467SCyrille Pitchen #include <linux/io.h>
27*b543e467SCyrille Pitchen #include <linux/clk.h>
28*b543e467SCyrille Pitchen #include <linux/mfd/syscon.h>
29*b543e467SCyrille Pitchen 
30*b543e467SCyrille Pitchen #include <sound/core.h>
31*b543e467SCyrille Pitchen #include <sound/pcm.h>
32*b543e467SCyrille Pitchen #include <sound/pcm_params.h>
33*b543e467SCyrille Pitchen #include <sound/initval.h>
34*b543e467SCyrille Pitchen #include <sound/soc.h>
35*b543e467SCyrille Pitchen #include <sound/dmaengine_pcm.h>
36*b543e467SCyrille Pitchen 
37*b543e467SCyrille Pitchen #define ATMEL_I2SC_MAX_TDM_CHANNELS	8
38*b543e467SCyrille Pitchen 
39*b543e467SCyrille Pitchen /*
40*b543e467SCyrille Pitchen  * ---- I2S Controller Register map ----
41*b543e467SCyrille Pitchen  */
42*b543e467SCyrille Pitchen #define ATMEL_I2SC_CR		0x0000	/* Control Register */
43*b543e467SCyrille Pitchen #define ATMEL_I2SC_MR		0x0004	/* Mode Register */
44*b543e467SCyrille Pitchen #define ATMEL_I2SC_SR		0x0008	/* Status Register */
45*b543e467SCyrille Pitchen #define ATMEL_I2SC_SCR		0x000c	/* Status Clear Register */
46*b543e467SCyrille Pitchen #define ATMEL_I2SC_SSR		0x0010	/* Status Set Register */
47*b543e467SCyrille Pitchen #define ATMEL_I2SC_IER		0x0014	/* Interrupt Enable Register */
48*b543e467SCyrille Pitchen #define ATMEL_I2SC_IDR		0x0018	/* Interrupt Disable Register */
49*b543e467SCyrille Pitchen #define ATMEL_I2SC_IMR		0x001c	/* Interrupt Mask Register */
50*b543e467SCyrille Pitchen #define ATMEL_I2SC_RHR		0x0020	/* Receiver Holding Register */
51*b543e467SCyrille Pitchen #define ATMEL_I2SC_THR		0x0024	/* Transmitter Holding Register */
52*b543e467SCyrille Pitchen #define ATMEL_I2SC_VERSION	0x0028	/* Version Register */
53*b543e467SCyrille Pitchen 
54*b543e467SCyrille Pitchen /*
55*b543e467SCyrille Pitchen  * ---- Control Register (Write-only) ----
56*b543e467SCyrille Pitchen  */
57*b543e467SCyrille Pitchen #define ATMEL_I2SC_CR_RXEN	BIT(0)	/* Receiver Enable */
58*b543e467SCyrille Pitchen #define ATMEL_I2SC_CR_RXDIS	BIT(1)	/* Receiver Disable */
59*b543e467SCyrille Pitchen #define ATMEL_I2SC_CR_CKEN	BIT(2)	/* Clock Enable */
60*b543e467SCyrille Pitchen #define ATMEL_I2SC_CR_CKDIS	BIT(3)	/* Clock Disable */
61*b543e467SCyrille Pitchen #define ATMEL_I2SC_CR_TXEN	BIT(4)	/* Transmitter Enable */
62*b543e467SCyrille Pitchen #define ATMEL_I2SC_CR_TXDIS	BIT(5)	/* Transmitter Disable */
63*b543e467SCyrille Pitchen #define ATMEL_I2SC_CR_SWRST	BIT(7)	/* Software Reset */
64*b543e467SCyrille Pitchen 
65*b543e467SCyrille Pitchen /*
66*b543e467SCyrille Pitchen  * ---- Mode Register (Read/Write) ----
67*b543e467SCyrille Pitchen  */
68*b543e467SCyrille Pitchen #define ATMEL_I2SC_MR_MODE_MASK		GENMASK(0, 0)
69*b543e467SCyrille Pitchen #define ATMEL_I2SC_MR_MODE_SLAVE	(0 << 0)
70*b543e467SCyrille Pitchen #define ATMEL_I2SC_MR_MODE_MASTER	(1 << 0)
71*b543e467SCyrille Pitchen 
72*b543e467SCyrille Pitchen #define ATMEL_I2SC_MR_DATALENGTH_MASK		GENMASK(4, 2)
73*b543e467SCyrille Pitchen #define ATMEL_I2SC_MR_DATALENGTH_32_BITS	(0 << 2)
74*b543e467SCyrille Pitchen #define ATMEL_I2SC_MR_DATALENGTH_24_BITS	(1 << 2)
75*b543e467SCyrille Pitchen #define ATMEL_I2SC_MR_DATALENGTH_20_BITS	(2 << 2)
76*b543e467SCyrille Pitchen #define ATMEL_I2SC_MR_DATALENGTH_18_BITS	(3 << 2)
77*b543e467SCyrille Pitchen #define ATMEL_I2SC_MR_DATALENGTH_16_BITS	(4 << 2)
78*b543e467SCyrille Pitchen #define ATMEL_I2SC_MR_DATALENGTH_16_BITS_COMPACT	(5 << 2)
79*b543e467SCyrille Pitchen #define ATMEL_I2SC_MR_DATALENGTH_8_BITS		(6 << 2)
80*b543e467SCyrille Pitchen #define ATMEL_I2SC_MR_DATALENGTH_8_BITS_COMPACT	(7 << 2)
81*b543e467SCyrille Pitchen 
82*b543e467SCyrille Pitchen #define ATMEL_I2SC_MR_FORMAT_MASK	GENMASK(7, 6)
83*b543e467SCyrille Pitchen #define ATMEL_I2SC_MR_FORMAT_I2S	(0 << 6)
84*b543e467SCyrille Pitchen #define ATMEL_I2SC_MR_FORMAT_LJ		(1 << 6)  /* Left Justified */
85*b543e467SCyrille Pitchen #define ATMEL_I2SC_MR_FORMAT_TDM	(2 << 6)
86*b543e467SCyrille Pitchen #define ATMEL_I2SC_MR_FORMAT_TDMLJ	(3 << 6)
87*b543e467SCyrille Pitchen 
88*b543e467SCyrille Pitchen /* Left audio samples duplicated to right audio channel */
89*b543e467SCyrille Pitchen #define ATMEL_I2SC_MR_RXMONO		BIT(8)
90*b543e467SCyrille Pitchen 
91*b543e467SCyrille Pitchen /* Receiver uses one DMA channel ... */
92*b543e467SCyrille Pitchen #define ATMEL_I2SC_MR_RXDMA_MASK	GENMASK(9, 9)
93*b543e467SCyrille Pitchen #define ATMEL_I2SC_MR_RXDMA_SINGLE	(0 << 9)  /* for all audio channels */
94*b543e467SCyrille Pitchen #define ATMEL_I2SC_MR_RXDMA_MULTIPLE	(1 << 9)  /* per audio channel */
95*b543e467SCyrille Pitchen 
96*b543e467SCyrille Pitchen /* I2SDO output of I2SC is internally connected to I2SDI input */
97*b543e467SCyrille Pitchen #define ATMEL_I2SC_MR_RXLOOP		BIT(10)
98*b543e467SCyrille Pitchen 
99*b543e467SCyrille Pitchen /* Left audio samples duplicated to right audio channel */
100*b543e467SCyrille Pitchen #define ATMEL_I2SC_MR_TXMONO		BIT(12)
101*b543e467SCyrille Pitchen 
102*b543e467SCyrille Pitchen /* Transmitter uses one DMA channel ... */
103*b543e467SCyrille Pitchen #define ATMEL_I2SC_MR_TXDMA_MASK	GENMASK(13, 13)
104*b543e467SCyrille Pitchen #define ATMEL_I2SC_MR_TXDMA_SINGLE	(0 << 13)  /* for all audio channels */
105*b543e467SCyrille Pitchen #define ATMEL_I2SC_MR_TXDME_MULTIPLE	(1 << 13)  /* per audio channel */
106*b543e467SCyrille Pitchen 
107*b543e467SCyrille Pitchen /* x sample transmitted when underrun */
108*b543e467SCyrille Pitchen #define ATMEL_I2SC_MR_TXSAME_MASK	GENMASK(14, 14)
109*b543e467SCyrille Pitchen #define ATMEL_I2SC_MR_TXSAME_ZERO	(0 << 14)  /* Zero sample */
110*b543e467SCyrille Pitchen #define ATMEL_I2SC_MR_TXSAME_PREVIOUS	(1 << 14)  /* Previous sample */
111*b543e467SCyrille Pitchen 
112*b543e467SCyrille Pitchen /* Audio Clock to I2SC Master Clock ratio */
113*b543e467SCyrille Pitchen #define ATMEL_I2SC_MR_IMCKDIV_MASK	GENMASK(21, 16)
114*b543e467SCyrille Pitchen #define ATMEL_I2SC_MR_IMCKDIV(div) \
115*b543e467SCyrille Pitchen 	(((div) << 16) & ATMEL_I2SC_MR_IMCKDIV_MASK)
116*b543e467SCyrille Pitchen 
117*b543e467SCyrille Pitchen /* Master Clock to fs ratio */
118*b543e467SCyrille Pitchen #define ATMEL_I2SC_MR_IMCKFS_MASK	GENMASK(29, 24)
119*b543e467SCyrille Pitchen #define ATMEL_I2SC_MR_IMCKFS(fs) \
120*b543e467SCyrille Pitchen 	(((fs) << 24) & ATMEL_I2SC_MR_IMCKFS_MASK)
121*b543e467SCyrille Pitchen 
122*b543e467SCyrille Pitchen /* Master Clock mode */
123*b543e467SCyrille Pitchen #define ATMEL_I2SC_MR_IMCKMODE_MASK	GENMASK(30, 30)
124*b543e467SCyrille Pitchen /* 0: No master clock generated (selected clock drives I2SCK pin) */
125*b543e467SCyrille Pitchen #define ATMEL_I2SC_MR_IMCKMODE_I2SCK	(0 << 30)
126*b543e467SCyrille Pitchen /* 1: master clock generated (internally generated clock drives I2SMCK pin) */
127*b543e467SCyrille Pitchen #define ATMEL_I2SC_MR_IMCKMODE_I2SMCK	(1 << 30)
128*b543e467SCyrille Pitchen 
129*b543e467SCyrille Pitchen /* Slot Width */
130*b543e467SCyrille Pitchen /* 0: slot is 32 bits wide for DATALENGTH = 18/20/24 bits. */
131*b543e467SCyrille Pitchen /* 1: slot is 24 bits wide for DATALENGTH = 18/20/24 bits. */
132*b543e467SCyrille Pitchen #define ATMEL_I2SC_MR_IWS		BIT(31)
133*b543e467SCyrille Pitchen 
134*b543e467SCyrille Pitchen /*
135*b543e467SCyrille Pitchen  * ---- Status Registers ----
136*b543e467SCyrille Pitchen  */
137*b543e467SCyrille Pitchen #define ATMEL_I2SC_SR_RXEN	BIT(0)	/* Receiver Enabled */
138*b543e467SCyrille Pitchen #define ATMEL_I2SC_SR_RXRDY	BIT(1)	/* Receive Ready */
139*b543e467SCyrille Pitchen #define ATMEL_I2SC_SR_RXOR	BIT(2)	/* Receive Overrun */
140*b543e467SCyrille Pitchen 
141*b543e467SCyrille Pitchen #define ATMEL_I2SC_SR_TXEN	BIT(4)	/* Transmitter Enabled */
142*b543e467SCyrille Pitchen #define ATMEL_I2SC_SR_TXRDY	BIT(5)	/* Transmit Ready */
143*b543e467SCyrille Pitchen #define ATMEL_I2SC_SR_TXUR	BIT(6)	/* Transmit Underrun */
144*b543e467SCyrille Pitchen 
145*b543e467SCyrille Pitchen /* Receive Overrun Channel */
146*b543e467SCyrille Pitchen #define ATMEL_I2SC_SR_RXORCH_MASK	GENMASK(15, 8)
147*b543e467SCyrille Pitchen #define ATMEL_I2SC_SR_RXORCH(ch)	(1 << (((ch) & 0x7) + 8))
148*b543e467SCyrille Pitchen 
149*b543e467SCyrille Pitchen /* Transmit Underrun Channel */
150*b543e467SCyrille Pitchen #define ATMEL_I2SC_SR_TXURCH_MASK	GENMASK(27, 20)
151*b543e467SCyrille Pitchen #define ATMEL_I2SC_SR_TXURCH(ch)	(1 << (((ch) & 0x7) + 20))
152*b543e467SCyrille Pitchen 
153*b543e467SCyrille Pitchen /*
154*b543e467SCyrille Pitchen  * ---- Interrupt Enable/Disable/Mask Registers ----
155*b543e467SCyrille Pitchen  */
156*b543e467SCyrille Pitchen #define ATMEL_I2SC_INT_RXRDY	ATMEL_I2SC_SR_RXRDY
157*b543e467SCyrille Pitchen #define ATMEL_I2SC_INT_RXOR	ATMEL_I2SC_SR_RXOR
158*b543e467SCyrille Pitchen #define ATMEL_I2SC_INT_TXRDY	ATMEL_I2SC_SR_TXRDY
159*b543e467SCyrille Pitchen #define ATMEL_I2SC_INT_TXUR	ATMEL_I2SC_SR_TXUR
160*b543e467SCyrille Pitchen 
161*b543e467SCyrille Pitchen static const struct regmap_config atmel_i2s_regmap_config = {
162*b543e467SCyrille Pitchen 	.reg_bits = 32,
163*b543e467SCyrille Pitchen 	.reg_stride = 4,
164*b543e467SCyrille Pitchen 	.val_bits = 32,
165*b543e467SCyrille Pitchen 	.max_register = ATMEL_I2SC_VERSION,
166*b543e467SCyrille Pitchen };
167*b543e467SCyrille Pitchen 
168*b543e467SCyrille Pitchen struct atmel_i2s_gck_param {
169*b543e467SCyrille Pitchen 	int		fs;
170*b543e467SCyrille Pitchen 	unsigned long	mck;
171*b543e467SCyrille Pitchen 	int		imckdiv;
172*b543e467SCyrille Pitchen 	int		imckfs;
173*b543e467SCyrille Pitchen };
174*b543e467SCyrille Pitchen 
175*b543e467SCyrille Pitchen #define I2S_MCK_12M288		12288000UL
176*b543e467SCyrille Pitchen #define I2S_MCK_11M2896		11289600UL
177*b543e467SCyrille Pitchen 
178*b543e467SCyrille Pitchen /* mck = (32 * (imckfs+1) / (imckdiv+1)) * fs */
179*b543e467SCyrille Pitchen static const struct atmel_i2s_gck_param gck_params[] = {
180*b543e467SCyrille Pitchen 	/* mck = 12.288MHz */
181*b543e467SCyrille Pitchen 	{  8000, I2S_MCK_12M288, 0, 47},	/* mck = 1536 fs */
182*b543e467SCyrille Pitchen 	{ 16000, I2S_MCK_12M288, 1, 47},	/* mck =  768 fs */
183*b543e467SCyrille Pitchen 	{ 24000, I2S_MCK_12M288, 3, 63},	/* mck =  512 fs */
184*b543e467SCyrille Pitchen 	{ 32000, I2S_MCK_12M288, 3, 47},	/* mck =  384 fs */
185*b543e467SCyrille Pitchen 	{ 48000, I2S_MCK_12M288, 7, 63},	/* mck =  256 fs */
186*b543e467SCyrille Pitchen 	{ 64000, I2S_MCK_12M288, 7, 47},	/* mck =  192 fs */
187*b543e467SCyrille Pitchen 	{ 96000, I2S_MCK_12M288, 7, 31},	/* mck =  128 fs */
188*b543e467SCyrille Pitchen 	{192000, I2S_MCK_12M288, 7, 15},	/* mck =   64 fs */
189*b543e467SCyrille Pitchen 
190*b543e467SCyrille Pitchen 	/* mck = 11.2896MHz */
191*b543e467SCyrille Pitchen 	{ 11025, I2S_MCK_11M2896, 1, 63},	/* mck = 1024 fs */
192*b543e467SCyrille Pitchen 	{ 22050, I2S_MCK_11M2896, 3, 63},	/* mck =  512 fs */
193*b543e467SCyrille Pitchen 	{ 44100, I2S_MCK_11M2896, 7, 63},	/* mck =  256 fs */
194*b543e467SCyrille Pitchen 	{ 88200, I2S_MCK_11M2896, 7, 31},	/* mck =  128 fs */
195*b543e467SCyrille Pitchen 	{176400, I2S_MCK_11M2896, 7, 15},	/* mck =   64 fs */
196*b543e467SCyrille Pitchen };
197*b543e467SCyrille Pitchen 
198*b543e467SCyrille Pitchen struct atmel_i2s_dev;
199*b543e467SCyrille Pitchen 
200*b543e467SCyrille Pitchen struct atmel_i2s_caps {
201*b543e467SCyrille Pitchen 	int	(*mck_init)(struct atmel_i2s_dev *, struct device_node *np);
202*b543e467SCyrille Pitchen };
203*b543e467SCyrille Pitchen 
204*b543e467SCyrille Pitchen struct atmel_i2s_dev {
205*b543e467SCyrille Pitchen 	struct device				*dev;
206*b543e467SCyrille Pitchen 	struct regmap				*regmap;
207*b543e467SCyrille Pitchen 	struct clk				*pclk;
208*b543e467SCyrille Pitchen 	struct clk				*gclk;
209*b543e467SCyrille Pitchen 	struct clk				*aclk;
210*b543e467SCyrille Pitchen 	struct snd_dmaengine_dai_dma_data	playback;
211*b543e467SCyrille Pitchen 	struct snd_dmaengine_dai_dma_data	capture;
212*b543e467SCyrille Pitchen 	unsigned int				fmt;
213*b543e467SCyrille Pitchen 	const struct atmel_i2s_gck_param	*gck_param;
214*b543e467SCyrille Pitchen 	const struct atmel_i2s_caps		*caps;
215*b543e467SCyrille Pitchen };
216*b543e467SCyrille Pitchen 
217*b543e467SCyrille Pitchen static irqreturn_t atmel_i2s_interrupt(int irq, void *dev_id)
218*b543e467SCyrille Pitchen {
219*b543e467SCyrille Pitchen 	struct atmel_i2s_dev *dev = dev_id;
220*b543e467SCyrille Pitchen 	unsigned int sr, imr, pending, ch, mask;
221*b543e467SCyrille Pitchen 	irqreturn_t ret = IRQ_NONE;
222*b543e467SCyrille Pitchen 
223*b543e467SCyrille Pitchen 	regmap_read(dev->regmap, ATMEL_I2SC_SR, &sr);
224*b543e467SCyrille Pitchen 	regmap_read(dev->regmap, ATMEL_I2SC_IMR, &imr);
225*b543e467SCyrille Pitchen 	pending = sr & imr;
226*b543e467SCyrille Pitchen 
227*b543e467SCyrille Pitchen 	if (!pending)
228*b543e467SCyrille Pitchen 		return IRQ_NONE;
229*b543e467SCyrille Pitchen 
230*b543e467SCyrille Pitchen 	if (pending & ATMEL_I2SC_INT_RXOR) {
231*b543e467SCyrille Pitchen 		mask = ATMEL_I2SC_SR_RXOR;
232*b543e467SCyrille Pitchen 
233*b543e467SCyrille Pitchen 		for (ch = 0; ch < ATMEL_I2SC_MAX_TDM_CHANNELS; ++ch) {
234*b543e467SCyrille Pitchen 			if (sr & ATMEL_I2SC_SR_RXORCH(ch)) {
235*b543e467SCyrille Pitchen 				mask |= ATMEL_I2SC_SR_RXORCH(ch);
236*b543e467SCyrille Pitchen 				dev_err(dev->dev,
237*b543e467SCyrille Pitchen 					"RX overrun on channel %d\n", ch);
238*b543e467SCyrille Pitchen 			}
239*b543e467SCyrille Pitchen 		}
240*b543e467SCyrille Pitchen 		regmap_write(dev->regmap, ATMEL_I2SC_SCR, mask);
241*b543e467SCyrille Pitchen 		ret = IRQ_HANDLED;
242*b543e467SCyrille Pitchen 	}
243*b543e467SCyrille Pitchen 
244*b543e467SCyrille Pitchen 	if (pending & ATMEL_I2SC_INT_TXUR) {
245*b543e467SCyrille Pitchen 		mask = ATMEL_I2SC_SR_TXUR;
246*b543e467SCyrille Pitchen 
247*b543e467SCyrille Pitchen 		for (ch = 0; ch < ATMEL_I2SC_MAX_TDM_CHANNELS; ++ch) {
248*b543e467SCyrille Pitchen 			if (sr & ATMEL_I2SC_SR_TXURCH(ch)) {
249*b543e467SCyrille Pitchen 				mask |= ATMEL_I2SC_SR_TXURCH(ch);
250*b543e467SCyrille Pitchen 				dev_err(dev->dev,
251*b543e467SCyrille Pitchen 					"TX underrun on channel %d\n", ch);
252*b543e467SCyrille Pitchen 			}
253*b543e467SCyrille Pitchen 		}
254*b543e467SCyrille Pitchen 		regmap_write(dev->regmap, ATMEL_I2SC_SCR, mask);
255*b543e467SCyrille Pitchen 		ret = IRQ_HANDLED;
256*b543e467SCyrille Pitchen 	}
257*b543e467SCyrille Pitchen 
258*b543e467SCyrille Pitchen 	return ret;
259*b543e467SCyrille Pitchen }
260*b543e467SCyrille Pitchen 
261*b543e467SCyrille Pitchen #define ATMEL_I2S_RATES		SNDRV_PCM_RATE_8000_192000
262*b543e467SCyrille Pitchen 
263*b543e467SCyrille Pitchen #define ATMEL_I2S_FORMATS	(SNDRV_PCM_FMTBIT_S8 |		\
264*b543e467SCyrille Pitchen 				 SNDRV_PCM_FMTBIT_S16_LE |	\
265*b543e467SCyrille Pitchen 				 SNDRV_PCM_FMTBIT_S18_3LE |	\
266*b543e467SCyrille Pitchen 				 SNDRV_PCM_FMTBIT_S20_3LE |	\
267*b543e467SCyrille Pitchen 				 SNDRV_PCM_FMTBIT_S24_3LE |	\
268*b543e467SCyrille Pitchen 				 SNDRV_PCM_FMTBIT_S24_LE |	\
269*b543e467SCyrille Pitchen 				 SNDRV_PCM_FMTBIT_S32_LE)
270*b543e467SCyrille Pitchen 
271*b543e467SCyrille Pitchen static int atmel_i2s_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
272*b543e467SCyrille Pitchen {
273*b543e467SCyrille Pitchen 	struct atmel_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
274*b543e467SCyrille Pitchen 
275*b543e467SCyrille Pitchen 	dev->fmt = fmt;
276*b543e467SCyrille Pitchen 	return 0;
277*b543e467SCyrille Pitchen }
278*b543e467SCyrille Pitchen 
279*b543e467SCyrille Pitchen static int atmel_i2s_prepare(struct snd_pcm_substream *substream,
280*b543e467SCyrille Pitchen 			     struct snd_soc_dai *dai)
281*b543e467SCyrille Pitchen {
282*b543e467SCyrille Pitchen 	struct atmel_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
283*b543e467SCyrille Pitchen 	bool is_playback = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
284*b543e467SCyrille Pitchen 	unsigned int rhr, sr = 0;
285*b543e467SCyrille Pitchen 
286*b543e467SCyrille Pitchen 	if (is_playback) {
287*b543e467SCyrille Pitchen 		regmap_read(dev->regmap, ATMEL_I2SC_SR, &sr);
288*b543e467SCyrille Pitchen 		if (sr & ATMEL_I2SC_SR_RXRDY) {
289*b543e467SCyrille Pitchen 			/*
290*b543e467SCyrille Pitchen 			 * The RX Ready flag should not be set. However if here,
291*b543e467SCyrille Pitchen 			 * we flush (read) the Receive Holding Register to start
292*b543e467SCyrille Pitchen 			 * from a clean state.
293*b543e467SCyrille Pitchen 			 */
294*b543e467SCyrille Pitchen 			dev_dbg(dev->dev, "RXRDY is set\n");
295*b543e467SCyrille Pitchen 			regmap_read(dev->regmap, ATMEL_I2SC_RHR, &rhr);
296*b543e467SCyrille Pitchen 		}
297*b543e467SCyrille Pitchen 	}
298*b543e467SCyrille Pitchen 
299*b543e467SCyrille Pitchen 	return 0;
300*b543e467SCyrille Pitchen }
301*b543e467SCyrille Pitchen 
302*b543e467SCyrille Pitchen static int atmel_i2s_get_gck_param(struct atmel_i2s_dev *dev, int fs)
303*b543e467SCyrille Pitchen {
304*b543e467SCyrille Pitchen 	int i, best;
305*b543e467SCyrille Pitchen 
306*b543e467SCyrille Pitchen 	if (!dev->gclk || !dev->aclk) {
307*b543e467SCyrille Pitchen 		dev_err(dev->dev, "cannot generate the I2S Master Clock\n");
308*b543e467SCyrille Pitchen 		return -EINVAL;
309*b543e467SCyrille Pitchen 	}
310*b543e467SCyrille Pitchen 
311*b543e467SCyrille Pitchen 	/*
312*b543e467SCyrille Pitchen 	 * Find the best possible settings to generate the I2S Master Clock
313*b543e467SCyrille Pitchen 	 * from the PLL Audio.
314*b543e467SCyrille Pitchen 	 */
315*b543e467SCyrille Pitchen 	dev->gck_param = NULL;
316*b543e467SCyrille Pitchen 	best = INT_MAX;
317*b543e467SCyrille Pitchen 	for (i = 0; i < ARRAY_SIZE(gck_params); ++i) {
318*b543e467SCyrille Pitchen 		const struct atmel_i2s_gck_param *gck_param = &gck_params[i];
319*b543e467SCyrille Pitchen 		int val = abs(fs - gck_param->fs);
320*b543e467SCyrille Pitchen 
321*b543e467SCyrille Pitchen 		if (val < best) {
322*b543e467SCyrille Pitchen 			best = val;
323*b543e467SCyrille Pitchen 			dev->gck_param = gck_param;
324*b543e467SCyrille Pitchen 		}
325*b543e467SCyrille Pitchen 	}
326*b543e467SCyrille Pitchen 
327*b543e467SCyrille Pitchen 	return 0;
328*b543e467SCyrille Pitchen }
329*b543e467SCyrille Pitchen 
330*b543e467SCyrille Pitchen static int atmel_i2s_hw_params(struct snd_pcm_substream *substream,
331*b543e467SCyrille Pitchen 			       struct snd_pcm_hw_params *params,
332*b543e467SCyrille Pitchen 			       struct snd_soc_dai *dai)
333*b543e467SCyrille Pitchen {
334*b543e467SCyrille Pitchen 	struct atmel_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
335*b543e467SCyrille Pitchen 	bool is_playback = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
336*b543e467SCyrille Pitchen 	unsigned int mr = 0;
337*b543e467SCyrille Pitchen 	int ret;
338*b543e467SCyrille Pitchen 
339*b543e467SCyrille Pitchen 	switch (dev->fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
340*b543e467SCyrille Pitchen 	case SND_SOC_DAIFMT_I2S:
341*b543e467SCyrille Pitchen 		mr |= ATMEL_I2SC_MR_FORMAT_I2S;
342*b543e467SCyrille Pitchen 		break;
343*b543e467SCyrille Pitchen 
344*b543e467SCyrille Pitchen 	default:
345*b543e467SCyrille Pitchen 		dev_err(dev->dev, "unsupported bus format\n");
346*b543e467SCyrille Pitchen 		return -EINVAL;
347*b543e467SCyrille Pitchen 	}
348*b543e467SCyrille Pitchen 
349*b543e467SCyrille Pitchen 	switch (dev->fmt & SND_SOC_DAIFMT_MASTER_MASK) {
350*b543e467SCyrille Pitchen 	case SND_SOC_DAIFMT_CBS_CFS:
351*b543e467SCyrille Pitchen 		/* codec is slave, so cpu is master */
352*b543e467SCyrille Pitchen 		mr |= ATMEL_I2SC_MR_MODE_MASTER;
353*b543e467SCyrille Pitchen 		ret = atmel_i2s_get_gck_param(dev, params_rate(params));
354*b543e467SCyrille Pitchen 		if (ret)
355*b543e467SCyrille Pitchen 			return ret;
356*b543e467SCyrille Pitchen 		break;
357*b543e467SCyrille Pitchen 
358*b543e467SCyrille Pitchen 	case SND_SOC_DAIFMT_CBM_CFM:
359*b543e467SCyrille Pitchen 		/* codec is master, so cpu is slave */
360*b543e467SCyrille Pitchen 		mr |= ATMEL_I2SC_MR_MODE_SLAVE;
361*b543e467SCyrille Pitchen 		dev->gck_param = NULL;
362*b543e467SCyrille Pitchen 		break;
363*b543e467SCyrille Pitchen 
364*b543e467SCyrille Pitchen 	default:
365*b543e467SCyrille Pitchen 		dev_err(dev->dev, "unsupported master/slave mode\n");
366*b543e467SCyrille Pitchen 		return -EINVAL;
367*b543e467SCyrille Pitchen 	}
368*b543e467SCyrille Pitchen 
369*b543e467SCyrille Pitchen 	switch (params_channels(params)) {
370*b543e467SCyrille Pitchen 	case 1:
371*b543e467SCyrille Pitchen 		if (is_playback)
372*b543e467SCyrille Pitchen 			mr |= ATMEL_I2SC_MR_TXMONO;
373*b543e467SCyrille Pitchen 		else
374*b543e467SCyrille Pitchen 			mr |= ATMEL_I2SC_MR_RXMONO;
375*b543e467SCyrille Pitchen 		break;
376*b543e467SCyrille Pitchen 	case 2:
377*b543e467SCyrille Pitchen 		break;
378*b543e467SCyrille Pitchen 	default:
379*b543e467SCyrille Pitchen 		dev_err(dev->dev, "unsupported number of audio channels\n");
380*b543e467SCyrille Pitchen 		return -EINVAL;
381*b543e467SCyrille Pitchen 	}
382*b543e467SCyrille Pitchen 
383*b543e467SCyrille Pitchen 	switch (params_format(params)) {
384*b543e467SCyrille Pitchen 	case SNDRV_PCM_FORMAT_S8:
385*b543e467SCyrille Pitchen 		mr |= ATMEL_I2SC_MR_DATALENGTH_8_BITS;
386*b543e467SCyrille Pitchen 		break;
387*b543e467SCyrille Pitchen 
388*b543e467SCyrille Pitchen 	case SNDRV_PCM_FORMAT_S16_LE:
389*b543e467SCyrille Pitchen 		mr |= ATMEL_I2SC_MR_DATALENGTH_16_BITS;
390*b543e467SCyrille Pitchen 		break;
391*b543e467SCyrille Pitchen 
392*b543e467SCyrille Pitchen 	case SNDRV_PCM_FORMAT_S18_3LE:
393*b543e467SCyrille Pitchen 		mr |= ATMEL_I2SC_MR_DATALENGTH_18_BITS | ATMEL_I2SC_MR_IWS;
394*b543e467SCyrille Pitchen 		break;
395*b543e467SCyrille Pitchen 
396*b543e467SCyrille Pitchen 	case SNDRV_PCM_FORMAT_S20_3LE:
397*b543e467SCyrille Pitchen 		mr |= ATMEL_I2SC_MR_DATALENGTH_20_BITS | ATMEL_I2SC_MR_IWS;
398*b543e467SCyrille Pitchen 		break;
399*b543e467SCyrille Pitchen 
400*b543e467SCyrille Pitchen 	case SNDRV_PCM_FORMAT_S24_3LE:
401*b543e467SCyrille Pitchen 		mr |= ATMEL_I2SC_MR_DATALENGTH_24_BITS | ATMEL_I2SC_MR_IWS;
402*b543e467SCyrille Pitchen 		break;
403*b543e467SCyrille Pitchen 
404*b543e467SCyrille Pitchen 	case SNDRV_PCM_FORMAT_S24_LE:
405*b543e467SCyrille Pitchen 		mr |= ATMEL_I2SC_MR_DATALENGTH_24_BITS;
406*b543e467SCyrille Pitchen 		break;
407*b543e467SCyrille Pitchen 
408*b543e467SCyrille Pitchen 	case SNDRV_PCM_FORMAT_S32_LE:
409*b543e467SCyrille Pitchen 		mr |= ATMEL_I2SC_MR_DATALENGTH_32_BITS;
410*b543e467SCyrille Pitchen 		break;
411*b543e467SCyrille Pitchen 
412*b543e467SCyrille Pitchen 	default:
413*b543e467SCyrille Pitchen 		dev_err(dev->dev, "unsupported size/endianness for audio samples\n");
414*b543e467SCyrille Pitchen 		return -EINVAL;
415*b543e467SCyrille Pitchen 	}
416*b543e467SCyrille Pitchen 
417*b543e467SCyrille Pitchen 	return regmap_write(dev->regmap, ATMEL_I2SC_MR, mr);
418*b543e467SCyrille Pitchen }
419*b543e467SCyrille Pitchen 
420*b543e467SCyrille Pitchen static int atmel_i2s_switch_mck_generator(struct atmel_i2s_dev *dev,
421*b543e467SCyrille Pitchen 					  bool enabled)
422*b543e467SCyrille Pitchen {
423*b543e467SCyrille Pitchen 	unsigned int mr, mr_mask;
424*b543e467SCyrille Pitchen 	unsigned long aclk_rate;
425*b543e467SCyrille Pitchen 	int ret;
426*b543e467SCyrille Pitchen 
427*b543e467SCyrille Pitchen 	mr = 0;
428*b543e467SCyrille Pitchen 	mr_mask = (ATMEL_I2SC_MR_IMCKDIV_MASK |
429*b543e467SCyrille Pitchen 		   ATMEL_I2SC_MR_IMCKFS_MASK |
430*b543e467SCyrille Pitchen 		   ATMEL_I2SC_MR_IMCKMODE_MASK);
431*b543e467SCyrille Pitchen 
432*b543e467SCyrille Pitchen 	if (!enabled) {
433*b543e467SCyrille Pitchen 		/* Disable the I2S Master Clock generator. */
434*b543e467SCyrille Pitchen 		ret = regmap_write(dev->regmap, ATMEL_I2SC_CR,
435*b543e467SCyrille Pitchen 				   ATMEL_I2SC_CR_CKDIS);
436*b543e467SCyrille Pitchen 		if (ret)
437*b543e467SCyrille Pitchen 			return ret;
438*b543e467SCyrille Pitchen 
439*b543e467SCyrille Pitchen 		/* Reset the I2S Master Clock generator settings. */
440*b543e467SCyrille Pitchen 		ret = regmap_update_bits(dev->regmap, ATMEL_I2SC_MR,
441*b543e467SCyrille Pitchen 					 mr_mask, mr);
442*b543e467SCyrille Pitchen 		if (ret)
443*b543e467SCyrille Pitchen 			return ret;
444*b543e467SCyrille Pitchen 
445*b543e467SCyrille Pitchen 		/* Disable/unprepare the PMC generated clock. */
446*b543e467SCyrille Pitchen 		clk_disable_unprepare(dev->gclk);
447*b543e467SCyrille Pitchen 
448*b543e467SCyrille Pitchen 		/* Disable/unprepare the PLL audio clock. */
449*b543e467SCyrille Pitchen 		clk_disable_unprepare(dev->aclk);
450*b543e467SCyrille Pitchen 		return 0;
451*b543e467SCyrille Pitchen 	}
452*b543e467SCyrille Pitchen 
453*b543e467SCyrille Pitchen 	if (!dev->gck_param)
454*b543e467SCyrille Pitchen 		return -EINVAL;
455*b543e467SCyrille Pitchen 
456*b543e467SCyrille Pitchen 	aclk_rate = dev->gck_param->mck * (dev->gck_param->imckdiv + 1);
457*b543e467SCyrille Pitchen 
458*b543e467SCyrille Pitchen 	/* Fist change the PLL audio clock frequency ... */
459*b543e467SCyrille Pitchen 	ret = clk_set_rate(dev->aclk, aclk_rate);
460*b543e467SCyrille Pitchen 	if (ret)
461*b543e467SCyrille Pitchen 		return ret;
462*b543e467SCyrille Pitchen 
463*b543e467SCyrille Pitchen 	/*
464*b543e467SCyrille Pitchen 	 * ... then set the PMC generated clock rate to the very same frequency
465*b543e467SCyrille Pitchen 	 * to set the gclk parent to aclk.
466*b543e467SCyrille Pitchen 	 */
467*b543e467SCyrille Pitchen 	ret = clk_set_rate(dev->gclk, aclk_rate);
468*b543e467SCyrille Pitchen 	if (ret)
469*b543e467SCyrille Pitchen 		return ret;
470*b543e467SCyrille Pitchen 
471*b543e467SCyrille Pitchen 	/* Prepare and enable the PLL audio clock first ... */
472*b543e467SCyrille Pitchen 	ret = clk_prepare_enable(dev->aclk);
473*b543e467SCyrille Pitchen 	if (ret)
474*b543e467SCyrille Pitchen 		return ret;
475*b543e467SCyrille Pitchen 
476*b543e467SCyrille Pitchen 	/* ... then prepare and enable the PMC generated clock. */
477*b543e467SCyrille Pitchen 	ret = clk_prepare_enable(dev->gclk);
478*b543e467SCyrille Pitchen 	if (ret)
479*b543e467SCyrille Pitchen 		return ret;
480*b543e467SCyrille Pitchen 
481*b543e467SCyrille Pitchen 	/* Update the Mode Register to generate the I2S Master Clock. */
482*b543e467SCyrille Pitchen 	mr |= ATMEL_I2SC_MR_IMCKDIV(dev->gck_param->imckdiv);
483*b543e467SCyrille Pitchen 	mr |= ATMEL_I2SC_MR_IMCKFS(dev->gck_param->imckfs);
484*b543e467SCyrille Pitchen 	mr |= ATMEL_I2SC_MR_IMCKMODE_I2SMCK;
485*b543e467SCyrille Pitchen 	ret = regmap_update_bits(dev->regmap, ATMEL_I2SC_MR, mr_mask, mr);
486*b543e467SCyrille Pitchen 	if (ret)
487*b543e467SCyrille Pitchen 		return ret;
488*b543e467SCyrille Pitchen 
489*b543e467SCyrille Pitchen 	/* Finally enable the I2S Master Clock generator. */
490*b543e467SCyrille Pitchen 	return regmap_write(dev->regmap, ATMEL_I2SC_CR,
491*b543e467SCyrille Pitchen 			    ATMEL_I2SC_CR_CKEN);
492*b543e467SCyrille Pitchen }
493*b543e467SCyrille Pitchen 
494*b543e467SCyrille Pitchen static int atmel_i2s_trigger(struct snd_pcm_substream *substream, int cmd,
495*b543e467SCyrille Pitchen 			     struct snd_soc_dai *dai)
496*b543e467SCyrille Pitchen {
497*b543e467SCyrille Pitchen 	struct atmel_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
498*b543e467SCyrille Pitchen 	bool is_playback = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
499*b543e467SCyrille Pitchen 	bool is_master, mck_enabled;
500*b543e467SCyrille Pitchen 	unsigned int cr, mr;
501*b543e467SCyrille Pitchen 	int err;
502*b543e467SCyrille Pitchen 
503*b543e467SCyrille Pitchen 	switch (cmd) {
504*b543e467SCyrille Pitchen 	case SNDRV_PCM_TRIGGER_START:
505*b543e467SCyrille Pitchen 	case SNDRV_PCM_TRIGGER_RESUME:
506*b543e467SCyrille Pitchen 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
507*b543e467SCyrille Pitchen 		cr = is_playback ? ATMEL_I2SC_CR_TXEN : ATMEL_I2SC_CR_RXEN;
508*b543e467SCyrille Pitchen 		mck_enabled = true;
509*b543e467SCyrille Pitchen 		break;
510*b543e467SCyrille Pitchen 	case SNDRV_PCM_TRIGGER_STOP:
511*b543e467SCyrille Pitchen 	case SNDRV_PCM_TRIGGER_SUSPEND:
512*b543e467SCyrille Pitchen 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
513*b543e467SCyrille Pitchen 		cr = is_playback ? ATMEL_I2SC_CR_TXDIS : ATMEL_I2SC_CR_RXDIS;
514*b543e467SCyrille Pitchen 		mck_enabled = false;
515*b543e467SCyrille Pitchen 		break;
516*b543e467SCyrille Pitchen 	default:
517*b543e467SCyrille Pitchen 		return -EINVAL;
518*b543e467SCyrille Pitchen 	}
519*b543e467SCyrille Pitchen 
520*b543e467SCyrille Pitchen 	/* Read the Mode Register to retrieve the master/slave state. */
521*b543e467SCyrille Pitchen 	err = regmap_read(dev->regmap, ATMEL_I2SC_MR, &mr);
522*b543e467SCyrille Pitchen 	if (err)
523*b543e467SCyrille Pitchen 		return err;
524*b543e467SCyrille Pitchen 	is_master = (mr & ATMEL_I2SC_MR_MODE_MASK) == ATMEL_I2SC_MR_MODE_MASTER;
525*b543e467SCyrille Pitchen 
526*b543e467SCyrille Pitchen 	/* If master starts, enable the audio clock. */
527*b543e467SCyrille Pitchen 	if (is_master && mck_enabled)
528*b543e467SCyrille Pitchen 		err = atmel_i2s_switch_mck_generator(dev, true);
529*b543e467SCyrille Pitchen 	if (err)
530*b543e467SCyrille Pitchen 		return err;
531*b543e467SCyrille Pitchen 
532*b543e467SCyrille Pitchen 	err = regmap_write(dev->regmap, ATMEL_I2SC_CR, cr);
533*b543e467SCyrille Pitchen 	if (err)
534*b543e467SCyrille Pitchen 		return err;
535*b543e467SCyrille Pitchen 
536*b543e467SCyrille Pitchen 	/* If master stops, disable the audio clock. */
537*b543e467SCyrille Pitchen 	if (is_master && !mck_enabled)
538*b543e467SCyrille Pitchen 		err = atmel_i2s_switch_mck_generator(dev, false);
539*b543e467SCyrille Pitchen 
540*b543e467SCyrille Pitchen 	return err;
541*b543e467SCyrille Pitchen }
542*b543e467SCyrille Pitchen 
543*b543e467SCyrille Pitchen static const struct snd_soc_dai_ops atmel_i2s_dai_ops = {
544*b543e467SCyrille Pitchen 	.prepare	= atmel_i2s_prepare,
545*b543e467SCyrille Pitchen 	.trigger	= atmel_i2s_trigger,
546*b543e467SCyrille Pitchen 	.hw_params	= atmel_i2s_hw_params,
547*b543e467SCyrille Pitchen 	.set_fmt	= atmel_i2s_set_dai_fmt,
548*b543e467SCyrille Pitchen };
549*b543e467SCyrille Pitchen 
550*b543e467SCyrille Pitchen static int atmel_i2s_dai_probe(struct snd_soc_dai *dai)
551*b543e467SCyrille Pitchen {
552*b543e467SCyrille Pitchen 	struct atmel_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
553*b543e467SCyrille Pitchen 
554*b543e467SCyrille Pitchen 	snd_soc_dai_init_dma_data(dai, &dev->playback, &dev->capture);
555*b543e467SCyrille Pitchen 	return 0;
556*b543e467SCyrille Pitchen }
557*b543e467SCyrille Pitchen 
558*b543e467SCyrille Pitchen static struct snd_soc_dai_driver atmel_i2s_dai = {
559*b543e467SCyrille Pitchen 	.probe	= atmel_i2s_dai_probe,
560*b543e467SCyrille Pitchen 	.playback = {
561*b543e467SCyrille Pitchen 		.channels_min = 1,
562*b543e467SCyrille Pitchen 		.channels_max = 2,
563*b543e467SCyrille Pitchen 		.rates = ATMEL_I2S_RATES,
564*b543e467SCyrille Pitchen 		.formats = ATMEL_I2S_FORMATS,
565*b543e467SCyrille Pitchen 	},
566*b543e467SCyrille Pitchen 	.capture = {
567*b543e467SCyrille Pitchen 		.channels_min = 1,
568*b543e467SCyrille Pitchen 		.channels_max = 2,
569*b543e467SCyrille Pitchen 		.rates = ATMEL_I2S_RATES,
570*b543e467SCyrille Pitchen 		.formats = ATMEL_I2S_FORMATS,
571*b543e467SCyrille Pitchen 	},
572*b543e467SCyrille Pitchen 	.ops = &atmel_i2s_dai_ops,
573*b543e467SCyrille Pitchen 	.symmetric_rates = 1,
574*b543e467SCyrille Pitchen };
575*b543e467SCyrille Pitchen 
576*b543e467SCyrille Pitchen static const struct snd_soc_component_driver atmel_i2s_component = {
577*b543e467SCyrille Pitchen 	.name	= "atmel-i2s",
578*b543e467SCyrille Pitchen };
579*b543e467SCyrille Pitchen 
580*b543e467SCyrille Pitchen static int atmel_i2s_sama5d2_mck_init(struct atmel_i2s_dev *dev,
581*b543e467SCyrille Pitchen 				      struct device_node *np)
582*b543e467SCyrille Pitchen {
583*b543e467SCyrille Pitchen 	struct clk *muxclk;
584*b543e467SCyrille Pitchen 	int err;
585*b543e467SCyrille Pitchen 
586*b543e467SCyrille Pitchen 	if (!dev->gclk)
587*b543e467SCyrille Pitchen 		return 0;
588*b543e467SCyrille Pitchen 
589*b543e467SCyrille Pitchen 	/* muxclk is optional, so we return error for probe defer only */
590*b543e467SCyrille Pitchen 	muxclk = devm_clk_get(dev->dev, "muxclk");
591*b543e467SCyrille Pitchen 	if (IS_ERR(muxclk)) {
592*b543e467SCyrille Pitchen 		err = PTR_ERR(muxclk);
593*b543e467SCyrille Pitchen 		if (err == -EPROBE_DEFER)
594*b543e467SCyrille Pitchen 			return -EPROBE_DEFER;
595*b543e467SCyrille Pitchen 		dev_warn(dev->dev,
596*b543e467SCyrille Pitchen 			 "failed to get the I2S clock control: %d\n", err);
597*b543e467SCyrille Pitchen 		return 0;
598*b543e467SCyrille Pitchen 	}
599*b543e467SCyrille Pitchen 
600*b543e467SCyrille Pitchen 	return clk_set_parent(muxclk, dev->gclk);
601*b543e467SCyrille Pitchen }
602*b543e467SCyrille Pitchen 
603*b543e467SCyrille Pitchen static const struct atmel_i2s_caps atmel_i2s_sama5d2_caps = {
604*b543e467SCyrille Pitchen 	.mck_init = atmel_i2s_sama5d2_mck_init,
605*b543e467SCyrille Pitchen };
606*b543e467SCyrille Pitchen 
607*b543e467SCyrille Pitchen static const struct of_device_id atmel_i2s_dt_ids[] = {
608*b543e467SCyrille Pitchen 	{
609*b543e467SCyrille Pitchen 		.compatible = "atmel,sama5d2-i2s",
610*b543e467SCyrille Pitchen 		.data = (void *)&atmel_i2s_sama5d2_caps,
611*b543e467SCyrille Pitchen 	},
612*b543e467SCyrille Pitchen 
613*b543e467SCyrille Pitchen 	{ /* sentinel */ }
614*b543e467SCyrille Pitchen };
615*b543e467SCyrille Pitchen 
616*b543e467SCyrille Pitchen MODULE_DEVICE_TABLE(of, atmel_i2s_dt_ids);
617*b543e467SCyrille Pitchen 
618*b543e467SCyrille Pitchen static int atmel_i2s_probe(struct platform_device *pdev)
619*b543e467SCyrille Pitchen {
620*b543e467SCyrille Pitchen 	struct device_node *np = pdev->dev.of_node;
621*b543e467SCyrille Pitchen 	const struct of_device_id *match;
622*b543e467SCyrille Pitchen 	struct atmel_i2s_dev *dev;
623*b543e467SCyrille Pitchen 	struct resource *mem;
624*b543e467SCyrille Pitchen 	struct regmap *regmap;
625*b543e467SCyrille Pitchen 	void __iomem *base;
626*b543e467SCyrille Pitchen 	int irq;
627*b543e467SCyrille Pitchen 	int err = -ENXIO;
628*b543e467SCyrille Pitchen 	unsigned int pcm_flags = 0;
629*b543e467SCyrille Pitchen 	unsigned int version;
630*b543e467SCyrille Pitchen 
631*b543e467SCyrille Pitchen 	/* Get memory for driver data. */
632*b543e467SCyrille Pitchen 	dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
633*b543e467SCyrille Pitchen 	if (!dev)
634*b543e467SCyrille Pitchen 		return -ENOMEM;
635*b543e467SCyrille Pitchen 
636*b543e467SCyrille Pitchen 	/* Get hardware capabilities. */
637*b543e467SCyrille Pitchen 	match = of_match_node(atmel_i2s_dt_ids, np);
638*b543e467SCyrille Pitchen 	if (match)
639*b543e467SCyrille Pitchen 		dev->caps = match->data;
640*b543e467SCyrille Pitchen 
641*b543e467SCyrille Pitchen 	/* Map I/O registers. */
642*b543e467SCyrille Pitchen 	mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
643*b543e467SCyrille Pitchen 	base = devm_ioremap_resource(&pdev->dev, mem);
644*b543e467SCyrille Pitchen 	if (IS_ERR(base))
645*b543e467SCyrille Pitchen 		return PTR_ERR(base);
646*b543e467SCyrille Pitchen 
647*b543e467SCyrille Pitchen 	regmap = devm_regmap_init_mmio(&pdev->dev, base,
648*b543e467SCyrille Pitchen 				       &atmel_i2s_regmap_config);
649*b543e467SCyrille Pitchen 	if (IS_ERR(regmap))
650*b543e467SCyrille Pitchen 		return PTR_ERR(regmap);
651*b543e467SCyrille Pitchen 
652*b543e467SCyrille Pitchen 	/* Request IRQ. */
653*b543e467SCyrille Pitchen 	irq = platform_get_irq(pdev, 0);
654*b543e467SCyrille Pitchen 	if (irq < 0)
655*b543e467SCyrille Pitchen 		return irq;
656*b543e467SCyrille Pitchen 
657*b543e467SCyrille Pitchen 	err = devm_request_irq(&pdev->dev, irq, atmel_i2s_interrupt, 0,
658*b543e467SCyrille Pitchen 			       dev_name(&pdev->dev), dev);
659*b543e467SCyrille Pitchen 	if (err)
660*b543e467SCyrille Pitchen 		return err;
661*b543e467SCyrille Pitchen 
662*b543e467SCyrille Pitchen 	/* Get the peripheral clock. */
663*b543e467SCyrille Pitchen 	dev->pclk = devm_clk_get(&pdev->dev, "pclk");
664*b543e467SCyrille Pitchen 	if (IS_ERR(dev->pclk)) {
665*b543e467SCyrille Pitchen 		err = PTR_ERR(dev->pclk);
666*b543e467SCyrille Pitchen 		dev_err(&pdev->dev,
667*b543e467SCyrille Pitchen 			"failed to get the peripheral clock: %d\n", err);
668*b543e467SCyrille Pitchen 		return err;
669*b543e467SCyrille Pitchen 	}
670*b543e467SCyrille Pitchen 
671*b543e467SCyrille Pitchen 	/* Get audio clocks to generate the I2S Master Clock (I2S_MCK) */
672*b543e467SCyrille Pitchen 	dev->aclk = devm_clk_get(&pdev->dev, "aclk");
673*b543e467SCyrille Pitchen 	dev->gclk = devm_clk_get(&pdev->dev, "gclk");
674*b543e467SCyrille Pitchen 	if (IS_ERR(dev->aclk) && IS_ERR(dev->gclk)) {
675*b543e467SCyrille Pitchen 		if (PTR_ERR(dev->aclk) == -EPROBE_DEFER ||
676*b543e467SCyrille Pitchen 		    PTR_ERR(dev->gclk) == -EPROBE_DEFER)
677*b543e467SCyrille Pitchen 			return -EPROBE_DEFER;
678*b543e467SCyrille Pitchen 		/* Master Mode not supported */
679*b543e467SCyrille Pitchen 		dev->aclk = NULL;
680*b543e467SCyrille Pitchen 		dev->gclk = NULL;
681*b543e467SCyrille Pitchen 	} else if (IS_ERR(dev->gclk)) {
682*b543e467SCyrille Pitchen 		err = PTR_ERR(dev->gclk);
683*b543e467SCyrille Pitchen 		dev_err(&pdev->dev,
684*b543e467SCyrille Pitchen 			"failed to get the PMC generated clock: %d\n", err);
685*b543e467SCyrille Pitchen 		return err;
686*b543e467SCyrille Pitchen 	} else if (IS_ERR(dev->aclk)) {
687*b543e467SCyrille Pitchen 		err = PTR_ERR(dev->aclk);
688*b543e467SCyrille Pitchen 		dev_err(&pdev->dev,
689*b543e467SCyrille Pitchen 			"failed to get the PLL audio clock: %d\n", err);
690*b543e467SCyrille Pitchen 		return err;
691*b543e467SCyrille Pitchen 	}
692*b543e467SCyrille Pitchen 
693*b543e467SCyrille Pitchen 	dev->dev = &pdev->dev;
694*b543e467SCyrille Pitchen 	dev->regmap = regmap;
695*b543e467SCyrille Pitchen 	platform_set_drvdata(pdev, dev);
696*b543e467SCyrille Pitchen 
697*b543e467SCyrille Pitchen 	/* Do hardware specific settings to initialize I2S_MCK generator */
698*b543e467SCyrille Pitchen 	if (dev->caps && dev->caps->mck_init) {
699*b543e467SCyrille Pitchen 		err = dev->caps->mck_init(dev, np);
700*b543e467SCyrille Pitchen 		if (err)
701*b543e467SCyrille Pitchen 			return err;
702*b543e467SCyrille Pitchen 	}
703*b543e467SCyrille Pitchen 
704*b543e467SCyrille Pitchen 	/* Enable the peripheral clock. */
705*b543e467SCyrille Pitchen 	err = clk_prepare_enable(dev->pclk);
706*b543e467SCyrille Pitchen 	if (err)
707*b543e467SCyrille Pitchen 		return err;
708*b543e467SCyrille Pitchen 
709*b543e467SCyrille Pitchen 	/* Get IP version. */
710*b543e467SCyrille Pitchen 	regmap_read(dev->regmap, ATMEL_I2SC_VERSION, &version);
711*b543e467SCyrille Pitchen 	dev_info(&pdev->dev, "hw version: %#x\n", version);
712*b543e467SCyrille Pitchen 
713*b543e467SCyrille Pitchen 	/* Enable error interrupts. */
714*b543e467SCyrille Pitchen 	regmap_write(dev->regmap, ATMEL_I2SC_IER,
715*b543e467SCyrille Pitchen 		     ATMEL_I2SC_INT_RXOR | ATMEL_I2SC_INT_TXUR);
716*b543e467SCyrille Pitchen 
717*b543e467SCyrille Pitchen 	err = devm_snd_soc_register_component(&pdev->dev,
718*b543e467SCyrille Pitchen 					      &atmel_i2s_component,
719*b543e467SCyrille Pitchen 					      &atmel_i2s_dai, 1);
720*b543e467SCyrille Pitchen 	if (err) {
721*b543e467SCyrille Pitchen 		dev_err(&pdev->dev, "failed to register DAI: %d\n", err);
722*b543e467SCyrille Pitchen 		clk_disable_unprepare(dev->pclk);
723*b543e467SCyrille Pitchen 		return err;
724*b543e467SCyrille Pitchen 	}
725*b543e467SCyrille Pitchen 
726*b543e467SCyrille Pitchen 	/* Prepare DMA config. */
727*b543e467SCyrille Pitchen 	dev->playback.addr	= (dma_addr_t)mem->start + ATMEL_I2SC_THR;
728*b543e467SCyrille Pitchen 	dev->playback.maxburst	= 1;
729*b543e467SCyrille Pitchen 	dev->capture.addr	= (dma_addr_t)mem->start + ATMEL_I2SC_RHR;
730*b543e467SCyrille Pitchen 	dev->capture.maxburst	= 1;
731*b543e467SCyrille Pitchen 
732*b543e467SCyrille Pitchen 	if (of_property_match_string(np, "dma-names", "rx-tx") == 0)
733*b543e467SCyrille Pitchen 		pcm_flags |= SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX;
734*b543e467SCyrille Pitchen 	err = devm_snd_dmaengine_pcm_register(&pdev->dev, NULL, pcm_flags);
735*b543e467SCyrille Pitchen 	if (err) {
736*b543e467SCyrille Pitchen 		dev_err(&pdev->dev, "failed to register PCM: %d\n", err);
737*b543e467SCyrille Pitchen 		clk_disable_unprepare(dev->pclk);
738*b543e467SCyrille Pitchen 		return err;
739*b543e467SCyrille Pitchen 	}
740*b543e467SCyrille Pitchen 
741*b543e467SCyrille Pitchen 	return 0;
742*b543e467SCyrille Pitchen }
743*b543e467SCyrille Pitchen 
744*b543e467SCyrille Pitchen static int atmel_i2s_remove(struct platform_device *pdev)
745*b543e467SCyrille Pitchen {
746*b543e467SCyrille Pitchen 	struct atmel_i2s_dev *dev = platform_get_drvdata(pdev);
747*b543e467SCyrille Pitchen 
748*b543e467SCyrille Pitchen 	clk_disable_unprepare(dev->pclk);
749*b543e467SCyrille Pitchen 
750*b543e467SCyrille Pitchen 	return 0;
751*b543e467SCyrille Pitchen }
752*b543e467SCyrille Pitchen 
753*b543e467SCyrille Pitchen static struct platform_driver atmel_i2s_driver = {
754*b543e467SCyrille Pitchen 	.driver		= {
755*b543e467SCyrille Pitchen 		.name	= "atmel_i2s",
756*b543e467SCyrille Pitchen 		.of_match_table	= of_match_ptr(atmel_i2s_dt_ids),
757*b543e467SCyrille Pitchen 	},
758*b543e467SCyrille Pitchen 	.probe		= atmel_i2s_probe,
759*b543e467SCyrille Pitchen 	.remove		= atmel_i2s_remove,
760*b543e467SCyrille Pitchen };
761*b543e467SCyrille Pitchen module_platform_driver(atmel_i2s_driver);
762*b543e467SCyrille Pitchen 
763*b543e467SCyrille Pitchen MODULE_DESCRIPTION("Atmel I2S Controller driver");
764*b543e467SCyrille Pitchen MODULE_AUTHOR("Cyrille Pitchen <cyrille.pitchen@atmel.com>");
765*b543e467SCyrille Pitchen MODULE_LICENSE("GPL v2");
766