Home | History | Annotate | Line # | Download | only in i2c
tvpll.c revision 1.2
      1 /* $NetBSD: tvpll.c,v 1.2 2011/07/14 23:45:43 jmcneill Exp $ */
      2 
      3 /*
      4  * Copyright (c) 2008, 2011 Jonathan A. Kollasch
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     17  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
     20  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     21  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     22  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     23  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     24  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     25  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     26  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: tvpll.c,v 1.2 2011/07/14 23:45:43 jmcneill Exp $");
     31 
     32 #include <sys/param.h>
     33 #include <sys/systm.h>
     34 #include <sys/device.h>
     35 #include <sys/kmem.h>
     36 #include <sys/syslog.h>
     37 #include <sys/module.h>
     38 
     39 #include <dev/dtv/dtvio.h>
     40 #include <dev/i2c/i2cvar.h>
     41 #include <dev/i2c/tvpllvar.h>
     42 
     43 struct tvpll {
     44 	device_t		parent;
     45 	i2c_tag_t		tag;
     46 	i2c_addr_t		addr;
     47 	const struct tvpll_data *	pll;
     48 	uint32_t		frequency;
     49 };
     50 
     51 static uint32_t tvpll_algo(struct tvpll *, uint8_t *,
     52 		const struct dvb_frontend_parameters *, uint32_t *);
     53 
     54 struct tvpll *
     55 tvpll_open(device_t parent, i2c_tag_t t, i2c_addr_t a, struct tvpll_data *p)
     56 {
     57 	struct tvpll *tvpll;
     58 
     59 	tvpll = kmem_alloc(sizeof(struct tvpll), KM_NOSLEEP);
     60         if (tvpll == NULL)
     61                 return NULL;
     62 
     63 	tvpll->tag = t;
     64 	tvpll->addr = a;
     65 
     66 	tvpll->pll = p;
     67 
     68 	return tvpll;
     69 }
     70 
     71 void
     72 tvpll_close(struct tvpll *tvpll)
     73 {
     74 	kmem_free(tvpll, sizeof(*tvpll));
     75 }
     76 
     77 static uint32_t
     78 tvpll_algo(struct tvpll *tvpll, uint8_t *b,
     79     const struct dvb_frontend_parameters *p, uint32_t *fr)
     80 {
     81 	const struct tvpll_data *pll;
     82 	uint32_t d;
     83 	int i;
     84 
     85 	pll = tvpll->pll;
     86 
     87 	if(p->frequency != 0 &&
     88 	    (p->frequency < pll->min || p->frequency > pll->max))
     89 		return 0;
     90 
     91 	for(i = 0; i < pll->count; i++) {
     92 		if (p->frequency > pll->entries[i].limit)
     93 			continue;
     94 		else
     95 			break;
     96 	}
     97 
     98 	if ( i >= pll->count)
     99 		return EINVAL;
    100 
    101 	d = (p->frequency + pll->iffreq) / pll->entries[i].stepsize;
    102 
    103 	b[0] = (d >> 8) & 0xff;
    104 	b[1] = (d >> 0) & 0xff;
    105 	b[2] = pll->entries[i].config;
    106 	b[3] = pll->entries[i].cb;
    107 
    108 	*fr = (d * pll->entries[i].stepsize) - pll->iffreq;
    109 
    110 	log(LOG_DEBUG, "pllw %d %02x %02x %02x %02x\n", *fr, b[0], b[1], b[2], b[3]);
    111 	return 0;
    112 }
    113 
    114 int
    115 tvpll_tune_dtv(struct tvpll *tvpll,
    116     const struct dvb_frontend_parameters *params)
    117 {
    118 	int rv;
    119 	uint32_t fr;
    120 	uint8_t b[4];
    121 
    122 	fr = 0;
    123 
    124 	if((rv = tvpll_algo(tvpll, b, params, &fr)) != 0)
    125 		return rv;
    126 
    127 	/* gate ctrl? */
    128 
    129 	iic_acquire_bus(tvpll->tag, I2C_F_POLL);
    130 	rv = iic_exec(tvpll->tag, I2C_OP_WRITE_WITH_STOP, tvpll->addr, b, 4, NULL, 0, I2C_F_POLL);
    131 	iic_release_bus(tvpll->tag, I2C_F_POLL);
    132 
    133 	if (rv != 0)
    134 		printf("%s\n", __func__);
    135 
    136 	tvpll->frequency = fr;
    137 
    138 	return rv;
    139 }
    140 
    141 MODULE(MODULE_CLASS_DRIVER, tvpll, NULL);
    142 
    143 static int
    144 tvpll_modcmd(modcmd_t cmd, void *opaque)
    145 {
    146 	if (cmd == MODULE_CMD_INIT || cmd == MODULE_CMD_FINI)
    147 		return 0;
    148 	return ENOTTY;
    149 }
    150