Home | History | Annotate | Line # | Download | only in i2c
xc5k.c revision 1.2.6.2
      1  1.2.6.2  rmind /* $NetBSD: xc5k.c,v 1.2.6.2 2011/03/05 20:53:11 rmind Exp $ */
      2  1.2.6.2  rmind 
      3  1.2.6.2  rmind /*-
      4  1.2.6.2  rmind  * Copyright (c) 2010 Jared D. McNeill <jmcneill (at) invisible.ca>
      5  1.2.6.2  rmind  * All rights reserved.
      6  1.2.6.2  rmind  *
      7  1.2.6.2  rmind  * Redistribution and use in source and binary forms, with or without
      8  1.2.6.2  rmind  * modification, are permitted provided that the following conditions
      9  1.2.6.2  rmind  * are met:
     10  1.2.6.2  rmind  * 1. Redistributions of source code must retain the above copyright
     11  1.2.6.2  rmind  *    notice, this list of conditions and the following disclaimer.
     12  1.2.6.2  rmind  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.2.6.2  rmind  *    notice, this list of conditions and the following disclaimer in the
     14  1.2.6.2  rmind  *    documentation and/or other materials provided with the distribution.
     15  1.2.6.2  rmind  *
     16  1.2.6.2  rmind  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  1.2.6.2  rmind  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.2.6.2  rmind  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.2.6.2  rmind  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  1.2.6.2  rmind  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.2.6.2  rmind  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.2.6.2  rmind  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.2.6.2  rmind  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.2.6.2  rmind  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.2.6.2  rmind  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.2.6.2  rmind  * POSSIBILITY OF SUCH DAMAGE.
     27  1.2.6.2  rmind  */
     28  1.2.6.2  rmind 
     29  1.2.6.2  rmind /*
     30  1.2.6.2  rmind  * Xceive XC5000
     31  1.2.6.2  rmind  */
     32  1.2.6.2  rmind 
     33  1.2.6.2  rmind #include <sys/cdefs.h>
     34  1.2.6.2  rmind __KERNEL_RCSID(0, "$NetBSD: xc5k.c,v 1.2.6.2 2011/03/05 20:53:11 rmind Exp $");
     35  1.2.6.2  rmind 
     36  1.2.6.2  rmind #include <sys/param.h>
     37  1.2.6.2  rmind #include <sys/systm.h>
     38  1.2.6.2  rmind #include <sys/device.h>
     39  1.2.6.2  rmind #include <sys/conf.h>
     40  1.2.6.2  rmind #include <sys/bus.h>
     41  1.2.6.2  rmind #include <sys/kmem.h>
     42  1.2.6.2  rmind #include <sys/mutex.h>
     43  1.2.6.2  rmind #include <sys/module.h>
     44  1.2.6.2  rmind 
     45  1.2.6.2  rmind #include <dev/firmload.h>
     46  1.2.6.2  rmind #include <dev/i2c/i2cvar.h>
     47  1.2.6.2  rmind 
     48  1.2.6.2  rmind #include <dev/i2c/xc5kreg.h>
     49  1.2.6.2  rmind #include <dev/i2c/xc5kvar.h>
     50  1.2.6.2  rmind 
     51  1.2.6.2  rmind #define	XC5K_FIRMWARE_DRVNAME	"xc5k"
     52  1.2.6.2  rmind #define	XC5K_FIRMWARE_IMGNAME	"dvb-fe-xc5000-1.6.114.fw"
     53  1.2.6.2  rmind 
     54  1.2.6.2  rmind static kmutex_t xc5k_firmware_lock;
     55  1.2.6.2  rmind 
     56  1.2.6.2  rmind static int	xc5k_reset(struct xc5k *);
     57  1.2.6.2  rmind static int	xc5k_read_2(struct xc5k *, uint16_t, uint16_t *);
     58  1.2.6.2  rmind static int	xc5k_write_buffer(struct xc5k *, const uint8_t *, size_t);
     59  1.2.6.2  rmind static int	xc5k_firmware_open(struct xc5k *);
     60  1.2.6.2  rmind static int	xc5k_firmware_upload(struct xc5k *, const uint8_t *, size_t);
     61  1.2.6.2  rmind 
     62  1.2.6.2  rmind static int
     63  1.2.6.2  rmind xc5k_reset(struct xc5k *xc)
     64  1.2.6.2  rmind {
     65  1.2.6.2  rmind 	int error = 0;
     66  1.2.6.2  rmind 
     67  1.2.6.2  rmind 	if (xc->reset)
     68  1.2.6.2  rmind 		error = xc->reset(xc->reset_priv);
     69  1.2.6.2  rmind 
     70  1.2.6.2  rmind 	return error;
     71  1.2.6.2  rmind }
     72  1.2.6.2  rmind 
     73  1.2.6.2  rmind static int
     74  1.2.6.2  rmind xc5k_firmware_upload(struct xc5k *xc, const uint8_t *fw, size_t fwlen)
     75  1.2.6.2  rmind {
     76  1.2.6.2  rmind 	const uint8_t *p;
     77  1.2.6.2  rmind 	uint8_t cmd[64];
     78  1.2.6.2  rmind 	unsigned int i;
     79  1.2.6.2  rmind 	uint16_t len, rem;
     80  1.2.6.2  rmind 	size_t wrlen;
     81  1.2.6.2  rmind 	int error;
     82  1.2.6.2  rmind 
     83  1.2.6.2  rmind 	for (i = 0; i < fwlen - 1;) {
     84  1.2.6.2  rmind 		len = (fw[i] << 8) | fw[i + 1];
     85  1.2.6.2  rmind 		i += 2;
     86  1.2.6.2  rmind 		if (len == 0xffff)
     87  1.2.6.2  rmind 			break;
     88  1.2.6.2  rmind 
     89  1.2.6.2  rmind 		/* reset command */
     90  1.2.6.2  rmind 		if (len == 0x0000) {
     91  1.2.6.2  rmind 			error = xc5k_reset(xc);
     92  1.2.6.2  rmind 			if (error)
     93  1.2.6.2  rmind 				return error;
     94  1.2.6.2  rmind 			continue;
     95  1.2.6.2  rmind 		}
     96  1.2.6.2  rmind 
     97  1.2.6.2  rmind 		/* delay command */
     98  1.2.6.2  rmind 		if (len & 0x8000) {
     99  1.2.6.2  rmind 			delay((len & 0x7fff) * 1000);
    100  1.2.6.2  rmind 			continue;
    101  1.2.6.2  rmind 		}
    102  1.2.6.2  rmind 
    103  1.2.6.2  rmind 		if (i + len >= fwlen)
    104  1.2.6.2  rmind 			break;
    105  1.2.6.2  rmind 
    106  1.2.6.2  rmind 		cmd[0] = fw[i];
    107  1.2.6.2  rmind 		cmd[1] = fw[i + 1];
    108  1.2.6.2  rmind 		p = &fw[i + 2];
    109  1.2.6.2  rmind 		rem = len - 2;
    110  1.2.6.2  rmind 		while (rem > 0) {
    111  1.2.6.2  rmind 			wrlen = min(rem, __arraycount(cmd) - 2);
    112  1.2.6.2  rmind 			memcpy(&cmd[2], p, wrlen);
    113  1.2.6.2  rmind 			error = xc5k_write_buffer(xc, cmd, wrlen + 2);
    114  1.2.6.2  rmind 			if (error)
    115  1.2.6.2  rmind 				return error;
    116  1.2.6.2  rmind 			p += wrlen;
    117  1.2.6.2  rmind 			rem -= wrlen;
    118  1.2.6.2  rmind 		}
    119  1.2.6.2  rmind 		i += len;
    120  1.2.6.2  rmind 	}
    121  1.2.6.2  rmind 
    122  1.2.6.2  rmind 	return 0;
    123  1.2.6.2  rmind }
    124  1.2.6.2  rmind 
    125  1.2.6.2  rmind static int
    126  1.2.6.2  rmind xc5k_firmware_open(struct xc5k *xc)
    127  1.2.6.2  rmind {
    128  1.2.6.2  rmind 	firmware_handle_t fwh;
    129  1.2.6.2  rmind 	uint16_t product_id;
    130  1.2.6.2  rmind 	uint8_t *fw = NULL;
    131  1.2.6.2  rmind 	size_t fwlen;
    132  1.2.6.2  rmind 	int error;
    133  1.2.6.2  rmind 
    134  1.2.6.2  rmind 	mutex_enter(&xc5k_firmware_lock);
    135  1.2.6.2  rmind 
    136  1.2.6.2  rmind 	error = xc5k_read_2(xc, XC5K_REG_PRODUCT_ID, &product_id);
    137  1.2.6.2  rmind 	if (error || product_id != XC5K_PRODUCT_ID_NOFW)
    138  1.2.6.2  rmind 		goto done;
    139  1.2.6.2  rmind 
    140  1.2.6.2  rmind 	error = firmware_open(XC5K_FIRMWARE_DRVNAME,
    141  1.2.6.2  rmind 	    XC5K_FIRMWARE_IMGNAME, &fwh);
    142  1.2.6.2  rmind 	if (error)
    143  1.2.6.2  rmind 		goto done;
    144  1.2.6.2  rmind 	fwlen = firmware_get_size(fwh);
    145  1.2.6.2  rmind 	fw = firmware_malloc(fwlen);
    146  1.2.6.2  rmind 	if (fw == NULL) {
    147  1.2.6.2  rmind 		firmware_close(fwh);
    148  1.2.6.2  rmind 		error = ENOMEM;
    149  1.2.6.2  rmind 		goto done;
    150  1.2.6.2  rmind 	}
    151  1.2.6.2  rmind 	error = firmware_read(fwh, 0, fw, fwlen);
    152  1.2.6.2  rmind 	firmware_close(fwh);
    153  1.2.6.2  rmind 	if (error)
    154  1.2.6.2  rmind 		goto done;
    155  1.2.6.2  rmind 
    156  1.2.6.2  rmind 	aprint_normal_dev(xc->parent, "xc5k: loading firmware '%s/%s'\n",
    157  1.2.6.2  rmind 	    XC5K_FIRMWARE_DRVNAME, XC5K_FIRMWARE_IMGNAME);
    158  1.2.6.2  rmind 	error = xc5k_firmware_upload(xc, fw, fwlen);
    159  1.2.6.2  rmind 
    160  1.2.6.2  rmind done:
    161  1.2.6.2  rmind 	if (fw)
    162  1.2.6.2  rmind 		firmware_free(fw, 0);
    163  1.2.6.2  rmind 	mutex_exit(&xc5k_firmware_lock);
    164  1.2.6.2  rmind 
    165  1.2.6.2  rmind 	if (error)
    166  1.2.6.2  rmind 		aprint_error_dev(xc->parent,
    167  1.2.6.2  rmind 		    "xc5k: couldn't open firmware '%s/%s' (error=%d)\n",
    168  1.2.6.2  rmind 		    XC5K_FIRMWARE_DRVNAME, XC5K_FIRMWARE_IMGNAME, error);
    169  1.2.6.2  rmind 
    170  1.2.6.2  rmind 	return error;
    171  1.2.6.2  rmind }
    172  1.2.6.2  rmind 
    173  1.2.6.2  rmind static int
    174  1.2.6.2  rmind xc5k_read_2(struct xc5k *xc, uint16_t reg, uint16_t *val)
    175  1.2.6.2  rmind {
    176  1.2.6.2  rmind 	uint8_t cmd[2], resp[2];
    177  1.2.6.2  rmind 	int error;
    178  1.2.6.2  rmind 
    179  1.2.6.2  rmind 	cmd[0] = reg >> 8;
    180  1.2.6.2  rmind 	cmd[1] = reg & 0xff;
    181  1.2.6.2  rmind 	error = iic_exec(xc->i2c, I2C_OP_WRITE, xc->i2c_addr,
    182  1.2.6.2  rmind 	    cmd, sizeof(cmd), NULL, 0, 0);
    183  1.2.6.2  rmind 	if (error)
    184  1.2.6.2  rmind 		return error;
    185  1.2.6.2  rmind 	resp[0] = resp[1] = 0;
    186  1.2.6.2  rmind 	error = iic_exec(xc->i2c, I2C_OP_READ, xc->i2c_addr,
    187  1.2.6.2  rmind 	    NULL, 0, resp, sizeof(resp), 0);
    188  1.2.6.2  rmind 	if (error)
    189  1.2.6.2  rmind 		return error;
    190  1.2.6.2  rmind 
    191  1.2.6.2  rmind 	*val = (resp[0] << 8) | resp[1];
    192  1.2.6.2  rmind 
    193  1.2.6.2  rmind 	return 0;
    194  1.2.6.2  rmind }
    195  1.2.6.2  rmind 
    196  1.2.6.2  rmind static int
    197  1.2.6.2  rmind xc5k_write_buffer(struct xc5k *xc, const uint8_t *data, size_t datalen)
    198  1.2.6.2  rmind {
    199  1.2.6.2  rmind 	return iic_exec(xc->i2c, I2C_OP_WRITE, xc->i2c_addr,
    200  1.2.6.2  rmind 	    data, datalen, NULL, 0, 0);
    201  1.2.6.2  rmind }
    202  1.2.6.2  rmind 
    203  1.2.6.2  rmind static int
    204  1.2.6.2  rmind xc5k_write_2(struct xc5k *xc, uint16_t reg, uint16_t val)
    205  1.2.6.2  rmind {
    206  1.2.6.2  rmind 	uint8_t data[4];
    207  1.2.6.2  rmind 	uint16_t busy;
    208  1.2.6.2  rmind 	int error, retry;
    209  1.2.6.2  rmind 
    210  1.2.6.2  rmind 	data[0] = reg >> 8;
    211  1.2.6.2  rmind 	data[1] = reg & 0xff;
    212  1.2.6.2  rmind 	data[2] = val >> 8;
    213  1.2.6.2  rmind 	data[3] = val & 0xff;
    214  1.2.6.2  rmind 	error = xc5k_write_buffer(xc, data, sizeof(data));
    215  1.2.6.2  rmind 	if (error)
    216  1.2.6.2  rmind 		return error;
    217  1.2.6.2  rmind 
    218  1.2.6.2  rmind 	retry = 1000;
    219  1.2.6.2  rmind 	while (--retry > 0) {
    220  1.2.6.2  rmind 		error = xc5k_read_2(xc, XC5K_REG_BUSY, &busy);
    221  1.2.6.2  rmind 		if (error || !busy)
    222  1.2.6.2  rmind 			break;
    223  1.2.6.2  rmind 		delay(5000);
    224  1.2.6.2  rmind 	}
    225  1.2.6.2  rmind 
    226  1.2.6.2  rmind 	return error;
    227  1.2.6.2  rmind }
    228  1.2.6.2  rmind 
    229  1.2.6.2  rmind struct xc5k *
    230  1.2.6.2  rmind xc5k_open(device_t parent, i2c_tag_t i2c, i2c_addr_t addr,
    231  1.2.6.2  rmind     xc5k_reset_cb reset, void *reset_priv)
    232  1.2.6.2  rmind {
    233  1.2.6.2  rmind 	struct xc5k *xc;
    234  1.2.6.2  rmind 	uint16_t product_id;
    235  1.2.6.2  rmind 
    236  1.2.6.2  rmind 	xc = kmem_alloc(sizeof(*xc), KM_SLEEP);
    237  1.2.6.2  rmind 	if (xc == NULL)
    238  1.2.6.2  rmind 		return NULL;
    239  1.2.6.2  rmind 	xc->parent = parent;
    240  1.2.6.2  rmind 	xc->i2c = i2c;
    241  1.2.6.2  rmind 	xc->i2c_addr = addr;
    242  1.2.6.2  rmind 	xc->reset = reset;
    243  1.2.6.2  rmind 	xc->reset_priv = reset_priv;
    244  1.2.6.2  rmind 
    245  1.2.6.2  rmind 	if (xc5k_read_2(xc, XC5K_REG_PRODUCT_ID, &product_id))
    246  1.2.6.2  rmind 		goto failed;
    247  1.2.6.2  rmind 
    248  1.2.6.2  rmind 	aprint_debug_dev(parent, "xc5k: product=0x%04x\n", product_id);
    249  1.2.6.2  rmind 
    250  1.2.6.2  rmind 	if (product_id != XC5K_PRODUCT_ID_NOFW && product_id != XC5K_PRODUCT_ID)
    251  1.2.6.2  rmind 		goto failed;
    252  1.2.6.2  rmind 
    253  1.2.6.2  rmind 	if (xc5k_firmware_open(xc))
    254  1.2.6.2  rmind 		goto failed;
    255  1.2.6.2  rmind 	if (xc5k_write_2(xc, XC5K_REG_INIT, 0))
    256  1.2.6.2  rmind 		goto failed;
    257  1.2.6.2  rmind 	delay(100000);
    258  1.2.6.2  rmind 
    259  1.2.6.2  rmind 	if (xc5k_read_2(xc, XC5K_REG_PRODUCT_ID, &product_id))
    260  1.2.6.2  rmind 		goto failed;
    261  1.2.6.2  rmind 
    262  1.2.6.2  rmind 	aprint_debug_dev(parent, "xc5k: product=0x%04x\n", product_id);
    263  1.2.6.2  rmind 
    264  1.2.6.2  rmind 	return xc;
    265  1.2.6.2  rmind 
    266  1.2.6.2  rmind failed:
    267  1.2.6.2  rmind 	kmem_free(xc, sizeof(*xc));
    268  1.2.6.2  rmind 	return NULL;
    269  1.2.6.2  rmind }
    270  1.2.6.2  rmind 
    271  1.2.6.2  rmind void
    272  1.2.6.2  rmind xc5k_close(struct xc5k *xc)
    273  1.2.6.2  rmind {
    274  1.2.6.2  rmind 	kmem_free(xc, sizeof(*xc));
    275  1.2.6.2  rmind }
    276  1.2.6.2  rmind 
    277  1.2.6.2  rmind int
    278  1.2.6.2  rmind xc5k_tune(struct xc5k *xc, struct xc5k_params *params)
    279  1.2.6.2  rmind {
    280  1.2.6.2  rmind 	uint16_t amode, vmode;
    281  1.2.6.2  rmind 	uint16_t lock, freq;
    282  1.2.6.2  rmind 	int retry;
    283  1.2.6.2  rmind 
    284  1.2.6.2  rmind 	switch (params->standard) {
    285  1.2.6.2  rmind 	case VIDEO_STANDARD_NTSC_M:
    286  1.2.6.2  rmind 	case VIDEO_STANDARD_NTSC_M_JP:
    287  1.2.6.2  rmind 	case VIDEO_STANDARD_NTSC_M_KR:
    288  1.2.6.2  rmind 		amode = XC5K_AUDIO_MODE_BTSC;
    289  1.2.6.2  rmind 		vmode = XC5K_VIDEO_MODE_BTSC;
    290  1.2.6.2  rmind 		break;
    291  1.2.6.2  rmind 	default:
    292  1.2.6.2  rmind 		return EINVAL;
    293  1.2.6.2  rmind 	}
    294  1.2.6.2  rmind 
    295  1.2.6.2  rmind 	if (xc5k_write_2(xc, XC5K_REG_SIGNAL_SOURCE, params->signal_source))
    296  1.2.6.2  rmind 		return EIO;
    297  1.2.6.2  rmind 	if (xc5k_write_2(xc, XC5K_REG_VIDEO_MODE, vmode))
    298  1.2.6.2  rmind 		return EIO;
    299  1.2.6.2  rmind 	if (xc5k_write_2(xc, XC5K_REG_AUDIO_MODE, amode))
    300  1.2.6.2  rmind 		return EIO;
    301  1.2.6.2  rmind 	freq = (params->frequency * 62500) / 15625;
    302  1.2.6.2  rmind #ifdef XC5K_DEBUG
    303  1.2.6.2  rmind 	printf("xc5k_tune: frequency=%u (%u Hz)\n", params->frequency,
    304  1.2.6.2  rmind 	    params->frequency * 62500);
    305  1.2.6.2  rmind 	printf("           freq=%u\n", freq);
    306  1.2.6.2  rmind #endif
    307  1.2.6.2  rmind 	if (xc5k_write_2(xc, XC5K_REG_FINER_FREQ, freq))
    308  1.2.6.2  rmind 		return EIO;
    309  1.2.6.2  rmind 
    310  1.2.6.2  rmind 	retry = 100;
    311  1.2.6.2  rmind 	while (--retry > 0) {
    312  1.2.6.2  rmind 		if (xc5k_read_2(xc, XC5K_REG_LOCK, &lock))
    313  1.2.6.2  rmind 			return EIO;
    314  1.2.6.2  rmind #ifdef XC5K_DEBUG
    315  1.2.6.2  rmind 		printf("xc5k_tune: lock=0x%04x\n", lock);
    316  1.2.6.2  rmind #endif
    317  1.2.6.2  rmind 		if (lock == 1)
    318  1.2.6.2  rmind 			break;
    319  1.2.6.2  rmind 		delay(5000);
    320  1.2.6.2  rmind 	}
    321  1.2.6.2  rmind 
    322  1.2.6.2  rmind 	return 0;
    323  1.2.6.2  rmind }
    324  1.2.6.2  rmind 
    325  1.2.6.2  rmind MODULE(MODULE_CLASS_DRIVER, xc5k, NULL);
    326  1.2.6.2  rmind 
    327  1.2.6.2  rmind static int
    328  1.2.6.2  rmind xc5k_modcmd(modcmd_t cmd, void *opaque)
    329  1.2.6.2  rmind {
    330  1.2.6.2  rmind 	switch (cmd) {
    331  1.2.6.2  rmind 	case MODULE_CMD_INIT:
    332  1.2.6.2  rmind 		mutex_init(&xc5k_firmware_lock, MUTEX_DEFAULT, IPL_VM);
    333  1.2.6.2  rmind 		return 0;
    334  1.2.6.2  rmind 	case MODULE_CMD_FINI:
    335  1.2.6.2  rmind 		mutex_destroy(&xc5k_firmware_lock);
    336  1.2.6.2  rmind 		return 0;
    337  1.2.6.2  rmind 	default:
    338  1.2.6.2  rmind 		return ENOTTY;
    339  1.2.6.2  rmind 	}
    340  1.2.6.2  rmind }
    341