1*c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 29a0bf528SMauro Carvalho Chehab /* 39a0bf528SMauro Carvalho Chehab * Driver for Conexant CX24113/CX24128 Tuner (Satellite) 49a0bf528SMauro Carvalho Chehab * 59a0bf528SMauro Carvalho Chehab * Copyright (C) 2007-8 Patrick Boettcher <pb@linuxtv.org> 69a0bf528SMauro Carvalho Chehab * 79a0bf528SMauro Carvalho Chehab * Developed for BBTI / Technisat 89a0bf528SMauro Carvalho Chehab */ 99a0bf528SMauro Carvalho Chehab 109a0bf528SMauro Carvalho Chehab #include <linux/slab.h> 119a0bf528SMauro Carvalho Chehab #include <linux/kernel.h> 129a0bf528SMauro Carvalho Chehab #include <linux/module.h> 139a0bf528SMauro Carvalho Chehab #include <linux/init.h> 149a0bf528SMauro Carvalho Chehab 15fada1935SMauro Carvalho Chehab #include <media/dvb_frontend.h> 169a0bf528SMauro Carvalho Chehab #include "cx24113.h" 179a0bf528SMauro Carvalho Chehab 189a0bf528SMauro Carvalho Chehab static int debug; 199a0bf528SMauro Carvalho Chehab 209a0bf528SMauro Carvalho Chehab #define cx_info(args...) do { printk(KERN_INFO "CX24113: " args); } while (0) 219a0bf528SMauro Carvalho Chehab #define cx_err(args...) do { printk(KERN_ERR "CX24113: " args); } while (0) 229a0bf528SMauro Carvalho Chehab 239a0bf528SMauro Carvalho Chehab #define dprintk(args...) \ 249a0bf528SMauro Carvalho Chehab do { \ 259a0bf528SMauro Carvalho Chehab if (debug) { \ 269a0bf528SMauro Carvalho Chehab printk(KERN_DEBUG "CX24113: %s: ", __func__); \ 279a0bf528SMauro Carvalho Chehab printk(args); \ 289a0bf528SMauro Carvalho Chehab } \ 299a0bf528SMauro Carvalho Chehab } while (0) 309a0bf528SMauro Carvalho Chehab 319a0bf528SMauro Carvalho Chehab struct cx24113_state { 329a0bf528SMauro Carvalho Chehab struct i2c_adapter *i2c; 339a0bf528SMauro Carvalho Chehab const struct cx24113_config *config; 349a0bf528SMauro Carvalho Chehab 359a0bf528SMauro Carvalho Chehab #define REV_CX24113 0x23 369a0bf528SMauro Carvalho Chehab u8 rev; 379a0bf528SMauro Carvalho Chehab u8 ver; 389a0bf528SMauro Carvalho Chehab 399a0bf528SMauro Carvalho Chehab u8 icp_mode:1; 409a0bf528SMauro Carvalho Chehab 419a0bf528SMauro Carvalho Chehab #define ICP_LEVEL1 0 429a0bf528SMauro Carvalho Chehab #define ICP_LEVEL2 1 439a0bf528SMauro Carvalho Chehab #define ICP_LEVEL3 2 449a0bf528SMauro Carvalho Chehab #define ICP_LEVEL4 3 459a0bf528SMauro Carvalho Chehab u8 icp_man:2; 469a0bf528SMauro Carvalho Chehab u8 icp_auto_low:2; 479a0bf528SMauro Carvalho Chehab u8 icp_auto_mlow:2; 489a0bf528SMauro Carvalho Chehab u8 icp_auto_mhi:2; 499a0bf528SMauro Carvalho Chehab u8 icp_auto_hi:2; 509a0bf528SMauro Carvalho Chehab u8 icp_dig; 519a0bf528SMauro Carvalho Chehab 529a0bf528SMauro Carvalho Chehab #define LNA_MIN_GAIN 0 539a0bf528SMauro Carvalho Chehab #define LNA_MID_GAIN 1 549a0bf528SMauro Carvalho Chehab #define LNA_MAX_GAIN 2 559a0bf528SMauro Carvalho Chehab u8 lna_gain:2; 569a0bf528SMauro Carvalho Chehab 579a0bf528SMauro Carvalho Chehab u8 acp_on:1; 589a0bf528SMauro Carvalho Chehab 599a0bf528SMauro Carvalho Chehab u8 vco_mode:2; 609a0bf528SMauro Carvalho Chehab u8 vco_shift:1; 619a0bf528SMauro Carvalho Chehab #define VCOBANDSEL_6 0x80 629a0bf528SMauro Carvalho Chehab #define VCOBANDSEL_5 0x01 639a0bf528SMauro Carvalho Chehab #define VCOBANDSEL_4 0x02 649a0bf528SMauro Carvalho Chehab #define VCOBANDSEL_3 0x04 659a0bf528SMauro Carvalho Chehab #define VCOBANDSEL_2 0x08 669a0bf528SMauro Carvalho Chehab #define VCOBANDSEL_1 0x10 679a0bf528SMauro Carvalho Chehab u8 vco_band; 689a0bf528SMauro Carvalho Chehab 699a0bf528SMauro Carvalho Chehab #define VCODIV4 4 709a0bf528SMauro Carvalho Chehab #define VCODIV2 2 719a0bf528SMauro Carvalho Chehab u8 vcodiv; 729a0bf528SMauro Carvalho Chehab 739a0bf528SMauro Carvalho Chehab u8 bs_delay:4; 749a0bf528SMauro Carvalho Chehab u16 bs_freqcnt:13; 759a0bf528SMauro Carvalho Chehab u16 bs_rdiv; 769a0bf528SMauro Carvalho Chehab u8 prescaler_mode:1; 779a0bf528SMauro Carvalho Chehab 789a0bf528SMauro Carvalho Chehab u8 rfvga_bias_ctrl; 799a0bf528SMauro Carvalho Chehab 809a0bf528SMauro Carvalho Chehab s16 tuner_gain_thres; 819a0bf528SMauro Carvalho Chehab u8 gain_level; 829a0bf528SMauro Carvalho Chehab 839a0bf528SMauro Carvalho Chehab u32 frequency; 849a0bf528SMauro Carvalho Chehab 859a0bf528SMauro Carvalho Chehab u8 refdiv; 869a0bf528SMauro Carvalho Chehab 879a0bf528SMauro Carvalho Chehab u8 Fwindow_enabled; 889a0bf528SMauro Carvalho Chehab }; 899a0bf528SMauro Carvalho Chehab 909a0bf528SMauro Carvalho Chehab static int cx24113_writereg(struct cx24113_state *state, int reg, int data) 919a0bf528SMauro Carvalho Chehab { 929a0bf528SMauro Carvalho Chehab u8 buf[] = { reg, data }; 939a0bf528SMauro Carvalho Chehab struct i2c_msg msg = { .addr = state->config->i2c_addr, 949a0bf528SMauro Carvalho Chehab .flags = 0, .buf = buf, .len = 2 }; 959a0bf528SMauro Carvalho Chehab int err = i2c_transfer(state->i2c, &msg, 1); 969a0bf528SMauro Carvalho Chehab if (err != 1) { 974bd69e7bSMauro Carvalho Chehab printk(KERN_DEBUG "%s: writereg error(err == %i, reg == 0x%02x, data == 0x%02x)\n", 984bd69e7bSMauro Carvalho Chehab __func__, err, reg, data); 999a0bf528SMauro Carvalho Chehab return err; 1009a0bf528SMauro Carvalho Chehab } 1019a0bf528SMauro Carvalho Chehab 1029a0bf528SMauro Carvalho Chehab return 0; 1039a0bf528SMauro Carvalho Chehab } 1049a0bf528SMauro Carvalho Chehab 1059a0bf528SMauro Carvalho Chehab static int cx24113_readreg(struct cx24113_state *state, u8 reg) 1069a0bf528SMauro Carvalho Chehab { 1079a0bf528SMauro Carvalho Chehab int ret; 1089a0bf528SMauro Carvalho Chehab u8 b; 1099a0bf528SMauro Carvalho Chehab struct i2c_msg msg[] = { 1109a0bf528SMauro Carvalho Chehab { .addr = state->config->i2c_addr, 1119a0bf528SMauro Carvalho Chehab .flags = 0, .buf = ®, .len = 1 }, 1129a0bf528SMauro Carvalho Chehab { .addr = state->config->i2c_addr, 1139a0bf528SMauro Carvalho Chehab .flags = I2C_M_RD, .buf = &b, .len = 1 } 1149a0bf528SMauro Carvalho Chehab }; 1159a0bf528SMauro Carvalho Chehab 1169a0bf528SMauro Carvalho Chehab ret = i2c_transfer(state->i2c, msg, 2); 1179a0bf528SMauro Carvalho Chehab 1189a0bf528SMauro Carvalho Chehab if (ret != 2) { 1199a0bf528SMauro Carvalho Chehab printk(KERN_DEBUG "%s: reg=0x%x (error=%d)\n", 1209a0bf528SMauro Carvalho Chehab __func__, reg, ret); 1219a0bf528SMauro Carvalho Chehab return ret; 1229a0bf528SMauro Carvalho Chehab } 1239a0bf528SMauro Carvalho Chehab 1249a0bf528SMauro Carvalho Chehab return b; 1259a0bf528SMauro Carvalho Chehab } 1269a0bf528SMauro Carvalho Chehab 1279a0bf528SMauro Carvalho Chehab static void cx24113_set_parameters(struct cx24113_state *state) 1289a0bf528SMauro Carvalho Chehab { 1299a0bf528SMauro Carvalho Chehab u8 r; 1309a0bf528SMauro Carvalho Chehab 1319a0bf528SMauro Carvalho Chehab r = cx24113_readreg(state, 0x10) & 0x82; 1329a0bf528SMauro Carvalho Chehab r |= state->icp_mode; 1339a0bf528SMauro Carvalho Chehab r |= state->icp_man << 4; 1349a0bf528SMauro Carvalho Chehab r |= state->icp_dig << 2; 1359a0bf528SMauro Carvalho Chehab r |= state->prescaler_mode << 5; 1369a0bf528SMauro Carvalho Chehab cx24113_writereg(state, 0x10, r); 1379a0bf528SMauro Carvalho Chehab 1389a0bf528SMauro Carvalho Chehab r = (state->icp_auto_low << 0) | (state->icp_auto_mlow << 2) 1399a0bf528SMauro Carvalho Chehab | (state->icp_auto_mhi << 4) | (state->icp_auto_hi << 6); 1409a0bf528SMauro Carvalho Chehab cx24113_writereg(state, 0x11, r); 1419a0bf528SMauro Carvalho Chehab 1429a0bf528SMauro Carvalho Chehab if (state->rev == REV_CX24113) { 1439a0bf528SMauro Carvalho Chehab r = cx24113_readreg(state, 0x20) & 0xec; 1449a0bf528SMauro Carvalho Chehab r |= state->lna_gain; 1459a0bf528SMauro Carvalho Chehab r |= state->rfvga_bias_ctrl << 4; 1469a0bf528SMauro Carvalho Chehab cx24113_writereg(state, 0x20, r); 1479a0bf528SMauro Carvalho Chehab } 1489a0bf528SMauro Carvalho Chehab 1499a0bf528SMauro Carvalho Chehab r = cx24113_readreg(state, 0x12) & 0x03; 1509a0bf528SMauro Carvalho Chehab r |= state->acp_on << 2; 1519a0bf528SMauro Carvalho Chehab r |= state->bs_delay << 4; 1529a0bf528SMauro Carvalho Chehab cx24113_writereg(state, 0x12, r); 1539a0bf528SMauro Carvalho Chehab 1549a0bf528SMauro Carvalho Chehab r = cx24113_readreg(state, 0x18) & 0x40; 1559a0bf528SMauro Carvalho Chehab r |= state->vco_shift; 1569a0bf528SMauro Carvalho Chehab if (state->vco_band == VCOBANDSEL_6) 1579a0bf528SMauro Carvalho Chehab r |= (1 << 7); 1589a0bf528SMauro Carvalho Chehab else 1599a0bf528SMauro Carvalho Chehab r |= (state->vco_band << 1); 1609a0bf528SMauro Carvalho Chehab cx24113_writereg(state, 0x18, r); 1619a0bf528SMauro Carvalho Chehab 1629a0bf528SMauro Carvalho Chehab r = cx24113_readreg(state, 0x14) & 0x20; 1639a0bf528SMauro Carvalho Chehab r |= (state->vco_mode << 6) | ((state->bs_freqcnt >> 8) & 0x1f); 1649a0bf528SMauro Carvalho Chehab cx24113_writereg(state, 0x14, r); 1659a0bf528SMauro Carvalho Chehab cx24113_writereg(state, 0x15, (state->bs_freqcnt & 0xff)); 1669a0bf528SMauro Carvalho Chehab 1679a0bf528SMauro Carvalho Chehab cx24113_writereg(state, 0x16, (state->bs_rdiv >> 4) & 0xff); 1689a0bf528SMauro Carvalho Chehab r = (cx24113_readreg(state, 0x17) & 0x0f) | 1699a0bf528SMauro Carvalho Chehab ((state->bs_rdiv & 0x0f) << 4); 1709a0bf528SMauro Carvalho Chehab cx24113_writereg(state, 0x17, r); 1719a0bf528SMauro Carvalho Chehab } 1729a0bf528SMauro Carvalho Chehab 1739a0bf528SMauro Carvalho Chehab #define VGA_0 0x00 1749a0bf528SMauro Carvalho Chehab #define VGA_1 0x04 1759a0bf528SMauro Carvalho Chehab #define VGA_2 0x02 1769a0bf528SMauro Carvalho Chehab #define VGA_3 0x06 1779a0bf528SMauro Carvalho Chehab #define VGA_4 0x01 1789a0bf528SMauro Carvalho Chehab #define VGA_5 0x05 1799a0bf528SMauro Carvalho Chehab #define VGA_6 0x03 1809a0bf528SMauro Carvalho Chehab #define VGA_7 0x07 1819a0bf528SMauro Carvalho Chehab 1829a0bf528SMauro Carvalho Chehab #define RFVGA_0 0x00 1839a0bf528SMauro Carvalho Chehab #define RFVGA_1 0x01 1849a0bf528SMauro Carvalho Chehab #define RFVGA_2 0x02 1859a0bf528SMauro Carvalho Chehab #define RFVGA_3 0x03 1869a0bf528SMauro Carvalho Chehab 1879a0bf528SMauro Carvalho Chehab static int cx24113_set_gain_settings(struct cx24113_state *state, 1889a0bf528SMauro Carvalho Chehab s16 power_estimation) 1899a0bf528SMauro Carvalho Chehab { 1909a0bf528SMauro Carvalho Chehab u8 ampout = cx24113_readreg(state, 0x1d) & 0xf0, 1919a0bf528SMauro Carvalho Chehab vga = cx24113_readreg(state, 0x1f) & 0x3f, 1929a0bf528SMauro Carvalho Chehab rfvga = cx24113_readreg(state, 0x20) & 0xf3; 1939a0bf528SMauro Carvalho Chehab u8 gain_level = power_estimation >= state->tuner_gain_thres; 1949a0bf528SMauro Carvalho Chehab 1959a0bf528SMauro Carvalho Chehab dprintk("power estimation: %d, thres: %d, gain_level: %d/%d\n", 1969a0bf528SMauro Carvalho Chehab power_estimation, state->tuner_gain_thres, 1979a0bf528SMauro Carvalho Chehab state->gain_level, gain_level); 1989a0bf528SMauro Carvalho Chehab 1999a0bf528SMauro Carvalho Chehab if (gain_level == state->gain_level) 2009a0bf528SMauro Carvalho Chehab return 0; /* nothing to be done */ 2019a0bf528SMauro Carvalho Chehab 2029a0bf528SMauro Carvalho Chehab ampout |= 0xf; 2039a0bf528SMauro Carvalho Chehab 2049a0bf528SMauro Carvalho Chehab if (gain_level) { 2059a0bf528SMauro Carvalho Chehab rfvga |= RFVGA_0 << 2; 2069a0bf528SMauro Carvalho Chehab vga |= (VGA_7 << 3) | VGA_7; 2079a0bf528SMauro Carvalho Chehab } else { 2089a0bf528SMauro Carvalho Chehab rfvga |= RFVGA_2 << 2; 2099a0bf528SMauro Carvalho Chehab vga |= (VGA_6 << 3) | VGA_2; 2109a0bf528SMauro Carvalho Chehab } 2119a0bf528SMauro Carvalho Chehab state->gain_level = gain_level; 2129a0bf528SMauro Carvalho Chehab 2139a0bf528SMauro Carvalho Chehab cx24113_writereg(state, 0x1d, ampout); 2149a0bf528SMauro Carvalho Chehab cx24113_writereg(state, 0x1f, vga); 2159a0bf528SMauro Carvalho Chehab cx24113_writereg(state, 0x20, rfvga); 2169a0bf528SMauro Carvalho Chehab 2179a0bf528SMauro Carvalho Chehab return 1; /* did something */ 2189a0bf528SMauro Carvalho Chehab } 2199a0bf528SMauro Carvalho Chehab 2209a0bf528SMauro Carvalho Chehab static int cx24113_set_Fref(struct cx24113_state *state, u8 high) 2219a0bf528SMauro Carvalho Chehab { 2229a0bf528SMauro Carvalho Chehab u8 xtal = cx24113_readreg(state, 0x02); 2239a0bf528SMauro Carvalho Chehab if (state->rev == 0x43 && state->vcodiv == VCODIV4) 2249a0bf528SMauro Carvalho Chehab high = 1; 2259a0bf528SMauro Carvalho Chehab 2269a0bf528SMauro Carvalho Chehab xtal &= ~0x2; 2279a0bf528SMauro Carvalho Chehab if (high) 2289a0bf528SMauro Carvalho Chehab xtal |= high << 1; 2299a0bf528SMauro Carvalho Chehab return cx24113_writereg(state, 0x02, xtal); 2309a0bf528SMauro Carvalho Chehab } 2319a0bf528SMauro Carvalho Chehab 2329a0bf528SMauro Carvalho Chehab static int cx24113_enable(struct cx24113_state *state, u8 enable) 2339a0bf528SMauro Carvalho Chehab { 2349a0bf528SMauro Carvalho Chehab u8 r21 = (cx24113_readreg(state, 0x21) & 0xc0) | enable; 2359a0bf528SMauro Carvalho Chehab if (state->rev == REV_CX24113) 2369a0bf528SMauro Carvalho Chehab r21 |= (1 << 1); 2379a0bf528SMauro Carvalho Chehab return cx24113_writereg(state, 0x21, r21); 2389a0bf528SMauro Carvalho Chehab } 2399a0bf528SMauro Carvalho Chehab 2409a0bf528SMauro Carvalho Chehab static int cx24113_set_bandwidth(struct cx24113_state *state, u32 bandwidth_khz) 2419a0bf528SMauro Carvalho Chehab { 2429a0bf528SMauro Carvalho Chehab u8 r; 2439a0bf528SMauro Carvalho Chehab 2449a0bf528SMauro Carvalho Chehab if (bandwidth_khz <= 19000) 2459a0bf528SMauro Carvalho Chehab r = 0x03 << 6; 2469a0bf528SMauro Carvalho Chehab else if (bandwidth_khz <= 25000) 2479a0bf528SMauro Carvalho Chehab r = 0x02 << 6; 2489a0bf528SMauro Carvalho Chehab else 2499a0bf528SMauro Carvalho Chehab r = 0x01 << 6; 2509a0bf528SMauro Carvalho Chehab 2519a0bf528SMauro Carvalho Chehab dprintk("bandwidth to be set: %d\n", bandwidth_khz); 2529a0bf528SMauro Carvalho Chehab bandwidth_khz *= 10; 2539a0bf528SMauro Carvalho Chehab bandwidth_khz -= 10000; 2549a0bf528SMauro Carvalho Chehab bandwidth_khz /= 1000; 2559a0bf528SMauro Carvalho Chehab bandwidth_khz += 5; 2569a0bf528SMauro Carvalho Chehab bandwidth_khz /= 10; 2579a0bf528SMauro Carvalho Chehab 2589a0bf528SMauro Carvalho Chehab dprintk("bandwidth: %d %d\n", r >> 6, bandwidth_khz); 2599a0bf528SMauro Carvalho Chehab 2609a0bf528SMauro Carvalho Chehab r |= bandwidth_khz & 0x3f; 2619a0bf528SMauro Carvalho Chehab 2629a0bf528SMauro Carvalho Chehab return cx24113_writereg(state, 0x1e, r); 2639a0bf528SMauro Carvalho Chehab } 2649a0bf528SMauro Carvalho Chehab 2659a0bf528SMauro Carvalho Chehab static int cx24113_set_clk_inversion(struct cx24113_state *state, u8 on) 2669a0bf528SMauro Carvalho Chehab { 2679a0bf528SMauro Carvalho Chehab u8 r = (cx24113_readreg(state, 0x10) & 0x7f) | ((on & 0x1) << 7); 2689a0bf528SMauro Carvalho Chehab return cx24113_writereg(state, 0x10, r); 2699a0bf528SMauro Carvalho Chehab } 2709a0bf528SMauro Carvalho Chehab 2719a0bf528SMauro Carvalho Chehab static int cx24113_get_status(struct dvb_frontend *fe, u32 *status) 2729a0bf528SMauro Carvalho Chehab { 2739a0bf528SMauro Carvalho Chehab struct cx24113_state *state = fe->tuner_priv; 2749a0bf528SMauro Carvalho Chehab u8 r = (cx24113_readreg(state, 0x10) & 0x02) >> 1; 2759a0bf528SMauro Carvalho Chehab if (r) 2769a0bf528SMauro Carvalho Chehab *status |= TUNER_STATUS_LOCKED; 2779a0bf528SMauro Carvalho Chehab dprintk("PLL locked: %d\n", r); 2789a0bf528SMauro Carvalho Chehab return 0; 2799a0bf528SMauro Carvalho Chehab } 2809a0bf528SMauro Carvalho Chehab 2819a0bf528SMauro Carvalho Chehab static u8 cx24113_set_ref_div(struct cx24113_state *state, u8 refdiv) 2829a0bf528SMauro Carvalho Chehab { 2839a0bf528SMauro Carvalho Chehab if (state->rev == 0x43 && state->vcodiv == VCODIV4) 2849a0bf528SMauro Carvalho Chehab refdiv = 2; 2859a0bf528SMauro Carvalho Chehab return state->refdiv = refdiv; 2869a0bf528SMauro Carvalho Chehab } 2879a0bf528SMauro Carvalho Chehab 2889a0bf528SMauro Carvalho Chehab static void cx24113_calc_pll_nf(struct cx24113_state *state, u16 *n, s32 *f) 2899a0bf528SMauro Carvalho Chehab { 2909a0bf528SMauro Carvalho Chehab s32 N; 2919a0bf528SMauro Carvalho Chehab s64 F; 2929a0bf528SMauro Carvalho Chehab u64 dividend; 2939a0bf528SMauro Carvalho Chehab u8 R, r; 2949a0bf528SMauro Carvalho Chehab u8 vcodiv; 2959a0bf528SMauro Carvalho Chehab u8 factor; 2969a0bf528SMauro Carvalho Chehab s32 freq_hz = state->frequency * 1000; 2979a0bf528SMauro Carvalho Chehab 2989a0bf528SMauro Carvalho Chehab if (state->config->xtal_khz < 20000) 2999a0bf528SMauro Carvalho Chehab factor = 1; 3009a0bf528SMauro Carvalho Chehab else 3019a0bf528SMauro Carvalho Chehab factor = 2; 3029a0bf528SMauro Carvalho Chehab 3039a0bf528SMauro Carvalho Chehab if (state->rev == REV_CX24113) { 3049a0bf528SMauro Carvalho Chehab if (state->frequency >= 1100000) 3059a0bf528SMauro Carvalho Chehab vcodiv = VCODIV2; 3069a0bf528SMauro Carvalho Chehab else 3079a0bf528SMauro Carvalho Chehab vcodiv = VCODIV4; 3089a0bf528SMauro Carvalho Chehab } else { 3099a0bf528SMauro Carvalho Chehab if (state->frequency >= 1165000) 3109a0bf528SMauro Carvalho Chehab vcodiv = VCODIV2; 3119a0bf528SMauro Carvalho Chehab else 3129a0bf528SMauro Carvalho Chehab vcodiv = VCODIV4; 3139a0bf528SMauro Carvalho Chehab } 3149a0bf528SMauro Carvalho Chehab state->vcodiv = vcodiv; 3159a0bf528SMauro Carvalho Chehab 3169a0bf528SMauro Carvalho Chehab dprintk("calculating N/F for %dHz with vcodiv %d\n", freq_hz, vcodiv); 3179a0bf528SMauro Carvalho Chehab R = 0; 3189a0bf528SMauro Carvalho Chehab do { 3199a0bf528SMauro Carvalho Chehab R = cx24113_set_ref_div(state, R + 1); 3209a0bf528SMauro Carvalho Chehab 3219a0bf528SMauro Carvalho Chehab /* calculate tuner PLL settings: */ 3229a0bf528SMauro Carvalho Chehab N = (freq_hz / 100 * vcodiv) * R; 3239a0bf528SMauro Carvalho Chehab N /= (state->config->xtal_khz) * factor * 2; 3249a0bf528SMauro Carvalho Chehab N += 5; /* For round up. */ 3259a0bf528SMauro Carvalho Chehab N /= 10; 3269a0bf528SMauro Carvalho Chehab N -= 32; 3279a0bf528SMauro Carvalho Chehab } while (N < 6 && R < 3); 3289a0bf528SMauro Carvalho Chehab 3299a0bf528SMauro Carvalho Chehab if (N < 6) { 3309a0bf528SMauro Carvalho Chehab cx_err("strange frequency: N < 6\n"); 3319a0bf528SMauro Carvalho Chehab return; 3329a0bf528SMauro Carvalho Chehab } 3339a0bf528SMauro Carvalho Chehab F = freq_hz; 3349a0bf528SMauro Carvalho Chehab F *= (u64) (R * vcodiv * 262144); 3359a0bf528SMauro Carvalho Chehab dprintk("1 N: %d, F: %lld, R: %d\n", N, (long long)F, R); 3369a0bf528SMauro Carvalho Chehab /* do_div needs an u64 as first argument */ 3379a0bf528SMauro Carvalho Chehab dividend = F; 3389a0bf528SMauro Carvalho Chehab do_div(dividend, state->config->xtal_khz * 1000 * factor * 2); 3399a0bf528SMauro Carvalho Chehab F = dividend; 3409a0bf528SMauro Carvalho Chehab dprintk("2 N: %d, F: %lld, R: %d\n", N, (long long)F, R); 3419a0bf528SMauro Carvalho Chehab F -= (N + 32) * 262144; 3429a0bf528SMauro Carvalho Chehab 3439a0bf528SMauro Carvalho Chehab dprintk("3 N: %d, F: %lld, R: %d\n", N, (long long)F, R); 3449a0bf528SMauro Carvalho Chehab 3459a0bf528SMauro Carvalho Chehab if (state->Fwindow_enabled) { 3469a0bf528SMauro Carvalho Chehab if (F > (262144 / 2 - 1638)) 3479a0bf528SMauro Carvalho Chehab F = 262144 / 2 - 1638; 3489a0bf528SMauro Carvalho Chehab if (F < (-262144 / 2 + 1638)) 3499a0bf528SMauro Carvalho Chehab F = -262144 / 2 + 1638; 3509a0bf528SMauro Carvalho Chehab if ((F < 3277 && F > 0) || (F > -3277 && F < 0)) { 3519a0bf528SMauro Carvalho Chehab F = 0; 3529a0bf528SMauro Carvalho Chehab r = cx24113_readreg(state, 0x10); 3539a0bf528SMauro Carvalho Chehab cx24113_writereg(state, 0x10, r | (1 << 6)); 3549a0bf528SMauro Carvalho Chehab } 3559a0bf528SMauro Carvalho Chehab } 3569a0bf528SMauro Carvalho Chehab dprintk("4 N: %d, F: %lld, R: %d\n", N, (long long)F, R); 3579a0bf528SMauro Carvalho Chehab 3589a0bf528SMauro Carvalho Chehab *n = (u16) N; 3599a0bf528SMauro Carvalho Chehab *f = (s32) F; 3609a0bf528SMauro Carvalho Chehab } 3619a0bf528SMauro Carvalho Chehab 3629a0bf528SMauro Carvalho Chehab 3639a0bf528SMauro Carvalho Chehab static void cx24113_set_nfr(struct cx24113_state *state, u16 n, s32 f, u8 r) 3649a0bf528SMauro Carvalho Chehab { 3659a0bf528SMauro Carvalho Chehab u8 reg; 3669a0bf528SMauro Carvalho Chehab cx24113_writereg(state, 0x19, (n >> 1) & 0xff); 3679a0bf528SMauro Carvalho Chehab 3689a0bf528SMauro Carvalho Chehab reg = ((n & 0x1) << 7) | ((f >> 11) & 0x7f); 3699a0bf528SMauro Carvalho Chehab cx24113_writereg(state, 0x1a, reg); 3709a0bf528SMauro Carvalho Chehab 3719a0bf528SMauro Carvalho Chehab cx24113_writereg(state, 0x1b, (f >> 3) & 0xff); 3729a0bf528SMauro Carvalho Chehab 3739a0bf528SMauro Carvalho Chehab reg = cx24113_readreg(state, 0x1c) & 0x1f; 3749a0bf528SMauro Carvalho Chehab cx24113_writereg(state, 0x1c, reg | ((f & 0x7) << 5)); 3759a0bf528SMauro Carvalho Chehab 3769a0bf528SMauro Carvalho Chehab cx24113_set_Fref(state, r - 1); 3779a0bf528SMauro Carvalho Chehab } 3789a0bf528SMauro Carvalho Chehab 3799a0bf528SMauro Carvalho Chehab static int cx24113_set_frequency(struct cx24113_state *state, u32 frequency) 3809a0bf528SMauro Carvalho Chehab { 3819a0bf528SMauro Carvalho Chehab u8 r = 1; /* or 2 */ 3829a0bf528SMauro Carvalho Chehab u16 n = 6; 3839a0bf528SMauro Carvalho Chehab s32 f = 0; 3849a0bf528SMauro Carvalho Chehab 3859a0bf528SMauro Carvalho Chehab r = cx24113_readreg(state, 0x14); 3869a0bf528SMauro Carvalho Chehab cx24113_writereg(state, 0x14, r & 0x3f); 3879a0bf528SMauro Carvalho Chehab 3889a0bf528SMauro Carvalho Chehab r = cx24113_readreg(state, 0x10); 3899a0bf528SMauro Carvalho Chehab cx24113_writereg(state, 0x10, r & 0xbf); 3909a0bf528SMauro Carvalho Chehab 3919a0bf528SMauro Carvalho Chehab state->frequency = frequency; 3929a0bf528SMauro Carvalho Chehab 3939a0bf528SMauro Carvalho Chehab dprintk("tuning to frequency: %d\n", frequency); 3949a0bf528SMauro Carvalho Chehab 3959a0bf528SMauro Carvalho Chehab cx24113_calc_pll_nf(state, &n, &f); 3969a0bf528SMauro Carvalho Chehab cx24113_set_nfr(state, n, f, state->refdiv); 3979a0bf528SMauro Carvalho Chehab 3989a0bf528SMauro Carvalho Chehab r = cx24113_readreg(state, 0x18) & 0xbf; 3999a0bf528SMauro Carvalho Chehab if (state->vcodiv != VCODIV2) 4009a0bf528SMauro Carvalho Chehab r |= 1 << 6; 4019a0bf528SMauro Carvalho Chehab cx24113_writereg(state, 0x18, r); 4029a0bf528SMauro Carvalho Chehab 4039a0bf528SMauro Carvalho Chehab /* The need for this sleep is not clear. But helps in some cases */ 4049a0bf528SMauro Carvalho Chehab msleep(5); 4059a0bf528SMauro Carvalho Chehab 4069a0bf528SMauro Carvalho Chehab r = cx24113_readreg(state, 0x1c) & 0xef; 4079a0bf528SMauro Carvalho Chehab cx24113_writereg(state, 0x1c, r | (1 << 4)); 4089a0bf528SMauro Carvalho Chehab return 0; 4099a0bf528SMauro Carvalho Chehab } 4109a0bf528SMauro Carvalho Chehab 4119a0bf528SMauro Carvalho Chehab static int cx24113_init(struct dvb_frontend *fe) 4129a0bf528SMauro Carvalho Chehab { 4139a0bf528SMauro Carvalho Chehab struct cx24113_state *state = fe->tuner_priv; 4149a0bf528SMauro Carvalho Chehab int ret; 4159a0bf528SMauro Carvalho Chehab 4169a0bf528SMauro Carvalho Chehab state->tuner_gain_thres = -50; 4179a0bf528SMauro Carvalho Chehab state->gain_level = 255; /* to force a gain-setting initialization */ 4189a0bf528SMauro Carvalho Chehab state->icp_mode = 0; 4199a0bf528SMauro Carvalho Chehab 4209a0bf528SMauro Carvalho Chehab if (state->config->xtal_khz < 11000) { 4219a0bf528SMauro Carvalho Chehab state->icp_auto_hi = ICP_LEVEL4; 4229a0bf528SMauro Carvalho Chehab state->icp_auto_mhi = ICP_LEVEL4; 4239a0bf528SMauro Carvalho Chehab state->icp_auto_mlow = ICP_LEVEL3; 4249a0bf528SMauro Carvalho Chehab state->icp_auto_low = ICP_LEVEL3; 4259a0bf528SMauro Carvalho Chehab } else { 4269a0bf528SMauro Carvalho Chehab state->icp_auto_hi = ICP_LEVEL4; 4279a0bf528SMauro Carvalho Chehab state->icp_auto_mhi = ICP_LEVEL4; 4289a0bf528SMauro Carvalho Chehab state->icp_auto_mlow = ICP_LEVEL3; 4299a0bf528SMauro Carvalho Chehab state->icp_auto_low = ICP_LEVEL2; 4309a0bf528SMauro Carvalho Chehab } 4319a0bf528SMauro Carvalho Chehab 4329a0bf528SMauro Carvalho Chehab state->icp_dig = ICP_LEVEL3; 4339a0bf528SMauro Carvalho Chehab state->icp_man = ICP_LEVEL1; 4349a0bf528SMauro Carvalho Chehab state->acp_on = 1; 4359a0bf528SMauro Carvalho Chehab state->vco_mode = 0; 4369a0bf528SMauro Carvalho Chehab state->vco_shift = 0; 4379a0bf528SMauro Carvalho Chehab state->vco_band = VCOBANDSEL_1; 4389a0bf528SMauro Carvalho Chehab state->bs_delay = 8; 4399a0bf528SMauro Carvalho Chehab state->bs_freqcnt = 0x0fff; 4409a0bf528SMauro Carvalho Chehab state->bs_rdiv = 0x0fff; 4419a0bf528SMauro Carvalho Chehab state->prescaler_mode = 0; 4429a0bf528SMauro Carvalho Chehab state->lna_gain = LNA_MAX_GAIN; 4439a0bf528SMauro Carvalho Chehab state->rfvga_bias_ctrl = 1; 4449a0bf528SMauro Carvalho Chehab state->Fwindow_enabled = 1; 4459a0bf528SMauro Carvalho Chehab 4469a0bf528SMauro Carvalho Chehab cx24113_set_Fref(state, 0); 4479a0bf528SMauro Carvalho Chehab cx24113_enable(state, 0x3d); 4489a0bf528SMauro Carvalho Chehab cx24113_set_parameters(state); 4499a0bf528SMauro Carvalho Chehab 4509a0bf528SMauro Carvalho Chehab cx24113_set_gain_settings(state, -30); 4519a0bf528SMauro Carvalho Chehab 4529a0bf528SMauro Carvalho Chehab cx24113_set_bandwidth(state, 18025); 4539a0bf528SMauro Carvalho Chehab cx24113_set_clk_inversion(state, 1); 4549a0bf528SMauro Carvalho Chehab 4559a0bf528SMauro Carvalho Chehab if (state->config->xtal_khz >= 40000) 4569a0bf528SMauro Carvalho Chehab ret = cx24113_writereg(state, 0x02, 4579a0bf528SMauro Carvalho Chehab (cx24113_readreg(state, 0x02) & 0xfb) | (1 << 2)); 4589a0bf528SMauro Carvalho Chehab else 4599a0bf528SMauro Carvalho Chehab ret = cx24113_writereg(state, 0x02, 4609a0bf528SMauro Carvalho Chehab (cx24113_readreg(state, 0x02) & 0xfb) | (0 << 2)); 4619a0bf528SMauro Carvalho Chehab 4629a0bf528SMauro Carvalho Chehab return ret; 4639a0bf528SMauro Carvalho Chehab } 4649a0bf528SMauro Carvalho Chehab 4659a0bf528SMauro Carvalho Chehab static int cx24113_set_params(struct dvb_frontend *fe) 4669a0bf528SMauro Carvalho Chehab { 4679a0bf528SMauro Carvalho Chehab struct dtv_frontend_properties *c = &fe->dtv_property_cache; 4689a0bf528SMauro Carvalho Chehab struct cx24113_state *state = fe->tuner_priv; 4699a0bf528SMauro Carvalho Chehab /* for a ROLL-OFF factor of 0.35, 0.2: 600, 0.25: 625 */ 4709a0bf528SMauro Carvalho Chehab u32 roll_off = 675; 4719a0bf528SMauro Carvalho Chehab u32 bw; 4729a0bf528SMauro Carvalho Chehab 4739a0bf528SMauro Carvalho Chehab bw = ((c->symbol_rate/100) * roll_off) / 1000; 4749a0bf528SMauro Carvalho Chehab bw += (10000000/100) + 5; 4759a0bf528SMauro Carvalho Chehab bw /= 10; 4769a0bf528SMauro Carvalho Chehab bw += 1000; 4779a0bf528SMauro Carvalho Chehab cx24113_set_bandwidth(state, bw); 4789a0bf528SMauro Carvalho Chehab 4799a0bf528SMauro Carvalho Chehab cx24113_set_frequency(state, c->frequency); 4809a0bf528SMauro Carvalho Chehab msleep(5); 4819a0bf528SMauro Carvalho Chehab return cx24113_get_status(fe, &bw); 4829a0bf528SMauro Carvalho Chehab } 4839a0bf528SMauro Carvalho Chehab 4849a0bf528SMauro Carvalho Chehab static s8 cx24113_agc_table[2][10] = { 4859a0bf528SMauro Carvalho Chehab {-54, -41, -35, -30, -25, -21, -16, -10, -6, -2}, 4869a0bf528SMauro Carvalho Chehab {-39, -35, -30, -25, -19, -15, -11, -5, 1, 9}, 4879a0bf528SMauro Carvalho Chehab }; 4889a0bf528SMauro Carvalho Chehab 4899a0bf528SMauro Carvalho Chehab void cx24113_agc_callback(struct dvb_frontend *fe) 4909a0bf528SMauro Carvalho Chehab { 4919a0bf528SMauro Carvalho Chehab struct cx24113_state *state = fe->tuner_priv; 4929a0bf528SMauro Carvalho Chehab s16 s, i; 4939a0bf528SMauro Carvalho Chehab if (!fe->ops.read_signal_strength) 4949a0bf528SMauro Carvalho Chehab return; 4959a0bf528SMauro Carvalho Chehab 4969a0bf528SMauro Carvalho Chehab do { 4979a0bf528SMauro Carvalho Chehab /* this only works with the current CX24123 implementation */ 4989a0bf528SMauro Carvalho Chehab fe->ops.read_signal_strength(fe, (u16 *) &s); 4999a0bf528SMauro Carvalho Chehab s >>= 8; 5009a0bf528SMauro Carvalho Chehab dprintk("signal strength: %d\n", s); 5019a0bf528SMauro Carvalho Chehab for (i = 0; i < sizeof(cx24113_agc_table[0]); i++) 5029a0bf528SMauro Carvalho Chehab if (cx24113_agc_table[state->gain_level][i] > s) 5039a0bf528SMauro Carvalho Chehab break; 5049a0bf528SMauro Carvalho Chehab s = -25 - i*5; 5059a0bf528SMauro Carvalho Chehab } while (cx24113_set_gain_settings(state, s)); 5069a0bf528SMauro Carvalho Chehab } 5079a0bf528SMauro Carvalho Chehab EXPORT_SYMBOL(cx24113_agc_callback); 5089a0bf528SMauro Carvalho Chehab 5099a0bf528SMauro Carvalho Chehab static int cx24113_get_frequency(struct dvb_frontend *fe, u32 *frequency) 5109a0bf528SMauro Carvalho Chehab { 5119a0bf528SMauro Carvalho Chehab struct cx24113_state *state = fe->tuner_priv; 5129a0bf528SMauro Carvalho Chehab *frequency = state->frequency; 5139a0bf528SMauro Carvalho Chehab return 0; 5149a0bf528SMauro Carvalho Chehab } 5159a0bf528SMauro Carvalho Chehab 516194ced7aSMax Kellermann static void cx24113_release(struct dvb_frontend *fe) 5179a0bf528SMauro Carvalho Chehab { 5189a0bf528SMauro Carvalho Chehab struct cx24113_state *state = fe->tuner_priv; 5199a0bf528SMauro Carvalho Chehab dprintk("\n"); 5209a0bf528SMauro Carvalho Chehab fe->tuner_priv = NULL; 5219a0bf528SMauro Carvalho Chehab kfree(state); 5229a0bf528SMauro Carvalho Chehab } 5239a0bf528SMauro Carvalho Chehab 5249a0bf528SMauro Carvalho Chehab static const struct dvb_tuner_ops cx24113_tuner_ops = { 5259a0bf528SMauro Carvalho Chehab .info = { 5269a0bf528SMauro Carvalho Chehab .name = "Conexant CX24113", 527a3f90c75SMauro Carvalho Chehab .frequency_min_hz = 950 * MHz, 528a3f90c75SMauro Carvalho Chehab .frequency_max_hz = 2150 * MHz, 529a3f90c75SMauro Carvalho Chehab .frequency_step_hz = 125 * kHz, 5309a0bf528SMauro Carvalho Chehab }, 5319a0bf528SMauro Carvalho Chehab 5329a0bf528SMauro Carvalho Chehab .release = cx24113_release, 5339a0bf528SMauro Carvalho Chehab 5349a0bf528SMauro Carvalho Chehab .init = cx24113_init, 5359a0bf528SMauro Carvalho Chehab 5369a0bf528SMauro Carvalho Chehab .set_params = cx24113_set_params, 5379a0bf528SMauro Carvalho Chehab .get_frequency = cx24113_get_frequency, 5389a0bf528SMauro Carvalho Chehab .get_status = cx24113_get_status, 5399a0bf528SMauro Carvalho Chehab }; 5409a0bf528SMauro Carvalho Chehab 5419a0bf528SMauro Carvalho Chehab struct dvb_frontend *cx24113_attach(struct dvb_frontend *fe, 5429a0bf528SMauro Carvalho Chehab const struct cx24113_config *config, struct i2c_adapter *i2c) 5439a0bf528SMauro Carvalho Chehab { 5449a0bf528SMauro Carvalho Chehab /* allocate memory for the internal state */ 5452d3da59fSMarkus Elfring struct cx24113_state *state = kzalloc(sizeof(*state), GFP_KERNEL); 5469a0bf528SMauro Carvalho Chehab int rc; 547c38e8657SMarkus Elfring 548c38e8657SMarkus Elfring if (!state) 5499722e569SMarkus Elfring return NULL; 5509a0bf528SMauro Carvalho Chehab 5519a0bf528SMauro Carvalho Chehab /* setup the state */ 5529a0bf528SMauro Carvalho Chehab state->config = config; 5539a0bf528SMauro Carvalho Chehab state->i2c = i2c; 5549a0bf528SMauro Carvalho Chehab 5559a0bf528SMauro Carvalho Chehab cx_info("trying to detect myself\n"); 5569a0bf528SMauro Carvalho Chehab 5579a0bf528SMauro Carvalho Chehab /* making a dummy read, because of some expected troubles 5589a0bf528SMauro Carvalho Chehab * after power on */ 5599a0bf528SMauro Carvalho Chehab cx24113_readreg(state, 0x00); 5609a0bf528SMauro Carvalho Chehab 5619a0bf528SMauro Carvalho Chehab rc = cx24113_readreg(state, 0x00); 5629a0bf528SMauro Carvalho Chehab if (rc < 0) { 5639a0bf528SMauro Carvalho Chehab cx_info("CX24113 not found.\n"); 5649a0bf528SMauro Carvalho Chehab goto error; 5659a0bf528SMauro Carvalho Chehab } 5669a0bf528SMauro Carvalho Chehab state->rev = rc; 5679a0bf528SMauro Carvalho Chehab 5689a0bf528SMauro Carvalho Chehab switch (rc) { 5699a0bf528SMauro Carvalho Chehab case 0x43: 5709a0bf528SMauro Carvalho Chehab cx_info("detected CX24113 variant\n"); 5719a0bf528SMauro Carvalho Chehab break; 5729a0bf528SMauro Carvalho Chehab case REV_CX24113: 5739a0bf528SMauro Carvalho Chehab cx_info("successfully detected\n"); 5749a0bf528SMauro Carvalho Chehab break; 5759a0bf528SMauro Carvalho Chehab default: 5769a0bf528SMauro Carvalho Chehab cx_err("unsupported device id: %x\n", state->rev); 5779a0bf528SMauro Carvalho Chehab goto error; 5789a0bf528SMauro Carvalho Chehab } 5799a0bf528SMauro Carvalho Chehab state->ver = cx24113_readreg(state, 0x01); 5809a0bf528SMauro Carvalho Chehab cx_info("version: %x\n", state->ver); 5819a0bf528SMauro Carvalho Chehab 5829a0bf528SMauro Carvalho Chehab /* create dvb_frontend */ 5839a0bf528SMauro Carvalho Chehab memcpy(&fe->ops.tuner_ops, &cx24113_tuner_ops, 5849a0bf528SMauro Carvalho Chehab sizeof(struct dvb_tuner_ops)); 5859a0bf528SMauro Carvalho Chehab fe->tuner_priv = state; 5869a0bf528SMauro Carvalho Chehab return fe; 5879a0bf528SMauro Carvalho Chehab 5889a0bf528SMauro Carvalho Chehab error: 5899a0bf528SMauro Carvalho Chehab kfree(state); 5909a0bf528SMauro Carvalho Chehab 5919a0bf528SMauro Carvalho Chehab return NULL; 5929a0bf528SMauro Carvalho Chehab } 5939a0bf528SMauro Carvalho Chehab EXPORT_SYMBOL(cx24113_attach); 5949a0bf528SMauro Carvalho Chehab 5959a0bf528SMauro Carvalho Chehab module_param(debug, int, 0644); 5969a0bf528SMauro Carvalho Chehab MODULE_PARM_DESC(debug, "Activates frontend debugging (default:0)"); 5979a0bf528SMauro Carvalho Chehab 5989a0bf528SMauro Carvalho Chehab MODULE_AUTHOR("Patrick Boettcher <pb@linuxtv.org>"); 5999a0bf528SMauro Carvalho Chehab MODULE_DESCRIPTION("DVB Frontend module for Conexant CX24113/CX24128hardware"); 6009a0bf528SMauro Carvalho Chehab MODULE_LICENSE("GPL"); 6019a0bf528SMauro Carvalho Chehab 602