Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
/* $NetBSD: xc5k.c,v 1.9 2018/09/03 16:29:31 riastradh Exp $ */

/*-
 * Copyright (c) 2010 Jared D. McNeill <jmcneill@invisible.ca>
 * 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
 */

/*
 * Xceive XC5000
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: xc5k.c,v 1.9 2018/09/03 16:29:31 riastradh Exp $");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/conf.h>
#include <sys/bus.h>
#include <sys/kmem.h>
#include <sys/mutex.h>
#include <sys/module.h>

#include <dev/firmload.h>
#include <dev/i2c/i2cvar.h>

#include <dev/i2c/xc5kreg.h>
#include <dev/i2c/xc5kvar.h>

#define	XC5K_FIRMWARE_DRVNAME	"xc5k"
#define	XC5K_FIRMWARE_IMGNAME	"dvb-fe-xc5000-1.6.114.fw"

#define	XC5K_FREQ_MIN		1000000
#define	XC5K_FREQ_MAX		1023000000

static kmutex_t xc5k_firmware_lock;

static int	xc5k_reset(struct xc5k *);
static int	xc5k_read_2(struct xc5k *, uint16_t, uint16_t *);
static int	xc5k_write_buffer(struct xc5k *, const uint8_t *, size_t);
static int	xc5k_firmware_open(struct xc5k *);
static int	xc5k_firmware_upload(struct xc5k *, const uint8_t *, size_t);

static int
xc5k_reset(struct xc5k *xc)
{
	int error = 0;

	if (xc->reset)
		error = xc->reset(xc->reset_priv);

	return error;
}

static int
xc5k_firmware_upload(struct xc5k *xc, const uint8_t *fw, size_t fwlen)
{
	const uint8_t *p;
	uint8_t cmd[64];
	unsigned int i;
	uint16_t len, rem;
	size_t wrlen;
	int error;

	for (i = 0; i < fwlen - 1;) {
		len = (fw[i] << 8) | fw[i + 1];
		i += 2;
		if (len == 0xffff)
			break;

		/* reset command */
		if (len == 0x0000) {
			error = xc5k_reset(xc);
			if (error)
				return error;
			continue;
		}

		/* delay command */
		if (len & 0x8000) {
			delay((len & 0x7fff) * 1000);
			continue;
		}

		if (i + len >= fwlen)
			break;

		cmd[0] = fw[i];
		cmd[1] = fw[i + 1];
		p = &fw[i + 2];
		rem = len - 2;
		while (rem > 0) {
			wrlen = uimin(rem, __arraycount(cmd) - 2);
			memcpy(&cmd[2], p, wrlen);
			error = xc5k_write_buffer(xc, cmd, wrlen + 2);
			if (error)
				return error;
			p += wrlen;
			rem -= wrlen;
		}
		i += len;
	}

	return 0;
}

static int
xc5k_firmware_open(struct xc5k *xc)
{
	firmware_handle_t fwh;
	uint16_t product_id, xcversion, xcbuild;
	uint8_t *fw = NULL;
	size_t fwlen;
	int error;

	mutex_enter(&xc5k_firmware_lock);
	
	error = xc5k_read_2(xc, XC5K_REG_PRODUCT_ID, &product_id);
	if (error || product_id != XC5K_PRODUCT_ID_NOFW)
		goto done;

	error = firmware_open(XC5K_FIRMWARE_DRVNAME,
	    XC5K_FIRMWARE_IMGNAME, &fwh);
	if (error)
		goto done;
	fwlen = firmware_get_size(fwh);
	fw = firmware_malloc(fwlen);
	if (fw == NULL) {
		firmware_close(fwh);
		error = ENOMEM;
		goto done;
	}
	error = firmware_read(fwh, 0, fw, fwlen);
	firmware_close(fwh);
	if (error)
		goto done;

	aprint_normal_dev(xc->parent, "xc5k: loading firmware '%s/%s'\n",
	    XC5K_FIRMWARE_DRVNAME, XC5K_FIRMWARE_IMGNAME);

	error = xc5k_firmware_upload(xc, fw, fwlen);
	if (error)
		goto done;

	error = xc5k_read_2(xc, XC5K_REG_VERSION, &xcversion);
	if (error) {
		error = 0;
		goto done;
	}

	error = xc5k_read_2(xc, XC5K_REG_BUILD, &xcbuild);
	if (error) {
		error = 0;
		xcbuild = 0;
	}

	aprint_normal_dev(xc->parent, "xc5k: hw %d.%d, fw %d.%d.%d\n",
	    (xcversion >> 12) & 0xf, (xcversion >> 8) & 0xf,
	    (xcversion >> 4) & 0xf, xcversion & 0xf, xcbuild);

done:
	if (fw)
		firmware_free(fw, fwlen);
	mutex_exit(&xc5k_firmware_lock);

	if (error)
		aprint_error_dev(xc->parent,
		    "xc5k: couldn't open firmware '%s/%s' (error=%d)\n",
		    XC5K_FIRMWARE_DRVNAME, XC5K_FIRMWARE_IMGNAME, error);

	return error;
}

static int
xc5k_read_2(struct xc5k *xc, uint16_t reg, uint16_t *val)
{
	uint8_t cmd[2], resp[2];
	int error;

	cmd[0] = reg >> 8;
	cmd[1] = reg & 0xff;
	error = iic_exec(xc->i2c, I2C_OP_WRITE, xc->i2c_addr,
	    cmd, sizeof(cmd), NULL, 0, 0);
	if (error)
		return error;
	resp[0] = resp[1] = 0;
	error = iic_exec(xc->i2c, I2C_OP_READ, xc->i2c_addr,
	    NULL, 0, resp, sizeof(resp), 0);
	if (error)
		return error;

	*val = (resp[0] << 8) | resp[1];

	return 0;
}

static int
xc5k_write_buffer(struct xc5k *xc, const uint8_t *data, size_t datalen)
{
	return iic_exec(xc->i2c, I2C_OP_WRITE, xc->i2c_addr,
	    data, datalen, NULL, 0, 0);
}

static int
xc5k_write_2(struct xc5k *xc, uint16_t reg, uint16_t val)
{
	uint8_t data[4];
	uint16_t busy;
	int error, retry;

	data[0] = reg >> 8;
	data[1] = reg & 0xff;
	data[2] = val >> 8;
	data[3] = val & 0xff;
	error = xc5k_write_buffer(xc, data, sizeof(data));
	if (error)
		return error;

	retry = 1000;
	while (--retry > 0) {
		error = xc5k_read_2(xc, XC5K_REG_BUSY, &busy);
		if (error || !busy)
			break;
		delay(5000);
	}

	return error;
}

struct xc5k *
xc5k_open(device_t parent, i2c_tag_t i2c, i2c_addr_t addr,
    xc5k_reset_cb reset, void *reset_priv, unsigned int if_freq,
    fe_type_t fe_type)
{
	struct xc5k *xc;
	uint16_t product_id;

	xc = kmem_alloc(sizeof(*xc), KM_SLEEP);
	xc->parent = parent;
	xc->i2c = i2c;
	xc->i2c_addr = addr;
	xc->reset = reset;
	xc->reset_priv = reset_priv;
	xc->if_freq = if_freq;
	xc->fe_type = fe_type;

	if (xc5k_read_2(xc, XC5K_REG_PRODUCT_ID, &product_id))
		goto failed;

	aprint_debug_dev(parent, "xc5k: product=0x%04x\n", product_id);

	if (product_id != XC5K_PRODUCT_ID_NOFW && product_id != XC5K_PRODUCT_ID)
		goto failed;

	if (xc5k_firmware_open(xc))
		goto failed;
	if (xc5k_write_2(xc, XC5K_REG_INIT, 0))
		goto failed;
	delay(100000);

	if (xc5k_read_2(xc, XC5K_REG_PRODUCT_ID, &product_id))
		goto failed;

	aprint_debug_dev(parent, "xc5k: product=0x%04x\n", product_id);

	return xc;

failed:
	kmem_free(xc, sizeof(*xc));
	return NULL;
}

void
xc5k_close(struct xc5k *xc)
{
	kmem_free(xc, sizeof(*xc));
}

int
xc5k_tune_video(struct xc5k *xc, struct xc5k_params *params)
{
	uint16_t amode, vmode;
	uint16_t lock, freq;
	int retry;

	switch (params->standard) {
	case VIDEO_STANDARD_NTSC_M:
	case VIDEO_STANDARD_NTSC_M_JP:
	case VIDEO_STANDARD_NTSC_M_KR:
		amode = XC5K_AUDIO_MODE_BTSC;
		vmode = XC5K_VIDEO_MODE_BTSC;
		break;
	default:
		return EINVAL;
	}

	if (xc5k_write_2(xc, XC5K_REG_SIGNAL_SOURCE, params->signal_source))
		return EIO;
	if (xc5k_write_2(xc, XC5K_REG_VIDEO_MODE, vmode))
		return EIO;
	if (xc5k_write_2(xc, XC5K_REG_AUDIO_MODE, amode))
		return EIO;
	if (xc5k_write_2(xc, XC5K_REG_OUTAMP, XC5K_OUTAMP_ANALOG))
		return EIO;
	freq = (params->frequency * 62500) / 15625;
#ifdef XC5K_DEBUG
	printf("xc5k_tune_video: frequency=%u (%u Hz)\n", params->frequency,
	    params->frequency * 62500);
	printf("           freq=%u\n", freq);
#endif
	if (xc5k_write_2(xc, XC5K_REG_FINER_FREQ, freq))
		return EIO;

	retry = 100;
	while (--retry > 0) {
		if (xc5k_read_2(xc, XC5K_REG_LOCK, &lock))
			return EIO;
#ifdef XC5K_DEBUG
		printf("xc5k_tune_video: lock=0x%04x\n", lock);
#endif
		if (lock == 1)
			break;
		delay(5000);
	}

	return 0;
}

int
xc5k_tune_dtv(struct xc5k *xc, const struct dvb_frontend_parameters *params)
{
	uint16_t amode, vmode;
	uint32_t freq, ifout;
	int signal_source;
	fe_modulation_t modulation;

	if (xc->fe_type == FE_ATSC)
		modulation = params->u.vsb.modulation;
	else if (xc->fe_type == FE_QAM)
		modulation = params->u.qam.modulation;
	else
		return EINVAL;

	switch (modulation) {
	case VSB_8:
	case VSB_16:
		signal_source = XC5K_SIGNAL_SOURCE_AIR;
		switch (xc->fe_type) {
		case FE_ATSC:
			amode = XC5K_AUDIO_MODE_DTV6;
			vmode = XC5K_VIDEO_MODE_DTV6;
			freq = params->frequency - 1750000;
			break;
		default:
			return EINVAL;
		}
		break;
	case QAM_16:
	case QAM_32:
	case QAM_64:
	case QAM_128:
	case QAM_256:
		signal_source = XC5K_SIGNAL_SOURCE_CABLE;
		switch (xc->fe_type) {
		case FE_ATSC:
			amode = XC5K_AUDIO_MODE_DTV6;
			vmode = XC5K_VIDEO_MODE_DTV6;
			freq = params->frequency - 1750000;
			break;
		case FE_QAM:
			amode = XC5K_AUDIO_MODE_DTV78;
			vmode = XC5K_VIDEO_MODE_DTV78;
			freq = params->frequency - 2750000;
			break;
		default:
			return EINVAL;
		}
		break;
	default:
		return EINVAL;
	}

	if (freq > XC5K_FREQ_MAX || freq < XC5K_FREQ_MIN)
		return ERANGE;

	if (xc5k_write_2(xc, XC5K_REG_SIGNAL_SOURCE, signal_source))
		return EIO;
	if (xc5k_write_2(xc, XC5K_REG_VIDEO_MODE, vmode))
		return EIO;
	if (xc5k_write_2(xc, XC5K_REG_AUDIO_MODE, amode))
		return EIO;
	ifout = ((xc->if_freq / 1000) * 1024) / 1000;
	if (xc5k_write_2(xc, XC5K_REG_IF_OUT, ifout))
		return EIO;
	if (xc5k_write_2(xc, XC5K_REG_OUTAMP, XC5K_OUTAMP_DIGITAL))
		return EIO;
	freq = (uint16_t)(freq / 15625);
	if (xc5k_write_2(xc, XC5K_REG_FINER_FREQ, freq))
		return EIO;

	return 0;
}

fe_status_t
xc5k_get_status(struct xc5k *xc)
{
	uint16_t lock_status;
	fe_status_t festatus = 0;

	if (xc5k_read_2(xc, XC5K_REG_LOCK, &lock_status))
		return 0;
	if (lock_status & XC5K_LOCK_LOCKED) {
		festatus |= FE_HAS_LOCK;
		if ((lock_status & XC5K_LOCK_NOSIGNAL) == 0)
			festatus |= FE_HAS_SIGNAL;
	}

	return festatus;
}

MODULE(MODULE_CLASS_DRIVER, xc5k, "i2cexec");

static int
xc5k_modcmd(modcmd_t cmd, void *opaque)
{
	switch (cmd) {
	case MODULE_CMD_INIT:
		mutex_init(&xc5k_firmware_lock, MUTEX_DEFAULT, IPL_NONE);
		return 0;
	case MODULE_CMD_FINI:
		mutex_destroy(&xc5k_firmware_lock);
		return 0;
	default:
		return ENOTTY;
	}
}