1 |
87fd3c1f
|
Harald Welte
|
|
2 |
|
|
/* (C) 2005-2011 by maintech GmbH
|
3 |
|
|
* (C) 2011-2012 by Harald Welte <laforge@gnumonks.org>
|
4 |
|
|
*
|
5 |
|
|
* All Rights Reserved
|
6 |
|
|
*
|
7 |
|
|
* This program is free software; you can redistribute it and/or modify
|
8 |
|
|
* it under the terms of the GNU General Public License as published by
|
9 |
|
|
* the Free Software Foundation; either version 3 of the License, or
|
10 |
|
|
* (at your option) any later version.
|
11 |
|
|
*
|
12 |
|
|
* This program is distributed in the hope that it will be useful,
|
13 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
14 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
15 |
|
|
* GNU General Public License for more details.
|
16 |
|
|
*
|
17 |
|
|
* You should have received a copy of the GNU General Public License
|
18 |
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
19 |
|
|
*/
|
20 |
|
|
|
21 |
|
|
#include <time.h>
|
22 |
|
|
#include <math.h>
|
23 |
|
|
#include <stdio.h>
|
24 |
|
|
#include <errno.h>
|
25 |
|
|
|
26 |
|
|
#include <si570.h>
|
27 |
|
|
#include <logging.h>
|
28 |
|
|
#include <utility/trace.h>
|
29 |
|
|
|
30 |
|
|
#include <twi/twid.h>
|
31 |
|
|
|
32 |
|
|
static void udelay(uint32_t usec)
|
33 |
|
|
{
|
34 |
|
|
uint32_t i, j;
|
35 |
|
|
volatile uint32_t k;
|
36 |
|
|
|
37 |
|
|
for (i = 0; i < usec; i++) {
|
38 |
|
|
for (j = 0; j < 0xff; j++) {
|
39 |
|
|
k = 0;
|
40 |
|
|
}
|
41 |
|
|
}
|
42 |
|
|
}
|
43 |
|
|
|
44 |
|
|
static int smbus8_read_bytes(void *i2c, uint8_t addr, uint8_t reg_nr,
|
45 |
|
|
uint8_t *out, uint8_t num)
|
46 |
|
|
{
|
47 |
|
|
unsigned char rc;
|
48 |
|
|
|
49 |
|
|
rc = TWID_Read(i2c, addr, reg_nr, 1, out, num, NULL);
|
50 |
|
|
if (rc != 0) {
|
51 |
|
|
LOGP(DVCO, LOGL_ERROR, "Error %u in TWID_Read\n", rc);
|
52 |
|
|
return -EIO;
|
53 |
|
|
}
|
54 |
|
|
|
55 |
|
|
return rc;
|
56 |
|
|
}
|
57 |
|
|
|
58 |
|
|
static int smbus8_read_byte(void *i2c, uint8_t addr, uint8_t reg_nr,
|
59 |
|
|
uint8_t *val)
|
60 |
|
|
{
|
61 |
|
|
return smbus8_read_bytes(i2c, addr, reg_nr, val, 1);
|
62 |
|
|
}
|
63 |
|
|
|
64 |
|
|
static int smbus8_write_bytes(void *i2c, uint8_t addr, uint8_t reg_nr,
|
65 |
|
|
uint8_t *val, uint8_t num)
|
66 |
|
|
{
|
67 |
|
|
unsigned char rc;
|
68 |
|
|
|
69 |
|
|
rc = TWID_Write(i2c, addr, reg_nr, 1, val, num, NULL);
|
70 |
|
|
if (rc != 0) {
|
71 |
|
|
LOGP(DVCO, LOGL_ERROR, "Error %u in TWID_Write\n", rc);
|
72 |
|
|
return -EIO;
|
73 |
|
|
}
|
74 |
|
|
|
75 |
|
|
return rc;
|
76 |
|
|
}
|
77 |
|
|
|
78 |
|
|
static int smbus8_write_byte(void *i2c, uint8_t addr, uint8_t reg_nr,
|
79 |
|
|
uint8_t val)
|
80 |
|
|
{
|
81 |
|
|
return smbus8_write_bytes(i2c, addr, reg_nr, &val, 1);
|
82 |
|
|
}
|
83 |
|
|
|
84 |
|
|
static const struct si570_info si570_data[2] = {
|
85 |
|
|
{.freq_range = {{.min=10000, .max=945000}, {.min=970000, .max=1134000}, {.min=1213000, .max=1417500}}, .init_freq=856000 },
|
86 |
|
|
{.freq_range = {{.min=10000, .max=280000}}, .init_freq=100000 },
|
87 |
|
|
};
|
88 |
|
|
|
89 |
|
|
|
90 |
|
|
//init module
|
91 |
|
|
int si570_init(struct si570_ctx *ctx, void *i2c_dev, uint8_t i2c_addr)
|
92 |
|
|
{
|
93 |
|
|
ctx->i2c = i2c_dev;
|
94 |
|
|
ctx->slave_addr = i2c_addr;
|
95 |
60237432
|
Christian Daniel
|
ctx->init = 0;
|
96 |
87fd3c1f
|
Harald Welte
|
|
97 |
60237432
|
Christian Daniel
|
return si570_reinit(ctx);
|
98 |
|
|
}
|
99 |
|
|
|
100 |
|
|
int si570_reinit(struct si570_ctx *ctx)
|
101 |
|
|
{
|
102 |
87fd3c1f
|
Harald Welte
|
TRACE_DEBUG("si570_init()\r\n");
|
103 |
|
|
|
104 |
|
|
if (0 != si570_reset(ctx)) {
|
105 |
|
|
TRACE_DEBUG("SI570 reset failed.\n");
|
106 |
|
|
return -EIO;
|
107 |
|
|
}
|
108 |
|
|
TRACE_DEBUG("si570_init():2\r\n");
|
109 |
|
|
|
110 |
|
|
if (0 != si570_read_calibration(ctx)) {
|
111 |
|
|
TRACE_DEBUG("SI570 init failed.\n");
|
112 |
|
|
return -EIO;
|
113 |
|
|
}
|
114 |
|
|
TRACE_DEBUG("si570_init():3\r\n");
|
115 |
|
|
|
116 |
|
|
ctx->init = 1;
|
117 |
|
|
|
118 |
|
|
return 0;
|
119 |
|
|
}
|
120 |
|
|
|
121 |
60237432
|
Christian Daniel
|
|
122 |
87fd3c1f
|
Harald Welte
|
void si570_print_info(struct si570_ctx *ctx)
|
123 |
|
|
{
|
124 |
|
|
TRACE_DEBUG("SI570 XTAL: %u Hz REF: %u Hz\n", ctx->xtal >> 3, ctx->info->init_freq * 1000);
|
125 |
|
|
}
|
126 |
|
|
|
127 |
|
|
static int hs_to_div(int n1)
|
128 |
|
|
{
|
129 |
|
|
switch (n1) {
|
130 |
|
|
case 0: return 4;
|
131 |
|
|
case 1: return 5;
|
132 |
|
|
case 2: return 6;
|
133 |
|
|
case 3: return 7;
|
134 |
|
|
case 5: return 9;
|
135 |
|
|
case 7: return 11;
|
136 |
|
|
}
|
137 |
|
|
|
138 |
|
|
return 0;
|
139 |
|
|
}
|
140 |
|
|
|
141 |
|
|
int si570_reset(struct si570_ctx *ctx)
|
142 |
|
|
{
|
143 |
|
|
TRACE_DEBUG("si570_reset:1\r\n");
|
144 |
|
|
smbus8_write_byte(ctx->i2c, ctx->slave_addr, 135, 80);
|
145 |
|
|
udelay(1000);
|
146 |
|
|
TRACE_DEBUG("si570_reset:2\r\n");
|
147 |
|
|
smbus8_write_byte(ctx->i2c, ctx->slave_addr, 135, 01);
|
148 |
|
|
udelay(1000);
|
149 |
|
|
TRACE_DEBUG("si570_reset:3\r\n");
|
150 |
|
|
|
151 |
|
|
return 0;
|
152 |
|
|
}
|
153 |
|
|
|
154 |
|
|
int si570_read_calibration(struct si570_ctx *ctx)
|
155 |
|
|
{
|
156 |
|
|
int n;
|
157 |
|
|
int res;
|
158 |
|
|
uint8_t data[6];
|
159 |
|
|
uint64_t xd, xf;
|
160 |
|
|
int n1, hs_div;
|
161 |
|
|
|
162 |
|
|
TRACE_DEBUG("si570_read_calib:1\r\n");
|
163 |
|
|
if (0 != (res = smbus8_read_bytes(ctx->i2c, ctx->slave_addr, 7, data, 6))) {
|
164 |
|
|
TRACE_DEBUG ("SI570 calibration read error (%i)\n", res);
|
165 |
|
|
return -EINVAL;
|
166 |
|
|
}
|
167 |
|
|
TRACE_DEBUG("si570_read_calib:2\r\n");
|
168 |
|
|
|
169 |
|
|
xd = data[1] & 0x3F;
|
170 |
|
|
|
171 |
|
|
xd <<= 8;
|
172 |
|
|
xd |= data[2];
|
173 |
|
|
|
174 |
|
|
xd <<= 8;
|
175 |
|
|
xd |= data[3];
|
176 |
|
|
|
177 |
|
|
xd <<= 8;
|
178 |
|
|
xd |= data[4];
|
179 |
|
|
|
180 |
|
|
xd <<= 8;
|
181 |
|
|
xd |= data[5];
|
182 |
|
|
|
183 |
|
|
TRACE_DEBUG("si570_read_calib:2b\r\n");
|
184 |
|
|
n1 = ((data[0] << 2) & 0x1F) | (data[1] >> 6);
|
185 |
|
|
hs_div = (data[0] >> 5);
|
186 |
|
|
|
187 |
|
|
TRACE_DEBUG("si570_read_calib:2c\r\n");
|
188 |
|
|
n = 1; if (xd > 0) {
|
189 |
|
|
//for (n=0; n<2; n++) {
|
190 |
|
|
|
191 |
|
|
TRACE_DEBUG("si570_read_calib:2d\r\n");
|
192 |
|
|
xf = si570_data[n].init_freq * 1000;
|
193 |
|
|
TRACE_DEBUG("si570_read_calib:2e\r\n");
|
194 |
|
|
xf *= hs_to_div(hs_div) * (n1+1);
|
195 |
|
|
TRACE_DEBUG("si570_read_calib:2f\r\n");
|
196 |
|
|
xf<<=31;
|
197 |
|
|
TRACE_DEBUG("si570_read_calib:2g\r\n");
|
198 |
|
|
|
199 |
|
|
ctx->xtal = (xf + (xd/2)) / xd;
|
200 |
|
|
|
201 |
|
|
TRACE_DEBUG("si570_read_calib:2h\r\n");
|
202 |
|
|
|
203 |
|
|
ctx->info = &si570_data[n];
|
204 |
|
|
|
205 |
|
|
TRACE_DEBUG("si570_read_calib:3\r\n");
|
206 |
|
|
return 0;
|
207 |
|
|
}
|
208 |
|
|
|
209 |
|
|
return -EINVAL;
|
210 |
|
|
}
|
211 |
|
|
|
212 |
|
|
//set frequency
|
213 |
|
|
int si570_set_freq(struct si570_ctx *ctx, uint32_t freq, int trim)
|
214 |
|
|
{
|
215 |
|
|
uint32_t dco_min = 4850000ULL;
|
216 |
|
|
int mul, hsv, n1;
|
217 |
|
|
int freq_in_range = 0;
|
218 |
|
|
|
219 |
|
|
ctx->lock = 0;
|
220 |
|
|
|
221 |
|
|
for (mul = (dco_min / freq) + 1; (freq * mul) <= 5670000; mul++) {
|
222 |
|
|
for (hsv=7; hsv>=0; hsv--) {
|
223 |
|
|
int hsdiv = hs_to_div(hsv);
|
224 |
|
|
|
225 |
|
|
if (!hsdiv) continue;
|
226 |
|
|
if (mul%hsdiv) continue;
|
227 |
|
|
|
228 |
|
|
n1 = mul / hsdiv;
|
229 |
|
|
|
230 |
|
|
if (n1 > 128) continue;
|
231 |
|
|
|
232 |
|
|
if (n1 != 1) {
|
233 |
|
|
if (n1 < 1) continue;
|
234 |
|
|
if (n1 % 2) continue;
|
235 |
|
|
}
|
236 |
|
|
|
237 |
|
|
goto found_solution;
|
238 |
|
|
}
|
239 |
|
|
}
|
240 |
|
|
|
241 |
|
|
TRACE_DEBUG("SI570 no solution\n");
|
242 |
|
|
|
243 |
|
|
return -EINVAL;
|
244 |
|
|
|
245 |
|
|
found_solution:
|
246 |
|
|
|
247 |
|
|
{
|
248 |
|
|
uint8_t data[6];
|
249 |
|
|
uint64_t xf, xd, xr;
|
250 |
|
|
uint32_t n1v;
|
251 |
|
|
|
252 |
|
|
xf = freq;
|
253 |
|
|
xf*= 1000;
|
254 |
|
|
xf+= trim;
|
255 |
|
|
|
256 |
|
|
xd = hs_to_div(hsv) * n1 * xf;
|
257 |
|
|
xd<<=31;
|
258 |
|
|
|
259 |
|
|
xr = (xd + ctx->xtal/2) / ctx->xtal;
|
260 |
|
|
|
261 |
|
|
n1v= n1 - 1;
|
262 |
|
|
|
263 |
|
|
data[0] = (hsv << 5) | (n1v>> 2);
|
264 |
|
|
data[1] = (n1v << 6) | (xr >> 32);
|
265 |
|
|
data[2] = (xr >> 24);
|
266 |
|
|
data[3] = (xr >> 16);
|
267 |
|
|
data[4] = (xr >> 8);
|
268 |
|
|
data[5] = (xr >> 0);
|
269 |
|
|
|
270 |
|
|
smbus8_write_byte(ctx->i2c, ctx->slave_addr, 137, 0x10);
|
271 |
|
|
|
272 |
|
|
smbus8_write_bytes(ctx->i2c,ctx->slave_addr, 7, data, 6);
|
273 |
|
|
|
274 |
|
|
smbus8_write_byte(ctx->i2c, ctx->slave_addr, 137, 0x00);
|
275 |
|
|
smbus8_write_byte(ctx->i2c, ctx->slave_addr, 135, 0x40);
|
276 |
|
|
|
277 |
|
|
ctx->lock = 1;
|
278 |
|
|
}
|
279 |
|
|
return 0;
|
280 |
|
|
}
|
281 |
|
|
|
282 |
|
|
int si570_get_lock(struct si570_ctx *ctx)
|
283 |
|
|
{
|
284 |
|
|
return ctx->init && ctx->lock;
|
285 |
|
|
}
|
286 |
|
|
|
287 |
|
|
//dump all registers
|
288 |
|
|
void si570_regdump(struct si570_ctx *ctx)
|
289 |
|
|
{
|
290 |
|
|
uint8_t data[8];
|
291 |
|
|
|
292 |
|
|
if (!ctx->init) {
|
293 |
|
|
TRACE_DEBUG("SI570 not initialized\n");
|
294 |
|
|
return;
|
295 |
|
|
}
|
296 |
|
|
|
297 |
|
|
smbus8_read_bytes(ctx->i2c, ctx->slave_addr, 7, data, 6);
|
298 |
|
|
smbus8_read_byte(ctx->i2c, ctx->slave_addr, 135, &data[6]);
|
299 |
|
|
smbus8_read_byte(ctx->i2c, ctx->slave_addr, 137, &data[7]);
|
300 |
|
|
|
301 |
|
|
TRACE_DEBUG("SI570 regs: %02X %02X %02X %02X %02X %02X %02X %02X\n",
|
302 |
|
|
data[0], data[1], data[2], data[3],
|
303 |
|
|
data[4], data[5], data[6], data[7]);
|
304 |
|
|
}
|
305 |
60237432
|
Christian Daniel
|
|
306 |
|
|
//write register(s)
|
307 |
|
|
int si570_reg_write(struct si570_ctx *ctx, uint8_t reg, int len, const uint8_t* data)
|
308 |
|
|
{
|
309 |
|
|
return smbus8_write_bytes(ctx->i2c, ctx->slave_addr, reg, data, len);
|
310 |
|
|
}
|