1 /* 2 * Driver for Sharp IX2505V (marked B0017) DVB-S silicon tuner 3 * 4 * Copyright (C) 2010 Malcolm Priestley 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 Version 2, as 8 * published by the Free Software Foundation. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 */ 16 17 #include <linux/module.h> 18 #include <linux/dvb/frontend.h> 19 #include <linux/slab.h> 20 #include <linux/types.h> 21 22 #include "ix2505v.h" 23 24 static int ix2505v_debug; 25 #define dprintk(level, args...) do { \ 26 if (ix2505v_debug & level) \ 27 printk(KERN_DEBUG "ix2505v: " args); \ 28 } while (0) 29 30 #define deb_info(args...) dprintk(0x01, args) 31 #define deb_i2c(args...) dprintk(0x02, args) 32 33 struct ix2505v_state { 34 struct i2c_adapter *i2c; 35 const struct ix2505v_config *config; 36 u32 frequency; 37 }; 38 39 /* 40 * Data read format of the Sharp IX2505V B0017 41 * 42 * byte1: 1 | 1 | 0 | 0 | 0 | MA1 | MA0 | 1 43 * byte2: POR | FL | RD2 | RD1 | RD0 | X | X | X 44 * 45 * byte1 = address 46 * byte2; 47 * POR = Power on Reset (VCC H=<2.2v L=>2.2v) 48 * FL = Phase Lock (H=lock L=unlock) 49 * RD0-2 = Reserved internal operations 50 * 51 * Only POR can be used to check the tuner is present 52 * 53 * Caution: after byte2 the I2C reverts to write mode continuing to read 54 * may corrupt tuning data. 55 * 56 */ 57 58 static int ix2505v_read_status_reg(struct ix2505v_state *state) 59 { 60 u8 addr = state->config->tuner_address; 61 u8 b2[] = {0}; 62 int ret; 63 64 struct i2c_msg msg[1] = { 65 { .addr = addr, .flags = I2C_M_RD, .buf = b2, .len = 1 } 66 }; 67 68 ret = i2c_transfer(state->i2c, msg, 1); 69 deb_i2c("Read %s ", __func__); 70 71 return (ret == 1) ? (int) b2[0] : -1; 72 } 73 74 static int ix2505v_write(struct ix2505v_state *state, u8 buf[], u8 count) 75 { 76 struct i2c_msg msg[1] = { 77 { .addr = state->config->tuner_address, .flags = 0, 78 .buf = buf, .len = count }, 79 }; 80 81 int ret; 82 83 ret = i2c_transfer(state->i2c, msg, 1); 84 85 if (ret != 1) { 86 deb_i2c("%s: i2c error, ret=%d\n", __func__, ret); 87 return -EIO; 88 } 89 90 return 0; 91 } 92 93 static void ix2505v_release(struct dvb_frontend *fe) 94 { 95 struct ix2505v_state *state = fe->tuner_priv; 96 97 fe->tuner_priv = NULL; 98 kfree(state); 99 100 } 101 102 /* 103 * Data write format of the Sharp IX2505V B0017 104 * 105 * byte1: 1 | 1 | 0 | 0 | 0 | 0(MA1)| 0(MA0)| 0 106 * byte2: 0 | BG1 | BG2 | N8 | N7 | N6 | N5 | N4 107 * byte3: N3 | N2 | N1 | A5 | A4 | A3 | A2 | A1 108 * byte4: 1 | 1(C1) | 1(C0) | PD5 | PD4 | TM | 0(RTS)| 1(REF) 109 * byte5: BA2 | BA1 | BA0 | PSC | PD3 |PD2/TS2|DIV/TS1|PD0/TS0 110 * 111 * byte1 = address 112 * 113 * Write order 114 * 1) byte1 -> byte2 -> byte3 -> byte4 -> byte5 115 * 2) byte1 -> byte4 -> byte5 -> byte2 -> byte3 116 * 3) byte1 -> byte2 -> byte3 -> byte4 117 * 4) byte1 -> byte4 -> byte5 -> byte2 118 * 5) byte1 -> byte2 -> byte3 119 * 6) byte1 -> byte4 -> byte5 120 * 7) byte1 -> byte2 121 * 8) byte1 -> byte4 122 * 123 * Recommended Setup 124 * 1 -> 8 -> 6 125 */ 126 127 static int ix2505v_set_params(struct dvb_frontend *fe) 128 { 129 struct dtv_frontend_properties *c = &fe->dtv_property_cache; 130 struct ix2505v_state *state = fe->tuner_priv; 131 u32 frequency = c->frequency; 132 u32 b_w = (c->symbol_rate * 27) / 32000; 133 u32 div_factor, N , A, x; 134 int ret = 0, len; 135 u8 gain, cc, ref, psc, local_osc, lpf; 136 u8 data[4] = {0}; 137 138 if ((frequency < fe->ops.info.frequency_min_hz / kHz) 139 || (frequency > fe->ops.info.frequency_max_hz / kHz)) 140 return -EINVAL; 141 142 if (state->config->tuner_gain) 143 gain = (state->config->tuner_gain < 4) 144 ? state->config->tuner_gain : 0; 145 else 146 gain = 0x0; 147 148 if (state->config->tuner_chargepump) 149 cc = state->config->tuner_chargepump; 150 else 151 cc = 0x3; 152 153 ref = 8; /* REF =1 */ 154 psc = 32; /* PSC = 0 */ 155 156 div_factor = (frequency * ref) / 40; /* local osc = 4Mhz */ 157 x = div_factor / psc; 158 N = x/100; 159 A = ((x - (N * 100)) * psc) / 100; 160 161 data[0] = ((gain & 0x3) << 5) | (N >> 3); 162 data[1] = (N << 5) | (A & 0x1f); 163 data[2] = 0x81 | ((cc & 0x3) << 5) ; /*PD5,PD4 & TM = 0|C1,C0|REF=1*/ 164 165 deb_info("Frq=%d x=%d N=%d A=%d\n", frequency, x, N, A); 166 167 if (frequency <= 1065000) 168 local_osc = (6 << 5) | 2; 169 else if (frequency <= 1170000) 170 local_osc = (7 << 5) | 2; 171 else if (frequency <= 1300000) 172 local_osc = (1 << 5); 173 else if (frequency <= 1445000) 174 local_osc = (2 << 5); 175 else if (frequency <= 1607000) 176 local_osc = (3 << 5); 177 else if (frequency <= 1778000) 178 local_osc = (4 << 5); 179 else if (frequency <= 1942000) 180 local_osc = (5 << 5); 181 else /*frequency up to 2150000*/ 182 local_osc = (6 << 5); 183 184 data[3] = local_osc; /* all other bits set 0 */ 185 186 if (b_w <= 10000) 187 lpf = 0xc; 188 else if (b_w <= 12000) 189 lpf = 0x2; 190 else if (b_w <= 14000) 191 lpf = 0xa; 192 else if (b_w <= 16000) 193 lpf = 0x6; 194 else if (b_w <= 18000) 195 lpf = 0xe; 196 else if (b_w <= 20000) 197 lpf = 0x1; 198 else if (b_w <= 22000) 199 lpf = 0x9; 200 else if (b_w <= 24000) 201 lpf = 0x5; 202 else if (b_w <= 26000) 203 lpf = 0xd; 204 else if (b_w <= 28000) 205 lpf = 0x3; 206 else 207 lpf = 0xb; 208 209 deb_info("Osc=%x b_w=%x lpf=%x\n", local_osc, b_w, lpf); 210 deb_info("Data 0=[%4phN]\n", data); 211 212 if (fe->ops.i2c_gate_ctrl) 213 fe->ops.i2c_gate_ctrl(fe, 1); 214 215 len = sizeof(data); 216 ret |= ix2505v_write(state, data, len); 217 218 data[2] |= 0x4; /* set TM = 1 other bits same */ 219 220 if (fe->ops.i2c_gate_ctrl) 221 fe->ops.i2c_gate_ctrl(fe, 1); 222 223 len = 1; 224 ret |= ix2505v_write(state, &data[2], len); /* write byte 4 only */ 225 226 msleep(10); 227 228 data[2] |= ((lpf >> 2) & 0x3) << 3; /* lpf */ 229 data[3] |= (lpf & 0x3) << 2; 230 231 deb_info("Data 2=[%x%x]\n", data[2], data[3]); 232 233 if (fe->ops.i2c_gate_ctrl) 234 fe->ops.i2c_gate_ctrl(fe, 1); 235 236 len = 2; 237 ret |= ix2505v_write(state, &data[2], len); /* write byte 4 & 5 */ 238 239 if (state->config->min_delay_ms) 240 msleep(state->config->min_delay_ms); 241 242 state->frequency = frequency; 243 244 return ret; 245 } 246 247 static int ix2505v_get_frequency(struct dvb_frontend *fe, u32 *frequency) 248 { 249 struct ix2505v_state *state = fe->tuner_priv; 250 251 *frequency = state->frequency; 252 253 return 0; 254 } 255 256 static const struct dvb_tuner_ops ix2505v_tuner_ops = { 257 .info = { 258 .name = "Sharp IX2505V (B0017)", 259 .frequency_min_hz = 950 * MHz, 260 .frequency_max_hz = 2175 * MHz 261 }, 262 .release = ix2505v_release, 263 .set_params = ix2505v_set_params, 264 .get_frequency = ix2505v_get_frequency, 265 }; 266 267 struct dvb_frontend *ix2505v_attach(struct dvb_frontend *fe, 268 const struct ix2505v_config *config, 269 struct i2c_adapter *i2c) 270 { 271 struct ix2505v_state *state = NULL; 272 int ret; 273 274 if (NULL == config) { 275 deb_i2c("%s: no config ", __func__); 276 goto error; 277 } 278 279 state = kzalloc(sizeof(struct ix2505v_state), GFP_KERNEL); 280 if (NULL == state) 281 return NULL; 282 283 state->config = config; 284 state->i2c = i2c; 285 286 if (state->config->tuner_write_only) { 287 if (fe->ops.i2c_gate_ctrl) 288 fe->ops.i2c_gate_ctrl(fe, 1); 289 290 ret = ix2505v_read_status_reg(state); 291 292 if (ret & 0x80) { 293 deb_i2c("%s: No IX2505V found\n", __func__); 294 goto error; 295 } 296 297 if (fe->ops.i2c_gate_ctrl) 298 fe->ops.i2c_gate_ctrl(fe, 0); 299 } 300 301 fe->tuner_priv = state; 302 303 memcpy(&fe->ops.tuner_ops, &ix2505v_tuner_ops, 304 sizeof(struct dvb_tuner_ops)); 305 deb_i2c("%s: initialization (%s addr=0x%02x) ok\n", 306 __func__, fe->ops.tuner_ops.info.name, config->tuner_address); 307 308 return fe; 309 310 error: 311 kfree(state); 312 return NULL; 313 } 314 EXPORT_SYMBOL(ix2505v_attach); 315 316 module_param_named(debug, ix2505v_debug, int, 0644); 317 MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off)."); 318 MODULE_DESCRIPTION("DVB IX2505V tuner driver"); 319 MODULE_AUTHOR("Malcolm Priestley"); 320 MODULE_LICENSE("GPL"); 321