xref: /netbsd/sys/dev/ic/lm700x.c (revision bf9ec67e)
1 /* $NetBSD: lm700x.c,v 1.1 2002/01/01 21:51:40 augustss Exp $ */
2 /*	$OpenBSD: lm700x.c,v 1.2 2001/12/06 16:28:18 mickey Exp $	*/
3 
4 /*
5  * Copyright (c) 2001 Vladimir Popov <jumbo@narod.ru>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
26  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /* Implementation of most common lm700x routines */
30 
31 /*
32  * Sanyo LM7001 Direct PLL Frequency Synthesizer
33  *    ??? See http://www.redsword.com/tjacobs/geeb/fmcard.htm
34  *
35  * The LM7001J and LM7001JM (used in Aztech/PackardBell cards) are PLL
36  * frequency synthesizer LSIs for tuners. These LSIs are software compatible
37  * with LM7000 (used in Radiotrack, Radioreveal RA300, some Mediaforte cards),
38  * but do not include an IF calculation circuit.
39  *
40  * The FM VCO circuit includes a high-speed programmable divider that can
41  * divide directly.
42  *
43  * Features:
44  * Seven reference frequencies: 1, 5, 9, 10, 25, 50, and 100 kHz;
45  * Band-switching outputs (3 bits);
46  * Controller clock output (400 kHz);
47  * Serial input circuit for data input (using the CE, CL and DATA pins).
48  *
49  * The LM7001J and LM7001JM have a 24-bit shift register.
50  */
51 
52 #include <sys/param.h>
53 #include <sys/radioio.h>
54 
55 #include <dev/ic/lm700x.h>
56 
57 u_int32_t
58 lm700x_encode_freq(u_int32_t nfreq, u_int32_t rf)
59 {
60 	nfreq += IF_FREQ;
61 	nfreq /= lm700x_decode_ref(rf);
62 	return nfreq;
63 }
64 
65 void
66 lm700x_hardware_write(struct lm700x_t *lm, u_int32_t data, u_int32_t addon)
67 {
68 	int i;
69 
70 	lm->init(lm->iot, lm->ioh, lm->offset, lm->rsetdata | addon);
71 
72 	for (i = 0; i < LM700X_REGISTER_LENGTH; i++)
73 		if (data & (1 << i)) {
74 			bus_space_write_1(lm->iot, lm->ioh, lm->offset,
75 					lm->wocl | addon);
76 			DELAY(LM700X_WRITE_DELAY);
77 			bus_space_write_1(lm->iot, lm->ioh, lm->offset,
78 					lm->woch | addon);
79 			DELAY(LM700X_WRITE_DELAY);
80 			bus_space_write_1(lm->iot, lm->ioh, lm->offset,
81 					lm->wocl | addon);
82 		} else {
83 			bus_space_write_1(lm->iot, lm->ioh, lm->offset,
84 					lm->wzcl | addon);
85 			DELAY(LM700X_WRITE_DELAY);
86 			bus_space_write_1(lm->iot, lm->ioh, lm->offset,
87 					lm->wzch | addon);
88 			DELAY(LM700X_WRITE_DELAY);
89 			bus_space_write_1(lm->iot, lm->ioh, lm->offset,
90 					lm->wzcl | addon);
91 		}
92 
93 	lm->rset(lm->iot, lm->ioh, lm->offset, lm->rsetdata | addon);
94 }
95 
96 u_int32_t
97 lm700x_encode_ref(u_int8_t rf)
98 {
99 	u_int32_t ret;
100 
101 	if (rf < 36)
102 		ret = LM700X_REF_025;
103 	else if (rf > 35 && rf < 75)
104 			ret = LM700X_REF_050;
105 	else
106 		ret = LM700X_REF_100;
107 
108 	return ret;
109 }
110 
111 u_int8_t
112 lm700x_decode_ref(u_int32_t rf)
113 {
114 	u_int8_t ret;
115 
116 	switch (rf) {
117 	case LM700X_REF_100:
118 		ret = 100;
119 		break;
120 	case LM700X_REF_025:
121 		ret = 25;
122 		break;
123 	case LM700X_REF_050:
124 		/* FALLTHROUGH */
125 	default:
126 		ret = 50;
127 		break;
128 	}
129 
130 	return ret;
131 }
132