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
/* $NetBSD: au8522.c,v 1.8 2017/06/01 02:45:10 chs 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.
 */

/*
 * Auvitek AU8522
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: au8522.c,v 1.8 2017/06/01 02:45:10 chs 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/module.h>

#include <dev/dtv/dtvio.h>

#include <dev/i2c/i2cvar.h>

#include <dev/i2c/au8522reg.h>
#include <dev/i2c/au8522var.h>
#include <dev/i2c/au8522mod.h>

static int	au8522_reset(struct au8522 *);
static int	au8522_read_1(struct au8522 *, uint16_t, uint8_t *);
static int	au8522_write_1(struct au8522 *, uint16_t, uint8_t);
static int	au8522_set_vinput(struct au8522 *, au8522_vinput_t);
static int	au8522_set_ainput(struct au8522 *, au8522_ainput_t);
static void	au8522_set_common(struct au8522 *, au8522_vinput_t);

static int
au8522_reset(struct au8522 *au)
{
	return au8522_write_1(au, 0xa4, 1 << 5);
}

static int
au8522_read_1(struct au8522 *au, uint16_t reg, uint8_t *val)
{
	uint8_t cmd[2];
	int error;

	cmd[0] = (reg >> 8) | 0x40;
	cmd[1] = reg & 0xff;
	error = iic_exec(au->i2c, I2C_OP_WRITE, au->i2c_addr,
	    cmd, sizeof(cmd), NULL, 0, 0);
	if (error)
		return error;
	return iic_exec(au->i2c, I2C_OP_READ, au->i2c_addr,
		    NULL, 0, val, sizeof(*val), 0);
}

static int
au8522_write_1(struct au8522 *au, uint16_t reg, uint8_t val)
{
	uint8_t data[3];

	data[0] = (reg >> 8) | 0x80;
	data[1] = reg & 0xff;
	data[2] = val;
	return iic_exec(au->i2c, I2C_OP_WRITE, au->i2c_addr,
	    data, sizeof(data), NULL, 0, 0);
}

static int
au8522_set_vinput(struct au8522 *au, au8522_vinput_t vi)
{
	switch (vi) {
	case AU8522_VINPUT_CVBS:
		au8522_write_1(au, AU8522_REG_MODCLKCTL, AU8522_MODCLKCTL_CVBS);
		au8522_write_1(au, AU8522_REG_PGACTL, 0x00);
		au8522_write_1(au, AU8522_REG_CLAMPCTL, 0x0e);
		au8522_write_1(au, AU8522_REG_PGACTL, 0x10);
		au8522_write_1(au, AU8522_REG_INPUTCTL,
		    AU8522_INPUTCTL_CVBS_CH1);

		au8522_set_common(au, vi);

		au8522_write_1(au, AU8522_REG_SYSMODCTL0,
		    AU8522_SYSMODCTL0_CVBS);
		break;
	case AU8522_VINPUT_SVIDEO:
		au8522_write_1(au, AU8522_REG_MODCLKCTL,
		    AU8522_MODCLKCTL_SVIDEO);
		au8522_write_1(au, AU8522_REG_INPUTCTL,
		    AU8522_INPUTCTL_SVIDEO_CH13);
		au8522_write_1(au, AU8522_REG_CLAMPCTL, 0x00);

		au8522_set_common(au, vi);

		au8522_write_1(au, AU8522_REG_SYSMODCTL0,
		    AU8522_SYSMODCTL0_CVBS);

		break;
	case AU8522_VINPUT_CVBS_TUNER:
		au8522_write_1(au, AU8522_REG_MODCLKCTL, AU8522_MODCLKCTL_CVBS);
		au8522_write_1(au, AU8522_REG_PGACTL, 0x00);
		au8522_write_1(au, AU8522_REG_CLAMPCTL, 0x0e);
		au8522_write_1(au, AU8522_REG_PGACTL, 0x10);
		au8522_write_1(au, AU8522_REG_INPUTCTL,
		    AU8522_INPUTCTL_CVBS_CH4_SIF);

		au8522_set_common(au, vi);

		au8522_write_1(au, AU8522_REG_SYSMODCTL0,
		    AU8522_SYSMODCTL0_CVBS);

		break;
	default:
		return EINVAL;
	}

	return 0;
}

static void
au8522_set_common(struct au8522 *au, au8522_vinput_t vi)
{
	au8522_write_1(au, AU8522_REG_INTMASK, 0x00);
	au8522_write_1(au, AU8522_REG_VIDEOMODE, vi == AU8522_VINPUT_SVIDEO ?
	    AU8522_VIDEOMODE_SVIDEO : AU8522_VIDEOMODE_CVBS);
	au8522_write_1(au, AU8522_REG_TV_PGA, AU8522_TV_PGA_CVBS);
}

static int
au8522_set_ainput(struct au8522 *au, au8522_ainput_t ai)
{
	/* mute during mode change */
	au8522_write_1(au, AU8522_REG_AUDIO_VOL_L, 0x00);
	au8522_write_1(au, AU8522_REG_AUDIO_VOL_R, 0x00);
	au8522_write_1(au, AU8522_REG_AUDIO_VOL, 0x00);

	switch (ai) {
	case AU8522_AINPUT_SIF:
		au8522_write_1(au, AU8522_REG_SYSMODCTL0,
		    AU8522_SYSMODCTL0_CVBS);
		au8522_write_1(au, AU8522_REG_AUDIO_MODE, 0x82);
		au8522_write_1(au, AU8522_REG_SYSMODCTL1,
		    AU8522_SYSMODCTL1_I2S);
		au8522_write_1(au, AU8522_REG_AUDIO_FREQ, 0x03);
		au8522_write_1(au, AU8522_REG_I2S_CTL2, 0xc2);
		/* unmute */
		au8522_write_1(au, AU8522_REG_AUDIO_VOL_L, 0x7f);
		au8522_write_1(au, AU8522_REG_AUDIO_VOL_R, 0x7f);
		au8522_write_1(au, AU8522_REG_AUDIO_VOL, 0xff);
		break;
	case AU8522_AINPUT_NONE:
		au8522_write_1(au, AU8522_REG_USBEN, 0x00);
		au8522_write_1(au, AU8522_REG_AUDIO_VOL_L, 0x7f);
		au8522_write_1(au, AU8522_REG_AUDIO_VOL_R, 0x7f);
		au8522_write_1(au, AU8522_REG_AUDIO_MODE, 0x40);
		au8522_write_1(au, AU8522_REG_SYSMODCTL1,
		    AU8522_SYSMODCTL1_SVIDEO);
		au8522_write_1(au, AU8522_REG_AUDIO_FREQ, 0x03);
		au8522_write_1(au, AU8522_REG_I2S_CTL2, 0x02);
		au8522_write_1(au, AU8522_REG_SYSMODCTL0,
		    AU8522_SYSMODCTL0_CVBS);
		break;
	default:
		return EINVAL;
	}
	return 0;
}

static int
au8522_set_if(struct au8522 *au)
{
	uint8_t ifinit[3];
	unsigned int n;

	switch (au->if_freq) {
	case 6000000:	/* 6MHz */
		ifinit[0] = 0xfb;
		ifinit[1] = 0x8e;
		ifinit[2] = 0x39;
		break;
	default:
		aprint_error_dev(au->parent, "au8522: unsupported if freq %dHz\n", au->if_freq);
		return EINVAL;
	}

	for (n = 0; n < __arraycount(ifinit); n++)
		au8522_write_1(au, 0x80b5 + n, ifinit[n]);

	return 0;
}

struct au8522 *
au8522_open(device_t parent, i2c_tag_t i2c, i2c_addr_t addr, unsigned int if_freq)
{
	struct au8522 *au;

	au = kmem_alloc(sizeof(*au), KM_SLEEP);
	au->parent = parent;
	au->i2c = i2c;
	au->i2c_addr = addr;
	au->current_modulation = -1;
	au->if_freq = if_freq;

	if (au8522_reset(au))
		goto failed;
	if (au8522_write_1(au, AU8522_REG_TUNERCTL, AU8522_TUNERCTL_EN))
		goto failed;

	return au;

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

void
au8522_close(struct au8522 *au)
{
	kmem_free(au, sizeof(*au));
}

void
au8522_enable(struct au8522 *au, bool enable)
{
	if (enable) {
		au8522_write_1(au, AU8522_REG_SYSMODCTL0,
		    AU8522_SYSMODCTL0_RESET);
		delay(1000);
		au8522_write_1(au, AU8522_REG_SYSMODCTL0,
		    AU8522_SYSMODCTL0_CVBS);
	} else {
		au8522_write_1(au, AU8522_REG_SYSMODCTL0,
		    AU8522_SYSMODCTL0_DISABLE);
	}
}

void
au8522_set_input(struct au8522 *au, au8522_vinput_t vi, au8522_ainput_t ai)
{
	au8522_reset(au);

	if (vi != AU8522_VINPUT_UNCONF)
		au8522_set_vinput(au, vi);
	if (ai != AU8522_AINPUT_UNCONF)
		au8522_set_ainput(au, ai);
}

int
au8522_get_signal(struct au8522 *au)
{
	uint8_t status;

	if (au8522_read_1(au, AU8522_REG_STATUS, &status))
		return 0;

#ifdef AU8522_DEBUG
	printf("au8522: status=0x%02x\n", status);
#endif
	return (status & AU8522_STATUS_LOCK) == AU8522_STATUS_LOCK ? 1 : 0;
}

void
au8522_set_audio(struct au8522 *au, bool onoff)
{
	if (onoff) {
		au8522_write_1(au, AU8522_REG_AUDIO_VOL_L, 0x7f);
		au8522_write_1(au, AU8522_REG_AUDIO_VOL_R, 0x7f);
		au8522_write_1(au, AU8522_REG_AUDIO_VOL, 0xff);
	} else {
		au8522_write_1(au, AU8522_REG_AUDIO_VOL_L, 0x00);
		au8522_write_1(au, AU8522_REG_AUDIO_VOL_R, 0x00);
		au8522_write_1(au, AU8522_REG_AUDIO_VOL, 0x00);
	}
}

int
au8522_set_modulation(struct au8522 *au, fe_modulation_t modulation)
{
	const struct au8522_modulation_table *modtab = NULL;
	size_t modtablen;
	unsigned int n;

	switch (modulation) {
	case VSB_8:
		modtab = au8522_modulation_8vsb;
		modtablen = __arraycount(au8522_modulation_8vsb);
		break;
	case QAM_64:
		modtab = au8522_modulation_qam64;
		modtablen = __arraycount(au8522_modulation_qam64);
		break;
	case QAM_256:
		modtab = au8522_modulation_qam256;
		modtablen = __arraycount(au8522_modulation_qam256);
		break;
	default:
		return EINVAL;
	}

	for (n = 0; n < modtablen; n++)
		au8522_write_1(au, modtab[n].reg, modtab[n].val);

	au8522_set_if(au);

	au->current_modulation = modulation;

	return 0;
}

void
au8522_set_gate(struct au8522 *au, bool onoff)
{
	au8522_write_1(au, AU8522_REG_TUNERCTL, onoff ? AU8522_TUNERCTL_EN : 0);
}

fe_status_t
au8522_get_dtv_status(struct au8522 *au)
{
	fe_status_t status = 0;
	uint8_t val;

	switch (au->current_modulation) {
	case VSB_8:
		if (au8522_read_1(au, 0x4088, &val))
			return 0;
		if ((val & 0x03) == 0x03) {
			status |= FE_HAS_SIGNAL;
			status |= FE_HAS_CARRIER;
			status |= FE_HAS_VITERBI;
		}
		break;
	case QAM_64:
	case QAM_256:
		if (au8522_read_1(au, 0x4541, &val))
			return 0;
		if (val & 0x80) {
			status |= FE_HAS_VITERBI;
		}
		if (val & 0x20) {
			status |= FE_HAS_SIGNAL;
			status |= FE_HAS_CARRIER;
		}
		break;
	default:
		break;
	}

	if (status & FE_HAS_VITERBI) {
		status |= FE_HAS_SYNC;
		status |= FE_HAS_LOCK;
	}

	return status;
}

uint16_t
au8522_get_snr(struct au8522 *au)
{
	const struct au8522_snr_table *snrtab = NULL;
	uint16_t snrreg;
	uint8_t val;
	size_t snrtablen;
	unsigned int n;

	switch (au->current_modulation) {
	case VSB_8:
		snrtab = au8522_snr_8vsb;
		snrtablen = __arraycount(au8522_snr_8vsb);
		snrreg = AU8522_REG_SNR_VSB;
		break;
	case QAM_64:
		snrtab = au8522_snr_qam64;
		snrtablen = __arraycount(au8522_snr_qam64);
		snrreg = AU8522_REG_SNR_QAM;
		break;
	case QAM_256:
		snrtab = au8522_snr_qam256;
		snrtablen = __arraycount(au8522_snr_qam256);
		snrreg = AU8522_REG_SNR_QAM;
		break;
	default:
		return 0;
	}

	if (au8522_read_1(au, snrreg, &val))
		return 0;

	for (n = 0; n < snrtablen; n++) {
		if (val < snrtab[n].val)
			return snrtab[n].snr;
	}

	return 0;
}

MODULE(MODULE_CLASS_DRIVER, au8522, "i2cexec");

static int
au8522_modcmd(modcmd_t cmd, void *opaque)
{
	switch (cmd) {
	case MODULE_CMD_INIT:
		return 0;
	case MODULE_CMD_FINI:
		return 0;
	default:
		return ENOTTY;
	}
}