blob: 3a851f524b16ba35b70c109922614846349d8d7f [file] [log] [blame]
Manu Abrahamc46b6562007-02-24 08:31:40 -03001/*
2 STB6100 Silicon Tuner
3 Copyright (C) Manu Abraham (abraham.manu@gmail.com)
4
5 Copyright (C) ST Microelectronics
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20*/
21
22#include <linux/init.h>
23#include <linux/kernel.h>
24#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Manu Abrahamc46b6562007-02-24 08:31:40 -030026#include <linux/string.h>
27
Mauro Carvalho Chehabfada1932017-12-28 13:03:51 -050028#include <media/dvb_frontend.h>
Manu Abrahamc46b6562007-02-24 08:31:40 -030029#include "stb6100.h"
30
31static unsigned int verbose;
32module_param(verbose, int, 0644);
33
Mauro Carvalho Chehab83937962013-11-02 05:05:18 -030034/* Max transfer size done by I2C transfer functions */
35#define MAX_XFER_SIZE 64
Manu Abrahamc46b6562007-02-24 08:31:40 -030036
37#define FE_ERROR 0
38#define FE_NOTICE 1
39#define FE_INFO 2
40#define FE_DEBUG 3
41
42#define dprintk(x, y, z, format, arg...) do { \
43 if (z) { \
44 if ((x > FE_ERROR) && (x > y)) \
45 printk(KERN_ERR "%s: " format "\n", __func__ , ##arg); \
46 else if ((x > FE_NOTICE) && (x > y)) \
47 printk(KERN_NOTICE "%s: " format "\n", __func__ , ##arg); \
48 else if ((x > FE_INFO) && (x > y)) \
49 printk(KERN_INFO "%s: " format "\n", __func__ , ##arg); \
50 else if ((x > FE_DEBUG) && (x > y)) \
51 printk(KERN_DEBUG "%s: " format "\n", __func__ , ##arg); \
52 } else { \
53 if (x > y) \
54 printk(format, ##arg); \
55 } \
Manu Abrahamf14bfe92010-11-14 15:52:10 -030056} while (0)
Manu Abrahamc46b6562007-02-24 08:31:40 -030057
58struct stb6100_lkup {
59 u32 val_low;
60 u32 val_high;
61 u8 reg;
62};
63
Mauro Carvalho Chehabf2709c22016-11-18 20:30:51 -020064static void stb6100_release(struct dvb_frontend *fe);
65
Manu Abrahamc46b6562007-02-24 08:31:40 -030066static const struct stb6100_lkup lkup[] = {
67 { 0, 950000, 0x0a },
68 { 950000, 1000000, 0x0a },
69 { 1000000, 1075000, 0x0c },
70 { 1075000, 1200000, 0x00 },
71 { 1200000, 1300000, 0x01 },
72 { 1300000, 1370000, 0x02 },
73 { 1370000, 1470000, 0x04 },
74 { 1470000, 1530000, 0x05 },
75 { 1530000, 1650000, 0x06 },
76 { 1650000, 1800000, 0x08 },
77 { 1800000, 1950000, 0x0a },
78 { 1950000, 2150000, 0x0c },
79 { 2150000, 9999999, 0x0c },
80 { 0, 0, 0x00 }
81};
82
83/* Register names for easy debugging. */
84static const char *stb6100_regnames[] = {
85 [STB6100_LD] = "LD",
86 [STB6100_VCO] = "VCO",
87 [STB6100_NI] = "NI",
88 [STB6100_NF_LSB] = "NF",
89 [STB6100_K] = "K",
90 [STB6100_G] = "G",
91 [STB6100_F] = "F",
92 [STB6100_DLB] = "DLB",
93 [STB6100_TEST1] = "TEST1",
94 [STB6100_FCCK] = "FCCK",
95 [STB6100_LPEN] = "LPEN",
96 [STB6100_TEST3] = "TEST3",
97};
98
99/* Template for normalisation, i.e. setting unused or undocumented
100 * bits as required according to the documentation.
101 */
102struct stb6100_regmask {
103 u8 mask;
104 u8 set;
105};
106
107static const struct stb6100_regmask stb6100_template[] = {
108 [STB6100_LD] = { 0xff, 0x00 },
109 [STB6100_VCO] = { 0xff, 0x00 },
110 [STB6100_NI] = { 0xff, 0x00 },
111 [STB6100_NF_LSB] = { 0xff, 0x00 },
112 [STB6100_K] = { 0xc7, 0x38 },
113 [STB6100_G] = { 0xef, 0x10 },
114 [STB6100_F] = { 0x1f, 0xc0 },
115 [STB6100_DLB] = { 0x38, 0xc4 },
116 [STB6100_TEST1] = { 0x00, 0x8f },
117 [STB6100_FCCK] = { 0x40, 0x0d },
118 [STB6100_LPEN] = { 0xf0, 0x0b },
119 [STB6100_TEST3] = { 0x00, 0xde },
120};
121
Mauro Carvalho Chehaba9319102010-11-22 13:22:37 -0300122/*
123 * Currently unused. Some boards might need it in the future
124 */
125static inline void stb6100_normalise_regs(u8 regs[])
Manu Abrahamc46b6562007-02-24 08:31:40 -0300126{
127 int i;
128
129 for (i = 0; i < STB6100_NUMREGS; i++)
130 regs[i] = (regs[i] & stb6100_template[i].mask) | stb6100_template[i].set;
131}
132
133static int stb6100_read_regs(struct stb6100_state *state, u8 regs[])
134{
135 int rc;
136 struct i2c_msg msg = {
137 .addr = state->config->tuner_address,
138 .flags = I2C_M_RD,
139 .buf = regs,
140 .len = STB6100_NUMREGS
141 };
142
Manu Abrahamc46b6562007-02-24 08:31:40 -0300143 rc = i2c_transfer(state->i2c, &msg, 1);
Manu Abrahamc46b6562007-02-24 08:31:40 -0300144 if (unlikely(rc != 1)) {
145 dprintk(verbose, FE_ERROR, 1, "Read (0x%x) err, rc=[%d]",
146 state->config->tuner_address, rc);
147
148 return -EREMOTEIO;
149 }
150 if (unlikely(verbose > FE_DEBUG)) {
151 int i;
152
153 dprintk(verbose, FE_DEBUG, 1, " Read from 0x%02x", state->config->tuner_address);
154 for (i = 0; i < STB6100_NUMREGS; i++)
155 dprintk(verbose, FE_DEBUG, 1, " %s: 0x%02x", stb6100_regnames[i], regs[i]);
156 }
157 return 0;
158}
159
160static int stb6100_read_reg(struct stb6100_state *state, u8 reg)
161{
162 u8 regs[STB6100_NUMREGS];
Manu Abrahamc46b6562007-02-24 08:31:40 -0300163
Manu Abrahamf14bfe92010-11-14 15:52:10 -0300164 struct i2c_msg msg = {
165 .addr = state->config->tuner_address + reg,
166 .flags = I2C_M_RD,
167 .buf = regs,
168 .len = 1
169 };
170
Hans Verkuilfdf07b02012-04-20 08:04:48 -0300171 i2c_transfer(state->i2c, &msg, 1);
Manu Abrahamf14bfe92010-11-14 15:52:10 -0300172
Manu Abrahamc46b6562007-02-24 08:31:40 -0300173 if (unlikely(reg >= STB6100_NUMREGS)) {
174 dprintk(verbose, FE_ERROR, 1, "Invalid register offset 0x%x", reg);
175 return -EINVAL;
176 }
Manu Abrahamf14bfe92010-11-14 15:52:10 -0300177 if (unlikely(verbose > FE_DEBUG)) {
178 dprintk(verbose, FE_DEBUG, 1, " Read from 0x%02x", state->config->tuner_address);
179 dprintk(verbose, FE_DEBUG, 1, " %s: 0x%02x", stb6100_regnames[reg], regs[0]);
180 }
181
182 return (unsigned int)regs[0];
Manu Abrahamc46b6562007-02-24 08:31:40 -0300183}
184
185static int stb6100_write_reg_range(struct stb6100_state *state, u8 buf[], int start, int len)
186{
187 int rc;
Mauro Carvalho Chehab83937962013-11-02 05:05:18 -0300188 u8 cmdbuf[MAX_XFER_SIZE];
Manu Abrahamc46b6562007-02-24 08:31:40 -0300189 struct i2c_msg msg = {
190 .addr = state->config->tuner_address,
191 .flags = 0,
192 .buf = cmdbuf,
193 .len = len + 1
194 };
195
Alexander Shiyan7e6bd122014-02-25 23:41:14 -0300196 if (1 + len > sizeof(cmdbuf)) {
Mauro Carvalho Chehab83937962013-11-02 05:05:18 -0300197 printk(KERN_WARNING
198 "%s: i2c wr: len=%d is too big!\n",
199 KBUILD_MODNAME, len);
200 return -EINVAL;
201 }
202
Manu Abrahamc46b6562007-02-24 08:31:40 -0300203 if (unlikely(start < 1 || start + len > STB6100_NUMREGS)) {
204 dprintk(verbose, FE_ERROR, 1, "Invalid register range %d:%d",
205 start, len);
206 return -EINVAL;
207 }
208 memcpy(&cmdbuf[1], buf, len);
209 cmdbuf[0] = start;
210
211 if (unlikely(verbose > FE_DEBUG)) {
212 int i;
213
214 dprintk(verbose, FE_DEBUG, 1, " Write @ 0x%02x: [%d:%d]", state->config->tuner_address, start, len);
215 for (i = 0; i < len; i++)
216 dprintk(verbose, FE_DEBUG, 1, " %s: 0x%02x", stb6100_regnames[start + i], buf[i]);
217 }
Manu Abrahamc46b6562007-02-24 08:31:40 -0300218 rc = i2c_transfer(state->i2c, &msg, 1);
Manu Abrahamc46b6562007-02-24 08:31:40 -0300219 if (unlikely(rc != 1)) {
220 dprintk(verbose, FE_ERROR, 1, "(0x%x) write err [%d:%d], rc=[%d]",
221 (unsigned int)state->config->tuner_address, start, len, rc);
222 return -EREMOTEIO;
223 }
224 return 0;
225}
226
227static int stb6100_write_reg(struct stb6100_state *state, u8 reg, u8 data)
228{
Arnd Bergmann3cd890d2017-11-30 11:55:46 -0500229 u8 tmp = data; /* see gcc.gnu.org/bugzilla/show_bug.cgi?id=81715 */
230
Manu Abrahamc46b6562007-02-24 08:31:40 -0300231 if (unlikely(reg >= STB6100_NUMREGS)) {
232 dprintk(verbose, FE_ERROR, 1, "Invalid register offset 0x%x", reg);
233 return -EREMOTEIO;
234 }
Arnd Bergmann3cd890d2017-11-30 11:55:46 -0500235 tmp = (tmp & stb6100_template[reg].mask) | stb6100_template[reg].set;
236 return stb6100_write_reg_range(state, &tmp, reg, 1);
Manu Abrahamc46b6562007-02-24 08:31:40 -0300237}
238
Manu Abrahamc46b6562007-02-24 08:31:40 -0300239
240static int stb6100_get_status(struct dvb_frontend *fe, u32 *status)
241{
242 int rc;
243 struct stb6100_state *state = fe->tuner_priv;
244
Manu Abrahamf14bfe92010-11-14 15:52:10 -0300245 rc = stb6100_read_reg(state, STB6100_LD);
246 if (rc < 0) {
247 dprintk(verbose, FE_ERROR, 1, "%s failed", __func__);
Manu Abrahamc46b6562007-02-24 08:31:40 -0300248 return rc;
Manu Abrahamf14bfe92010-11-14 15:52:10 -0300249 }
Manu Abrahamc46b6562007-02-24 08:31:40 -0300250 return (rc & STB6100_LD_LOCK) ? TUNER_STATUS_LOCKED : 0;
251}
252
253static int stb6100_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
254{
255 int rc;
256 u8 f;
Mauro Carvalho Chehabcffdbfe2015-11-11 16:52:46 -0200257 u32 bw;
Manu Abrahamc46b6562007-02-24 08:31:40 -0300258 struct stb6100_state *state = fe->tuner_priv;
259
Manu Abrahamf14bfe92010-11-14 15:52:10 -0300260 rc = stb6100_read_reg(state, STB6100_F);
261 if (rc < 0)
Manu Abrahamc46b6562007-02-24 08:31:40 -0300262 return rc;
263 f = rc & STB6100_F_F;
264
Mauro Carvalho Chehabcffdbfe2015-11-11 16:52:46 -0200265 bw = (f + 5) * 2000; /* x2 for ZIF */
Manu Abrahamc46b6562007-02-24 08:31:40 -0300266
Mauro Carvalho Chehabcffdbfe2015-11-11 16:52:46 -0200267 *bandwidth = state->bandwidth = bw * 1000;
Manu Abrahamc46b6562007-02-24 08:31:40 -0300268 dprintk(verbose, FE_DEBUG, 1, "bandwidth = %u Hz", state->bandwidth);
269 return 0;
270}
271
272static int stb6100_set_bandwidth(struct dvb_frontend *fe, u32 bandwidth)
273{
274 u32 tmp;
275 int rc;
276 struct stb6100_state *state = fe->tuner_priv;
277
Reinhard Nissl89693b72008-01-18 16:06:18 -0300278 dprintk(verbose, FE_DEBUG, 1, "set bandwidth to %u Hz", bandwidth);
Manu Abrahamc46b6562007-02-24 08:31:40 -0300279
Manu Abraham763fbaf2008-01-18 11:28:48 -0300280 bandwidth /= 2; /* ZIF */
Manu Abrahamc46b6562007-02-24 08:31:40 -0300281
Reinhard Nissl89693b72008-01-18 16:06:18 -0300282 if (bandwidth >= 36000000) /* F[4:0] BW/2 max =31+5=36 mhz for F=31 */
Manu Abrahamc46b6562007-02-24 08:31:40 -0300283 tmp = 31;
Reinhard Nissl89693b72008-01-18 16:06:18 -0300284 else if (bandwidth <= 5000000) /* bw/2 min = 5Mhz for F=0 */
Manu Abrahamc46b6562007-02-24 08:31:40 -0300285 tmp = 0;
286 else /* if 5 < bw/2 < 36 */
Reinhard Nissl6f6c2682008-01-21 16:15:14 -0300287 tmp = (bandwidth + 500000) / 1000000 - 5;
Manu Abrahamc46b6562007-02-24 08:31:40 -0300288
289 /* Turn on LPF bandwidth setting clock control,
290 * set bandwidth, wait 10ms, turn off.
291 */
Manu Abrahamf14bfe92010-11-14 15:52:10 -0300292 rc = stb6100_write_reg(state, STB6100_FCCK, 0x0d | STB6100_FCCK_FCCK);
293 if (rc < 0)
Manu Abrahamc46b6562007-02-24 08:31:40 -0300294 return rc;
Manu Abrahamf14bfe92010-11-14 15:52:10 -0300295 rc = stb6100_write_reg(state, STB6100_F, 0xc0 | tmp);
296 if (rc < 0)
Manu Abrahamc46b6562007-02-24 08:31:40 -0300297 return rc;
Manu Abrahamf14bfe92010-11-14 15:52:10 -0300298
299 msleep(5); /* This is dangerous as another (related) thread may start */
300
301 rc = stb6100_write_reg(state, STB6100_FCCK, 0x0d);
302 if (rc < 0)
Manu Abrahamc46b6562007-02-24 08:31:40 -0300303 return rc;
304
Manu Abrahamf14bfe92010-11-14 15:52:10 -0300305 msleep(10); /* This is dangerous as another (related) thread may start */
306
Manu Abrahamc46b6562007-02-24 08:31:40 -0300307 return 0;
308}
309
310static int stb6100_get_frequency(struct dvb_frontend *fe, u32 *frequency)
311{
312 int rc;
313 u32 nint, nfrac, fvco;
314 int psd2, odiv;
315 struct stb6100_state *state = fe->tuner_priv;
316 u8 regs[STB6100_NUMREGS];
317
Manu Abrahamf14bfe92010-11-14 15:52:10 -0300318 rc = stb6100_read_regs(state, regs);
319 if (rc < 0)
Manu Abrahamc46b6562007-02-24 08:31:40 -0300320 return rc;
321
322 odiv = (regs[STB6100_VCO] & STB6100_VCO_ODIV) >> STB6100_VCO_ODIV_SHIFT;
323 psd2 = (regs[STB6100_K] & STB6100_K_PSD2) >> STB6100_K_PSD2_SHIFT;
324 nint = regs[STB6100_NI];
325 nfrac = ((regs[STB6100_K] & STB6100_K_NF_MSB) << 8) | regs[STB6100_NF_LSB];
326 fvco = (nfrac * state->reference >> (9 - psd2)) + (nint * state->reference << psd2);
327 *frequency = state->frequency = fvco >> (odiv + 1);
328
329 dprintk(verbose, FE_DEBUG, 1,
330 "frequency = %u kHz, odiv = %u, psd2 = %u, fxtal = %u kHz, fvco = %u kHz, N(I) = %u, N(F) = %u",
331 state->frequency, odiv, psd2, state->reference, fvco, nint, nfrac);
332 return 0;
333}
334
335
336static int stb6100_set_frequency(struct dvb_frontend *fe, u32 frequency)
337{
338 int rc;
339 const struct stb6100_lkup *ptr;
340 struct stb6100_state *state = fe->tuner_priv;
Mauro Carvalho Chehab1e73fa52011-12-31 17:24:19 -0200341 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
Manu Abrahamc46b6562007-02-24 08:31:40 -0300342
343 u32 srate = 0, fvco, nint, nfrac;
344 u8 regs[STB6100_NUMREGS];
345 u8 g, psd2, odiv;
346
Manu Abrahamf14bfe92010-11-14 15:52:10 -0300347 dprintk(verbose, FE_DEBUG, 1, "Version 2010-8-14 13:51");
Manu Abraham3f400922008-10-26 18:28:52 -0300348
Mauro Carvalho Chehab51359862011-12-26 14:20:02 -0300349 if (fe->ops.get_frontend) {
Manu Abraham3f400922008-10-26 18:28:52 -0300350 dprintk(verbose, FE_DEBUG, 1, "Get frontend parameters");
Mauro Carvalho Chehab7e3e68b2016-02-04 12:58:30 -0200351 fe->ops.get_frontend(fe, p);
Manu Abrahamc46b6562007-02-24 08:31:40 -0300352 }
Mauro Carvalho Chehab1e73fa52011-12-31 17:24:19 -0200353 srate = p->symbol_rate;
Manu Abrahamc46b6562007-02-24 08:31:40 -0300354
Manu Abrahamf14bfe92010-11-14 15:52:10 -0300355 /* Set up tuner cleanly, LPF calibration on */
356 rc = stb6100_write_reg(state, STB6100_FCCK, 0x4d | STB6100_FCCK_FCCK);
357 if (rc < 0)
358 return rc; /* allow LPF calibration */
Ales Jurik20dafb32008-10-26 18:45:59 -0300359
Manu Abrahamf14bfe92010-11-14 15:52:10 -0300360 /* PLL Loop disabled, bias on, VCO on, synth on */
361 regs[STB6100_LPEN] = 0xeb;
362 rc = stb6100_write_reg(state, STB6100_LPEN, regs[STB6100_LPEN]);
363 if (rc < 0)
Ales Jurik20dafb32008-10-26 18:45:59 -0300364 return rc;
365
Manu Abrahamf14bfe92010-11-14 15:52:10 -0300366 /* Program the registers with their data values */
Manu Abrahamc46b6562007-02-24 08:31:40 -0300367
368 /* VCO divide ratio (LO divide ratio, VCO prescaler enable). */
369 if (frequency <= 1075000)
370 odiv = 1;
371 else
372 odiv = 0;
Manu Abrahamc46b6562007-02-24 08:31:40 -0300373
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300374 /* VCO enabled, search clock off as per LL3.7, 3.4.1 */
Manu Abrahamf14bfe92010-11-14 15:52:10 -0300375 regs[STB6100_VCO] = 0xe0 | (odiv << STB6100_VCO_ODIV_SHIFT);
Manu Abrahamc46b6562007-02-24 08:31:40 -0300376
377 /* OSM */
378 for (ptr = lkup;
379 (ptr->val_high != 0) && !CHKRANGE(frequency, ptr->val_low, ptr->val_high);
380 ptr++);
Manu Abrahamf14bfe92010-11-14 15:52:10 -0300381
Manu Abrahamc46b6562007-02-24 08:31:40 -0300382 if (ptr->val_high == 0) {
383 printk(KERN_ERR "%s: frequency out of range: %u kHz\n", __func__, frequency);
384 return -EINVAL;
385 }
386 regs[STB6100_VCO] = (regs[STB6100_VCO] & ~STB6100_VCO_OSM) | ptr->reg;
Manu Abrahamf14bfe92010-11-14 15:52:10 -0300387 rc = stb6100_write_reg(state, STB6100_VCO, regs[STB6100_VCO]);
388 if (rc < 0)
389 return rc;
Manu Abrahamc46b6562007-02-24 08:31:40 -0300390
Manu Abrahamf14bfe92010-11-14 15:52:10 -0300391 if ((frequency > 1075000) && (frequency <= 1325000))
392 psd2 = 0;
393 else
394 psd2 = 1;
Manu Abrahamc46b6562007-02-24 08:31:40 -0300395 /* F(VCO) = F(LO) * (ODIV == 0 ? 2 : 4) */
396 fvco = frequency << (1 + odiv);
397 /* N(I) = floor(f(VCO) / (f(XTAL) * (PSD2 ? 2 : 1))) */
398 nint = fvco / (state->reference << psd2);
399 /* N(F) = round(f(VCO) / f(XTAL) * (PSD2 ? 2 : 1) - N(I)) * 2 ^ 9 */
Julia Lawall75b697f2009-08-01 16:48:41 -0300400 nfrac = DIV_ROUND_CLOSEST((fvco - (nint * state->reference << psd2))
Manu Abrahamf14bfe92010-11-14 15:52:10 -0300401 << (9 - psd2), state->reference);
402
403 /* NI */
404 regs[STB6100_NI] = nint;
405 rc = stb6100_write_reg(state, STB6100_NI, regs[STB6100_NI]);
406 if (rc < 0)
407 return rc;
408
409 /* NF */
410 regs[STB6100_NF_LSB] = nfrac;
411 rc = stb6100_write_reg(state, STB6100_NF_LSB, regs[STB6100_NF_LSB]);
412 if (rc < 0)
413 return rc;
414
415 /* K */
416 regs[STB6100_K] = (0x38 & ~STB6100_K_PSD2) | (psd2 << STB6100_K_PSD2_SHIFT);
417 regs[STB6100_K] = (regs[STB6100_K] & ~STB6100_K_NF_MSB) | ((nfrac >> 8) & STB6100_K_NF_MSB);
418 rc = stb6100_write_reg(state, STB6100_K, regs[STB6100_K]);
419 if (rc < 0)
420 return rc;
421
422 /* G Baseband gain. */
423 if (srate >= 15000000)
424 g = 9; /* +4 dB */
425 else if (srate >= 5000000)
426 g = 11; /* +8 dB */
427 else
428 g = 14; /* +14 dB */
429
430 regs[STB6100_G] = (0x10 & ~STB6100_G_G) | g;
431 regs[STB6100_G] &= ~STB6100_G_GCT; /* mask GCT */
432 regs[STB6100_G] |= (1 << 5); /* 2Vp-p Mode */
433 rc = stb6100_write_reg(state, STB6100_G, regs[STB6100_G]);
434 if (rc < 0)
435 return rc;
436
437 /* F we don't write as it is set up in BW set */
438
439 /* DLB set DC servo loop BW to 160Hz (LLA 3.8 / 2.1) */
440 regs[STB6100_DLB] = 0xcc;
441 rc = stb6100_write_reg(state, STB6100_DLB, regs[STB6100_DLB]);
442 if (rc < 0)
443 return rc;
444
Manu Abrahamc46b6562007-02-24 08:31:40 -0300445 dprintk(verbose, FE_DEBUG, 1,
446 "frequency = %u, srate = %u, g = %u, odiv = %u, psd2 = %u, fxtal = %u, osm = %u, fvco = %u, N(I) = %u, N(F) = %u",
447 frequency, srate, (unsigned int)g, (unsigned int)odiv,
448 (unsigned int)psd2, state->reference,
449 ptr->reg, fvco, nint, nfrac);
Manu Abrahamc46b6562007-02-24 08:31:40 -0300450
Manu Abrahamf14bfe92010-11-14 15:52:10 -0300451 /* Set up the test registers */
452 regs[STB6100_TEST1] = 0x8f;
453 rc = stb6100_write_reg(state, STB6100_TEST1, regs[STB6100_TEST1]);
454 if (rc < 0)
455 return rc;
456 regs[STB6100_TEST3] = 0xde;
457 rc = stb6100_write_reg(state, STB6100_TEST3, regs[STB6100_TEST3]);
458 if (rc < 0)
459 return rc;
460
461 /* Bring up tuner according to LLA 3.7 3.4.1, step 2 */
462 regs[STB6100_LPEN] = 0xfb; /* PLL Loop enabled, bias on, VCO on, synth on */
463 rc = stb6100_write_reg(state, STB6100_LPEN, regs[STB6100_LPEN]);
464 if (rc < 0)
Manu Abrahamc46b6562007-02-24 08:31:40 -0300465 return rc;
466
Ales Jurik20dafb32008-10-26 18:45:59 -0300467 msleep(2);
Manu Abrahamc46b6562007-02-24 08:31:40 -0300468
Manu Abrahamf14bfe92010-11-14 15:52:10 -0300469 /* Bring up tuner according to LLA 3.7 3.4.1, step 3 */
Manu Abrahamc46b6562007-02-24 08:31:40 -0300470 regs[STB6100_VCO] &= ~STB6100_VCO_OCK; /* VCO fast search */
Manu Abrahamf14bfe92010-11-14 15:52:10 -0300471 rc = stb6100_write_reg(state, STB6100_VCO, regs[STB6100_VCO]);
472 if (rc < 0)
Manu Abrahamc46b6562007-02-24 08:31:40 -0300473 return rc;
474
Manu Abrahamf14bfe92010-11-14 15:52:10 -0300475 msleep(10); /* This is dangerous as another (related) thread may start */ /* wait for LO to lock */
476
Manu Abrahamc46b6562007-02-24 08:31:40 -0300477 regs[STB6100_VCO] &= ~STB6100_VCO_OSCH; /* vco search disabled */
478 regs[STB6100_VCO] |= STB6100_VCO_OCK; /* search clock off */
Manu Abrahamf14bfe92010-11-14 15:52:10 -0300479 rc = stb6100_write_reg(state, STB6100_VCO, regs[STB6100_VCO]);
480 if (rc < 0)
Manu Abrahamc46b6562007-02-24 08:31:40 -0300481 return rc;
482
Manu Abrahamf14bfe92010-11-14 15:52:10 -0300483 rc = stb6100_write_reg(state, STB6100_FCCK, 0x0d);
484 if (rc < 0)
485 return rc; /* Stop LPF calibration */
Manu Abraham7e4e8c52007-10-19 06:27:56 -0300486
Manu Abrahamf14bfe92010-11-14 15:52:10 -0300487 msleep(10); /* This is dangerous as another (related) thread may start */
488 /* wait for stabilisation, (should not be necessary) */
Manu Abrahamc46b6562007-02-24 08:31:40 -0300489 return 0;
490}
491
492static int stb6100_sleep(struct dvb_frontend *fe)
493{
494 /* TODO: power down */
495 return 0;
496}
497
498static int stb6100_init(struct dvb_frontend *fe)
499{
500 struct stb6100_state *state = fe->tuner_priv;
Mauro Carvalho Chehab2184e252015-11-11 15:52:25 -0200501 int refclk = 27000000; /* Hz */
Manu Abrahamc46b6562007-02-24 08:31:40 -0300502
Mauro Carvalho Chehab2184e252015-11-11 15:52:25 -0200503 /*
504 * iqsense = 1
505 * tunerstep = 125000
506 */
Mauro Carvalho Chehab65f0f682015-11-11 18:42:00 -0200507 state->bandwidth = 36000000; /* Hz */
Mauro Carvalho Chehab2184e252015-11-11 15:52:25 -0200508 state->reference = refclk / 1000; /* kHz */
Manu Abrahamc46b6562007-02-24 08:31:40 -0300509
Manu Abrahamf14bfe92010-11-14 15:52:10 -0300510 /* Set default bandwidth. Modified, PN 13-May-10 */
511 return 0;
Manu Abrahamc46b6562007-02-24 08:31:40 -0300512}
513
Mauro Carvalho Chehab65f0f682015-11-11 18:42:00 -0200514static int stb6100_set_params(struct dvb_frontend *fe)
Manu Abrahamc46b6562007-02-24 08:31:40 -0300515{
Mauro Carvalho Chehab65f0f682015-11-11 18:42:00 -0200516 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Manu Abrahamc46b6562007-02-24 08:31:40 -0300517
Mauro Carvalho Chehab65f0f682015-11-11 18:42:00 -0200518 if (c->frequency > 0)
519 stb6100_set_frequency(fe, c->frequency);
Manu Abrahamc46b6562007-02-24 08:31:40 -0300520
Mauro Carvalho Chehab65f0f682015-11-11 18:42:00 -0200521 if (c->bandwidth_hz > 0)
522 stb6100_set_bandwidth(fe, c->bandwidth_hz);
Manu Abrahamc46b6562007-02-24 08:31:40 -0300523
524 return 0;
525}
526
Julia Lawall14c4bf32016-09-11 11:44:12 -0300527static const struct dvb_tuner_ops stb6100_ops = {
Manu Abrahamc46b6562007-02-24 08:31:40 -0300528 .info = {
529 .name = "STB6100 Silicon Tuner",
530 .frequency_min = 950000,
531 .frequency_max = 2150000,
532 .frequency_step = 0,
533 },
534
535 .init = stb6100_init,
536 .sleep = stb6100_sleep,
537 .get_status = stb6100_get_status,
Mauro Carvalho Chehab65f0f682015-11-11 18:42:00 -0200538 .set_params = stb6100_set_params,
539 .get_frequency = stb6100_get_frequency,
540 .get_bandwidth = stb6100_get_bandwidth,
Mauro Carvalho Chehabf2709c22016-11-18 20:30:51 -0200541 .release = stb6100_release
Manu Abrahamc46b6562007-02-24 08:31:40 -0300542};
543
544struct dvb_frontend *stb6100_attach(struct dvb_frontend *fe,
lawrence rust2e4e98e2010-08-25 09:50:20 -0300545 const struct stb6100_config *config,
Manu Abrahamc46b6562007-02-24 08:31:40 -0300546 struct i2c_adapter *i2c)
547{
548 struct stb6100_state *state = NULL;
549
Marko Schluesslera18d4312007-09-21 18:13:30 -0300550 state = kzalloc(sizeof (struct stb6100_state), GFP_KERNEL);
Peter Senna Tschudine4533e62012-09-12 08:56:00 -0300551 if (!state)
552 return NULL;
Manu Abrahamc46b6562007-02-24 08:31:40 -0300553
554 state->config = config;
555 state->i2c = i2c;
556 state->frontend = fe;
Marko Schluessler3e3263e2007-09-21 18:08:01 -0300557 state->reference = config->refclock / 1000; /* kHz */
Manu Abrahamc46b6562007-02-24 08:31:40 -0300558 fe->tuner_priv = state;
559 fe->ops.tuner_ops = stb6100_ops;
560
Marko Schluessler3e3263e2007-09-21 18:08:01 -0300561 printk("%s: Attaching STB6100 \n", __func__);
Manu Abrahamc46b6562007-02-24 08:31:40 -0300562 return fe;
Manu Abrahamc46b6562007-02-24 08:31:40 -0300563}
564
Mauro Carvalho Chehabf2709c22016-11-18 20:30:51 -0200565static void stb6100_release(struct dvb_frontend *fe)
566{
567 struct stb6100_state *state = fe->tuner_priv;
568
569 fe->tuner_priv = NULL;
570 kfree(state);
571}
572
Manu Abrahamc46b6562007-02-24 08:31:40 -0300573EXPORT_SYMBOL(stb6100_attach);
574MODULE_PARM_DESC(verbose, "Set Verbosity level");
575
576MODULE_AUTHOR("Manu Abraham");
577MODULE_DESCRIPTION("STB6100 Silicon tuner");
578MODULE_LICENSE("GPL");