]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/media/tuners/mt2131.c
Merge branches 'for-4.11/upstream-fixes', 'for-4.12/accutouch', 'for-4.12/cp2112...
[mirror_ubuntu-artful-kernel.git] / drivers / media / tuners / mt2131.c
CommitLineData
f47623a0
ST
1/*
2 * Driver for Microtune MT2131 "QAM/8VSB single chip tuner"
3 *
6d897616 4 * Copyright (c) 2006 Steven Toth <stoth@linuxtv.org>
f47623a0
ST
5 *
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.
f47623a0
ST
16 */
17
18#include <linux/module.h>
f47623a0
ST
19#include <linux/delay.h>
20#include <linux/dvb/frontend.h>
21#include <linux/i2c.h>
5a0e3ad6 22#include <linux/slab.h>
f47623a0
ST
23
24#include "dvb_frontend.h"
25
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] = {
3873dd04
MK
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 },
f47623a0
ST
56 };
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 };
3873dd04
MK
68 struct i2c_msg msg = { .addr = priv->cfg->i2c_address, .flags = 0,
69 .buf = buf, .len = 2 };
f47623a0
ST
70
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{
3873dd04
MK
80 struct i2c_msg msg = { .addr = priv->cfg->i2c_address,
81 .flags = 0, .buf = buf, .len = len };
f47623a0
ST
82
83 if (i2c_transfer(priv->i2c, &msg, 1) != 1) {
3873dd04
MK
84 printk(KERN_WARNING "mt2131 I2C write failed (len=%i)\n",
85 (int)len);
f47623a0
ST
86 return -EREMOTEIO;
87 }
88 return 0;
89}
90
14d24d14 91static int mt2131_set_params(struct dvb_frontend *fe)
f47623a0 92{
01ce5a79 93 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
f47623a0
ST
94 struct mt2131_priv *priv;
95 int ret=0, i;
96 u32 freq;
97 u8 if_band_center;
3873dd04
MK
98 u32 f_lo1, f_lo2;
99 u32 div1, num1, div2, num2;
f47623a0
ST
100 u8 b[8];
101 u8 lockval = 0;
102
103 priv = fe->tuner_priv;
f47623a0 104
01ce5a79 105 freq = c->frequency / 1000; /* Hz -> kHz */
271ddbf7 106 dprintk(1, "%s() freq=%d\n", __func__, freq);
f47623a0
ST
107
108 f_lo1 = freq + MT2131_IF1 * 1000;
109 f_lo1 = (f_lo1 / 250) * 250;
110 f_lo2 = f_lo1 - freq - MT2131_IF2;
111
195ccf67 112 priv->frequency = (f_lo1 - f_lo2 - MT2131_IF2) * 1000;
f47623a0
ST
113
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;
271ddbf7 183 dprintk(1, "%s()\n", __func__);
f47623a0
ST
184 *frequency = priv->frequency;
185 return 0;
186}
187
f47623a0
ST
188static 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",
271ddbf7 202 __func__, lock_status, afc_status);
f47623a0
ST
203
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;
271ddbf7 211 dprintk(1, "%s()\n", __func__);
f47623a0 212
3873dd04
MK
213 if ((ret = mt2131_writeregs(priv, mt2131_config1,
214 sizeof(mt2131_config1))) < 0)
f47623a0
ST
215 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
3873dd04
MK
222 if ((ret = mt2131_writeregs(priv, mt2131_config2,
223 sizeof(mt2131_config2))) < 0)
f47623a0
ST
224 return ret;
225
226 return ret;
227}
228
194ced7a 229static void mt2131_release(struct dvb_frontend *fe)
f47623a0 230{
271ddbf7 231 dprintk(1, "%s()\n", __func__);
f47623a0
ST
232 kfree(fe->tuner_priv);
233 fe->tuner_priv = NULL;
f47623a0
ST
234}
235
236static const struct dvb_tuner_ops mt2131_tuner_ops = {
237 .info = {
238 .name = "Microtune MT2131",
239 .frequency_min = 48000000,
240 .frequency_max = 860000000,
241 .frequency_step = 50000,
242 },
243
244 .release = mt2131_release,
245 .init = mt2131_init,
246
247 .set_params = mt2131_set_params,
248 .get_frequency = mt2131_get_frequency,
f47623a0
ST
249 .get_status = mt2131_get_status
250};
251
3873dd04
MK
252struct dvb_frontend * mt2131_attach(struct dvb_frontend *fe,
253 struct i2c_adapter *i2c,
254 struct mt2131_config *cfg, u16 if1)
f47623a0
ST
255{
256 struct mt2131_priv *priv = NULL;
257 u8 id = 0;
258
271ddbf7 259 dprintk(1, "%s()\n", __func__);
f47623a0
ST
260
261 priv = kzalloc(sizeof(struct mt2131_priv), GFP_KERNEL);
262 if (priv == NULL)
263 return NULL;
264
265 priv->cfg = cfg;
f47623a0
ST
266 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) ) {
3873dd04
MK
273 printk(KERN_ERR "MT2131: Device not found at addr 0x%02x\n",
274 cfg->i2c_address);
f47623a0
ST
275 kfree(priv);
276 return NULL;
277 }
278
3873dd04
MK
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));
f47623a0
ST
283
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");