1/*
2 * Copyright (C) STMicroelectronics SA 2015
3 * Authors: Arnaud Pouliquen <arnaud.pouliquen@st.com>
4 *          for STMicroelectronics.
5 * License terms:  GNU General Public License (GPL), version 2
6 */
7
8#include <linux/clk.h>
9#include <linux/delay.h>
10#include <linux/io.h>
11
12#include <sound/soc.h>
13
14#include "uniperif.h"
15
16/*
17 * Note: snd_pcm_hardware is linked to DMA controller but is declared here to
18 * integrate unireader capability in term of rate and supported channels
19 */
20static const struct snd_pcm_hardware uni_reader_pcm_hw = {
21	.info = SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER |
22		SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_MMAP |
23		SNDRV_PCM_INFO_MMAP_VALID,
24	.formats = SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S16_LE,
25
26	.rates = SNDRV_PCM_RATE_CONTINUOUS,
27	.rate_min = 8000,
28	.rate_max = 96000,
29
30	.channels_min = 2,
31	.channels_max = 8,
32
33	.periods_min = 2,
34	.periods_max = 48,
35
36	.period_bytes_min = 128,
37	.period_bytes_max = 64 * PAGE_SIZE,
38	.buffer_bytes_max = 256 * PAGE_SIZE
39};
40
41/*
42 * uni_reader_irq_handler
43 * In case of error audio stream is stopped; stop action is protected via PCM
44 * stream lock  to avoid race condition with trigger callback.
45 */
46static irqreturn_t uni_reader_irq_handler(int irq, void *dev_id)
47{
48	irqreturn_t ret = IRQ_NONE;
49	struct uniperif *reader = dev_id;
50	unsigned int status;
51
52	if (reader->state == UNIPERIF_STATE_STOPPED) {
53		/* Unexpected IRQ: do nothing */
54		dev_warn(reader->dev, "unexpected IRQ ");
55		return IRQ_HANDLED;
56	}
57
58	/* Get interrupt status & clear them immediately */
59	status = GET_UNIPERIF_ITS(reader);
60	SET_UNIPERIF_ITS_BCLR(reader, status);
61
62	/* Check for fifo overflow error */
63	if (unlikely(status & UNIPERIF_ITS_FIFO_ERROR_MASK(reader))) {
64		dev_err(reader->dev, "FIFO error detected");
65
66		snd_pcm_stream_lock(reader->substream);
67		snd_pcm_stop(reader->substream, SNDRV_PCM_STATE_XRUN);
68		snd_pcm_stream_unlock(reader->substream);
69
70		return IRQ_HANDLED;
71	}
72
73	return ret;
74}
75
76static int uni_reader_prepare(struct snd_pcm_substream *substream,
77			      struct snd_soc_dai *dai)
78{
79	struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
80	struct uniperif *reader = priv->dai_data.uni;
81	struct snd_pcm_runtime *runtime = substream->runtime;
82	int transfer_size, trigger_limit;
83	int slot_width;
84	int count = 10;
85
86	/* The reader should be stopped */
87	if (reader->state != UNIPERIF_STATE_STOPPED) {
88		dev_err(reader->dev, "%s: invalid reader state %d", __func__,
89			reader->state);
90		return -EINVAL;
91	}
92
93	/* Calculate transfer size (in fifo cells and bytes) for frame count */
94	transfer_size = runtime->channels * UNIPERIF_FIFO_FRAMES;
95
96	/* Calculate number of empty cells available before asserting DREQ */
97	if (reader->ver < SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0)
98		trigger_limit = UNIPERIF_FIFO_SIZE - transfer_size;
99	else
100		/*
101		 * Since SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0
102		 * FDMA_TRIGGER_LIMIT also controls when the state switches
103		 * from OFF or STANDBY to AUDIO DATA.
104		 */
105		trigger_limit = transfer_size;
106
107	/* Trigger limit must be an even number */
108	if ((!trigger_limit % 2) ||
109	    (trigger_limit != 1 && transfer_size % 2) ||
110	    (trigger_limit > UNIPERIF_CONFIG_DMA_TRIG_LIMIT_MASK(reader))) {
111		dev_err(reader->dev, "invalid trigger limit %d", trigger_limit);
112		return -EINVAL;
113	}
114
115	SET_UNIPERIF_CONFIG_DMA_TRIG_LIMIT(reader, trigger_limit);
116
117	switch (reader->daifmt & SND_SOC_DAIFMT_INV_MASK) {
118	case SND_SOC_DAIFMT_IB_IF:
119	case SND_SOC_DAIFMT_NB_IF:
120		SET_UNIPERIF_I2S_FMT_LR_POL_HIG(reader);
121		break;
122	default:
123		SET_UNIPERIF_I2S_FMT_LR_POL_LOW(reader);
124	}
125
126	/* Force slot width to 32 in I2S mode */
127	if ((reader->daifmt & SND_SOC_DAIFMT_FORMAT_MASK)
128		== SND_SOC_DAIFMT_I2S) {
129		slot_width = 32;
130	} else {
131		switch (runtime->format) {
132		case SNDRV_PCM_FORMAT_S16_LE:
133			slot_width = 16;
134			break;
135		default:
136			slot_width = 32;
137			break;
138		}
139	}
140
141	/* Number of bits per subframe (i.e one channel sample) on input. */
142	switch (slot_width) {
143	case 32:
144		SET_UNIPERIF_I2S_FMT_NBIT_32(reader);
145		SET_UNIPERIF_I2S_FMT_DATA_SIZE_32(reader);
146		break;
147	case 16:
148		SET_UNIPERIF_I2S_FMT_NBIT_16(reader);
149		SET_UNIPERIF_I2S_FMT_DATA_SIZE_16(reader);
150		break;
151	default:
152		dev_err(reader->dev, "subframe format not supported");
153		return -EINVAL;
154	}
155
156	/* Configure data memory format */
157	switch (runtime->format) {
158	case SNDRV_PCM_FORMAT_S16_LE:
159		/* One data word contains two samples */
160		SET_UNIPERIF_CONFIG_MEM_FMT_16_16(reader);
161		break;
162
163	case SNDRV_PCM_FORMAT_S32_LE:
164		/*
165		 * Actually "16 bits/0 bits" means "32/28/24/20/18/16 bits
166		 * on the MSB then zeros (if less than 32 bytes)"...
167		 */
168		SET_UNIPERIF_CONFIG_MEM_FMT_16_0(reader);
169		break;
170
171	default:
172		dev_err(reader->dev, "format not supported");
173		return -EINVAL;
174	}
175
176	switch (reader->daifmt & SND_SOC_DAIFMT_FORMAT_MASK) {
177	case SND_SOC_DAIFMT_I2S:
178		SET_UNIPERIF_I2S_FMT_ALIGN_LEFT(reader);
179		SET_UNIPERIF_I2S_FMT_PADDING_I2S_MODE(reader);
180		break;
181	case SND_SOC_DAIFMT_LEFT_J:
182		SET_UNIPERIF_I2S_FMT_ALIGN_LEFT(reader);
183		SET_UNIPERIF_I2S_FMT_PADDING_SONY_MODE(reader);
184		break;
185	case SND_SOC_DAIFMT_RIGHT_J:
186		SET_UNIPERIF_I2S_FMT_ALIGN_RIGHT(reader);
187		SET_UNIPERIF_I2S_FMT_PADDING_SONY_MODE(reader);
188		break;
189	default:
190		dev_err(reader->dev, "format not supported");
191		return -EINVAL;
192	}
193
194	SET_UNIPERIF_I2S_FMT_ORDER_MSB(reader);
195
196	/* Data clocking (changing) on the rising edge */
197	SET_UNIPERIF_I2S_FMT_SCLK_EDGE_RISING(reader);
198
199	/* Number of channels must be even */
200
201	if ((runtime->channels % 2) || (runtime->channels < 2) ||
202	    (runtime->channels > 10)) {
203		dev_err(reader->dev, "%s: invalid nb of channels", __func__);
204		return -EINVAL;
205	}
206
207	SET_UNIPERIF_I2S_FMT_NUM_CH(reader, runtime->channels / 2);
208
209	/* Clear any pending interrupts */
210	SET_UNIPERIF_ITS_BCLR(reader, GET_UNIPERIF_ITS(reader));
211
212	SET_UNIPERIF_I2S_FMT_NO_OF_SAMPLES_TO_READ(reader, 0);
213
214	/* Set the interrupt mask */
215	SET_UNIPERIF_ITM_BSET_DMA_ERROR(reader);
216	SET_UNIPERIF_ITM_BSET_FIFO_ERROR(reader);
217	SET_UNIPERIF_ITM_BSET_MEM_BLK_READ(reader);
218
219	/* Enable underflow recovery interrupts */
220	if (reader->info->underflow_enabled) {
221		SET_UNIPERIF_ITM_BSET_UNDERFLOW_REC_DONE(reader);
222		SET_UNIPERIF_ITM_BSET_UNDERFLOW_REC_FAILED(reader);
223	}
224
225	/* Reset uniperipheral reader */
226	SET_UNIPERIF_SOFT_RST_SOFT_RST(reader);
227
228	while (GET_UNIPERIF_SOFT_RST_SOFT_RST(reader)) {
229		udelay(5);
230		count--;
231	}
232	if (!count) {
233		dev_err(reader->dev, "Failed to reset uniperif");
234		return -EIO;
235	}
236
237	return 0;
238}
239
240static int uni_reader_start(struct uniperif *reader)
241{
242	/* The reader should be stopped */
243	if (reader->state != UNIPERIF_STATE_STOPPED) {
244		dev_err(reader->dev, "%s: invalid reader state", __func__);
245		return -EINVAL;
246	}
247
248	/* Enable reader interrupts (and clear possible stalled ones) */
249	SET_UNIPERIF_ITS_BCLR_FIFO_ERROR(reader);
250	SET_UNIPERIF_ITM_BSET_FIFO_ERROR(reader);
251
252	/* Launch the reader */
253	SET_UNIPERIF_CTRL_OPERATION_PCM_DATA(reader);
254
255	/* Update state to started */
256	reader->state = UNIPERIF_STATE_STARTED;
257	return 0;
258}
259
260static int uni_reader_stop(struct uniperif *reader)
261{
262	/* The reader should not be in stopped state */
263	if (reader->state == UNIPERIF_STATE_STOPPED) {
264		dev_err(reader->dev, "%s: invalid reader state", __func__);
265		return -EINVAL;
266	}
267
268	/* Turn the reader off */
269	SET_UNIPERIF_CTRL_OPERATION_OFF(reader);
270
271	/* Disable interrupts */
272	SET_UNIPERIF_ITM_BCLR(reader, GET_UNIPERIF_ITM(reader));
273
274	/* Update state to stopped and return */
275	reader->state = UNIPERIF_STATE_STOPPED;
276
277	return 0;
278}
279
280static int  uni_reader_trigger(struct snd_pcm_substream *substream,
281			       int cmd, struct snd_soc_dai *dai)
282{
283	struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
284	struct uniperif *reader = priv->dai_data.uni;
285
286	switch (cmd) {
287	case SNDRV_PCM_TRIGGER_START:
288		return  uni_reader_start(reader);
289	case SNDRV_PCM_TRIGGER_STOP:
290		return  uni_reader_stop(reader);
291	default:
292		return -EINVAL;
293	}
294}
295
296static void uni_reader_shutdown(struct snd_pcm_substream *substream,
297				struct snd_soc_dai *dai)
298{
299	struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
300	struct uniperif *reader = priv->dai_data.uni;
301
302	if (reader->state != UNIPERIF_STATE_STOPPED) {
303		/* Stop the reader */
304		uni_reader_stop(reader);
305	}
306}
307
308static int uni_reader_parse_dt(struct platform_device *pdev,
309			       struct uniperif *reader)
310{
311	struct uniperif_info *info;
312	struct device_node *node = pdev->dev.of_node;
313
314	/* Allocate memory for the info structure */
315	info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
316	if (!info)
317		return -ENOMEM;
318
319	if (of_property_read_u32(node, "st,version", &reader->ver) ||
320	    reader->ver == SND_ST_UNIPERIF_VERSION_UNKNOWN) {
321		dev_err(&pdev->dev, "Unknown uniperipheral version ");
322		return -EINVAL;
323	}
324
325	/* Save the info structure */
326	reader->info = info;
327
328	return 0;
329}
330
331static const struct snd_soc_dai_ops uni_reader_dai_ops = {
332		.shutdown = uni_reader_shutdown,
333		.prepare = uni_reader_prepare,
334		.trigger = uni_reader_trigger,
335		.hw_params = sti_uniperiph_dai_hw_params,
336		.set_fmt = sti_uniperiph_dai_set_fmt,
337};
338
339int uni_reader_init(struct platform_device *pdev,
340		    struct uniperif *reader)
341{
342	int ret = 0;
343
344	reader->dev = &pdev->dev;
345	reader->state = UNIPERIF_STATE_STOPPED;
346	reader->hw = &uni_reader_pcm_hw;
347	reader->dai_ops = &uni_reader_dai_ops;
348
349	ret = uni_reader_parse_dt(pdev, reader);
350	if (ret < 0) {
351		dev_err(reader->dev, "Failed to parse DeviceTree");
352		return ret;
353	}
354
355	ret = devm_request_irq(&pdev->dev, reader->irq,
356			       uni_reader_irq_handler, IRQF_SHARED,
357			       dev_name(&pdev->dev), reader);
358	if (ret < 0) {
359		dev_err(&pdev->dev, "Failed to request IRQ");
360		return -EBUSY;
361	}
362
363	return 0;
364}
365EXPORT_SYMBOL_GPL(uni_reader_init);
366