summaryrefslogtreecommitdiff
path: root/sys/dev/isa/aztech.c
blob: dddc9be96648c7560ca10b4c21cb25433c69ab8a (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
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
/*	$OpenBSD: aztech.c,v 1.1 2001/10/04 20:15:42 gluk Exp $	*/
/* $RuOBSD: aztech.c,v 1.8 2001/10/04 18:51:50 pva Exp $ */

/*
 * Copyright (c) 2001 Maxim Tsyplakov <tm@oganer.net>,
 *                    Vladimir Popov <jumbo@narod.ru>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/* Aztech/PackardBell FM Radio Card device driver */

/*
 * Sanyo LM7001J Direct PLL Frequency Synthesizer:
 *     ??? See http://www.redsword.com/tjacobs/geeb/fmcard.htm
 *
 * Philips TEA5712T AM/FM Stereo DTS Radio:
 *     http://www.semiconductors.philips.com/pip/TEA5712
 */

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/proc.h>
#include <sys/errno.h>
#include <sys/ioctl.h>
#include <sys/device.h>
#include <sys/radioio.h>

#include <machine/bus.h>

#include <dev/isa/isavar.h>
#include <dev/ic/lm700x.h>
#include <dev/radio_if.h>

#define RF_25K	25
#define RF_50K	50
#define RF_100K	100

#define MAX_VOL	3
#define VOLUME_RATIO(x)	(255 * x / MAX_VOL)

#define AZ_BASE_VALID(x)	((x == 0x350) || (x == 0x358))
#define AZTECH_CAPABILITIES	RADIO_CAPS_DETECT_STEREO | 		\
				RADIO_CAPS_DETECT_SIGNAL | 		\
				RADIO_CAPS_SET_MONO | 			\
				RADIO_CAPS_REFERENCE_FREQ

#define	AZ_WREN_ON	(1 << 1)
#define	AZ_WREN_OFF	(0 << 1)

#define AZ_CLCK_ON	(1 << 6)
#define AZ_CLCK_OFF	(0 << 6)

#define AZ_DATA_ON	(1 << 7)
#define AZ_DATA_OFF	(0 << 7)

int	az_probe(struct device *, void *, void *);
void	az_attach(struct device *, struct device * self, void *);
int	az_open(dev_t, int, int, struct proc *);
int	az_close(dev_t, int, int, struct proc *);
int	az_ioctl(dev_t, u_long, caddr_t, int, struct proc *);

struct radio_hw_if az_hw_if = {
	az_open,
	az_close,
	az_ioctl
};

struct az_softc {
	struct device	sc_dev;

	u_long		sc_freq;
	u_long		sc_rf;
	u_char		sc_vol;
	u_char		sc_muted;
	u_long		sc_stereo;
	struct lm700x_t	lm;
};

struct cfattach az_ca = {
	sizeof(struct az_softc), az_probe, az_attach
};

struct cfdriver az_cd = {
	NULL, "az", DV_DULL
};

u_int	az_find(bus_space_tag_t, bus_space_handle_t);
void	az_set_mute(struct az_softc *);
void	az_set_freq(struct az_softc *, u_long);
u_char	az_state(bus_space_tag_t, bus_space_handle_t);

void	az_lm700x_init(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_long);
void	az_lm700x_rset(bus_space_tag_t, bus_space_handle_t, bus_size_t, u_long);

u_char	az_conv_vol(u_char);
u_char	az_unconv_vol(u_char);

int
az_probe(struct device *parent, void *self, void *aux)
{
	struct isa_attach_args *ia = aux;
	bus_space_tag_t iot = ia->ia_iot;
	bus_space_handle_t ioh;

	int iosize = 1, iobase = ia->ia_iobase;

	if (!AZ_BASE_VALID(iobase)) {
		printf("az: configured iobase 0x%x invalid", iobase);
		return 0;
	}

	if (bus_space_map(iot, iobase, iosize, 0, &ioh))
		return 0;

	bus_space_unmap(iot, ioh, iosize);

	if (!az_find(iot, ioh))
		return 0;

	ia->ia_iosize = iosize;
	return 1;
}

void
az_attach(struct device *parent, struct device *self, void *aux)
{
	struct az_softc *sc = (void *) self;
	struct isa_attach_args *ia = aux;

	sc->lm.iot = ia->ia_iot;
	sc->sc_rf = LM700X_REF_050;
	sc->sc_stereo = LM700X_STEREO;
#ifdef RADIO_INIT_MUTE
	sc->sc_muted = RADIO_INIT_MUTE;
#else
	sc->sc_muted = 0;
#endif /* RADIO_INIT_MUTE */
#ifdef RADIO_INIT_FREQ
	sc->sc_freq = RADIO_INIT_FREQ;
#else
	sc->sc_freq = MIN_FM_FREQ;
#endif /* RADIO_INIT_FREQ */
#ifdef RADIO_INIT_VOL
	sc->sc_vol = az_conv_vol(RADIO_INIT_VOL);
#else
	sc->sc_vol = 0;
#endif /* RADIO_INIT_VOL */

	/* remap I/O */
	if (bus_space_map(sc->lm.iot, ia->ia_iobase, ia->ia_iosize,
			  0, &sc->lm.ioh))
		panic(": bus_space_map() of %s failed", sc->sc_dev.dv_xname);

	printf(": Aztech/PackardBell");

	/* Configure struct lm700x_t lm */
	sc->lm.offset = 0;
	sc->lm.wzcl = AZ_WREN_ON | AZ_CLCK_OFF | AZ_DATA_OFF;
	sc->lm.wzch = AZ_WREN_ON | AZ_CLCK_ON  | AZ_DATA_OFF;
	sc->lm.wocl = AZ_WREN_ON | AZ_CLCK_OFF | AZ_DATA_ON;
	sc->lm.woch = AZ_WREN_ON | AZ_CLCK_ON  | AZ_DATA_ON;
	sc->lm.initdata = 0;
	sc->lm.rsetdata = AZ_DATA_ON | AZ_CLCK_ON | AZ_WREN_OFF;
	sc->lm.init = az_lm700x_init;
	sc->lm.rset = az_lm700x_rset;

	az_set_freq(sc, sc->sc_freq);

	radio_attach_mi(&az_hw_if, sc, &sc->sc_dev);
}

int
az_open(dev_t dev, int flags, int fmt, struct proc *p)
{
	struct az_softc *sc;
	return !(sc = az_cd.cd_devs[0]) ? ENXIO : 0;
}

int
az_close(dev_t dev, int flags, int fmt, struct proc *p)
{
	return 0;
}

/*
 * Handle the ioctl for the device
 */
int
az_ioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct proc *p)
{
	int error;
	struct az_softc *sc = az_cd.cd_devs[0];

	error = 0;
	switch (cmd) {
	case RIOCGMUTE:
		*(u_long *)data = sc->sc_muted ? 1 : 0;
		break;
	case RIOCSMUTE:
		sc->sc_muted = *(u_long *)data ? 1 : 0;
		az_set_mute(sc);
		break;
	case RIOCGVOLU:
		*(u_long *)data = az_unconv_vol(sc->sc_vol);
		break;
	case RIOCSVOLU:
		sc->sc_vol = az_conv_vol(*(u_int *)data);
		az_set_mute(sc);
		break;
	case RIOCGMONO:
		*(u_long *)data = sc->sc_stereo == LM700X_STEREO ? 0 : 1;
		break;
	case RIOCSMONO:
		sc->sc_stereo = *(u_long *)data ? LM700X_MONO : LM700X_STEREO;
		az_set_freq(sc, sc->sc_freq);
		break;
	case RIOCGFREQ:
		*(u_long *)data = sc->sc_freq;
		break;
	case RIOCSFREQ:
		az_set_freq(sc, *(u_long *)data);
		break;
	case RIOCGCAPS:
		*(u_long *)data = AZTECH_CAPABILITIES;
		break;
	case RIOCGINFO: /* Get Info */
		*(u_long *)data = 0x03 &
			(3 ^ az_state(sc->lm.iot, sc->lm.ioh));
		break;
	case RIOCSREFF:
		sc->sc_rf = lm700x_encode_ref(*(u_char *)data);
		az_set_freq(sc, sc->sc_freq);
		break;
	case RIOCGREFF:
		*(u_long *)data = lm700x_decode_ref(sc->sc_rf);
		break;
	case RIOCSSRCH:
		/* FALLTHROUGH */
	case RIOCSLOCK:
		/* FALLTHROUGH */
	case RIOCGLOCK:
		/* NOT SUPPORTED */
		error = ENODEV;
		break;
	default:
		error = EINVAL;
	}
	return (error);
}

/*
 * Mute the card
 */
void
az_set_mute(struct az_softc *sc)
{
	bus_space_write_1(sc->lm.iot, sc->lm.ioh, 0,
	    sc->sc_muted ? 0 : sc->sc_vol);
	DELAY(6);
	bus_space_write_1(sc->lm.iot, sc->lm.ioh, 0,
	    sc->sc_muted ? 0 : sc->sc_vol);
}

void
az_set_freq(struct az_softc *sc, u_long nfreq)
{
	u_char vol;
	u_long reg;

	vol = sc->sc_muted ? 0 : sc->sc_vol;

	if (nfreq > MAX_FM_FREQ)
		nfreq = MAX_FM_FREQ;
	if (nfreq < MIN_FM_FREQ)
		nfreq = MIN_FM_FREQ;

	sc->sc_freq = nfreq;

	reg = lm700x_encode_freq(nfreq, sc->sc_rf);
	reg |= sc->sc_stereo | sc->sc_rf | LM700X_DIVIDER_FM;

	lm700x_hardware_write(&sc->lm, reg, vol);

	az_set_mute(sc);
}

/*
 * Return state of the card - tuned/not tuned, mono/stereo
 */
u_char
az_state(bus_space_tag_t iot, bus_space_handle_t ioh)
{
	return bus_space_read_1(iot, ioh, 0) & 3;
}

/*
 * Convert volume to hardware representation.
 * The card uses bits 00000x0x to set volume.
 */
u_char
az_conv_vol(u_char vol)
{
	if (vol < VOLUME_RATIO(1))
		return 0;
	else if (vol >= VOLUME_RATIO(1) && vol < VOLUME_RATIO(2))
		return 1;
	else if (vol >= VOLUME_RATIO(2) && vol < VOLUME_RATIO(3))
		return 4;
	else
		return 5;
}

/*
 * Convert volume from hardware representation
 */
u_char
az_unconv_vol(u_char vol)
{
	switch (vol) {
	case 0:
		return VOLUME_RATIO(0);
	case 1:
		return VOLUME_RATIO(1);
	case 4:
		return VOLUME_RATIO(2);
	}
	return VOLUME_RATIO(3);
}

u_int
az_find(bus_space_tag_t iot, bus_space_handle_t ioh)
{
	struct az_softc sc;
	u_int i, scanres = 0;

	sc.lm.iot = iot;
	sc.lm.ioh = ioh;
	sc.lm.offset = 0;
	sc.lm.wzcl = AZ_WREN_ON | AZ_CLCK_OFF | AZ_DATA_OFF;
	sc.lm.wzch = AZ_WREN_ON | AZ_CLCK_ON  | AZ_DATA_OFF;
	sc.lm.wocl = AZ_WREN_ON | AZ_CLCK_OFF | AZ_DATA_ON;
	sc.lm.woch = AZ_WREN_ON | AZ_CLCK_ON  | AZ_DATA_ON;
	sc.lm.initdata = 0;
	sc.lm.rsetdata = AZ_DATA_ON | AZ_CLCK_ON | AZ_WREN_OFF;
	sc.lm.init = az_lm700x_init;
	sc.lm.rset = az_lm700x_rset;
	sc.sc_rf = LM700X_REF_050;
	sc.sc_muted = 0;
	sc.sc_stereo = LM700X_STEREO;
	sc.sc_vol = 0;

	/*
	 * Scan whole FM range. If there is a card it'll
	 * respond on some frequency.
	 */
	for (i = MIN_FM_FREQ; !scanres && i < MAX_FM_FREQ; i += 10) {
		az_set_freq(&sc, i);
		scanres += 3 - az_state(iot, ioh);
	}

	return scanres;
}

void
az_lm700x_init(bus_space_tag_t iot, bus_space_handle_t ioh, bus_size_t off,
		u_long data)
{
	/* Do nothing */
	return;
}

void
az_lm700x_rset(bus_space_tag_t iot, bus_space_handle_t ioh, bus_size_t off,
		u_long data)
{
	bus_space_write_1(iot, ioh, off, data);
}