blob: 086a7b7cf6348fe45a44a18b41a0894df62f045d [file] [log] [blame]
Steven Tothf47623a2007-07-28 19:17:39 -03001/*
2 * Driver for Microtune MT2131 "QAM/8VSB single chip tuner"
3 *
Steven Toth6d897612008-09-03 17:12:12 -03004 * Copyright (c) 2006 Steven Toth <stoth@linuxtv.org>
Steven Tothf47623a2007-07-28 19:17:39 -03005 *
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.
10 *
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
14 *
15 * GNU General Public License for more details.
Steven Tothf47623a2007-07-28 19:17:39 -030016 */
17
18#include <linux/module.h>
Steven Tothf47623a2007-07-28 19:17:39 -030019#include <linux/delay.h>
20#include <linux/dvb/frontend.h>
21#include <linux/i2c.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Steven Tothf47623a2007-07-28 19:17:39 -030023
Mauro Carvalho Chehabfada1932017-12-28 13:03:51 -050024#include <media/dvb_frontend.h>
Steven Tothf47623a2007-07-28 19:17:39 -030025
26#include "mt2131.h"
27#include "mt2131_priv.h"
28
29static int debug;
30module_param(debug, int, 0644);
31MODULE_PARM_DESC(debug, "Turn on/off debugging (default:off).");
32
33#define dprintk(level,fmt, arg...) if (debug >= level) \
34 printk(KERN_INFO "%s: " fmt, "mt2131", ## arg)
35
36static 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
42};
43
44static u8 mt2131_config2[] = {
45 0x10,
46 0x7f, 0xc8, 0x0a, 0x5f, 0x00, 0x04
47};
48
49static int mt2131_readreg(struct mt2131_priv *priv, u8 reg, u8 *val)
50{
51 struct i2c_msg msg[2] = {
Michael Krufky3873dd02007-07-28 20:02:55 -030052 { .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 },
Steven Tothf47623a2007-07-28 19:17:39 -030056 };
57
58 if (i2c_transfer(priv->i2c, msg, 2) != 2) {
59 printk(KERN_WARNING "mt2131 I2C read failed\n");
60 return -EREMOTEIO;
61 }
62 return 0;
63}
64
65static int mt2131_writereg(struct mt2131_priv *priv, u8 reg, u8 val)
66{
67 u8 buf[2] = { reg, val };
Michael Krufky3873dd02007-07-28 20:02:55 -030068 struct i2c_msg msg = { .addr = priv->cfg->i2c_address, .flags = 0,
69 .buf = buf, .len = 2 };
Steven Tothf47623a2007-07-28 19:17:39 -030070
71 if (i2c_transfer(priv->i2c, &msg, 1) != 1) {
72 printk(KERN_WARNING "mt2131 I2C write failed\n");
73 return -EREMOTEIO;
74 }
75 return 0;
76}
77
78static int mt2131_writeregs(struct mt2131_priv *priv,u8 *buf, u8 len)
79{
Michael Krufky3873dd02007-07-28 20:02:55 -030080 struct i2c_msg msg = { .addr = priv->cfg->i2c_address,
81 .flags = 0, .buf = buf, .len = len };
Steven Tothf47623a2007-07-28 19:17:39 -030082
83 if (i2c_transfer(priv->i2c, &msg, 1) != 1) {
Michael Krufky3873dd02007-07-28 20:02:55 -030084 printk(KERN_WARNING "mt2131 I2C write failed (len=%i)\n",
85 (int)len);
Steven Tothf47623a2007-07-28 19:17:39 -030086 return -EREMOTEIO;
87 }
88 return 0;
89}
90
Mauro Carvalho Chehab14d24d12011-12-24 12:24:33 -030091static int mt2131_set_params(struct dvb_frontend *fe)
Steven Tothf47623a2007-07-28 19:17:39 -030092{
Mauro Carvalho Chehab01ce5a72011-12-20 21:26:01 -030093 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Steven Tothf47623a2007-07-28 19:17:39 -030094 struct mt2131_priv *priv;
95 int ret=0, i;
96 u32 freq;
97 u8 if_band_center;
Michael Krufky3873dd02007-07-28 20:02:55 -030098 u32 f_lo1, f_lo2;
99 u32 div1, num1, div2, num2;
Steven Tothf47623a2007-07-28 19:17:39 -0300100 u8 b[8];
101 u8 lockval = 0;
102
103 priv = fe->tuner_priv;
Steven Tothf47623a2007-07-28 19:17:39 -0300104
Mauro Carvalho Chehab01ce5a72011-12-20 21:26:01 -0300105 freq = c->frequency / 1000; /* Hz -> kHz */
Harvey Harrison271ddbf2008-04-08 23:20:00 -0300106 dprintk(1, "%s() freq=%d\n", __func__, freq);
Steven Tothf47623a2007-07-28 19:17:39 -0300107
108 f_lo1 = freq + MT2131_IF1 * 1000;
109 f_lo1 = (f_lo1 / 250) * 250;
110 f_lo2 = f_lo1 - freq - MT2131_IF2;
111
Steven Toth195ccf62007-10-24 23:12:58 -0300112 priv->frequency = (f_lo1 - f_lo2 - MT2131_IF2) * 1000;
Steven Tothf47623a2007-07-28 19:17:39 -0300113
114 /* Frequency LO1 = 16MHz * (DIV1 + NUM1/8192 ) */
115 num1 = f_lo1 * 64 / (MT2131_FREF / 128);
116 div1 = num1 / 8192;
117 num1 &= 0x1fff;
118
119 /* Frequency LO2 = 16MHz * (DIV2 + NUM2/8192 ) */
120 num2 = f_lo2 * 64 / (MT2131_FREF / 128);
121 div2 = num2 / 8192;
122 num2 &= 0x1fff;
123
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;
143
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;
151
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]);
160
161 ret = mt2131_writeregs(priv,b,7);
162 if (ret < 0)
163 return ret;
164
165 mt2131_writereg(priv, 0x0b, if_band_center);
166
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);
176
177 return ret;
178}
179
180static int mt2131_get_frequency(struct dvb_frontend *fe, u32 *frequency)
181{
182 struct mt2131_priv *priv = fe->tuner_priv;
Harvey Harrison271ddbf2008-04-08 23:20:00 -0300183 dprintk(1, "%s()\n", __func__);
Steven Tothf47623a2007-07-28 19:17:39 -0300184 *frequency = priv->frequency;
185 return 0;
186}
187
Steven Tothf47623a2007-07-28 19:17:39 -0300188static int mt2131_get_status(struct dvb_frontend *fe, u32 *status)
189{
190 struct mt2131_priv *priv = fe->tuner_priv;
191 u8 lock_status = 0;
192 u8 afc_status = 0;
193
194 *status = 0;
195
196 mt2131_readreg(priv, 0x08, &lock_status);
197 if ((lock_status & 0x88) == 0x88)
198 *status = TUNER_STATUS_LOCKED;
199
200 mt2131_readreg(priv, 0x09, &afc_status);
201 dprintk(1, "%s() - LO Status = 0x%x, AFC Status = 0x%x\n",
Harvey Harrison271ddbf2008-04-08 23:20:00 -0300202 __func__, lock_status, afc_status);
Steven Tothf47623a2007-07-28 19:17:39 -0300203
204 return 0;
205}
206
207static int mt2131_init(struct dvb_frontend *fe)
208{
209 struct mt2131_priv *priv = fe->tuner_priv;
210 int ret;
Harvey Harrison271ddbf2008-04-08 23:20:00 -0300211 dprintk(1, "%s()\n", __func__);
Steven Tothf47623a2007-07-28 19:17:39 -0300212
Michael Krufky3873dd02007-07-28 20:02:55 -0300213 if ((ret = mt2131_writeregs(priv, mt2131_config1,
214 sizeof(mt2131_config1))) < 0)
Steven Tothf47623a2007-07-28 19:17:39 -0300215 return ret;
216
217 mt2131_writereg(priv, 0x0b, 0x09);
218 mt2131_writereg(priv, 0x15, 0x47);
219 mt2131_writereg(priv, 0x07, 0xf2);
220 mt2131_writereg(priv, 0x0b, 0x01);
221
Michael Krufky3873dd02007-07-28 20:02:55 -0300222 if ((ret = mt2131_writeregs(priv, mt2131_config2,
223 sizeof(mt2131_config2))) < 0)
Steven Tothf47623a2007-07-28 19:17:39 -0300224 return ret;
225
226 return ret;
227}
228
Max Kellermann194ced72016-08-09 18:32:31 -0300229static void mt2131_release(struct dvb_frontend *fe)
Steven Tothf47623a2007-07-28 19:17:39 -0300230{
Harvey Harrison271ddbf2008-04-08 23:20:00 -0300231 dprintk(1, "%s()\n", __func__);
Steven Tothf47623a2007-07-28 19:17:39 -0300232 kfree(fe->tuner_priv);
233 fe->tuner_priv = NULL;
Steven Tothf47623a2007-07-28 19:17:39 -0300234}
235
236static const struct dvb_tuner_ops mt2131_tuner_ops = {
237 .info = {
Mauro Carvalho Chehaba3f90c72018-07-05 18:59:35 -0400238 .name = "Microtune MT2131",
239 .frequency_min_hz = 48 * MHz,
240 .frequency_max_hz = 860 * MHz,
241 .frequency_step_hz = 50 * kHz,
Steven Tothf47623a2007-07-28 19:17:39 -0300242 },
243
244 .release = mt2131_release,
245 .init = mt2131_init,
246
247 .set_params = mt2131_set_params,
248 .get_frequency = mt2131_get_frequency,
Steven Tothf47623a2007-07-28 19:17:39 -0300249 .get_status = mt2131_get_status
250};
251
Michael Krufky3873dd02007-07-28 20:02:55 -0300252struct dvb_frontend * mt2131_attach(struct dvb_frontend *fe,
253 struct i2c_adapter *i2c,
254 struct mt2131_config *cfg, u16 if1)
Steven Tothf47623a2007-07-28 19:17:39 -0300255{
256 struct mt2131_priv *priv = NULL;
257 u8 id = 0;
258
Harvey Harrison271ddbf2008-04-08 23:20:00 -0300259 dprintk(1, "%s()\n", __func__);
Steven Tothf47623a2007-07-28 19:17:39 -0300260
261 priv = kzalloc(sizeof(struct mt2131_priv), GFP_KERNEL);
262 if (priv == NULL)
263 return NULL;
264
265 priv->cfg = cfg;
Steven Tothf47623a2007-07-28 19:17:39 -0300266 priv->i2c = i2c;
267
268 if (mt2131_readreg(priv, 0, &id) != 0) {
269 kfree(priv);
270 return NULL;
271 }
272 if ( (id != 0x3E) && (id != 0x3F) ) {
Michael Krufky3873dd02007-07-28 20:02:55 -0300273 printk(KERN_ERR "MT2131: Device not found at addr 0x%02x\n",
274 cfg->i2c_address);
Steven Tothf47623a2007-07-28 19:17:39 -0300275 kfree(priv);
276 return NULL;
277 }
278
Michael Krufky3873dd02007-07-28 20:02:55 -0300279 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));
Steven Tothf47623a2007-07-28 19:17:39 -0300283
284 fe->tuner_priv = priv;
285 return fe;
286}
287EXPORT_SYMBOL(mt2131_attach);
288
289MODULE_AUTHOR("Steven Toth");
290MODULE_DESCRIPTION("Microtune MT2131 silicon tuner driver");
291MODULE_LICENSE("GPL");