summaryrefslogtreecommitdiff
path: root/sys/src/cmd/aux/wm8960.c
blob: 86aaea4bb5baea440a8dfaf369929d2b5455ab2c (plain)
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
#include <u.h>
#include <libc.h>
#include <fcall.h>
#include <thread.h>
#include <9p.h>

#define MIN(a,b) (a<b?a:b)
#define MAX(a,b) (a>b?a:b)
#define CLAMP(x,a,b) MAX(a,MIN(x,b))

typedef struct Out Out;

enum
{
	Dac,
	Hp,
	Spk,
	Nout,

	Ctl = 1,
	Vol,
};

struct Out
{
	char *name;
	int volreg;
	int volmax;
	int togglemask;
	void (*toggle)(Out *o, int on);
	int on;
	int vol[2];
};

static char *uid = "audio";
static int data;
static int reg1a;
static int rate = 44100;
static int d;

static void
wr(int a, int v)
{
	u8int c;

	//fprint(2, "[0x%x] ← 0x%ux\n", a, v & 0x1ff);
	c = v & 0xff;
	if(pwrite(data, &c, 1, a<<1 | ((v>>8)&1)) < 1)
		fprint(2, "reg %x write failed: %r\n", a);
}

static void
classdspk(Out *, int on)
{
	wr(0x31, (on ? 3 : 0)<<6 | 0x37); /* class D SPK out */
}

static void
toggle(Out *o, int on)
{
	if(on)
		reg1a |= o->togglemask;
	else
		reg1a &= ~o->togglemask;
	wr(0x1a, reg1a);
	if(o->toggle != nil)
		o->toggle(o, on);
}

static Out out[Nout] =
{
	[Dac] = {"master", 0x0a, 0xff, 3<<7, nil, 0},
	[Hp] = {"hp", 0x02, 0x7f, 3<<5, nil, 0},
	[Spk] = {"spk", 0x28, 0x7f, 3<<3, classdspk, 0},
};

static void
setvol(Out *o, int l, int r)
{
	int zc;

	l = CLAMP(l, 0, 100);
	r = CLAMP(r, 0, 100);
	if(l == o->vol[0] && r == o->vol[1])
		return;
	if((o->vol[0] = l) > 0)
		l += o->volmax - 100;
	if((o->vol[1] = r) > 0)
		r += o->volmax - 100;

	zc = o->volmax < 0x80;
	wr(o->volreg+0, 0<<8 | zc<<7 | l);
	wr(o->volreg+1, 1<<8 | zc<<7 | r);
}

static void
set3d(int x)
{
	d = CLAMP(x, 0, 100);
	x = (d+5)/7;
	wr(0x10, x<<1 | (x ? 1 : 0)<<0);
}

static int
setrate(int s)
{
	u32int k;

	if(s != 44100 && s != 48000)
		return -1;

	/*
	 * getting DAC ready for s16c2r44100:
	 *
	 * mclk₀ = 25Mhz (set in sai)
	 * pllprescale = /2 → *actual* mclk₁ is 25/2 = 12.5Mhz
	 * sysclk = 44.1kHz*256 = 11.2896Mhz
	 *   → dacdiv = /(1*256) = sysclk/(1*256) = 44.1kHz
	 * f₂ = 4*2*sysclk = 90.3168Mhz
	 *
	 * PLL freq ration:
	 *   R = f₂/mclk₁
	 *   N = int(R) = 7
	 *   K = 2²⁴*(R-N) = 3780644.9623
	 *
	 * dacdiv = /(1*256) → DAC at max rate
	 *  → pick bclk rate 1.4112Mhz (sysclk/8)
	 *  → bclkdiv = /8
	 *
	 * class D clk needs to be ~768kHz (700-800)
	 *  → sysclk/768000 = 14
	 *  → dclkdiv = /16 → dclk = 705.6kHz
	 */
	wr(0x1a, reg1a = reg1a & ~(1<<0)); /* disable pll */

	wr(0x04,
		0<<3 | /* dacdiv → sysclk/(1*256) = 44100 */
		2<<1 | /* sysclkdiv → /2 */
		1<<0 | /* clksel → pll output */
		0
	);
	wr(0x34,
		1<<5 | /* enable fractional mode */
		1<<4 | /* pllprescale */
		7<<0 | /* N */
		0
	);
	k = s == 44100 ? 3780645 : 14500883; /* K */
	wr(0x35, (k>>16) & 0xff);
	wr(0x36, (k>>8) & 0xff);
	wr(0x37, k & 0xff);

	wr(0x08, 7<<6 | 7<<0); /* dclkdiv → sysclk/16; bclkdiv → sysclk/8 */
	wr(0x1a, reg1a = reg1a | 1<<0); /* enable pll */

	rate = s;

	return 0;
}

static void
reset(void)
{
	toggle(out+Dac, 0);
	wr(0x1c, 1<<7 | 1<<4 | 1<<3 | 1<<2); /* Vmid/r bias; Vgs/r on; Vmid soft start */
	wr(0x19, 0<<7); /* Vmid off, Vref off */
	sleep(500);
	wr(0x0f, 0); /* reset registers to default */

	setrate(rate);
	set3d(d);

	wr(0x07, 1<<6 | 2); /* master mode; i²s, 16-bit words */

	wr(0x17, 1<<8 | 3<<6 | 1<<1); /* thermal shutdown on; avdd=3.3v; slow clock on */

	wr(0x06, 1<<3 | 1<<2); /* ramp up DAC volume slowly */
	wr(0x2f, 3<<2); /* output mixer on */
	wr(0x22, 1<<8); /* L DAC to mixer */
	wr(0x25, 1<<8); /* R DAC to mixer */

	wr(0x17, 1<<8 | 3<<6 | 1<<0); /* thermal shutdown on; avdd=3.3v; slow clock on */
	wr(0x1c, 1<<7 | 1<<4 | 1<<3 | 1<<2); /* Vmid/r bias; Vgs/r on; Vmid soft start */
	wr(0x19, 1<<7); /* start Vmid (playback) */
	sleep(650);
	wr(0x1c, 1<<3); /* done with anti-pop */
	wr(0x19, 1<<7 | 1<<6); /* Vref on */

	wr(0x30, 2<<4 | 2<<2 | 1<<1); /* JD2 jack detect; Tsense on */
	wr(0x1b, 1<<3); /* HP_[LR] responsive to jack detect */
	wr(0x18, 1<<6); /* HP switch on; high = HP */

	/* turn on all outputs */
	toggle(out+Hp, 1);
	toggle(out+Spk, 1);
	toggle(out+Dac, 1);

	/* sensible defaults */
	setvol(out+Spk, 80, 80);
	setvol(out+Hp, 65, 65);
	setvol(out+Dac, 80, 80);
	wr(0x05, 0<<3); /* unmute DAC */
}

static void
fsread(Req *r)
{
	char msg[256], *s, *e;
	Out *o;
	int i;

	s = msg;
	e = msg+sizeof(msg);
	*s = 0;
	if(r->fid->file->aux == (void*)Ctl){
		for(i = 0, o = out; i < Nout; i++, o++)
			s = seprint(s, e, "%s %s\n", o->name, o->on ? "on" : "off");
	}else if(r->fid->file->aux == (void*)Vol){
		for(i = 0, o = out; i < Nout; i++, o++)
			s = seprint(s, e, "%s %d %d\n", o->name, o->vol[0], o->vol[1]);
		s = seprint(s, e, "speed %d\n", rate);
	}
	seprint(s, e, "3d %d\n", d);

	readstr(r, msg);
	respond(r, nil);
}

static void
fswrite(Req *r)
{
	int nf, on, i, vl, vr;
	char msg[256], *f[4];
	Out *o;

	snprint(msg, sizeof(msg), "%.*s",
		utfnlen((char*)r->ifcall.data, r->ifcall.count), (char*)r->ifcall.data);
	nf = tokenize(msg, f, nelem(f));
	if(nf == 1 && strcmp(f[0], "reset") == 0){
		reset();
		goto Done;
	}else if(nf == 2 && strcmp(f[0], "speed") == 0){
		if(setrate(atoi(f[1])) != 0){
			respond(r, "not supported");
			return;
		}
		goto Done;
	}else if(nf == 2 && strcmp(f[0], "3d") == 0){
		set3d(atoi(f[1]));
		goto Done;
	}
	if(nf < 2){
Emsg:
		respond(r, "invalid ctl message");
		return;
	}

	for(i = 0, o = out; i < Nout && strcmp(f[0], o->name) != 0; i++, o++)
		;
	if(i >= Nout)
		goto Emsg;

	if(r->fid->file->aux == (void*)Ctl){
		if(nf != 2)
			goto Emsg;
		if(strcmp(f[1], "on") == 0)
			on = 1;
		else if(strcmp(f[1], "off") == 0)
			on = 0;
		else if(strcmp(f[1], "toggle") == 0)
			on = !o->on;
		else
			goto Emsg;
		toggle(o, on);
	}else if(r->fid->file->aux == (void*)Vol){
		vl = atoi(f[1]);
		vr = nf < 3 ? vl : atoi(f[2]);
		setvol(o, vl, vr);
	}

Done:
	r->ofcall.count = r->ifcall.count;
	respond(r, nil);
}

static Srv fs = {
	.read = fsread,
	.write = fswrite,
};

static void
usage(void)
{
	fprint(2, "usage: aux/wm8960 [-1] [-D] [-m /mnt/pm] [-s service]\n");
	exits("usage");
}

void
main(int argc, char **argv)
{
	char *mtpt, *srv;
	int ctl, oneshot;

	mtpt = "/mnt/wm8960";
	srv = nil;
	oneshot = 0;
	ARGBEGIN{
	case '1':
		oneshot = 1;
		break;
	case 'D':
		chatty9p = 1;
		break;
	case 'm':
		mtpt = EARGF(usage());
		break;
	case 's':
		srv = EARGF(usage());
		break;
	default:
		usage();
	}ARGEND

	if((data = open("#J/i2c3/i2c.1a.data", OWRITE)) < 0)
		sysfatal("i2c data: %r");
	if((ctl = open("#J/i2c3/i2c.1a.ctl", OWRITE)) < 0)
		sysfatal("i2c ctl: %r");
	fprint(ctl, "subaddress 1\n");
	fprint(ctl, "size %d\n", 0x38<<1);
	close(ctl);

	reset();

	if(oneshot)
		exits(nil);

	fs.tree = alloctree(uid, uid, DMDIR|0555, nil);
	createfile(fs.tree->root, "audioctl", uid, 0666, (void*)Ctl);
	createfile(fs.tree->root, "volume", uid, 0666, (void*)Vol);

	postmountsrv(&fs, srv, mtpt, MREPL);

	exits(nil);
}