1 | /*
|
---|
2 | * Copyright (c) 2011 Jan Vesely
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | #include <assert.h>
|
---|
30 | #include <stdbool.h>
|
---|
31 | #include <errno.h>
|
---|
32 | #include <libarch/ddi.h>
|
---|
33 | #include <macros.h>
|
---|
34 | #include <unistd.h>
|
---|
35 |
|
---|
36 | #include "ddf_log.h"
|
---|
37 | #include "mixer.h"
|
---|
38 |
|
---|
39 | typedef struct channel {
|
---|
40 | const char *name;
|
---|
41 | uint8_t address;
|
---|
42 | unsigned shift;
|
---|
43 | unsigned volume_levels;
|
---|
44 | bool preserve_bits;
|
---|
45 | } channel_t;
|
---|
46 |
|
---|
47 | /* CT1335 channels */
|
---|
48 | static const channel_t channels_table_ct1335[] = {
|
---|
49 | { "Master", 0x02, 1, 8, false }, /* Master, Mono, 3bit volume level */
|
---|
50 | { "Midi", 0x06, 1, 8, false }, /* Midi, Mono, 3bit volume level */
|
---|
51 | { "CD", 0x08, 1, 8, false }, /* CD, Mono, 3bit volume level */
|
---|
52 | { "Voice", 0x0a, 1, 4, false }, /* Voice, Mono, 2bit volume level */
|
---|
53 | };
|
---|
54 |
|
---|
55 | /* CT1345 channels */
|
---|
56 | static const channel_t channels_table_ct1345[] = {
|
---|
57 | { "Master Left", 0x22, 5, 8, true }, /* Master, Left, 3bit volume level */
|
---|
58 | { "Master Right", 0x22, 1, 8, true }, /* Master, Right, 3bit volume level */
|
---|
59 | { "MIDI Left", 0x26, 5, 8, true }, /* Midi, Left, 3bit volume level */
|
---|
60 | { "MIDI Right", 0x26, 1, 8, true }, /* Midi, Right, 3bit volume level */
|
---|
61 | { "CD Left", 0x28, 5, 8, true }, /* CD, Left, 3bit volume level */
|
---|
62 | { "CD Right", 0x28, 1, 8, true }, /* CD, Right, 3bit volume level */
|
---|
63 | { "Line In Left", 0x2e, 5, 8, true }, /* Line, Left, 3bit volume level */
|
---|
64 | { "Line In Right", 0x2e, 1, 8, true }, /* Line, Right, 3bit volume level */
|
---|
65 | { "Voice Left", 0x04, 5, 8, true }, /* Voice, Left, 3bit volume level */
|
---|
66 | { "Voice Right", 0x04, 1, 8, true }, /* Voice, Right, 3bit volume level */
|
---|
67 | { "Mic", 0x0a, 1, 4, false }, /* Mic, Mono, 2bit volume level */
|
---|
68 | };
|
---|
69 |
|
---|
70 | /* CT1745 channels */
|
---|
71 | static const channel_t channels_table_ct1745[] = {
|
---|
72 | { "Master Left", 0x30, 3, 32, false }, /* Master, Left, 5bit volume level */
|
---|
73 | { "Master Right", 0x31, 3, 32, false }, /* Master, Right, 5bit volume level */
|
---|
74 | { "Voice Left", 0x32, 3, 32, false }, /* Voice, Left, 5bit volume level */
|
---|
75 | { "Voice Right", 0x33, 3, 32, false }, /* Voice, Right, 5bit volume level */
|
---|
76 | { "MIDI Left", 0x34, 3, 32, false }, /* MIDI, Left, 5bit volume level */
|
---|
77 | { "MIDI Right", 0x35, 3, 32, false }, /* MIDI, Right, 5bit volume level */
|
---|
78 | { "CD Left", 0x36, 3, 32, false }, /* CD, Left, 5bit volume level */
|
---|
79 | { "CD Right", 0x37, 3, 32, false }, /* CD, Right, 5bit volume level */
|
---|
80 | { "Line In Left", 0x38, 3, 32, false }, /* Line, Left, 5bit volume level */
|
---|
81 | { "Line In Right", 0x39, 3, 32, false }, /* Line, Right, 5bit volume level */
|
---|
82 | { "Mic", 0x3a, 3, 32, false }, /* Mic, Mono, 5bit volume level */
|
---|
83 | { "PC Speaker", 0x3b, 6, 4, false }, /* PC speaker, Mono, 2bit level */
|
---|
84 | { "Input Gain Left", 0x3f, 6, 4, false }, /* Input Gain, Left, 2bit level */
|
---|
85 | { "Input Gain Right", 0x40, 6, 4, false }, /* Input Gain, Right, 2bit level */
|
---|
86 | { "Output Gain Left", 0x41, 6, 4, false }, /* Output Gain, Left, 2bit level */
|
---|
87 | { "Output Gain Right", 0x42, 6, 4, false }, /* Output Gain, Right, 2bit level */
|
---|
88 | { "Treble Left", 0x44, 4, 16, false }, /* Treble, Left, 4bit volume level */
|
---|
89 | { "Treble Right", 0x45, 4, 16, false }, /* Treble, Right, 4bit volume level */
|
---|
90 | { "Bass Left", 0x46, 4, 16, false }, /* Bass, Left, 4bit volume level */
|
---|
91 | { "Bass Right", 0x47, 4, 16, false }, /* Bass, Right, 4bit volume level */
|
---|
92 | };
|
---|
93 |
|
---|
94 | static const struct {
|
---|
95 | const channel_t *table;
|
---|
96 | size_t count;
|
---|
97 | } volume_table[] = {
|
---|
98 | [SB_MIXER_NONE] = { NULL, 0 },
|
---|
99 | [SB_MIXER_UNKNOWN] = { NULL, 0 },
|
---|
100 | [SB_MIXER_CT1335] = {
|
---|
101 | channels_table_ct1335,
|
---|
102 | ARRAY_SIZE(channels_table_ct1335),
|
---|
103 | },
|
---|
104 | [SB_MIXER_CT1345] = {
|
---|
105 | channels_table_ct1345,
|
---|
106 | ARRAY_SIZE(channels_table_ct1345),
|
---|
107 | },
|
---|
108 | [SB_MIXER_CT1745] = {
|
---|
109 | channels_table_ct1745,
|
---|
110 | ARRAY_SIZE(channels_table_ct1745),
|
---|
111 | },
|
---|
112 | };
|
---|
113 |
|
---|
114 | const char * sb_mixer_type_str(sb_mixer_type_t type)
|
---|
115 | {
|
---|
116 | static const char *names[] = {
|
---|
117 | [SB_MIXER_NONE] = "No mixer",
|
---|
118 | [SB_MIXER_CT1335] = "CT 1335",
|
---|
119 | [SB_MIXER_CT1345] = "CT 1345",
|
---|
120 | [SB_MIXER_CT1745] = "CT 1745",
|
---|
121 | [SB_MIXER_UNKNOWN] = "Unknown mixer",
|
---|
122 | };
|
---|
123 | return names[type];
|
---|
124 | }
|
---|
125 |
|
---|
126 | int sb_mixer_init(sb_mixer_t *mixer, sb16_regs_t *regs, sb_mixer_type_t type)
|
---|
127 | {
|
---|
128 | assert(mixer);
|
---|
129 | mixer->regs = regs;
|
---|
130 | mixer->type = type;
|
---|
131 | if (type == SB_MIXER_UNKNOWN)
|
---|
132 | return ENOTSUP;
|
---|
133 |
|
---|
134 | if (type != SB_MIXER_NONE) {
|
---|
135 | pio_write_8(®s->mixer_address, MIXER_RESET_ADDRESS);
|
---|
136 | pio_write_8(®s->mixer_data, 1);
|
---|
137 | }
|
---|
138 | pio_write_8(®s->mixer_address, MIXER_PNP_IRQ_ADDRESS);
|
---|
139 | const uint8_t irq = pio_read_8(®s->mixer_data);
|
---|
140 | pio_write_8(®s->mixer_address, MIXER_PNP_DMA_ADDRESS);
|
---|
141 | const uint8_t dma = pio_read_8(®s->mixer_data);
|
---|
142 | ddf_log_debug("SB16 setup with IRQ 0x%hhx and DMA 0x%hhx.", irq, dma);
|
---|
143 | return EOK;
|
---|
144 | }
|
---|
145 |
|
---|
146 | int sb_mixer_get_control_item_count(const sb_mixer_t *mixer)
|
---|
147 | {
|
---|
148 | assert(mixer);
|
---|
149 | return volume_table[mixer->type].count;
|
---|
150 | }
|
---|
151 |
|
---|
152 | int sb_mixer_get_control_item_info(const sb_mixer_t *mixer, unsigned item,
|
---|
153 | const char** name, unsigned *levels)
|
---|
154 | {
|
---|
155 | assert(mixer);
|
---|
156 | if (item > volume_table[mixer->type].count)
|
---|
157 | return ENOENT;
|
---|
158 |
|
---|
159 | const channel_t *ch = &volume_table[mixer->type].table[item];
|
---|
160 | if (name)
|
---|
161 | *name = ch->name;
|
---|
162 | if (levels)
|
---|
163 | *levels = ch->volume_levels;
|
---|
164 | return EOK;
|
---|
165 | }
|
---|
166 |
|
---|
167 | /**
|
---|
168 | * Write new volume level from mixer registers.
|
---|
169 | * @param mixer SB Mixer to use.
|
---|
170 | * @param index Control item(channel) index.
|
---|
171 | * @param value New volume level.
|
---|
172 | * @return Error code.
|
---|
173 | */
|
---|
174 | int sb_mixer_get_control_item_value(const sb_mixer_t *mixer, unsigned item,
|
---|
175 | unsigned *value)
|
---|
176 | {
|
---|
177 | assert(mixer);
|
---|
178 | if (!value)
|
---|
179 | return EBADMEM;
|
---|
180 | if (item > volume_table[mixer->type].count)
|
---|
181 | return ENOENT;
|
---|
182 |
|
---|
183 | const channel_t *chan = &volume_table[mixer->type].table[item];
|
---|
184 | pio_write_8(&mixer->regs->mixer_address, chan->address);
|
---|
185 | *value = (pio_read_8(&mixer->regs->mixer_data) >> chan->shift)
|
---|
186 | & (chan->volume_levels - 1);
|
---|
187 | return EOK;
|
---|
188 | }
|
---|
189 |
|
---|
190 | /**
|
---|
191 | * Write new volume level to mixer registers.
|
---|
192 | * @param mixer SB Mixer to use.
|
---|
193 | * @param index Control item(channel) index.
|
---|
194 | * @param value New volume level.
|
---|
195 | * @return Error code.
|
---|
196 | */
|
---|
197 | int sb_mixer_set_control_item_value(const sb_mixer_t *mixer, unsigned item,
|
---|
198 | unsigned value)
|
---|
199 | {
|
---|
200 | assert(mixer);
|
---|
201 | if (item > volume_table[mixer->type].count)
|
---|
202 | return ENOENT;
|
---|
203 |
|
---|
204 | const channel_t *chan = &volume_table[mixer->type].table[item];
|
---|
205 | if (value >= chan->volume_levels)
|
---|
206 | value = chan->volume_levels - 1;
|
---|
207 |
|
---|
208 | pio_write_8(&mixer->regs->mixer_address, chan->address);
|
---|
209 |
|
---|
210 | uint8_t regv = 0;
|
---|
211 | if (chan->preserve_bits) {
|
---|
212 | regv = pio_read_8(&mixer->regs->mixer_data);
|
---|
213 | regv &= ~(uint8_t)((chan->volume_levels - 1) << chan->shift);
|
---|
214 | }
|
---|
215 |
|
---|
216 | regv |= value << chan->shift;
|
---|
217 | pio_write_8(&mixer->regs->mixer_data, regv);
|
---|
218 | ddf_log_note("Item %s new value is: %u.",
|
---|
219 | volume_table[mixer->type].table[item].name, value);
|
---|
220 | return EOK;
|
---|
221 | }
|
---|