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