Home | History | Annotate | Line # | Download | only in i2c
xc5k.c revision 1.8.2.1
      1  1.8.2.1  christos /* $NetBSD: xc5k.c,v 1.8.2.1 2019/06/10 22:07:09 christos Exp $ */
      2      1.1  jmcneill 
      3      1.1  jmcneill /*-
      4      1.1  jmcneill  * Copyright (c) 2010 Jared D. McNeill <jmcneill (at) invisible.ca>
      5      1.1  jmcneill  * All rights reserved.
      6      1.1  jmcneill  *
      7      1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
      8      1.1  jmcneill  * modification, are permitted provided that the following conditions
      9      1.1  jmcneill  * are met:
     10      1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     11      1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     12      1.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     13      1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     14      1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     15      1.1  jmcneill  *
     16      1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17      1.1  jmcneill  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18      1.1  jmcneill  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19      1.1  jmcneill  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20      1.1  jmcneill  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21      1.1  jmcneill  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22      1.1  jmcneill  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23      1.1  jmcneill  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24      1.1  jmcneill  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25      1.1  jmcneill  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26      1.1  jmcneill  * POSSIBILITY OF SUCH DAMAGE.
     27      1.1  jmcneill  */
     28      1.1  jmcneill 
     29      1.1  jmcneill /*
     30      1.1  jmcneill  * Xceive XC5000
     31      1.1  jmcneill  */
     32      1.1  jmcneill 
     33      1.1  jmcneill #include <sys/cdefs.h>
     34  1.8.2.1  christos __KERNEL_RCSID(0, "$NetBSD: xc5k.c,v 1.8.2.1 2019/06/10 22:07:09 christos Exp $");
     35      1.1  jmcneill 
     36      1.1  jmcneill #include <sys/param.h>
     37      1.1  jmcneill #include <sys/systm.h>
     38      1.1  jmcneill #include <sys/device.h>
     39      1.1  jmcneill #include <sys/conf.h>
     40      1.1  jmcneill #include <sys/bus.h>
     41      1.1  jmcneill #include <sys/kmem.h>
     42      1.1  jmcneill #include <sys/mutex.h>
     43      1.1  jmcneill #include <sys/module.h>
     44      1.1  jmcneill 
     45      1.1  jmcneill #include <dev/firmload.h>
     46      1.1  jmcneill #include <dev/i2c/i2cvar.h>
     47      1.1  jmcneill 
     48      1.1  jmcneill #include <dev/i2c/xc5kreg.h>
     49      1.1  jmcneill #include <dev/i2c/xc5kvar.h>
     50      1.1  jmcneill 
     51      1.1  jmcneill #define	XC5K_FIRMWARE_DRVNAME	"xc5k"
     52      1.1  jmcneill #define	XC5K_FIRMWARE_IMGNAME	"dvb-fe-xc5000-1.6.114.fw"
     53      1.1  jmcneill 
     54      1.3  jmcneill #define	XC5K_FREQ_MIN		1000000
     55      1.3  jmcneill #define	XC5K_FREQ_MAX		1023000000
     56      1.3  jmcneill 
     57      1.1  jmcneill static kmutex_t xc5k_firmware_lock;
     58      1.1  jmcneill 
     59      1.1  jmcneill static int	xc5k_reset(struct xc5k *);
     60      1.1  jmcneill static int	xc5k_read_2(struct xc5k *, uint16_t, uint16_t *);
     61      1.1  jmcneill static int	xc5k_write_buffer(struct xc5k *, const uint8_t *, size_t);
     62      1.1  jmcneill static int	xc5k_firmware_open(struct xc5k *);
     63      1.1  jmcneill static int	xc5k_firmware_upload(struct xc5k *, const uint8_t *, size_t);
     64      1.1  jmcneill 
     65      1.1  jmcneill static int
     66      1.1  jmcneill xc5k_reset(struct xc5k *xc)
     67      1.1  jmcneill {
     68      1.1  jmcneill 	int error = 0;
     69      1.1  jmcneill 
     70      1.1  jmcneill 	if (xc->reset)
     71      1.1  jmcneill 		error = xc->reset(xc->reset_priv);
     72      1.1  jmcneill 
     73      1.1  jmcneill 	return error;
     74      1.1  jmcneill }
     75      1.1  jmcneill 
     76      1.1  jmcneill static int
     77      1.1  jmcneill xc5k_firmware_upload(struct xc5k *xc, const uint8_t *fw, size_t fwlen)
     78      1.1  jmcneill {
     79      1.1  jmcneill 	const uint8_t *p;
     80      1.1  jmcneill 	uint8_t cmd[64];
     81      1.1  jmcneill 	unsigned int i;
     82      1.1  jmcneill 	uint16_t len, rem;
     83      1.1  jmcneill 	size_t wrlen;
     84      1.1  jmcneill 	int error;
     85      1.1  jmcneill 
     86      1.1  jmcneill 	for (i = 0; i < fwlen - 1;) {
     87      1.1  jmcneill 		len = (fw[i] << 8) | fw[i + 1];
     88      1.1  jmcneill 		i += 2;
     89      1.1  jmcneill 		if (len == 0xffff)
     90      1.1  jmcneill 			break;
     91      1.1  jmcneill 
     92      1.1  jmcneill 		/* reset command */
     93      1.1  jmcneill 		if (len == 0x0000) {
     94      1.1  jmcneill 			error = xc5k_reset(xc);
     95      1.1  jmcneill 			if (error)
     96      1.1  jmcneill 				return error;
     97      1.1  jmcneill 			continue;
     98      1.1  jmcneill 		}
     99      1.1  jmcneill 
    100      1.1  jmcneill 		/* delay command */
    101      1.1  jmcneill 		if (len & 0x8000) {
    102      1.1  jmcneill 			delay((len & 0x7fff) * 1000);
    103      1.1  jmcneill 			continue;
    104      1.1  jmcneill 		}
    105      1.1  jmcneill 
    106      1.1  jmcneill 		if (i + len >= fwlen)
    107      1.1  jmcneill 			break;
    108      1.1  jmcneill 
    109      1.1  jmcneill 		cmd[0] = fw[i];
    110      1.1  jmcneill 		cmd[1] = fw[i + 1];
    111      1.1  jmcneill 		p = &fw[i + 2];
    112      1.1  jmcneill 		rem = len - 2;
    113      1.1  jmcneill 		while (rem > 0) {
    114  1.8.2.1  christos 			wrlen = uimin(rem, __arraycount(cmd) - 2);
    115      1.1  jmcneill 			memcpy(&cmd[2], p, wrlen);
    116      1.1  jmcneill 			error = xc5k_write_buffer(xc, cmd, wrlen + 2);
    117      1.1  jmcneill 			if (error)
    118      1.1  jmcneill 				return error;
    119      1.1  jmcneill 			p += wrlen;
    120      1.1  jmcneill 			rem -= wrlen;
    121      1.1  jmcneill 		}
    122      1.1  jmcneill 		i += len;
    123      1.1  jmcneill 	}
    124      1.1  jmcneill 
    125      1.1  jmcneill 	return 0;
    126      1.1  jmcneill }
    127      1.1  jmcneill 
    128      1.1  jmcneill static int
    129      1.1  jmcneill xc5k_firmware_open(struct xc5k *xc)
    130      1.1  jmcneill {
    131      1.1  jmcneill 	firmware_handle_t fwh;
    132      1.3  jmcneill 	uint16_t product_id, xcversion, xcbuild;
    133      1.1  jmcneill 	uint8_t *fw = NULL;
    134      1.1  jmcneill 	size_t fwlen;
    135      1.1  jmcneill 	int error;
    136      1.1  jmcneill 
    137      1.1  jmcneill 	mutex_enter(&xc5k_firmware_lock);
    138      1.1  jmcneill 
    139      1.1  jmcneill 	error = xc5k_read_2(xc, XC5K_REG_PRODUCT_ID, &product_id);
    140      1.1  jmcneill 	if (error || product_id != XC5K_PRODUCT_ID_NOFW)
    141      1.1  jmcneill 		goto done;
    142      1.1  jmcneill 
    143      1.1  jmcneill 	error = firmware_open(XC5K_FIRMWARE_DRVNAME,
    144      1.1  jmcneill 	    XC5K_FIRMWARE_IMGNAME, &fwh);
    145      1.1  jmcneill 	if (error)
    146      1.1  jmcneill 		goto done;
    147      1.1  jmcneill 	fwlen = firmware_get_size(fwh);
    148      1.1  jmcneill 	fw = firmware_malloc(fwlen);
    149      1.1  jmcneill 	if (fw == NULL) {
    150      1.1  jmcneill 		firmware_close(fwh);
    151      1.1  jmcneill 		error = ENOMEM;
    152      1.1  jmcneill 		goto done;
    153      1.1  jmcneill 	}
    154      1.1  jmcneill 	error = firmware_read(fwh, 0, fw, fwlen);
    155      1.1  jmcneill 	firmware_close(fwh);
    156      1.1  jmcneill 	if (error)
    157      1.1  jmcneill 		goto done;
    158      1.1  jmcneill 
    159      1.1  jmcneill 	aprint_normal_dev(xc->parent, "xc5k: loading firmware '%s/%s'\n",
    160      1.1  jmcneill 	    XC5K_FIRMWARE_DRVNAME, XC5K_FIRMWARE_IMGNAME);
    161      1.8  christos 
    162      1.1  jmcneill 	error = xc5k_firmware_upload(xc, fw, fwlen);
    163      1.8  christos 	if (error)
    164      1.8  christos 		goto done;
    165      1.8  christos 
    166      1.8  christos 	error = xc5k_read_2(xc, XC5K_REG_VERSION, &xcversion);
    167      1.8  christos 	if (error) {
    168      1.8  christos 		error = 0;
    169      1.8  christos 		goto done;
    170      1.8  christos 	}
    171      1.8  christos 
    172      1.8  christos 	error = xc5k_read_2(xc, XC5K_REG_BUILD, &xcbuild);
    173      1.8  christos 	if (error) {
    174      1.8  christos 		error = 0;
    175      1.8  christos 		xcbuild = 0;
    176      1.3  jmcneill 	}
    177      1.1  jmcneill 
    178      1.8  christos 	aprint_normal_dev(xc->parent, "xc5k: hw %d.%d, fw %d.%d.%d\n",
    179      1.8  christos 	    (xcversion >> 12) & 0xf, (xcversion >> 8) & 0xf,
    180      1.8  christos 	    (xcversion >> 4) & 0xf, xcversion & 0xf, xcbuild);
    181      1.8  christos 
    182      1.1  jmcneill done:
    183      1.1  jmcneill 	if (fw)
    184      1.5     ozaki 		firmware_free(fw, fwlen);
    185      1.1  jmcneill 	mutex_exit(&xc5k_firmware_lock);
    186      1.1  jmcneill 
    187      1.1  jmcneill 	if (error)
    188      1.1  jmcneill 		aprint_error_dev(xc->parent,
    189      1.1  jmcneill 		    "xc5k: couldn't open firmware '%s/%s' (error=%d)\n",
    190      1.1  jmcneill 		    XC5K_FIRMWARE_DRVNAME, XC5K_FIRMWARE_IMGNAME, error);
    191      1.1  jmcneill 
    192      1.1  jmcneill 	return error;
    193      1.1  jmcneill }
    194      1.1  jmcneill 
    195      1.1  jmcneill static int
    196      1.1  jmcneill xc5k_read_2(struct xc5k *xc, uint16_t reg, uint16_t *val)
    197      1.1  jmcneill {
    198      1.1  jmcneill 	uint8_t cmd[2], resp[2];
    199      1.1  jmcneill 	int error;
    200      1.1  jmcneill 
    201      1.1  jmcneill 	cmd[0] = reg >> 8;
    202      1.1  jmcneill 	cmd[1] = reg & 0xff;
    203      1.1  jmcneill 	error = iic_exec(xc->i2c, I2C_OP_WRITE, xc->i2c_addr,
    204      1.1  jmcneill 	    cmd, sizeof(cmd), NULL, 0, 0);
    205      1.1  jmcneill 	if (error)
    206      1.1  jmcneill 		return error;
    207      1.1  jmcneill 	resp[0] = resp[1] = 0;
    208      1.1  jmcneill 	error = iic_exec(xc->i2c, I2C_OP_READ, xc->i2c_addr,
    209      1.1  jmcneill 	    NULL, 0, resp, sizeof(resp), 0);
    210      1.1  jmcneill 	if (error)
    211      1.1  jmcneill 		return error;
    212      1.1  jmcneill 
    213      1.1  jmcneill 	*val = (resp[0] << 8) | resp[1];
    214      1.1  jmcneill 
    215      1.1  jmcneill 	return 0;
    216      1.1  jmcneill }
    217      1.1  jmcneill 
    218      1.1  jmcneill static int
    219      1.1  jmcneill xc5k_write_buffer(struct xc5k *xc, const uint8_t *data, size_t datalen)
    220      1.1  jmcneill {
    221      1.1  jmcneill 	return iic_exec(xc->i2c, I2C_OP_WRITE, xc->i2c_addr,
    222      1.1  jmcneill 	    data, datalen, NULL, 0, 0);
    223      1.1  jmcneill }
    224      1.1  jmcneill 
    225      1.1  jmcneill static int
    226      1.1  jmcneill xc5k_write_2(struct xc5k *xc, uint16_t reg, uint16_t val)
    227      1.1  jmcneill {
    228      1.1  jmcneill 	uint8_t data[4];
    229      1.1  jmcneill 	uint16_t busy;
    230      1.1  jmcneill 	int error, retry;
    231      1.1  jmcneill 
    232      1.1  jmcneill 	data[0] = reg >> 8;
    233      1.1  jmcneill 	data[1] = reg & 0xff;
    234      1.1  jmcneill 	data[2] = val >> 8;
    235      1.1  jmcneill 	data[3] = val & 0xff;
    236      1.1  jmcneill 	error = xc5k_write_buffer(xc, data, sizeof(data));
    237      1.1  jmcneill 	if (error)
    238      1.1  jmcneill 		return error;
    239      1.1  jmcneill 
    240      1.1  jmcneill 	retry = 1000;
    241      1.1  jmcneill 	while (--retry > 0) {
    242      1.1  jmcneill 		error = xc5k_read_2(xc, XC5K_REG_BUSY, &busy);
    243      1.1  jmcneill 		if (error || !busy)
    244      1.1  jmcneill 			break;
    245      1.1  jmcneill 		delay(5000);
    246      1.1  jmcneill 	}
    247      1.1  jmcneill 
    248      1.1  jmcneill 	return error;
    249      1.1  jmcneill }
    250      1.1  jmcneill 
    251      1.1  jmcneill struct xc5k *
    252      1.1  jmcneill xc5k_open(device_t parent, i2c_tag_t i2c, i2c_addr_t addr,
    253      1.3  jmcneill     xc5k_reset_cb reset, void *reset_priv, unsigned int if_freq,
    254      1.3  jmcneill     fe_type_t fe_type)
    255      1.1  jmcneill {
    256      1.1  jmcneill 	struct xc5k *xc;
    257      1.1  jmcneill 	uint16_t product_id;
    258      1.1  jmcneill 
    259      1.1  jmcneill 	xc = kmem_alloc(sizeof(*xc), KM_SLEEP);
    260      1.1  jmcneill 	xc->parent = parent;
    261      1.1  jmcneill 	xc->i2c = i2c;
    262      1.1  jmcneill 	xc->i2c_addr = addr;
    263      1.1  jmcneill 	xc->reset = reset;
    264      1.1  jmcneill 	xc->reset_priv = reset_priv;
    265      1.3  jmcneill 	xc->if_freq = if_freq;
    266      1.3  jmcneill 	xc->fe_type = fe_type;
    267      1.1  jmcneill 
    268      1.1  jmcneill 	if (xc5k_read_2(xc, XC5K_REG_PRODUCT_ID, &product_id))
    269      1.1  jmcneill 		goto failed;
    270      1.1  jmcneill 
    271      1.1  jmcneill 	aprint_debug_dev(parent, "xc5k: product=0x%04x\n", product_id);
    272      1.1  jmcneill 
    273      1.1  jmcneill 	if (product_id != XC5K_PRODUCT_ID_NOFW && product_id != XC5K_PRODUCT_ID)
    274      1.1  jmcneill 		goto failed;
    275      1.1  jmcneill 
    276      1.1  jmcneill 	if (xc5k_firmware_open(xc))
    277      1.1  jmcneill 		goto failed;
    278      1.1  jmcneill 	if (xc5k_write_2(xc, XC5K_REG_INIT, 0))
    279      1.1  jmcneill 		goto failed;
    280      1.1  jmcneill 	delay(100000);
    281      1.1  jmcneill 
    282      1.1  jmcneill 	if (xc5k_read_2(xc, XC5K_REG_PRODUCT_ID, &product_id))
    283      1.1  jmcneill 		goto failed;
    284      1.1  jmcneill 
    285      1.1  jmcneill 	aprint_debug_dev(parent, "xc5k: product=0x%04x\n", product_id);
    286      1.1  jmcneill 
    287      1.1  jmcneill 	return xc;
    288      1.1  jmcneill 
    289      1.1  jmcneill failed:
    290      1.1  jmcneill 	kmem_free(xc, sizeof(*xc));
    291      1.1  jmcneill 	return NULL;
    292      1.1  jmcneill }
    293      1.1  jmcneill 
    294      1.1  jmcneill void
    295      1.1  jmcneill xc5k_close(struct xc5k *xc)
    296      1.1  jmcneill {
    297      1.1  jmcneill 	kmem_free(xc, sizeof(*xc));
    298      1.1  jmcneill }
    299      1.1  jmcneill 
    300      1.1  jmcneill int
    301      1.3  jmcneill xc5k_tune_video(struct xc5k *xc, struct xc5k_params *params)
    302      1.1  jmcneill {
    303      1.1  jmcneill 	uint16_t amode, vmode;
    304      1.1  jmcneill 	uint16_t lock, freq;
    305      1.1  jmcneill 	int retry;
    306      1.1  jmcneill 
    307      1.1  jmcneill 	switch (params->standard) {
    308      1.1  jmcneill 	case VIDEO_STANDARD_NTSC_M:
    309      1.1  jmcneill 	case VIDEO_STANDARD_NTSC_M_JP:
    310      1.1  jmcneill 	case VIDEO_STANDARD_NTSC_M_KR:
    311      1.1  jmcneill 		amode = XC5K_AUDIO_MODE_BTSC;
    312      1.1  jmcneill 		vmode = XC5K_VIDEO_MODE_BTSC;
    313      1.1  jmcneill 		break;
    314      1.1  jmcneill 	default:
    315      1.1  jmcneill 		return EINVAL;
    316      1.1  jmcneill 	}
    317      1.1  jmcneill 
    318      1.1  jmcneill 	if (xc5k_write_2(xc, XC5K_REG_SIGNAL_SOURCE, params->signal_source))
    319      1.1  jmcneill 		return EIO;
    320      1.1  jmcneill 	if (xc5k_write_2(xc, XC5K_REG_VIDEO_MODE, vmode))
    321      1.1  jmcneill 		return EIO;
    322      1.1  jmcneill 	if (xc5k_write_2(xc, XC5K_REG_AUDIO_MODE, amode))
    323      1.1  jmcneill 		return EIO;
    324      1.3  jmcneill 	if (xc5k_write_2(xc, XC5K_REG_OUTAMP, XC5K_OUTAMP_ANALOG))
    325      1.3  jmcneill 		return EIO;
    326      1.1  jmcneill 	freq = (params->frequency * 62500) / 15625;
    327      1.1  jmcneill #ifdef XC5K_DEBUG
    328      1.3  jmcneill 	printf("xc5k_tune_video: frequency=%u (%u Hz)\n", params->frequency,
    329      1.1  jmcneill 	    params->frequency * 62500);
    330      1.1  jmcneill 	printf("           freq=%u\n", freq);
    331      1.1  jmcneill #endif
    332      1.1  jmcneill 	if (xc5k_write_2(xc, XC5K_REG_FINER_FREQ, freq))
    333      1.1  jmcneill 		return EIO;
    334      1.1  jmcneill 
    335      1.1  jmcneill 	retry = 100;
    336      1.1  jmcneill 	while (--retry > 0) {
    337      1.1  jmcneill 		if (xc5k_read_2(xc, XC5K_REG_LOCK, &lock))
    338      1.1  jmcneill 			return EIO;
    339      1.1  jmcneill #ifdef XC5K_DEBUG
    340      1.3  jmcneill 		printf("xc5k_tune_video: lock=0x%04x\n", lock);
    341      1.1  jmcneill #endif
    342      1.1  jmcneill 		if (lock == 1)
    343      1.1  jmcneill 			break;
    344      1.1  jmcneill 		delay(5000);
    345      1.1  jmcneill 	}
    346      1.1  jmcneill 
    347      1.1  jmcneill 	return 0;
    348      1.1  jmcneill }
    349      1.1  jmcneill 
    350      1.3  jmcneill int
    351      1.3  jmcneill xc5k_tune_dtv(struct xc5k *xc, const struct dvb_frontend_parameters *params)
    352      1.3  jmcneill {
    353      1.3  jmcneill 	uint16_t amode, vmode;
    354      1.3  jmcneill 	uint32_t freq, ifout;
    355      1.3  jmcneill 	int signal_source;
    356      1.3  jmcneill 	fe_modulation_t modulation;
    357      1.3  jmcneill 
    358      1.3  jmcneill 	if (xc->fe_type == FE_ATSC)
    359      1.3  jmcneill 		modulation = params->u.vsb.modulation;
    360      1.3  jmcneill 	else if (xc->fe_type == FE_QAM)
    361      1.3  jmcneill 		modulation = params->u.qam.modulation;
    362      1.3  jmcneill 	else
    363      1.3  jmcneill 		return EINVAL;
    364      1.3  jmcneill 
    365      1.3  jmcneill 	switch (modulation) {
    366      1.3  jmcneill 	case VSB_8:
    367      1.3  jmcneill 	case VSB_16:
    368      1.3  jmcneill 		signal_source = XC5K_SIGNAL_SOURCE_AIR;
    369      1.3  jmcneill 		switch (xc->fe_type) {
    370      1.3  jmcneill 		case FE_ATSC:
    371      1.3  jmcneill 			amode = XC5K_AUDIO_MODE_DTV6;
    372      1.3  jmcneill 			vmode = XC5K_VIDEO_MODE_DTV6;
    373      1.3  jmcneill 			freq = params->frequency - 1750000;
    374      1.3  jmcneill 			break;
    375      1.3  jmcneill 		default:
    376      1.3  jmcneill 			return EINVAL;
    377      1.3  jmcneill 		}
    378      1.3  jmcneill 		break;
    379      1.3  jmcneill 	case QAM_16:
    380      1.3  jmcneill 	case QAM_32:
    381      1.3  jmcneill 	case QAM_64:
    382      1.3  jmcneill 	case QAM_128:
    383      1.3  jmcneill 	case QAM_256:
    384      1.3  jmcneill 		signal_source = XC5K_SIGNAL_SOURCE_CABLE;
    385      1.3  jmcneill 		switch (xc->fe_type) {
    386      1.3  jmcneill 		case FE_ATSC:
    387      1.3  jmcneill 			amode = XC5K_AUDIO_MODE_DTV6;
    388      1.3  jmcneill 			vmode = XC5K_VIDEO_MODE_DTV6;
    389      1.3  jmcneill 			freq = params->frequency - 1750000;
    390      1.3  jmcneill 			break;
    391      1.3  jmcneill 		case FE_QAM:
    392      1.3  jmcneill 			amode = XC5K_AUDIO_MODE_DTV78;
    393      1.3  jmcneill 			vmode = XC5K_VIDEO_MODE_DTV78;
    394      1.3  jmcneill 			freq = params->frequency - 2750000;
    395      1.3  jmcneill 			break;
    396      1.3  jmcneill 		default:
    397      1.3  jmcneill 			return EINVAL;
    398      1.3  jmcneill 		}
    399      1.3  jmcneill 		break;
    400      1.3  jmcneill 	default:
    401      1.3  jmcneill 		return EINVAL;
    402      1.3  jmcneill 	}
    403      1.3  jmcneill 
    404      1.3  jmcneill 	if (freq > XC5K_FREQ_MAX || freq < XC5K_FREQ_MIN)
    405      1.3  jmcneill 		return ERANGE;
    406      1.3  jmcneill 
    407      1.3  jmcneill 	if (xc5k_write_2(xc, XC5K_REG_SIGNAL_SOURCE, signal_source))
    408      1.3  jmcneill 		return EIO;
    409      1.3  jmcneill 	if (xc5k_write_2(xc, XC5K_REG_VIDEO_MODE, vmode))
    410      1.3  jmcneill 		return EIO;
    411      1.3  jmcneill 	if (xc5k_write_2(xc, XC5K_REG_AUDIO_MODE, amode))
    412      1.3  jmcneill 		return EIO;
    413      1.3  jmcneill 	ifout = ((xc->if_freq / 1000) * 1024) / 1000;
    414      1.3  jmcneill 	if (xc5k_write_2(xc, XC5K_REG_IF_OUT, ifout))
    415      1.3  jmcneill 		return EIO;
    416      1.3  jmcneill 	if (xc5k_write_2(xc, XC5K_REG_OUTAMP, XC5K_OUTAMP_DIGITAL))
    417      1.3  jmcneill 		return EIO;
    418      1.3  jmcneill 	freq = (uint16_t)(freq / 15625);
    419      1.3  jmcneill 	if (xc5k_write_2(xc, XC5K_REG_FINER_FREQ, freq))
    420      1.3  jmcneill 		return EIO;
    421      1.3  jmcneill 
    422      1.3  jmcneill 	return 0;
    423      1.3  jmcneill }
    424      1.3  jmcneill 
    425      1.3  jmcneill fe_status_t
    426      1.3  jmcneill xc5k_get_status(struct xc5k *xc)
    427      1.3  jmcneill {
    428      1.3  jmcneill 	uint16_t lock_status;
    429      1.3  jmcneill 	fe_status_t festatus = 0;
    430      1.3  jmcneill 
    431      1.3  jmcneill 	if (xc5k_read_2(xc, XC5K_REG_LOCK, &lock_status))
    432      1.3  jmcneill 		return 0;
    433      1.3  jmcneill 	if (lock_status & XC5K_LOCK_LOCKED) {
    434      1.3  jmcneill 		festatus |= FE_HAS_LOCK;
    435      1.3  jmcneill 		if ((lock_status & XC5K_LOCK_NOSIGNAL) == 0)
    436      1.3  jmcneill 			festatus |= FE_HAS_SIGNAL;
    437      1.3  jmcneill 	}
    438      1.3  jmcneill 
    439      1.3  jmcneill 	return festatus;
    440      1.3  jmcneill }
    441      1.3  jmcneill 
    442      1.6  jmcneill MODULE(MODULE_CLASS_DRIVER, xc5k, "i2cexec");
    443      1.1  jmcneill 
    444      1.1  jmcneill static int
    445      1.1  jmcneill xc5k_modcmd(modcmd_t cmd, void *opaque)
    446      1.1  jmcneill {
    447      1.1  jmcneill 	switch (cmd) {
    448      1.1  jmcneill 	case MODULE_CMD_INIT:
    449      1.3  jmcneill 		mutex_init(&xc5k_firmware_lock, MUTEX_DEFAULT, IPL_NONE);
    450      1.1  jmcneill 		return 0;
    451      1.1  jmcneill 	case MODULE_CMD_FINI:
    452      1.1  jmcneill 		mutex_destroy(&xc5k_firmware_lock);
    453      1.1  jmcneill 		return 0;
    454      1.1  jmcneill 	default:
    455      1.1  jmcneill 		return ENOTTY;
    456      1.1  jmcneill 	}
    457      1.1  jmcneill }
    458