summaryrefslogtreecommitdiff
path: root/sys/dev/i2c/mt2131.c
blob: d9a78b42dd95a3b17d651bee2ccc4d45a27c65ad (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
/* $NetBSD: mt2131.c,v 1.7 2019/12/23 18:57:30 thorpej Exp $ */

/*
 * Copyright (c) 2008, 2011 Jonathan A. Kollasch
 * 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 COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "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 COPYRIGHT HOLDER OR
 * CONTRIBUTORS 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.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: mt2131.c,v 1.7 2019/12/23 18:57:30 thorpej Exp $");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/kmem.h>
#include <sys/syslog.h>
#include <sys/proc.h>
#include <sys/module.h>

#include <dev/i2c/mt2131var.h>

#define PWR 0x07
#define UPC_1 0x0b
#define AGC_RL 0x10
#define MISC_2 0x15

#define IF1 1220
#define IF2 44000
#define REF 16000

static const uint8_t mt2131_initstring[] = {
	0x01,
	0x50, 0x00, 0x50, 0x80, 0x00, 0x49,
	0xfa, 0x88, 0x08, 0x77, 0x41, 0x04, 0x00, 0x00, 0x00, 0x32,
	0x7f, 0xda, 0x4c, 0x00, 0x10, 0xaa, 0x78, 0x80, 0xff, 0x68,
	0xa0, 0xff, 0xdd, 0x00, 0x00
};

static const uint8_t mt2131_agcinitstring[] = {
        AGC_RL,
        0x7f, 0xc8, 0x0a, 0x5f, 0x00, 0x04
};


struct mt2131_softc {
	device_t		parent;
	i2c_tag_t		tag;
	i2c_addr_t		addr;
	uint32_t		frequency;
	uint32_t		bandwidth;
};

static int mt2131_init(struct mt2131_softc *);

static int mt2131_read(struct mt2131_softc *, uint8_t, uint8_t *);
static int mt2131_write(struct mt2131_softc *, uint8_t, uint8_t);

struct mt2131_softc *
mt2131_open(device_t parent, i2c_tag_t t, i2c_addr_t a)
{
	struct mt2131_softc *sc;
	int ret;
	uint8_t cmd, reg;

	cmd = reg = 0;

	/* get id reg */
	ret = iic_acquire_bus(t, 0);
	if (ret == 0) {
		ret = iic_exec(t, I2C_OP_READ_WITH_STOP, a, &cmd, 1, &reg, 1,
		    0);
		iic_release_bus(t, 0);
	}

	if (ret) {
		device_printf(parent, "%s(): read fail\n", __func__);
		return NULL;
	}

	if ((reg & 0xfe) != 0x3e) {
		device_printf(parent, "%s(): chip id %02x unknown\n",
		    __func__, reg);
		return NULL;
	}

	sc = kmem_alloc(sizeof(*sc), KM_SLEEP);
	sc->parent = parent;
	sc->tag = t;
	sc->addr = a;

	mt2131_init(sc);

	return sc;
}

void
mt2131_close(struct mt2131_softc *sc)
{
	kmem_free(sc, sizeof(*sc));
}

int
mt2131_tune_dtv(struct mt2131_softc *sc, const struct dvb_frontend_parameters *p)
{
	int rv, i;
	uint64_t o1, o2;
	uint64_t d1, d2;
	uint32_t r1, r2;
	uint32_t fr;
	uint8_t b[7];
	uint8_t regval;

	mt2131_init(sc);

	b[0] = 0x01;

	if(p->frequency != 0 &&
		(p->frequency < 50000000 || p->frequency > 1000000000))
		return EINVAL;

	fr = p->frequency / 1000;

	o1 = fr + IF1 * 1000;
	o2 = o1 - fr - IF2;

	d1 = (o1 * 8192)/REF;
	d2 = (o2 * 8192)/REF;

	r1 = d1/8192;
	r2 = d2/8192;

	b[1] = (d1 & 0x1fe0) >> 5;
	b[2] = (d1 & 0x001f);
	b[3] = r1;
	b[4] = (d2 & 0x1fe0) >> 5;
	b[5] = (d2 & 0x001f);
	b[6] = r2;

	rv = iic_acquire_bus(sc->tag, 0);
	if (rv == 0) {
		rv = iic_exec(sc->tag, I2C_OP_WRITE_WITH_STOP, sc->addr, b, 7,
		    NULL, 0, 0);
		iic_release_bus(sc->tag, 0);
	}

	regval = (fr - 27501) / 55000;

	if(regval > 0x13)
		regval = 0x13;

	rv = mt2131_write(sc, UPC_1, regval);

	if (rv != 0)
		device_printf(sc->parent, "%s write failed\n", __func__);

	sc->frequency = (o1 - o2 - IF2) * 1000;

	for (i = 0; i < 100; i++) {
		kpause("mt2131", true, 1, NULL);

		rv = mt2131_read(sc, 0x08, &regval);
		if (rv != 0)
			device_printf(sc->parent, "%s read failed\n", __func__);

		if (( regval & 0x88 ) == 0x88 ) {
			return 0;
		}
	}

	device_printf(sc->parent, "mt2131 not locked, %02x\n", b[1]);

	return rv;
}

static int
mt2131_init(struct mt2131_softc *sc)
{
	int ret;

	ret = iic_acquire_bus(sc->tag, 0);
	if (ret)
		return -1;
	ret = iic_exec(sc->tag, I2C_OP_WRITE_WITH_STOP, sc->addr,
	    mt2131_initstring, sizeof(mt2131_initstring), NULL, 0, 0);
	iic_release_bus(sc->tag, 0);
	if (ret)
		return -1;

	ret = mt2131_write(sc, UPC_1, 0x09);
	ret = mt2131_write(sc, MISC_2, 0x47);
	ret = mt2131_write(sc, PWR, 0xf2);
	ret = mt2131_write(sc, UPC_1, 0x01);

	ret = iic_acquire_bus(sc->tag, 0);
	if (ret)
		return -1;
	ret = iic_exec(sc->tag, I2C_OP_WRITE_WITH_STOP, sc->addr,
	    mt2131_agcinitstring, sizeof(mt2131_agcinitstring),
	    NULL, 0, 0);
	iic_release_bus(sc->tag, 0);
	if (ret)
		return -1;
	
	return 0;
}

static int
mt2131_read(struct mt2131_softc *sc, uint8_t r, uint8_t *v)
{
	int ret;

	ret = iic_acquire_bus(sc->tag, 0);
	if (ret)
		return ret;
	ret = iic_exec(sc->tag, I2C_OP_READ_WITH_STOP, sc->addr,
	    &r, 1, v, 1, 0);

	iic_release_bus(sc->tag, 0);

	return ret;
}

static int
mt2131_write(struct mt2131_softc *sc, uint8_t a, uint8_t v)
{
	int ret;
	uint8_t b[] = { a, v };

	ret = iic_acquire_bus(sc->tag, 0);
	if (ret)
		return ret;

	ret = iic_exec(sc->tag, I2C_OP_READ_WITH_STOP, sc->addr,
	    b, sizeof(b), NULL, 0, 0);

	iic_release_bus(sc->tag, 0);

	return ret;
}

MODULE(MODULE_CLASS_DRIVER, mt2131, "i2cexec");

static int
mt2131_modcmd(modcmd_t cmd, void *priv)
{
	if (cmd == MODULE_CMD_INIT || cmd == MODULE_CMD_FINI)
		return 0;
	return ENOTTY;
}