Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 1 | /* |
| 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 Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 25 | #include <linux/slab.h> |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 26 | #include <linux/string.h> |
| 27 | |
| 28 | #include "dvb_frontend.h" |
| 29 | #include "stb6100.h" |
| 30 | |
| 31 | static unsigned int verbose; |
| 32 | module_param(verbose, int, 0644); |
| 33 | |
| 34 | |
| 35 | #define FE_ERROR 0 |
| 36 | #define FE_NOTICE 1 |
| 37 | #define FE_INFO 2 |
| 38 | #define FE_DEBUG 3 |
| 39 | |
| 40 | #define dprintk(x, y, z, format, arg...) do { \ |
| 41 | if (z) { \ |
| 42 | if ((x > FE_ERROR) && (x > y)) \ |
| 43 | printk(KERN_ERR "%s: " format "\n", __func__ , ##arg); \ |
| 44 | else if ((x > FE_NOTICE) && (x > y)) \ |
| 45 | printk(KERN_NOTICE "%s: " format "\n", __func__ , ##arg); \ |
| 46 | else if ((x > FE_INFO) && (x > y)) \ |
| 47 | printk(KERN_INFO "%s: " format "\n", __func__ , ##arg); \ |
| 48 | else if ((x > FE_DEBUG) && (x > y)) \ |
| 49 | printk(KERN_DEBUG "%s: " format "\n", __func__ , ##arg); \ |
| 50 | } else { \ |
| 51 | if (x > y) \ |
| 52 | printk(format, ##arg); \ |
| 53 | } \ |
Manu Abraham | f14bfe9 | 2010-11-14 15:52:10 -0300 | [diff] [blame] | 54 | } while (0) |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 55 | |
| 56 | struct stb6100_lkup { |
| 57 | u32 val_low; |
| 58 | u32 val_high; |
| 59 | u8 reg; |
| 60 | }; |
| 61 | |
| 62 | static int stb6100_release(struct dvb_frontend *fe); |
| 63 | |
| 64 | static const struct stb6100_lkup lkup[] = { |
| 65 | { 0, 950000, 0x0a }, |
| 66 | { 950000, 1000000, 0x0a }, |
| 67 | { 1000000, 1075000, 0x0c }, |
| 68 | { 1075000, 1200000, 0x00 }, |
| 69 | { 1200000, 1300000, 0x01 }, |
| 70 | { 1300000, 1370000, 0x02 }, |
| 71 | { 1370000, 1470000, 0x04 }, |
| 72 | { 1470000, 1530000, 0x05 }, |
| 73 | { 1530000, 1650000, 0x06 }, |
| 74 | { 1650000, 1800000, 0x08 }, |
| 75 | { 1800000, 1950000, 0x0a }, |
| 76 | { 1950000, 2150000, 0x0c }, |
| 77 | { 2150000, 9999999, 0x0c }, |
| 78 | { 0, 0, 0x00 } |
| 79 | }; |
| 80 | |
| 81 | /* Register names for easy debugging. */ |
| 82 | static const char *stb6100_regnames[] = { |
| 83 | [STB6100_LD] = "LD", |
| 84 | [STB6100_VCO] = "VCO", |
| 85 | [STB6100_NI] = "NI", |
| 86 | [STB6100_NF_LSB] = "NF", |
| 87 | [STB6100_K] = "K", |
| 88 | [STB6100_G] = "G", |
| 89 | [STB6100_F] = "F", |
| 90 | [STB6100_DLB] = "DLB", |
| 91 | [STB6100_TEST1] = "TEST1", |
| 92 | [STB6100_FCCK] = "FCCK", |
| 93 | [STB6100_LPEN] = "LPEN", |
| 94 | [STB6100_TEST3] = "TEST3", |
| 95 | }; |
| 96 | |
| 97 | /* Template for normalisation, i.e. setting unused or undocumented |
| 98 | * bits as required according to the documentation. |
| 99 | */ |
| 100 | struct stb6100_regmask { |
| 101 | u8 mask; |
| 102 | u8 set; |
| 103 | }; |
| 104 | |
| 105 | static const struct stb6100_regmask stb6100_template[] = { |
| 106 | [STB6100_LD] = { 0xff, 0x00 }, |
| 107 | [STB6100_VCO] = { 0xff, 0x00 }, |
| 108 | [STB6100_NI] = { 0xff, 0x00 }, |
| 109 | [STB6100_NF_LSB] = { 0xff, 0x00 }, |
| 110 | [STB6100_K] = { 0xc7, 0x38 }, |
| 111 | [STB6100_G] = { 0xef, 0x10 }, |
| 112 | [STB6100_F] = { 0x1f, 0xc0 }, |
| 113 | [STB6100_DLB] = { 0x38, 0xc4 }, |
| 114 | [STB6100_TEST1] = { 0x00, 0x8f }, |
| 115 | [STB6100_FCCK] = { 0x40, 0x0d }, |
| 116 | [STB6100_LPEN] = { 0xf0, 0x0b }, |
| 117 | [STB6100_TEST3] = { 0x00, 0xde }, |
| 118 | }; |
| 119 | |
Mauro Carvalho Chehab | a931910 | 2010-11-22 13:22:37 -0300 | [diff] [blame] | 120 | /* |
| 121 | * Currently unused. Some boards might need it in the future |
| 122 | */ |
| 123 | static inline void stb6100_normalise_regs(u8 regs[]) |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 124 | { |
| 125 | int i; |
| 126 | |
| 127 | for (i = 0; i < STB6100_NUMREGS; i++) |
| 128 | regs[i] = (regs[i] & stb6100_template[i].mask) | stb6100_template[i].set; |
| 129 | } |
| 130 | |
| 131 | static int stb6100_read_regs(struct stb6100_state *state, u8 regs[]) |
| 132 | { |
| 133 | int rc; |
| 134 | struct i2c_msg msg = { |
| 135 | .addr = state->config->tuner_address, |
| 136 | .flags = I2C_M_RD, |
| 137 | .buf = regs, |
| 138 | .len = STB6100_NUMREGS |
| 139 | }; |
| 140 | |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 141 | rc = i2c_transfer(state->i2c, &msg, 1); |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 142 | if (unlikely(rc != 1)) { |
| 143 | dprintk(verbose, FE_ERROR, 1, "Read (0x%x) err, rc=[%d]", |
| 144 | state->config->tuner_address, rc); |
| 145 | |
| 146 | return -EREMOTEIO; |
| 147 | } |
| 148 | if (unlikely(verbose > FE_DEBUG)) { |
| 149 | int i; |
| 150 | |
| 151 | dprintk(verbose, FE_DEBUG, 1, " Read from 0x%02x", state->config->tuner_address); |
| 152 | for (i = 0; i < STB6100_NUMREGS; i++) |
| 153 | dprintk(verbose, FE_DEBUG, 1, " %s: 0x%02x", stb6100_regnames[i], regs[i]); |
| 154 | } |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | static int stb6100_read_reg(struct stb6100_state *state, u8 reg) |
| 159 | { |
| 160 | u8 regs[STB6100_NUMREGS]; |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 161 | |
Manu Abraham | f14bfe9 | 2010-11-14 15:52:10 -0300 | [diff] [blame] | 162 | struct i2c_msg msg = { |
| 163 | .addr = state->config->tuner_address + reg, |
| 164 | .flags = I2C_M_RD, |
| 165 | .buf = regs, |
| 166 | .len = 1 |
| 167 | }; |
| 168 | |
Hans Verkuil | fdf07b0 | 2012-04-20 08:04:48 -0300 | [diff] [blame] | 169 | i2c_transfer(state->i2c, &msg, 1); |
Manu Abraham | f14bfe9 | 2010-11-14 15:52:10 -0300 | [diff] [blame] | 170 | |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 171 | if (unlikely(reg >= STB6100_NUMREGS)) { |
| 172 | dprintk(verbose, FE_ERROR, 1, "Invalid register offset 0x%x", reg); |
| 173 | return -EINVAL; |
| 174 | } |
Manu Abraham | f14bfe9 | 2010-11-14 15:52:10 -0300 | [diff] [blame] | 175 | if (unlikely(verbose > FE_DEBUG)) { |
| 176 | dprintk(verbose, FE_DEBUG, 1, " Read from 0x%02x", state->config->tuner_address); |
| 177 | dprintk(verbose, FE_DEBUG, 1, " %s: 0x%02x", stb6100_regnames[reg], regs[0]); |
| 178 | } |
| 179 | |
| 180 | return (unsigned int)regs[0]; |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | static int stb6100_write_reg_range(struct stb6100_state *state, u8 buf[], int start, int len) |
| 184 | { |
| 185 | int rc; |
| 186 | u8 cmdbuf[len + 1]; |
| 187 | struct i2c_msg msg = { |
| 188 | .addr = state->config->tuner_address, |
| 189 | .flags = 0, |
| 190 | .buf = cmdbuf, |
| 191 | .len = len + 1 |
| 192 | }; |
| 193 | |
| 194 | if (unlikely(start < 1 || start + len > STB6100_NUMREGS)) { |
| 195 | dprintk(verbose, FE_ERROR, 1, "Invalid register range %d:%d", |
| 196 | start, len); |
| 197 | return -EINVAL; |
| 198 | } |
| 199 | memcpy(&cmdbuf[1], buf, len); |
| 200 | cmdbuf[0] = start; |
| 201 | |
| 202 | if (unlikely(verbose > FE_DEBUG)) { |
| 203 | int i; |
| 204 | |
| 205 | dprintk(verbose, FE_DEBUG, 1, " Write @ 0x%02x: [%d:%d]", state->config->tuner_address, start, len); |
| 206 | for (i = 0; i < len; i++) |
| 207 | dprintk(verbose, FE_DEBUG, 1, " %s: 0x%02x", stb6100_regnames[start + i], buf[i]); |
| 208 | } |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 209 | rc = i2c_transfer(state->i2c, &msg, 1); |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 210 | if (unlikely(rc != 1)) { |
| 211 | dprintk(verbose, FE_ERROR, 1, "(0x%x) write err [%d:%d], rc=[%d]", |
| 212 | (unsigned int)state->config->tuner_address, start, len, rc); |
| 213 | return -EREMOTEIO; |
| 214 | } |
| 215 | return 0; |
| 216 | } |
| 217 | |
| 218 | static int stb6100_write_reg(struct stb6100_state *state, u8 reg, u8 data) |
| 219 | { |
| 220 | if (unlikely(reg >= STB6100_NUMREGS)) { |
| 221 | dprintk(verbose, FE_ERROR, 1, "Invalid register offset 0x%x", reg); |
| 222 | return -EREMOTEIO; |
| 223 | } |
| 224 | data = (data & stb6100_template[reg].mask) | stb6100_template[reg].set; |
| 225 | return stb6100_write_reg_range(state, &data, reg, 1); |
| 226 | } |
| 227 | |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 228 | |
| 229 | static int stb6100_get_status(struct dvb_frontend *fe, u32 *status) |
| 230 | { |
| 231 | int rc; |
| 232 | struct stb6100_state *state = fe->tuner_priv; |
| 233 | |
Manu Abraham | f14bfe9 | 2010-11-14 15:52:10 -0300 | [diff] [blame] | 234 | rc = stb6100_read_reg(state, STB6100_LD); |
| 235 | if (rc < 0) { |
| 236 | dprintk(verbose, FE_ERROR, 1, "%s failed", __func__); |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 237 | return rc; |
Manu Abraham | f14bfe9 | 2010-11-14 15:52:10 -0300 | [diff] [blame] | 238 | } |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 239 | return (rc & STB6100_LD_LOCK) ? TUNER_STATUS_LOCKED : 0; |
| 240 | } |
| 241 | |
| 242 | static int stb6100_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth) |
| 243 | { |
| 244 | int rc; |
| 245 | u8 f; |
| 246 | struct stb6100_state *state = fe->tuner_priv; |
| 247 | |
Manu Abraham | f14bfe9 | 2010-11-14 15:52:10 -0300 | [diff] [blame] | 248 | rc = stb6100_read_reg(state, STB6100_F); |
| 249 | if (rc < 0) |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 250 | return rc; |
| 251 | f = rc & STB6100_F_F; |
| 252 | |
| 253 | state->status.bandwidth = (f + 5) * 2000; /* x2 for ZIF */ |
| 254 | |
| 255 | *bandwidth = state->bandwidth = state->status.bandwidth * 1000; |
| 256 | dprintk(verbose, FE_DEBUG, 1, "bandwidth = %u Hz", state->bandwidth); |
| 257 | return 0; |
| 258 | } |
| 259 | |
| 260 | static int stb6100_set_bandwidth(struct dvb_frontend *fe, u32 bandwidth) |
| 261 | { |
| 262 | u32 tmp; |
| 263 | int rc; |
| 264 | struct stb6100_state *state = fe->tuner_priv; |
| 265 | |
Reinhard Nissl | 89693b7 | 2008-01-18 16:06:18 -0300 | [diff] [blame] | 266 | dprintk(verbose, FE_DEBUG, 1, "set bandwidth to %u Hz", bandwidth); |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 267 | |
Manu Abraham | 763fbaf | 2008-01-18 11:28:48 -0300 | [diff] [blame] | 268 | bandwidth /= 2; /* ZIF */ |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 269 | |
Reinhard Nissl | 89693b7 | 2008-01-18 16:06:18 -0300 | [diff] [blame] | 270 | if (bandwidth >= 36000000) /* F[4:0] BW/2 max =31+5=36 mhz for F=31 */ |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 271 | tmp = 31; |
Reinhard Nissl | 89693b7 | 2008-01-18 16:06:18 -0300 | [diff] [blame] | 272 | else if (bandwidth <= 5000000) /* bw/2 min = 5Mhz for F=0 */ |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 273 | tmp = 0; |
| 274 | else /* if 5 < bw/2 < 36 */ |
Reinhard Nissl | 6f6c268 | 2008-01-21 16:15:14 -0300 | [diff] [blame] | 275 | tmp = (bandwidth + 500000) / 1000000 - 5; |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 276 | |
| 277 | /* Turn on LPF bandwidth setting clock control, |
| 278 | * set bandwidth, wait 10ms, turn off. |
| 279 | */ |
Manu Abraham | f14bfe9 | 2010-11-14 15:52:10 -0300 | [diff] [blame] | 280 | rc = stb6100_write_reg(state, STB6100_FCCK, 0x0d | STB6100_FCCK_FCCK); |
| 281 | if (rc < 0) |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 282 | return rc; |
Manu Abraham | f14bfe9 | 2010-11-14 15:52:10 -0300 | [diff] [blame] | 283 | rc = stb6100_write_reg(state, STB6100_F, 0xc0 | tmp); |
| 284 | if (rc < 0) |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 285 | return rc; |
Manu Abraham | f14bfe9 | 2010-11-14 15:52:10 -0300 | [diff] [blame] | 286 | |
| 287 | msleep(5); /* This is dangerous as another (related) thread may start */ |
| 288 | |
| 289 | rc = stb6100_write_reg(state, STB6100_FCCK, 0x0d); |
| 290 | if (rc < 0) |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 291 | return rc; |
| 292 | |
Manu Abraham | f14bfe9 | 2010-11-14 15:52:10 -0300 | [diff] [blame] | 293 | msleep(10); /* This is dangerous as another (related) thread may start */ |
| 294 | |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 295 | return 0; |
| 296 | } |
| 297 | |
| 298 | static int stb6100_get_frequency(struct dvb_frontend *fe, u32 *frequency) |
| 299 | { |
| 300 | int rc; |
| 301 | u32 nint, nfrac, fvco; |
| 302 | int psd2, odiv; |
| 303 | struct stb6100_state *state = fe->tuner_priv; |
| 304 | u8 regs[STB6100_NUMREGS]; |
| 305 | |
Manu Abraham | f14bfe9 | 2010-11-14 15:52:10 -0300 | [diff] [blame] | 306 | rc = stb6100_read_regs(state, regs); |
| 307 | if (rc < 0) |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 308 | return rc; |
| 309 | |
| 310 | odiv = (regs[STB6100_VCO] & STB6100_VCO_ODIV) >> STB6100_VCO_ODIV_SHIFT; |
| 311 | psd2 = (regs[STB6100_K] & STB6100_K_PSD2) >> STB6100_K_PSD2_SHIFT; |
| 312 | nint = regs[STB6100_NI]; |
| 313 | nfrac = ((regs[STB6100_K] & STB6100_K_NF_MSB) << 8) | regs[STB6100_NF_LSB]; |
| 314 | fvco = (nfrac * state->reference >> (9 - psd2)) + (nint * state->reference << psd2); |
| 315 | *frequency = state->frequency = fvco >> (odiv + 1); |
| 316 | |
| 317 | dprintk(verbose, FE_DEBUG, 1, |
| 318 | "frequency = %u kHz, odiv = %u, psd2 = %u, fxtal = %u kHz, fvco = %u kHz, N(I) = %u, N(F) = %u", |
| 319 | state->frequency, odiv, psd2, state->reference, fvco, nint, nfrac); |
| 320 | return 0; |
| 321 | } |
| 322 | |
| 323 | |
| 324 | static int stb6100_set_frequency(struct dvb_frontend *fe, u32 frequency) |
| 325 | { |
| 326 | int rc; |
| 327 | const struct stb6100_lkup *ptr; |
| 328 | struct stb6100_state *state = fe->tuner_priv; |
Mauro Carvalho Chehab | 1e73fa5 | 2011-12-31 17:24:19 -0200 | [diff] [blame] | 329 | struct dtv_frontend_properties *p = &fe->dtv_property_cache; |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 330 | |
| 331 | u32 srate = 0, fvco, nint, nfrac; |
| 332 | u8 regs[STB6100_NUMREGS]; |
| 333 | u8 g, psd2, odiv; |
| 334 | |
Manu Abraham | f14bfe9 | 2010-11-14 15:52:10 -0300 | [diff] [blame] | 335 | dprintk(verbose, FE_DEBUG, 1, "Version 2010-8-14 13:51"); |
Manu Abraham | 3f40092 | 2008-10-26 18:28:52 -0300 | [diff] [blame] | 336 | |
Mauro Carvalho Chehab | 5135986 | 2011-12-26 14:20:02 -0300 | [diff] [blame] | 337 | if (fe->ops.get_frontend) { |
Manu Abraham | 3f40092 | 2008-10-26 18:28:52 -0300 | [diff] [blame] | 338 | dprintk(verbose, FE_DEBUG, 1, "Get frontend parameters"); |
Mauro Carvalho Chehab | 7c61d80 | 2011-12-30 11:30:21 -0300 | [diff] [blame] | 339 | fe->ops.get_frontend(fe); |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 340 | } |
Mauro Carvalho Chehab | 1e73fa5 | 2011-12-31 17:24:19 -0200 | [diff] [blame] | 341 | srate = p->symbol_rate; |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 342 | |
Manu Abraham | f14bfe9 | 2010-11-14 15:52:10 -0300 | [diff] [blame] | 343 | /* Set up tuner cleanly, LPF calibration on */ |
| 344 | rc = stb6100_write_reg(state, STB6100_FCCK, 0x4d | STB6100_FCCK_FCCK); |
| 345 | if (rc < 0) |
| 346 | return rc; /* allow LPF calibration */ |
Ales Jurik | 20dafb3 | 2008-10-26 18:45:59 -0300 | [diff] [blame] | 347 | |
Manu Abraham | f14bfe9 | 2010-11-14 15:52:10 -0300 | [diff] [blame] | 348 | /* PLL Loop disabled, bias on, VCO on, synth on */ |
| 349 | regs[STB6100_LPEN] = 0xeb; |
| 350 | rc = stb6100_write_reg(state, STB6100_LPEN, regs[STB6100_LPEN]); |
| 351 | if (rc < 0) |
Ales Jurik | 20dafb3 | 2008-10-26 18:45:59 -0300 | [diff] [blame] | 352 | return rc; |
| 353 | |
Manu Abraham | f14bfe9 | 2010-11-14 15:52:10 -0300 | [diff] [blame] | 354 | /* Program the registers with their data values */ |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 355 | |
| 356 | /* VCO divide ratio (LO divide ratio, VCO prescaler enable). */ |
| 357 | if (frequency <= 1075000) |
| 358 | odiv = 1; |
| 359 | else |
| 360 | odiv = 0; |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 361 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 362 | /* VCO enabled, search clock off as per LL3.7, 3.4.1 */ |
Manu Abraham | f14bfe9 | 2010-11-14 15:52:10 -0300 | [diff] [blame] | 363 | regs[STB6100_VCO] = 0xe0 | (odiv << STB6100_VCO_ODIV_SHIFT); |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 364 | |
| 365 | /* OSM */ |
| 366 | for (ptr = lkup; |
| 367 | (ptr->val_high != 0) && !CHKRANGE(frequency, ptr->val_low, ptr->val_high); |
| 368 | ptr++); |
Manu Abraham | f14bfe9 | 2010-11-14 15:52:10 -0300 | [diff] [blame] | 369 | |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 370 | if (ptr->val_high == 0) { |
| 371 | printk(KERN_ERR "%s: frequency out of range: %u kHz\n", __func__, frequency); |
| 372 | return -EINVAL; |
| 373 | } |
| 374 | regs[STB6100_VCO] = (regs[STB6100_VCO] & ~STB6100_VCO_OSM) | ptr->reg; |
Manu Abraham | f14bfe9 | 2010-11-14 15:52:10 -0300 | [diff] [blame] | 375 | rc = stb6100_write_reg(state, STB6100_VCO, regs[STB6100_VCO]); |
| 376 | if (rc < 0) |
| 377 | return rc; |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 378 | |
Manu Abraham | f14bfe9 | 2010-11-14 15:52:10 -0300 | [diff] [blame] | 379 | if ((frequency > 1075000) && (frequency <= 1325000)) |
| 380 | psd2 = 0; |
| 381 | else |
| 382 | psd2 = 1; |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 383 | /* F(VCO) = F(LO) * (ODIV == 0 ? 2 : 4) */ |
| 384 | fvco = frequency << (1 + odiv); |
| 385 | /* N(I) = floor(f(VCO) / (f(XTAL) * (PSD2 ? 2 : 1))) */ |
| 386 | nint = fvco / (state->reference << psd2); |
| 387 | /* N(F) = round(f(VCO) / f(XTAL) * (PSD2 ? 2 : 1) - N(I)) * 2 ^ 9 */ |
Julia Lawall | 75b697f | 2009-08-01 16:48:41 -0300 | [diff] [blame] | 388 | nfrac = DIV_ROUND_CLOSEST((fvco - (nint * state->reference << psd2)) |
Manu Abraham | f14bfe9 | 2010-11-14 15:52:10 -0300 | [diff] [blame] | 389 | << (9 - psd2), state->reference); |
| 390 | |
| 391 | /* NI */ |
| 392 | regs[STB6100_NI] = nint; |
| 393 | rc = stb6100_write_reg(state, STB6100_NI, regs[STB6100_NI]); |
| 394 | if (rc < 0) |
| 395 | return rc; |
| 396 | |
| 397 | /* NF */ |
| 398 | regs[STB6100_NF_LSB] = nfrac; |
| 399 | rc = stb6100_write_reg(state, STB6100_NF_LSB, regs[STB6100_NF_LSB]); |
| 400 | if (rc < 0) |
| 401 | return rc; |
| 402 | |
| 403 | /* K */ |
| 404 | regs[STB6100_K] = (0x38 & ~STB6100_K_PSD2) | (psd2 << STB6100_K_PSD2_SHIFT); |
| 405 | regs[STB6100_K] = (regs[STB6100_K] & ~STB6100_K_NF_MSB) | ((nfrac >> 8) & STB6100_K_NF_MSB); |
| 406 | rc = stb6100_write_reg(state, STB6100_K, regs[STB6100_K]); |
| 407 | if (rc < 0) |
| 408 | return rc; |
| 409 | |
| 410 | /* G Baseband gain. */ |
| 411 | if (srate >= 15000000) |
| 412 | g = 9; /* +4 dB */ |
| 413 | else if (srate >= 5000000) |
| 414 | g = 11; /* +8 dB */ |
| 415 | else |
| 416 | g = 14; /* +14 dB */ |
| 417 | |
| 418 | regs[STB6100_G] = (0x10 & ~STB6100_G_G) | g; |
| 419 | regs[STB6100_G] &= ~STB6100_G_GCT; /* mask GCT */ |
| 420 | regs[STB6100_G] |= (1 << 5); /* 2Vp-p Mode */ |
| 421 | rc = stb6100_write_reg(state, STB6100_G, regs[STB6100_G]); |
| 422 | if (rc < 0) |
| 423 | return rc; |
| 424 | |
| 425 | /* F we don't write as it is set up in BW set */ |
| 426 | |
| 427 | /* DLB set DC servo loop BW to 160Hz (LLA 3.8 / 2.1) */ |
| 428 | regs[STB6100_DLB] = 0xcc; |
| 429 | rc = stb6100_write_reg(state, STB6100_DLB, regs[STB6100_DLB]); |
| 430 | if (rc < 0) |
| 431 | return rc; |
| 432 | |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 433 | dprintk(verbose, FE_DEBUG, 1, |
| 434 | "frequency = %u, srate = %u, g = %u, odiv = %u, psd2 = %u, fxtal = %u, osm = %u, fvco = %u, N(I) = %u, N(F) = %u", |
| 435 | frequency, srate, (unsigned int)g, (unsigned int)odiv, |
| 436 | (unsigned int)psd2, state->reference, |
| 437 | ptr->reg, fvco, nint, nfrac); |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 438 | |
Manu Abraham | f14bfe9 | 2010-11-14 15:52:10 -0300 | [diff] [blame] | 439 | /* Set up the test registers */ |
| 440 | regs[STB6100_TEST1] = 0x8f; |
| 441 | rc = stb6100_write_reg(state, STB6100_TEST1, regs[STB6100_TEST1]); |
| 442 | if (rc < 0) |
| 443 | return rc; |
| 444 | regs[STB6100_TEST3] = 0xde; |
| 445 | rc = stb6100_write_reg(state, STB6100_TEST3, regs[STB6100_TEST3]); |
| 446 | if (rc < 0) |
| 447 | return rc; |
| 448 | |
| 449 | /* Bring up tuner according to LLA 3.7 3.4.1, step 2 */ |
| 450 | regs[STB6100_LPEN] = 0xfb; /* PLL Loop enabled, bias on, VCO on, synth on */ |
| 451 | rc = stb6100_write_reg(state, STB6100_LPEN, regs[STB6100_LPEN]); |
| 452 | if (rc < 0) |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 453 | return rc; |
| 454 | |
Ales Jurik | 20dafb3 | 2008-10-26 18:45:59 -0300 | [diff] [blame] | 455 | msleep(2); |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 456 | |
Manu Abraham | f14bfe9 | 2010-11-14 15:52:10 -0300 | [diff] [blame] | 457 | /* Bring up tuner according to LLA 3.7 3.4.1, step 3 */ |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 458 | regs[STB6100_VCO] &= ~STB6100_VCO_OCK; /* VCO fast search */ |
Manu Abraham | f14bfe9 | 2010-11-14 15:52:10 -0300 | [diff] [blame] | 459 | rc = stb6100_write_reg(state, STB6100_VCO, regs[STB6100_VCO]); |
| 460 | if (rc < 0) |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 461 | return rc; |
| 462 | |
Manu Abraham | f14bfe9 | 2010-11-14 15:52:10 -0300 | [diff] [blame] | 463 | msleep(10); /* This is dangerous as another (related) thread may start */ /* wait for LO to lock */ |
| 464 | |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 465 | regs[STB6100_VCO] &= ~STB6100_VCO_OSCH; /* vco search disabled */ |
| 466 | regs[STB6100_VCO] |= STB6100_VCO_OCK; /* search clock off */ |
Manu Abraham | f14bfe9 | 2010-11-14 15:52:10 -0300 | [diff] [blame] | 467 | rc = stb6100_write_reg(state, STB6100_VCO, regs[STB6100_VCO]); |
| 468 | if (rc < 0) |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 469 | return rc; |
| 470 | |
Manu Abraham | f14bfe9 | 2010-11-14 15:52:10 -0300 | [diff] [blame] | 471 | rc = stb6100_write_reg(state, STB6100_FCCK, 0x0d); |
| 472 | if (rc < 0) |
| 473 | return rc; /* Stop LPF calibration */ |
Manu Abraham | 7e4e8c5 | 2007-10-19 06:27:56 -0300 | [diff] [blame] | 474 | |
Manu Abraham | f14bfe9 | 2010-11-14 15:52:10 -0300 | [diff] [blame] | 475 | msleep(10); /* This is dangerous as another (related) thread may start */ |
| 476 | /* wait for stabilisation, (should not be necessary) */ |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 477 | return 0; |
| 478 | } |
| 479 | |
| 480 | static int stb6100_sleep(struct dvb_frontend *fe) |
| 481 | { |
| 482 | /* TODO: power down */ |
| 483 | return 0; |
| 484 | } |
| 485 | |
| 486 | static int stb6100_init(struct dvb_frontend *fe) |
| 487 | { |
| 488 | struct stb6100_state *state = fe->tuner_priv; |
| 489 | struct tuner_state *status = &state->status; |
| 490 | |
| 491 | status->tunerstep = 125000; |
| 492 | status->ifreq = 0; |
| 493 | status->refclock = 27000000; /* Hz */ |
| 494 | status->iqsense = 1; |
| 495 | status->bandwidth = 36000; /* kHz */ |
Hans Werner | 26f26fa | 2009-01-27 16:09:12 -0300 | [diff] [blame] | 496 | state->bandwidth = status->bandwidth * 1000; /* Hz */ |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 497 | state->reference = status->refclock / 1000; /* kHz */ |
| 498 | |
Manu Abraham | f14bfe9 | 2010-11-14 15:52:10 -0300 | [diff] [blame] | 499 | /* Set default bandwidth. Modified, PN 13-May-10 */ |
| 500 | return 0; |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | static int stb6100_get_state(struct dvb_frontend *fe, |
| 504 | enum tuner_param param, |
| 505 | struct tuner_state *state) |
| 506 | { |
| 507 | switch (param) { |
| 508 | case DVBFE_TUNER_FREQUENCY: |
| 509 | stb6100_get_frequency(fe, &state->frequency); |
| 510 | break; |
| 511 | case DVBFE_TUNER_TUNERSTEP: |
| 512 | break; |
| 513 | case DVBFE_TUNER_IFFREQ: |
| 514 | break; |
| 515 | case DVBFE_TUNER_BANDWIDTH: |
| 516 | stb6100_get_bandwidth(fe, &state->bandwidth); |
| 517 | break; |
| 518 | case DVBFE_TUNER_REFCLOCK: |
| 519 | break; |
| 520 | default: |
| 521 | break; |
| 522 | } |
| 523 | |
| 524 | return 0; |
| 525 | } |
| 526 | |
| 527 | static int stb6100_set_state(struct dvb_frontend *fe, |
| 528 | enum tuner_param param, |
| 529 | struct tuner_state *state) |
| 530 | { |
| 531 | struct stb6100_state *tstate = fe->tuner_priv; |
| 532 | |
| 533 | switch (param) { |
| 534 | case DVBFE_TUNER_FREQUENCY: |
| 535 | stb6100_set_frequency(fe, state->frequency); |
Marko Schluessler | 7d8f1e5 | 2007-10-23 19:56:18 -0300 | [diff] [blame] | 536 | tstate->frequency = state->frequency; |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 537 | break; |
| 538 | case DVBFE_TUNER_TUNERSTEP: |
| 539 | break; |
| 540 | case DVBFE_TUNER_IFFREQ: |
| 541 | break; |
| 542 | case DVBFE_TUNER_BANDWIDTH: |
| 543 | stb6100_set_bandwidth(fe, state->bandwidth); |
Marko Schluessler | 7d8f1e5 | 2007-10-23 19:56:18 -0300 | [diff] [blame] | 544 | tstate->bandwidth = state->bandwidth; |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 545 | break; |
| 546 | case DVBFE_TUNER_REFCLOCK: |
| 547 | break; |
| 548 | default: |
| 549 | break; |
| 550 | } |
| 551 | |
| 552 | return 0; |
| 553 | } |
| 554 | |
| 555 | static struct dvb_tuner_ops stb6100_ops = { |
| 556 | .info = { |
| 557 | .name = "STB6100 Silicon Tuner", |
| 558 | .frequency_min = 950000, |
| 559 | .frequency_max = 2150000, |
| 560 | .frequency_step = 0, |
| 561 | }, |
| 562 | |
| 563 | .init = stb6100_init, |
| 564 | .sleep = stb6100_sleep, |
| 565 | .get_status = stb6100_get_status, |
| 566 | .get_state = stb6100_get_state, |
| 567 | .set_state = stb6100_set_state, |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 568 | .release = stb6100_release |
| 569 | }; |
| 570 | |
| 571 | struct dvb_frontend *stb6100_attach(struct dvb_frontend *fe, |
lawrence rust | 2e4e98e | 2010-08-25 09:50:20 -0300 | [diff] [blame] | 572 | const struct stb6100_config *config, |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 573 | struct i2c_adapter *i2c) |
| 574 | { |
| 575 | struct stb6100_state *state = NULL; |
| 576 | |
Marko Schluessler | a18d431 | 2007-09-21 18:13:30 -0300 | [diff] [blame] | 577 | state = kzalloc(sizeof (struct stb6100_state), GFP_KERNEL); |
Peter Senna Tschudin | e4533e6 | 2012-09-12 08:56:00 -0300 | [diff] [blame^] | 578 | if (!state) |
| 579 | return NULL; |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 580 | |
| 581 | state->config = config; |
| 582 | state->i2c = i2c; |
| 583 | state->frontend = fe; |
Marko Schluessler | 3e3263e | 2007-09-21 18:08:01 -0300 | [diff] [blame] | 584 | state->reference = config->refclock / 1000; /* kHz */ |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 585 | fe->tuner_priv = state; |
| 586 | fe->ops.tuner_ops = stb6100_ops; |
| 587 | |
Marko Schluessler | 3e3263e | 2007-09-21 18:08:01 -0300 | [diff] [blame] | 588 | printk("%s: Attaching STB6100 \n", __func__); |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 589 | return fe; |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | static int stb6100_release(struct dvb_frontend *fe) |
| 593 | { |
| 594 | struct stb6100_state *state = fe->tuner_priv; |
| 595 | |
| 596 | fe->tuner_priv = NULL; |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 597 | kfree(state); |
| 598 | |
| 599 | return 0; |
| 600 | } |
| 601 | |
| 602 | EXPORT_SYMBOL(stb6100_attach); |
| 603 | MODULE_PARM_DESC(verbose, "Set Verbosity level"); |
| 604 | |
| 605 | MODULE_AUTHOR("Manu Abraham"); |
| 606 | MODULE_DESCRIPTION("STB6100 Silicon tuner"); |
| 607 | MODULE_LICENSE("GPL"); |