summaryrefslogtreecommitdiffstats
path: root/sound/soc/sof/intel/hda-probes.c
blob: 3e33101f052102d24ce7779dc70b0395277b35e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
//
// This file is provided under a dual BSD/GPLv2 license.  When using or
// redistributing this file, you may do so under either license.
//
// Copyright(c) 2019-2021 Intel Corporation
//
// Author: Cezary Rojewski <cezary.rojewski@intel.com>
// Converted to SOF client:
//  Ranjani Sridharan <ranjani.sridharan@linux.intel.com>
//  Peter Ujfalusi <peter.ujfalusi@linux.intel.com>
//

#include <linux/module.h>
#include <sound/hdaudio_ext.h>
#include <sound/soc.h>
#include "../sof-priv.h"
#include "../sof-client-probes.h"
#include "../sof-client.h"
#include "hda.h"

static inline struct hdac_ext_stream *
hda_compr_get_stream(struct snd_compr_stream *cstream)
{
	return cstream->runtime->private_data;
}

static int hda_probes_compr_startup(struct sof_client_dev *cdev,
				    struct snd_compr_stream *cstream,
				    struct snd_soc_dai *dai, u32 *stream_id)
{
	struct snd_sof_dev *sdev = sof_client_dev_to_sof_dev(cdev);
	struct hdac_ext_stream *hext_stream;

	hext_stream = hda_dsp_stream_get(sdev, cstream->direction, 0);
	if (!hext_stream)
		return -EBUSY;

	hdac_stream(hext_stream)->curr_pos = 0;
	hdac_stream(hext_stream)->cstream = cstream;
	cstream->runtime->private_data = hext_stream;

	*stream_id = hdac_stream(hext_stream)->stream_tag;

	return 0;
}

static int hda_probes_compr_shutdown(struct sof_client_dev *cdev,
				     struct snd_compr_stream *cstream,
				     struct snd_soc_dai *dai)
{
	struct hdac_ext_stream *hext_stream = hda_compr_get_stream(cstream);
	struct snd_sof_dev *sdev = sof_client_dev_to_sof_dev(cdev);
	int ret;

	ret = hda_dsp_stream_put(sdev, cstream->direction,
				 hdac_stream(hext_stream)->stream_tag);
	if (ret < 0) {
		dev_dbg(sdev->dev, "stream put failed: %d\n", ret);
		return ret;
	}

	hdac_stream(hext_stream)->cstream = NULL;
	cstream->runtime->private_data = NULL;

	return 0;
}

static int hda_probes_compr_set_params(struct sof_client_dev *cdev,
				       struct snd_compr_stream *cstream,
				       struct snd_compr_params *params,
				       struct snd_soc_dai *dai)
{
	struct hdac_ext_stream *hext_stream = hda_compr_get_stream(cstream);
	struct snd_sof_dev *sdev = sof_client_dev_to_sof_dev(cdev);
	struct hdac_stream *hstream = hdac_stream(hext_stream);
	struct snd_dma_buffer *dmab;
	u32 bits, rate;
	int bps, ret;

	dmab = cstream->runtime->dma_buffer_p;
	/* compr params do not store bit depth, default to S32_LE */
	bps = snd_pcm_format_physical_width(SNDRV_PCM_FORMAT_S32_LE);
	if (bps < 0)
		return bps;
	bits = hda_dsp_get_bits(sdev, bps);
	rate = hda_dsp_get_mult_div(sdev, params->codec.sample_rate);

	hstream->format_val = rate | bits | (params->codec.ch_out - 1);
	hstream->bufsize = cstream->runtime->buffer_size;
	hstream->period_bytes = cstream->runtime->fragment_size;
	hstream->no_period_wakeup = 0;

	ret = hda_dsp_stream_hw_params(sdev, hext_stream, dmab, NULL);
	if (ret < 0) {
		dev_err(sdev->dev, "error: hdac prepare failed: %d\n", ret);
		return ret;
	}

	return 0;
}

static int hda_probes_compr_trigger(struct sof_client_dev *cdev,
				    struct snd_compr_stream *cstream,
				    int cmd, struct snd_soc_dai *dai)
{
	struct hdac_ext_stream *hext_stream = hda_compr_get_stream(cstream);
	struct snd_sof_dev *sdev = sof_client_dev_to_sof_dev(cdev);

	return hda_dsp_stream_trigger(sdev, hext_stream, cmd);
}

static int hda_probes_compr_pointer(struct sof_client_dev *cdev,
				    struct snd_compr_stream *cstream,
				    struct snd_compr_tstamp *tstamp,
				    struct snd_soc_dai *dai)
{
	struct hdac_ext_stream *hext_stream = hda_compr_get_stream(cstream);
	struct snd_soc_pcm_stream *pstream;

	pstream = &dai->driver->capture;
	tstamp->copied_total = hdac_stream(hext_stream)->curr_pos;
	tstamp->sampling_rate = snd_pcm_rate_bit_to_rate(pstream->rates);

	return 0;
}

/* SOF client implementation */
static const struct sof_probes_host_ops hda_probes_ops = {
	.startup = hda_probes_compr_startup,
	.shutdown = hda_probes_compr_shutdown,
	.set_params = hda_probes_compr_set_params,
	.trigger = hda_probes_compr_trigger,
	.pointer = hda_probes_compr_pointer,
};

int hda_probes_register(struct snd_sof_dev *sdev)
{
	return sof_client_dev_register(sdev, "hda-probes", 0, &hda_probes_ops,
				       sizeof(hda_probes_ops));
}
EXPORT_SYMBOL_NS(hda_probes_register, SND_SOC_SOF_INTEL_HDA_COMMON);

void hda_probes_unregister(struct snd_sof_dev *sdev)
{
	sof_client_dev_unregister(sdev, "hda-probes", 0);
}
EXPORT_SYMBOL_NS(hda_probes_unregister, SND_SOC_SOF_INTEL_HDA_COMMON);

MODULE_IMPORT_NS(SND_SOC_SOF_CLIENT);