es8316ac.c revision 1.3 1 1.3 thorpej /* $NetBSD: es8316ac.c,v 1.3 2021/01/17 21:42:35 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.3 thorpej __KERNEL_RCSID(0, "$NetBSD: es8316ac.c,v 1.3 2021/01/17 21:42:35 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.3 thorpej
108 1.3 thorpej { 0 }
109 1.1 jmcneill };
110 1.1 jmcneill
111 1.1 jmcneill struct escodec_softc {
112 1.1 jmcneill device_t sc_dev;
113 1.1 jmcneill i2c_tag_t sc_i2c;
114 1.1 jmcneill i2c_addr_t sc_addr;
115 1.1 jmcneill int sc_phandle;
116 1.1 jmcneill struct clk *sc_clk;
117 1.1 jmcneill
118 1.1 jmcneill struct audio_dai_device sc_dai;
119 1.1 jmcneill audio_dai_tag_t sc_amplifier;
120 1.1 jmcneill
121 1.1 jmcneill uint8_t sc_swvol;
122 1.1 jmcneill };
123 1.1 jmcneill
124 1.1 jmcneill static void
125 1.1 jmcneill escodec_lock(struct escodec_softc *sc)
126 1.1 jmcneill {
127 1.1 jmcneill iic_acquire_bus(sc->sc_i2c, 0);
128 1.1 jmcneill }
129 1.1 jmcneill
130 1.1 jmcneill static void
131 1.1 jmcneill escodec_unlock(struct escodec_softc *sc)
132 1.1 jmcneill {
133 1.1 jmcneill iic_release_bus(sc->sc_i2c, 0);
134 1.1 jmcneill }
135 1.1 jmcneill
136 1.1 jmcneill static uint8_t
137 1.1 jmcneill escodec_read(struct escodec_softc *sc, uint8_t reg)
138 1.1 jmcneill {
139 1.1 jmcneill uint8_t val;
140 1.1 jmcneill
141 1.2 jmcneill if (iic_smbus_read_byte(sc->sc_i2c, sc->sc_addr, reg, &val, 0) != 0)
142 1.1 jmcneill val = 0xff;
143 1.1 jmcneill
144 1.1 jmcneill return val;
145 1.1 jmcneill }
146 1.1 jmcneill
147 1.1 jmcneill static void
148 1.1 jmcneill escodec_write(struct escodec_softc *sc, uint8_t reg, uint8_t val)
149 1.1 jmcneill {
150 1.2 jmcneill (void)iic_smbus_write_byte(sc->sc_i2c, sc->sc_addr, reg, val, 0);
151 1.1 jmcneill }
152 1.1 jmcneill
153 1.1 jmcneill enum escodec_mixer_ctrl {
154 1.1 jmcneill ESCODEC_OUTPUT_CLASS,
155 1.1 jmcneill ESCODEC_INPUT_CLASS,
156 1.1 jmcneill ESCODEC_OUTPUT_MASTER,
157 1.1 jmcneill ESCODEC_INPUT_DAC,
158 1.1 jmcneill ESCODEC_INPUT_DAC_MUTE,
159 1.1 jmcneill ESCODEC_INPUT_HEADPHONE,
160 1.1 jmcneill ESCODEC_INPUT_MIXEROUT,
161 1.1 jmcneill ESCODEC_INPUT_MIXEROUT_MUTE,
162 1.1 jmcneill
163 1.1 jmcneill ESCODEC_MIXER_CTRL_LAST
164 1.1 jmcneill };
165 1.1 jmcneill
166 1.1 jmcneill enum escodec_mixer_type {
167 1.1 jmcneill ESCODEC_MIXER_CLASS,
168 1.1 jmcneill ESCODEC_MIXER_SWVOL,
169 1.1 jmcneill ESCODEC_MIXER_AMPLIFIER,
170 1.1 jmcneill ESCODEC_MIXER_ATTENUATOR,
171 1.1 jmcneill ESCODEC_MIXER_MUTE,
172 1.1 jmcneill };
173 1.1 jmcneill
174 1.1 jmcneill static const struct escodec_mixer {
175 1.1 jmcneill const char * name;
176 1.1 jmcneill int mixer_class;
177 1.1 jmcneill int prev, next;
178 1.1 jmcneill enum escodec_mixer_ctrl ctrl;
179 1.1 jmcneill enum escodec_mixer_type type;
180 1.1 jmcneill u_int reg[2];
181 1.1 jmcneill uint8_t mask[2];
182 1.1 jmcneill uint8_t maxval;
183 1.1 jmcneill } escodec_mixers[ESCODEC_MIXER_CTRL_LAST] = {
184 1.1 jmcneill /*
185 1.1 jmcneill * Mixer classes
186 1.1 jmcneill */
187 1.1 jmcneill [ESCODEC_OUTPUT_CLASS] = {
188 1.1 jmcneill .name = AudioCoutputs,
189 1.1 jmcneill .type = ESCODEC_MIXER_CLASS,
190 1.1 jmcneill },
191 1.1 jmcneill [ESCODEC_INPUT_CLASS] = {
192 1.1 jmcneill .name = AudioCinputs,
193 1.1 jmcneill .type = ESCODEC_MIXER_CLASS,
194 1.1 jmcneill },
195 1.1 jmcneill
196 1.1 jmcneill /*
197 1.1 jmcneill * Software master volume
198 1.1 jmcneill */
199 1.1 jmcneill [ESCODEC_OUTPUT_MASTER] = {
200 1.1 jmcneill .name = AudioNmaster,
201 1.1 jmcneill .mixer_class = ESCODEC_OUTPUT_CLASS,
202 1.1 jmcneill .prev = AUDIO_MIXER_LAST,
203 1.1 jmcneill .next = AUDIO_MIXER_LAST,
204 1.1 jmcneill .type = ESCODEC_MIXER_SWVOL,
205 1.1 jmcneill },
206 1.1 jmcneill
207 1.1 jmcneill /*
208 1.1 jmcneill * Stereo DAC
209 1.1 jmcneill */
210 1.1 jmcneill [ESCODEC_INPUT_DAC] = {
211 1.1 jmcneill .name = AudioNdac,
212 1.1 jmcneill .mixer_class = ESCODEC_INPUT_CLASS,
213 1.1 jmcneill .prev = AUDIO_MIXER_LAST,
214 1.1 jmcneill .next = ESCODEC_INPUT_DAC_MUTE,
215 1.1 jmcneill .type = ESCODEC_MIXER_ATTENUATOR,
216 1.1 jmcneill .reg = {
217 1.1 jmcneill [AUDIO_MIXER_LEVEL_LEFT] = ESCODEC_DACVOL_L_REG,
218 1.1 jmcneill [AUDIO_MIXER_LEVEL_RIGHT] = ESCODEC_DACVOL_R_REG,
219 1.1 jmcneill },
220 1.1 jmcneill .mask = {
221 1.1 jmcneill [AUDIO_MIXER_LEVEL_LEFT] = DACVOL_L_DACVOLUME,
222 1.1 jmcneill [AUDIO_MIXER_LEVEL_RIGHT] = DACVOL_R_DACVOLUME,
223 1.1 jmcneill },
224 1.1 jmcneill .maxval = 0xc0,
225 1.1 jmcneill },
226 1.1 jmcneill [ESCODEC_INPUT_DAC_MUTE] = {
227 1.1 jmcneill .name = AudioNmute,
228 1.1 jmcneill .mixer_class = ESCODEC_INPUT_CLASS,
229 1.1 jmcneill .prev = ESCODEC_INPUT_DAC,
230 1.1 jmcneill .next = AUDIO_MIXER_LAST,
231 1.1 jmcneill .type = ESCODEC_MIXER_MUTE,
232 1.1 jmcneill .reg = {
233 1.1 jmcneill [AUDIO_MIXER_LEVEL_MONO] = ESCODEC_DACCTL1_REG,
234 1.1 jmcneill },
235 1.1 jmcneill .mask = {
236 1.1 jmcneill [AUDIO_MIXER_LEVEL_MONO] = DACCTL1_MUTE,
237 1.1 jmcneill }
238 1.1 jmcneill },
239 1.1 jmcneill
240 1.1 jmcneill /*
241 1.1 jmcneill * Charge Pump Headphones
242 1.1 jmcneill */
243 1.1 jmcneill [ESCODEC_INPUT_HEADPHONE] = {
244 1.1 jmcneill .name = AudioNheadphone,
245 1.1 jmcneill .mixer_class = ESCODEC_INPUT_CLASS,
246 1.1 jmcneill .prev = AUDIO_MIXER_LAST,
247 1.1 jmcneill .next = AUDIO_MIXER_LAST,
248 1.1 jmcneill .type = ESCODEC_MIXER_ATTENUATOR,
249 1.1 jmcneill .reg = {
250 1.1 jmcneill [AUDIO_MIXER_LEVEL_LEFT] = ESCODEC_HPVOL_REG,
251 1.1 jmcneill [AUDIO_MIXER_LEVEL_RIGHT] = ESCODEC_HPVOL_REG,
252 1.1 jmcneill },
253 1.1 jmcneill .mask = {
254 1.1 jmcneill [AUDIO_MIXER_LEVEL_LEFT] = HPVOL_HPLVOL,
255 1.1 jmcneill [AUDIO_MIXER_LEVEL_RIGHT] = HPVOL_HPRVOL,
256 1.1 jmcneill }
257 1.1 jmcneill },
258 1.1 jmcneill
259 1.1 jmcneill /*
260 1.1 jmcneill * Headphone mixer
261 1.1 jmcneill */
262 1.1 jmcneill [ESCODEC_INPUT_MIXEROUT] = {
263 1.1 jmcneill .name = AudioNmixerout,
264 1.1 jmcneill .mixer_class = ESCODEC_INPUT_CLASS,
265 1.1 jmcneill .prev = AUDIO_MIXER_LAST,
266 1.1 jmcneill .next = ESCODEC_INPUT_MIXEROUT_MUTE,
267 1.1 jmcneill .type = ESCODEC_MIXER_AMPLIFIER,
268 1.1 jmcneill .reg = {
269 1.1 jmcneill [AUDIO_MIXER_LEVEL_LEFT] = ESCODEC_HPMIXVOL_REG,
270 1.1 jmcneill [AUDIO_MIXER_LEVEL_RIGHT] = ESCODEC_HPMIXVOL_REG,
271 1.1 jmcneill },
272 1.1 jmcneill .mask = {
273 1.1 jmcneill [AUDIO_MIXER_LEVEL_LEFT] = HPMIXVOL_LHPMIXVOL,
274 1.1 jmcneill [AUDIO_MIXER_LEVEL_RIGHT] = HPMIXVOL_RHPMIXVOL
275 1.1 jmcneill },
276 1.1 jmcneill /*
277 1.1 jmcneill * Datasheet says this field goes up to 0xb, but values
278 1.1 jmcneill * above 0x4 result in noisy output in practice.
279 1.1 jmcneill */
280 1.1 jmcneill .maxval = 0x4,
281 1.1 jmcneill },
282 1.1 jmcneill [ESCODEC_INPUT_MIXEROUT_MUTE] = {
283 1.1 jmcneill .name = AudioNmute,
284 1.1 jmcneill .mixer_class = ESCODEC_INPUT_CLASS,
285 1.1 jmcneill .prev = ESCODEC_INPUT_MIXEROUT,
286 1.1 jmcneill .next = AUDIO_MIXER_LAST,
287 1.1 jmcneill .type = ESCODEC_MIXER_MUTE,
288 1.1 jmcneill .reg = {
289 1.1 jmcneill [AUDIO_MIXER_LEVEL_MONO] = ESCODEC_HPMIX_REG,
290 1.1 jmcneill },
291 1.1 jmcneill .mask = {
292 1.1 jmcneill [AUDIO_MIXER_LEVEL_MONO] = HPMIX_LHPMIX_MUTE | HPMIX_RHPMIX_MUTE,
293 1.1 jmcneill }
294 1.1 jmcneill },
295 1.1 jmcneill };
296 1.1 jmcneill
297 1.1 jmcneill static void
298 1.1 jmcneill escodec_swvol_codec(audio_filter_arg_t *arg)
299 1.1 jmcneill {
300 1.1 jmcneill struct escodec_softc * const sc = arg->context;
301 1.1 jmcneill const aint_t *src;
302 1.1 jmcneill aint_t *dst;
303 1.1 jmcneill u_int sample_count;
304 1.1 jmcneill u_int i;
305 1.1 jmcneill
306 1.1 jmcneill src = arg->src;
307 1.1 jmcneill dst = arg->dst;
308 1.1 jmcneill sample_count = arg->count * arg->srcfmt->channels;
309 1.1 jmcneill for (i = 0; i < sample_count; i++) {
310 1.1 jmcneill aint2_t v = (aint2_t)(*src++);
311 1.1 jmcneill v = v * sc->sc_swvol / 255;
312 1.1 jmcneill *dst++ = (aint_t)v;
313 1.1 jmcneill }
314 1.1 jmcneill }
315 1.1 jmcneill
316 1.1 jmcneill static const struct escodec_mixer *
317 1.1 jmcneill escodec_get_mixer(u_int index)
318 1.1 jmcneill {
319 1.1 jmcneill if (index >= ESCODEC_MIXER_CTRL_LAST)
320 1.1 jmcneill return NULL;
321 1.1 jmcneill
322 1.1 jmcneill return &escodec_mixers[index];
323 1.1 jmcneill }
324 1.1 jmcneill
325 1.1 jmcneill static int
326 1.1 jmcneill escodec_set_format(void *priv, int setmode,
327 1.1 jmcneill const audio_params_t *play, const audio_params_t *rec,
328 1.1 jmcneill audio_filter_reg_t *pfil, audio_filter_reg_t *rfil)
329 1.1 jmcneill {
330 1.1 jmcneill struct escodec_softc * const sc = priv;
331 1.1 jmcneill
332 1.1 jmcneill pfil->codec = escodec_swvol_codec;
333 1.1 jmcneill pfil->context = sc;
334 1.1 jmcneill
335 1.1 jmcneill return 0;
336 1.1 jmcneill }
337 1.1 jmcneill
338 1.1 jmcneill static int
339 1.1 jmcneill escodec_set_port(void *priv, mixer_ctrl_t *mc)
340 1.1 jmcneill {
341 1.1 jmcneill struct escodec_softc * const sc = priv;
342 1.1 jmcneill const struct escodec_mixer *mix;
343 1.1 jmcneill int nvol, shift, ch;
344 1.1 jmcneill uint8_t val;
345 1.1 jmcneill
346 1.1 jmcneill if ((mix = escodec_get_mixer(mc->dev)) == NULL)
347 1.1 jmcneill return ENXIO;
348 1.1 jmcneill
349 1.1 jmcneill switch (mix->type) {
350 1.1 jmcneill case ESCODEC_MIXER_SWVOL:
351 1.1 jmcneill sc->sc_swvol = mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT];
352 1.1 jmcneill return 0;
353 1.1 jmcneill
354 1.1 jmcneill case ESCODEC_MIXER_AMPLIFIER:
355 1.1 jmcneill case ESCODEC_MIXER_ATTENUATOR:
356 1.1 jmcneill escodec_lock(sc);
357 1.1 jmcneill for (ch = 0; ch < 2; ch++) {
358 1.1 jmcneill val = escodec_read(sc, mix->reg[ch]);
359 1.1 jmcneill shift = 8 - fls32(__SHIFTOUT_MASK(mix->mask[ch]));
360 1.1 jmcneill nvol = mc->un.value.level[ch] >> shift;
361 1.1 jmcneill if (mix->type == ESCODEC_MIXER_ATTENUATOR)
362 1.1 jmcneill nvol = __SHIFTOUT_MASK(mix->mask[ch]) - nvol;
363 1.1 jmcneill if (mix->maxval != 0 && nvol > mix->maxval)
364 1.1 jmcneill nvol = mix->maxval;
365 1.1 jmcneill
366 1.1 jmcneill val &= ~mix->mask[ch];
367 1.1 jmcneill val |= __SHIFTIN(nvol, mix->mask[ch]);
368 1.1 jmcneill escodec_write(sc, mix->reg[ch], val);
369 1.1 jmcneill }
370 1.1 jmcneill escodec_unlock(sc);
371 1.1 jmcneill return 0;
372 1.1 jmcneill
373 1.1 jmcneill case ESCODEC_MIXER_MUTE:
374 1.1 jmcneill if (mc->un.ord < 0 || mc->un.ord > 1)
375 1.1 jmcneill return EINVAL;
376 1.1 jmcneill escodec_lock(sc);
377 1.1 jmcneill val = escodec_read(sc, mix->reg[0]);
378 1.1 jmcneill if (mc->un.ord)
379 1.1 jmcneill val |= mix->mask[0];
380 1.1 jmcneill else
381 1.1 jmcneill val &= ~mix->mask[0];
382 1.1 jmcneill escodec_write(sc, mix->reg[0], val);
383 1.1 jmcneill escodec_unlock(sc);
384 1.1 jmcneill return 0;
385 1.1 jmcneill
386 1.1 jmcneill default:
387 1.1 jmcneill return ENXIO;
388 1.1 jmcneill }
389 1.1 jmcneill }
390 1.1 jmcneill
391 1.1 jmcneill static int
392 1.1 jmcneill escodec_get_port(void *priv, mixer_ctrl_t *mc)
393 1.1 jmcneill {
394 1.1 jmcneill struct escodec_softc * const sc = priv;
395 1.1 jmcneill const struct escodec_mixer *mix;
396 1.1 jmcneill int nvol, shift, ch;
397 1.1 jmcneill uint8_t val;
398 1.1 jmcneill
399 1.1 jmcneill if ((mix = escodec_get_mixer(mc->dev)) == NULL)
400 1.1 jmcneill return ENXIO;
401 1.1 jmcneill
402 1.1 jmcneill switch (mix->type) {
403 1.1 jmcneill case ESCODEC_MIXER_SWVOL:
404 1.1 jmcneill mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = sc->sc_swvol;
405 1.1 jmcneill mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = sc->sc_swvol;
406 1.1 jmcneill return 0;
407 1.1 jmcneill
408 1.1 jmcneill case ESCODEC_MIXER_AMPLIFIER:
409 1.1 jmcneill case ESCODEC_MIXER_ATTENUATOR:
410 1.1 jmcneill escodec_lock(sc);
411 1.1 jmcneill for (ch = 0; ch < 2; ch++) {
412 1.1 jmcneill val = escodec_read(sc, mix->reg[ch]);
413 1.1 jmcneill shift = 8 - fls32(__SHIFTOUT_MASK(mix->mask[ch]));
414 1.1 jmcneill nvol = __SHIFTOUT(val, mix->mask[ch]);
415 1.1 jmcneill if (mix->type == ESCODEC_MIXER_ATTENUATOR)
416 1.1 jmcneill nvol = __SHIFTOUT_MASK(mix->mask[ch]) - nvol;
417 1.1 jmcneill nvol <<= shift;
418 1.1 jmcneill mc->un.value.level[ch] = nvol;
419 1.1 jmcneill }
420 1.1 jmcneill escodec_unlock(sc);
421 1.1 jmcneill return 0;
422 1.1 jmcneill
423 1.1 jmcneill case ESCODEC_MIXER_MUTE:
424 1.1 jmcneill escodec_lock(sc);
425 1.1 jmcneill val = escodec_read(sc, mix->reg[0]);
426 1.1 jmcneill mc->un.ord = (val & mix->mask[0]) != 0;
427 1.1 jmcneill escodec_unlock(sc);
428 1.1 jmcneill return 0;
429 1.1 jmcneill
430 1.1 jmcneill default:
431 1.1 jmcneill return ENXIO;
432 1.1 jmcneill }
433 1.1 jmcneill }
434 1.1 jmcneill
435 1.1 jmcneill static int
436 1.1 jmcneill escodec_query_devinfo(void *priv, mixer_devinfo_t *di)
437 1.1 jmcneill {
438 1.1 jmcneill const struct escodec_mixer *mix;
439 1.1 jmcneill
440 1.1 jmcneill if ((mix = escodec_get_mixer(di->index)) == NULL)
441 1.1 jmcneill return ENXIO;
442 1.1 jmcneill
443 1.1 jmcneill strcpy(di->label.name, mix->name);
444 1.1 jmcneill di->mixer_class = mix->mixer_class;
445 1.1 jmcneill di->next = mix->next;
446 1.1 jmcneill di->prev = mix->prev;
447 1.1 jmcneill
448 1.1 jmcneill switch (mix->type) {
449 1.1 jmcneill case ESCODEC_MIXER_CLASS:
450 1.1 jmcneill di->type = AUDIO_MIXER_CLASS;
451 1.1 jmcneill return 0;
452 1.1 jmcneill
453 1.1 jmcneill case ESCODEC_MIXER_SWVOL:
454 1.1 jmcneill di->type = AUDIO_MIXER_VALUE;
455 1.1 jmcneill di->un.v.delta = 1;
456 1.1 jmcneill di->un.v.num_channels = 2;
457 1.1 jmcneill strcpy(di->un.v.units.name, AudioNvolume);
458 1.1 jmcneill return 0;
459 1.1 jmcneill
460 1.1 jmcneill case ESCODEC_MIXER_AMPLIFIER:
461 1.1 jmcneill case ESCODEC_MIXER_ATTENUATOR:
462 1.1 jmcneill di->type = AUDIO_MIXER_VALUE;
463 1.1 jmcneill di->un.v.delta =
464 1.1 jmcneill 256 / (__SHIFTOUT_MASK(mix->mask[0]) + 1);
465 1.1 jmcneill di->un.v.num_channels = 2;
466 1.1 jmcneill strcpy(di->un.v.units.name, AudioNvolume);
467 1.1 jmcneill return 0;
468 1.1 jmcneill
469 1.1 jmcneill case ESCODEC_MIXER_MUTE:
470 1.1 jmcneill di->type = AUDIO_MIXER_ENUM;
471 1.1 jmcneill di->un.e.num_mem = 2;
472 1.1 jmcneill strcpy(di->un.e.member[0].label.name, AudioNoff);
473 1.1 jmcneill di->un.e.member[0].ord = 0;
474 1.1 jmcneill strcpy(di->un.e.member[1].label.name, AudioNon);
475 1.1 jmcneill di->un.e.member[1].ord = 1;
476 1.1 jmcneill return 0;
477 1.1 jmcneill
478 1.1 jmcneill default:
479 1.1 jmcneill return ENXIO;
480 1.1 jmcneill }
481 1.1 jmcneill }
482 1.1 jmcneill
483 1.1 jmcneill static const struct audio_hw_if escodec_hw_if = {
484 1.1 jmcneill .set_format = escodec_set_format,
485 1.1 jmcneill .set_port = escodec_set_port,
486 1.1 jmcneill .get_port = escodec_get_port,
487 1.1 jmcneill .query_devinfo = escodec_query_devinfo,
488 1.1 jmcneill };
489 1.1 jmcneill
490 1.1 jmcneill static int
491 1.1 jmcneill escodec_dai_set_sysclk(audio_dai_tag_t dai, u_int rate, int dir)
492 1.1 jmcneill {
493 1.1 jmcneill struct escodec_softc * const sc = audio_dai_private(dai);
494 1.1 jmcneill int error;
495 1.1 jmcneill
496 1.1 jmcneill error = clk_set_rate(sc->sc_clk, rate);
497 1.1 jmcneill if (error != 0)
498 1.1 jmcneill return error;
499 1.1 jmcneill
500 1.1 jmcneill return 0;
501 1.1 jmcneill }
502 1.1 jmcneill
503 1.1 jmcneill static int
504 1.1 jmcneill escodec_dai_set_format(audio_dai_tag_t dai, u_int format)
505 1.1 jmcneill {
506 1.1 jmcneill struct escodec_softc * const sc = audio_dai_private(dai);
507 1.1 jmcneill uint8_t sd_clk, sd_fmt, val;
508 1.1 jmcneill
509 1.1 jmcneill const u_int fmt = __SHIFTOUT(format, AUDIO_DAI_FORMAT_MASK);
510 1.1 jmcneill const u_int clk = __SHIFTOUT(format, AUDIO_DAI_CLOCK_MASK);
511 1.1 jmcneill const u_int pol = __SHIFTOUT(format, AUDIO_DAI_POLARITY_MASK);
512 1.1 jmcneill
513 1.1 jmcneill if (fmt != AUDIO_DAI_FORMAT_I2S)
514 1.1 jmcneill return EINVAL;
515 1.1 jmcneill
516 1.1 jmcneill if (clk != AUDIO_DAI_CLOCK_CBS_CFS)
517 1.1 jmcneill return EINVAL;
518 1.1 jmcneill
519 1.1 jmcneill switch (pol) {
520 1.1 jmcneill case AUDIO_DAI_POLARITY_NB_NF:
521 1.1 jmcneill sd_clk = 0;
522 1.1 jmcneill sd_fmt = 0;
523 1.1 jmcneill break;
524 1.1 jmcneill case AUDIO_DAI_POLARITY_NB_IF:
525 1.1 jmcneill sd_clk = 0;
526 1.1 jmcneill sd_fmt = SD_FMT_LRP;
527 1.1 jmcneill break;
528 1.1 jmcneill case AUDIO_DAI_POLARITY_IB_NF:
529 1.1 jmcneill sd_clk = SD_CLK_BCLK_INV;
530 1.1 jmcneill sd_fmt = 0;
531 1.1 jmcneill break;
532 1.1 jmcneill case AUDIO_DAI_POLARITY_IB_IF:
533 1.1 jmcneill sd_clk = SD_CLK_BCLK_INV;
534 1.1 jmcneill sd_fmt = SD_FMT_LRP;
535 1.1 jmcneill break;
536 1.1 jmcneill }
537 1.1 jmcneill
538 1.1 jmcneill escodec_lock(sc);
539 1.1 jmcneill
540 1.1 jmcneill val = escodec_read(sc, ESCODEC_SD_CLK_REG);
541 1.1 jmcneill val &= ~(SD_CLK_MSC|SD_CLK_BCLK_INV);
542 1.1 jmcneill val |= sd_clk;
543 1.1 jmcneill escodec_write(sc, ESCODEC_SD_CLK_REG, val);
544 1.1 jmcneill
545 1.1 jmcneill val = escodec_read(sc, ESCODEC_SD_ADC_REG);
546 1.1 jmcneill val &= ~SD_FMT_MASK;
547 1.1 jmcneill val |= __SHIFTIN(SD_FMT_I2S, SD_FMT_MASK);
548 1.1 jmcneill val &= ~SD_FMT_LRP;
549 1.1 jmcneill val |= sd_fmt;
550 1.1 jmcneill escodec_write(sc, ESCODEC_SD_ADC_REG, val);
551 1.1 jmcneill
552 1.1 jmcneill val = escodec_read(sc, ESCODEC_SD_DAC_REG);
553 1.1 jmcneill val &= ~SD_FMT_MASK;
554 1.1 jmcneill val |= __SHIFTIN(SD_FMT_I2S, SD_FMT_MASK);
555 1.1 jmcneill val &= ~SD_FMT_LRP;
556 1.1 jmcneill val |= sd_fmt;
557 1.1 jmcneill escodec_write(sc, ESCODEC_SD_DAC_REG, val);
558 1.1 jmcneill
559 1.1 jmcneill val = escodec_read(sc, ESCODEC_CLKMAN1_REG);
560 1.1 jmcneill val |= CLKMAN1_MCLK_ON;
561 1.1 jmcneill val |= CLKMAN1_BCLK_ON;
562 1.1 jmcneill val |= CLKMAN1_CLK_CP_ON;
563 1.1 jmcneill val |= CLKMAN1_CLK_DAC_ON;
564 1.1 jmcneill val |= CLKMAN1_ANACLK_DAC_ON;
565 1.1 jmcneill escodec_write(sc, ESCODEC_CLKMAN1_REG, val);
566 1.1 jmcneill
567 1.1 jmcneill escodec_unlock(sc);
568 1.1 jmcneill
569 1.1 jmcneill return 0;
570 1.1 jmcneill }
571 1.1 jmcneill
572 1.1 jmcneill static int
573 1.1 jmcneill escodec_dai_add_device(audio_dai_tag_t dai, audio_dai_tag_t aux)
574 1.1 jmcneill {
575 1.1 jmcneill struct escodec_softc * const sc = audio_dai_private(dai);
576 1.1 jmcneill
577 1.1 jmcneill if (sc->sc_amplifier != NULL)
578 1.1 jmcneill return 0;
579 1.1 jmcneill
580 1.1 jmcneill sc->sc_amplifier = aux;
581 1.1 jmcneill
582 1.1 jmcneill return 0;
583 1.1 jmcneill }
584 1.1 jmcneill
585 1.1 jmcneill static audio_dai_tag_t
586 1.1 jmcneill escodec_dai_get_tag(device_t dev, const void *data, size_t len)
587 1.1 jmcneill {
588 1.1 jmcneill struct escodec_softc * const sc = device_private(dev);
589 1.1 jmcneill
590 1.1 jmcneill if (len != 4)
591 1.1 jmcneill return NULL;
592 1.1 jmcneill
593 1.1 jmcneill return &sc->sc_dai;
594 1.1 jmcneill }
595 1.1 jmcneill
596 1.1 jmcneill static struct fdtbus_dai_controller_func escodec_dai_funcs = {
597 1.1 jmcneill .get_tag = escodec_dai_get_tag
598 1.1 jmcneill };
599 1.1 jmcneill
600 1.1 jmcneill static void
601 1.1 jmcneill escodec_init(struct escodec_softc *sc)
602 1.1 jmcneill {
603 1.1 jmcneill uint8_t val;
604 1.1 jmcneill
605 1.1 jmcneill escodec_lock(sc);
606 1.1 jmcneill
607 1.1 jmcneill escodec_write(sc, ESCODEC_RESET_REG, RESET_ALL);
608 1.1 jmcneill delay(5000);
609 1.1 jmcneill escodec_write(sc, ESCODEC_RESET_REG, RESET_CSM_ON);
610 1.1 jmcneill delay(30000);
611 1.1 jmcneill
612 1.1 jmcneill escodec_write(sc, ESCODEC_VMID_REG, 0xff);
613 1.1 jmcneill escodec_write(sc, ESCODEC_ADC_OSR_REG, 0x32);
614 1.1 jmcneill
615 1.1 jmcneill val = escodec_read(sc, ESCODEC_SD_ADC_REG);
616 1.1 jmcneill val &= ~SD_FMT_WL;
617 1.1 jmcneill val |= __SHIFTIN(SD_FMT_WL_16, SD_FMT_WL);
618 1.1 jmcneill escodec_write(sc, ESCODEC_SD_ADC_REG, val);
619 1.1 jmcneill
620 1.1 jmcneill val = escodec_read(sc, ESCODEC_SD_DAC_REG);
621 1.1 jmcneill val &= ~SD_FMT_WL;
622 1.1 jmcneill val |= __SHIFTIN(SD_FMT_WL_16, SD_FMT_WL);
623 1.1 jmcneill escodec_write(sc, ESCODEC_SD_DAC_REG, val);
624 1.1 jmcneill
625 1.1 jmcneill /* Power up */
626 1.1 jmcneill escodec_write(sc, ESCODEC_PDN_REG, 0);
627 1.1 jmcneill
628 1.1 jmcneill /* Route DAC signal to HP mixer */
629 1.1 jmcneill val = HPMIXRT_LD2LHPMIX | HPMIXRT_RD2RHPMIX;
630 1.1 jmcneill escodec_write(sc, ESCODEC_HPMIXRT_REG, val);
631 1.1 jmcneill
632 1.1 jmcneill /* Power up DAC */
633 1.1 jmcneill escodec_write(sc, ESCODEC_DACPWR_REG, 0);
634 1.1 jmcneill
635 1.1 jmcneill /* Power up HP mixer and unmute */
636 1.1 jmcneill escodec_write(sc, ESCODEC_HPMIX_REG, 0);
637 1.1 jmcneill
638 1.1 jmcneill /* Power up HP output driver */
639 1.1 jmcneill val = escodec_read(sc, ESCODEC_HPPWR_REG);
640 1.1 jmcneill val &= ~HPPWR_PDN_CPHP;
641 1.1 jmcneill escodec_write(sc, ESCODEC_HPPWR_REG, val);
642 1.1 jmcneill
643 1.1 jmcneill /* Power up HP charge pump circuits */
644 1.1 jmcneill val = escodec_read(sc, ESCODEC_CPPWR_REG);
645 1.1 jmcneill val &= ~CPPWR_PDN_CP;
646 1.1 jmcneill escodec_write(sc, ESCODEC_CPPWR_REG, val);
647 1.1 jmcneill
648 1.1 jmcneill /* Set LIN1/RIN1 as inputs for HP mixer */
649 1.1 jmcneill escodec_write(sc, ESCODEC_HPSEL_REG, 0);
650 1.1 jmcneill
651 1.1 jmcneill /* Power up HP output driver calibration */
652 1.1 jmcneill val = escodec_read(sc, ESCODEC_HPVOL_REG);
653 1.1 jmcneill val &= ~HPVOL_PDN_LICAL;
654 1.1 jmcneill val &= ~HPVOL_PDN_RICAL;
655 1.1 jmcneill escodec_write(sc, ESCODEC_HPVOL_REG, val);
656 1.1 jmcneill
657 1.1 jmcneill /* Set headphone mixer to -6dB */
658 1.1 jmcneill escodec_write(sc, ESCODEC_HPMIXVOL_REG, 0x44);
659 1.1 jmcneill
660 1.1 jmcneill /* Set charge pump headphone to -48dB */
661 1.1 jmcneill escodec_write(sc, ESCODEC_HPVOL_REG, 0x33);
662 1.1 jmcneill
663 1.1 jmcneill /* Set DAC to 0dB */
664 1.1 jmcneill escodec_write(sc, ESCODEC_DACVOL_L_REG, 0);
665 1.1 jmcneill escodec_write(sc, ESCODEC_DACVOL_R_REG, 0);
666 1.1 jmcneill
667 1.2 jmcneill /* Enable HP output */
668 1.2 jmcneill val = HPOUTEN_EN_HPL | HPOUTEN_EN_HPR |
669 1.2 jmcneill HPOUTEN_HPL_OUTEN | HPOUTEN_HPR_OUTEN;
670 1.2 jmcneill escodec_write(sc, ESCODEC_HPOUTEN_REG, val);
671 1.2 jmcneill
672 1.1 jmcneill escodec_unlock(sc);
673 1.1 jmcneill }
674 1.1 jmcneill
675 1.1 jmcneill static int
676 1.1 jmcneill escodec_match(device_t parent, cfdata_t match, void *aux)
677 1.1 jmcneill {
678 1.1 jmcneill struct i2c_attach_args *ia = aux;
679 1.1 jmcneill int match_result;
680 1.1 jmcneill
681 1.1 jmcneill if (iic_use_direct_match(ia, match, compat_data, &match_result))
682 1.1 jmcneill return match_result;
683 1.1 jmcneill
684 1.1 jmcneill /* This device is direct-config only */
685 1.1 jmcneill
686 1.1 jmcneill return 0;
687 1.1 jmcneill }
688 1.1 jmcneill
689 1.1 jmcneill static void
690 1.1 jmcneill escodec_attach(device_t parent, device_t self, void *aux)
691 1.1 jmcneill {
692 1.1 jmcneill struct escodec_softc * const sc = device_private(self);
693 1.1 jmcneill struct i2c_attach_args * const ia = aux;
694 1.1 jmcneill const int phandle = ia->ia_cookie;
695 1.1 jmcneill
696 1.1 jmcneill sc->sc_dev = self;
697 1.1 jmcneill sc->sc_i2c = ia->ia_tag;
698 1.1 jmcneill sc->sc_addr = ia->ia_addr;
699 1.1 jmcneill sc->sc_phandle = ia->ia_cookie;
700 1.1 jmcneill sc->sc_swvol = 0xff;
701 1.1 jmcneill
702 1.1 jmcneill sc->sc_clk = fdtbus_clock_get(phandle, "mclk");
703 1.1 jmcneill if (sc->sc_clk == NULL || clk_enable(sc->sc_clk) != 0) {
704 1.1 jmcneill aprint_error(": couldn't enable mclk\n");
705 1.1 jmcneill return;
706 1.1 jmcneill }
707 1.1 jmcneill
708 1.1 jmcneill aprint_naive("\n");
709 1.1 jmcneill aprint_normal(": Everest Semi ES8316 Audio CODEC\n");
710 1.1 jmcneill
711 1.1 jmcneill escodec_init(sc);
712 1.1 jmcneill
713 1.1 jmcneill sc->sc_dai.dai_set_sysclk = escodec_dai_set_sysclk;
714 1.1 jmcneill sc->sc_dai.dai_set_format = escodec_dai_set_format;
715 1.1 jmcneill sc->sc_dai.dai_add_device = escodec_dai_add_device;
716 1.1 jmcneill sc->sc_dai.dai_hw_if = &escodec_hw_if;
717 1.1 jmcneill sc->sc_dai.dai_dev = self;
718 1.1 jmcneill sc->sc_dai.dai_priv = sc;
719 1.1 jmcneill fdtbus_register_dai_controller(self, phandle, &escodec_dai_funcs);
720 1.1 jmcneill }
721 1.1 jmcneill
722 1.1 jmcneill CFATTACH_DECL_NEW(es8316ac, sizeof(struct escodec_softc),
723 1.1 jmcneill escodec_match, escodec_attach, NULL, NULL);
724