1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
|
/* $OpenBSD: sncodec.c,v 1.3 2023/07/09 12:32:22 kettenis Exp $ */
/*
* Copyright (c) 2023 Mark Kettenis <kettenis@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/audioio.h>
#include <sys/device.h>
#include <sys/malloc.h>
#include <machine/bus.h>
#include <dev/ofw/openfirm.h>
#include <dev/ofw/ofw_gpio.h>
#include <dev/ofw/ofw_misc.h>
#include <dev/ofw/fdt.h>
#include <dev/i2c/i2cvar.h>
#include <dev/audio_if.h>
#define MODE_CTRL 0x02
#define MODE_CTRL_BOP_SRC (1 << 7)
#define MODE_CTRL_ISNS_PD (1 << 4)
#define MODE_CTRL_VSNS_PD (1 << 3)
#define MODE_CTRL_MODE_MASK (7 << 0)
#define MODE_CTRL_MODE_ACTIVE (0 << 0)
#define MODE_CTRL_MODE_MUTE (1 << 0)
#define MODE_CTRL_MODE_SHUTDOWN (2 << 0)
#define TDM_CFG0 0x08
#define TDM_CFG0_FRAME_START (1 << 0)
#define TDM_CFG1 0x09
#define TDM_CFG1_RX_JUSTIFY (1 << 6)
#define TDM_CFG1_RX_OFFSET_MASK (0x1f << 1)
#define TDM_CFG1_RX_OFFSET_SHIFT 1
#define TDM_CFG1_RX_EDGE (1 << 0)
#define TDM_CFG2 0x0a
#define TDM_CFG2_SCFG_MASK (3 << 4)
#define TDM_CFG2_SCFG_MONO_LEFT (1 << 4)
#define TDM_CFG2_SCFG_MONO_RIGHT (2 << 4)
#define TDM_CFG2_SCFG_STEREO_DOWNMIX (3 << 4)
#define TDM_CFG3 0x0c
#define TDM_CFG3_RX_SLOT_R_MASK 0xf0
#define TDM_CFG3_RX_SLOT_R_SHIFT 4
#define TDM_CFG3_RX_SLOT_L_MASK 0x0f
#define TDM_CFG3_RX_SLOT_L_SHIFT 0
#define DVC 0x1a
#define DVC_LVL_MIN 0xc9
#define DVC_LVL_30DB 0x3c
#define BOP_CFG0 0x1d
uint8_t sncodec_bop_cfg[] = {
0x01, 0x32, 0x02, 0x22, 0x83, 0x2d, 0x80, 0x02, 0x06,
0x32, 0x40, 0x30, 0x02, 0x06, 0x38, 0x40, 0x30, 0x02,
0x06, 0x3e, 0x37, 0x30, 0xff, 0xe6
};
struct sncodec_softc {
struct device sc_dev;
i2c_tag_t sc_tag;
i2c_addr_t sc_addr;
struct dai_device sc_dai;
uint8_t sc_dvc;
uint8_t sc_mute;
};
int sncodec_match(struct device *, void *, void *);
void sncodec_attach(struct device *, struct device *, void *);
int sncodec_activate(struct device *, int);
const struct cfattach sncodec_ca = {
sizeof(struct sncodec_softc), sncodec_match, sncodec_attach,
NULL, sncodec_activate
};
struct cfdriver sncodec_cd = {
NULL, "sncodec", DV_DULL
};
int sncodec_set_format(void *, uint32_t, uint32_t, uint32_t);
int sncodec_set_tdm_slot(void *, int);
int sncodec_set_port(void *, mixer_ctrl_t *);
int sncodec_get_port(void *, mixer_ctrl_t *);
int sncodec_query_devinfo(void *, mixer_devinfo_t *);
int sncodec_trigger_output(void *, void *, void *, int,
void (*)(void *), void *, struct audio_params *);
int sncodec_halt_output(void *);
const struct audio_hw_if sncodec_hw_if = {
.set_port = sncodec_set_port,
.get_port = sncodec_get_port,
.query_devinfo = sncodec_query_devinfo,
.trigger_output = sncodec_trigger_output,
.halt_output = sncodec_halt_output,
};
uint8_t sncodec_read(struct sncodec_softc *, int);
void sncodec_write(struct sncodec_softc *, int, uint8_t);
int
sncodec_match(struct device *parent, void *match, void *aux)
{
struct i2c_attach_args *ia = aux;
return iic_is_compatible(ia, "ti,tas2764");
}
void
sncodec_attach(struct device *parent, struct device *self, void *aux)
{
struct sncodec_softc *sc = (struct sncodec_softc *)self;
struct i2c_attach_args *ia = aux;
int node = *(int *)ia->ia_cookie;
uint32_t *sdz_gpio;
int sdz_gpiolen;
uint8_t cfg2;
int i;
sc->sc_tag = ia->ia_tag;
sc->sc_addr = ia->ia_addr;
printf("\n");
sdz_gpiolen = OF_getproplen(node, "shutdown-gpios");
if (sdz_gpiolen > 0) {
sdz_gpio = malloc(sdz_gpiolen, M_TEMP, M_WAITOK);
OF_getpropintarray(node, "shutdown-gpios",
sdz_gpio, sdz_gpiolen);
gpio_controller_config_pin(sdz_gpio, GPIO_CONFIG_OUTPUT);
gpio_controller_set_pin(sdz_gpio, 1);
free(sdz_gpio, M_TEMP, sdz_gpiolen);
delay(1000);
}
/* Set volume to a reasonable level. */
sc->sc_dvc = DVC_LVL_30DB;
sc->sc_mute = MODE_CTRL_MODE_ACTIVE;
sncodec_write(sc, DVC, sc->sc_dvc);
/* Default to stereo downmix mode for now. */
cfg2 = sncodec_read(sc, TDM_CFG2);
cfg2 &= ~TDM_CFG2_SCFG_MASK;
cfg2 |= TDM_CFG2_SCFG_STEREO_DOWNMIX;
sncodec_write(sc, TDM_CFG2, cfg2);
/* Configure brownout prevention. */
for (i = 0; i < nitems(sncodec_bop_cfg); i++)
sncodec_write(sc, BOP_CFG0 + i, sncodec_bop_cfg[i]);
sc->sc_dai.dd_node = node;
sc->sc_dai.dd_cookie = sc;
sc->sc_dai.dd_hw_if = &sncodec_hw_if;
sc->sc_dai.dd_set_format = sncodec_set_format;
sc->sc_dai.dd_set_tdm_slot = sncodec_set_tdm_slot;
dai_register(&sc->sc_dai);
}
int
sncodec_activate(struct device *self, int act)
{
struct sncodec_softc *sc = (struct sncodec_softc *)self;
switch (act) {
case DVACT_POWERDOWN:
sncodec_write(sc, MODE_CTRL, MODE_CTRL_ISNS_PD |
MODE_CTRL_VSNS_PD | MODE_CTRL_MODE_SHUTDOWN);
break;
}
return 0;
}
int
sncodec_set_format(void *cookie, uint32_t fmt, uint32_t pol,
uint32_t clk)
{
struct sncodec_softc *sc = cookie;
uint8_t cfg0, cfg1;
cfg0 = sncodec_read(sc, TDM_CFG0);
cfg1 = sncodec_read(sc, TDM_CFG1);
cfg1 &= ~TDM_CFG1_RX_OFFSET_MASK;
switch (fmt) {
case DAI_FORMAT_I2S:
cfg0 |= TDM_CFG0_FRAME_START;
cfg1 &= ~TDM_CFG1_RX_JUSTIFY;
cfg1 |= (1 << TDM_CFG1_RX_OFFSET_SHIFT);
cfg1 &= ~TDM_CFG1_RX_EDGE;
break;
case DAI_FORMAT_RJ:
cfg0 &= ~TDM_CFG0_FRAME_START;
cfg1 |= TDM_CFG1_RX_JUSTIFY;
cfg1 &= ~TDM_CFG1_RX_EDGE;
break;
case DAI_FORMAT_LJ:
cfg0 &= ~TDM_CFG0_FRAME_START;
cfg1 &= ~TDM_CFG1_RX_JUSTIFY;
cfg1 &= ~TDM_CFG1_RX_EDGE;
break;
default:
return EINVAL;
}
if (pol & DAI_POLARITY_IB)
cfg1 ^= TDM_CFG1_RX_EDGE;
if (pol & DAI_POLARITY_IF)
cfg0 ^= TDM_CFG0_FRAME_START;
if (!(clk & DAI_CLOCK_CBM) || !(clk & DAI_CLOCK_CFM))
return EINVAL;
sncodec_write(sc, TDM_CFG0, cfg0);
sncodec_write(sc, TDM_CFG1, cfg1);
return 0;
}
int
sncodec_set_tdm_slot(void *cookie, int slot)
{
struct sncodec_softc *sc = cookie;
uint8_t cfg2, cfg3;
if (slot < 0 || slot >= 16)
return EINVAL;
cfg2 = sncodec_read(sc, TDM_CFG2);
cfg3 = sncodec_read(sc, TDM_CFG3);
cfg2 &= ~TDM_CFG2_SCFG_MASK;
cfg2 |= TDM_CFG2_SCFG_MONO_LEFT;
cfg3 &= ~TDM_CFG3_RX_SLOT_L_MASK;
cfg3 |= slot << TDM_CFG3_RX_SLOT_L_SHIFT;
sncodec_write(sc, TDM_CFG2, cfg2);
sncodec_write(sc, TDM_CFG3, cfg3);
return 0;
}
/*
* Mixer controls; the gain of the TAS2764 is determined by the
* amplifier gain and digital volume control setting, but we only
* expose the digital volume control setting through the mixer
* interface.
*/
enum {
SNCODEC_MASTER_VOL,
SNCODEC_MASTER_MUTE,
SNCODEC_OUTPUT_CLASS
};
int
sncodec_set_port(void *priv, mixer_ctrl_t *mc)
{
struct sncodec_softc *sc = priv;
u_char level;
uint8_t mode;
switch (mc->dev) {
case SNCODEC_MASTER_VOL:
level = mc->un.value.level[AUDIO_MIXER_LEVEL_MONO];
sc->sc_dvc = (DVC_LVL_MIN * (255 - level)) / 255;
sncodec_write(sc, DVC, sc->sc_dvc);
return 0;
case SNCODEC_MASTER_MUTE:
sc->sc_mute = mc->un.ord ?
MODE_CTRL_MODE_MUTE : MODE_CTRL_MODE_ACTIVE;
mode = sncodec_read(sc, MODE_CTRL);
if ((mode & MODE_CTRL_MODE_MASK) == MODE_CTRL_MODE_ACTIVE ||
(mode & MODE_CTRL_MODE_MASK) == MODE_CTRL_MODE_MUTE) {
mode &= ~MODE_CTRL_MODE_MASK;
mode |= sc->sc_mute;
sncodec_write(sc, MODE_CTRL, mode);
}
return 0;
}
return EINVAL;
}
int
sncodec_get_port(void *priv, mixer_ctrl_t *mc)
{
struct sncodec_softc *sc = priv;
u_char level;
switch (mc->dev) {
case SNCODEC_MASTER_VOL:
mc->un.value.num_channels = 1;
level = 255 - ((255 * sc->sc_dvc) / DVC_LVL_MIN);
mc->un.value.level[AUDIO_MIXER_LEVEL_MONO] = level;
return 0;
case SNCODEC_MASTER_MUTE:
mc->un.ord = (sc->sc_mute == MODE_CTRL_MODE_MUTE);
return 0;
}
return EINVAL;
}
int
sncodec_query_devinfo(void *priv, mixer_devinfo_t *di)
{
switch (di->index) {
case SNCODEC_MASTER_VOL:
di->mixer_class = SNCODEC_OUTPUT_CLASS;
di->prev = AUDIO_MIXER_LAST;
di->next = SNCODEC_MASTER_MUTE;
strlcpy(di->label.name, AudioNmaster, sizeof(di->label.name));
di->type = AUDIO_MIXER_VALUE;
di->un.v.num_channels = 1;
strlcpy(di->un.v.units.name, AudioNvolume,
sizeof(di->un.v.units.name));
return 0;
case SNCODEC_MASTER_MUTE:
di->mixer_class = SNCODEC_OUTPUT_CLASS;
di->prev = SNCODEC_MASTER_VOL;
di->next = AUDIO_MIXER_LAST;
strlcpy(di->label.name, AudioNmute, sizeof(di->label.name));
di->type = AUDIO_MIXER_ENUM;
di->un.e.num_mem = 2;
di->un.e.member[0].ord = 0;
strlcpy(di->un.e.member[0].label.name, AudioNoff,
MAX_AUDIO_DEV_LEN);
di->un.e.member[1].ord = 1;
strlcpy(di->un.e.member[1].label.name, AudioNon,
MAX_AUDIO_DEV_LEN);
return 0;
case SNCODEC_OUTPUT_CLASS:
di->mixer_class = SNCODEC_OUTPUT_CLASS;
di->next = di->prev = AUDIO_MIXER_LAST;
strlcpy(di->label.name, AudioCoutputs, sizeof(di->label.name));
di->type = AUDIO_MIXER_CLASS;
return 0;
}
return ENXIO;
}
int
sncodec_trigger_output(void *cookie, void *start, void *end, int blksize,
void (*intr)(void *), void *intrarg, struct audio_params *params)
{
struct sncodec_softc *sc = cookie;
sncodec_write(sc, MODE_CTRL, MODE_CTRL_BOP_SRC |
MODE_CTRL_ISNS_PD | MODE_CTRL_VSNS_PD | sc->sc_mute);
return 0;
}
int
sncodec_halt_output(void *cookie)
{
struct sncodec_softc *sc = cookie;
sncodec_write(sc, MODE_CTRL, MODE_CTRL_BOP_SRC |
MODE_CTRL_ISNS_PD | MODE_CTRL_VSNS_PD | MODE_CTRL_MODE_SHUTDOWN);
return 0;
}
uint8_t
sncodec_read(struct sncodec_softc *sc, int reg)
{
uint8_t cmd = reg;
uint8_t val;
int error;
iic_acquire_bus(sc->sc_tag, I2C_F_POLL);
error = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr,
&cmd, sizeof cmd, &val, sizeof val, I2C_F_POLL);
iic_release_bus(sc->sc_tag, I2C_F_POLL);
if (error) {
printf("%s: can't read register 0x%02x\n",
sc->sc_dev.dv_xname, reg);
val = 0xff;
}
return val;
}
void
sncodec_write(struct sncodec_softc *sc, int reg, uint8_t val)
{
uint8_t cmd = reg;
int error;
iic_acquire_bus(sc->sc_tag, I2C_F_POLL);
error = iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_addr,
&cmd, sizeof cmd, &val, sizeof val, I2C_F_POLL);
iic_release_bus(sc->sc_tag, I2C_F_POLL);
if (error) {
printf("%s: can't write register 0x%02x\n",
sc->sc_dev.dv_xname, reg);
}
}
|