1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  sst_mfld_platform.c - Intel MID Platform driver
4  *
5  *  Copyright (C) 2010-2014 Intel Corp
6  *  Author: Vinod Koul <vinod.koul@intel.com>
7  *  Author: Harsha Priya <priya.harsha@intel.com>
8  *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9  *
10  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11  */
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 
14 #include <linux/slab.h>
15 #include <linux/io.h>
16 #include <linux/module.h>
17 #include <sound/core.h>
18 #include <sound/pcm.h>
19 #include <sound/pcm_params.h>
20 #include <sound/soc.h>
21 #include <sound/compress_driver.h>
22 #include <asm/platform_sst_audio.h>
23 #include "sst-mfld-platform.h"
24 #include "sst-atom-controls.h"
25 
26 struct sst_device *sst;
27 static DEFINE_MUTEX(sst_lock);
28 
29 int sst_register_dsp(struct sst_device *dev)
30 {
31 	if (WARN_ON(!dev))
32 		return -EINVAL;
33 	if (!try_module_get(dev->dev->driver->owner))
34 		return -ENODEV;
35 	mutex_lock(&sst_lock);
36 	if (sst) {
37 		dev_err(dev->dev, "we already have a device %s\n", sst->name);
38 		module_put(dev->dev->driver->owner);
39 		mutex_unlock(&sst_lock);
40 		return -EEXIST;
41 	}
42 	dev_dbg(dev->dev, "registering device %s\n", dev->name);
43 	sst = dev;
44 	mutex_unlock(&sst_lock);
45 	return 0;
46 }
47 EXPORT_SYMBOL_GPL(sst_register_dsp);
48 
49 int sst_unregister_dsp(struct sst_device *dev)
50 {
51 	if (WARN_ON(!dev))
52 		return -EINVAL;
53 	if (dev != sst)
54 		return -EINVAL;
55 
56 	mutex_lock(&sst_lock);
57 
58 	if (!sst) {
59 		mutex_unlock(&sst_lock);
60 		return -EIO;
61 	}
62 
63 	module_put(sst->dev->driver->owner);
64 	dev_dbg(dev->dev, "unreg %s\n", sst->name);
65 	sst = NULL;
66 	mutex_unlock(&sst_lock);
67 	return 0;
68 }
69 EXPORT_SYMBOL_GPL(sst_unregister_dsp);
70 
71 static const struct snd_pcm_hardware sst_platform_pcm_hw = {
72 	.info =	(SNDRV_PCM_INFO_INTERLEAVED |
73 			SNDRV_PCM_INFO_DOUBLE |
74 			SNDRV_PCM_INFO_PAUSE |
75 			SNDRV_PCM_INFO_RESUME |
76 			SNDRV_PCM_INFO_MMAP|
77 			SNDRV_PCM_INFO_MMAP_VALID |
78 			SNDRV_PCM_INFO_BLOCK_TRANSFER |
79 			SNDRV_PCM_INFO_SYNC_START),
80 	.buffer_bytes_max = SST_MAX_BUFFER,
81 	.period_bytes_min = SST_MIN_PERIOD_BYTES,
82 	.period_bytes_max = SST_MAX_PERIOD_BYTES,
83 	.periods_min = SST_MIN_PERIODS,
84 	.periods_max = SST_MAX_PERIODS,
85 	.fifo_size = SST_FIFO_SIZE,
86 };
87 
88 static struct sst_dev_stream_map dpcm_strm_map[] = {
89 	{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, /* Reserved, not in use */
90 	{MERR_DPCM_AUDIO, 0, SNDRV_PCM_STREAM_PLAYBACK, PIPE_MEDIA1_IN, SST_TASK_ID_MEDIA, 0},
91 	{MERR_DPCM_COMPR, 0, SNDRV_PCM_STREAM_PLAYBACK, PIPE_MEDIA0_IN, SST_TASK_ID_MEDIA, 0},
92 	{MERR_DPCM_AUDIO, 0, SNDRV_PCM_STREAM_CAPTURE, PIPE_PCM1_OUT, SST_TASK_ID_MEDIA, 0},
93 	{MERR_DPCM_DEEP_BUFFER, 0, SNDRV_PCM_STREAM_PLAYBACK, PIPE_MEDIA3_IN, SST_TASK_ID_MEDIA, 0},
94 };
95 
96 static int sst_media_digital_mute(struct snd_soc_dai *dai, int mute, int stream)
97 {
98 
99 	return sst_send_pipe_gains(dai, stream, mute);
100 }
101 
102 /* helper functions */
103 void sst_set_stream_status(struct sst_runtime_stream *stream,
104 					int state)
105 {
106 	unsigned long flags;
107 	spin_lock_irqsave(&stream->status_lock, flags);
108 	stream->stream_status = state;
109 	spin_unlock_irqrestore(&stream->status_lock, flags);
110 }
111 
112 static inline int sst_get_stream_status(struct sst_runtime_stream *stream)
113 {
114 	int state;
115 	unsigned long flags;
116 
117 	spin_lock_irqsave(&stream->status_lock, flags);
118 	state = stream->stream_status;
119 	spin_unlock_irqrestore(&stream->status_lock, flags);
120 	return state;
121 }
122 
123 static void sst_fill_alloc_params(struct snd_pcm_substream *substream,
124 				struct snd_sst_alloc_params_ext *alloc_param)
125 {
126 	unsigned int channels;
127 	snd_pcm_uframes_t period_size;
128 	ssize_t periodbytes;
129 	ssize_t buffer_bytes = snd_pcm_lib_buffer_bytes(substream);
130 	u32 buffer_addr = virt_to_phys(substream->dma_buffer.area);
131 
132 	channels = substream->runtime->channels;
133 	period_size = substream->runtime->period_size;
134 	periodbytes = samples_to_bytes(substream->runtime, period_size);
135 	alloc_param->ring_buf_info[0].addr = buffer_addr;
136 	alloc_param->ring_buf_info[0].size = buffer_bytes;
137 	alloc_param->sg_count = 1;
138 	alloc_param->reserved = 0;
139 	alloc_param->frag_size = periodbytes * channels;
140 
141 }
142 static void sst_fill_pcm_params(struct snd_pcm_substream *substream,
143 				struct snd_sst_stream_params *param)
144 {
145 	param->uc.pcm_params.num_chan = (u8) substream->runtime->channels;
146 	param->uc.pcm_params.pcm_wd_sz = substream->runtime->sample_bits;
147 	param->uc.pcm_params.sfreq = substream->runtime->rate;
148 
149 	/* PCM stream via ALSA interface */
150 	param->uc.pcm_params.use_offload_path = 0;
151 	param->uc.pcm_params.reserved2 = 0;
152 	memset(param->uc.pcm_params.channel_map, 0, sizeof(u8));
153 
154 }
155 
156 static int sst_get_stream_mapping(int dev, int sdev, int dir,
157 	struct sst_dev_stream_map *map, int size)
158 {
159 	int i;
160 
161 	if (map == NULL)
162 		return -EINVAL;
163 
164 
165 	/* index 0 is not used in stream map */
166 	for (i = 1; i < size; i++) {
167 		if ((map[i].dev_num == dev) && (map[i].direction == dir))
168 			return i;
169 	}
170 	return 0;
171 }
172 
173 int sst_fill_stream_params(void *substream,
174 	const struct sst_data *ctx, struct snd_sst_params *str_params, bool is_compress)
175 {
176 	int map_size;
177 	int index;
178 	struct sst_dev_stream_map *map;
179 	struct snd_pcm_substream *pstream = NULL;
180 	struct snd_compr_stream *cstream = NULL;
181 
182 	map = ctx->pdata->pdev_strm_map;
183 	map_size = ctx->pdata->strm_map_size;
184 
185 	if (is_compress)
186 		cstream = (struct snd_compr_stream *)substream;
187 	else
188 		pstream = (struct snd_pcm_substream *)substream;
189 
190 	str_params->stream_type = SST_STREAM_TYPE_MUSIC;
191 
192 	/* For pcm streams */
193 	if (pstream) {
194 		index = sst_get_stream_mapping(pstream->pcm->device,
195 					  pstream->number, pstream->stream,
196 					  map, map_size);
197 		if (index <= 0)
198 			return -EINVAL;
199 
200 		str_params->stream_id = index;
201 		str_params->device_type = map[index].device_id;
202 		str_params->task = map[index].task_id;
203 
204 		str_params->ops = (u8)pstream->stream;
205 	}
206 
207 	if (cstream) {
208 		index = sst_get_stream_mapping(cstream->device->device,
209 					       0, cstream->direction,
210 					       map, map_size);
211 		if (index <= 0)
212 			return -EINVAL;
213 		str_params->stream_id = index;
214 		str_params->device_type = map[index].device_id;
215 		str_params->task = map[index].task_id;
216 
217 		str_params->ops = (u8)cstream->direction;
218 	}
219 	return 0;
220 }
221 
222 static int sst_platform_alloc_stream(struct snd_pcm_substream *substream,
223 		struct snd_soc_dai *dai)
224 {
225 	struct sst_runtime_stream *stream =
226 			substream->runtime->private_data;
227 	struct snd_sst_stream_params param = {{{0,},},};
228 	struct snd_sst_params str_params = {0};
229 	struct snd_sst_alloc_params_ext alloc_params = {0};
230 	int ret_val = 0;
231 	struct sst_data *ctx = snd_soc_dai_get_drvdata(dai);
232 
233 	/* set codec params and inform SST driver the same */
234 	sst_fill_pcm_params(substream, &param);
235 	sst_fill_alloc_params(substream, &alloc_params);
236 	substream->runtime->dma_area = substream->dma_buffer.area;
237 	str_params.sparams = param;
238 	str_params.aparams = alloc_params;
239 	str_params.codec = SST_CODEC_TYPE_PCM;
240 
241 	/* fill the device type and stream id to pass to SST driver */
242 	ret_val = sst_fill_stream_params(substream, ctx, &str_params, false);
243 	if (ret_val < 0)
244 		return ret_val;
245 
246 	stream->stream_info.str_id = str_params.stream_id;
247 
248 	ret_val = stream->ops->open(sst->dev, &str_params);
249 	if (ret_val <= 0)
250 		return ret_val;
251 
252 
253 	return ret_val;
254 }
255 
256 static void sst_period_elapsed(void *arg)
257 {
258 	struct snd_pcm_substream *substream = arg;
259 	struct sst_runtime_stream *stream;
260 	int status;
261 
262 	if (!substream || !substream->runtime)
263 		return;
264 	stream = substream->runtime->private_data;
265 	if (!stream)
266 		return;
267 	status = sst_get_stream_status(stream);
268 	if (status != SST_PLATFORM_RUNNING)
269 		return;
270 	snd_pcm_period_elapsed(substream);
271 }
272 
273 static int sst_platform_init_stream(struct snd_pcm_substream *substream)
274 {
275 	struct sst_runtime_stream *stream =
276 			substream->runtime->private_data;
277 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
278 	int ret_val;
279 
280 	dev_dbg(rtd->dev, "setting buffer ptr param\n");
281 	sst_set_stream_status(stream, SST_PLATFORM_INIT);
282 	stream->stream_info.period_elapsed = sst_period_elapsed;
283 	stream->stream_info.arg = substream;
284 	stream->stream_info.buffer_ptr = 0;
285 	stream->stream_info.sfreq = substream->runtime->rate;
286 	ret_val = stream->ops->stream_init(sst->dev, &stream->stream_info);
287 	if (ret_val)
288 		dev_err(rtd->dev, "control_set ret error %d\n", ret_val);
289 	return ret_val;
290 
291 }
292 
293 static int power_up_sst(struct sst_runtime_stream *stream)
294 {
295 	return stream->ops->power(sst->dev, true);
296 }
297 
298 static void power_down_sst(struct sst_runtime_stream *stream)
299 {
300 	stream->ops->power(sst->dev, false);
301 }
302 
303 static int sst_media_open(struct snd_pcm_substream *substream,
304 		struct snd_soc_dai *dai)
305 {
306 	int ret_val = 0;
307 	struct snd_pcm_runtime *runtime = substream->runtime;
308 	struct sst_runtime_stream *stream;
309 
310 	stream = kzalloc(sizeof(*stream), GFP_KERNEL);
311 	if (!stream)
312 		return -ENOMEM;
313 	spin_lock_init(&stream->status_lock);
314 
315 	/* get the sst ops */
316 	mutex_lock(&sst_lock);
317 	if (!sst ||
318 	    !try_module_get(sst->dev->driver->owner)) {
319 		dev_err(dai->dev, "no device available to run\n");
320 		ret_val = -ENODEV;
321 		goto out_ops;
322 	}
323 	stream->ops = sst->ops;
324 	mutex_unlock(&sst_lock);
325 
326 	stream->stream_info.str_id = 0;
327 
328 	stream->stream_info.arg = substream;
329 	/* allocate memory for SST API set */
330 	runtime->private_data = stream;
331 
332 	ret_val = power_up_sst(stream);
333 	if (ret_val < 0)
334 		goto out_power_up;
335 
336 	/*
337 	 * Make sure the period to be multiple of 1ms to align the
338 	 * design of firmware. Apply same rule to buffer size to make
339 	 * sure alsa could always find a value for period size
340 	 * regardless the buffer size given by user space.
341 	 */
342 	snd_pcm_hw_constraint_step(substream->runtime, 0,
343 			   SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 48);
344 	snd_pcm_hw_constraint_step(substream->runtime, 0,
345 			   SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 48);
346 
347 	/* Make sure, that the period size is always even */
348 	snd_pcm_hw_constraint_step(substream->runtime, 0,
349 			   SNDRV_PCM_HW_PARAM_PERIODS, 2);
350 
351 	return snd_pcm_hw_constraint_integer(runtime,
352 			 SNDRV_PCM_HW_PARAM_PERIODS);
353 out_ops:
354 	mutex_unlock(&sst_lock);
355 out_power_up:
356 	kfree(stream);
357 	return ret_val;
358 }
359 
360 static void sst_media_close(struct snd_pcm_substream *substream,
361 		struct snd_soc_dai *dai)
362 {
363 	struct sst_runtime_stream *stream;
364 	int str_id;
365 
366 	stream = substream->runtime->private_data;
367 	power_down_sst(stream);
368 
369 	str_id = stream->stream_info.str_id;
370 	if (str_id)
371 		stream->ops->close(sst->dev, str_id);
372 	module_put(sst->dev->driver->owner);
373 	kfree(stream);
374 }
375 
376 static int sst_media_prepare(struct snd_pcm_substream *substream,
377 		struct snd_soc_dai *dai)
378 {
379 	struct sst_runtime_stream *stream;
380 	int ret_val, str_id;
381 
382 	stream = substream->runtime->private_data;
383 	str_id = stream->stream_info.str_id;
384 	if (stream->stream_info.str_id) {
385 		ret_val = stream->ops->stream_drop(sst->dev, str_id);
386 		return ret_val;
387 	}
388 
389 	ret_val = sst_platform_alloc_stream(substream, dai);
390 	if (ret_val <= 0)
391 		return ret_val;
392 	snprintf(substream->pcm->id, sizeof(substream->pcm->id),
393 			"%d", stream->stream_info.str_id);
394 
395 	ret_val = sst_platform_init_stream(substream);
396 	if (ret_val)
397 		return ret_val;
398 	substream->runtime->hw.info = SNDRV_PCM_INFO_BLOCK_TRANSFER;
399 	return 0;
400 }
401 
402 static int sst_enable_ssp(struct snd_pcm_substream *substream,
403 			struct snd_soc_dai *dai)
404 {
405 	int ret = 0;
406 
407 	if (!snd_soc_dai_active(dai)) {
408 		ret = sst_handle_vb_timer(dai, true);
409 		sst_fill_ssp_defaults(dai);
410 	}
411 	return ret;
412 }
413 
414 static int sst_be_hw_params(struct snd_pcm_substream *substream,
415 				struct snd_pcm_hw_params *params,
416 				struct snd_soc_dai *dai)
417 {
418 	int ret = 0;
419 
420 	if (snd_soc_dai_active(dai) == 1)
421 		ret = send_ssp_cmd(dai, dai->name, 1);
422 	return ret;
423 }
424 
425 static int sst_set_format(struct snd_soc_dai *dai, unsigned int fmt)
426 {
427 	int ret = 0;
428 
429 	if (!snd_soc_dai_active(dai))
430 		return 0;
431 
432 	ret = sst_fill_ssp_config(dai, fmt);
433 	if (ret < 0)
434 		dev_err(dai->dev, "sst_set_format failed..\n");
435 
436 	return ret;
437 }
438 
439 static int sst_platform_set_ssp_slot(struct snd_soc_dai *dai,
440 			unsigned int tx_mask, unsigned int rx_mask,
441 			int slots, int slot_width) {
442 	int ret = 0;
443 
444 	if (!snd_soc_dai_active(dai))
445 		return ret;
446 
447 	ret = sst_fill_ssp_slot(dai, tx_mask, rx_mask, slots, slot_width);
448 	if (ret < 0)
449 		dev_err(dai->dev, "sst_fill_ssp_slot failed..%d\n", ret);
450 
451 	return ret;
452 }
453 
454 static void sst_disable_ssp(struct snd_pcm_substream *substream,
455 			struct snd_soc_dai *dai)
456 {
457 	if (!snd_soc_dai_active(dai)) {
458 		send_ssp_cmd(dai, dai->name, 0);
459 		sst_handle_vb_timer(dai, false);
460 	}
461 }
462 
463 static const struct snd_soc_dai_ops sst_media_dai_ops = {
464 	.startup = sst_media_open,
465 	.shutdown = sst_media_close,
466 	.prepare = sst_media_prepare,
467 	.mute_stream = sst_media_digital_mute,
468 };
469 
470 static const struct snd_soc_dai_ops sst_compr_dai_ops = {
471 	.mute_stream = sst_media_digital_mute,
472 };
473 
474 static const struct snd_soc_dai_ops sst_be_dai_ops = {
475 	.startup = sst_enable_ssp,
476 	.hw_params = sst_be_hw_params,
477 	.set_fmt = sst_set_format,
478 	.set_tdm_slot = sst_platform_set_ssp_slot,
479 	.shutdown = sst_disable_ssp,
480 };
481 
482 static struct snd_soc_dai_driver sst_platform_dai[] = {
483 {
484 	.name = "media-cpu-dai",
485 	.ops = &sst_media_dai_ops,
486 	.playback = {
487 		.stream_name = "Headset Playback",
488 		.channels_min = SST_STEREO,
489 		.channels_max = SST_STEREO,
490 		.rates = SNDRV_PCM_RATE_44100|SNDRV_PCM_RATE_48000,
491 		.formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
492 	},
493 	.capture = {
494 		.stream_name = "Headset Capture",
495 		.channels_min = 1,
496 		.channels_max = 2,
497 		.rates = SNDRV_PCM_RATE_44100|SNDRV_PCM_RATE_48000,
498 		.formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
499 	},
500 },
501 {
502 	.name = "deepbuffer-cpu-dai",
503 	.ops = &sst_media_dai_ops,
504 	.playback = {
505 		.stream_name = "Deepbuffer Playback",
506 		.channels_min = SST_STEREO,
507 		.channels_max = SST_STEREO,
508 		.rates = SNDRV_PCM_RATE_44100|SNDRV_PCM_RATE_48000,
509 		.formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
510 	},
511 },
512 {
513 	.name = "compress-cpu-dai",
514 	.compress_new = snd_soc_new_compress,
515 	.ops = &sst_compr_dai_ops,
516 	.playback = {
517 		.stream_name = "Compress Playback",
518 		.channels_min = 1,
519 	},
520 },
521 /* BE CPU  Dais */
522 {
523 	.name = "ssp0-port",
524 	.ops = &sst_be_dai_ops,
525 	.playback = {
526 		.stream_name = "ssp0 Tx",
527 		.channels_min = SST_STEREO,
528 		.channels_max = SST_STEREO,
529 		.rates = SNDRV_PCM_RATE_48000,
530 		.formats = SNDRV_PCM_FMTBIT_S16_LE,
531 	},
532 	.capture = {
533 		.stream_name = "ssp0 Rx",
534 		.channels_min = SST_STEREO,
535 		.channels_max = SST_STEREO,
536 		.rates = SNDRV_PCM_RATE_48000,
537 		.formats = SNDRV_PCM_FMTBIT_S16_LE,
538 	},
539 },
540 {
541 	.name = "ssp1-port",
542 	.ops = &sst_be_dai_ops,
543 	.playback = {
544 		.stream_name = "ssp1 Tx",
545 		.channels_min = SST_STEREO,
546 		.channels_max = SST_STEREO,
547 		.rates = SNDRV_PCM_RATE_8000|SNDRV_PCM_RATE_16000|SNDRV_PCM_RATE_48000,
548 		.formats = SNDRV_PCM_FMTBIT_S16_LE,
549 	},
550 	.capture = {
551 		.stream_name = "ssp1 Rx",
552 		.channels_min = SST_STEREO,
553 		.channels_max = SST_STEREO,
554 		.rates = SNDRV_PCM_RATE_8000|SNDRV_PCM_RATE_16000|SNDRV_PCM_RATE_48000,
555 		.formats = SNDRV_PCM_FMTBIT_S16_LE,
556 	},
557 },
558 {
559 	.name = "ssp2-port",
560 	.ops = &sst_be_dai_ops,
561 	.playback = {
562 		.stream_name = "ssp2 Tx",
563 		.channels_min = SST_STEREO,
564 		.channels_max = SST_STEREO,
565 		.rates = SNDRV_PCM_RATE_48000,
566 		.formats = SNDRV_PCM_FMTBIT_S16_LE,
567 	},
568 	.capture = {
569 		.stream_name = "ssp2 Rx",
570 		.channels_min = SST_STEREO,
571 		.channels_max = SST_STEREO,
572 		.rates = SNDRV_PCM_RATE_48000,
573 		.formats = SNDRV_PCM_FMTBIT_S16_LE,
574 	},
575 },
576 };
577 
578 static int sst_soc_open(struct snd_soc_component *component,
579 			struct snd_pcm_substream *substream)
580 {
581 	struct snd_pcm_runtime *runtime;
582 
583 	if (substream->pcm->internal)
584 		return 0;
585 
586 	runtime = substream->runtime;
587 	runtime->hw = sst_platform_pcm_hw;
588 	return 0;
589 }
590 
591 static int sst_soc_trigger(struct snd_soc_component *component,
592 			   struct snd_pcm_substream *substream, int cmd)
593 {
594 	int ret_val = 0, str_id;
595 	struct sst_runtime_stream *stream;
596 	int status;
597 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
598 
599 	dev_dbg(rtd->dev, "%s called\n", __func__);
600 	if (substream->pcm->internal)
601 		return 0;
602 	stream = substream->runtime->private_data;
603 	str_id = stream->stream_info.str_id;
604 	switch (cmd) {
605 	case SNDRV_PCM_TRIGGER_START:
606 		dev_dbg(rtd->dev, "sst: Trigger Start\n");
607 		status = SST_PLATFORM_RUNNING;
608 		stream->stream_info.arg = substream;
609 		ret_val = stream->ops->stream_start(sst->dev, str_id);
610 		break;
611 	case SNDRV_PCM_TRIGGER_STOP:
612 		dev_dbg(rtd->dev, "sst: in stop\n");
613 		status = SST_PLATFORM_DROPPED;
614 		ret_val = stream->ops->stream_drop(sst->dev, str_id);
615 		break;
616 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
617 	case SNDRV_PCM_TRIGGER_SUSPEND:
618 		dev_dbg(rtd->dev, "sst: in pause\n");
619 		status = SST_PLATFORM_PAUSED;
620 		ret_val = stream->ops->stream_pause(sst->dev, str_id);
621 		break;
622 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
623 	case SNDRV_PCM_TRIGGER_RESUME:
624 		dev_dbg(rtd->dev, "sst: in pause release\n");
625 		status = SST_PLATFORM_RUNNING;
626 		ret_val = stream->ops->stream_pause_release(sst->dev, str_id);
627 		break;
628 	default:
629 		return -EINVAL;
630 	}
631 
632 	if (!ret_val)
633 		sst_set_stream_status(stream, status);
634 
635 	return ret_val;
636 }
637 
638 
639 static snd_pcm_uframes_t sst_soc_pointer(struct snd_soc_component *component,
640 					 struct snd_pcm_substream *substream)
641 {
642 	struct sst_runtime_stream *stream;
643 	int ret_val, status;
644 	struct pcm_stream_info *str_info;
645 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
646 
647 	stream = substream->runtime->private_data;
648 	status = sst_get_stream_status(stream);
649 	if (status == SST_PLATFORM_INIT)
650 		return 0;
651 	str_info = &stream->stream_info;
652 	ret_val = stream->ops->stream_read_tstamp(sst->dev, str_info);
653 	if (ret_val) {
654 		dev_err(rtd->dev, "sst: error code = %d\n", ret_val);
655 		return ret_val;
656 	}
657 	substream->runtime->delay = str_info->pcm_delay;
658 	return str_info->buffer_ptr;
659 }
660 
661 static int sst_soc_pcm_new(struct snd_soc_component *component,
662 			   struct snd_soc_pcm_runtime *rtd)
663 {
664 	struct snd_soc_dai *dai = asoc_rtd_to_cpu(rtd, 0);
665 	struct snd_pcm *pcm = rtd->pcm;
666 
667 	if (dai->driver->playback.channels_min ||
668 			dai->driver->capture.channels_min) {
669 		snd_pcm_set_managed_buffer_all(pcm,
670 			SNDRV_DMA_TYPE_CONTINUOUS,
671 			snd_dma_continuous_data(GFP_DMA),
672 			SST_MIN_BUFFER, SST_MAX_BUFFER);
673 	}
674 	return 0;
675 }
676 
677 static int sst_soc_probe(struct snd_soc_component *component)
678 {
679 	struct sst_data *drv = dev_get_drvdata(component->dev);
680 
681 	drv->soc_card = component->card;
682 	return sst_dsp_init_v2_dpcm(component);
683 }
684 
685 static void sst_soc_remove(struct snd_soc_component *component)
686 {
687 	struct sst_data *drv = dev_get_drvdata(component->dev);
688 
689 	drv->soc_card = NULL;
690 }
691 
692 static const struct snd_soc_component_driver sst_soc_platform_drv  = {
693 	.name		= DRV_NAME,
694 	.probe		= sst_soc_probe,
695 	.remove		= sst_soc_remove,
696 	.open		= sst_soc_open,
697 	.trigger	= sst_soc_trigger,
698 	.pointer	= sst_soc_pointer,
699 	.compress_ops	= &sst_platform_compress_ops,
700 	.pcm_construct	= sst_soc_pcm_new,
701 };
702 
703 static int sst_platform_probe(struct platform_device *pdev)
704 {
705 	struct sst_data *drv;
706 	int ret;
707 	struct sst_platform_data *pdata;
708 
709 	drv = devm_kzalloc(&pdev->dev, sizeof(*drv), GFP_KERNEL);
710 	if (drv == NULL) {
711 		return -ENOMEM;
712 	}
713 
714 	pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
715 	if (pdata == NULL) {
716 		return -ENOMEM;
717 	}
718 
719 	pdata->pdev_strm_map = dpcm_strm_map;
720 	pdata->strm_map_size = ARRAY_SIZE(dpcm_strm_map);
721 	drv->pdata = pdata;
722 	drv->pdev = pdev;
723 	mutex_init(&drv->lock);
724 	dev_set_drvdata(&pdev->dev, drv);
725 
726 	ret = devm_snd_soc_register_component(&pdev->dev, &sst_soc_platform_drv,
727 				sst_platform_dai, ARRAY_SIZE(sst_platform_dai));
728 	if (ret)
729 		dev_err(&pdev->dev, "registering cpu dais failed\n");
730 
731 	return ret;
732 }
733 
734 static int sst_platform_remove(struct platform_device *pdev)
735 {
736 	dev_dbg(&pdev->dev, "sst_platform_remove success\n");
737 	return 0;
738 }
739 
740 #ifdef CONFIG_PM_SLEEP
741 
742 static int sst_soc_prepare(struct device *dev)
743 {
744 	struct sst_data *drv = dev_get_drvdata(dev);
745 	struct snd_soc_pcm_runtime *rtd;
746 
747 	if (!drv->soc_card)
748 		return 0;
749 
750 	/* suspend all pcms first */
751 	snd_soc_suspend(drv->soc_card->dev);
752 	snd_soc_poweroff(drv->soc_card->dev);
753 
754 	/* set the SSPs to idle */
755 	for_each_card_rtds(drv->soc_card, rtd) {
756 		struct snd_soc_dai *dai = asoc_rtd_to_cpu(rtd, 0);
757 
758 		if (snd_soc_dai_active(dai)) {
759 			send_ssp_cmd(dai, dai->name, 0);
760 			sst_handle_vb_timer(dai, false);
761 		}
762 	}
763 
764 	return 0;
765 }
766 
767 static void sst_soc_complete(struct device *dev)
768 {
769 	struct sst_data *drv = dev_get_drvdata(dev);
770 	struct snd_soc_pcm_runtime *rtd;
771 
772 	if (!drv->soc_card)
773 		return;
774 
775 	/* restart SSPs */
776 	for_each_card_rtds(drv->soc_card, rtd) {
777 		struct snd_soc_dai *dai = asoc_rtd_to_cpu(rtd, 0);
778 
779 		if (snd_soc_dai_active(dai)) {
780 			sst_handle_vb_timer(dai, true);
781 			send_ssp_cmd(dai, dai->name, 1);
782 		}
783 	}
784 	snd_soc_resume(drv->soc_card->dev);
785 }
786 
787 #else
788 
789 #define sst_soc_prepare NULL
790 #define sst_soc_complete NULL
791 
792 #endif
793 
794 
795 static const struct dev_pm_ops sst_platform_pm = {
796 	.prepare	= sst_soc_prepare,
797 	.complete	= sst_soc_complete,
798 };
799 
800 static struct platform_driver sst_platform_driver = {
801 	.driver		= {
802 		.name		= "sst-mfld-platform",
803 		.pm             = &sst_platform_pm,
804 	},
805 	.probe		= sst_platform_probe,
806 	.remove		= sst_platform_remove,
807 };
808 
809 module_platform_driver(sst_platform_driver);
810 
811 MODULE_DESCRIPTION("ASoC Intel(R) MID Platform driver");
812 MODULE_AUTHOR("Vinod Koul <vinod.koul@intel.com>");
813 MODULE_AUTHOR("Harsha Priya <priya.harsha@intel.com>");
814 MODULE_LICENSE("GPL v2");
815 MODULE_ALIAS("platform:sst-atom-hifi2-platform");
816 MODULE_ALIAS("platform:sst-mfld-platform");
817