es8316ac.c revision 1.4 1 1.4 thorpej /* $NetBSD: es8316ac.c,v 1.4 2021/01/25 13:30:20 thorpej Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2020 Jared McNeill <jmcneill (at) invisible.ca>
5 1.1 jmcneill * All rights reserved.
6 1.1 jmcneill *
7 1.1 jmcneill * Redistribution and use in source and binary forms, with or without
8 1.1 jmcneill * modification, are permitted provided that the following conditions
9 1.1 jmcneill * are met:
10 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright
11 1.1 jmcneill * notice, this list of conditions and the following disclaimer.
12 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the
14 1.1 jmcneill * documentation and/or other materials provided with the distribution.
15 1.1 jmcneill *
16 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1 jmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1 jmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1 jmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1 jmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 1.1 jmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 1.1 jmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 1.1 jmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 1.1 jmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 jmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 jmcneill * SUCH DAMAGE.
27 1.1 jmcneill */
28 1.1 jmcneill
29 1.1 jmcneill #include <sys/cdefs.h>
30 1.4 thorpej __KERNEL_RCSID(0, "$NetBSD: es8316ac.c,v 1.4 2021/01/25 13:30:20 thorpej Exp $");
31 1.1 jmcneill
32 1.1 jmcneill #include <sys/param.h>
33 1.1 jmcneill #include <sys/bus.h>
34 1.1 jmcneill #include <sys/device.h>
35 1.1 jmcneill #include <sys/intr.h>
36 1.1 jmcneill #include <sys/systm.h>
37 1.1 jmcneill #include <sys/kernel.h>
38 1.1 jmcneill #include <sys/conf.h>
39 1.1 jmcneill #include <sys/bitops.h>
40 1.1 jmcneill
41 1.1 jmcneill #include <dev/audio/audio_dai.h>
42 1.1 jmcneill
43 1.1 jmcneill #include <dev/i2c/i2cvar.h>
44 1.1 jmcneill
45 1.1 jmcneill #include <dev/fdt/fdtvar.h>
46 1.1 jmcneill
47 1.1 jmcneill #define ESCODEC_RESET_REG 0x00
48 1.1 jmcneill #define RESET_ALL __BITS(5,0)
49 1.1 jmcneill #define RESET_CSM_ON __BIT(7)
50 1.1 jmcneill #define ESCODEC_CLKMAN1_REG 0x01
51 1.1 jmcneill #define CLKMAN1_MCLK_ON __BIT(6)
52 1.1 jmcneill #define CLKMAN1_BCLK_ON __BIT(5)
53 1.1 jmcneill #define CLKMAN1_CLK_CP_ON __BIT(4)
54 1.1 jmcneill #define CLKMAN1_CLK_DAC_ON __BIT(2)
55 1.1 jmcneill #define CLKMAN1_ANACLK_DAC_ON __BIT(0)
56 1.1 jmcneill #define ESCODEC_ADC_OSR_REG 0x03
57 1.1 jmcneill #define ESCODEC_SD_CLK_REG 0x09
58 1.1 jmcneill #define SD_CLK_MSC __BIT(7)
59 1.1 jmcneill #define SD_CLK_BCLK_INV __BIT(5)
60 1.1 jmcneill #define ESCODEC_SD_ADC_REG 0x0a
61 1.1 jmcneill #define ESCODEC_SD_DAC_REG 0x0b
62 1.1 jmcneill #define SD_FMT_LRP __BIT(5)
63 1.1 jmcneill #define SD_FMT_WL __BITS(4,2)
64 1.1 jmcneill #define SD_FMT_WL_16 3
65 1.1 jmcneill #define SD_FMT_MASK __BITS(1,0)
66 1.1 jmcneill #define SD_FMT_I2S 0
67 1.1 jmcneill #define ESCODEC_VMID_REG 0x0c
68 1.1 jmcneill #define ESCODEC_PDN_REG 0x0d
69 1.1 jmcneill #define ESCODEC_HPSEL_REG 0x13
70 1.1 jmcneill #define ESCODEC_HPMIXRT_REG 0x14
71 1.1 jmcneill #define HPMIXRT_LD2LHPMIX __BIT(7)
72 1.1 jmcneill #define HPMIXRT_RD2RHPMIX __BIT(3)
73 1.1 jmcneill #define ESCODEC_HPMIX_REG 0x15
74 1.1 jmcneill #define HPMIX_LHPMIX_MUTE __BIT(5)
75 1.1 jmcneill #define HPMIX_PDN_LHP_MIX __BIT(4)
76 1.1 jmcneill #define HPMIX_RHPMIX_MUTE __BIT(1)
77 1.1 jmcneill #define HPMIX_PDN_RHP_MIX __BIT(0)
78 1.1 jmcneill #define ESCODEC_HPMIXVOL_REG 0x16
79 1.1 jmcneill #define HPMIXVOL_LHPMIXVOL __BITS(7,4)
80 1.1 jmcneill #define HPMIXVOL_RHPMIXVOL __BITS(3,0)
81 1.1 jmcneill #define ESCODEC_HPOUTEN_REG 0x17
82 1.1 jmcneill #define HPOUTEN_EN_HPL __BIT(6)
83 1.1 jmcneill #define HPOUTEN_HPL_OUTEN __BIT(5)
84 1.1 jmcneill #define HPOUTEN_EN_HPR __BIT(2)
85 1.1 jmcneill #define HPOUTEN_HPR_OUTEN __BIT(1)
86 1.1 jmcneill #define ESCODEC_HPVOL_REG 0x18
87 1.1 jmcneill #define HPVOL_PDN_LICAL __BIT(7)
88 1.1 jmcneill #define HPVOL_HPLVOL __BITS(5,4)
89 1.1 jmcneill #define HPVOL_PDN_RICAL __BIT(3)
90 1.1 jmcneill #define HPVOL_HPRVOL __BITS(1,0)
91 1.1 jmcneill #define ESCODEC_HPPWR_REG 0x19
92 1.1 jmcneill #define HPPWR_PDN_CPHP __BIT(2)
93 1.1 jmcneill #define ESCODEC_CPPWR_REG 0x1a
94 1.1 jmcneill #define CPPWR_PDN_CP __BIT(5)
95 1.1 jmcneill #define ESCODEC_DACPWR_REG 0x2f
96 1.1 jmcneill #define DACPWR_PDN_DAC_L __BIT(4)
97 1.1 jmcneill #define DACPWR_PDN_DAC_R __BIT(0)
98 1.1 jmcneill #define ESCODEC_DACCTL1_REG 0x30
99 1.1 jmcneill #define DACCTL1_MUTE __BIT(5)
100 1.1 jmcneill #define ESCODEC_DACVOL_L_REG 0x33
101 1.1 jmcneill #define DACVOL_L_DACVOLUME __BITS(7,0)
102 1.1 jmcneill #define ESCODEC_DACVOL_R_REG 0x34
103 1.1 jmcneill #define DACVOL_R_DACVOLUME __BITS(7,0)
104 1.1 jmcneill
105 1.1 jmcneill static const struct device_compatible_entry compat_data[] = {
106 1.3 thorpej { .compat = "everest,es8316" },
107 1.4 thorpej { }
108 1.1 jmcneill };
109 1.1 jmcneill
110 1.1 jmcneill struct escodec_softc {
111 1.1 jmcneill device_t sc_dev;
112 1.1 jmcneill i2c_tag_t sc_i2c;
113 1.1 jmcneill i2c_addr_t sc_addr;
114 1.1 jmcneill int sc_phandle;
115 1.1 jmcneill struct clk *sc_clk;
116 1.1 jmcneill
117 1.1 jmcneill struct audio_dai_device sc_dai;
118 1.1 jmcneill audio_dai_tag_t sc_amplifier;
119 1.1 jmcneill
120 1.1 jmcneill uint8_t sc_swvol;
121 1.1 jmcneill };
122 1.1 jmcneill
123 1.1 jmcneill static void
124 1.1 jmcneill escodec_lock(struct escodec_softc *sc)
125 1.1 jmcneill {
126 1.1 jmcneill iic_acquire_bus(sc->sc_i2c, 0);
127 1.1 jmcneill }
128 1.1 jmcneill
129 1.1 jmcneill static void
130 1.1 jmcneill escodec_unlock(struct escodec_softc *sc)
131 1.1 jmcneill {
132 1.1 jmcneill iic_release_bus(sc->sc_i2c, 0);
133 1.1 jmcneill }
134 1.1 jmcneill
135 1.1 jmcneill static uint8_t
136 1.1 jmcneill escodec_read(struct escodec_softc *sc, uint8_t reg)
137 1.1 jmcneill {
138 1.1 jmcneill uint8_t val;
139 1.1 jmcneill
140 1.2 jmcneill if (iic_smbus_read_byte(sc->sc_i2c, sc->sc_addr, reg, &val, 0) != 0)
141 1.1 jmcneill val = 0xff;
142 1.1 jmcneill
143 1.1 jmcneill return val;
144 1.1 jmcneill }
145 1.1 jmcneill
146 1.1 jmcneill static void
147 1.1 jmcneill escodec_write(struct escodec_softc *sc, uint8_t reg, uint8_t val)
148 1.1 jmcneill {
149 1.2 jmcneill (void)iic_smbus_write_byte(sc->sc_i2c, sc->sc_addr, reg, val, 0);
150 1.1 jmcneill }
151 1.1 jmcneill
152 1.1 jmcneill enum escodec_mixer_ctrl {
153 1.1 jmcneill ESCODEC_OUTPUT_CLASS,
154 1.1 jmcneill ESCODEC_INPUT_CLASS,
155 1.1 jmcneill ESCODEC_OUTPUT_MASTER,
156 1.1 jmcneill ESCODEC_INPUT_DAC,
157 1.1 jmcneill ESCODEC_INPUT_DAC_MUTE,
158 1.1 jmcneill ESCODEC_INPUT_HEADPHONE,
159 1.1 jmcneill ESCODEC_INPUT_MIXEROUT,
160 1.1 jmcneill ESCODEC_INPUT_MIXEROUT_MUTE,
161 1.1 jmcneill
162 1.1 jmcneill ESCODEC_MIXER_CTRL_LAST
163 1.1 jmcneill };
164 1.1 jmcneill
165 1.1 jmcneill enum escodec_mixer_type {
166 1.1 jmcneill ESCODEC_MIXER_CLASS,
167 1.1 jmcneill ESCODEC_MIXER_SWVOL,
168 1.1 jmcneill ESCODEC_MIXER_AMPLIFIER,
169 1.1 jmcneill ESCODEC_MIXER_ATTENUATOR,
170 1.1 jmcneill ESCODEC_MIXER_MUTE,
171 1.1 jmcneill };
172 1.1 jmcneill
173 1.1 jmcneill static const struct escodec_mixer {
174 1.1 jmcneill const char * name;
175 1.1 jmcneill int mixer_class;
176 1.1 jmcneill int prev, next;
177 1.1 jmcneill enum escodec_mixer_ctrl ctrl;
178 1.1 jmcneill enum escodec_mixer_type type;
179 1.1 jmcneill u_int reg[2];
180 1.1 jmcneill uint8_t mask[2];
181 1.1 jmcneill uint8_t maxval;
182 1.1 jmcneill } escodec_mixers[ESCODEC_MIXER_CTRL_LAST] = {
183 1.1 jmcneill /*
184 1.1 jmcneill * Mixer classes
185 1.1 jmcneill */
186 1.1 jmcneill [ESCODEC_OUTPUT_CLASS] = {
187 1.1 jmcneill .name = AudioCoutputs,
188 1.1 jmcneill .type = ESCODEC_MIXER_CLASS,
189 1.1 jmcneill },
190 1.1 jmcneill [ESCODEC_INPUT_CLASS] = {
191 1.1 jmcneill .name = AudioCinputs,
192 1.1 jmcneill .type = ESCODEC_MIXER_CLASS,
193 1.1 jmcneill },
194 1.1 jmcneill
195 1.1 jmcneill /*
196 1.1 jmcneill * Software master volume
197 1.1 jmcneill */
198 1.1 jmcneill [ESCODEC_OUTPUT_MASTER] = {
199 1.1 jmcneill .name = AudioNmaster,
200 1.1 jmcneill .mixer_class = ESCODEC_OUTPUT_CLASS,
201 1.1 jmcneill .prev = AUDIO_MIXER_LAST,
202 1.1 jmcneill .next = AUDIO_MIXER_LAST,
203 1.1 jmcneill .type = ESCODEC_MIXER_SWVOL,
204 1.1 jmcneill },
205 1.1 jmcneill
206 1.1 jmcneill /*
207 1.1 jmcneill * Stereo DAC
208 1.1 jmcneill */
209 1.1 jmcneill [ESCODEC_INPUT_DAC] = {
210 1.1 jmcneill .name = AudioNdac,
211 1.1 jmcneill .mixer_class = ESCODEC_INPUT_CLASS,
212 1.1 jmcneill .prev = AUDIO_MIXER_LAST,
213 1.1 jmcneill .next = ESCODEC_INPUT_DAC_MUTE,
214 1.1 jmcneill .type = ESCODEC_MIXER_ATTENUATOR,
215 1.1 jmcneill .reg = {
216 1.1 jmcneill [AUDIO_MIXER_LEVEL_LEFT] = ESCODEC_DACVOL_L_REG,
217 1.1 jmcneill [AUDIO_MIXER_LEVEL_RIGHT] = ESCODEC_DACVOL_R_REG,
218 1.1 jmcneill },
219 1.1 jmcneill .mask = {
220 1.1 jmcneill [AUDIO_MIXER_LEVEL_LEFT] = DACVOL_L_DACVOLUME,
221 1.1 jmcneill [AUDIO_MIXER_LEVEL_RIGHT] = DACVOL_R_DACVOLUME,
222 1.1 jmcneill },
223 1.1 jmcneill .maxval = 0xc0,
224 1.1 jmcneill },
225 1.1 jmcneill [ESCODEC_INPUT_DAC_MUTE] = {
226 1.1 jmcneill .name = AudioNmute,
227 1.1 jmcneill .mixer_class = ESCODEC_INPUT_CLASS,
228 1.1 jmcneill .prev = ESCODEC_INPUT_DAC,
229 1.1 jmcneill .next = AUDIO_MIXER_LAST,
230 1.1 jmcneill .type = ESCODEC_MIXER_MUTE,
231 1.1 jmcneill .reg = {
232 1.1 jmcneill [AUDIO_MIXER_LEVEL_MONO] = ESCODEC_DACCTL1_REG,
233 1.1 jmcneill },
234 1.1 jmcneill .mask = {
235 1.1 jmcneill [AUDIO_MIXER_LEVEL_MONO] = DACCTL1_MUTE,
236 1.1 jmcneill }
237 1.1 jmcneill },
238 1.1 jmcneill
239 1.1 jmcneill /*
240 1.1 jmcneill * Charge Pump Headphones
241 1.1 jmcneill */
242 1.1 jmcneill [ESCODEC_INPUT_HEADPHONE] = {
243 1.1 jmcneill .name = AudioNheadphone,
244 1.1 jmcneill .mixer_class = ESCODEC_INPUT_CLASS,
245 1.1 jmcneill .prev = AUDIO_MIXER_LAST,
246 1.1 jmcneill .next = AUDIO_MIXER_LAST,
247 1.1 jmcneill .type = ESCODEC_MIXER_ATTENUATOR,
248 1.1 jmcneill .reg = {
249 1.1 jmcneill [AUDIO_MIXER_LEVEL_LEFT] = ESCODEC_HPVOL_REG,
250 1.1 jmcneill [AUDIO_MIXER_LEVEL_RIGHT] = ESCODEC_HPVOL_REG,
251 1.1 jmcneill },
252 1.1 jmcneill .mask = {
253 1.1 jmcneill [AUDIO_MIXER_LEVEL_LEFT] = HPVOL_HPLVOL,
254 1.1 jmcneill [AUDIO_MIXER_LEVEL_RIGHT] = HPVOL_HPRVOL,
255 1.1 jmcneill }
256 1.1 jmcneill },
257 1.1 jmcneill
258 1.1 jmcneill /*
259 1.1 jmcneill * Headphone mixer
260 1.1 jmcneill */
261 1.1 jmcneill [ESCODEC_INPUT_MIXEROUT] = {
262 1.1 jmcneill .name = AudioNmixerout,
263 1.1 jmcneill .mixer_class = ESCODEC_INPUT_CLASS,
264 1.1 jmcneill .prev = AUDIO_MIXER_LAST,
265 1.1 jmcneill .next = ESCODEC_INPUT_MIXEROUT_MUTE,
266 1.1 jmcneill .type = ESCODEC_MIXER_AMPLIFIER,
267 1.1 jmcneill .reg = {
268 1.1 jmcneill [AUDIO_MIXER_LEVEL_LEFT] = ESCODEC_HPMIXVOL_REG,
269 1.1 jmcneill [AUDIO_MIXER_LEVEL_RIGHT] = ESCODEC_HPMIXVOL_REG,
270 1.1 jmcneill },
271 1.1 jmcneill .mask = {
272 1.1 jmcneill [AUDIO_MIXER_LEVEL_LEFT] = HPMIXVOL_LHPMIXVOL,
273 1.1 jmcneill [AUDIO_MIXER_LEVEL_RIGHT] = HPMIXVOL_RHPMIXVOL
274 1.1 jmcneill },
275 1.1 jmcneill /*
276 1.1 jmcneill * Datasheet says this field goes up to 0xb, but values
277 1.1 jmcneill * above 0x4 result in noisy output in practice.
278 1.1 jmcneill */
279 1.1 jmcneill .maxval = 0x4,
280 1.1 jmcneill },
281 1.1 jmcneill [ESCODEC_INPUT_MIXEROUT_MUTE] = {
282 1.1 jmcneill .name = AudioNmute,
283 1.1 jmcneill .mixer_class = ESCODEC_INPUT_CLASS,
284 1.1 jmcneill .prev = ESCODEC_INPUT_MIXEROUT,
285 1.1 jmcneill .next = AUDIO_MIXER_LAST,
286 1.1 jmcneill .type = ESCODEC_MIXER_MUTE,
287 1.1 jmcneill .reg = {
288 1.1 jmcneill [AUDIO_MIXER_LEVEL_MONO] = ESCODEC_HPMIX_REG,
289 1.1 jmcneill },
290 1.1 jmcneill .mask = {
291 1.1 jmcneill [AUDIO_MIXER_LEVEL_MONO] = HPMIX_LHPMIX_MUTE | HPMIX_RHPMIX_MUTE,
292 1.1 jmcneill }
293 1.1 jmcneill },
294 1.1 jmcneill };
295 1.1 jmcneill
296 1.1 jmcneill static void
297 1.1 jmcneill escodec_swvol_codec(audio_filter_arg_t *arg)
298 1.1 jmcneill {
299 1.1 jmcneill struct escodec_softc * const sc = arg->context;
300 1.1 jmcneill const aint_t *src;
301 1.1 jmcneill aint_t *dst;
302 1.1 jmcneill u_int sample_count;
303 1.1 jmcneill u_int i;
304 1.1 jmcneill
305 1.1 jmcneill src = arg->src;
306 1.1 jmcneill dst = arg->dst;
307 1.1 jmcneill sample_count = arg->count * arg->srcfmt->channels;
308 1.1 jmcneill for (i = 0; i < sample_count; i++) {
309 1.1 jmcneill aint2_t v = (aint2_t)(*src++);
310 1.1 jmcneill v = v * sc->sc_swvol / 255;
311 1.1 jmcneill *dst++ = (aint_t)v;
312 1.1 jmcneill }
313 1.1 jmcneill }
314 1.1 jmcneill
315 1.1 jmcneill static const struct escodec_mixer *
316 1.1 jmcneill escodec_get_mixer(u_int index)
317 1.1 jmcneill {
318 1.1 jmcneill if (index >= ESCODEC_MIXER_CTRL_LAST)
319 1.1 jmcneill return NULL;
320 1.1 jmcneill
321 1.1 jmcneill return &escodec_mixers[index];
322 1.1 jmcneill }
323 1.1 jmcneill
324 1.1 jmcneill static int
325 1.1 jmcneill escodec_set_format(void *priv, int setmode,
326 1.1 jmcneill const audio_params_t *play, const audio_params_t *rec,
327 1.1 jmcneill audio_filter_reg_t *pfil, audio_filter_reg_t *rfil)
328 1.1 jmcneill {
329 1.1 jmcneill struct escodec_softc * const sc = priv;
330 1.1 jmcneill
331 1.1 jmcneill pfil->codec = escodec_swvol_codec;
332 1.1 jmcneill pfil->context = sc;
333 1.1 jmcneill
334 1.1 jmcneill return 0;
335 1.1 jmcneill }
336 1.1 jmcneill
337 1.1 jmcneill static int
338 1.1 jmcneill escodec_set_port(void *priv, mixer_ctrl_t *mc)
339 1.1 jmcneill {
340 1.1 jmcneill struct escodec_softc * const sc = priv;
341 1.1 jmcneill const struct escodec_mixer *mix;
342 1.1 jmcneill int nvol, shift, ch;
343 1.1 jmcneill uint8_t val;
344 1.1 jmcneill
345 1.1 jmcneill if ((mix = escodec_get_mixer(mc->dev)) == NULL)
346 1.1 jmcneill return ENXIO;
347 1.1 jmcneill
348 1.1 jmcneill switch (mix->type) {
349 1.1 jmcneill case ESCODEC_MIXER_SWVOL:
350 1.1 jmcneill sc->sc_swvol = mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT];
351 1.1 jmcneill return 0;
352 1.1 jmcneill
353 1.1 jmcneill case ESCODEC_MIXER_AMPLIFIER:
354 1.1 jmcneill case ESCODEC_MIXER_ATTENUATOR:
355 1.1 jmcneill escodec_lock(sc);
356 1.1 jmcneill for (ch = 0; ch < 2; ch++) {
357 1.1 jmcneill val = escodec_read(sc, mix->reg[ch]);
358 1.1 jmcneill shift = 8 - fls32(__SHIFTOUT_MASK(mix->mask[ch]));
359 1.1 jmcneill nvol = mc->un.value.level[ch] >> shift;
360 1.1 jmcneill if (mix->type == ESCODEC_MIXER_ATTENUATOR)
361 1.1 jmcneill nvol = __SHIFTOUT_MASK(mix->mask[ch]) - nvol;
362 1.1 jmcneill if (mix->maxval != 0 && nvol > mix->maxval)
363 1.1 jmcneill nvol = mix->maxval;
364 1.1 jmcneill
365 1.1 jmcneill val &= ~mix->mask[ch];
366 1.1 jmcneill val |= __SHIFTIN(nvol, mix->mask[ch]);
367 1.1 jmcneill escodec_write(sc, mix->reg[ch], val);
368 1.1 jmcneill }
369 1.1 jmcneill escodec_unlock(sc);
370 1.1 jmcneill return 0;
371 1.1 jmcneill
372 1.1 jmcneill case ESCODEC_MIXER_MUTE:
373 1.1 jmcneill if (mc->un.ord < 0 || mc->un.ord > 1)
374 1.1 jmcneill return EINVAL;
375 1.1 jmcneill escodec_lock(sc);
376 1.1 jmcneill val = escodec_read(sc, mix->reg[0]);
377 1.1 jmcneill if (mc->un.ord)
378 1.1 jmcneill val |= mix->mask[0];
379 1.1 jmcneill else
380 1.1 jmcneill val &= ~mix->mask[0];
381 1.1 jmcneill escodec_write(sc, mix->reg[0], val);
382 1.1 jmcneill escodec_unlock(sc);
383 1.1 jmcneill return 0;
384 1.1 jmcneill
385 1.1 jmcneill default:
386 1.1 jmcneill return ENXIO;
387 1.1 jmcneill }
388 1.1 jmcneill }
389 1.1 jmcneill
390 1.1 jmcneill static int
391 1.1 jmcneill escodec_get_port(void *priv, mixer_ctrl_t *mc)
392 1.1 jmcneill {
393 1.1 jmcneill struct escodec_softc * const sc = priv;
394 1.1 jmcneill const struct escodec_mixer *mix;
395 1.1 jmcneill int nvol, shift, ch;
396 1.1 jmcneill uint8_t val;
397 1.1 jmcneill
398 1.1 jmcneill if ((mix = escodec_get_mixer(mc->dev)) == NULL)
399 1.1 jmcneill return ENXIO;
400 1.1 jmcneill
401 1.1 jmcneill switch (mix->type) {
402 1.1 jmcneill case ESCODEC_MIXER_SWVOL:
403 1.1 jmcneill mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = sc->sc_swvol;
404 1.1 jmcneill mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = sc->sc_swvol;
405 1.1 jmcneill return 0;
406 1.1 jmcneill
407 1.1 jmcneill case ESCODEC_MIXER_AMPLIFIER:
408 1.1 jmcneill case ESCODEC_MIXER_ATTENUATOR:
409 1.1 jmcneill escodec_lock(sc);
410 1.1 jmcneill for (ch = 0; ch < 2; ch++) {
411 1.1 jmcneill val = escodec_read(sc, mix->reg[ch]);
412 1.1 jmcneill shift = 8 - fls32(__SHIFTOUT_MASK(mix->mask[ch]));
413 1.1 jmcneill nvol = __SHIFTOUT(val, mix->mask[ch]);
414 1.1 jmcneill if (mix->type == ESCODEC_MIXER_ATTENUATOR)
415 1.1 jmcneill nvol = __SHIFTOUT_MASK(mix->mask[ch]) - nvol;
416 1.1 jmcneill nvol <<= shift;
417 1.1 jmcneill mc->un.value.level[ch] = nvol;
418 1.1 jmcneill }
419 1.1 jmcneill escodec_unlock(sc);
420 1.1 jmcneill return 0;
421 1.1 jmcneill
422 1.1 jmcneill case ESCODEC_MIXER_MUTE:
423 1.1 jmcneill escodec_lock(sc);
424 1.1 jmcneill val = escodec_read(sc, mix->reg[0]);
425 1.1 jmcneill mc->un.ord = (val & mix->mask[0]) != 0;
426 1.1 jmcneill escodec_unlock(sc);
427 1.1 jmcneill return 0;
428 1.1 jmcneill
429 1.1 jmcneill default:
430 1.1 jmcneill return ENXIO;
431 1.1 jmcneill }
432 1.1 jmcneill }
433 1.1 jmcneill
434 1.1 jmcneill static int
435 1.1 jmcneill escodec_query_devinfo(void *priv, mixer_devinfo_t *di)
436 1.1 jmcneill {
437 1.1 jmcneill const struct escodec_mixer *mix;
438 1.1 jmcneill
439 1.1 jmcneill if ((mix = escodec_get_mixer(di->index)) == NULL)
440 1.1 jmcneill return ENXIO;
441 1.1 jmcneill
442 1.1 jmcneill strcpy(di->label.name, mix->name);
443 1.1 jmcneill di->mixer_class = mix->mixer_class;
444 1.1 jmcneill di->next = mix->next;
445 1.1 jmcneill di->prev = mix->prev;
446 1.1 jmcneill
447 1.1 jmcneill switch (mix->type) {
448 1.1 jmcneill case ESCODEC_MIXER_CLASS:
449 1.1 jmcneill di->type = AUDIO_MIXER_CLASS;
450 1.1 jmcneill return 0;
451 1.1 jmcneill
452 1.1 jmcneill case ESCODEC_MIXER_SWVOL:
453 1.1 jmcneill di->type = AUDIO_MIXER_VALUE;
454 1.1 jmcneill di->un.v.delta = 1;
455 1.1 jmcneill di->un.v.num_channels = 2;
456 1.1 jmcneill strcpy(di->un.v.units.name, AudioNvolume);
457 1.1 jmcneill return 0;
458 1.1 jmcneill
459 1.1 jmcneill case ESCODEC_MIXER_AMPLIFIER:
460 1.1 jmcneill case ESCODEC_MIXER_ATTENUATOR:
461 1.1 jmcneill di->type = AUDIO_MIXER_VALUE;
462 1.1 jmcneill di->un.v.delta =
463 1.1 jmcneill 256 / (__SHIFTOUT_MASK(mix->mask[0]) + 1);
464 1.1 jmcneill di->un.v.num_channels = 2;
465 1.1 jmcneill strcpy(di->un.v.units.name, AudioNvolume);
466 1.1 jmcneill return 0;
467 1.1 jmcneill
468 1.1 jmcneill case ESCODEC_MIXER_MUTE:
469 1.1 jmcneill di->type = AUDIO_MIXER_ENUM;
470 1.1 jmcneill di->un.e.num_mem = 2;
471 1.1 jmcneill strcpy(di->un.e.member[0].label.name, AudioNoff);
472 1.1 jmcneill di->un.e.member[0].ord = 0;
473 1.1 jmcneill strcpy(di->un.e.member[1].label.name, AudioNon);
474 1.1 jmcneill di->un.e.member[1].ord = 1;
475 1.1 jmcneill return 0;
476 1.1 jmcneill
477 1.1 jmcneill default:
478 1.1 jmcneill return ENXIO;
479 1.1 jmcneill }
480 1.1 jmcneill }
481 1.1 jmcneill
482 1.1 jmcneill static const struct audio_hw_if escodec_hw_if = {
483 1.1 jmcneill .set_format = escodec_set_format,
484 1.1 jmcneill .set_port = escodec_set_port,
485 1.1 jmcneill .get_port = escodec_get_port,
486 1.1 jmcneill .query_devinfo = escodec_query_devinfo,
487 1.1 jmcneill };
488 1.1 jmcneill
489 1.1 jmcneill static int
490 1.1 jmcneill escodec_dai_set_sysclk(audio_dai_tag_t dai, u_int rate, int dir)
491 1.1 jmcneill {
492 1.1 jmcneill struct escodec_softc * const sc = audio_dai_private(dai);
493 1.1 jmcneill int error;
494 1.1 jmcneill
495 1.1 jmcneill error = clk_set_rate(sc->sc_clk, rate);
496 1.1 jmcneill if (error != 0)
497 1.1 jmcneill return error;
498 1.1 jmcneill
499 1.1 jmcneill return 0;
500 1.1 jmcneill }
501 1.1 jmcneill
502 1.1 jmcneill static int
503 1.1 jmcneill escodec_dai_set_format(audio_dai_tag_t dai, u_int format)
504 1.1 jmcneill {
505 1.1 jmcneill struct escodec_softc * const sc = audio_dai_private(dai);
506 1.1 jmcneill uint8_t sd_clk, sd_fmt, val;
507 1.1 jmcneill
508 1.1 jmcneill const u_int fmt = __SHIFTOUT(format, AUDIO_DAI_FORMAT_MASK);
509 1.1 jmcneill const u_int clk = __SHIFTOUT(format, AUDIO_DAI_CLOCK_MASK);
510 1.1 jmcneill const u_int pol = __SHIFTOUT(format, AUDIO_DAI_POLARITY_MASK);
511 1.1 jmcneill
512 1.1 jmcneill if (fmt != AUDIO_DAI_FORMAT_I2S)
513 1.1 jmcneill return EINVAL;
514 1.1 jmcneill
515 1.1 jmcneill if (clk != AUDIO_DAI_CLOCK_CBS_CFS)
516 1.1 jmcneill return EINVAL;
517 1.1 jmcneill
518 1.1 jmcneill switch (pol) {
519 1.1 jmcneill case AUDIO_DAI_POLARITY_NB_NF:
520 1.1 jmcneill sd_clk = 0;
521 1.1 jmcneill sd_fmt = 0;
522 1.1 jmcneill break;
523 1.1 jmcneill case AUDIO_DAI_POLARITY_NB_IF:
524 1.1 jmcneill sd_clk = 0;
525 1.1 jmcneill sd_fmt = SD_FMT_LRP;
526 1.1 jmcneill break;
527 1.1 jmcneill case AUDIO_DAI_POLARITY_IB_NF:
528 1.1 jmcneill sd_clk = SD_CLK_BCLK_INV;
529 1.1 jmcneill sd_fmt = 0;
530 1.1 jmcneill break;
531 1.1 jmcneill case AUDIO_DAI_POLARITY_IB_IF:
532 1.1 jmcneill sd_clk = SD_CLK_BCLK_INV;
533 1.1 jmcneill sd_fmt = SD_FMT_LRP;
534 1.1 jmcneill break;
535 1.1 jmcneill }
536 1.1 jmcneill
537 1.1 jmcneill escodec_lock(sc);
538 1.1 jmcneill
539 1.1 jmcneill val = escodec_read(sc, ESCODEC_SD_CLK_REG);
540 1.1 jmcneill val &= ~(SD_CLK_MSC|SD_CLK_BCLK_INV);
541 1.1 jmcneill val |= sd_clk;
542 1.1 jmcneill escodec_write(sc, ESCODEC_SD_CLK_REG, val);
543 1.1 jmcneill
544 1.1 jmcneill val = escodec_read(sc, ESCODEC_SD_ADC_REG);
545 1.1 jmcneill val &= ~SD_FMT_MASK;
546 1.1 jmcneill val |= __SHIFTIN(SD_FMT_I2S, SD_FMT_MASK);
547 1.1 jmcneill val &= ~SD_FMT_LRP;
548 1.1 jmcneill val |= sd_fmt;
549 1.1 jmcneill escodec_write(sc, ESCODEC_SD_ADC_REG, val);
550 1.1 jmcneill
551 1.1 jmcneill val = escodec_read(sc, ESCODEC_SD_DAC_REG);
552 1.1 jmcneill val &= ~SD_FMT_MASK;
553 1.1 jmcneill val |= __SHIFTIN(SD_FMT_I2S, SD_FMT_MASK);
554 1.1 jmcneill val &= ~SD_FMT_LRP;
555 1.1 jmcneill val |= sd_fmt;
556 1.1 jmcneill escodec_write(sc, ESCODEC_SD_DAC_REG, val);
557 1.1 jmcneill
558 1.1 jmcneill val = escodec_read(sc, ESCODEC_CLKMAN1_REG);
559 1.1 jmcneill val |= CLKMAN1_MCLK_ON;
560 1.1 jmcneill val |= CLKMAN1_BCLK_ON;
561 1.1 jmcneill val |= CLKMAN1_CLK_CP_ON;
562 1.1 jmcneill val |= CLKMAN1_CLK_DAC_ON;
563 1.1 jmcneill val |= CLKMAN1_ANACLK_DAC_ON;
564 1.1 jmcneill escodec_write(sc, ESCODEC_CLKMAN1_REG, val);
565 1.1 jmcneill
566 1.1 jmcneill escodec_unlock(sc);
567 1.1 jmcneill
568 1.1 jmcneill return 0;
569 1.1 jmcneill }
570 1.1 jmcneill
571 1.1 jmcneill static int
572 1.1 jmcneill escodec_dai_add_device(audio_dai_tag_t dai, audio_dai_tag_t aux)
573 1.1 jmcneill {
574 1.1 jmcneill struct escodec_softc * const sc = audio_dai_private(dai);
575 1.1 jmcneill
576 1.1 jmcneill if (sc->sc_amplifier != NULL)
577 1.1 jmcneill return 0;
578 1.1 jmcneill
579 1.1 jmcneill sc->sc_amplifier = aux;
580 1.1 jmcneill
581 1.1 jmcneill return 0;
582 1.1 jmcneill }
583 1.1 jmcneill
584 1.1 jmcneill static audio_dai_tag_t
585 1.1 jmcneill escodec_dai_get_tag(device_t dev, const void *data, size_t len)
586 1.1 jmcneill {
587 1.1 jmcneill struct escodec_softc * const sc = device_private(dev);
588 1.1 jmcneill
589 1.1 jmcneill if (len != 4)
590 1.1 jmcneill return NULL;
591 1.1 jmcneill
592 1.1 jmcneill return &sc->sc_dai;
593 1.1 jmcneill }
594 1.1 jmcneill
595 1.1 jmcneill static struct fdtbus_dai_controller_func escodec_dai_funcs = {
596 1.1 jmcneill .get_tag = escodec_dai_get_tag
597 1.1 jmcneill };
598 1.1 jmcneill
599 1.1 jmcneill static void
600 1.1 jmcneill escodec_init(struct escodec_softc *sc)
601 1.1 jmcneill {
602 1.1 jmcneill uint8_t val;
603 1.1 jmcneill
604 1.1 jmcneill escodec_lock(sc);
605 1.1 jmcneill
606 1.1 jmcneill escodec_write(sc, ESCODEC_RESET_REG, RESET_ALL);
607 1.1 jmcneill delay(5000);
608 1.1 jmcneill escodec_write(sc, ESCODEC_RESET_REG, RESET_CSM_ON);
609 1.1 jmcneill delay(30000);
610 1.1 jmcneill
611 1.1 jmcneill escodec_write(sc, ESCODEC_VMID_REG, 0xff);
612 1.1 jmcneill escodec_write(sc, ESCODEC_ADC_OSR_REG, 0x32);
613 1.1 jmcneill
614 1.1 jmcneill val = escodec_read(sc, ESCODEC_SD_ADC_REG);
615 1.1 jmcneill val &= ~SD_FMT_WL;
616 1.1 jmcneill val |= __SHIFTIN(SD_FMT_WL_16, SD_FMT_WL);
617 1.1 jmcneill escodec_write(sc, ESCODEC_SD_ADC_REG, val);
618 1.1 jmcneill
619 1.1 jmcneill val = escodec_read(sc, ESCODEC_SD_DAC_REG);
620 1.1 jmcneill val &= ~SD_FMT_WL;
621 1.1 jmcneill val |= __SHIFTIN(SD_FMT_WL_16, SD_FMT_WL);
622 1.1 jmcneill escodec_write(sc, ESCODEC_SD_DAC_REG, val);
623 1.1 jmcneill
624 1.1 jmcneill /* Power up */
625 1.1 jmcneill escodec_write(sc, ESCODEC_PDN_REG, 0);
626 1.1 jmcneill
627 1.1 jmcneill /* Route DAC signal to HP mixer */
628 1.1 jmcneill val = HPMIXRT_LD2LHPMIX | HPMIXRT_RD2RHPMIX;
629 1.1 jmcneill escodec_write(sc, ESCODEC_HPMIXRT_REG, val);
630 1.1 jmcneill
631 1.1 jmcneill /* Power up DAC */
632 1.1 jmcneill escodec_write(sc, ESCODEC_DACPWR_REG, 0);
633 1.1 jmcneill
634 1.1 jmcneill /* Power up HP mixer and unmute */
635 1.1 jmcneill escodec_write(sc, ESCODEC_HPMIX_REG, 0);
636 1.1 jmcneill
637 1.1 jmcneill /* Power up HP output driver */
638 1.1 jmcneill val = escodec_read(sc, ESCODEC_HPPWR_REG);
639 1.1 jmcneill val &= ~HPPWR_PDN_CPHP;
640 1.1 jmcneill escodec_write(sc, ESCODEC_HPPWR_REG, val);
641 1.1 jmcneill
642 1.1 jmcneill /* Power up HP charge pump circuits */
643 1.1 jmcneill val = escodec_read(sc, ESCODEC_CPPWR_REG);
644 1.1 jmcneill val &= ~CPPWR_PDN_CP;
645 1.1 jmcneill escodec_write(sc, ESCODEC_CPPWR_REG, val);
646 1.1 jmcneill
647 1.1 jmcneill /* Set LIN1/RIN1 as inputs for HP mixer */
648 1.1 jmcneill escodec_write(sc, ESCODEC_HPSEL_REG, 0);
649 1.1 jmcneill
650 1.1 jmcneill /* Power up HP output driver calibration */
651 1.1 jmcneill val = escodec_read(sc, ESCODEC_HPVOL_REG);
652 1.1 jmcneill val &= ~HPVOL_PDN_LICAL;
653 1.1 jmcneill val &= ~HPVOL_PDN_RICAL;
654 1.1 jmcneill escodec_write(sc, ESCODEC_HPVOL_REG, val);
655 1.1 jmcneill
656 1.1 jmcneill /* Set headphone mixer to -6dB */
657 1.1 jmcneill escodec_write(sc, ESCODEC_HPMIXVOL_REG, 0x44);
658 1.1 jmcneill
659 1.1 jmcneill /* Set charge pump headphone to -48dB */
660 1.1 jmcneill escodec_write(sc, ESCODEC_HPVOL_REG, 0x33);
661 1.1 jmcneill
662 1.1 jmcneill /* Set DAC to 0dB */
663 1.1 jmcneill escodec_write(sc, ESCODEC_DACVOL_L_REG, 0);
664 1.1 jmcneill escodec_write(sc, ESCODEC_DACVOL_R_REG, 0);
665 1.1 jmcneill
666 1.2 jmcneill /* Enable HP output */
667 1.2 jmcneill val = HPOUTEN_EN_HPL | HPOUTEN_EN_HPR |
668 1.2 jmcneill HPOUTEN_HPL_OUTEN | HPOUTEN_HPR_OUTEN;
669 1.2 jmcneill escodec_write(sc, ESCODEC_HPOUTEN_REG, val);
670 1.2 jmcneill
671 1.1 jmcneill escodec_unlock(sc);
672 1.1 jmcneill }
673 1.1 jmcneill
674 1.1 jmcneill static int
675 1.1 jmcneill escodec_match(device_t parent, cfdata_t match, void *aux)
676 1.1 jmcneill {
677 1.1 jmcneill struct i2c_attach_args *ia = aux;
678 1.1 jmcneill int match_result;
679 1.1 jmcneill
680 1.1 jmcneill if (iic_use_direct_match(ia, match, compat_data, &match_result))
681 1.1 jmcneill return match_result;
682 1.1 jmcneill
683 1.1 jmcneill /* This device is direct-config only */
684 1.1 jmcneill
685 1.1 jmcneill return 0;
686 1.1 jmcneill }
687 1.1 jmcneill
688 1.1 jmcneill static void
689 1.1 jmcneill escodec_attach(device_t parent, device_t self, void *aux)
690 1.1 jmcneill {
691 1.1 jmcneill struct escodec_softc * const sc = device_private(self);
692 1.1 jmcneill struct i2c_attach_args * const ia = aux;
693 1.1 jmcneill const int phandle = ia->ia_cookie;
694 1.1 jmcneill
695 1.1 jmcneill sc->sc_dev = self;
696 1.1 jmcneill sc->sc_i2c = ia->ia_tag;
697 1.1 jmcneill sc->sc_addr = ia->ia_addr;
698 1.1 jmcneill sc->sc_phandle = ia->ia_cookie;
699 1.1 jmcneill sc->sc_swvol = 0xff;
700 1.1 jmcneill
701 1.1 jmcneill sc->sc_clk = fdtbus_clock_get(phandle, "mclk");
702 1.1 jmcneill if (sc->sc_clk == NULL || clk_enable(sc->sc_clk) != 0) {
703 1.1 jmcneill aprint_error(": couldn't enable mclk\n");
704 1.1 jmcneill return;
705 1.1 jmcneill }
706 1.1 jmcneill
707 1.1 jmcneill aprint_naive("\n");
708 1.1 jmcneill aprint_normal(": Everest Semi ES8316 Audio CODEC\n");
709 1.1 jmcneill
710 1.1 jmcneill escodec_init(sc);
711 1.1 jmcneill
712 1.1 jmcneill sc->sc_dai.dai_set_sysclk = escodec_dai_set_sysclk;
713 1.1 jmcneill sc->sc_dai.dai_set_format = escodec_dai_set_format;
714 1.1 jmcneill sc->sc_dai.dai_add_device = escodec_dai_add_device;
715 1.1 jmcneill sc->sc_dai.dai_hw_if = &escodec_hw_if;
716 1.1 jmcneill sc->sc_dai.dai_dev = self;
717 1.1 jmcneill sc->sc_dai.dai_priv = sc;
718 1.1 jmcneill fdtbus_register_dai_controller(self, phandle, &escodec_dai_funcs);
719 1.1 jmcneill }
720 1.1 jmcneill
721 1.1 jmcneill CFATTACH_DECL_NEW(es8316ac, sizeof(struct escodec_softc),
722 1.1 jmcneill escodec_match, escodec_attach, NULL, NULL);
723