Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 1 | /* |
| 2 | * mxl5007t.c - driver for the MaxLinear MxL5007T silicon tuner |
| 3 | * |
Michael Krufky | 7434ca4 | 2009-01-19 01:11:49 -0300 | [diff] [blame] | 4 | * Copyright (C) 2008, 2009 Michael Krufky <mkrufky@linuxtv.org> |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 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 |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 19 | */ |
| 20 | |
| 21 | #include <linux/i2c.h> |
| 22 | #include <linux/types.h> |
| 23 | #include <linux/videodev2.h> |
| 24 | #include "tuner-i2c.h" |
| 25 | #include "mxl5007t.h" |
| 26 | |
| 27 | static DEFINE_MUTEX(mxl5007t_list_mutex); |
| 28 | static LIST_HEAD(hybrid_tuner_instance_list); |
| 29 | |
| 30 | static int mxl5007t_debug; |
| 31 | module_param_named(debug, mxl5007t_debug, int, 0644); |
| 32 | MODULE_PARM_DESC(debug, "set debug level"); |
| 33 | |
| 34 | /* ------------------------------------------------------------------------- */ |
| 35 | |
| 36 | #define mxl_printk(kern, fmt, arg...) \ |
| 37 | printk(kern "%s: " fmt "\n", __func__, ##arg) |
| 38 | |
| 39 | #define mxl_err(fmt, arg...) \ |
| 40 | mxl_printk(KERN_ERR, "%d: " fmt, __LINE__, ##arg) |
| 41 | |
| 42 | #define mxl_warn(fmt, arg...) \ |
| 43 | mxl_printk(KERN_WARNING, fmt, ##arg) |
| 44 | |
| 45 | #define mxl_info(fmt, arg...) \ |
| 46 | mxl_printk(KERN_INFO, fmt, ##arg) |
| 47 | |
| 48 | #define mxl_debug(fmt, arg...) \ |
| 49 | ({ \ |
| 50 | if (mxl5007t_debug) \ |
| 51 | mxl_printk(KERN_DEBUG, fmt, ##arg); \ |
| 52 | }) |
| 53 | |
| 54 | #define mxl_fail(ret) \ |
| 55 | ({ \ |
| 56 | int __ret; \ |
| 57 | __ret = (ret < 0); \ |
| 58 | if (__ret) \ |
| 59 | mxl_printk(KERN_ERR, "error %d on line %d", \ |
| 60 | ret, __LINE__); \ |
| 61 | __ret; \ |
| 62 | }) |
| 63 | |
| 64 | /* ------------------------------------------------------------------------- */ |
| 65 | |
| 66 | #define MHz 1000000 |
| 67 | |
| 68 | enum mxl5007t_mode { |
Michael Krufky | 7434ca4 | 2009-01-19 01:11:49 -0300 | [diff] [blame] | 69 | MxL_MODE_ISDBT = 0, |
| 70 | MxL_MODE_DVBT = 1, |
| 71 | MxL_MODE_ATSC = 2, |
| 72 | MxL_MODE_CABLE = 0x10, |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | enum mxl5007t_chip_version { |
| 76 | MxL_UNKNOWN_ID = 0x00, |
| 77 | MxL_5007_V1_F1 = 0x11, |
| 78 | MxL_5007_V1_F2 = 0x12, |
Michael Krufky | 7434ca4 | 2009-01-19 01:11:49 -0300 | [diff] [blame] | 79 | MxL_5007_V4 = 0x14, |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 80 | MxL_5007_V2_100_F1 = 0x21, |
| 81 | MxL_5007_V2_100_F2 = 0x22, |
| 82 | MxL_5007_V2_200_F1 = 0x23, |
| 83 | MxL_5007_V2_200_F2 = 0x24, |
| 84 | }; |
| 85 | |
| 86 | struct reg_pair_t { |
| 87 | u8 reg; |
| 88 | u8 val; |
| 89 | }; |
| 90 | |
| 91 | /* ------------------------------------------------------------------------- */ |
| 92 | |
| 93 | static struct reg_pair_t init_tab[] = { |
Michael Krufky | 7434ca4 | 2009-01-19 01:11:49 -0300 | [diff] [blame] | 94 | { 0x02, 0x06 }, |
| 95 | { 0x03, 0x48 }, |
| 96 | { 0x05, 0x04 }, |
| 97 | { 0x06, 0x10 }, |
| 98 | { 0x2e, 0x15 }, /* OVERRIDE */ |
| 99 | { 0x30, 0x10 }, /* OVERRIDE */ |
| 100 | { 0x45, 0x58 }, /* OVERRIDE */ |
| 101 | { 0x48, 0x19 }, /* OVERRIDE */ |
| 102 | { 0x52, 0x03 }, /* OVERRIDE */ |
| 103 | { 0x53, 0x44 }, /* OVERRIDE */ |
| 104 | { 0x6a, 0x4b }, /* OVERRIDE */ |
| 105 | { 0x76, 0x00 }, /* OVERRIDE */ |
| 106 | { 0x78, 0x18 }, /* OVERRIDE */ |
| 107 | { 0x7a, 0x17 }, /* OVERRIDE */ |
| 108 | { 0x85, 0x06 }, /* OVERRIDE */ |
| 109 | { 0x01, 0x01 }, /* TOP_MASTER_ENABLE */ |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 110 | { 0, 0 } |
| 111 | }; |
| 112 | |
| 113 | static struct reg_pair_t init_tab_cable[] = { |
Michael Krufky | 7434ca4 | 2009-01-19 01:11:49 -0300 | [diff] [blame] | 114 | { 0x02, 0x06 }, |
| 115 | { 0x03, 0x48 }, |
| 116 | { 0x05, 0x04 }, |
| 117 | { 0x06, 0x10 }, |
| 118 | { 0x09, 0x3f }, |
| 119 | { 0x0a, 0x3f }, |
| 120 | { 0x0b, 0x3f }, |
| 121 | { 0x2e, 0x15 }, /* OVERRIDE */ |
| 122 | { 0x30, 0x10 }, /* OVERRIDE */ |
| 123 | { 0x45, 0x58 }, /* OVERRIDE */ |
| 124 | { 0x48, 0x19 }, /* OVERRIDE */ |
| 125 | { 0x52, 0x03 }, /* OVERRIDE */ |
| 126 | { 0x53, 0x44 }, /* OVERRIDE */ |
| 127 | { 0x6a, 0x4b }, /* OVERRIDE */ |
| 128 | { 0x76, 0x00 }, /* OVERRIDE */ |
| 129 | { 0x78, 0x18 }, /* OVERRIDE */ |
| 130 | { 0x7a, 0x17 }, /* OVERRIDE */ |
| 131 | { 0x85, 0x06 }, /* OVERRIDE */ |
| 132 | { 0x01, 0x01 }, /* TOP_MASTER_ENABLE */ |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 133 | { 0, 0 } |
| 134 | }; |
| 135 | |
| 136 | /* ------------------------------------------------------------------------- */ |
| 137 | |
| 138 | static struct reg_pair_t reg_pair_rftune[] = { |
Michael Krufky | 7434ca4 | 2009-01-19 01:11:49 -0300 | [diff] [blame] | 139 | { 0x0f, 0x00 }, /* abort tune */ |
| 140 | { 0x0c, 0x15 }, |
| 141 | { 0x0d, 0x40 }, |
| 142 | { 0x0e, 0x0e }, |
| 143 | { 0x1f, 0x87 }, /* OVERRIDE */ |
| 144 | { 0x20, 0x1f }, /* OVERRIDE */ |
| 145 | { 0x21, 0x87 }, /* OVERRIDE */ |
| 146 | { 0x22, 0x1f }, /* OVERRIDE */ |
| 147 | { 0x80, 0x01 }, /* freq dependent */ |
| 148 | { 0x0f, 0x01 }, /* start tune */ |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 149 | { 0, 0 } |
| 150 | }; |
| 151 | |
| 152 | /* ------------------------------------------------------------------------- */ |
| 153 | |
| 154 | struct mxl5007t_state { |
| 155 | struct list_head hybrid_tuner_instance_list; |
| 156 | struct tuner_i2c_props i2c_props; |
| 157 | |
| 158 | struct mutex lock; |
| 159 | |
| 160 | struct mxl5007t_config *config; |
| 161 | |
| 162 | enum mxl5007t_chip_version chip_id; |
| 163 | |
| 164 | struct reg_pair_t tab_init[ARRAY_SIZE(init_tab)]; |
| 165 | struct reg_pair_t tab_init_cable[ARRAY_SIZE(init_tab_cable)]; |
| 166 | struct reg_pair_t tab_rftune[ARRAY_SIZE(reg_pair_rftune)]; |
| 167 | |
Michael Krufky | d697b4c | 2011-11-03 09:12:04 -0300 | [diff] [blame] | 168 | enum mxl5007t_if_freq if_freq; |
| 169 | |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 170 | u32 frequency; |
| 171 | u32 bandwidth; |
| 172 | }; |
| 173 | |
| 174 | /* ------------------------------------------------------------------------- */ |
| 175 | |
| 176 | /* called by _init and _rftun to manipulate the register arrays */ |
| 177 | |
| 178 | static void set_reg_bits(struct reg_pair_t *reg_pair, u8 reg, u8 mask, u8 val) |
| 179 | { |
| 180 | unsigned int i = 0; |
| 181 | |
| 182 | while (reg_pair[i].reg || reg_pair[i].val) { |
| 183 | if (reg_pair[i].reg == reg) { |
| 184 | reg_pair[i].val &= ~mask; |
| 185 | reg_pair[i].val |= val; |
| 186 | } |
| 187 | i++; |
| 188 | |
| 189 | } |
| 190 | return; |
| 191 | } |
| 192 | |
| 193 | static void copy_reg_bits(struct reg_pair_t *reg_pair1, |
| 194 | struct reg_pair_t *reg_pair2) |
| 195 | { |
| 196 | unsigned int i, j; |
| 197 | |
| 198 | i = j = 0; |
| 199 | |
| 200 | while (reg_pair1[i].reg || reg_pair1[i].val) { |
Roel Kluin | c95a419 | 2009-11-20 15:34:13 -0300 | [diff] [blame] | 201 | while (reg_pair2[j].reg || reg_pair2[j].val) { |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 202 | if (reg_pair1[i].reg != reg_pair2[j].reg) { |
| 203 | j++; |
| 204 | continue; |
| 205 | } |
| 206 | reg_pair2[j].val = reg_pair1[i].val; |
| 207 | break; |
| 208 | } |
| 209 | i++; |
| 210 | } |
| 211 | return; |
| 212 | } |
| 213 | |
| 214 | /* ------------------------------------------------------------------------- */ |
| 215 | |
| 216 | static void mxl5007t_set_mode_bits(struct mxl5007t_state *state, |
| 217 | enum mxl5007t_mode mode, |
| 218 | s32 if_diff_out_level) |
| 219 | { |
| 220 | switch (mode) { |
Michael Krufky | 7434ca4 | 2009-01-19 01:11:49 -0300 | [diff] [blame] | 221 | case MxL_MODE_ATSC: |
| 222 | set_reg_bits(state->tab_init, 0x06, 0x1f, 0x12); |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 223 | break; |
Michael Krufky | 7434ca4 | 2009-01-19 01:11:49 -0300 | [diff] [blame] | 224 | case MxL_MODE_DVBT: |
| 225 | set_reg_bits(state->tab_init, 0x06, 0x1f, 0x11); |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 226 | break; |
Michael Krufky | 7434ca4 | 2009-01-19 01:11:49 -0300 | [diff] [blame] | 227 | case MxL_MODE_ISDBT: |
| 228 | set_reg_bits(state->tab_init, 0x06, 0x1f, 0x10); |
| 229 | break; |
| 230 | case MxL_MODE_CABLE: |
| 231 | set_reg_bits(state->tab_init_cable, 0x09, 0xff, 0xc1); |
| 232 | set_reg_bits(state->tab_init_cable, 0x0a, 0xff, |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 233 | 8 - if_diff_out_level); |
Michael Krufky | 7434ca4 | 2009-01-19 01:11:49 -0300 | [diff] [blame] | 234 | set_reg_bits(state->tab_init_cable, 0x0b, 0xff, 0x17); |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 235 | break; |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 236 | default: |
| 237 | mxl_fail(-EINVAL); |
| 238 | } |
| 239 | return; |
| 240 | } |
| 241 | |
| 242 | static void mxl5007t_set_if_freq_bits(struct mxl5007t_state *state, |
| 243 | enum mxl5007t_if_freq if_freq, |
| 244 | int invert_if) |
| 245 | { |
| 246 | u8 val; |
| 247 | |
| 248 | switch (if_freq) { |
| 249 | case MxL_IF_4_MHZ: |
| 250 | val = 0x00; |
| 251 | break; |
| 252 | case MxL_IF_4_5_MHZ: |
Michael Krufky | 7434ca4 | 2009-01-19 01:11:49 -0300 | [diff] [blame] | 253 | val = 0x02; |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 254 | break; |
| 255 | case MxL_IF_4_57_MHZ: |
Michael Krufky | 7434ca4 | 2009-01-19 01:11:49 -0300 | [diff] [blame] | 256 | val = 0x03; |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 257 | break; |
| 258 | case MxL_IF_5_MHZ: |
Michael Krufky | 7434ca4 | 2009-01-19 01:11:49 -0300 | [diff] [blame] | 259 | val = 0x04; |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 260 | break; |
| 261 | case MxL_IF_5_38_MHZ: |
Michael Krufky | 7434ca4 | 2009-01-19 01:11:49 -0300 | [diff] [blame] | 262 | val = 0x05; |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 263 | break; |
| 264 | case MxL_IF_6_MHZ: |
Michael Krufky | 7434ca4 | 2009-01-19 01:11:49 -0300 | [diff] [blame] | 265 | val = 0x06; |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 266 | break; |
| 267 | case MxL_IF_6_28_MHZ: |
Michael Krufky | 7434ca4 | 2009-01-19 01:11:49 -0300 | [diff] [blame] | 268 | val = 0x07; |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 269 | break; |
| 270 | case MxL_IF_9_1915_MHZ: |
Michael Krufky | 7434ca4 | 2009-01-19 01:11:49 -0300 | [diff] [blame] | 271 | val = 0x08; |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 272 | break; |
| 273 | case MxL_IF_35_25_MHZ: |
Michael Krufky | 7434ca4 | 2009-01-19 01:11:49 -0300 | [diff] [blame] | 274 | val = 0x09; |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 275 | break; |
| 276 | case MxL_IF_36_15_MHZ: |
Michael Krufky | 7434ca4 | 2009-01-19 01:11:49 -0300 | [diff] [blame] | 277 | val = 0x0a; |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 278 | break; |
| 279 | case MxL_IF_44_MHZ: |
Michael Krufky | 7434ca4 | 2009-01-19 01:11:49 -0300 | [diff] [blame] | 280 | val = 0x0b; |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 281 | break; |
| 282 | default: |
| 283 | mxl_fail(-EINVAL); |
| 284 | return; |
| 285 | } |
Michael Krufky | 7434ca4 | 2009-01-19 01:11:49 -0300 | [diff] [blame] | 286 | set_reg_bits(state->tab_init, 0x02, 0x0f, val); |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 287 | |
| 288 | /* set inverted IF or normal IF */ |
Michael Krufky | 7434ca4 | 2009-01-19 01:11:49 -0300 | [diff] [blame] | 289 | set_reg_bits(state->tab_init, 0x02, 0x10, invert_if ? 0x10 : 0x00); |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 290 | |
Michael Krufky | d697b4c | 2011-11-03 09:12:04 -0300 | [diff] [blame] | 291 | state->if_freq = if_freq; |
| 292 | |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 293 | return; |
| 294 | } |
| 295 | |
| 296 | static void mxl5007t_set_xtal_freq_bits(struct mxl5007t_state *state, |
| 297 | enum mxl5007t_xtal_freq xtal_freq) |
| 298 | { |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 299 | switch (xtal_freq) { |
| 300 | case MxL_XTAL_16_MHZ: |
Michael Krufky | 7434ca4 | 2009-01-19 01:11:49 -0300 | [diff] [blame] | 301 | /* select xtal freq & ref freq */ |
| 302 | set_reg_bits(state->tab_init, 0x03, 0xf0, 0x00); |
| 303 | set_reg_bits(state->tab_init, 0x05, 0x0f, 0x00); |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 304 | break; |
| 305 | case MxL_XTAL_20_MHZ: |
Michael Krufky | 7434ca4 | 2009-01-19 01:11:49 -0300 | [diff] [blame] | 306 | set_reg_bits(state->tab_init, 0x03, 0xf0, 0x10); |
| 307 | set_reg_bits(state->tab_init, 0x05, 0x0f, 0x01); |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 308 | break; |
| 309 | case MxL_XTAL_20_25_MHZ: |
Michael Krufky | 7434ca4 | 2009-01-19 01:11:49 -0300 | [diff] [blame] | 310 | set_reg_bits(state->tab_init, 0x03, 0xf0, 0x20); |
| 311 | set_reg_bits(state->tab_init, 0x05, 0x0f, 0x02); |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 312 | break; |
| 313 | case MxL_XTAL_20_48_MHZ: |
Michael Krufky | 7434ca4 | 2009-01-19 01:11:49 -0300 | [diff] [blame] | 314 | set_reg_bits(state->tab_init, 0x03, 0xf0, 0x30); |
| 315 | set_reg_bits(state->tab_init, 0x05, 0x0f, 0x03); |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 316 | break; |
| 317 | case MxL_XTAL_24_MHZ: |
Michael Krufky | 7434ca4 | 2009-01-19 01:11:49 -0300 | [diff] [blame] | 318 | set_reg_bits(state->tab_init, 0x03, 0xf0, 0x40); |
| 319 | set_reg_bits(state->tab_init, 0x05, 0x0f, 0x04); |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 320 | break; |
| 321 | case MxL_XTAL_25_MHZ: |
Michael Krufky | 7434ca4 | 2009-01-19 01:11:49 -0300 | [diff] [blame] | 322 | set_reg_bits(state->tab_init, 0x03, 0xf0, 0x50); |
| 323 | set_reg_bits(state->tab_init, 0x05, 0x0f, 0x05); |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 324 | break; |
| 325 | case MxL_XTAL_25_14_MHZ: |
Michael Krufky | 7434ca4 | 2009-01-19 01:11:49 -0300 | [diff] [blame] | 326 | set_reg_bits(state->tab_init, 0x03, 0xf0, 0x60); |
| 327 | set_reg_bits(state->tab_init, 0x05, 0x0f, 0x06); |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 328 | break; |
| 329 | case MxL_XTAL_27_MHZ: |
Michael Krufky | 7434ca4 | 2009-01-19 01:11:49 -0300 | [diff] [blame] | 330 | set_reg_bits(state->tab_init, 0x03, 0xf0, 0x70); |
| 331 | set_reg_bits(state->tab_init, 0x05, 0x0f, 0x07); |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 332 | break; |
| 333 | case MxL_XTAL_28_8_MHZ: |
Michael Krufky | 7434ca4 | 2009-01-19 01:11:49 -0300 | [diff] [blame] | 334 | set_reg_bits(state->tab_init, 0x03, 0xf0, 0x80); |
| 335 | set_reg_bits(state->tab_init, 0x05, 0x0f, 0x08); |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 336 | break; |
| 337 | case MxL_XTAL_32_MHZ: |
Michael Krufky | 7434ca4 | 2009-01-19 01:11:49 -0300 | [diff] [blame] | 338 | set_reg_bits(state->tab_init, 0x03, 0xf0, 0x90); |
| 339 | set_reg_bits(state->tab_init, 0x05, 0x0f, 0x09); |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 340 | break; |
| 341 | case MxL_XTAL_40_MHZ: |
Michael Krufky | 7434ca4 | 2009-01-19 01:11:49 -0300 | [diff] [blame] | 342 | set_reg_bits(state->tab_init, 0x03, 0xf0, 0xa0); |
| 343 | set_reg_bits(state->tab_init, 0x05, 0x0f, 0x0a); |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 344 | break; |
| 345 | case MxL_XTAL_44_MHZ: |
Michael Krufky | 7434ca4 | 2009-01-19 01:11:49 -0300 | [diff] [blame] | 346 | set_reg_bits(state->tab_init, 0x03, 0xf0, 0xb0); |
| 347 | set_reg_bits(state->tab_init, 0x05, 0x0f, 0x0b); |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 348 | break; |
| 349 | case MxL_XTAL_48_MHZ: |
Michael Krufky | 7434ca4 | 2009-01-19 01:11:49 -0300 | [diff] [blame] | 350 | set_reg_bits(state->tab_init, 0x03, 0xf0, 0xc0); |
| 351 | set_reg_bits(state->tab_init, 0x05, 0x0f, 0x0c); |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 352 | break; |
| 353 | case MxL_XTAL_49_3811_MHZ: |
Michael Krufky | 7434ca4 | 2009-01-19 01:11:49 -0300 | [diff] [blame] | 354 | set_reg_bits(state->tab_init, 0x03, 0xf0, 0xd0); |
| 355 | set_reg_bits(state->tab_init, 0x05, 0x0f, 0x0d); |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 356 | break; |
| 357 | default: |
| 358 | mxl_fail(-EINVAL); |
| 359 | return; |
| 360 | } |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 361 | |
| 362 | return; |
| 363 | } |
| 364 | |
| 365 | static struct reg_pair_t *mxl5007t_calc_init_regs(struct mxl5007t_state *state, |
| 366 | enum mxl5007t_mode mode) |
| 367 | { |
| 368 | struct mxl5007t_config *cfg = state->config; |
| 369 | |
| 370 | memcpy(&state->tab_init, &init_tab, sizeof(init_tab)); |
| 371 | memcpy(&state->tab_init_cable, &init_tab_cable, sizeof(init_tab_cable)); |
| 372 | |
| 373 | mxl5007t_set_mode_bits(state, mode, cfg->if_diff_out_level); |
| 374 | mxl5007t_set_if_freq_bits(state, cfg->if_freq_hz, cfg->invert_if); |
| 375 | mxl5007t_set_xtal_freq_bits(state, cfg->xtal_freq_hz); |
| 376 | |
Michael Krufky | 7434ca4 | 2009-01-19 01:11:49 -0300 | [diff] [blame] | 377 | set_reg_bits(state->tab_init, 0x04, 0x01, cfg->loop_thru_enable); |
| 378 | set_reg_bits(state->tab_init, 0x03, 0x08, cfg->clk_out_enable << 3); |
| 379 | set_reg_bits(state->tab_init, 0x03, 0x07, cfg->clk_out_amp); |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 380 | |
Michael Krufky | 7434ca4 | 2009-01-19 01:11:49 -0300 | [diff] [blame] | 381 | if (mode >= MxL_MODE_CABLE) { |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 382 | copy_reg_bits(state->tab_init, state->tab_init_cable); |
| 383 | return state->tab_init_cable; |
| 384 | } else |
| 385 | return state->tab_init; |
| 386 | } |
| 387 | |
| 388 | /* ------------------------------------------------------------------------- */ |
| 389 | |
| 390 | enum mxl5007t_bw_mhz { |
| 391 | MxL_BW_6MHz = 6, |
| 392 | MxL_BW_7MHz = 7, |
| 393 | MxL_BW_8MHz = 8, |
| 394 | }; |
| 395 | |
| 396 | static void mxl5007t_set_bw_bits(struct mxl5007t_state *state, |
| 397 | enum mxl5007t_bw_mhz bw) |
| 398 | { |
| 399 | u8 val; |
| 400 | |
| 401 | switch (bw) { |
| 402 | case MxL_BW_6MHz: |
| 403 | val = 0x15; /* set DIG_MODEINDEX, DIG_MODEINDEX_A, |
| 404 | * and DIG_MODEINDEX_CSF */ |
| 405 | break; |
| 406 | case MxL_BW_7MHz: |
Michael Krufky | 7434ca4 | 2009-01-19 01:11:49 -0300 | [diff] [blame] | 407 | val = 0x2a; |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 408 | break; |
| 409 | case MxL_BW_8MHz: |
| 410 | val = 0x3f; |
| 411 | break; |
| 412 | default: |
| 413 | mxl_fail(-EINVAL); |
| 414 | return; |
| 415 | } |
Michael Krufky | 7434ca4 | 2009-01-19 01:11:49 -0300 | [diff] [blame] | 416 | set_reg_bits(state->tab_rftune, 0x0c, 0x3f, val); |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 417 | |
| 418 | return; |
| 419 | } |
| 420 | |
| 421 | static struct |
| 422 | reg_pair_t *mxl5007t_calc_rf_tune_regs(struct mxl5007t_state *state, |
| 423 | u32 rf_freq, enum mxl5007t_bw_mhz bw) |
| 424 | { |
| 425 | u32 dig_rf_freq = 0; |
| 426 | u32 temp; |
| 427 | u32 frac_divider = 1000000; |
| 428 | unsigned int i; |
| 429 | |
| 430 | memcpy(&state->tab_rftune, ®_pair_rftune, sizeof(reg_pair_rftune)); |
| 431 | |
| 432 | mxl5007t_set_bw_bits(state, bw); |
| 433 | |
| 434 | /* Convert RF frequency into 16 bits => |
| 435 | * 10 bit integer (MHz) + 6 bit fraction */ |
| 436 | dig_rf_freq = rf_freq / MHz; |
| 437 | |
| 438 | temp = rf_freq % MHz; |
| 439 | |
| 440 | for (i = 0; i < 6; i++) { |
| 441 | dig_rf_freq <<= 1; |
| 442 | frac_divider /= 2; |
| 443 | if (temp > frac_divider) { |
| 444 | temp -= frac_divider; |
| 445 | dig_rf_freq++; |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | /* add to have shift center point by 7.8124 kHz */ |
| 450 | if (temp > 7812) |
| 451 | dig_rf_freq++; |
| 452 | |
Michael Krufky | 7434ca4 | 2009-01-19 01:11:49 -0300 | [diff] [blame] | 453 | set_reg_bits(state->tab_rftune, 0x0d, 0xff, (u8) dig_rf_freq); |
| 454 | set_reg_bits(state->tab_rftune, 0x0e, 0xff, (u8) (dig_rf_freq >> 8)); |
| 455 | |
| 456 | if (rf_freq >= 333000000) |
| 457 | set_reg_bits(state->tab_rftune, 0x80, 0x40, 0x40); |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 458 | |
| 459 | return state->tab_rftune; |
| 460 | } |
| 461 | |
| 462 | /* ------------------------------------------------------------------------- */ |
| 463 | |
| 464 | static int mxl5007t_write_reg(struct mxl5007t_state *state, u8 reg, u8 val) |
| 465 | { |
| 466 | u8 buf[] = { reg, val }; |
| 467 | struct i2c_msg msg = { .addr = state->i2c_props.addr, .flags = 0, |
| 468 | .buf = buf, .len = 2 }; |
| 469 | int ret; |
| 470 | |
| 471 | ret = i2c_transfer(state->i2c_props.adap, &msg, 1); |
| 472 | if (ret != 1) { |
| 473 | mxl_err("failed!"); |
| 474 | return -EREMOTEIO; |
| 475 | } |
| 476 | return 0; |
| 477 | } |
| 478 | |
| 479 | static int mxl5007t_write_regs(struct mxl5007t_state *state, |
| 480 | struct reg_pair_t *reg_pair) |
| 481 | { |
| 482 | unsigned int i = 0; |
| 483 | int ret = 0; |
| 484 | |
| 485 | while ((ret == 0) && (reg_pair[i].reg || reg_pair[i].val)) { |
| 486 | ret = mxl5007t_write_reg(state, |
| 487 | reg_pair[i].reg, reg_pair[i].val); |
| 488 | i++; |
| 489 | } |
| 490 | return ret; |
| 491 | } |
| 492 | |
| 493 | static int mxl5007t_read_reg(struct mxl5007t_state *state, u8 reg, u8 *val) |
| 494 | { |
Antti Palosaari | 576b849 | 2011-10-09 08:59:16 -0300 | [diff] [blame] | 495 | u8 buf[2] = { 0xfb, reg }; |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 496 | struct i2c_msg msg[] = { |
| 497 | { .addr = state->i2c_props.addr, .flags = 0, |
Antti Palosaari | 576b849 | 2011-10-09 08:59:16 -0300 | [diff] [blame] | 498 | .buf = buf, .len = 2 }, |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 499 | { .addr = state->i2c_props.addr, .flags = I2C_M_RD, |
| 500 | .buf = val, .len = 1 }, |
| 501 | }; |
| 502 | int ret; |
| 503 | |
| 504 | ret = i2c_transfer(state->i2c_props.adap, msg, 2); |
| 505 | if (ret != 2) { |
| 506 | mxl_err("failed!"); |
| 507 | return -EREMOTEIO; |
| 508 | } |
| 509 | return 0; |
| 510 | } |
| 511 | |
| 512 | static int mxl5007t_soft_reset(struct mxl5007t_state *state) |
| 513 | { |
| 514 | u8 d = 0xff; |
Michael Krufky | 7434ca4 | 2009-01-19 01:11:49 -0300 | [diff] [blame] | 515 | struct i2c_msg msg = { |
| 516 | .addr = state->i2c_props.addr, .flags = 0, |
| 517 | .buf = &d, .len = 1 |
| 518 | }; |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 519 | int ret = i2c_transfer(state->i2c_props.adap, &msg, 1); |
| 520 | |
| 521 | if (ret != 1) { |
| 522 | mxl_err("failed!"); |
| 523 | return -EREMOTEIO; |
| 524 | } |
| 525 | return 0; |
| 526 | } |
| 527 | |
| 528 | static int mxl5007t_tuner_init(struct mxl5007t_state *state, |
| 529 | enum mxl5007t_mode mode) |
| 530 | { |
| 531 | struct reg_pair_t *init_regs; |
| 532 | int ret; |
| 533 | |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 534 | /* calculate initialization reg array */ |
| 535 | init_regs = mxl5007t_calc_init_regs(state, mode); |
| 536 | |
| 537 | ret = mxl5007t_write_regs(state, init_regs); |
| 538 | if (mxl_fail(ret)) |
| 539 | goto fail; |
| 540 | mdelay(1); |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 541 | fail: |
| 542 | return ret; |
| 543 | } |
| 544 | |
| 545 | static int mxl5007t_tuner_rf_tune(struct mxl5007t_state *state, u32 rf_freq_hz, |
| 546 | enum mxl5007t_bw_mhz bw) |
| 547 | { |
| 548 | struct reg_pair_t *rf_tune_regs; |
| 549 | int ret; |
| 550 | |
| 551 | /* calculate channel change reg array */ |
| 552 | rf_tune_regs = mxl5007t_calc_rf_tune_regs(state, rf_freq_hz, bw); |
| 553 | |
| 554 | ret = mxl5007t_write_regs(state, rf_tune_regs); |
| 555 | if (mxl_fail(ret)) |
| 556 | goto fail; |
| 557 | msleep(3); |
| 558 | fail: |
| 559 | return ret; |
| 560 | } |
| 561 | |
| 562 | /* ------------------------------------------------------------------------- */ |
| 563 | |
| 564 | static int mxl5007t_synth_lock_status(struct mxl5007t_state *state, |
| 565 | int *rf_locked, int *ref_locked) |
| 566 | { |
| 567 | u8 d; |
| 568 | int ret; |
| 569 | |
| 570 | *rf_locked = 0; |
| 571 | *ref_locked = 0; |
| 572 | |
Michael Krufky | 7434ca4 | 2009-01-19 01:11:49 -0300 | [diff] [blame] | 573 | ret = mxl5007t_read_reg(state, 0xd8, &d); |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 574 | if (mxl_fail(ret)) |
| 575 | goto fail; |
| 576 | |
| 577 | if ((d & 0x0c) == 0x0c) |
| 578 | *rf_locked = 1; |
| 579 | |
| 580 | if ((d & 0x03) == 0x03) |
| 581 | *ref_locked = 1; |
| 582 | fail: |
| 583 | return ret; |
| 584 | } |
| 585 | |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 586 | /* ------------------------------------------------------------------------- */ |
| 587 | |
| 588 | static int mxl5007t_get_status(struct dvb_frontend *fe, u32 *status) |
| 589 | { |
| 590 | struct mxl5007t_state *state = fe->tuner_priv; |
Michael Krufky | d90958e | 2009-02-28 19:42:59 -0300 | [diff] [blame] | 591 | int rf_locked, ref_locked, ret; |
| 592 | |
| 593 | *status = 0; |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 594 | |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 595 | if (fe->ops.i2c_gate_ctrl) |
| 596 | fe->ops.i2c_gate_ctrl(fe, 1); |
| 597 | |
| 598 | ret = mxl5007t_synth_lock_status(state, &rf_locked, &ref_locked); |
| 599 | if (mxl_fail(ret)) |
| 600 | goto fail; |
| 601 | mxl_debug("%s%s", rf_locked ? "rf locked " : "", |
| 602 | ref_locked ? "ref locked" : ""); |
Michael Krufky | d90958e | 2009-02-28 19:42:59 -0300 | [diff] [blame] | 603 | |
| 604 | if ((rf_locked) || (ref_locked)) |
| 605 | *status |= TUNER_STATUS_LOCKED; |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 606 | fail: |
| 607 | if (fe->ops.i2c_gate_ctrl) |
| 608 | fe->ops.i2c_gate_ctrl(fe, 0); |
| 609 | |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 610 | return ret; |
| 611 | } |
| 612 | |
| 613 | /* ------------------------------------------------------------------------- */ |
| 614 | |
Mauro Carvalho Chehab | 14d24d1 | 2011-12-24 12:24:33 -0300 | [diff] [blame] | 615 | static int mxl5007t_set_params(struct dvb_frontend *fe) |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 616 | { |
Mauro Carvalho Chehab | e12617e | 2011-12-21 07:31:23 -0300 | [diff] [blame] | 617 | struct dtv_frontend_properties *c = &fe->dtv_property_cache; |
| 618 | u32 delsys = c->delivery_system; |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 619 | struct mxl5007t_state *state = fe->tuner_priv; |
| 620 | enum mxl5007t_bw_mhz bw; |
| 621 | enum mxl5007t_mode mode; |
| 622 | int ret; |
Mauro Carvalho Chehab | e12617e | 2011-12-21 07:31:23 -0300 | [diff] [blame] | 623 | u32 freq = c->frequency; |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 624 | |
Mauro Carvalho Chehab | e12617e | 2011-12-21 07:31:23 -0300 | [diff] [blame] | 625 | switch (delsys) { |
| 626 | case SYS_ATSC: |
| 627 | mode = MxL_MODE_ATSC; |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 628 | bw = MxL_BW_6MHz; |
Mauro Carvalho Chehab | e12617e | 2011-12-21 07:31:23 -0300 | [diff] [blame] | 629 | break; |
| 630 | case SYS_DVBC_ANNEX_B: |
| 631 | mode = MxL_MODE_CABLE; |
| 632 | bw = MxL_BW_6MHz; |
| 633 | break; |
| 634 | case SYS_DVBT: |
| 635 | case SYS_DVBT2: |
| 636 | mode = MxL_MODE_DVBT; |
| 637 | switch (c->bandwidth_hz) { |
| 638 | case 6000000: |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 639 | bw = MxL_BW_6MHz; |
| 640 | break; |
Mauro Carvalho Chehab | e12617e | 2011-12-21 07:31:23 -0300 | [diff] [blame] | 641 | case 7000000: |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 642 | bw = MxL_BW_7MHz; |
Antti Palosaari | 9e94136 | 2012-01-10 13:31:37 -0300 | [diff] [blame] | 643 | break; |
Mauro Carvalho Chehab | e12617e | 2011-12-21 07:31:23 -0300 | [diff] [blame] | 644 | case 8000000: |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 645 | bw = MxL_BW_8MHz; |
Antti Palosaari | 9e94136 | 2012-01-10 13:31:37 -0300 | [diff] [blame] | 646 | break; |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 647 | default: |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 648 | return -EINVAL; |
| 649 | } |
Mauro Carvalho Chehab | e12617e | 2011-12-21 07:31:23 -0300 | [diff] [blame] | 650 | break; |
| 651 | default: |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 652 | mxl_err("modulation type not supported!"); |
| 653 | return -EINVAL; |
| 654 | } |
| 655 | |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 656 | if (fe->ops.i2c_gate_ctrl) |
| 657 | fe->ops.i2c_gate_ctrl(fe, 1); |
| 658 | |
Michael Krufky | c39c1fd | 2008-07-26 12:06:57 -0300 | [diff] [blame] | 659 | mutex_lock(&state->lock); |
| 660 | |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 661 | ret = mxl5007t_tuner_init(state, mode); |
| 662 | if (mxl_fail(ret)) |
| 663 | goto fail; |
| 664 | |
| 665 | ret = mxl5007t_tuner_rf_tune(state, freq, bw); |
| 666 | if (mxl_fail(ret)) |
| 667 | goto fail; |
| 668 | |
| 669 | state->frequency = freq; |
Mauro Carvalho Chehab | c6f56e7 | 2011-12-26 20:02:28 -0300 | [diff] [blame] | 670 | state->bandwidth = c->bandwidth_hz; |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 671 | fail: |
Michael Krufky | c39c1fd | 2008-07-26 12:06:57 -0300 | [diff] [blame] | 672 | mutex_unlock(&state->lock); |
| 673 | |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 674 | if (fe->ops.i2c_gate_ctrl) |
| 675 | fe->ops.i2c_gate_ctrl(fe, 0); |
| 676 | |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 677 | return ret; |
| 678 | } |
| 679 | |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 680 | /* ------------------------------------------------------------------------- */ |
| 681 | |
| 682 | static int mxl5007t_init(struct dvb_frontend *fe) |
| 683 | { |
| 684 | struct mxl5007t_state *state = fe->tuner_priv; |
Michael Krufky | 452a53a | 2008-07-12 18:22:38 -0300 | [diff] [blame] | 685 | int ret; |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 686 | |
Michael Krufky | 452a53a | 2008-07-12 18:22:38 -0300 | [diff] [blame] | 687 | if (fe->ops.i2c_gate_ctrl) |
| 688 | fe->ops.i2c_gate_ctrl(fe, 1); |
| 689 | |
Michael Krufky | 7434ca4 | 2009-01-19 01:11:49 -0300 | [diff] [blame] | 690 | /* wake from standby */ |
| 691 | ret = mxl5007t_write_reg(state, 0x01, 0x01); |
Michael Krufky | 452a53a | 2008-07-12 18:22:38 -0300 | [diff] [blame] | 692 | mxl_fail(ret); |
Michael Krufky | 7434ca4 | 2009-01-19 01:11:49 -0300 | [diff] [blame] | 693 | |
Michael Krufky | 452a53a | 2008-07-12 18:22:38 -0300 | [diff] [blame] | 694 | if (fe->ops.i2c_gate_ctrl) |
| 695 | fe->ops.i2c_gate_ctrl(fe, 0); |
| 696 | |
Michael Krufky | 452a53a | 2008-07-12 18:22:38 -0300 | [diff] [blame] | 697 | return ret; |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | static int mxl5007t_sleep(struct dvb_frontend *fe) |
| 701 | { |
| 702 | struct mxl5007t_state *state = fe->tuner_priv; |
Michael Krufky | 452a53a | 2008-07-12 18:22:38 -0300 | [diff] [blame] | 703 | int ret; |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 704 | |
Michael Krufky | 452a53a | 2008-07-12 18:22:38 -0300 | [diff] [blame] | 705 | if (fe->ops.i2c_gate_ctrl) |
| 706 | fe->ops.i2c_gate_ctrl(fe, 1); |
| 707 | |
Michael Krufky | 7434ca4 | 2009-01-19 01:11:49 -0300 | [diff] [blame] | 708 | /* enter standby mode */ |
| 709 | ret = mxl5007t_write_reg(state, 0x01, 0x00); |
Michael Krufky | 452a53a | 2008-07-12 18:22:38 -0300 | [diff] [blame] | 710 | mxl_fail(ret); |
Michael Krufky | 7434ca4 | 2009-01-19 01:11:49 -0300 | [diff] [blame] | 711 | ret = mxl5007t_write_reg(state, 0x0f, 0x00); |
| 712 | mxl_fail(ret); |
| 713 | |
Michael Krufky | 452a53a | 2008-07-12 18:22:38 -0300 | [diff] [blame] | 714 | if (fe->ops.i2c_gate_ctrl) |
| 715 | fe->ops.i2c_gate_ctrl(fe, 0); |
| 716 | |
Michael Krufky | 452a53a | 2008-07-12 18:22:38 -0300 | [diff] [blame] | 717 | return ret; |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | /* ------------------------------------------------------------------------- */ |
| 721 | |
| 722 | static int mxl5007t_get_frequency(struct dvb_frontend *fe, u32 *frequency) |
| 723 | { |
| 724 | struct mxl5007t_state *state = fe->tuner_priv; |
| 725 | *frequency = state->frequency; |
| 726 | return 0; |
| 727 | } |
| 728 | |
| 729 | static int mxl5007t_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth) |
| 730 | { |
| 731 | struct mxl5007t_state *state = fe->tuner_priv; |
| 732 | *bandwidth = state->bandwidth; |
| 733 | return 0; |
| 734 | } |
| 735 | |
Michael Krufky | d697b4c | 2011-11-03 09:12:04 -0300 | [diff] [blame] | 736 | static int mxl5007t_get_if_frequency(struct dvb_frontend *fe, u32 *frequency) |
| 737 | { |
| 738 | struct mxl5007t_state *state = fe->tuner_priv; |
| 739 | |
| 740 | *frequency = 0; |
| 741 | |
| 742 | switch (state->if_freq) { |
| 743 | case MxL_IF_4_MHZ: |
| 744 | *frequency = 4000000; |
| 745 | break; |
| 746 | case MxL_IF_4_5_MHZ: |
| 747 | *frequency = 4500000; |
| 748 | break; |
| 749 | case MxL_IF_4_57_MHZ: |
| 750 | *frequency = 4570000; |
| 751 | break; |
| 752 | case MxL_IF_5_MHZ: |
| 753 | *frequency = 5000000; |
| 754 | break; |
| 755 | case MxL_IF_5_38_MHZ: |
| 756 | *frequency = 5380000; |
| 757 | break; |
| 758 | case MxL_IF_6_MHZ: |
| 759 | *frequency = 6000000; |
| 760 | break; |
| 761 | case MxL_IF_6_28_MHZ: |
| 762 | *frequency = 6280000; |
| 763 | break; |
| 764 | case MxL_IF_9_1915_MHZ: |
| 765 | *frequency = 9191500; |
| 766 | break; |
| 767 | case MxL_IF_35_25_MHZ: |
| 768 | *frequency = 35250000; |
| 769 | break; |
| 770 | case MxL_IF_36_15_MHZ: |
| 771 | *frequency = 36150000; |
| 772 | break; |
| 773 | case MxL_IF_44_MHZ: |
| 774 | *frequency = 44000000; |
| 775 | break; |
| 776 | } |
| 777 | return 0; |
| 778 | } |
| 779 | |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 780 | static int mxl5007t_release(struct dvb_frontend *fe) |
| 781 | { |
| 782 | struct mxl5007t_state *state = fe->tuner_priv; |
| 783 | |
| 784 | mutex_lock(&mxl5007t_list_mutex); |
| 785 | |
| 786 | if (state) |
| 787 | hybrid_tuner_release_state(state); |
| 788 | |
| 789 | mutex_unlock(&mxl5007t_list_mutex); |
| 790 | |
| 791 | fe->tuner_priv = NULL; |
| 792 | |
| 793 | return 0; |
| 794 | } |
| 795 | |
| 796 | /* ------------------------------------------------------------------------- */ |
| 797 | |
| 798 | static struct dvb_tuner_ops mxl5007t_tuner_ops = { |
| 799 | .info = { |
| 800 | .name = "MaxLinear MxL5007T", |
| 801 | }, |
| 802 | .init = mxl5007t_init, |
| 803 | .sleep = mxl5007t_sleep, |
| 804 | .set_params = mxl5007t_set_params, |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 805 | .get_status = mxl5007t_get_status, |
| 806 | .get_frequency = mxl5007t_get_frequency, |
| 807 | .get_bandwidth = mxl5007t_get_bandwidth, |
| 808 | .release = mxl5007t_release, |
Michael Krufky | d697b4c | 2011-11-03 09:12:04 -0300 | [diff] [blame] | 809 | .get_if_frequency = mxl5007t_get_if_frequency, |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 810 | }; |
| 811 | |
| 812 | static int mxl5007t_get_chip_id(struct mxl5007t_state *state) |
| 813 | { |
| 814 | char *name; |
| 815 | int ret; |
| 816 | u8 id; |
| 817 | |
Michael Krufky | 7434ca4 | 2009-01-19 01:11:49 -0300 | [diff] [blame] | 818 | ret = mxl5007t_read_reg(state, 0xd9, &id); |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 819 | if (mxl_fail(ret)) |
| 820 | goto fail; |
| 821 | |
| 822 | switch (id) { |
| 823 | case MxL_5007_V1_F1: |
| 824 | name = "MxL5007.v1.f1"; |
| 825 | break; |
| 826 | case MxL_5007_V1_F2: |
| 827 | name = "MxL5007.v1.f2"; |
| 828 | break; |
| 829 | case MxL_5007_V2_100_F1: |
| 830 | name = "MxL5007.v2.100.f1"; |
| 831 | break; |
| 832 | case MxL_5007_V2_100_F2: |
| 833 | name = "MxL5007.v2.100.f2"; |
| 834 | break; |
| 835 | case MxL_5007_V2_200_F1: |
| 836 | name = "MxL5007.v2.200.f1"; |
| 837 | break; |
| 838 | case MxL_5007_V2_200_F2: |
| 839 | name = "MxL5007.v2.200.f2"; |
| 840 | break; |
Michael Krufky | 7434ca4 | 2009-01-19 01:11:49 -0300 | [diff] [blame] | 841 | case MxL_5007_V4: |
| 842 | name = "MxL5007T.v4"; |
| 843 | break; |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 844 | default: |
| 845 | name = "MxL5007T"; |
Michael Krufky | d202515b | 2009-02-28 19:56:30 -0300 | [diff] [blame] | 846 | printk(KERN_WARNING "%s: unknown rev (%02x)\n", __func__, id); |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 847 | id = MxL_UNKNOWN_ID; |
| 848 | } |
| 849 | state->chip_id = id; |
| 850 | mxl_info("%s detected @ %d-%04x", name, |
| 851 | i2c_adapter_id(state->i2c_props.adap), |
| 852 | state->i2c_props.addr); |
| 853 | return 0; |
| 854 | fail: |
| 855 | mxl_warn("unable to identify device @ %d-%04x", |
| 856 | i2c_adapter_id(state->i2c_props.adap), |
| 857 | state->i2c_props.addr); |
| 858 | |
| 859 | state->chip_id = MxL_UNKNOWN_ID; |
| 860 | return ret; |
| 861 | } |
| 862 | |
| 863 | struct dvb_frontend *mxl5007t_attach(struct dvb_frontend *fe, |
| 864 | struct i2c_adapter *i2c, u8 addr, |
| 865 | struct mxl5007t_config *cfg) |
| 866 | { |
| 867 | struct mxl5007t_state *state = NULL; |
| 868 | int instance, ret; |
| 869 | |
| 870 | mutex_lock(&mxl5007t_list_mutex); |
| 871 | instance = hybrid_tuner_request_state(struct mxl5007t_state, state, |
| 872 | hybrid_tuner_instance_list, |
Michael Krufky | 3d0081d | 2009-02-28 22:55:55 -0300 | [diff] [blame] | 873 | i2c, addr, "mxl5007t"); |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 874 | switch (instance) { |
| 875 | case 0: |
| 876 | goto fail; |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 877 | case 1: |
| 878 | /* new tuner instance */ |
| 879 | state->config = cfg; |
| 880 | |
| 881 | mutex_init(&state->lock); |
| 882 | |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 883 | if (fe->ops.i2c_gate_ctrl) |
| 884 | fe->ops.i2c_gate_ctrl(fe, 1); |
| 885 | |
| 886 | ret = mxl5007t_get_chip_id(state); |
| 887 | |
| 888 | if (fe->ops.i2c_gate_ctrl) |
| 889 | fe->ops.i2c_gate_ctrl(fe, 0); |
| 890 | |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 891 | /* check return value of mxl5007t_get_chip_id */ |
| 892 | if (mxl_fail(ret)) |
| 893 | goto fail; |
| 894 | break; |
| 895 | default: |
| 896 | /* existing tuner instance */ |
| 897 | break; |
| 898 | } |
Jose Alberto Reguero | 0a32377 | 2013-02-03 18:30:38 -0300 | [diff] [blame^] | 899 | |
| 900 | if (fe->ops.i2c_gate_ctrl) |
| 901 | fe->ops.i2c_gate_ctrl(fe, 1); |
| 902 | |
| 903 | ret = mxl5007t_soft_reset(state); |
| 904 | |
| 905 | if (fe->ops.i2c_gate_ctrl) |
| 906 | fe->ops.i2c_gate_ctrl(fe, 0); |
| 907 | |
| 908 | if (mxl_fail(ret)) |
| 909 | goto fail; |
| 910 | |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 911 | fe->tuner_priv = state; |
Jose Alberto Reguero | 0a32377 | 2013-02-03 18:30:38 -0300 | [diff] [blame^] | 912 | |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 913 | mutex_unlock(&mxl5007t_list_mutex); |
| 914 | |
| 915 | memcpy(&fe->ops.tuner_ops, &mxl5007t_tuner_ops, |
| 916 | sizeof(struct dvb_tuner_ops)); |
| 917 | |
| 918 | return fe; |
| 919 | fail: |
| 920 | mutex_unlock(&mxl5007t_list_mutex); |
| 921 | |
| 922 | mxl5007t_release(fe); |
| 923 | return NULL; |
| 924 | } |
| 925 | EXPORT_SYMBOL_GPL(mxl5007t_attach); |
| 926 | MODULE_DESCRIPTION("MaxLinear MxL5007T Silicon IC tuner driver"); |
| 927 | MODULE_AUTHOR("Michael Krufky <mkrufky@linuxtv.org>"); |
| 928 | MODULE_LICENSE("GPL"); |
Michael Krufky | 7434ca4 | 2009-01-19 01:11:49 -0300 | [diff] [blame] | 929 | MODULE_VERSION("0.2"); |
Michael Krufky | 2a83e4d | 2008-07-07 18:20:58 -0300 | [diff] [blame] | 930 | |
| 931 | /* |
| 932 | * Overrides for Emacs so that we follow Linus's tabbing style. |
| 933 | * --------------------------------------------------------------------------- |
| 934 | * Local variables: |
| 935 | * c-basic-offset: 8 |
| 936 | * End: |
| 937 | */ |