es8316ac.c revision 1.1 1 1.1 jmcneill /* $NetBSD: es8316ac.c,v 1.1 2020/01/02 22:06:59 jmcneill 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.1 jmcneill __KERNEL_RCSID(0, "$NetBSD: es8316ac.c,v 1.1 2020/01/02 22:06:59 jmcneill 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.1 jmcneill { "everest,es8316", 1 },
107 1.1 jmcneill { NULL }
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.1 jmcneill if (iic_smbus_read_byte(sc->sc_i2c, sc->sc_addr, reg, &val, I2C_F_POLL) != 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.1 jmcneill (void)iic_smbus_write_byte(sc->sc_i2c, sc->sc_addr, reg, val, I2C_F_POLL);
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_trigger_output(void *priv, void *start, void *end, int blksize,
326 1.1 jmcneill void (*intr)(void *), void *intarg, const audio_params_t *params)
327 1.1 jmcneill {
328 1.1 jmcneill struct escodec_softc * const sc = priv;
329 1.1 jmcneill uint8_t val;
330 1.1 jmcneill
331 1.1 jmcneill escodec_lock(sc);
332 1.1 jmcneill
333 1.1 jmcneill /* Enable HP output */
334 1.1 jmcneill val = HPOUTEN_EN_HPL | HPOUTEN_EN_HPR |
335 1.1 jmcneill HPOUTEN_HPL_OUTEN | HPOUTEN_HPR_OUTEN;
336 1.1 jmcneill escodec_write(sc, ESCODEC_HPOUTEN_REG, val);
337 1.1 jmcneill
338 1.1 jmcneill escodec_unlock(sc);
339 1.1 jmcneill
340 1.1 jmcneill return 0;
341 1.1 jmcneill }
342 1.1 jmcneill
343 1.1 jmcneill static int
344 1.1 jmcneill escodec_halt_output(void *priv)
345 1.1 jmcneill {
346 1.1 jmcneill struct escodec_softc * const sc = priv;
347 1.1 jmcneill
348 1.1 jmcneill escodec_lock(sc);
349 1.1 jmcneill
350 1.1 jmcneill /* Disable HP output */
351 1.1 jmcneill escodec_write(sc, ESCODEC_HPOUTEN_REG, 0);
352 1.1 jmcneill
353 1.1 jmcneill escodec_unlock(sc);
354 1.1 jmcneill
355 1.1 jmcneill return 0;
356 1.1 jmcneill }
357 1.1 jmcneill
358 1.1 jmcneill static int
359 1.1 jmcneill escodec_set_format(void *priv, int setmode,
360 1.1 jmcneill const audio_params_t *play, const audio_params_t *rec,
361 1.1 jmcneill audio_filter_reg_t *pfil, audio_filter_reg_t *rfil)
362 1.1 jmcneill {
363 1.1 jmcneill struct escodec_softc * const sc = priv;
364 1.1 jmcneill
365 1.1 jmcneill pfil->codec = escodec_swvol_codec;
366 1.1 jmcneill pfil->context = sc;
367 1.1 jmcneill
368 1.1 jmcneill return 0;
369 1.1 jmcneill }
370 1.1 jmcneill
371 1.1 jmcneill static int
372 1.1 jmcneill escodec_set_port(void *priv, mixer_ctrl_t *mc)
373 1.1 jmcneill {
374 1.1 jmcneill struct escodec_softc * const sc = priv;
375 1.1 jmcneill const struct escodec_mixer *mix;
376 1.1 jmcneill int nvol, shift, ch;
377 1.1 jmcneill uint8_t val;
378 1.1 jmcneill
379 1.1 jmcneill if ((mix = escodec_get_mixer(mc->dev)) == NULL)
380 1.1 jmcneill return ENXIO;
381 1.1 jmcneill
382 1.1 jmcneill switch (mix->type) {
383 1.1 jmcneill case ESCODEC_MIXER_SWVOL:
384 1.1 jmcneill sc->sc_swvol = mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT];
385 1.1 jmcneill return 0;
386 1.1 jmcneill
387 1.1 jmcneill case ESCODEC_MIXER_AMPLIFIER:
388 1.1 jmcneill case ESCODEC_MIXER_ATTENUATOR:
389 1.1 jmcneill escodec_lock(sc);
390 1.1 jmcneill for (ch = 0; ch < 2; ch++) {
391 1.1 jmcneill val = escodec_read(sc, mix->reg[ch]);
392 1.1 jmcneill shift = 8 - fls32(__SHIFTOUT_MASK(mix->mask[ch]));
393 1.1 jmcneill nvol = mc->un.value.level[ch] >> shift;
394 1.1 jmcneill if (mix->type == ESCODEC_MIXER_ATTENUATOR)
395 1.1 jmcneill nvol = __SHIFTOUT_MASK(mix->mask[ch]) - nvol;
396 1.1 jmcneill if (mix->maxval != 0 && nvol > mix->maxval)
397 1.1 jmcneill nvol = mix->maxval;
398 1.1 jmcneill
399 1.1 jmcneill val &= ~mix->mask[ch];
400 1.1 jmcneill val |= __SHIFTIN(nvol, mix->mask[ch]);
401 1.1 jmcneill escodec_write(sc, mix->reg[ch], val);
402 1.1 jmcneill }
403 1.1 jmcneill escodec_unlock(sc);
404 1.1 jmcneill return 0;
405 1.1 jmcneill
406 1.1 jmcneill case ESCODEC_MIXER_MUTE:
407 1.1 jmcneill if (mc->un.ord < 0 || mc->un.ord > 1)
408 1.1 jmcneill return EINVAL;
409 1.1 jmcneill escodec_lock(sc);
410 1.1 jmcneill val = escodec_read(sc, mix->reg[0]);
411 1.1 jmcneill if (mc->un.ord)
412 1.1 jmcneill val |= mix->mask[0];
413 1.1 jmcneill else
414 1.1 jmcneill val &= ~mix->mask[0];
415 1.1 jmcneill escodec_write(sc, mix->reg[0], val);
416 1.1 jmcneill escodec_unlock(sc);
417 1.1 jmcneill return 0;
418 1.1 jmcneill
419 1.1 jmcneill default:
420 1.1 jmcneill return ENXIO;
421 1.1 jmcneill }
422 1.1 jmcneill }
423 1.1 jmcneill
424 1.1 jmcneill static int
425 1.1 jmcneill escodec_get_port(void *priv, mixer_ctrl_t *mc)
426 1.1 jmcneill {
427 1.1 jmcneill struct escodec_softc * const sc = priv;
428 1.1 jmcneill const struct escodec_mixer *mix;
429 1.1 jmcneill int nvol, shift, ch;
430 1.1 jmcneill uint8_t val;
431 1.1 jmcneill
432 1.1 jmcneill if ((mix = escodec_get_mixer(mc->dev)) == NULL)
433 1.1 jmcneill return ENXIO;
434 1.1 jmcneill
435 1.1 jmcneill switch (mix->type) {
436 1.1 jmcneill case ESCODEC_MIXER_SWVOL:
437 1.1 jmcneill mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = sc->sc_swvol;
438 1.1 jmcneill mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = sc->sc_swvol;
439 1.1 jmcneill return 0;
440 1.1 jmcneill
441 1.1 jmcneill case ESCODEC_MIXER_AMPLIFIER:
442 1.1 jmcneill case ESCODEC_MIXER_ATTENUATOR:
443 1.1 jmcneill escodec_lock(sc);
444 1.1 jmcneill for (ch = 0; ch < 2; ch++) {
445 1.1 jmcneill val = escodec_read(sc, mix->reg[ch]);
446 1.1 jmcneill shift = 8 - fls32(__SHIFTOUT_MASK(mix->mask[ch]));
447 1.1 jmcneill nvol = __SHIFTOUT(val, mix->mask[ch]);
448 1.1 jmcneill if (mix->type == ESCODEC_MIXER_ATTENUATOR)
449 1.1 jmcneill nvol = __SHIFTOUT_MASK(mix->mask[ch]) - nvol;
450 1.1 jmcneill nvol <<= shift;
451 1.1 jmcneill mc->un.value.level[ch] = nvol;
452 1.1 jmcneill }
453 1.1 jmcneill escodec_unlock(sc);
454 1.1 jmcneill return 0;
455 1.1 jmcneill
456 1.1 jmcneill case ESCODEC_MIXER_MUTE:
457 1.1 jmcneill escodec_lock(sc);
458 1.1 jmcneill val = escodec_read(sc, mix->reg[0]);
459 1.1 jmcneill mc->un.ord = (val & mix->mask[0]) != 0;
460 1.1 jmcneill escodec_unlock(sc);
461 1.1 jmcneill return 0;
462 1.1 jmcneill
463 1.1 jmcneill default:
464 1.1 jmcneill return ENXIO;
465 1.1 jmcneill }
466 1.1 jmcneill }
467 1.1 jmcneill
468 1.1 jmcneill static int
469 1.1 jmcneill escodec_query_devinfo(void *priv, mixer_devinfo_t *di)
470 1.1 jmcneill {
471 1.1 jmcneill const struct escodec_mixer *mix;
472 1.1 jmcneill
473 1.1 jmcneill if ((mix = escodec_get_mixer(di->index)) == NULL)
474 1.1 jmcneill return ENXIO;
475 1.1 jmcneill
476 1.1 jmcneill strcpy(di->label.name, mix->name);
477 1.1 jmcneill di->mixer_class = mix->mixer_class;
478 1.1 jmcneill di->next = mix->next;
479 1.1 jmcneill di->prev = mix->prev;
480 1.1 jmcneill
481 1.1 jmcneill switch (mix->type) {
482 1.1 jmcneill case ESCODEC_MIXER_CLASS:
483 1.1 jmcneill di->type = AUDIO_MIXER_CLASS;
484 1.1 jmcneill return 0;
485 1.1 jmcneill
486 1.1 jmcneill case ESCODEC_MIXER_SWVOL:
487 1.1 jmcneill di->type = AUDIO_MIXER_VALUE;
488 1.1 jmcneill di->un.v.delta = 1;
489 1.1 jmcneill di->un.v.num_channels = 2;
490 1.1 jmcneill strcpy(di->un.v.units.name, AudioNvolume);
491 1.1 jmcneill return 0;
492 1.1 jmcneill
493 1.1 jmcneill case ESCODEC_MIXER_AMPLIFIER:
494 1.1 jmcneill case ESCODEC_MIXER_ATTENUATOR:
495 1.1 jmcneill di->type = AUDIO_MIXER_VALUE;
496 1.1 jmcneill di->un.v.delta =
497 1.1 jmcneill 256 / (__SHIFTOUT_MASK(mix->mask[0]) + 1);
498 1.1 jmcneill di->un.v.num_channels = 2;
499 1.1 jmcneill strcpy(di->un.v.units.name, AudioNvolume);
500 1.1 jmcneill return 0;
501 1.1 jmcneill
502 1.1 jmcneill case ESCODEC_MIXER_MUTE:
503 1.1 jmcneill di->type = AUDIO_MIXER_ENUM;
504 1.1 jmcneill di->un.e.num_mem = 2;
505 1.1 jmcneill strcpy(di->un.e.member[0].label.name, AudioNoff);
506 1.1 jmcneill di->un.e.member[0].ord = 0;
507 1.1 jmcneill strcpy(di->un.e.member[1].label.name, AudioNon);
508 1.1 jmcneill di->un.e.member[1].ord = 1;
509 1.1 jmcneill return 0;
510 1.1 jmcneill
511 1.1 jmcneill default:
512 1.1 jmcneill return ENXIO;
513 1.1 jmcneill }
514 1.1 jmcneill }
515 1.1 jmcneill
516 1.1 jmcneill static const struct audio_hw_if escodec_hw_if = {
517 1.1 jmcneill .trigger_output = escodec_trigger_output,
518 1.1 jmcneill .halt_output = escodec_halt_output,
519 1.1 jmcneill .set_format = escodec_set_format,
520 1.1 jmcneill .set_port = escodec_set_port,
521 1.1 jmcneill .get_port = escodec_get_port,
522 1.1 jmcneill .query_devinfo = escodec_query_devinfo,
523 1.1 jmcneill };
524 1.1 jmcneill
525 1.1 jmcneill static int
526 1.1 jmcneill escodec_dai_set_sysclk(audio_dai_tag_t dai, u_int rate, int dir)
527 1.1 jmcneill {
528 1.1 jmcneill struct escodec_softc * const sc = audio_dai_private(dai);
529 1.1 jmcneill int error;
530 1.1 jmcneill
531 1.1 jmcneill error = clk_set_rate(sc->sc_clk, rate);
532 1.1 jmcneill if (error != 0)
533 1.1 jmcneill return error;
534 1.1 jmcneill
535 1.1 jmcneill return 0;
536 1.1 jmcneill }
537 1.1 jmcneill
538 1.1 jmcneill static int
539 1.1 jmcneill escodec_dai_set_format(audio_dai_tag_t dai, u_int format)
540 1.1 jmcneill {
541 1.1 jmcneill struct escodec_softc * const sc = audio_dai_private(dai);
542 1.1 jmcneill uint8_t sd_clk, sd_fmt, val;
543 1.1 jmcneill
544 1.1 jmcneill const u_int fmt = __SHIFTOUT(format, AUDIO_DAI_FORMAT_MASK);
545 1.1 jmcneill const u_int clk = __SHIFTOUT(format, AUDIO_DAI_CLOCK_MASK);
546 1.1 jmcneill const u_int pol = __SHIFTOUT(format, AUDIO_DAI_POLARITY_MASK);
547 1.1 jmcneill
548 1.1 jmcneill if (fmt != AUDIO_DAI_FORMAT_I2S)
549 1.1 jmcneill return EINVAL;
550 1.1 jmcneill
551 1.1 jmcneill if (clk != AUDIO_DAI_CLOCK_CBS_CFS)
552 1.1 jmcneill return EINVAL;
553 1.1 jmcneill
554 1.1 jmcneill switch (pol) {
555 1.1 jmcneill case AUDIO_DAI_POLARITY_NB_NF:
556 1.1 jmcneill sd_clk = 0;
557 1.1 jmcneill sd_fmt = 0;
558 1.1 jmcneill break;
559 1.1 jmcneill case AUDIO_DAI_POLARITY_NB_IF:
560 1.1 jmcneill sd_clk = 0;
561 1.1 jmcneill sd_fmt = SD_FMT_LRP;
562 1.1 jmcneill break;
563 1.1 jmcneill case AUDIO_DAI_POLARITY_IB_NF:
564 1.1 jmcneill sd_clk = SD_CLK_BCLK_INV;
565 1.1 jmcneill sd_fmt = 0;
566 1.1 jmcneill break;
567 1.1 jmcneill case AUDIO_DAI_POLARITY_IB_IF:
568 1.1 jmcneill sd_clk = SD_CLK_BCLK_INV;
569 1.1 jmcneill sd_fmt = SD_FMT_LRP;
570 1.1 jmcneill break;
571 1.1 jmcneill }
572 1.1 jmcneill
573 1.1 jmcneill escodec_lock(sc);
574 1.1 jmcneill
575 1.1 jmcneill val = escodec_read(sc, ESCODEC_SD_CLK_REG);
576 1.1 jmcneill val &= ~(SD_CLK_MSC|SD_CLK_BCLK_INV);
577 1.1 jmcneill val |= sd_clk;
578 1.1 jmcneill escodec_write(sc, ESCODEC_SD_CLK_REG, val);
579 1.1 jmcneill
580 1.1 jmcneill val = escodec_read(sc, ESCODEC_SD_ADC_REG);
581 1.1 jmcneill val &= ~SD_FMT_MASK;
582 1.1 jmcneill val |= __SHIFTIN(SD_FMT_I2S, SD_FMT_MASK);
583 1.1 jmcneill val &= ~SD_FMT_LRP;
584 1.1 jmcneill val |= sd_fmt;
585 1.1 jmcneill escodec_write(sc, ESCODEC_SD_ADC_REG, val);
586 1.1 jmcneill
587 1.1 jmcneill val = escodec_read(sc, ESCODEC_SD_DAC_REG);
588 1.1 jmcneill val &= ~SD_FMT_MASK;
589 1.1 jmcneill val |= __SHIFTIN(SD_FMT_I2S, SD_FMT_MASK);
590 1.1 jmcneill val &= ~SD_FMT_LRP;
591 1.1 jmcneill val |= sd_fmt;
592 1.1 jmcneill escodec_write(sc, ESCODEC_SD_DAC_REG, val);
593 1.1 jmcneill
594 1.1 jmcneill val = escodec_read(sc, ESCODEC_CLKMAN1_REG);
595 1.1 jmcneill val |= CLKMAN1_MCLK_ON;
596 1.1 jmcneill val |= CLKMAN1_BCLK_ON;
597 1.1 jmcneill val |= CLKMAN1_CLK_CP_ON;
598 1.1 jmcneill val |= CLKMAN1_CLK_DAC_ON;
599 1.1 jmcneill val |= CLKMAN1_ANACLK_DAC_ON;
600 1.1 jmcneill escodec_write(sc, ESCODEC_CLKMAN1_REG, val);
601 1.1 jmcneill
602 1.1 jmcneill escodec_unlock(sc);
603 1.1 jmcneill
604 1.1 jmcneill return 0;
605 1.1 jmcneill }
606 1.1 jmcneill
607 1.1 jmcneill static int
608 1.1 jmcneill escodec_dai_add_device(audio_dai_tag_t dai, audio_dai_tag_t aux)
609 1.1 jmcneill {
610 1.1 jmcneill struct escodec_softc * const sc = audio_dai_private(dai);
611 1.1 jmcneill
612 1.1 jmcneill if (sc->sc_amplifier != NULL)
613 1.1 jmcneill return 0;
614 1.1 jmcneill
615 1.1 jmcneill sc->sc_amplifier = aux;
616 1.1 jmcneill
617 1.1 jmcneill return 0;
618 1.1 jmcneill }
619 1.1 jmcneill
620 1.1 jmcneill static audio_dai_tag_t
621 1.1 jmcneill escodec_dai_get_tag(device_t dev, const void *data, size_t len)
622 1.1 jmcneill {
623 1.1 jmcneill struct escodec_softc * const sc = device_private(dev);
624 1.1 jmcneill
625 1.1 jmcneill if (len != 4)
626 1.1 jmcneill return NULL;
627 1.1 jmcneill
628 1.1 jmcneill return &sc->sc_dai;
629 1.1 jmcneill }
630 1.1 jmcneill
631 1.1 jmcneill static struct fdtbus_dai_controller_func escodec_dai_funcs = {
632 1.1 jmcneill .get_tag = escodec_dai_get_tag
633 1.1 jmcneill };
634 1.1 jmcneill
635 1.1 jmcneill static void
636 1.1 jmcneill escodec_init(struct escodec_softc *sc)
637 1.1 jmcneill {
638 1.1 jmcneill uint8_t val;
639 1.1 jmcneill
640 1.1 jmcneill escodec_lock(sc);
641 1.1 jmcneill
642 1.1 jmcneill escodec_write(sc, ESCODEC_RESET_REG, RESET_ALL);
643 1.1 jmcneill delay(5000);
644 1.1 jmcneill escodec_write(sc, ESCODEC_RESET_REG, RESET_CSM_ON);
645 1.1 jmcneill delay(30000);
646 1.1 jmcneill
647 1.1 jmcneill escodec_write(sc, ESCODEC_VMID_REG, 0xff);
648 1.1 jmcneill escodec_write(sc, ESCODEC_ADC_OSR_REG, 0x32);
649 1.1 jmcneill
650 1.1 jmcneill val = escodec_read(sc, ESCODEC_SD_ADC_REG);
651 1.1 jmcneill val &= ~SD_FMT_WL;
652 1.1 jmcneill val |= __SHIFTIN(SD_FMT_WL_16, SD_FMT_WL);
653 1.1 jmcneill escodec_write(sc, ESCODEC_SD_ADC_REG, val);
654 1.1 jmcneill
655 1.1 jmcneill val = escodec_read(sc, ESCODEC_SD_DAC_REG);
656 1.1 jmcneill val &= ~SD_FMT_WL;
657 1.1 jmcneill val |= __SHIFTIN(SD_FMT_WL_16, SD_FMT_WL);
658 1.1 jmcneill escodec_write(sc, ESCODEC_SD_DAC_REG, val);
659 1.1 jmcneill
660 1.1 jmcneill /* Power up */
661 1.1 jmcneill escodec_write(sc, ESCODEC_PDN_REG, 0);
662 1.1 jmcneill
663 1.1 jmcneill /* Route DAC signal to HP mixer */
664 1.1 jmcneill val = HPMIXRT_LD2LHPMIX | HPMIXRT_RD2RHPMIX;
665 1.1 jmcneill escodec_write(sc, ESCODEC_HPMIXRT_REG, val);
666 1.1 jmcneill
667 1.1 jmcneill /* Power up DAC */
668 1.1 jmcneill escodec_write(sc, ESCODEC_DACPWR_REG, 0);
669 1.1 jmcneill
670 1.1 jmcneill /* Power up HP mixer and unmute */
671 1.1 jmcneill escodec_write(sc, ESCODEC_HPMIX_REG, 0);
672 1.1 jmcneill
673 1.1 jmcneill /* Power up HP output driver */
674 1.1 jmcneill val = escodec_read(sc, ESCODEC_HPPWR_REG);
675 1.1 jmcneill val &= ~HPPWR_PDN_CPHP;
676 1.1 jmcneill escodec_write(sc, ESCODEC_HPPWR_REG, val);
677 1.1 jmcneill
678 1.1 jmcneill /* Power up HP charge pump circuits */
679 1.1 jmcneill val = escodec_read(sc, ESCODEC_CPPWR_REG);
680 1.1 jmcneill val &= ~CPPWR_PDN_CP;
681 1.1 jmcneill escodec_write(sc, ESCODEC_CPPWR_REG, val);
682 1.1 jmcneill
683 1.1 jmcneill /* Set LIN1/RIN1 as inputs for HP mixer */
684 1.1 jmcneill escodec_write(sc, ESCODEC_HPSEL_REG, 0);
685 1.1 jmcneill
686 1.1 jmcneill /* Power up HP output driver calibration */
687 1.1 jmcneill val = escodec_read(sc, ESCODEC_HPVOL_REG);
688 1.1 jmcneill val &= ~HPVOL_PDN_LICAL;
689 1.1 jmcneill val &= ~HPVOL_PDN_RICAL;
690 1.1 jmcneill escodec_write(sc, ESCODEC_HPVOL_REG, val);
691 1.1 jmcneill
692 1.1 jmcneill /* Set headphone mixer to -6dB */
693 1.1 jmcneill escodec_write(sc, ESCODEC_HPMIXVOL_REG, 0x44);
694 1.1 jmcneill
695 1.1 jmcneill /* Set charge pump headphone to -48dB */
696 1.1 jmcneill escodec_write(sc, ESCODEC_HPVOL_REG, 0x33);
697 1.1 jmcneill
698 1.1 jmcneill /* Set DAC to 0dB */
699 1.1 jmcneill escodec_write(sc, ESCODEC_DACVOL_L_REG, 0);
700 1.1 jmcneill escodec_write(sc, ESCODEC_DACVOL_R_REG, 0);
701 1.1 jmcneill
702 1.1 jmcneill escodec_unlock(sc);
703 1.1 jmcneill }
704 1.1 jmcneill
705 1.1 jmcneill static int
706 1.1 jmcneill escodec_match(device_t parent, cfdata_t match, void *aux)
707 1.1 jmcneill {
708 1.1 jmcneill struct i2c_attach_args *ia = aux;
709 1.1 jmcneill int match_result;
710 1.1 jmcneill
711 1.1 jmcneill if (iic_use_direct_match(ia, match, compat_data, &match_result))
712 1.1 jmcneill return match_result;
713 1.1 jmcneill
714 1.1 jmcneill /* This device is direct-config only */
715 1.1 jmcneill
716 1.1 jmcneill return 0;
717 1.1 jmcneill }
718 1.1 jmcneill
719 1.1 jmcneill static void
720 1.1 jmcneill escodec_attach(device_t parent, device_t self, void *aux)
721 1.1 jmcneill {
722 1.1 jmcneill struct escodec_softc * const sc = device_private(self);
723 1.1 jmcneill struct i2c_attach_args * const ia = aux;
724 1.1 jmcneill const int phandle = ia->ia_cookie;
725 1.1 jmcneill
726 1.1 jmcneill sc->sc_dev = self;
727 1.1 jmcneill sc->sc_i2c = ia->ia_tag;
728 1.1 jmcneill sc->sc_addr = ia->ia_addr;
729 1.1 jmcneill sc->sc_phandle = ia->ia_cookie;
730 1.1 jmcneill sc->sc_swvol = 0xff;
731 1.1 jmcneill
732 1.1 jmcneill sc->sc_clk = fdtbus_clock_get(phandle, "mclk");
733 1.1 jmcneill if (sc->sc_clk == NULL || clk_enable(sc->sc_clk) != 0) {
734 1.1 jmcneill aprint_error(": couldn't enable mclk\n");
735 1.1 jmcneill return;
736 1.1 jmcneill }
737 1.1 jmcneill
738 1.1 jmcneill aprint_naive("\n");
739 1.1 jmcneill aprint_normal(": Everest Semi ES8316 Audio CODEC\n");
740 1.1 jmcneill
741 1.1 jmcneill escodec_init(sc);
742 1.1 jmcneill
743 1.1 jmcneill sc->sc_dai.dai_set_sysclk = escodec_dai_set_sysclk;
744 1.1 jmcneill sc->sc_dai.dai_set_format = escodec_dai_set_format;
745 1.1 jmcneill sc->sc_dai.dai_add_device = escodec_dai_add_device;
746 1.1 jmcneill sc->sc_dai.dai_hw_if = &escodec_hw_if;
747 1.1 jmcneill sc->sc_dai.dai_dev = self;
748 1.1 jmcneill sc->sc_dai.dai_priv = sc;
749 1.1 jmcneill fdtbus_register_dai_controller(self, phandle, &escodec_dai_funcs);
750 1.1 jmcneill }
751 1.1 jmcneill
752 1.1 jmcneill CFATTACH_DECL_NEW(es8316ac, sizeof(struct escodec_softc),
753 1.1 jmcneill escodec_match, escodec_attach, NULL, NULL);
754