xref: /netbsd-src/sys/dev/i2c/tvpll.c (revision a5847cc334d9a7029f6352b847e9e8d71a0f9e0c)
1 /* $NetBSD: tvpll.c,v 1.4 2011/10/02 19:03:56 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.4 2011/10/02 19:03:56 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 	int rv;
59 
60 	tvpll = kmem_alloc(sizeof(struct tvpll), KM_SLEEP);
61         if (tvpll == NULL)
62                 return NULL;
63 
64 	tvpll->tag = t;
65 	tvpll->addr = a;
66 
67 	tvpll->pll = p;
68 
69 	if (tvpll->pll->initdata) {
70 		iic_acquire_bus(tvpll->tag, I2C_F_POLL);
71 		rv = iic_exec(tvpll->tag, I2C_OP_WRITE_WITH_STOP, tvpll->addr,
72 		    &tvpll->pll->initdata[1], tvpll->pll->initdata[0],
73 		    NULL, 0, I2C_F_POLL);
74 		iic_release_bus(tvpll->tag, I2C_F_POLL);
75 	}
76 
77 	device_printf(parent, "tvpll: %s\n", tvpll->pll->name);
78 
79 	return tvpll;
80 }
81 
82 void
83 tvpll_close(struct tvpll *tvpll)
84 {
85 	kmem_free(tvpll, sizeof(*tvpll));
86 }
87 
88 static uint32_t
89 tvpll_algo(struct tvpll *tvpll, uint8_t *b,
90     const struct dvb_frontend_parameters *p, uint32_t *fr)
91 {
92 	const struct tvpll_data *pll;
93 	uint32_t d;
94 	int i;
95 
96 	pll = tvpll->pll;
97 
98 	if(p->frequency != 0 &&
99 	    (p->frequency < pll->min || p->frequency > pll->max))
100 		return 0;
101 
102 	for(i = 0; i < pll->count; i++) {
103 		if (p->frequency > pll->entries[i].limit)
104 			continue;
105 		else
106 			break;
107 	}
108 
109 	if ( i >= pll->count)
110 		return EINVAL;
111 
112 	d = (p->frequency + pll->iffreq) / pll->entries[i].stepsize;
113 
114 	b[0] = (d >> 8) & 0xff;
115 	b[1] = (d >> 0) & 0xff;
116 	b[2] = pll->entries[i].config;
117 	b[3] = pll->entries[i].cb;
118 	b[4] = pll->entries[i].aux;
119 
120 	*fr = (d * pll->entries[i].stepsize) - pll->iffreq;
121 
122 	log(LOG_DEBUG, "pllw %d %02x %02x %02x %02x %02x\n", *fr, b[0], b[1], b[2], b[3], b[4]);
123 	return 0;
124 }
125 
126 int
127 tvpll_tune_dtv(struct tvpll *tvpll,
128     const struct dvb_frontend_parameters *params)
129 {
130 	int rv;
131 	uint32_t fr;
132 	uint8_t b[5], ab[2];
133 
134 	fr = 0;
135 
136 	if((rv = tvpll_algo(tvpll, b, params, &fr)) != 0)
137 		return rv;
138 
139 	iic_acquire_bus(tvpll->tag, I2C_F_POLL);
140 	/* gate ctrl? */
141 	if (b[4] != TVPLL_IGNORE_AUX) {
142 		ab[0] = b[2] | 0x18;
143 		ab[1] = b[4];
144 		rv = iic_exec(tvpll->tag, I2C_OP_WRITE_WITH_STOP, tvpll->addr, ab, 2, NULL, 0, I2C_F_POLL);
145 	}
146 	rv = iic_exec(tvpll->tag, I2C_OP_WRITE_WITH_STOP, tvpll->addr, b, 4, NULL, 0, I2C_F_POLL);
147 	iic_release_bus(tvpll->tag, I2C_F_POLL);
148 
149 	if (rv != 0)
150 		printf("%s\n", __func__);
151 
152 	tvpll->frequency = fr;
153 
154 	return rv;
155 }
156 
157 MODULE(MODULE_CLASS_DRIVER, tvpll, "iic");
158 
159 static int
160 tvpll_modcmd(modcmd_t cmd, void *opaque)
161 {
162 	if (cmd == MODULE_CMD_INIT || cmd == MODULE_CMD_FINI)
163 		return 0;
164 	return ENOTTY;
165 }
166