Antti Palosaari | 695efd0 | 2013-02-25 08:43:32 -0300 | [diff] [blame] | 1 | /* |
| 2 | * Montage M88TS2022 silicon tuner driver |
| 3 | * |
| 4 | * Copyright (C) 2013 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 | * |
Antti Palosaari | 695efd0 | 2013-02-25 08:43:32 -0300 | [diff] [blame] | 16 | * Some calculations are taken from existing TS2020 driver. |
| 17 | */ |
| 18 | |
| 19 | #include "m88ts2022_priv.h" |
| 20 | |
| 21 | /* write multiple registers */ |
| 22 | static int m88ts2022_wr_regs(struct m88ts2022_priv *priv, |
| 23 | u8 reg, const u8 *val, int len) |
| 24 | { |
Antti Palosaari | 050ae82 | 2013-11-07 17:01:31 -0300 | [diff] [blame] | 25 | #define MAX_WR_LEN 3 |
| 26 | #define MAX_WR_XFER_LEN (MAX_WR_LEN + 1) |
Antti Palosaari | 695efd0 | 2013-02-25 08:43:32 -0300 | [diff] [blame] | 27 | int ret; |
Antti Palosaari | 050ae82 | 2013-11-07 17:01:31 -0300 | [diff] [blame] | 28 | u8 buf[MAX_WR_XFER_LEN]; |
Antti Palosaari | 695efd0 | 2013-02-25 08:43:32 -0300 | [diff] [blame] | 29 | struct i2c_msg msg[1] = { |
| 30 | { |
Antti Palosaari | eafa2ad | 2013-12-02 18:38:41 -0300 | [diff] [blame] | 31 | .addr = priv->client->addr, |
Antti Palosaari | 695efd0 | 2013-02-25 08:43:32 -0300 | [diff] [blame] | 32 | .flags = 0, |
Antti Palosaari | 050ae82 | 2013-11-07 17:01:31 -0300 | [diff] [blame] | 33 | .len = 1 + len, |
Antti Palosaari | 695efd0 | 2013-02-25 08:43:32 -0300 | [diff] [blame] | 34 | .buf = buf, |
| 35 | } |
| 36 | }; |
| 37 | |
Antti Palosaari | 050ae82 | 2013-11-07 17:01:31 -0300 | [diff] [blame] | 38 | if (WARN_ON(len > MAX_WR_LEN)) |
| 39 | return -EINVAL; |
| 40 | |
Antti Palosaari | 695efd0 | 2013-02-25 08:43:32 -0300 | [diff] [blame] | 41 | buf[0] = reg; |
| 42 | memcpy(&buf[1], val, len); |
| 43 | |
Antti Palosaari | eafa2ad | 2013-12-02 18:38:41 -0300 | [diff] [blame] | 44 | ret = i2c_transfer(priv->client->adapter, msg, 1); |
Antti Palosaari | 695efd0 | 2013-02-25 08:43:32 -0300 | [diff] [blame] | 45 | if (ret == 1) { |
| 46 | ret = 0; |
| 47 | } else { |
Antti Palosaari | eafa2ad | 2013-12-02 18:38:41 -0300 | [diff] [blame] | 48 | dev_warn(&priv->client->dev, |
Antti Palosaari | 695efd0 | 2013-02-25 08:43:32 -0300 | [diff] [blame] | 49 | "%s: i2c wr failed=%d reg=%02x len=%d\n", |
| 50 | KBUILD_MODNAME, ret, reg, len); |
| 51 | ret = -EREMOTEIO; |
| 52 | } |
| 53 | |
| 54 | return ret; |
| 55 | } |
| 56 | |
| 57 | /* read multiple registers */ |
| 58 | static int m88ts2022_rd_regs(struct m88ts2022_priv *priv, u8 reg, |
| 59 | u8 *val, int len) |
| 60 | { |
Antti Palosaari | 050ae82 | 2013-11-07 17:01:31 -0300 | [diff] [blame] | 61 | #define MAX_RD_LEN 1 |
| 62 | #define MAX_RD_XFER_LEN (MAX_RD_LEN) |
Antti Palosaari | 695efd0 | 2013-02-25 08:43:32 -0300 | [diff] [blame] | 63 | int ret; |
Antti Palosaari | 050ae82 | 2013-11-07 17:01:31 -0300 | [diff] [blame] | 64 | u8 buf[MAX_RD_XFER_LEN]; |
Antti Palosaari | 695efd0 | 2013-02-25 08:43:32 -0300 | [diff] [blame] | 65 | struct i2c_msg msg[2] = { |
| 66 | { |
Antti Palosaari | eafa2ad | 2013-12-02 18:38:41 -0300 | [diff] [blame] | 67 | .addr = priv->client->addr, |
Antti Palosaari | 695efd0 | 2013-02-25 08:43:32 -0300 | [diff] [blame] | 68 | .flags = 0, |
| 69 | .len = 1, |
| 70 | .buf = ®, |
| 71 | }, { |
Antti Palosaari | eafa2ad | 2013-12-02 18:38:41 -0300 | [diff] [blame] | 72 | .addr = priv->client->addr, |
Antti Palosaari | 695efd0 | 2013-02-25 08:43:32 -0300 | [diff] [blame] | 73 | .flags = I2C_M_RD, |
Antti Palosaari | 050ae82 | 2013-11-07 17:01:31 -0300 | [diff] [blame] | 74 | .len = len, |
Antti Palosaari | 695efd0 | 2013-02-25 08:43:32 -0300 | [diff] [blame] | 75 | .buf = buf, |
| 76 | } |
| 77 | }; |
| 78 | |
Antti Palosaari | 050ae82 | 2013-11-07 17:01:31 -0300 | [diff] [blame] | 79 | if (WARN_ON(len > MAX_RD_LEN)) |
| 80 | return -EINVAL; |
| 81 | |
Antti Palosaari | eafa2ad | 2013-12-02 18:38:41 -0300 | [diff] [blame] | 82 | ret = i2c_transfer(priv->client->adapter, msg, 2); |
Antti Palosaari | 695efd0 | 2013-02-25 08:43:32 -0300 | [diff] [blame] | 83 | if (ret == 2) { |
| 84 | memcpy(val, buf, len); |
| 85 | ret = 0; |
| 86 | } else { |
Antti Palosaari | eafa2ad | 2013-12-02 18:38:41 -0300 | [diff] [blame] | 87 | dev_warn(&priv->client->dev, |
Antti Palosaari | 695efd0 | 2013-02-25 08:43:32 -0300 | [diff] [blame] | 88 | "%s: i2c rd failed=%d reg=%02x len=%d\n", |
| 89 | KBUILD_MODNAME, ret, reg, len); |
| 90 | ret = -EREMOTEIO; |
| 91 | } |
| 92 | |
| 93 | return ret; |
| 94 | } |
| 95 | |
| 96 | /* write single register */ |
| 97 | static int m88ts2022_wr_reg(struct m88ts2022_priv *priv, u8 reg, u8 val) |
| 98 | { |
| 99 | return m88ts2022_wr_regs(priv, reg, &val, 1); |
| 100 | } |
| 101 | |
| 102 | /* read single register */ |
| 103 | static int m88ts2022_rd_reg(struct m88ts2022_priv *priv, u8 reg, u8 *val) |
| 104 | { |
| 105 | return m88ts2022_rd_regs(priv, reg, val, 1); |
| 106 | } |
| 107 | |
| 108 | /* write single register with mask */ |
| 109 | static int m88ts2022_wr_reg_mask(struct m88ts2022_priv *priv, |
| 110 | u8 reg, u8 val, u8 mask) |
| 111 | { |
| 112 | int ret; |
| 113 | u8 u8tmp; |
| 114 | |
| 115 | /* no need for read if whole reg is written */ |
| 116 | if (mask != 0xff) { |
| 117 | ret = m88ts2022_rd_regs(priv, reg, &u8tmp, 1); |
| 118 | if (ret) |
| 119 | return ret; |
| 120 | |
| 121 | val &= mask; |
| 122 | u8tmp &= ~mask; |
| 123 | val |= u8tmp; |
| 124 | } |
| 125 | |
| 126 | return m88ts2022_wr_regs(priv, reg, &val, 1); |
| 127 | } |
| 128 | |
| 129 | static int m88ts2022_cmd(struct dvb_frontend *fe, |
| 130 | int op, int sleep, u8 reg, u8 mask, u8 val, u8 *reg_val) |
| 131 | { |
| 132 | struct m88ts2022_priv *priv = fe->tuner_priv; |
| 133 | int ret, i; |
| 134 | u8 u8tmp; |
| 135 | struct m88ts2022_reg_val reg_vals[] = { |
| 136 | {0x51, 0x1f - op}, |
| 137 | {0x51, 0x1f}, |
| 138 | {0x50, 0x00 + op}, |
| 139 | {0x50, 0x00}, |
| 140 | }; |
| 141 | |
| 142 | for (i = 0; i < 2; i++) { |
Antti Palosaari | eafa2ad | 2013-12-02 18:38:41 -0300 | [diff] [blame] | 143 | dev_dbg(&priv->client->dev, |
Antti Palosaari | 695efd0 | 2013-02-25 08:43:32 -0300 | [diff] [blame] | 144 | "%s: i=%d op=%02x reg=%02x mask=%02x val=%02x\n", |
| 145 | __func__, i, op, reg, mask, val); |
| 146 | |
| 147 | for (i = 0; i < ARRAY_SIZE(reg_vals); i++) { |
| 148 | ret = m88ts2022_wr_reg(priv, reg_vals[i].reg, |
| 149 | reg_vals[i].val); |
| 150 | if (ret) |
| 151 | goto err; |
| 152 | } |
| 153 | |
| 154 | usleep_range(sleep * 1000, sleep * 10000); |
| 155 | |
| 156 | ret = m88ts2022_rd_reg(priv, reg, &u8tmp); |
| 157 | if (ret) |
| 158 | goto err; |
| 159 | |
| 160 | if ((u8tmp & mask) != val) |
| 161 | break; |
| 162 | } |
| 163 | |
| 164 | if (reg_val) |
| 165 | *reg_val = u8tmp; |
| 166 | err: |
| 167 | return ret; |
| 168 | } |
| 169 | |
| 170 | static int m88ts2022_set_params(struct dvb_frontend *fe) |
| 171 | { |
| 172 | struct m88ts2022_priv *priv = fe->tuner_priv; |
| 173 | struct dtv_frontend_properties *c = &fe->dtv_property_cache; |
Antti Palosaari | c19314b | 2013-12-02 01:39:24 -0300 | [diff] [blame] | 174 | int ret; |
| 175 | unsigned int frequency_khz, frequency_offset_khz, f_3db_hz; |
| 176 | unsigned int f_ref_khz, f_vco_khz, div_ref, div_out, pll_n, gdiv28; |
| 177 | u8 buf[3], u8tmp, cap_code, lpf_gm, lpf_mxdiv, div_max, div_min; |
| 178 | u16 u16tmp; |
Antti Palosaari | eafa2ad | 2013-12-02 18:38:41 -0300 | [diff] [blame] | 179 | dev_dbg(&priv->client->dev, |
Antti Palosaari | 695efd0 | 2013-02-25 08:43:32 -0300 | [diff] [blame] | 180 | "%s: frequency=%d symbol_rate=%d rolloff=%d\n", |
| 181 | __func__, c->frequency, c->symbol_rate, c->rolloff); |
Antti Palosaari | c19314b | 2013-12-02 01:39:24 -0300 | [diff] [blame] | 182 | /* |
| 183 | * Integer-N PLL synthesizer |
| 184 | * kHz is used for all calculations to keep calculations within 32-bit |
| 185 | */ |
Antti Palosaari | eafa2ad | 2013-12-02 18:38:41 -0300 | [diff] [blame] | 186 | f_ref_khz = DIV_ROUND_CLOSEST(priv->cfg.clock, 1000); |
Antti Palosaari | c19314b | 2013-12-02 01:39:24 -0300 | [diff] [blame] | 187 | div_ref = DIV_ROUND_CLOSEST(f_ref_khz, 2000); |
Antti Palosaari | 695efd0 | 2013-02-25 08:43:32 -0300 | [diff] [blame] | 188 | |
| 189 | if (c->symbol_rate < 5000000) |
Antti Palosaari | c19314b | 2013-12-02 01:39:24 -0300 | [diff] [blame] | 190 | frequency_offset_khz = 3000; /* 3 MHz */ |
Antti Palosaari | 695efd0 | 2013-02-25 08:43:32 -0300 | [diff] [blame] | 191 | else |
Antti Palosaari | c19314b | 2013-12-02 01:39:24 -0300 | [diff] [blame] | 192 | frequency_offset_khz = 0; |
Antti Palosaari | 695efd0 | 2013-02-25 08:43:32 -0300 | [diff] [blame] | 193 | |
Antti Palosaari | c19314b | 2013-12-02 01:39:24 -0300 | [diff] [blame] | 194 | frequency_khz = c->frequency + frequency_offset_khz; |
Antti Palosaari | 695efd0 | 2013-02-25 08:43:32 -0300 | [diff] [blame] | 195 | |
| 196 | if (frequency_khz < 1103000) { |
Antti Palosaari | c19314b | 2013-12-02 01:39:24 -0300 | [diff] [blame] | 197 | div_out = 4; |
Antti Palosaari | 695efd0 | 2013-02-25 08:43:32 -0300 | [diff] [blame] | 198 | u8tmp = 0x1b; |
| 199 | } else { |
Antti Palosaari | c19314b | 2013-12-02 01:39:24 -0300 | [diff] [blame] | 200 | div_out = 2; |
Antti Palosaari | 695efd0 | 2013-02-25 08:43:32 -0300 | [diff] [blame] | 201 | u8tmp = 0x0b; |
| 202 | } |
| 203 | |
| 204 | buf[0] = u8tmp; |
| 205 | buf[1] = 0x40; |
| 206 | ret = m88ts2022_wr_regs(priv, 0x10, buf, 2); |
| 207 | if (ret) |
| 208 | goto err; |
| 209 | |
Antti Palosaari | c19314b | 2013-12-02 01:39:24 -0300 | [diff] [blame] | 210 | f_vco_khz = frequency_khz * div_out; |
| 211 | pll_n = f_vco_khz * div_ref / f_ref_khz; |
| 212 | pll_n += pll_n % 2; |
| 213 | priv->frequency_khz = pll_n * f_ref_khz / div_ref / div_out; |
Antti Palosaari | 695efd0 | 2013-02-25 08:43:32 -0300 | [diff] [blame] | 214 | |
Antti Palosaari | c19314b | 2013-12-02 01:39:24 -0300 | [diff] [blame] | 215 | if (pll_n < 4095) |
| 216 | u16tmp = pll_n - 1024; |
| 217 | else if (pll_n < 6143) |
| 218 | u16tmp = pll_n + 1024; |
Antti Palosaari | 695efd0 | 2013-02-25 08:43:32 -0300 | [diff] [blame] | 219 | else |
Antti Palosaari | c19314b | 2013-12-02 01:39:24 -0300 | [diff] [blame] | 220 | u16tmp = pll_n + 3072; |
Antti Palosaari | 695efd0 | 2013-02-25 08:43:32 -0300 | [diff] [blame] | 221 | |
Antti Palosaari | c19314b | 2013-12-02 01:39:24 -0300 | [diff] [blame] | 222 | buf[0] = (u16tmp >> 8) & 0x3f; |
| 223 | buf[1] = (u16tmp >> 0) & 0xff; |
| 224 | buf[2] = div_ref - 8; |
Antti Palosaari | 695efd0 | 2013-02-25 08:43:32 -0300 | [diff] [blame] | 225 | ret = m88ts2022_wr_regs(priv, 0x01, buf, 3); |
| 226 | if (ret) |
| 227 | goto err; |
| 228 | |
Antti Palosaari | eafa2ad | 2013-12-02 18:38:41 -0300 | [diff] [blame] | 229 | dev_dbg(&priv->client->dev, |
Antti Palosaari | c19314b | 2013-12-02 01:39:24 -0300 | [diff] [blame] | 230 | "%s: frequency=%u offset=%d f_vco_khz=%u pll_n=%u div_ref=%u div_out=%u\n", |
Antti Palosaari | 695efd0 | 2013-02-25 08:43:32 -0300 | [diff] [blame] | 231 | __func__, priv->frequency_khz, |
Antti Palosaari | c19314b | 2013-12-02 01:39:24 -0300 | [diff] [blame] | 232 | priv->frequency_khz - c->frequency, f_vco_khz, pll_n, |
| 233 | div_ref, div_out); |
Antti Palosaari | 695efd0 | 2013-02-25 08:43:32 -0300 | [diff] [blame] | 234 | |
| 235 | ret = m88ts2022_cmd(fe, 0x10, 5, 0x15, 0x40, 0x00, NULL); |
| 236 | if (ret) |
| 237 | goto err; |
| 238 | |
| 239 | ret = m88ts2022_rd_reg(priv, 0x14, &u8tmp); |
| 240 | if (ret) |
| 241 | goto err; |
| 242 | |
| 243 | u8tmp &= 0x7f; |
| 244 | if (u8tmp < 64) { |
| 245 | ret = m88ts2022_wr_reg_mask(priv, 0x10, 0x80, 0x80); |
| 246 | if (ret) |
| 247 | goto err; |
| 248 | |
| 249 | ret = m88ts2022_wr_reg(priv, 0x11, 0x6f); |
| 250 | if (ret) |
| 251 | goto err; |
| 252 | |
| 253 | ret = m88ts2022_cmd(fe, 0x10, 5, 0x15, 0x40, 0x00, NULL); |
| 254 | if (ret) |
| 255 | goto err; |
| 256 | } |
| 257 | |
| 258 | ret = m88ts2022_rd_reg(priv, 0x14, &u8tmp); |
| 259 | if (ret) |
| 260 | goto err; |
| 261 | |
| 262 | u8tmp &= 0x1f; |
| 263 | if (u8tmp > 19) { |
| 264 | ret = m88ts2022_wr_reg_mask(priv, 0x10, 0x00, 0x02); |
| 265 | if (ret) |
| 266 | goto err; |
| 267 | } |
| 268 | |
| 269 | ret = m88ts2022_cmd(fe, 0x08, 5, 0x3c, 0xff, 0x00, NULL); |
| 270 | if (ret) |
| 271 | goto err; |
| 272 | |
| 273 | ret = m88ts2022_wr_reg(priv, 0x25, 0x00); |
| 274 | if (ret) |
| 275 | goto err; |
| 276 | |
| 277 | ret = m88ts2022_wr_reg(priv, 0x27, 0x70); |
| 278 | if (ret) |
| 279 | goto err; |
| 280 | |
| 281 | ret = m88ts2022_wr_reg(priv, 0x41, 0x09); |
| 282 | if (ret) |
| 283 | goto err; |
| 284 | |
| 285 | ret = m88ts2022_wr_reg(priv, 0x08, 0x0b); |
| 286 | if (ret) |
| 287 | goto err; |
| 288 | |
Antti Palosaari | c19314b | 2013-12-02 01:39:24 -0300 | [diff] [blame] | 289 | /* filters */ |
| 290 | gdiv28 = DIV_ROUND_CLOSEST(f_ref_khz * 1694U, 1000000U); |
Antti Palosaari | 695efd0 | 2013-02-25 08:43:32 -0300 | [diff] [blame] | 291 | |
| 292 | ret = m88ts2022_wr_reg(priv, 0x04, gdiv28); |
| 293 | if (ret) |
| 294 | goto err; |
| 295 | |
| 296 | ret = m88ts2022_cmd(fe, 0x04, 2, 0x26, 0xff, 0x00, &u8tmp); |
| 297 | if (ret) |
| 298 | goto err; |
| 299 | |
| 300 | cap_code = u8tmp & 0x3f; |
| 301 | |
| 302 | ret = m88ts2022_wr_reg(priv, 0x41, 0x0d); |
| 303 | if (ret) |
| 304 | goto err; |
| 305 | |
| 306 | ret = m88ts2022_cmd(fe, 0x04, 2, 0x26, 0xff, 0x00, &u8tmp); |
| 307 | if (ret) |
| 308 | goto err; |
| 309 | |
| 310 | u8tmp &= 0x3f; |
| 311 | cap_code = (cap_code + u8tmp) / 2; |
| 312 | gdiv28 = gdiv28 * 207 / (cap_code * 2 + 151); |
| 313 | div_max = gdiv28 * 135 / 100; |
| 314 | div_min = gdiv28 * 78 / 100; |
Antti Palosaari | c19314b | 2013-12-02 01:39:24 -0300 | [diff] [blame] | 315 | div_max = clamp_val(div_max, 0U, 63U); |
Antti Palosaari | 695efd0 | 2013-02-25 08:43:32 -0300 | [diff] [blame] | 316 | |
Antti Palosaari | c19314b | 2013-12-02 01:39:24 -0300 | [diff] [blame] | 317 | f_3db_hz = c->symbol_rate * 135UL / 200UL; |
| 318 | f_3db_hz += 2000000U + (frequency_offset_khz * 1000U); |
| 319 | f_3db_hz = clamp(f_3db_hz, 7000000U, 40000000U); |
Antti Palosaari | 695efd0 | 2013-02-25 08:43:32 -0300 | [diff] [blame] | 320 | |
Antti Palosaari | c19314b | 2013-12-02 01:39:24 -0300 | [diff] [blame] | 321 | #define LPF_COEFF 3200U |
| 322 | lpf_gm = DIV_ROUND_CLOSEST(f_3db_hz * gdiv28, LPF_COEFF * f_ref_khz); |
| 323 | lpf_gm = clamp_val(lpf_gm, 1U, 23U); |
Antti Palosaari | 695efd0 | 2013-02-25 08:43:32 -0300 | [diff] [blame] | 324 | |
Antti Palosaari | c19314b | 2013-12-02 01:39:24 -0300 | [diff] [blame] | 325 | lpf_mxdiv = DIV_ROUND_CLOSEST(lpf_gm * LPF_COEFF * f_ref_khz, f_3db_hz); |
| 326 | if (lpf_mxdiv < div_min) |
| 327 | lpf_mxdiv = DIV_ROUND_CLOSEST(++lpf_gm * LPF_COEFF * f_ref_khz, f_3db_hz); |
| 328 | lpf_mxdiv = clamp_val(lpf_mxdiv, 0U, div_max); |
Antti Palosaari | 695efd0 | 2013-02-25 08:43:32 -0300 | [diff] [blame] | 329 | |
| 330 | ret = m88ts2022_wr_reg(priv, 0x04, lpf_mxdiv); |
| 331 | if (ret) |
| 332 | goto err; |
| 333 | |
| 334 | ret = m88ts2022_wr_reg(priv, 0x06, lpf_gm); |
| 335 | if (ret) |
| 336 | goto err; |
| 337 | |
| 338 | ret = m88ts2022_cmd(fe, 0x04, 2, 0x26, 0xff, 0x00, &u8tmp); |
| 339 | if (ret) |
| 340 | goto err; |
| 341 | |
| 342 | cap_code = u8tmp & 0x3f; |
| 343 | |
| 344 | ret = m88ts2022_wr_reg(priv, 0x41, 0x09); |
| 345 | if (ret) |
| 346 | goto err; |
| 347 | |
| 348 | ret = m88ts2022_cmd(fe, 0x04, 2, 0x26, 0xff, 0x00, &u8tmp); |
| 349 | if (ret) |
| 350 | goto err; |
| 351 | |
| 352 | u8tmp &= 0x3f; |
| 353 | cap_code = (cap_code + u8tmp) / 2; |
| 354 | |
| 355 | u8tmp = cap_code | 0x80; |
| 356 | ret = m88ts2022_wr_reg(priv, 0x25, u8tmp); |
| 357 | if (ret) |
| 358 | goto err; |
| 359 | |
| 360 | ret = m88ts2022_wr_reg(priv, 0x27, 0x30); |
| 361 | if (ret) |
| 362 | goto err; |
| 363 | |
| 364 | ret = m88ts2022_wr_reg(priv, 0x08, 0x09); |
| 365 | if (ret) |
| 366 | goto err; |
| 367 | |
| 368 | ret = m88ts2022_cmd(fe, 0x01, 20, 0x21, 0xff, 0x00, NULL); |
| 369 | if (ret) |
| 370 | goto err; |
| 371 | err: |
| 372 | if (ret) |
Antti Palosaari | eafa2ad | 2013-12-02 18:38:41 -0300 | [diff] [blame] | 373 | dev_dbg(&priv->client->dev, "%s: failed=%d\n", __func__, ret); |
Antti Palosaari | 695efd0 | 2013-02-25 08:43:32 -0300 | [diff] [blame] | 374 | |
| 375 | return ret; |
| 376 | } |
| 377 | |
| 378 | static int m88ts2022_init(struct dvb_frontend *fe) |
| 379 | { |
| 380 | struct m88ts2022_priv *priv = fe->tuner_priv; |
| 381 | int ret, i; |
| 382 | u8 u8tmp; |
| 383 | static const struct m88ts2022_reg_val reg_vals[] = { |
| 384 | {0x7d, 0x9d}, |
| 385 | {0x7c, 0x9a}, |
| 386 | {0x7a, 0x76}, |
| 387 | {0x3b, 0x01}, |
| 388 | {0x63, 0x88}, |
| 389 | {0x61, 0x85}, |
| 390 | {0x22, 0x30}, |
| 391 | {0x30, 0x40}, |
| 392 | {0x20, 0x23}, |
| 393 | {0x24, 0x02}, |
| 394 | {0x12, 0xa0}, |
| 395 | }; |
Antti Palosaari | eafa2ad | 2013-12-02 18:38:41 -0300 | [diff] [blame] | 396 | dev_dbg(&priv->client->dev, "%s:\n", __func__); |
Antti Palosaari | 695efd0 | 2013-02-25 08:43:32 -0300 | [diff] [blame] | 397 | |
| 398 | ret = m88ts2022_wr_reg(priv, 0x00, 0x01); |
| 399 | if (ret) |
| 400 | goto err; |
| 401 | |
| 402 | ret = m88ts2022_wr_reg(priv, 0x00, 0x03); |
| 403 | if (ret) |
| 404 | goto err; |
| 405 | |
Antti Palosaari | eafa2ad | 2013-12-02 18:38:41 -0300 | [diff] [blame] | 406 | switch (priv->cfg.clock_out) { |
Antti Palosaari | 695efd0 | 2013-02-25 08:43:32 -0300 | [diff] [blame] | 407 | case M88TS2022_CLOCK_OUT_DISABLED: |
| 408 | u8tmp = 0x60; |
| 409 | break; |
| 410 | case M88TS2022_CLOCK_OUT_ENABLED: |
| 411 | u8tmp = 0x70; |
Antti Palosaari | eafa2ad | 2013-12-02 18:38:41 -0300 | [diff] [blame] | 412 | ret = m88ts2022_wr_reg(priv, 0x05, priv->cfg.clock_out_div); |
Antti Palosaari | 695efd0 | 2013-02-25 08:43:32 -0300 | [diff] [blame] | 413 | if (ret) |
| 414 | goto err; |
| 415 | break; |
| 416 | case M88TS2022_CLOCK_OUT_ENABLED_XTALOUT: |
| 417 | u8tmp = 0x6c; |
| 418 | break; |
| 419 | default: |
| 420 | goto err; |
| 421 | } |
| 422 | |
| 423 | ret = m88ts2022_wr_reg(priv, 0x42, u8tmp); |
| 424 | if (ret) |
| 425 | goto err; |
| 426 | |
Antti Palosaari | eafa2ad | 2013-12-02 18:38:41 -0300 | [diff] [blame] | 427 | if (priv->cfg.loop_through) |
Antti Palosaari | 695efd0 | 2013-02-25 08:43:32 -0300 | [diff] [blame] | 428 | u8tmp = 0xec; |
| 429 | else |
| 430 | u8tmp = 0x6c; |
| 431 | |
| 432 | ret = m88ts2022_wr_reg(priv, 0x62, u8tmp); |
| 433 | if (ret) |
| 434 | goto err; |
| 435 | |
| 436 | for (i = 0; i < ARRAY_SIZE(reg_vals); i++) { |
| 437 | ret = m88ts2022_wr_reg(priv, reg_vals[i].reg, reg_vals[i].val); |
| 438 | if (ret) |
| 439 | goto err; |
| 440 | } |
| 441 | err: |
| 442 | if (ret) |
Antti Palosaari | eafa2ad | 2013-12-02 18:38:41 -0300 | [diff] [blame] | 443 | dev_dbg(&priv->client->dev, "%s: failed=%d\n", __func__, ret); |
Antti Palosaari | 695efd0 | 2013-02-25 08:43:32 -0300 | [diff] [blame] | 444 | return ret; |
| 445 | } |
| 446 | |
| 447 | static int m88ts2022_sleep(struct dvb_frontend *fe) |
| 448 | { |
| 449 | struct m88ts2022_priv *priv = fe->tuner_priv; |
| 450 | int ret; |
Antti Palosaari | eafa2ad | 2013-12-02 18:38:41 -0300 | [diff] [blame] | 451 | dev_dbg(&priv->client->dev, "%s:\n", __func__); |
Antti Palosaari | 695efd0 | 2013-02-25 08:43:32 -0300 | [diff] [blame] | 452 | |
| 453 | ret = m88ts2022_wr_reg(priv, 0x00, 0x00); |
| 454 | if (ret) |
| 455 | goto err; |
| 456 | err: |
| 457 | if (ret) |
Antti Palosaari | eafa2ad | 2013-12-02 18:38:41 -0300 | [diff] [blame] | 458 | dev_dbg(&priv->client->dev, "%s: failed=%d\n", __func__, ret); |
Antti Palosaari | 695efd0 | 2013-02-25 08:43:32 -0300 | [diff] [blame] | 459 | return ret; |
| 460 | } |
| 461 | |
| 462 | static int m88ts2022_get_frequency(struct dvb_frontend *fe, u32 *frequency) |
| 463 | { |
| 464 | struct m88ts2022_priv *priv = fe->tuner_priv; |
Antti Palosaari | eafa2ad | 2013-12-02 18:38:41 -0300 | [diff] [blame] | 465 | dev_dbg(&priv->client->dev, "%s:\n", __func__); |
Antti Palosaari | 695efd0 | 2013-02-25 08:43:32 -0300 | [diff] [blame] | 466 | |
| 467 | *frequency = priv->frequency_khz; |
| 468 | return 0; |
| 469 | } |
| 470 | |
| 471 | static int m88ts2022_get_if_frequency(struct dvb_frontend *fe, u32 *frequency) |
| 472 | { |
| 473 | struct m88ts2022_priv *priv = fe->tuner_priv; |
Antti Palosaari | eafa2ad | 2013-12-02 18:38:41 -0300 | [diff] [blame] | 474 | dev_dbg(&priv->client->dev, "%s:\n", __func__); |
Antti Palosaari | 695efd0 | 2013-02-25 08:43:32 -0300 | [diff] [blame] | 475 | |
| 476 | *frequency = 0; /* Zero-IF */ |
| 477 | return 0; |
| 478 | } |
| 479 | |
| 480 | static int m88ts2022_get_rf_strength(struct dvb_frontend *fe, u16 *strength) |
| 481 | { |
| 482 | struct m88ts2022_priv *priv = fe->tuner_priv; |
Antti Palosaari | 695efd0 | 2013-02-25 08:43:32 -0300 | [diff] [blame] | 483 | int ret; |
Antti Palosaari | c19314b | 2013-12-02 01:39:24 -0300 | [diff] [blame] | 484 | u8 u8tmp; |
| 485 | u16 gain, u16tmp; |
| 486 | unsigned int gain1, gain2, gain3; |
Antti Palosaari | 695efd0 | 2013-02-25 08:43:32 -0300 | [diff] [blame] | 487 | |
| 488 | ret = m88ts2022_rd_reg(priv, 0x3d, &u8tmp); |
| 489 | if (ret) |
| 490 | goto err; |
| 491 | |
| 492 | gain1 = (u8tmp >> 0) & 0x1f; |
Antti Palosaari | c19314b | 2013-12-02 01:39:24 -0300 | [diff] [blame] | 493 | gain1 = clamp(gain1, 0U, 15U); |
Antti Palosaari | 695efd0 | 2013-02-25 08:43:32 -0300 | [diff] [blame] | 494 | |
| 495 | ret = m88ts2022_rd_reg(priv, 0x21, &u8tmp); |
| 496 | if (ret) |
| 497 | goto err; |
| 498 | |
| 499 | gain2 = (u8tmp >> 0) & 0x1f; |
Antti Palosaari | c19314b | 2013-12-02 01:39:24 -0300 | [diff] [blame] | 500 | gain2 = clamp(gain2, 2U, 16U); |
Antti Palosaari | 695efd0 | 2013-02-25 08:43:32 -0300 | [diff] [blame] | 501 | |
| 502 | ret = m88ts2022_rd_reg(priv, 0x66, &u8tmp); |
| 503 | if (ret) |
| 504 | goto err; |
| 505 | |
| 506 | gain3 = (u8tmp >> 3) & 0x07; |
Antti Palosaari | c19314b | 2013-12-02 01:39:24 -0300 | [diff] [blame] | 507 | gain3 = clamp(gain3, 0U, 6U); |
Antti Palosaari | 695efd0 | 2013-02-25 08:43:32 -0300 | [diff] [blame] | 508 | |
| 509 | gain = gain1 * 265 + gain2 * 338 + gain3 * 285; |
| 510 | |
| 511 | /* scale value to 0x0000-0xffff */ |
| 512 | u16tmp = (0xffff - gain); |
Antti Palosaari | c19314b | 2013-12-02 01:39:24 -0300 | [diff] [blame] | 513 | u16tmp = clamp_val(u16tmp, 59000U, 61500U); |
Antti Palosaari | 695efd0 | 2013-02-25 08:43:32 -0300 | [diff] [blame] | 514 | |
| 515 | *strength = (u16tmp - 59000) * 0xffff / (61500 - 59000); |
| 516 | err: |
| 517 | if (ret) |
Antti Palosaari | eafa2ad | 2013-12-02 18:38:41 -0300 | [diff] [blame] | 518 | dev_dbg(&priv->client->dev, "%s: failed=%d\n", __func__, ret); |
Antti Palosaari | 695efd0 | 2013-02-25 08:43:32 -0300 | [diff] [blame] | 519 | return ret; |
| 520 | } |
| 521 | |
Antti Palosaari | 695efd0 | 2013-02-25 08:43:32 -0300 | [diff] [blame] | 522 | static const struct dvb_tuner_ops m88ts2022_tuner_ops = { |
| 523 | .info = { |
| 524 | .name = "Montage M88TS2022", |
| 525 | .frequency_min = 950000, |
| 526 | .frequency_max = 2150000, |
| 527 | }, |
| 528 | |
Antti Palosaari | 695efd0 | 2013-02-25 08:43:32 -0300 | [diff] [blame] | 529 | .init = m88ts2022_init, |
| 530 | .sleep = m88ts2022_sleep, |
| 531 | .set_params = m88ts2022_set_params, |
| 532 | |
| 533 | .get_frequency = m88ts2022_get_frequency, |
| 534 | .get_if_frequency = m88ts2022_get_if_frequency, |
| 535 | .get_rf_strength = m88ts2022_get_rf_strength, |
| 536 | }; |
| 537 | |
Antti Palosaari | eafa2ad | 2013-12-02 18:38:41 -0300 | [diff] [blame] | 538 | static int m88ts2022_probe(struct i2c_client *client, |
| 539 | const struct i2c_device_id *id) |
Antti Palosaari | 695efd0 | 2013-02-25 08:43:32 -0300 | [diff] [blame] | 540 | { |
Antti Palosaari | eafa2ad | 2013-12-02 18:38:41 -0300 | [diff] [blame] | 541 | struct m88ts2022_config *cfg = client->dev.platform_data; |
| 542 | struct dvb_frontend *fe = cfg->fe; |
Antti Palosaari | 695efd0 | 2013-02-25 08:43:32 -0300 | [diff] [blame] | 543 | struct m88ts2022_priv *priv; |
| 544 | int ret; |
| 545 | u8 chip_id, u8tmp; |
| 546 | |
Antti Palosaari | b03129f | 2013-12-18 12:46:45 -0300 | [diff] [blame] | 547 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
Antti Palosaari | 695efd0 | 2013-02-25 08:43:32 -0300 | [diff] [blame] | 548 | if (!priv) { |
| 549 | ret = -ENOMEM; |
Antti Palosaari | eafa2ad | 2013-12-02 18:38:41 -0300 | [diff] [blame] | 550 | dev_err(&client->dev, "%s: kzalloc() failed\n", KBUILD_MODNAME); |
Antti Palosaari | 695efd0 | 2013-02-25 08:43:32 -0300 | [diff] [blame] | 551 | goto err; |
| 552 | } |
| 553 | |
Antti Palosaari | eafa2ad | 2013-12-02 18:38:41 -0300 | [diff] [blame] | 554 | memcpy(&priv->cfg, cfg, sizeof(struct m88ts2022_config)); |
| 555 | priv->client = client; |
Antti Palosaari | 695efd0 | 2013-02-25 08:43:32 -0300 | [diff] [blame] | 556 | |
| 557 | /* check if the tuner is there */ |
| 558 | ret = m88ts2022_rd_reg(priv, 0x00, &u8tmp); |
| 559 | if (ret) |
| 560 | goto err; |
| 561 | |
| 562 | if ((u8tmp & 0x03) == 0x00) { |
| 563 | ret = m88ts2022_wr_reg(priv, 0x00, 0x01); |
| 564 | if (ret < 0) |
| 565 | goto err; |
| 566 | |
| 567 | usleep_range(2000, 50000); |
| 568 | } |
| 569 | |
| 570 | ret = m88ts2022_wr_reg(priv, 0x00, 0x03); |
| 571 | if (ret) |
| 572 | goto err; |
| 573 | |
| 574 | usleep_range(2000, 50000); |
| 575 | |
| 576 | ret = m88ts2022_rd_reg(priv, 0x00, &chip_id); |
| 577 | if (ret) |
| 578 | goto err; |
| 579 | |
Antti Palosaari | eafa2ad | 2013-12-02 18:38:41 -0300 | [diff] [blame] | 580 | dev_dbg(&priv->client->dev, "%s: chip_id=%02x\n", __func__, chip_id); |
Antti Palosaari | 695efd0 | 2013-02-25 08:43:32 -0300 | [diff] [blame] | 581 | |
| 582 | switch (chip_id) { |
| 583 | case 0xc3: |
| 584 | case 0x83: |
| 585 | break; |
| 586 | default: |
| 587 | goto err; |
| 588 | } |
| 589 | |
Antti Palosaari | eafa2ad | 2013-12-02 18:38:41 -0300 | [diff] [blame] | 590 | switch (priv->cfg.clock_out) { |
Antti Palosaari | 695efd0 | 2013-02-25 08:43:32 -0300 | [diff] [blame] | 591 | case M88TS2022_CLOCK_OUT_DISABLED: |
| 592 | u8tmp = 0x60; |
| 593 | break; |
| 594 | case M88TS2022_CLOCK_OUT_ENABLED: |
| 595 | u8tmp = 0x70; |
Antti Palosaari | eafa2ad | 2013-12-02 18:38:41 -0300 | [diff] [blame] | 596 | ret = m88ts2022_wr_reg(priv, 0x05, priv->cfg.clock_out_div); |
Antti Palosaari | 695efd0 | 2013-02-25 08:43:32 -0300 | [diff] [blame] | 597 | if (ret) |
| 598 | goto err; |
| 599 | break; |
| 600 | case M88TS2022_CLOCK_OUT_ENABLED_XTALOUT: |
| 601 | u8tmp = 0x6c; |
| 602 | break; |
| 603 | default: |
| 604 | goto err; |
| 605 | } |
| 606 | |
| 607 | ret = m88ts2022_wr_reg(priv, 0x42, u8tmp); |
| 608 | if (ret) |
| 609 | goto err; |
| 610 | |
Antti Palosaari | eafa2ad | 2013-12-02 18:38:41 -0300 | [diff] [blame] | 611 | if (priv->cfg.loop_through) |
Antti Palosaari | 695efd0 | 2013-02-25 08:43:32 -0300 | [diff] [blame] | 612 | u8tmp = 0xec; |
| 613 | else |
| 614 | u8tmp = 0x6c; |
| 615 | |
| 616 | ret = m88ts2022_wr_reg(priv, 0x62, u8tmp); |
| 617 | if (ret) |
| 618 | goto err; |
| 619 | |
| 620 | /* sleep */ |
| 621 | ret = m88ts2022_wr_reg(priv, 0x00, 0x00); |
| 622 | if (ret) |
| 623 | goto err; |
| 624 | |
Antti Palosaari | eafa2ad | 2013-12-02 18:38:41 -0300 | [diff] [blame] | 625 | dev_info(&priv->client->dev, |
Antti Palosaari | 695efd0 | 2013-02-25 08:43:32 -0300 | [diff] [blame] | 626 | "%s: Montage M88TS2022 successfully identified\n", |
| 627 | KBUILD_MODNAME); |
| 628 | |
| 629 | fe->tuner_priv = priv; |
| 630 | memcpy(&fe->ops.tuner_ops, &m88ts2022_tuner_ops, |
| 631 | sizeof(struct dvb_tuner_ops)); |
Antti Palosaari | 695efd0 | 2013-02-25 08:43:32 -0300 | [diff] [blame] | 632 | |
Antti Palosaari | eafa2ad | 2013-12-02 18:38:41 -0300 | [diff] [blame] | 633 | i2c_set_clientdata(client, priv); |
| 634 | return 0; |
| 635 | err: |
| 636 | dev_dbg(&client->dev, "%s: failed=%d\n", __func__, ret); |
| 637 | kfree(priv); |
| 638 | return ret; |
Antti Palosaari | 695efd0 | 2013-02-25 08:43:32 -0300 | [diff] [blame] | 639 | } |
Antti Palosaari | eafa2ad | 2013-12-02 18:38:41 -0300 | [diff] [blame] | 640 | |
| 641 | static int m88ts2022_remove(struct i2c_client *client) |
| 642 | { |
| 643 | struct m88ts2022_priv *priv = i2c_get_clientdata(client); |
| 644 | struct dvb_frontend *fe = priv->cfg.fe; |
| 645 | dev_dbg(&client->dev, "%s:\n", __func__); |
| 646 | |
| 647 | memset(&fe->ops.tuner_ops, 0, sizeof(struct dvb_tuner_ops)); |
| 648 | fe->tuner_priv = NULL; |
| 649 | kfree(priv); |
| 650 | |
| 651 | return 0; |
| 652 | } |
| 653 | |
| 654 | static const struct i2c_device_id m88ts2022_id[] = { |
| 655 | {"m88ts2022", 0}, |
| 656 | {} |
| 657 | }; |
| 658 | MODULE_DEVICE_TABLE(i2c, m88ts2022_id); |
| 659 | |
| 660 | static struct i2c_driver m88ts2022_driver = { |
| 661 | .driver = { |
| 662 | .owner = THIS_MODULE, |
| 663 | .name = "m88ts2022", |
| 664 | }, |
| 665 | .probe = m88ts2022_probe, |
| 666 | .remove = m88ts2022_remove, |
| 667 | .id_table = m88ts2022_id, |
| 668 | }; |
| 669 | |
| 670 | module_i2c_driver(m88ts2022_driver); |
Antti Palosaari | 695efd0 | 2013-02-25 08:43:32 -0300 | [diff] [blame] | 671 | |
| 672 | MODULE_DESCRIPTION("Montage M88TS2022 silicon tuner driver"); |
| 673 | MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>"); |
| 674 | MODULE_LICENSE("GPL"); |