summaryrefslogtreecommitdiffstats
path: root/firmware/src/si570.c
blob: 1ada99c92c580a3d9157f8b036a5ae9a51e0bd2d (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
/* (C) 2005-2011 by maintech GmbH
 * (C) 2011-2012 by Harald Welte <laforge@gnumonks.org>
 *
 * All Rights Reserved
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <time.h>
#include <math.h>
#include <stdio.h>
#include <errno.h>

#include <si570.h>
#include <logging.h>
#include <utility/trace.h>

#include <twi/twid.h>

static void udelay(uint32_t usec)
{
	uint32_t i, j;
	volatile uint32_t k;

	for (i = 0; i < usec; i++) {
		for (j = 0; j < 0xff; j++) {
			k = 0;
		}
	}
}

static int smbus8_read_bytes(void *i2c, uint8_t addr, uint8_t reg_nr,
			    uint8_t *out, uint8_t num)
{
	unsigned char rc;

	rc = TWID_Read(i2c, addr, reg_nr, 1, out, num, NULL);
	if (rc != 0) {
		LOGP(DVCO, LOGL_ERROR, "Error %u in TWID_Read\n", rc);
		return -EIO;
	}

	return rc;
}

static int smbus8_read_byte(void *i2c, uint8_t addr, uint8_t reg_nr,
			    uint8_t *val)
{
	return smbus8_read_bytes(i2c, addr, reg_nr, val, 1);
}

static int smbus8_write_bytes(void *i2c, uint8_t addr, uint8_t reg_nr,
			      uint8_t *val, uint8_t num)
{
	unsigned char rc;

	rc = TWID_Write(i2c, addr, reg_nr, 1, val, num, NULL);
	if (rc != 0) {
		LOGP(DVCO, LOGL_ERROR, "Error %u in TWID_Write\n", rc);
		return -EIO;
	}

	return rc;
}

static int smbus8_write_byte(void *i2c, uint8_t addr, uint8_t reg_nr,
			      uint8_t val)
{
	return smbus8_write_bytes(i2c, addr, reg_nr, &val, 1);
}

static const struct si570_info si570_data[2] = {
	{.freq_range = {{.min=10000, .max=945000}, {.min=970000, .max=1134000}, {.min=1213000, .max=1417500}}, .init_freq=856000 },
	{.freq_range = {{.min=10000, .max=280000}}, .init_freq=100000 },
};


//init module
int si570_init(struct si570_ctx *ctx, void *i2c_dev, uint8_t i2c_addr)
{
	ctx->i2c = i2c_dev;
	ctx->slave_addr = i2c_addr;
	ctx->init = 0;

	return si570_reinit(ctx);
}

int si570_reinit(struct si570_ctx *ctx)
{
	TRACE_DEBUG("si570_init()\r\n");

	if (0 != si570_reset(ctx)) {
		TRACE_DEBUG("SI570 reset failed.\n");
		return -EIO;
	}
	TRACE_DEBUG("si570_init():2\r\n");

	if (0 != si570_read_calibration(ctx)) {
		TRACE_DEBUG("SI570 init failed.\n");
		return -EIO;
	}
	TRACE_DEBUG("si570_init():3\r\n");

	ctx->init = 1;

	return 0;
}


void si570_print_info(struct si570_ctx *ctx)
{
	TRACE_DEBUG("SI570 XTAL: %u Hz  REF: %u Hz\n", ctx->xtal >> 3, ctx->info->init_freq * 1000);
}

static int hs_to_div(int n1)
{
	switch (n1) {
		case 0: return 4;
		case 1: return 5;
		case 2: return 6;
		case 3: return 7;
		case 5: return 9;
		case 7: return 11;
	}

	return 0;
}

int si570_reset(struct si570_ctx *ctx)
{
	TRACE_DEBUG("si570_reset:1\r\n");
	smbus8_write_byte(ctx->i2c, ctx->slave_addr, 135, 80);
	udelay(1000);
	TRACE_DEBUG("si570_reset:2\r\n");
	smbus8_write_byte(ctx->i2c, ctx->slave_addr, 135, 01);
	udelay(1000);
	TRACE_DEBUG("si570_reset:3\r\n");

	return 0;
}

int si570_read_calibration(struct si570_ctx *ctx)
{
	int n;
	int res;
	uint8_t data[6];
	uint64_t xd, xf;
	int n1, hs_div;

	TRACE_DEBUG("si570_read_calib:1\r\n");
	if (0 != (res = smbus8_read_bytes(ctx->i2c, ctx->slave_addr, 7, data, 6))) {
		TRACE_DEBUG ("SI570 calibration read error (%i)\n", res);
		return -EINVAL;
	}
	TRACE_DEBUG("si570_read_calib:2\r\n");

	xd   = data[1] & 0x3F;

	xd <<= 8;
	xd  |= data[2];

	xd <<= 8;
	xd  |= data[3];

	xd <<= 8;
	xd  |= data[4];

	xd <<= 8;
	xd  |= data[5];

	TRACE_DEBUG("si570_read_calib:2b\r\n");
	n1     = ((data[0] << 2) & 0x1F) | (data[1] >> 6);
	hs_div = (data[0] >> 5);

	TRACE_DEBUG("si570_read_calib:2c\r\n");
	n = 1; if (xd > 0) {
	//for (n=0; n<2; n++) {

		TRACE_DEBUG("si570_read_calib:2d\r\n");
		xf  = si570_data[n].init_freq * 1000;
		TRACE_DEBUG("si570_read_calib:2e\r\n");
		xf *= hs_to_div(hs_div) * (n1+1);
		TRACE_DEBUG("si570_read_calib:2f\r\n");
		xf<<=31;
		TRACE_DEBUG("si570_read_calib:2g\r\n");

		ctx->xtal = (xf + (xd/2)) / xd;

		TRACE_DEBUG("si570_read_calib:2h\r\n");

		ctx->info = &si570_data[n];

		TRACE_DEBUG("si570_read_calib:3\r\n");
		return 0;
	}

	return -EINVAL;
}

//set frequency
int si570_set_freq(struct si570_ctx *ctx, uint32_t freq, int trim)
{
	uint32_t dco_min = 4850000ULL;
	int mul, hsv, n1;
	int freq_in_range = 0;

	ctx->lock = 0;

	for (mul = (dco_min / freq) + 1; (freq * mul) <= 5670000; mul++) {
		for (hsv=7; hsv>=0; hsv--) {
			int hsdiv = hs_to_div(hsv);

			if (!hsdiv) continue;
			if (mul%hsdiv) continue;

			n1 = mul / hsdiv;

			if (n1 > 128) continue;

			if (n1 != 1) {
				if (n1 < 1) continue;
				if (n1 % 2) continue;
			}

			goto found_solution;
		}
	}

	TRACE_DEBUG("SI570 no solution\n");

	return -EINVAL;

found_solution:

{
	uint8_t data[6];
	uint64_t xf, xd, xr;
	uint32_t n1v;

	xf = freq;
	xf*= 1000;
	xf+= trim;

	xd = hs_to_div(hsv) * n1 * xf;
	xd<<=31;

	xr = (xd + ctx->xtal/2) / ctx->xtal;

	n1v= n1 - 1;

	data[0] = (hsv << 5) | (n1v>> 2);
	data[1] = (n1v << 6) | (xr >> 32);
	data[2] = (xr  >> 24);
	data[3] = (xr  >> 16);
	data[4] = (xr  >> 8);
	data[5] = (xr  >> 0);

	smbus8_write_byte(ctx->i2c, ctx->slave_addr, 137, 0x10);

	smbus8_write_bytes(ctx->i2c,ctx->slave_addr, 7, data, 6);

	smbus8_write_byte(ctx->i2c, ctx->slave_addr, 137, 0x00);
	smbus8_write_byte(ctx->i2c, ctx->slave_addr, 135, 0x40);

	ctx->lock = 1;
}
	return 0;
}

int si570_get_lock(struct si570_ctx *ctx)
{
	return ctx->init && ctx->lock;
}

//dump all registers
void si570_regdump(struct si570_ctx *ctx)
{
	uint8_t data[8];

	if (!ctx->init) {
		TRACE_DEBUG("SI570 not initialized\n");
		return;
	}

	smbus8_read_bytes(ctx->i2c, ctx->slave_addr, 7, data, 6);
	smbus8_read_byte(ctx->i2c, ctx->slave_addr, 135, &data[6]);
	smbus8_read_byte(ctx->i2c, ctx->slave_addr, 137, &data[7]);

	TRACE_DEBUG("SI570 regs: %02X %02X %02X %02X %02X %02X %02X %02X\n",
		data[0], data[1], data[2], data[3],
		data[4], data[5], data[6], data[7]);
}

//write register(s)
int si570_reg_write(struct si570_ctx *ctx, uint8_t reg, int len, const uint8_t* data)
{
	return smbus8_write_bytes(ctx->i2c, ctx->slave_addr, reg, data, len);
}