source: mainline/uspace/drv/audio/sb16/mixer.c

Last change on this file was 3bacee1, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Make ccheck-fix again and commit more good files.

  • Property mode set to 100644
File size: 7.8 KB
Line 
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
35#include "ddf_log.h"
36#include "mixer.h"
37
38typedef struct channel {
39 const char *name;
40 uint8_t address;
41 unsigned shift;
42 unsigned volume_levels;
43 bool preserve_bits;
44} channel_t;
45
46/* CT1335 channels */
47static const channel_t channels_table_ct1335[] = {
48 { "Master", 0x02, 1, 8, false }, /* Master, Mono, 3bit volume level */
49 { "Midi", 0x06, 1, 8, false }, /* Midi, Mono, 3bit volume level */
50 { "CD", 0x08, 1, 8, false }, /* CD, Mono, 3bit volume level */
51 { "Voice", 0x0a, 1, 4, false }, /* Voice, Mono, 2bit volume level */
52};
53
54/* CT1345 channels */
55static const channel_t channels_table_ct1345[] = {
56 { "Master Left", 0x22, 5, 8, true }, /* Master, Left, 3bit volume level */
57 { "Master Right", 0x22, 1, 8, true }, /* Master, Right, 3bit volume level */
58 { "MIDI Left", 0x26, 5, 8, true }, /* Midi, Left, 3bit volume level */
59 { "MIDI Right", 0x26, 1, 8, true }, /* Midi, Right, 3bit volume level */
60 { "CD Left", 0x28, 5, 8, true }, /* CD, Left, 3bit volume level */
61 { "CD Right", 0x28, 1, 8, true }, /* CD, Right, 3bit volume level */
62 { "Line In Left", 0x2e, 5, 8, true }, /* Line, Left, 3bit volume level */
63 { "Line In Right", 0x2e, 1, 8, true }, /* Line, Right, 3bit volume level */
64 { "Voice Left", 0x04, 5, 8, true }, /* Voice, Left, 3bit volume level */
65 { "Voice Right", 0x04, 1, 8, true }, /* Voice, Right, 3bit volume level */
66 { "Mic", 0x0a, 1, 4, false }, /* Mic, Mono, 2bit volume level */
67};
68
69/* CT1745 channels */
70static const channel_t channels_table_ct1745[] = {
71 { "Master Left", 0x30, 3, 32, false }, /* Master, Left, 5bit volume level */
72 { "Master Right", 0x31, 3, 32, false }, /* Master, Right, 5bit volume level */
73 { "Voice Left", 0x32, 3, 32, false }, /* Voice, Left, 5bit volume level */
74 { "Voice Right", 0x33, 3, 32, false }, /* Voice, Right, 5bit volume level */
75 { "MIDI Left", 0x34, 3, 32, false }, /* MIDI, Left, 5bit volume level */
76 { "MIDI Right", 0x35, 3, 32, false }, /* MIDI, Right, 5bit volume level */
77 { "CD Left", 0x36, 3, 32, false }, /* CD, Left, 5bit volume level */
78 { "CD Right", 0x37, 3, 32, false }, /* CD, Right, 5bit volume level */
79 { "Line In Left", 0x38, 3, 32, false }, /* Line, Left, 5bit volume level */
80 { "Line In Right", 0x39, 3, 32, false }, /* Line, Right, 5bit volume level */
81 { "Mic", 0x3a, 3, 32, false }, /* Mic, Mono, 5bit volume level */
82 { "PC Speaker", 0x3b, 6, 4, false }, /* PC speaker, Mono, 2bit level */
83 { "Input Gain Left", 0x3f, 6, 4, false }, /* Input Gain, Left, 2bit level */
84 { "Input Gain Right", 0x40, 6, 4, false }, /* Input Gain, Right, 2bit level */
85 { "Output Gain Left", 0x41, 6, 4, false }, /* Output Gain, Left, 2bit level */
86 { "Output Gain Right", 0x42, 6, 4, false }, /* Output Gain, Right, 2bit level */
87 { "Treble Left", 0x44, 4, 16, false }, /* Treble, Left, 4bit volume level */
88 { "Treble Right", 0x45, 4, 16, false }, /* Treble, Right, 4bit volume level */
89 { "Bass Left", 0x46, 4, 16, false }, /* Bass, Left, 4bit volume level */
90 { "Bass Right", 0x47, 4, 16, false }, /* Bass, Right, 4bit volume level */
91};
92
93static const struct {
94 const channel_t *table;
95 size_t count;
96} volume_table[] = {
97 [SB_MIXER_NONE] = { NULL, 0 },
98 [SB_MIXER_UNKNOWN] = { NULL, 0 },
99 [SB_MIXER_CT1335] = {
100 channels_table_ct1335,
101 ARRAY_SIZE(channels_table_ct1335),
102 },
103 [SB_MIXER_CT1345] = {
104 channels_table_ct1345,
105 ARRAY_SIZE(channels_table_ct1345),
106 },
107 [SB_MIXER_CT1745] = {
108 channels_table_ct1745,
109 ARRAY_SIZE(channels_table_ct1745),
110 },
111};
112
113const char *sb_mixer_type_str(sb_mixer_type_t type)
114{
115 static const char *names[] = {
116 [SB_MIXER_NONE] = "No mixer",
117 [SB_MIXER_CT1335] = "CT 1335",
118 [SB_MIXER_CT1345] = "CT 1345",
119 [SB_MIXER_CT1745] = "CT 1745",
120 [SB_MIXER_UNKNOWN] = "Unknown mixer",
121 };
122 return names[type];
123}
124
125errno_t sb_mixer_init(sb_mixer_t *mixer, sb16_regs_t *regs, sb_mixer_type_t type)
126{
127 assert(mixer);
128 mixer->regs = regs;
129 mixer->type = type;
130 if (type == SB_MIXER_UNKNOWN)
131 return ENOTSUP;
132
133 if (type != SB_MIXER_NONE) {
134 pio_write_8(&regs->mixer_address, MIXER_RESET_ADDRESS);
135 pio_write_8(&regs->mixer_data, 1);
136 }
137 pio_write_8(&regs->mixer_address, MIXER_PNP_IRQ_ADDRESS);
138 const uint8_t irq = pio_read_8(&regs->mixer_data);
139 pio_write_8(&regs->mixer_address, MIXER_PNP_DMA_ADDRESS);
140 const uint8_t dma = pio_read_8(&regs->mixer_data);
141 ddf_log_debug("SB16 setup with IRQ 0x%hhx and DMA 0x%hhx.", irq, dma);
142 return EOK;
143}
144
145int sb_mixer_get_control_item_count(const sb_mixer_t *mixer)
146{
147 assert(mixer);
148 return volume_table[mixer->type].count;
149}
150
151errno_t sb_mixer_get_control_item_info(const sb_mixer_t *mixer, unsigned item,
152 const char **name, unsigned *levels)
153{
154 assert(mixer);
155 if (item > volume_table[mixer->type].count)
156 return ENOENT;
157
158 const channel_t *ch = &volume_table[mixer->type].table[item];
159 if (name)
160 *name = ch->name;
161 if (levels)
162 *levels = ch->volume_levels;
163 return EOK;
164}
165
166/**
167 * Write new volume level from mixer registers.
168 * @param mixer SB Mixer to use.
169 * @param index Control item(channel) index.
170 * @param value New volume level.
171 * @return Error code.
172 */
173errno_t sb_mixer_get_control_item_value(const sb_mixer_t *mixer, unsigned item,
174 unsigned *value)
175{
176 assert(mixer);
177 if (!value)
178 return EBADMEM;
179 if (item > volume_table[mixer->type].count)
180 return ENOENT;
181
182 const channel_t *chan = &volume_table[mixer->type].table[item];
183 pio_write_8(&mixer->regs->mixer_address, chan->address);
184 *value = (pio_read_8(&mixer->regs->mixer_data) >> chan->shift) &
185 (chan->volume_levels - 1);
186 return EOK;
187}
188
189/**
190 * Write new volume level to mixer registers.
191 * @param mixer SB Mixer to use.
192 * @param index Control item(channel) index.
193 * @param value New volume level.
194 * @return Error code.
195 */
196errno_t sb_mixer_set_control_item_value(const sb_mixer_t *mixer, unsigned item,
197 unsigned value)
198{
199 assert(mixer);
200 if (item > volume_table[mixer->type].count)
201 return ENOENT;
202
203 const channel_t *chan = &volume_table[mixer->type].table[item];
204 if (value >= chan->volume_levels)
205 value = chan->volume_levels - 1;
206
207 pio_write_8(&mixer->regs->mixer_address, chan->address);
208
209 uint8_t regv = 0;
210 if (chan->preserve_bits) {
211 regv = pio_read_8(&mixer->regs->mixer_data);
212 regv &= ~(uint8_t)((chan->volume_levels - 1) << chan->shift);
213 }
214
215 regv |= value << chan->shift;
216 pio_write_8(&mixer->regs->mixer_data, regv);
217 ddf_log_note("Item %s new value is: %u.",
218 volume_table[mixer->type].table[item].name, value);
219 return EOK;
220}
Note: See TracBrowser for help on using the repository browser.