1 /* 2 * NXP TDA18212HN silicon tuner driver 3 * 4 * Copyright (C) 2011 Antti Palosaari <crope@iki.fi> 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 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License along 17 * with this program; if not, write to the Free Software Foundation, Inc., 18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 19 */ 20 21 #include "tda18212.h" 22 23 struct tda18212_priv { 24 struct tda18212_config *cfg; 25 struct i2c_adapter *i2c; 26 27 u32 if_frequency; 28 }; 29 30 /* write multiple registers */ 31 static int tda18212_wr_regs(struct tda18212_priv *priv, u8 reg, u8 *val, 32 int len) 33 { 34 int ret; 35 u8 buf[len+1]; 36 struct i2c_msg msg[1] = { 37 { 38 .addr = priv->cfg->i2c_address, 39 .flags = 0, 40 .len = sizeof(buf), 41 .buf = buf, 42 } 43 }; 44 45 buf[0] = reg; 46 memcpy(&buf[1], val, len); 47 48 ret = i2c_transfer(priv->i2c, msg, 1); 49 if (ret == 1) { 50 ret = 0; 51 } else { 52 dev_warn(&priv->i2c->dev, "%s: i2c wr failed=%d reg=%02x " \ 53 "len=%d\n", KBUILD_MODNAME, ret, reg, len); 54 ret = -EREMOTEIO; 55 } 56 return ret; 57 } 58 59 /* read multiple registers */ 60 static int tda18212_rd_regs(struct tda18212_priv *priv, u8 reg, u8 *val, 61 int len) 62 { 63 int ret; 64 u8 buf[len]; 65 struct i2c_msg msg[2] = { 66 { 67 .addr = priv->cfg->i2c_address, 68 .flags = 0, 69 .len = 1, 70 .buf = ®, 71 }, { 72 .addr = priv->cfg->i2c_address, 73 .flags = I2C_M_RD, 74 .len = sizeof(buf), 75 .buf = buf, 76 } 77 }; 78 79 ret = i2c_transfer(priv->i2c, msg, 2); 80 if (ret == 2) { 81 memcpy(val, buf, len); 82 ret = 0; 83 } else { 84 dev_warn(&priv->i2c->dev, "%s: i2c rd failed=%d reg=%02x " \ 85 "len=%d\n", KBUILD_MODNAME, ret, reg, len); 86 ret = -EREMOTEIO; 87 } 88 89 return ret; 90 } 91 92 /* write single register */ 93 static int tda18212_wr_reg(struct tda18212_priv *priv, u8 reg, u8 val) 94 { 95 return tda18212_wr_regs(priv, reg, &val, 1); 96 } 97 98 /* read single register */ 99 static int tda18212_rd_reg(struct tda18212_priv *priv, u8 reg, u8 *val) 100 { 101 return tda18212_rd_regs(priv, reg, val, 1); 102 } 103 104 #if 0 /* keep, useful when developing driver */ 105 static void tda18212_dump_regs(struct tda18212_priv *priv) 106 { 107 int i; 108 u8 buf[256]; 109 110 #define TDA18212_RD_LEN 32 111 for (i = 0; i < sizeof(buf); i += TDA18212_RD_LEN) 112 tda18212_rd_regs(priv, i, &buf[i], TDA18212_RD_LEN); 113 114 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET, 32, 1, buf, 115 sizeof(buf), true); 116 117 return; 118 } 119 #endif 120 121 static int tda18212_set_params(struct dvb_frontend *fe) 122 { 123 struct tda18212_priv *priv = fe->tuner_priv; 124 struct dtv_frontend_properties *c = &fe->dtv_property_cache; 125 int ret, i; 126 u32 if_khz; 127 u8 buf[9]; 128 #define DVBT_6 0 129 #define DVBT_7 1 130 #define DVBT_8 2 131 #define DVBT2_6 3 132 #define DVBT2_7 4 133 #define DVBT2_8 5 134 #define DVBC_6 6 135 #define DVBC_8 7 136 static const u8 bw_params[][3] = { 137 /* reg: 0f 13 23 */ 138 [DVBT_6] = { 0xb3, 0x20, 0x03 }, 139 [DVBT_7] = { 0xb3, 0x31, 0x01 }, 140 [DVBT_8] = { 0xb3, 0x22, 0x01 }, 141 [DVBT2_6] = { 0xbc, 0x20, 0x03 }, 142 [DVBT2_7] = { 0xbc, 0x72, 0x03 }, 143 [DVBT2_8] = { 0xbc, 0x22, 0x01 }, 144 [DVBC_6] = { 0x92, 0x50, 0x03 }, 145 [DVBC_8] = { 0x92, 0x53, 0x03 }, 146 }; 147 148 dev_dbg(&priv->i2c->dev, 149 "%s: delivery_system=%d frequency=%d bandwidth_hz=%d\n", 150 __func__, c->delivery_system, c->frequency, 151 c->bandwidth_hz); 152 153 if (fe->ops.i2c_gate_ctrl) 154 fe->ops.i2c_gate_ctrl(fe, 1); /* open I2C-gate */ 155 156 switch (c->delivery_system) { 157 case SYS_DVBT: 158 switch (c->bandwidth_hz) { 159 case 6000000: 160 if_khz = priv->cfg->if_dvbt_6; 161 i = DVBT_6; 162 break; 163 case 7000000: 164 if_khz = priv->cfg->if_dvbt_7; 165 i = DVBT_7; 166 break; 167 case 8000000: 168 if_khz = priv->cfg->if_dvbt_8; 169 i = DVBT_8; 170 break; 171 default: 172 ret = -EINVAL; 173 goto error; 174 } 175 break; 176 case SYS_DVBT2: 177 switch (c->bandwidth_hz) { 178 case 6000000: 179 if_khz = priv->cfg->if_dvbt2_6; 180 i = DVBT2_6; 181 break; 182 case 7000000: 183 if_khz = priv->cfg->if_dvbt2_7; 184 i = DVBT2_7; 185 break; 186 case 8000000: 187 if_khz = priv->cfg->if_dvbt2_8; 188 i = DVBT2_8; 189 break; 190 default: 191 ret = -EINVAL; 192 goto error; 193 } 194 break; 195 case SYS_DVBC_ANNEX_A: 196 case SYS_DVBC_ANNEX_C: 197 if_khz = priv->cfg->if_dvbc; 198 i = DVBC_8; 199 break; 200 default: 201 ret = -EINVAL; 202 goto error; 203 } 204 205 ret = tda18212_wr_reg(priv, 0x23, bw_params[i][2]); 206 if (ret) 207 goto error; 208 209 ret = tda18212_wr_reg(priv, 0x06, 0x00); 210 if (ret) 211 goto error; 212 213 ret = tda18212_wr_reg(priv, 0x0f, bw_params[i][0]); 214 if (ret) 215 goto error; 216 217 buf[0] = 0x02; 218 buf[1] = bw_params[i][1]; 219 buf[2] = 0x03; /* default value */ 220 buf[3] = DIV_ROUND_CLOSEST(if_khz, 50); 221 buf[4] = ((c->frequency / 1000) >> 16) & 0xff; 222 buf[5] = ((c->frequency / 1000) >> 8) & 0xff; 223 buf[6] = ((c->frequency / 1000) >> 0) & 0xff; 224 buf[7] = 0xc1; 225 buf[8] = 0x01; 226 ret = tda18212_wr_regs(priv, 0x12, buf, sizeof(buf)); 227 if (ret) 228 goto error; 229 230 /* actual IF rounded as it is on register */ 231 priv->if_frequency = buf[3] * 50 * 1000; 232 233 exit: 234 if (fe->ops.i2c_gate_ctrl) 235 fe->ops.i2c_gate_ctrl(fe, 0); /* close I2C-gate */ 236 237 return ret; 238 239 error: 240 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret); 241 goto exit; 242 } 243 244 static int tda18212_get_if_frequency(struct dvb_frontend *fe, u32 *frequency) 245 { 246 struct tda18212_priv *priv = fe->tuner_priv; 247 248 *frequency = priv->if_frequency; 249 250 return 0; 251 } 252 253 static int tda18212_release(struct dvb_frontend *fe) 254 { 255 kfree(fe->tuner_priv); 256 fe->tuner_priv = NULL; 257 return 0; 258 } 259 260 static const struct dvb_tuner_ops tda18212_tuner_ops = { 261 .info = { 262 .name = "NXP TDA18212", 263 264 .frequency_min = 48000000, 265 .frequency_max = 864000000, 266 .frequency_step = 1000, 267 }, 268 269 .release = tda18212_release, 270 271 .set_params = tda18212_set_params, 272 .get_if_frequency = tda18212_get_if_frequency, 273 }; 274 275 struct dvb_frontend *tda18212_attach(struct dvb_frontend *fe, 276 struct i2c_adapter *i2c, struct tda18212_config *cfg) 277 { 278 struct tda18212_priv *priv = NULL; 279 int ret; 280 u8 uninitialized_var(val); 281 282 priv = kzalloc(sizeof(struct tda18212_priv), GFP_KERNEL); 283 if (priv == NULL) 284 return NULL; 285 286 priv->cfg = cfg; 287 priv->i2c = i2c; 288 fe->tuner_priv = priv; 289 290 if (fe->ops.i2c_gate_ctrl) 291 fe->ops.i2c_gate_ctrl(fe, 1); /* open I2C-gate */ 292 293 /* check if the tuner is there */ 294 ret = tda18212_rd_reg(priv, 0x00, &val); 295 296 if (fe->ops.i2c_gate_ctrl) 297 fe->ops.i2c_gate_ctrl(fe, 0); /* close I2C-gate */ 298 299 dev_dbg(&priv->i2c->dev, "%s: ret=%d chip id=%02x\n", __func__, ret, 300 val); 301 if (ret || val != 0xc7) { 302 kfree(priv); 303 return NULL; 304 } 305 306 dev_info(&priv->i2c->dev, 307 "%s: NXP TDA18212HN successfully identified\n", 308 KBUILD_MODNAME); 309 310 memcpy(&fe->ops.tuner_ops, &tda18212_tuner_ops, 311 sizeof(struct dvb_tuner_ops)); 312 313 return fe; 314 } 315 EXPORT_SYMBOL(tda18212_attach); 316 317 MODULE_DESCRIPTION("NXP TDA18212HN silicon tuner driver"); 318 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>"); 319 MODULE_LICENSE("GPL"); 320