summaryrefslogtreecommitdiffstats
path: root/drivers/sound/sdl.c
blob: 118d7742955f7ac987d8f1711202c77b3ec9415b (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
// SPDX-License-Identifier: GPL-2.0-only

#include <common.h>
#include <errno.h>
#include <driver.h>
#include <mach/linux.h>
#include <linux/time.h>
#include <linux/math64.h>
#include <of.h>
#include <sound.h>

#define AMPLITUDE	28000
#define SAMPLERATE	44000ULL

struct sandbox_sound {
	struct sound_card card;
};

static int sandbox_sound_beep(struct sound_card *card, unsigned freq, unsigned duration)
{
    size_t nsamples = div_s64(SAMPLERATE * duration, USEC_PER_SEC);
    int16_t *data;
    int ret;

    if (!freq) {
	    sdl_sound_stop();
	    return 0;
    }

    data = malloc(nsamples * sizeof(*data));
    if (!data)
	    return -ENOMEM;

    synth_sin(freq, AMPLITUDE, data, SAMPLERATE, nsamples);
    ret = sdl_sound_play(data, nsamples);
    if (ret)
	    ret = -EIO;
    free(data);

    return ret;
}

static int sandbox_sound_probe(struct device_d *dev)
{
	struct sandbox_sound *priv;
	struct sound_card *card;
	int ret;

	priv = xzalloc(sizeof(*priv));

	card = &priv->card;
	card->name = "SDL-Audio";
	card->beep = sandbox_sound_beep;

	ret = sdl_sound_init(SAMPLERATE);
	if (ret) {
		ret = -ENODEV;
		goto free_priv;
	}

	ret = sound_card_register(card);
	if (ret)
		goto sdl_sound_close;

	dev_info(dev, "probed\n");
	return 0;

sdl_sound_close:
	sdl_sound_close();
free_priv:
	free(priv);

	return ret;
}


static __maybe_unused struct of_device_id sandbox_sound_dt_ids[] = {
	{ .compatible = "barebox,sandbox-sound" },
	{ /* sentinel */ }
};

static struct driver_d sandbox_sound_drv = {
	.name  = "sandbox-sound",
	.of_compatible = sandbox_sound_dt_ids,
	.probe = sandbox_sound_probe,
};
device_platform_driver(sandbox_sound_drv);