summaryrefslogtreecommitdiff
path: root/drivers/sound/sandbox.c
blob: ee2635f41d256ad402d0e17ac399fe7ca584f24a (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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// SPDX-License-Identifier: GPL-2.0+
/*
 * Copyright (c) 2013 Google, Inc
 */

#include <common.h>
#include <audio_codec.h>
#include <dm.h>
#include <i2s.h>
#include <sound.h>
#include <asm/sound.h>
#include <asm/sdl.h>

struct sandbox_codec_priv {
	int interface;
	int rate;
	int mclk_freq;
	int bits_per_sample;
	uint channels;
};

struct sandbox_i2s_priv {
	int sum;	/* Use to sum the provided audio data */
};

struct sandbox_sound_priv {
	int setup_called;
	int sum;	/* Use to sum the provided audio data */
};

#ifndef CONFIG_DM_SOUND
int sound_play(uint32_t msec, uint32_t frequency)
{
	sandbox_sdl_sound_start(frequency);
	mdelay(msec);
	sandbox_sdl_sound_stop();

	return 0;
}
#endif /* CONFIG_DM_SOUND */

int sound_init(const void *blob)
{
	return sandbox_sdl_sound_init();
}

void sandbox_get_codec_params(struct udevice *dev, int *interfacep, int *ratep,
			      int *mclk_freqp, int *bits_per_samplep,
			      uint *channelsp)
{
	struct sandbox_codec_priv *priv = dev_get_priv(dev);

	*interfacep = priv->interface;
	*ratep = priv->rate;
	*mclk_freqp = priv->mclk_freq;
	*bits_per_samplep = priv->bits_per_sample;
	*channelsp = priv->channels;
}

int sandbox_get_i2s_sum(struct udevice *dev)
{
	struct sandbox_i2s_priv *priv = dev_get_priv(dev);

	return priv->sum;
}

int sandbox_get_setup_called(struct udevice *dev)
{
	struct sandbox_sound_priv *priv = dev_get_priv(dev);

	return priv->setup_called;
}

int sandbox_get_sound_sum(struct udevice *dev)
{
	struct sandbox_sound_priv *priv = dev_get_priv(dev);

	return priv->sum;
}

static int sandbox_codec_set_params(struct udevice *dev, int interface,
				    int rate, int mclk_freq,
				    int bits_per_sample, uint channels)
{
	struct sandbox_codec_priv *priv = dev_get_priv(dev);

	priv->interface = interface;
	priv->rate = rate;
	priv->mclk_freq = mclk_freq;
	priv->bits_per_sample = bits_per_sample;
	priv->channels = channels;

	return 0;
}

static int sandbox_i2s_tx_data(struct udevice *dev, void *data,
			       uint data_size)
{
	struct sandbox_i2s_priv *priv = dev_get_priv(dev);
	int i;

	for (i = 0; i < data_size; i++)
		priv->sum += ((uint8_t *)data)[i];

	return 0;
}

static int sandbox_i2s_probe(struct udevice *dev)
{
	struct i2s_uc_priv *uc_priv = dev_get_uclass_priv(dev);

	/* Use hard-coded values here */
	uc_priv->rfs = 256;
	uc_priv->bfs = 32;
	uc_priv->audio_pll_clk = 192000000;
	uc_priv->samplingrate = 48000;
	uc_priv->bitspersample = 16;
	uc_priv->channels = 2;
	uc_priv->id = 1;

	return sandbox_sdl_sound_init();
}

static int sandbox_sound_setup(struct udevice *dev)
{
	struct sandbox_sound_priv *priv = dev_get_priv(dev);

	priv->setup_called++;

	return 0;
}

static int sandbox_sound_play(struct udevice *dev, void *data, uint data_size)
{
	struct sound_uc_priv *uc_priv = dev_get_uclass_priv(dev);
	struct sandbox_sound_priv *priv = dev_get_priv(dev);
	int i;

	for (i = 0; i < data_size; i++)
		priv->sum += ((uint8_t *)data)[i];

	return i2s_tx_data(uc_priv->i2s, data, data_size);
}

static int sandbox_sound_probe(struct udevice *dev)
{
	return sound_find_codec_i2s(dev);
}

static const struct audio_codec_ops sandbox_codec_ops = {
	.set_params	= sandbox_codec_set_params,
};

static const struct udevice_id sandbox_codec_ids[] = {
	{ .compatible = "sandbox,audio-codec" },
	{ }
};

U_BOOT_DRIVER(sandbox_codec) = {
	.name		= "sandbox_codec",
	.id		= UCLASS_AUDIO_CODEC,
	.of_match	= sandbox_codec_ids,
	.ops		= &sandbox_codec_ops,
	.priv_auto_alloc_size	= sizeof(struct sandbox_codec_priv),
};

static const struct i2s_ops sandbox_i2s_ops = {
	.tx_data	= sandbox_i2s_tx_data,
};

static const struct udevice_id sandbox_i2s_ids[] = {
	{ .compatible = "sandbox,i2s" },
	{ }
};

U_BOOT_DRIVER(sandbox_i2s) = {
	.name		= "sandbox_i2s",
	.id		= UCLASS_I2S,
	.of_match	= sandbox_i2s_ids,
	.ops		= &sandbox_i2s_ops,
	.probe		= sandbox_i2s_probe,
	.priv_auto_alloc_size	= sizeof(struct sandbox_i2s_priv),
};

static const struct sound_ops sandbox_sound_ops = {
	.setup	= sandbox_sound_setup,
	.play	= sandbox_sound_play,
};

static const struct udevice_id sandbox_sound_ids[] = {
	{ .compatible = "sandbox,sound" },
	{ }
};

U_BOOT_DRIVER(sandbox_sound) = {
	.name		= "sandbox_sound",
	.id		= UCLASS_SOUND,
	.of_match	= sandbox_sound_ids,
	.ops		= &sandbox_sound_ops,
	.priv_auto_alloc_size	= sizeof(struct sandbox_sound_priv),
	.probe		= sandbox_sound_probe,
};