dm: sound: Create a uclass for i2s
[pandora-u-boot.git] / drivers / sound / sandbox.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2013 Google, Inc
4  */
5
6 #include <common.h>
7 #include <audio_codec.h>
8 #include <dm.h>
9 #include <i2s.h>
10 #include <asm/sound.h>
11 #include <asm/sdl.h>
12
13 struct sandbox_codec_priv {
14         int interface;
15         int rate;
16         int mclk_freq;
17         int bits_per_sample;
18         uint channels;
19 };
20
21 struct sandbox_i2s_priv {
22         int sum;        /* Use to sum the provided audio data */
23 };
24
25 int sound_play(uint32_t msec, uint32_t frequency)
26 {
27         sandbox_sdl_sound_start(frequency);
28         mdelay(msec);
29         sandbox_sdl_sound_stop();
30
31         return 0;
32 }
33
34 int sound_init(const void *blob)
35 {
36         return sandbox_sdl_sound_init();
37 }
38
39 void sandbox_get_codec_params(struct udevice *dev, int *interfacep, int *ratep,
40                               int *mclk_freqp, int *bits_per_samplep,
41                               uint *channelsp)
42 {
43         struct sandbox_codec_priv *priv = dev_get_priv(dev);
44
45         *interfacep = priv->interface;
46         *ratep = priv->rate;
47         *mclk_freqp = priv->mclk_freq;
48         *bits_per_samplep = priv->bits_per_sample;
49         *channelsp = priv->channels;
50 }
51
52 int sandbox_get_i2s_sum(struct udevice *dev)
53 {
54         struct sandbox_i2s_priv *priv = dev_get_priv(dev);
55
56         return priv->sum;
57 }
58
59 static int sandbox_codec_set_params(struct udevice *dev, int interface,
60                                     int rate, int mclk_freq,
61                                     int bits_per_sample, uint channels)
62 {
63         struct sandbox_codec_priv *priv = dev_get_priv(dev);
64
65         priv->interface = interface;
66         priv->rate = rate;
67         priv->mclk_freq = mclk_freq;
68         priv->bits_per_sample = bits_per_sample;
69         priv->channels = channels;
70
71         return 0;
72 }
73
74 static int sandbox_i2s_tx_data(struct udevice *dev, void *data,
75                                uint data_size)
76 {
77         struct sandbox_i2s_priv *priv = dev_get_priv(dev);
78         int i;
79
80         for (i = 0; i < data_size; i++)
81                 priv->sum += ((uint8_t *)data)[i];
82
83         return 0;
84 }
85
86 static int sandbox_i2s_probe(struct udevice *dev)
87 {
88         struct i2s_uc_priv *uc_priv = dev_get_uclass_priv(dev);
89
90         /* Use hard-coded values here */
91         uc_priv->rfs = 256;
92         uc_priv->bfs = 32;
93         uc_priv->audio_pll_clk = 192000000;
94         uc_priv->samplingrate = 48000;
95         uc_priv->bitspersample = 16;
96         uc_priv->channels = 2;
97         uc_priv->id = 1;
98
99         return 0;
100 }
101
102 static const struct audio_codec_ops sandbox_codec_ops = {
103         .set_params     = sandbox_codec_set_params,
104 };
105
106 static const struct udevice_id sandbox_codec_ids[] = {
107         { .compatible = "sandbox,audio-codec" },
108         { }
109 };
110
111 U_BOOT_DRIVER(sandbox_codec) = {
112         .name           = "sandbox_codec",
113         .id             = UCLASS_AUDIO_CODEC,
114         .of_match       = sandbox_codec_ids,
115         .ops            = &sandbox_codec_ops,
116         .priv_auto_alloc_size   = sizeof(struct sandbox_codec_priv),
117 };
118
119 static const struct i2s_ops sandbox_i2s_ops = {
120         .tx_data        = sandbox_i2s_tx_data,
121 };
122
123 static const struct udevice_id sandbox_i2s_ids[] = {
124         { .compatible = "sandbox,i2s" },
125         { }
126 };
127
128 U_BOOT_DRIVER(sandbox_i2s) = {
129         .name           = "sandbox_i2s",
130         .id             = UCLASS_I2S,
131         .of_match       = sandbox_i2s_ids,
132         .ops            = &sandbox_i2s_ops,
133         .probe          = sandbox_i2s_probe,
134         .priv_auto_alloc_size   = sizeof(struct sandbox_i2s_priv),
135 };