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 | |
Mauro Carvalho Chehab | 8393796 | 2013-11-02 05:05:18 -0300 | [diff] [blame] | 34 | /* Max transfer size done by I2C transfer functions */ |
| 35 | #define MAX_XFER_SIZE 64 |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 36 | |
| 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 Abraham | f14bfe9 | 2010-11-14 15:52:10 -0300 | [diff] [blame] | 56 | } while (0) |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 57 | |
| 58 | struct stb6100_lkup { |
| 59 | u32 val_low; |
| 60 | u32 val_high; |
| 61 | u8 reg; |
| 62 | }; |
| 63 | |
Mauro Carvalho Chehab | f2709c2 | 2016-11-18 20:30:51 -0200 | [diff] [blame] | 64 | static void stb6100_release(struct dvb_frontend *fe); |
| 65 | |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 66 | static 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. */ |
| 84 | static 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 | */ |
| 102 | struct stb6100_regmask { |
| 103 | u8 mask; |
| 104 | u8 set; |
| 105 | }; |
| 106 | |
| 107 | static 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 Chehab | a931910 | 2010-11-22 13:22:37 -0300 | [diff] [blame] | 122 | /* |
| 123 | * Currently unused. Some boards might need it in the future |
| 124 | */ |
| 125 | static inline void stb6100_normalise_regs(u8 regs[]) |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 126 | { |
| 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 | |
| 133 | static 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 Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 143 | rc = i2c_transfer(state->i2c, &msg, 1); |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 144 | 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 | |
| 160 | static int stb6100_read_reg(struct stb6100_state *state, u8 reg) |
| 161 | { |
| 162 | u8 regs[STB6100_NUMREGS]; |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 163 | |
Manu Abraham | f14bfe9 | 2010-11-14 15:52:10 -0300 | [diff] [blame] | 164 | 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 Verkuil | fdf07b0 | 2012-04-20 08:04:48 -0300 | [diff] [blame] | 171 | i2c_transfer(state->i2c, &msg, 1); |
Manu Abraham | f14bfe9 | 2010-11-14 15:52:10 -0300 | [diff] [blame] | 172 | |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 173 | if (unlikely(reg >= STB6100_NUMREGS)) { |
| 174 | dprintk(verbose, FE_ERROR, 1, "Invalid register offset 0x%x", reg); |
| 175 | return -EINVAL; |
| 176 | } |
Manu Abraham | f14bfe9 | 2010-11-14 15:52:10 -0300 | [diff] [blame] | 177 | 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 Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | static int stb6100_write_reg_range(struct stb6100_state *state, u8 buf[], int start, int len) |
| 186 | { |
| 187 | int rc; |
Mauro Carvalho Chehab | 8393796 | 2013-11-02 05:05:18 -0300 | [diff] [blame] | 188 | u8 cmdbuf[MAX_XFER_SIZE]; |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 189 | struct i2c_msg msg = { |
| 190 | .addr = state->config->tuner_address, |
| 191 | .flags = 0, |
| 192 | .buf = cmdbuf, |
| 193 | .len = len + 1 |
| 194 | }; |
| 195 | |
Alexander Shiyan | 7e6bd12 | 2014-02-25 23:41:14 -0300 | [diff] [blame] | 196 | if (1 + len > sizeof(cmdbuf)) { |
Mauro Carvalho Chehab | 8393796 | 2013-11-02 05:05:18 -0300 | [diff] [blame] | 197 | printk(KERN_WARNING |
| 198 | "%s: i2c wr: len=%d is too big!\n", |
| 199 | KBUILD_MODNAME, len); |
| 200 | return -EINVAL; |
| 201 | } |
| 202 | |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 203 | 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 Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 218 | rc = i2c_transfer(state->i2c, &msg, 1); |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 219 | 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 | |
| 227 | static int stb6100_write_reg(struct stb6100_state *state, u8 reg, u8 data) |
| 228 | { |
| 229 | if (unlikely(reg >= STB6100_NUMREGS)) { |
| 230 | dprintk(verbose, FE_ERROR, 1, "Invalid register offset 0x%x", reg); |
| 231 | return -EREMOTEIO; |
| 232 | } |
| 233 | data = (data & stb6100_template[reg].mask) | stb6100_template[reg].set; |
| 234 | return stb6100_write_reg_range(state, &data, reg, 1); |
| 235 | } |
| 236 | |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 237 | |
| 238 | static int stb6100_get_status(struct dvb_frontend *fe, u32 *status) |
| 239 | { |
| 240 | int rc; |
| 241 | struct stb6100_state *state = fe->tuner_priv; |
| 242 | |
Manu Abraham | f14bfe9 | 2010-11-14 15:52:10 -0300 | [diff] [blame] | 243 | rc = stb6100_read_reg(state, STB6100_LD); |
| 244 | if (rc < 0) { |
| 245 | dprintk(verbose, FE_ERROR, 1, "%s failed", __func__); |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 246 | return rc; |
Manu Abraham | f14bfe9 | 2010-11-14 15:52:10 -0300 | [diff] [blame] | 247 | } |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 248 | return (rc & STB6100_LD_LOCK) ? TUNER_STATUS_LOCKED : 0; |
| 249 | } |
| 250 | |
| 251 | static int stb6100_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth) |
| 252 | { |
| 253 | int rc; |
| 254 | u8 f; |
Mauro Carvalho Chehab | cffdbfe | 2015-11-11 16:52:46 -0200 | [diff] [blame] | 255 | u32 bw; |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 256 | struct stb6100_state *state = fe->tuner_priv; |
| 257 | |
Manu Abraham | f14bfe9 | 2010-11-14 15:52:10 -0300 | [diff] [blame] | 258 | rc = stb6100_read_reg(state, STB6100_F); |
| 259 | if (rc < 0) |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 260 | return rc; |
| 261 | f = rc & STB6100_F_F; |
| 262 | |
Mauro Carvalho Chehab | cffdbfe | 2015-11-11 16:52:46 -0200 | [diff] [blame] | 263 | bw = (f + 5) * 2000; /* x2 for ZIF */ |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 264 | |
Mauro Carvalho Chehab | cffdbfe | 2015-11-11 16:52:46 -0200 | [diff] [blame] | 265 | *bandwidth = state->bandwidth = bw * 1000; |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 266 | dprintk(verbose, FE_DEBUG, 1, "bandwidth = %u Hz", state->bandwidth); |
| 267 | return 0; |
| 268 | } |
| 269 | |
| 270 | static int stb6100_set_bandwidth(struct dvb_frontend *fe, u32 bandwidth) |
| 271 | { |
| 272 | u32 tmp; |
| 273 | int rc; |
| 274 | struct stb6100_state *state = fe->tuner_priv; |
| 275 | |
Reinhard Nissl | 89693b7 | 2008-01-18 16:06:18 -0300 | [diff] [blame] | 276 | dprintk(verbose, FE_DEBUG, 1, "set bandwidth to %u Hz", bandwidth); |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 277 | |
Manu Abraham | 763fbaf | 2008-01-18 11:28:48 -0300 | [diff] [blame] | 278 | bandwidth /= 2; /* ZIF */ |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 279 | |
Reinhard Nissl | 89693b7 | 2008-01-18 16:06:18 -0300 | [diff] [blame] | 280 | 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] | 281 | tmp = 31; |
Reinhard Nissl | 89693b7 | 2008-01-18 16:06:18 -0300 | [diff] [blame] | 282 | else if (bandwidth <= 5000000) /* bw/2 min = 5Mhz for F=0 */ |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 283 | tmp = 0; |
| 284 | else /* if 5 < bw/2 < 36 */ |
Reinhard Nissl | 6f6c268 | 2008-01-21 16:15:14 -0300 | [diff] [blame] | 285 | tmp = (bandwidth + 500000) / 1000000 - 5; |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 286 | |
| 287 | /* Turn on LPF bandwidth setting clock control, |
| 288 | * set bandwidth, wait 10ms, turn off. |
| 289 | */ |
Manu Abraham | f14bfe9 | 2010-11-14 15:52:10 -0300 | [diff] [blame] | 290 | rc = stb6100_write_reg(state, STB6100_FCCK, 0x0d | STB6100_FCCK_FCCK); |
| 291 | if (rc < 0) |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 292 | return rc; |
Manu Abraham | f14bfe9 | 2010-11-14 15:52:10 -0300 | [diff] [blame] | 293 | rc = stb6100_write_reg(state, STB6100_F, 0xc0 | tmp); |
| 294 | if (rc < 0) |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 295 | return rc; |
Manu Abraham | f14bfe9 | 2010-11-14 15:52:10 -0300 | [diff] [blame] | 296 | |
| 297 | msleep(5); /* This is dangerous as another (related) thread may start */ |
| 298 | |
| 299 | rc = stb6100_write_reg(state, STB6100_FCCK, 0x0d); |
| 300 | if (rc < 0) |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 301 | return rc; |
| 302 | |
Manu Abraham | f14bfe9 | 2010-11-14 15:52:10 -0300 | [diff] [blame] | 303 | msleep(10); /* This is dangerous as another (related) thread may start */ |
| 304 | |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 305 | return 0; |
| 306 | } |
| 307 | |
| 308 | static int stb6100_get_frequency(struct dvb_frontend *fe, u32 *frequency) |
| 309 | { |
| 310 | int rc; |
| 311 | u32 nint, nfrac, fvco; |
| 312 | int psd2, odiv; |
| 313 | struct stb6100_state *state = fe->tuner_priv; |
| 314 | u8 regs[STB6100_NUMREGS]; |
| 315 | |
Manu Abraham | f14bfe9 | 2010-11-14 15:52:10 -0300 | [diff] [blame] | 316 | rc = stb6100_read_regs(state, regs); |
| 317 | if (rc < 0) |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 318 | return rc; |
| 319 | |
| 320 | odiv = (regs[STB6100_VCO] & STB6100_VCO_ODIV) >> STB6100_VCO_ODIV_SHIFT; |
| 321 | psd2 = (regs[STB6100_K] & STB6100_K_PSD2) >> STB6100_K_PSD2_SHIFT; |
| 322 | nint = regs[STB6100_NI]; |
| 323 | nfrac = ((regs[STB6100_K] & STB6100_K_NF_MSB) << 8) | regs[STB6100_NF_LSB]; |
| 324 | fvco = (nfrac * state->reference >> (9 - psd2)) + (nint * state->reference << psd2); |
| 325 | *frequency = state->frequency = fvco >> (odiv + 1); |
| 326 | |
| 327 | dprintk(verbose, FE_DEBUG, 1, |
| 328 | "frequency = %u kHz, odiv = %u, psd2 = %u, fxtal = %u kHz, fvco = %u kHz, N(I) = %u, N(F) = %u", |
| 329 | state->frequency, odiv, psd2, state->reference, fvco, nint, nfrac); |
| 330 | return 0; |
| 331 | } |
| 332 | |
| 333 | |
| 334 | static int stb6100_set_frequency(struct dvb_frontend *fe, u32 frequency) |
| 335 | { |
| 336 | int rc; |
| 337 | const struct stb6100_lkup *ptr; |
| 338 | struct stb6100_state *state = fe->tuner_priv; |
Mauro Carvalho Chehab | 1e73fa5 | 2011-12-31 17:24:19 -0200 | [diff] [blame] | 339 | struct dtv_frontend_properties *p = &fe->dtv_property_cache; |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 340 | |
| 341 | u32 srate = 0, fvco, nint, nfrac; |
| 342 | u8 regs[STB6100_NUMREGS]; |
| 343 | u8 g, psd2, odiv; |
| 344 | |
Manu Abraham | f14bfe9 | 2010-11-14 15:52:10 -0300 | [diff] [blame] | 345 | dprintk(verbose, FE_DEBUG, 1, "Version 2010-8-14 13:51"); |
Manu Abraham | 3f40092 | 2008-10-26 18:28:52 -0300 | [diff] [blame] | 346 | |
Mauro Carvalho Chehab | 5135986 | 2011-12-26 14:20:02 -0300 | [diff] [blame] | 347 | if (fe->ops.get_frontend) { |
Manu Abraham | 3f40092 | 2008-10-26 18:28:52 -0300 | [diff] [blame] | 348 | dprintk(verbose, FE_DEBUG, 1, "Get frontend parameters"); |
Mauro Carvalho Chehab | 7e3e68b | 2016-02-04 12:58:30 -0200 | [diff] [blame] | 349 | fe->ops.get_frontend(fe, p); |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 350 | } |
Mauro Carvalho Chehab | 1e73fa5 | 2011-12-31 17:24:19 -0200 | [diff] [blame] | 351 | srate = p->symbol_rate; |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 352 | |
Manu Abraham | f14bfe9 | 2010-11-14 15:52:10 -0300 | [diff] [blame] | 353 | /* Set up tuner cleanly, LPF calibration on */ |
| 354 | rc = stb6100_write_reg(state, STB6100_FCCK, 0x4d | STB6100_FCCK_FCCK); |
| 355 | if (rc < 0) |
| 356 | return rc; /* allow LPF calibration */ |
Ales Jurik | 20dafb3 | 2008-10-26 18:45:59 -0300 | [diff] [blame] | 357 | |
Manu Abraham | f14bfe9 | 2010-11-14 15:52:10 -0300 | [diff] [blame] | 358 | /* PLL Loop disabled, bias on, VCO on, synth on */ |
| 359 | regs[STB6100_LPEN] = 0xeb; |
| 360 | rc = stb6100_write_reg(state, STB6100_LPEN, regs[STB6100_LPEN]); |
| 361 | if (rc < 0) |
Ales Jurik | 20dafb3 | 2008-10-26 18:45:59 -0300 | [diff] [blame] | 362 | return rc; |
| 363 | |
Manu Abraham | f14bfe9 | 2010-11-14 15:52:10 -0300 | [diff] [blame] | 364 | /* Program the registers with their data values */ |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 365 | |
| 366 | /* VCO divide ratio (LO divide ratio, VCO prescaler enable). */ |
| 367 | if (frequency <= 1075000) |
| 368 | odiv = 1; |
| 369 | else |
| 370 | odiv = 0; |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 371 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 372 | /* VCO enabled, search clock off as per LL3.7, 3.4.1 */ |
Manu Abraham | f14bfe9 | 2010-11-14 15:52:10 -0300 | [diff] [blame] | 373 | regs[STB6100_VCO] = 0xe0 | (odiv << STB6100_VCO_ODIV_SHIFT); |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 374 | |
| 375 | /* OSM */ |
| 376 | for (ptr = lkup; |
| 377 | (ptr->val_high != 0) && !CHKRANGE(frequency, ptr->val_low, ptr->val_high); |
| 378 | ptr++); |
Manu Abraham | f14bfe9 | 2010-11-14 15:52:10 -0300 | [diff] [blame] | 379 | |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 380 | if (ptr->val_high == 0) { |
| 381 | printk(KERN_ERR "%s: frequency out of range: %u kHz\n", __func__, frequency); |
| 382 | return -EINVAL; |
| 383 | } |
| 384 | regs[STB6100_VCO] = (regs[STB6100_VCO] & ~STB6100_VCO_OSM) | ptr->reg; |
Manu Abraham | f14bfe9 | 2010-11-14 15:52:10 -0300 | [diff] [blame] | 385 | rc = stb6100_write_reg(state, STB6100_VCO, regs[STB6100_VCO]); |
| 386 | if (rc < 0) |
| 387 | return rc; |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 388 | |
Manu Abraham | f14bfe9 | 2010-11-14 15:52:10 -0300 | [diff] [blame] | 389 | if ((frequency > 1075000) && (frequency <= 1325000)) |
| 390 | psd2 = 0; |
| 391 | else |
| 392 | psd2 = 1; |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 393 | /* F(VCO) = F(LO) * (ODIV == 0 ? 2 : 4) */ |
| 394 | fvco = frequency << (1 + odiv); |
| 395 | /* N(I) = floor(f(VCO) / (f(XTAL) * (PSD2 ? 2 : 1))) */ |
| 396 | nint = fvco / (state->reference << psd2); |
| 397 | /* 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] | 398 | nfrac = DIV_ROUND_CLOSEST((fvco - (nint * state->reference << psd2)) |
Manu Abraham | f14bfe9 | 2010-11-14 15:52:10 -0300 | [diff] [blame] | 399 | << (9 - psd2), state->reference); |
| 400 | |
| 401 | /* NI */ |
| 402 | regs[STB6100_NI] = nint; |
| 403 | rc = stb6100_write_reg(state, STB6100_NI, regs[STB6100_NI]); |
| 404 | if (rc < 0) |
| 405 | return rc; |
| 406 | |
| 407 | /* NF */ |
| 408 | regs[STB6100_NF_LSB] = nfrac; |
| 409 | rc = stb6100_write_reg(state, STB6100_NF_LSB, regs[STB6100_NF_LSB]); |
| 410 | if (rc < 0) |
| 411 | return rc; |
| 412 | |
| 413 | /* K */ |
| 414 | regs[STB6100_K] = (0x38 & ~STB6100_K_PSD2) | (psd2 << STB6100_K_PSD2_SHIFT); |
| 415 | regs[STB6100_K] = (regs[STB6100_K] & ~STB6100_K_NF_MSB) | ((nfrac >> 8) & STB6100_K_NF_MSB); |
| 416 | rc = stb6100_write_reg(state, STB6100_K, regs[STB6100_K]); |
| 417 | if (rc < 0) |
| 418 | return rc; |
| 419 | |
| 420 | /* G Baseband gain. */ |
| 421 | if (srate >= 15000000) |
| 422 | g = 9; /* +4 dB */ |
| 423 | else if (srate >= 5000000) |
| 424 | g = 11; /* +8 dB */ |
| 425 | else |
| 426 | g = 14; /* +14 dB */ |
| 427 | |
| 428 | regs[STB6100_G] = (0x10 & ~STB6100_G_G) | g; |
| 429 | regs[STB6100_G] &= ~STB6100_G_GCT; /* mask GCT */ |
| 430 | regs[STB6100_G] |= (1 << 5); /* 2Vp-p Mode */ |
| 431 | rc = stb6100_write_reg(state, STB6100_G, regs[STB6100_G]); |
| 432 | if (rc < 0) |
| 433 | return rc; |
| 434 | |
| 435 | /* F we don't write as it is set up in BW set */ |
| 436 | |
| 437 | /* DLB set DC servo loop BW to 160Hz (LLA 3.8 / 2.1) */ |
| 438 | regs[STB6100_DLB] = 0xcc; |
| 439 | rc = stb6100_write_reg(state, STB6100_DLB, regs[STB6100_DLB]); |
| 440 | if (rc < 0) |
| 441 | return rc; |
| 442 | |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 443 | dprintk(verbose, FE_DEBUG, 1, |
| 444 | "frequency = %u, srate = %u, g = %u, odiv = %u, psd2 = %u, fxtal = %u, osm = %u, fvco = %u, N(I) = %u, N(F) = %u", |
| 445 | frequency, srate, (unsigned int)g, (unsigned int)odiv, |
| 446 | (unsigned int)psd2, state->reference, |
| 447 | ptr->reg, fvco, nint, nfrac); |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 448 | |
Manu Abraham | f14bfe9 | 2010-11-14 15:52:10 -0300 | [diff] [blame] | 449 | /* Set up the test registers */ |
| 450 | regs[STB6100_TEST1] = 0x8f; |
| 451 | rc = stb6100_write_reg(state, STB6100_TEST1, regs[STB6100_TEST1]); |
| 452 | if (rc < 0) |
| 453 | return rc; |
| 454 | regs[STB6100_TEST3] = 0xde; |
| 455 | rc = stb6100_write_reg(state, STB6100_TEST3, regs[STB6100_TEST3]); |
| 456 | if (rc < 0) |
| 457 | return rc; |
| 458 | |
| 459 | /* Bring up tuner according to LLA 3.7 3.4.1, step 2 */ |
| 460 | regs[STB6100_LPEN] = 0xfb; /* PLL Loop enabled, bias on, VCO on, synth on */ |
| 461 | rc = stb6100_write_reg(state, STB6100_LPEN, regs[STB6100_LPEN]); |
| 462 | if (rc < 0) |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 463 | return rc; |
| 464 | |
Ales Jurik | 20dafb3 | 2008-10-26 18:45:59 -0300 | [diff] [blame] | 465 | msleep(2); |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 466 | |
Manu Abraham | f14bfe9 | 2010-11-14 15:52:10 -0300 | [diff] [blame] | 467 | /* 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] | 468 | regs[STB6100_VCO] &= ~STB6100_VCO_OCK; /* VCO fast search */ |
Manu Abraham | f14bfe9 | 2010-11-14 15:52:10 -0300 | [diff] [blame] | 469 | rc = stb6100_write_reg(state, STB6100_VCO, regs[STB6100_VCO]); |
| 470 | if (rc < 0) |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 471 | return rc; |
| 472 | |
Manu Abraham | f14bfe9 | 2010-11-14 15:52:10 -0300 | [diff] [blame] | 473 | msleep(10); /* This is dangerous as another (related) thread may start */ /* wait for LO to lock */ |
| 474 | |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 475 | regs[STB6100_VCO] &= ~STB6100_VCO_OSCH; /* vco search disabled */ |
| 476 | regs[STB6100_VCO] |= STB6100_VCO_OCK; /* search clock off */ |
Manu Abraham | f14bfe9 | 2010-11-14 15:52:10 -0300 | [diff] [blame] | 477 | rc = stb6100_write_reg(state, STB6100_VCO, regs[STB6100_VCO]); |
| 478 | if (rc < 0) |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 479 | return rc; |
| 480 | |
Manu Abraham | f14bfe9 | 2010-11-14 15:52:10 -0300 | [diff] [blame] | 481 | rc = stb6100_write_reg(state, STB6100_FCCK, 0x0d); |
| 482 | if (rc < 0) |
| 483 | return rc; /* Stop LPF calibration */ |
Manu Abraham | 7e4e8c5 | 2007-10-19 06:27:56 -0300 | [diff] [blame] | 484 | |
Manu Abraham | f14bfe9 | 2010-11-14 15:52:10 -0300 | [diff] [blame] | 485 | msleep(10); /* This is dangerous as another (related) thread may start */ |
| 486 | /* wait for stabilisation, (should not be necessary) */ |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 487 | return 0; |
| 488 | } |
| 489 | |
| 490 | static int stb6100_sleep(struct dvb_frontend *fe) |
| 491 | { |
| 492 | /* TODO: power down */ |
| 493 | return 0; |
| 494 | } |
| 495 | |
| 496 | static int stb6100_init(struct dvb_frontend *fe) |
| 497 | { |
| 498 | struct stb6100_state *state = fe->tuner_priv; |
Mauro Carvalho Chehab | 2184e25 | 2015-11-11 15:52:25 -0200 | [diff] [blame] | 499 | int refclk = 27000000; /* Hz */ |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 500 | |
Mauro Carvalho Chehab | 2184e25 | 2015-11-11 15:52:25 -0200 | [diff] [blame] | 501 | /* |
| 502 | * iqsense = 1 |
| 503 | * tunerstep = 125000 |
| 504 | */ |
Mauro Carvalho Chehab | 65f0f68 | 2015-11-11 18:42:00 -0200 | [diff] [blame] | 505 | state->bandwidth = 36000000; /* Hz */ |
Mauro Carvalho Chehab | 2184e25 | 2015-11-11 15:52:25 -0200 | [diff] [blame] | 506 | state->reference = refclk / 1000; /* kHz */ |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 507 | |
Manu Abraham | f14bfe9 | 2010-11-14 15:52:10 -0300 | [diff] [blame] | 508 | /* Set default bandwidth. Modified, PN 13-May-10 */ |
| 509 | return 0; |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 510 | } |
| 511 | |
Mauro Carvalho Chehab | 65f0f68 | 2015-11-11 18:42:00 -0200 | [diff] [blame] | 512 | static int stb6100_set_params(struct dvb_frontend *fe) |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 513 | { |
Mauro Carvalho Chehab | 65f0f68 | 2015-11-11 18:42:00 -0200 | [diff] [blame] | 514 | struct dtv_frontend_properties *c = &fe->dtv_property_cache; |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 515 | |
Mauro Carvalho Chehab | 65f0f68 | 2015-11-11 18:42:00 -0200 | [diff] [blame] | 516 | if (c->frequency > 0) |
| 517 | stb6100_set_frequency(fe, c->frequency); |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 518 | |
Mauro Carvalho Chehab | 65f0f68 | 2015-11-11 18:42:00 -0200 | [diff] [blame] | 519 | if (c->bandwidth_hz > 0) |
| 520 | stb6100_set_bandwidth(fe, c->bandwidth_hz); |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 521 | |
| 522 | return 0; |
| 523 | } |
| 524 | |
Julia Lawall | 14c4bf3 | 2016-09-11 11:44:12 -0300 | [diff] [blame] | 525 | static const struct dvb_tuner_ops stb6100_ops = { |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 526 | .info = { |
| 527 | .name = "STB6100 Silicon Tuner", |
| 528 | .frequency_min = 950000, |
| 529 | .frequency_max = 2150000, |
| 530 | .frequency_step = 0, |
| 531 | }, |
| 532 | |
| 533 | .init = stb6100_init, |
| 534 | .sleep = stb6100_sleep, |
| 535 | .get_status = stb6100_get_status, |
Mauro Carvalho Chehab | 65f0f68 | 2015-11-11 18:42:00 -0200 | [diff] [blame] | 536 | .set_params = stb6100_set_params, |
| 537 | .get_frequency = stb6100_get_frequency, |
| 538 | .get_bandwidth = stb6100_get_bandwidth, |
Mauro Carvalho Chehab | f2709c2 | 2016-11-18 20:30:51 -0200 | [diff] [blame] | 539 | .release = stb6100_release |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 540 | }; |
| 541 | |
| 542 | struct dvb_frontend *stb6100_attach(struct dvb_frontend *fe, |
lawrence rust | 2e4e98e | 2010-08-25 09:50:20 -0300 | [diff] [blame] | 543 | const struct stb6100_config *config, |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 544 | struct i2c_adapter *i2c) |
| 545 | { |
| 546 | struct stb6100_state *state = NULL; |
| 547 | |
Marko Schluessler | a18d431 | 2007-09-21 18:13:30 -0300 | [diff] [blame] | 548 | state = kzalloc(sizeof (struct stb6100_state), GFP_KERNEL); |
Peter Senna Tschudin | e4533e6 | 2012-09-12 08:56:00 -0300 | [diff] [blame] | 549 | if (!state) |
| 550 | return NULL; |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 551 | |
| 552 | state->config = config; |
| 553 | state->i2c = i2c; |
| 554 | state->frontend = fe; |
Marko Schluessler | 3e3263e | 2007-09-21 18:08:01 -0300 | [diff] [blame] | 555 | state->reference = config->refclock / 1000; /* kHz */ |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 556 | fe->tuner_priv = state; |
| 557 | fe->ops.tuner_ops = stb6100_ops; |
| 558 | |
Marko Schluessler | 3e3263e | 2007-09-21 18:08:01 -0300 | [diff] [blame] | 559 | printk("%s: Attaching STB6100 \n", __func__); |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 560 | return fe; |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 561 | } |
| 562 | |
Mauro Carvalho Chehab | f2709c2 | 2016-11-18 20:30:51 -0200 | [diff] [blame] | 563 | static void stb6100_release(struct dvb_frontend *fe) |
| 564 | { |
| 565 | struct stb6100_state *state = fe->tuner_priv; |
| 566 | |
| 567 | fe->tuner_priv = NULL; |
| 568 | kfree(state); |
| 569 | } |
| 570 | |
Manu Abraham | c46b656 | 2007-02-24 08:31:40 -0300 | [diff] [blame] | 571 | EXPORT_SYMBOL(stb6100_attach); |
| 572 | MODULE_PARM_DESC(verbose, "Set Verbosity level"); |
| 573 | |
| 574 | MODULE_AUTHOR("Manu Abraham"); |
| 575 | MODULE_DESCRIPTION("STB6100 Silicon tuner"); |
| 576 | MODULE_LICENSE("GPL"); |