Linux 4.19.133
[linux/fpc-iii.git] / drivers / media / tuners / mt2131.c
blob086a7b7cf6348fe45a44a18b41a0894df62f045d
1 /*
2 * Driver for Microtune MT2131 "QAM/8VSB single chip tuner"
4 * Copyright (c) 2006 Steven Toth <stoth@linuxtv.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
18 #include <linux/module.h>
19 #include <linux/delay.h>
20 #include <linux/dvb/frontend.h>
21 #include <linux/i2c.h>
22 #include <linux/slab.h>
24 #include <media/dvb_frontend.h>
26 #include "mt2131.h"
27 #include "mt2131_priv.h"
29 static int debug;
30 module_param(debug, int, 0644);
31 MODULE_PARM_DESC(debug, "Turn on/off debugging (default:off).");
33 #define dprintk(level,fmt, arg...) if (debug >= level) \
34 printk(KERN_INFO "%s: " fmt, "mt2131", ## arg)
36 static u8 mt2131_config1[] = {
37 0x01,
38 0x50, 0x00, 0x50, 0x80, 0x00, 0x49, 0xfa, 0x88,
39 0x08, 0x77, 0x41, 0x04, 0x00, 0x00, 0x00, 0x32,
40 0x7f, 0xda, 0x4c, 0x00, 0x10, 0xaa, 0x78, 0x80,
41 0xff, 0x68, 0xa0, 0xff, 0xdd, 0x00, 0x00
44 static u8 mt2131_config2[] = {
45 0x10,
46 0x7f, 0xc8, 0x0a, 0x5f, 0x00, 0x04
49 static int mt2131_readreg(struct mt2131_priv *priv, u8 reg, u8 *val)
51 struct i2c_msg msg[2] = {
52 { .addr = priv->cfg->i2c_address, .flags = 0,
53 .buf = &reg, .len = 1 },
54 { .addr = priv->cfg->i2c_address, .flags = I2C_M_RD,
55 .buf = val, .len = 1 },
58 if (i2c_transfer(priv->i2c, msg, 2) != 2) {
59 printk(KERN_WARNING "mt2131 I2C read failed\n");
60 return -EREMOTEIO;
62 return 0;
65 static int mt2131_writereg(struct mt2131_priv *priv, u8 reg, u8 val)
67 u8 buf[2] = { reg, val };
68 struct i2c_msg msg = { .addr = priv->cfg->i2c_address, .flags = 0,
69 .buf = buf, .len = 2 };
71 if (i2c_transfer(priv->i2c, &msg, 1) != 1) {
72 printk(KERN_WARNING "mt2131 I2C write failed\n");
73 return -EREMOTEIO;
75 return 0;
78 static int mt2131_writeregs(struct mt2131_priv *priv,u8 *buf, u8 len)
80 struct i2c_msg msg = { .addr = priv->cfg->i2c_address,
81 .flags = 0, .buf = buf, .len = len };
83 if (i2c_transfer(priv->i2c, &msg, 1) != 1) {
84 printk(KERN_WARNING "mt2131 I2C write failed (len=%i)\n",
85 (int)len);
86 return -EREMOTEIO;
88 return 0;
91 static int mt2131_set_params(struct dvb_frontend *fe)
93 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
94 struct mt2131_priv *priv;
95 int ret=0, i;
96 u32 freq;
97 u8 if_band_center;
98 u32 f_lo1, f_lo2;
99 u32 div1, num1, div2, num2;
100 u8 b[8];
101 u8 lockval = 0;
103 priv = fe->tuner_priv;
105 freq = c->frequency / 1000; /* Hz -> kHz */
106 dprintk(1, "%s() freq=%d\n", __func__, freq);
108 f_lo1 = freq + MT2131_IF1 * 1000;
109 f_lo1 = (f_lo1 / 250) * 250;
110 f_lo2 = f_lo1 - freq - MT2131_IF2;
112 priv->frequency = (f_lo1 - f_lo2 - MT2131_IF2) * 1000;
114 /* Frequency LO1 = 16MHz * (DIV1 + NUM1/8192 ) */
115 num1 = f_lo1 * 64 / (MT2131_FREF / 128);
116 div1 = num1 / 8192;
117 num1 &= 0x1fff;
119 /* Frequency LO2 = 16MHz * (DIV2 + NUM2/8192 ) */
120 num2 = f_lo2 * 64 / (MT2131_FREF / 128);
121 div2 = num2 / 8192;
122 num2 &= 0x1fff;
124 if (freq <= 82500) if_band_center = 0x00; else
125 if (freq <= 137500) if_band_center = 0x01; else
126 if (freq <= 192500) if_band_center = 0x02; else
127 if (freq <= 247500) if_band_center = 0x03; else
128 if (freq <= 302500) if_band_center = 0x04; else
129 if (freq <= 357500) if_band_center = 0x05; else
130 if (freq <= 412500) if_band_center = 0x06; else
131 if (freq <= 467500) if_band_center = 0x07; else
132 if (freq <= 522500) if_band_center = 0x08; else
133 if (freq <= 577500) if_band_center = 0x09; else
134 if (freq <= 632500) if_band_center = 0x0A; else
135 if (freq <= 687500) if_band_center = 0x0B; else
136 if (freq <= 742500) if_band_center = 0x0C; else
137 if (freq <= 797500) if_band_center = 0x0D; else
138 if (freq <= 852500) if_band_center = 0x0E; else
139 if (freq <= 907500) if_band_center = 0x0F; else
140 if (freq <= 962500) if_band_center = 0x10; else
141 if (freq <= 1017500) if_band_center = 0x11; else
142 if (freq <= 1072500) if_band_center = 0x12; else if_band_center = 0x13;
144 b[0] = 1;
145 b[1] = (num1 >> 5) & 0xFF;
146 b[2] = (num1 & 0x1F);
147 b[3] = div1;
148 b[4] = (num2 >> 5) & 0xFF;
149 b[5] = num2 & 0x1F;
150 b[6] = div2;
152 dprintk(1, "IF1: %dMHz IF2: %dMHz\n", MT2131_IF1, MT2131_IF2);
153 dprintk(1, "PLL freq=%dkHz band=%d\n", (int)freq, (int)if_band_center);
154 dprintk(1, "PLL f_lo1=%dkHz f_lo2=%dkHz\n", (int)f_lo1, (int)f_lo2);
155 dprintk(1, "PLL div1=%d num1=%d div2=%d num2=%d\n",
156 (int)div1, (int)num1, (int)div2, (int)num2);
157 dprintk(1, "PLL [1..6]: %2x %2x %2x %2x %2x %2x\n",
158 (int)b[1], (int)b[2], (int)b[3], (int)b[4], (int)b[5],
159 (int)b[6]);
161 ret = mt2131_writeregs(priv,b,7);
162 if (ret < 0)
163 return ret;
165 mt2131_writereg(priv, 0x0b, if_band_center);
167 /* Wait for lock */
168 i = 0;
169 do {
170 mt2131_readreg(priv, 0x08, &lockval);
171 if ((lockval & 0x88) == 0x88)
172 break;
173 msleep(4);
174 i++;
175 } while (i < 10);
177 return ret;
180 static int mt2131_get_frequency(struct dvb_frontend *fe, u32 *frequency)
182 struct mt2131_priv *priv = fe->tuner_priv;
183 dprintk(1, "%s()\n", __func__);
184 *frequency = priv->frequency;
185 return 0;
188 static int mt2131_get_status(struct dvb_frontend *fe, u32 *status)
190 struct mt2131_priv *priv = fe->tuner_priv;
191 u8 lock_status = 0;
192 u8 afc_status = 0;
194 *status = 0;
196 mt2131_readreg(priv, 0x08, &lock_status);
197 if ((lock_status & 0x88) == 0x88)
198 *status = TUNER_STATUS_LOCKED;
200 mt2131_readreg(priv, 0x09, &afc_status);
201 dprintk(1, "%s() - LO Status = 0x%x, AFC Status = 0x%x\n",
202 __func__, lock_status, afc_status);
204 return 0;
207 static int mt2131_init(struct dvb_frontend *fe)
209 struct mt2131_priv *priv = fe->tuner_priv;
210 int ret;
211 dprintk(1, "%s()\n", __func__);
213 if ((ret = mt2131_writeregs(priv, mt2131_config1,
214 sizeof(mt2131_config1))) < 0)
215 return ret;
217 mt2131_writereg(priv, 0x0b, 0x09);
218 mt2131_writereg(priv, 0x15, 0x47);
219 mt2131_writereg(priv, 0x07, 0xf2);
220 mt2131_writereg(priv, 0x0b, 0x01);
222 if ((ret = mt2131_writeregs(priv, mt2131_config2,
223 sizeof(mt2131_config2))) < 0)
224 return ret;
226 return ret;
229 static void mt2131_release(struct dvb_frontend *fe)
231 dprintk(1, "%s()\n", __func__);
232 kfree(fe->tuner_priv);
233 fe->tuner_priv = NULL;
236 static const struct dvb_tuner_ops mt2131_tuner_ops = {
237 .info = {
238 .name = "Microtune MT2131",
239 .frequency_min_hz = 48 * MHz,
240 .frequency_max_hz = 860 * MHz,
241 .frequency_step_hz = 50 * kHz,
244 .release = mt2131_release,
245 .init = mt2131_init,
247 .set_params = mt2131_set_params,
248 .get_frequency = mt2131_get_frequency,
249 .get_status = mt2131_get_status
252 struct dvb_frontend * mt2131_attach(struct dvb_frontend *fe,
253 struct i2c_adapter *i2c,
254 struct mt2131_config *cfg, u16 if1)
256 struct mt2131_priv *priv = NULL;
257 u8 id = 0;
259 dprintk(1, "%s()\n", __func__);
261 priv = kzalloc(sizeof(struct mt2131_priv), GFP_KERNEL);
262 if (priv == NULL)
263 return NULL;
265 priv->cfg = cfg;
266 priv->i2c = i2c;
268 if (mt2131_readreg(priv, 0, &id) != 0) {
269 kfree(priv);
270 return NULL;
272 if ( (id != 0x3E) && (id != 0x3F) ) {
273 printk(KERN_ERR "MT2131: Device not found at addr 0x%02x\n",
274 cfg->i2c_address);
275 kfree(priv);
276 return NULL;
279 printk(KERN_INFO "MT2131: successfully identified at address 0x%02x\n",
280 cfg->i2c_address);
281 memcpy(&fe->ops.tuner_ops, &mt2131_tuner_ops,
282 sizeof(struct dvb_tuner_ops));
284 fe->tuner_priv = priv;
285 return fe;
287 EXPORT_SYMBOL(mt2131_attach);
289 MODULE_AUTHOR("Steven Toth");
290 MODULE_DESCRIPTION("Microtune MT2131 silicon tuner driver");
291 MODULE_LICENSE("GPL");