Thomas Gleixner | c942fdd | 2019-05-27 08:55:06 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Mac Michaels | d8667cb | 2005-07-07 17:58:29 -0700 | [diff] [blame] | 2 | /* |
Michael Krufky | 1963c90 | 2005-08-08 09:22:43 -0700 | [diff] [blame] | 3 | * Support for LGDT3302 and LGDT3303 - VSB/QAM |
Mac Michaels | d8667cb | 2005-07-07 17:58:29 -0700 | [diff] [blame] | 4 | * |
| 5 | * Copyright (C) 2005 Wilson Michaels <wilsonmichaels@earthlink.net> |
Mac Michaels | d8667cb | 2005-07-07 17:58:29 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | /* |
| 9 | * NOTES ABOUT THIS DRIVER |
| 10 | * |
Michael Krufky | 1963c90 | 2005-08-08 09:22:43 -0700 | [diff] [blame] | 11 | * This Linux driver supports: |
| 12 | * DViCO FusionHDTV 3 Gold-Q |
| 13 | * DViCO FusionHDTV 3 Gold-T |
| 14 | * DViCO FusionHDTV 5 Gold |
Michael Krufky | 3cff00d | 2005-11-08 21:35:12 -0800 | [diff] [blame] | 15 | * DViCO FusionHDTV 5 Lite |
Michael Krufky | d8e6acf | 2006-01-09 15:32:42 -0200 | [diff] [blame] | 16 | * DViCO FusionHDTV 5 USB Gold |
Michael Krufky | c0b11b9 | 2005-11-08 21:35:32 -0800 | [diff] [blame] | 17 | * Air2PC/AirStar 2 ATSC 3rd generation (HD5000) |
Michael Krufky | 20fe4f65 | 2006-04-10 09:40:40 -0300 | [diff] [blame] | 18 | * pcHDTV HD5500 |
Mac Michaels | d8667cb | 2005-07-07 17:58:29 -0700 | [diff] [blame] | 19 | * |
Mac Michaels | d8667cb | 2005-07-07 17:58:29 -0700 | [diff] [blame] | 20 | */ |
| 21 | |
Mac Michaels | d8667cb | 2005-07-07 17:58:29 -0700 | [diff] [blame] | 22 | #include <linux/kernel.h> |
| 23 | #include <linux/module.h> |
Mac Michaels | d8667cb | 2005-07-07 17:58:29 -0700 | [diff] [blame] | 24 | #include <linux/init.h> |
| 25 | #include <linux/delay.h> |
Tim Schmielau | 4e57b68 | 2005-10-30 15:03:48 -0800 | [diff] [blame] | 26 | #include <linux/string.h> |
| 27 | #include <linux/slab.h> |
Mac Michaels | d8667cb | 2005-07-07 17:58:29 -0700 | [diff] [blame] | 28 | #include <asm/byteorder.h> |
| 29 | |
Mauro Carvalho Chehab | fada193 | 2017-12-28 13:03:51 -0500 | [diff] [blame] | 30 | #include <media/dvb_frontend.h> |
| 31 | #include <media/dvb_math.h> |
Michael Krufky | 6ddcc91 | 2005-07-27 11:46:00 -0700 | [diff] [blame] | 32 | #include "lgdt330x_priv.h" |
| 33 | #include "lgdt330x.h" |
Mac Michaels | d8667cb | 2005-07-07 17:58:29 -0700 | [diff] [blame] | 34 | |
Trent Piepho | 19be685 | 2006-10-29 13:35:39 -0300 | [diff] [blame] | 35 | /* Use Equalizer Mean Squared Error instead of Phaser Tracker MSE */ |
| 36 | /* #define USE_EQMSE */ |
| 37 | |
Douglas Schilling Landgraf | ff699e6 | 2008-04-22 14:41:48 -0300 | [diff] [blame] | 38 | static int debug; |
Mac Michaels | d8667cb | 2005-07-07 17:58:29 -0700 | [diff] [blame] | 39 | module_param(debug, int, 0644); |
Mauro Carvalho Chehab | 467845a | 2018-03-09 10:53:27 -0500 | [diff] [blame] | 40 | MODULE_PARM_DESC(debug, "Turn on/off lgdt330x frontend debugging (default:off)."); |
Mauro Carvalho Chehab | 6752c79 | 2018-03-09 10:53:28 -0500 | [diff] [blame] | 41 | |
Mauro Carvalho Chehab | 23ba635 | 2018-03-09 10:53:30 -0500 | [diff] [blame] | 42 | #define dprintk(state, fmt, arg...) do { \ |
Mauro Carvalho Chehab | 6752c79 | 2018-03-09 10:53:28 -0500 | [diff] [blame] | 43 | if (debug) \ |
Mauro Carvalho Chehab | 23ba635 | 2018-03-09 10:53:30 -0500 | [diff] [blame] | 44 | dev_printk(KERN_DEBUG, &state->client->dev, fmt, ##arg);\ |
Mac Michaels | d8667cb | 2005-07-07 17:58:29 -0700 | [diff] [blame] | 45 | } while (0) |
| 46 | |
Mauro Carvalho Chehab | 467845a | 2018-03-09 10:53:27 -0500 | [diff] [blame] | 47 | struct lgdt330x_state { |
Mauro Carvalho Chehab | 23ba635 | 2018-03-09 10:53:30 -0500 | [diff] [blame] | 48 | struct i2c_client *client; |
Mac Michaels | d8667cb | 2005-07-07 17:58:29 -0700 | [diff] [blame] | 49 | |
| 50 | /* Configuration settings */ |
Mauro Carvalho Chehab | 23ba635 | 2018-03-09 10:53:30 -0500 | [diff] [blame] | 51 | struct lgdt330x_config config; |
Mac Michaels | d8667cb | 2005-07-07 17:58:29 -0700 | [diff] [blame] | 52 | |
| 53 | struct dvb_frontend frontend; |
| 54 | |
| 55 | /* Demodulator private data */ |
Mauro Carvalho Chehab | 0df289a | 2015-06-07 14:53:52 -0300 | [diff] [blame] | 56 | enum fe_modulation current_modulation; |
Mauro Carvalho Chehab | 19bdd0d | 2018-03-09 10:53:34 -0500 | [diff] [blame] | 57 | u32 snr; /* Result of last SNR calculation */ |
Mauro Carvalho Chehab | 5f939c3 | 2018-03-09 11:15:41 -0500 | [diff] [blame] | 58 | u16 ucblocks; |
| 59 | unsigned long last_stats_time; |
Mac Michaels | d8667cb | 2005-07-07 17:58:29 -0700 | [diff] [blame] | 60 | |
| 61 | /* Tuner private data */ |
| 62 | u32 current_frequency; |
| 63 | }; |
| 64 | |
Mauro Carvalho Chehab | 467845a | 2018-03-09 10:53:27 -0500 | [diff] [blame] | 65 | static int i2c_write_demod_bytes(struct lgdt330x_state *state, |
Mauro Carvalho Chehab | 23ba635 | 2018-03-09 10:53:30 -0500 | [diff] [blame] | 66 | const u8 *buf, /* data bytes to send */ |
Mauro Carvalho Chehab | 467845a | 2018-03-09 10:53:27 -0500 | [diff] [blame] | 67 | int len /* number of bytes to send */) |
Mac Michaels | d8667cb | 2005-07-07 17:58:29 -0700 | [diff] [blame] | 68 | { |
Michael Krufky | b6aef07 | 2005-07-27 11:45:54 -0700 | [diff] [blame] | 69 | int i; |
Michael Krufky | 1963c90 | 2005-08-08 09:22:43 -0700 | [diff] [blame] | 70 | int err; |
Mac Michaels | d8667cb | 2005-07-07 17:58:29 -0700 | [diff] [blame] | 71 | |
Mauro Carvalho Chehab | 467845a | 2018-03-09 10:53:27 -0500 | [diff] [blame] | 72 | for (i = 0; i < len - 1; i += 2) { |
Mauro Carvalho Chehab | 23ba635 | 2018-03-09 10:53:30 -0500 | [diff] [blame] | 73 | err = i2c_master_send(state->client, buf, 2); |
| 74 | if (err != 2) { |
| 75 | dev_warn(&state->client->dev, |
| 76 | "%s: error (addr %02x <- %02x, err = %i)\n", |
| 77 | __func__, buf[0], buf[1], err); |
Michael Krufky | 58ba006 | 2005-07-12 13:58:37 -0700 | [diff] [blame] | 78 | if (err < 0) |
| 79 | return err; |
| 80 | else |
| 81 | return -EREMOTEIO; |
Mac Michaels | d8667cb | 2005-07-07 17:58:29 -0700 | [diff] [blame] | 82 | } |
Mauro Carvalho Chehab | 23ba635 | 2018-03-09 10:53:30 -0500 | [diff] [blame] | 83 | buf += 2; |
Mac Michaels | d8667cb | 2005-07-07 17:58:29 -0700 | [diff] [blame] | 84 | } |
| 85 | return 0; |
| 86 | } |
Michael Krufky | b6aef07 | 2005-07-27 11:45:54 -0700 | [diff] [blame] | 87 | |
Mac Michaels | d8667cb | 2005-07-07 17:58:29 -0700 | [diff] [blame] | 88 | /* |
| 89 | * This routine writes the register (reg) to the demod bus |
| 90 | * then reads the data returned for (len) bytes. |
| 91 | */ |
Xi Wang | 3481717 | 2012-02-14 14:32:41 -0300 | [diff] [blame] | 92 | static int i2c_read_demod_bytes(struct lgdt330x_state *state, |
| 93 | enum I2C_REG reg, u8 *buf, int len) |
Mac Michaels | d8667cb | 2005-07-07 17:58:29 -0700 | [diff] [blame] | 94 | { |
Mauro Carvalho Chehab | 467845a | 2018-03-09 10:53:27 -0500 | [diff] [blame] | 95 | u8 wr[] = { reg }; |
| 96 | struct i2c_msg msg[] = { |
| 97 | { |
Mauro Carvalho Chehab | 23ba635 | 2018-03-09 10:53:30 -0500 | [diff] [blame] | 98 | .addr = state->client->addr, |
Mauro Carvalho Chehab | 467845a | 2018-03-09 10:53:27 -0500 | [diff] [blame] | 99 | .flags = 0, |
| 100 | .buf = wr, |
| 101 | .len = 1 |
| 102 | }, { |
Mauro Carvalho Chehab | 23ba635 | 2018-03-09 10:53:30 -0500 | [diff] [blame] | 103 | .addr = state->client->addr, |
Mauro Carvalho Chehab | 467845a | 2018-03-09 10:53:27 -0500 | [diff] [blame] | 104 | .flags = I2C_M_RD, |
| 105 | .buf = buf, |
| 106 | .len = len |
| 107 | }, |
Mac Michaels | d8667cb | 2005-07-07 17:58:29 -0700 | [diff] [blame] | 108 | }; |
| 109 | int ret; |
Mauro Carvalho Chehab | 467845a | 2018-03-09 10:53:27 -0500 | [diff] [blame] | 110 | |
Mauro Carvalho Chehab | 23ba635 | 2018-03-09 10:53:30 -0500 | [diff] [blame] | 111 | ret = i2c_transfer(state->client->adapter, msg, 2); |
Mac Michaels | d8667cb | 2005-07-07 17:58:29 -0700 | [diff] [blame] | 112 | if (ret != 2) { |
Mauro Carvalho Chehab | 23ba635 | 2018-03-09 10:53:30 -0500 | [diff] [blame] | 113 | dev_warn(&state->client->dev, |
| 114 | "%s: addr 0x%02x select 0x%02x error (ret == %i)\n", |
| 115 | __func__, state->client->addr, reg, ret); |
Xi Wang | 3481717 | 2012-02-14 14:32:41 -0300 | [diff] [blame] | 116 | if (ret >= 0) |
| 117 | ret = -EIO; |
Mac Michaels | d8667cb | 2005-07-07 17:58:29 -0700 | [diff] [blame] | 118 | } else { |
| 119 | ret = 0; |
| 120 | } |
| 121 | return ret; |
| 122 | } |
| 123 | |
| 124 | /* Software reset */ |
Mauro Carvalho Chehab | 467845a | 2018-03-09 10:53:27 -0500 | [diff] [blame] | 125 | static int lgdt3302_sw_reset(struct lgdt330x_state *state) |
Mac Michaels | d8667cb | 2005-07-07 17:58:29 -0700 | [diff] [blame] | 126 | { |
| 127 | u8 ret; |
| 128 | u8 reset[] = { |
| 129 | IRQ_MASK, |
Mauro Carvalho Chehab | 467845a | 2018-03-09 10:53:27 -0500 | [diff] [blame] | 130 | /* |
| 131 | * bit 6 is active low software reset |
| 132 | * bits 5-0 are 1 to mask interrupts |
| 133 | */ |
| 134 | 0x00 |
Mac Michaels | d8667cb | 2005-07-07 17:58:29 -0700 | [diff] [blame] | 135 | }; |
| 136 | |
Michael Krufky | 1963c90 | 2005-08-08 09:22:43 -0700 | [diff] [blame] | 137 | ret = i2c_write_demod_bytes(state, |
Michael Krufky | dc9ca2a | 2005-09-06 15:19:40 -0700 | [diff] [blame] | 138 | reset, sizeof(reset)); |
Mac Michaels | d8667cb | 2005-07-07 17:58:29 -0700 | [diff] [blame] | 139 | if (ret == 0) { |
Michael Krufky | 1963c90 | 2005-08-08 09:22:43 -0700 | [diff] [blame] | 140 | /* force reset high (inactive) and unmask interrupts */ |
| 141 | reset[1] = 0x7f; |
| 142 | ret = i2c_write_demod_bytes(state, |
Michael Krufky | dc9ca2a | 2005-09-06 15:19:40 -0700 | [diff] [blame] | 143 | reset, sizeof(reset)); |
Mac Michaels | d8667cb | 2005-07-07 17:58:29 -0700 | [diff] [blame] | 144 | } |
Mac Michaels | d8667cb | 2005-07-07 17:58:29 -0700 | [diff] [blame] | 145 | return ret; |
| 146 | } |
| 147 | |
Mauro Carvalho Chehab | 467845a | 2018-03-09 10:53:27 -0500 | [diff] [blame] | 148 | static int lgdt3303_sw_reset(struct lgdt330x_state *state) |
Michael Krufky | 1963c90 | 2005-08-08 09:22:43 -0700 | [diff] [blame] | 149 | { |
| 150 | u8 ret; |
| 151 | u8 reset[] = { |
| 152 | 0x02, |
| 153 | 0x00 /* bit 0 is active low software reset */ |
| 154 | }; |
| 155 | |
| 156 | ret = i2c_write_demod_bytes(state, |
Michael Krufky | dc9ca2a | 2005-09-06 15:19:40 -0700 | [diff] [blame] | 157 | reset, sizeof(reset)); |
Michael Krufky | 1963c90 | 2005-08-08 09:22:43 -0700 | [diff] [blame] | 158 | if (ret == 0) { |
Michael Krufky | 1963c90 | 2005-08-08 09:22:43 -0700 | [diff] [blame] | 159 | /* force reset high (inactive) */ |
| 160 | reset[1] = 0x01; |
| 161 | ret = i2c_write_demod_bytes(state, |
Michael Krufky | dc9ca2a | 2005-09-06 15:19:40 -0700 | [diff] [blame] | 162 | reset, sizeof(reset)); |
Michael Krufky | 1963c90 | 2005-08-08 09:22:43 -0700 | [diff] [blame] | 163 | } |
| 164 | return ret; |
| 165 | } |
| 166 | |
Mauro Carvalho Chehab | 467845a | 2018-03-09 10:53:27 -0500 | [diff] [blame] | 167 | static int lgdt330x_sw_reset(struct lgdt330x_state *state) |
Michael Krufky | 1963c90 | 2005-08-08 09:22:43 -0700 | [diff] [blame] | 168 | { |
Mauro Carvalho Chehab | 23ba635 | 2018-03-09 10:53:30 -0500 | [diff] [blame] | 169 | switch (state->config.demod_chip) { |
Michael Krufky | 1963c90 | 2005-08-08 09:22:43 -0700 | [diff] [blame] | 170 | case LGDT3302: |
Mauro Carvalho Chehab | 467845a | 2018-03-09 10:53:27 -0500 | [diff] [blame] | 171 | return lgdt3302_sw_reset(state); |
Michael Krufky | 1963c90 | 2005-08-08 09:22:43 -0700 | [diff] [blame] | 172 | case LGDT3303: |
Mauro Carvalho Chehab | 467845a | 2018-03-09 10:53:27 -0500 | [diff] [blame] | 173 | return lgdt3303_sw_reset(state); |
Michael Krufky | 1963c90 | 2005-08-08 09:22:43 -0700 | [diff] [blame] | 174 | default: |
| 175 | return -ENODEV; |
| 176 | } |
| 177 | } |
| 178 | |
Mauro Carvalho Chehab | 467845a | 2018-03-09 10:53:27 -0500 | [diff] [blame] | 179 | static int lgdt330x_init(struct dvb_frontend *fe) |
Mac Michaels | d8667cb | 2005-07-07 17:58:29 -0700 | [diff] [blame] | 180 | { |
Mauro Carvalho Chehab | 1334a7d | 2018-03-09 10:53:32 -0500 | [diff] [blame] | 181 | struct lgdt330x_state *state = fe->demodulator_priv; |
Mauro Carvalho Chehab | 19bdd0d | 2018-03-09 10:53:34 -0500 | [diff] [blame] | 182 | struct dtv_frontend_properties *p = &fe->dtv_property_cache; |
Mauro Carvalho Chehab | 1334a7d | 2018-03-09 10:53:32 -0500 | [diff] [blame] | 183 | char *chip_name; |
| 184 | int err; |
Michael Krufky | 1963c90 | 2005-08-08 09:22:43 -0700 | [diff] [blame] | 185 | /* |
| 186 | * Array of byte pairs <address, value> |
| 187 | * to initialize each different chip |
| 188 | */ |
Mauro Carvalho Chehab | 1334a7d | 2018-03-09 10:53:32 -0500 | [diff] [blame] | 189 | static const u8 lgdt3302_init_data[] = { |
Mauro Carvalho Chehab | 467845a | 2018-03-09 10:53:27 -0500 | [diff] [blame] | 190 | /* Use 50MHz param values from spec sheet since xtal is 50 */ |
| 191 | /* |
| 192 | * Change the value of NCOCTFV[25:0] of carrier |
| 193 | * recovery center frequency register |
| 194 | */ |
Michael Krufky | 1963c90 | 2005-08-08 09:22:43 -0700 | [diff] [blame] | 195 | VSB_CARRIER_FREQ0, 0x00, |
| 196 | VSB_CARRIER_FREQ1, 0x87, |
| 197 | VSB_CARRIER_FREQ2, 0x8e, |
| 198 | VSB_CARRIER_FREQ3, 0x01, |
Mauro Carvalho Chehab | 467845a | 2018-03-09 10:53:27 -0500 | [diff] [blame] | 199 | /* |
| 200 | * Change the TPCLK pin polarity |
| 201 | * data is valid on falling clock |
| 202 | */ |
Michael Krufky | 1963c90 | 2005-08-08 09:22:43 -0700 | [diff] [blame] | 203 | DEMUX_CONTROL, 0xfb, |
Mauro Carvalho Chehab | 467845a | 2018-03-09 10:53:27 -0500 | [diff] [blame] | 204 | /* |
| 205 | * Change the value of IFBW[11:0] of |
| 206 | * AGC IF/RF loop filter bandwidth register |
| 207 | */ |
Michael Krufky | 1963c90 | 2005-08-08 09:22:43 -0700 | [diff] [blame] | 208 | AGC_RF_BANDWIDTH0, 0x40, |
| 209 | AGC_RF_BANDWIDTH1, 0x93, |
| 210 | AGC_RF_BANDWIDTH2, 0x00, |
Mauro Carvalho Chehab | 467845a | 2018-03-09 10:53:27 -0500 | [diff] [blame] | 211 | /* |
| 212 | * Change the value of bit 6, 'nINAGCBY' and |
| 213 | * 'NSSEL[1:0] of ACG function control register 2 |
| 214 | */ |
Michael Krufky | 1963c90 | 2005-08-08 09:22:43 -0700 | [diff] [blame] | 215 | AGC_FUNC_CTRL2, 0xc6, |
Mauro Carvalho Chehab | 467845a | 2018-03-09 10:53:27 -0500 | [diff] [blame] | 216 | /* |
| 217 | * Change the value of bit 6 'RFFIX' |
| 218 | * of AGC function control register 3 |
| 219 | */ |
Michael Krufky | 1963c90 | 2005-08-08 09:22:43 -0700 | [diff] [blame] | 220 | AGC_FUNC_CTRL3, 0x40, |
Mauro Carvalho Chehab | 467845a | 2018-03-09 10:53:27 -0500 | [diff] [blame] | 221 | /* |
| 222 | * Set the value of 'INLVTHD' register 0x2a/0x2c |
| 223 | * to 0x7fe |
| 224 | */ |
Michael Krufky | 1963c90 | 2005-08-08 09:22:43 -0700 | [diff] [blame] | 225 | AGC_DELAY0, 0x07, |
| 226 | AGC_DELAY2, 0xfe, |
Mauro Carvalho Chehab | 467845a | 2018-03-09 10:53:27 -0500 | [diff] [blame] | 227 | /* |
| 228 | * Change the value of IAGCBW[15:8] |
| 229 | * of inner AGC loop filter bandwidth |
| 230 | */ |
Michael Krufky | 1963c90 | 2005-08-08 09:22:43 -0700 | [diff] [blame] | 231 | AGC_LOOP_BANDWIDTH0, 0x08, |
| 232 | AGC_LOOP_BANDWIDTH1, 0x9a |
| 233 | }; |
Mauro Carvalho Chehab | 1334a7d | 2018-03-09 10:53:32 -0500 | [diff] [blame] | 234 | static const u8 lgdt3303_init_data[] = { |
Michael Krufky | 1963c90 | 2005-08-08 09:22:43 -0700 | [diff] [blame] | 235 | 0x4c, 0x14 |
| 236 | }; |
Mauro Carvalho Chehab | 1334a7d | 2018-03-09 10:53:32 -0500 | [diff] [blame] | 237 | static const u8 flip_1_lgdt3303_init_data[] = { |
Michael Krufky | c0b11b9 | 2005-11-08 21:35:32 -0800 | [diff] [blame] | 238 | 0x4c, 0x14, |
| 239 | 0x87, 0xf3 |
| 240 | }; |
Mauro Carvalho Chehab | 1334a7d | 2018-03-09 10:53:32 -0500 | [diff] [blame] | 241 | static const u8 flip_2_lgdt3303_init_data[] = { |
Michael Krufky | c0f4c0a | 2008-06-28 02:06:50 -0300 | [diff] [blame] | 242 | 0x4c, 0x14, |
| 243 | 0x87, 0xda |
| 244 | }; |
| 245 | |
Mauro Carvalho Chehab | 1334a7d | 2018-03-09 10:53:32 -0500 | [diff] [blame] | 246 | /* |
| 247 | * Hardware reset is done using gpio[0] of cx23880x chip. |
| 248 | * I'd like to do it here, but don't know how to find chip address. |
| 249 | * cx88-cards.c arranges for the reset bit to be inactive (high). |
| 250 | * Maybe there needs to be a callable function in cx88-core or |
| 251 | * the caller of this function needs to do it. |
| 252 | */ |
Michael Krufky | 1963c90 | 2005-08-08 09:22:43 -0700 | [diff] [blame] | 253 | |
Mauro Carvalho Chehab | 23ba635 | 2018-03-09 10:53:30 -0500 | [diff] [blame] | 254 | switch (state->config.demod_chip) { |
Michael Krufky | 1963c90 | 2005-08-08 09:22:43 -0700 | [diff] [blame] | 255 | case LGDT3302: |
| 256 | chip_name = "LGDT3302"; |
| 257 | err = i2c_write_demod_bytes(state, lgdt3302_init_data, |
Michael Krufky | dc9ca2a | 2005-09-06 15:19:40 -0700 | [diff] [blame] | 258 | sizeof(lgdt3302_init_data)); |
| 259 | break; |
Michael Krufky | 1963c90 | 2005-08-08 09:22:43 -0700 | [diff] [blame] | 260 | case LGDT3303: |
| 261 | chip_name = "LGDT3303"; |
Mauro Carvalho Chehab | 23ba635 | 2018-03-09 10:53:30 -0500 | [diff] [blame] | 262 | switch (state->config.clock_polarity_flip) { |
Michael Krufky | c0f4c0a | 2008-06-28 02:06:50 -0300 | [diff] [blame] | 263 | case 2: |
| 264 | err = i2c_write_demod_bytes(state, |
Mauro Carvalho Chehab | 467845a | 2018-03-09 10:53:27 -0500 | [diff] [blame] | 265 | flip_2_lgdt3303_init_data, |
| 266 | sizeof(flip_2_lgdt3303_init_data)); |
Michael Krufky | c0f4c0a | 2008-06-28 02:06:50 -0300 | [diff] [blame] | 267 | break; |
| 268 | case 1: |
| 269 | err = i2c_write_demod_bytes(state, |
Mauro Carvalho Chehab | 467845a | 2018-03-09 10:53:27 -0500 | [diff] [blame] | 270 | flip_1_lgdt3303_init_data, |
| 271 | sizeof(flip_1_lgdt3303_init_data)); |
Michael Krufky | c0f4c0a | 2008-06-28 02:06:50 -0300 | [diff] [blame] | 272 | break; |
| 273 | case 0: |
| 274 | default: |
Michael Krufky | c0b11b9 | 2005-11-08 21:35:32 -0800 | [diff] [blame] | 275 | err = i2c_write_demod_bytes(state, lgdt3303_init_data, |
| 276 | sizeof(lgdt3303_init_data)); |
| 277 | } |
Michael Krufky | dc9ca2a | 2005-09-06 15:19:40 -0700 | [diff] [blame] | 278 | break; |
Michael Krufky | 1963c90 | 2005-08-08 09:22:43 -0700 | [diff] [blame] | 279 | default: |
| 280 | chip_name = "undefined"; |
Mauro Carvalho Chehab | 23ba635 | 2018-03-09 10:53:30 -0500 | [diff] [blame] | 281 | dev_warn(&state->client->dev, |
| 282 | "Only LGDT3302 and LGDT3303 are supported chips.\n"); |
Michael Krufky | 1963c90 | 2005-08-08 09:22:43 -0700 | [diff] [blame] | 283 | err = -ENODEV; |
| 284 | } |
Mauro Carvalho Chehab | 23ba635 | 2018-03-09 10:53:30 -0500 | [diff] [blame] | 285 | dprintk(state, "Initialized the %s chip\n", chip_name); |
Michael Krufky | 1963c90 | 2005-08-08 09:22:43 -0700 | [diff] [blame] | 286 | if (err < 0) |
| 287 | return err; |
Mauro Carvalho Chehab | 19bdd0d | 2018-03-09 10:53:34 -0500 | [diff] [blame] | 288 | |
| 289 | p->cnr.len = 1; |
| 290 | p->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE; |
Mauro Carvalho Chehab | 5f939c3 | 2018-03-09 11:15:41 -0500 | [diff] [blame] | 291 | p->block_error.len = 1; |
| 292 | p->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE; |
| 293 | p->block_count.len = 1; |
| 294 | p->block_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE; |
| 295 | state->last_stats_time = 0; |
Mauro Carvalho Chehab | 19bdd0d | 2018-03-09 10:53:34 -0500 | [diff] [blame] | 296 | |
Mauro Carvalho Chehab | 467845a | 2018-03-09 10:53:27 -0500 | [diff] [blame] | 297 | return lgdt330x_sw_reset(state); |
Mac Michaels | d8667cb | 2005-07-07 17:58:29 -0700 | [diff] [blame] | 298 | } |
| 299 | |
Mauro Carvalho Chehab | 467845a | 2018-03-09 10:53:27 -0500 | [diff] [blame] | 300 | static int lgdt330x_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks) |
Mac Michaels | d8667cb | 2005-07-07 17:58:29 -0700 | [diff] [blame] | 301 | { |
Mauro Carvalho Chehab | 467845a | 2018-03-09 10:53:27 -0500 | [diff] [blame] | 302 | struct lgdt330x_state *state = fe->demodulator_priv; |
Mac Michaels | d8667cb | 2005-07-07 17:58:29 -0700 | [diff] [blame] | 303 | |
Mauro Carvalho Chehab | 5f939c3 | 2018-03-09 11:15:41 -0500 | [diff] [blame] | 304 | *ucblocks = state->ucblocks; |
Michael Krufky | 26110da | 2011-12-15 10:16:09 -0300 | [diff] [blame] | 305 | |
Mac Michaels | d8667cb | 2005-07-07 17:58:29 -0700 | [diff] [blame] | 306 | return 0; |
| 307 | } |
| 308 | |
Mauro Carvalho Chehab | ca7072d | 2011-12-26 11:25:21 -0300 | [diff] [blame] | 309 | static int lgdt330x_set_parameters(struct dvb_frontend *fe) |
Mac Michaels | d8667cb | 2005-07-07 17:58:29 -0700 | [diff] [blame] | 310 | { |
Mauro Carvalho Chehab | ca7072d | 2011-12-26 11:25:21 -0300 | [diff] [blame] | 311 | struct dtv_frontend_properties *p = &fe->dtv_property_cache; |
Mauro Carvalho Chehab | 1334a7d | 2018-03-09 10:53:32 -0500 | [diff] [blame] | 312 | struct lgdt330x_state *state = fe->demodulator_priv; |
Michael Krufky | 1963c90 | 2005-08-08 09:22:43 -0700 | [diff] [blame] | 313 | /* |
| 314 | * Array of byte pairs <address, value> |
| 315 | * to initialize 8VSB for lgdt3303 chip 50 MHz IF |
| 316 | */ |
Mauro Carvalho Chehab | 1334a7d | 2018-03-09 10:53:32 -0500 | [diff] [blame] | 317 | static const u8 lgdt3303_8vsb_44_data[] = { |
Michael Krufky | 1963c90 | 2005-08-08 09:22:43 -0700 | [diff] [blame] | 318 | 0x04, 0x00, |
| 319 | 0x0d, 0x40, |
Michael Krufky | 0b6389f | 2006-11-28 02:35:02 -0300 | [diff] [blame] | 320 | 0x0e, 0x87, |
| 321 | 0x0f, 0x8e, |
| 322 | 0x10, 0x01, |
Mauro Carvalho Chehab | 467845a | 2018-03-09 10:53:27 -0500 | [diff] [blame] | 323 | 0x47, 0x8b |
| 324 | }; |
Michael Krufky | 1963c90 | 2005-08-08 09:22:43 -0700 | [diff] [blame] | 325 | /* |
| 326 | * Array of byte pairs <address, value> |
| 327 | * to initialize QAM for lgdt3303 chip |
| 328 | */ |
Mauro Carvalho Chehab | 1334a7d | 2018-03-09 10:53:32 -0500 | [diff] [blame] | 329 | static const u8 lgdt3303_qam_data[] = { |
Michael Krufky | 1963c90 | 2005-08-08 09:22:43 -0700 | [diff] [blame] | 330 | 0x04, 0x00, |
| 331 | 0x0d, 0x00, |
| 332 | 0x0e, 0x00, |
| 333 | 0x0f, 0x00, |
| 334 | 0x10, 0x00, |
| 335 | 0x51, 0x63, |
| 336 | 0x47, 0x66, |
| 337 | 0x48, 0x66, |
| 338 | 0x4d, 0x1a, |
| 339 | 0x49, 0x08, |
Mauro Carvalho Chehab | 467845a | 2018-03-09 10:53:27 -0500 | [diff] [blame] | 340 | 0x4a, 0x9b |
| 341 | }; |
Mauro Carvalho Chehab | 1334a7d | 2018-03-09 10:53:32 -0500 | [diff] [blame] | 342 | u8 top_ctrl_cfg[] = { TOP_CONTROL, 0x03 }; |
Mac Michaels | d8667cb | 2005-07-07 17:58:29 -0700 | [diff] [blame] | 343 | |
Michael Krufky | 54828d1 | 2011-12-15 10:30:38 -0300 | [diff] [blame] | 344 | int err = 0; |
Mac Michaels | d8667cb | 2005-07-07 17:58:29 -0700 | [diff] [blame] | 345 | /* Change only if we are actually changing the modulation */ |
Mauro Carvalho Chehab | ca7072d | 2011-12-26 11:25:21 -0300 | [diff] [blame] | 346 | if (state->current_modulation != p->modulation) { |
| 347 | switch (p->modulation) { |
Mac Michaels | d8667cb | 2005-07-07 17:58:29 -0700 | [diff] [blame] | 348 | case VSB_8: |
Mauro Carvalho Chehab | 23ba635 | 2018-03-09 10:53:30 -0500 | [diff] [blame] | 349 | dprintk(state, "VSB_8 MODE\n"); |
Mac Michaels | d8667cb | 2005-07-07 17:58:29 -0700 | [diff] [blame] | 350 | |
Michael Krufky | 1963c90 | 2005-08-08 09:22:43 -0700 | [diff] [blame] | 351 | /* Select VSB mode */ |
| 352 | top_ctrl_cfg[1] = 0x03; |
Michael Krufky | 0ccef6d | 2005-07-27 11:45:55 -0700 | [diff] [blame] | 353 | |
| 354 | /* Select ANT connector if supported by card */ |
Mauro Carvalho Chehab | 23ba635 | 2018-03-09 10:53:30 -0500 | [diff] [blame] | 355 | if (state->config.pll_rf_set) |
| 356 | state->config.pll_rf_set(fe, 1); |
Michael Krufky | 1963c90 | 2005-08-08 09:22:43 -0700 | [diff] [blame] | 357 | |
Mauro Carvalho Chehab | 23ba635 | 2018-03-09 10:53:30 -0500 | [diff] [blame] | 358 | if (state->config.demod_chip == LGDT3303) { |
Mauro Carvalho Chehab | 467845a | 2018-03-09 10:53:27 -0500 | [diff] [blame] | 359 | err = i2c_write_demod_bytes(state, |
| 360 | lgdt3303_8vsb_44_data, |
Michael Krufky | dc9ca2a | 2005-09-06 15:19:40 -0700 | [diff] [blame] | 361 | sizeof(lgdt3303_8vsb_44_data)); |
Michael Krufky | 1963c90 | 2005-08-08 09:22:43 -0700 | [diff] [blame] | 362 | } |
Mac Michaels | d8667cb | 2005-07-07 17:58:29 -0700 | [diff] [blame] | 363 | break; |
| 364 | |
| 365 | case QAM_64: |
Mauro Carvalho Chehab | 23ba635 | 2018-03-09 10:53:30 -0500 | [diff] [blame] | 366 | dprintk(state, "QAM_64 MODE\n"); |
Mac Michaels | d8667cb | 2005-07-07 17:58:29 -0700 | [diff] [blame] | 367 | |
Michael Krufky | 1963c90 | 2005-08-08 09:22:43 -0700 | [diff] [blame] | 368 | /* Select QAM_64 mode */ |
| 369 | top_ctrl_cfg[1] = 0x00; |
Michael Krufky | 0ccef6d | 2005-07-27 11:45:55 -0700 | [diff] [blame] | 370 | |
| 371 | /* Select CABLE connector if supported by card */ |
Mauro Carvalho Chehab | 23ba635 | 2018-03-09 10:53:30 -0500 | [diff] [blame] | 372 | if (state->config.pll_rf_set) |
| 373 | state->config.pll_rf_set(fe, 0); |
Michael Krufky | 1963c90 | 2005-08-08 09:22:43 -0700 | [diff] [blame] | 374 | |
Mauro Carvalho Chehab | 23ba635 | 2018-03-09 10:53:30 -0500 | [diff] [blame] | 375 | if (state->config.demod_chip == LGDT3303) { |
Mauro Carvalho Chehab | 467845a | 2018-03-09 10:53:27 -0500 | [diff] [blame] | 376 | err = i2c_write_demod_bytes(state, |
| 377 | lgdt3303_qam_data, |
| 378 | sizeof(lgdt3303_qam_data)); |
Michael Krufky | 1963c90 | 2005-08-08 09:22:43 -0700 | [diff] [blame] | 379 | } |
Mac Michaels | d8667cb | 2005-07-07 17:58:29 -0700 | [diff] [blame] | 380 | break; |
| 381 | |
| 382 | case QAM_256: |
Mauro Carvalho Chehab | 23ba635 | 2018-03-09 10:53:30 -0500 | [diff] [blame] | 383 | dprintk(state, "QAM_256 MODE\n"); |
Mac Michaels | d8667cb | 2005-07-07 17:58:29 -0700 | [diff] [blame] | 384 | |
Michael Krufky | 1963c90 | 2005-08-08 09:22:43 -0700 | [diff] [blame] | 385 | /* Select QAM_256 mode */ |
| 386 | top_ctrl_cfg[1] = 0x01; |
Michael Krufky | 0ccef6d | 2005-07-27 11:45:55 -0700 | [diff] [blame] | 387 | |
| 388 | /* Select CABLE connector if supported by card */ |
Mauro Carvalho Chehab | 23ba635 | 2018-03-09 10:53:30 -0500 | [diff] [blame] | 389 | if (state->config.pll_rf_set) |
| 390 | state->config.pll_rf_set(fe, 0); |
Michael Krufky | 1963c90 | 2005-08-08 09:22:43 -0700 | [diff] [blame] | 391 | |
Mauro Carvalho Chehab | 23ba635 | 2018-03-09 10:53:30 -0500 | [diff] [blame] | 392 | if (state->config.demod_chip == LGDT3303) { |
Mauro Carvalho Chehab | 467845a | 2018-03-09 10:53:27 -0500 | [diff] [blame] | 393 | err = i2c_write_demod_bytes(state, |
| 394 | lgdt3303_qam_data, |
| 395 | sizeof(lgdt3303_qam_data)); |
Michael Krufky | 1963c90 | 2005-08-08 09:22:43 -0700 | [diff] [blame] | 396 | } |
Mac Michaels | d8667cb | 2005-07-07 17:58:29 -0700 | [diff] [blame] | 397 | break; |
| 398 | default: |
Mauro Carvalho Chehab | 23ba635 | 2018-03-09 10:53:30 -0500 | [diff] [blame] | 399 | dev_warn(&state->client->dev, |
| 400 | "%s: Modulation type(%d) UNSUPPORTED\n", |
| 401 | __func__, p->modulation); |
Mac Michaels | d8667cb | 2005-07-07 17:58:29 -0700 | [diff] [blame] | 402 | return -1; |
| 403 | } |
Michael Krufky | 54828d1 | 2011-12-15 10:30:38 -0300 | [diff] [blame] | 404 | if (err < 0) |
Mauro Carvalho Chehab | 23ba635 | 2018-03-09 10:53:30 -0500 | [diff] [blame] | 405 | dev_warn(&state->client->dev, |
| 406 | "%s: error blasting bytes to lgdt3303 for modulation type(%d)\n", |
| 407 | __func__, p->modulation); |
Michael Krufky | 54828d1 | 2011-12-15 10:30:38 -0300 | [diff] [blame] | 408 | |
Michael Krufky | 1963c90 | 2005-08-08 09:22:43 -0700 | [diff] [blame] | 409 | /* |
Mauro Carvalho Chehab | 467845a | 2018-03-09 10:53:27 -0500 | [diff] [blame] | 410 | * select serial or parallel MPEG hardware interface |
Michael Krufky | 1963c90 | 2005-08-08 09:22:43 -0700 | [diff] [blame] | 411 | * Serial: 0x04 for LGDT3302 or 0x40 for LGDT3303 |
| 412 | * Parallel: 0x00 |
| 413 | */ |
Mauro Carvalho Chehab | 23ba635 | 2018-03-09 10:53:30 -0500 | [diff] [blame] | 414 | top_ctrl_cfg[1] |= state->config.serial_mpeg; |
Mac Michaels | d8667cb | 2005-07-07 17:58:29 -0700 | [diff] [blame] | 415 | |
| 416 | /* Select the requested mode */ |
Michael Krufky | 1963c90 | 2005-08-08 09:22:43 -0700 | [diff] [blame] | 417 | i2c_write_demod_bytes(state, top_ctrl_cfg, |
Michael Krufky | dc9ca2a | 2005-09-06 15:19:40 -0700 | [diff] [blame] | 418 | sizeof(top_ctrl_cfg)); |
Mauro Carvalho Chehab | 23ba635 | 2018-03-09 10:53:30 -0500 | [diff] [blame] | 419 | if (state->config.set_ts_params) |
| 420 | state->config.set_ts_params(fe, 0); |
Mauro Carvalho Chehab | ca7072d | 2011-12-26 11:25:21 -0300 | [diff] [blame] | 421 | state->current_modulation = p->modulation; |
Mac Michaels | d8667cb | 2005-07-07 17:58:29 -0700 | [diff] [blame] | 422 | } |
Mac Michaels | d8667cb | 2005-07-07 17:58:29 -0700 | [diff] [blame] | 423 | |
Michael Krufky | dc9ca2a | 2005-09-06 15:19:40 -0700 | [diff] [blame] | 424 | /* Tune to the specified frequency */ |
Patrick Boettcher | dea7486 | 2006-05-14 05:01:31 -0300 | [diff] [blame] | 425 | if (fe->ops.tuner_ops.set_params) { |
Mauro Carvalho Chehab | 14d24d1 | 2011-12-24 12:24:33 -0300 | [diff] [blame] | 426 | fe->ops.tuner_ops.set_params(fe); |
Mauro Carvalho Chehab | 467845a | 2018-03-09 10:53:27 -0500 | [diff] [blame] | 427 | if (fe->ops.i2c_gate_ctrl) |
| 428 | fe->ops.i2c_gate_ctrl(fe, 0); |
Andrew de Quincey | 02269f3 | 2006-04-18 17:47:11 -0300 | [diff] [blame] | 429 | } |
Michael Krufky | dc9ca2a | 2005-09-06 15:19:40 -0700 | [diff] [blame] | 430 | |
| 431 | /* Keep track of the new frequency */ |
Mauro Carvalho Chehab | 467845a | 2018-03-09 10:53:27 -0500 | [diff] [blame] | 432 | /* |
| 433 | * FIXME this is the wrong way to do this... |
| 434 | * The tuner is shared with the video4linux analog API |
| 435 | */ |
Mauro Carvalho Chehab | ca7072d | 2011-12-26 11:25:21 -0300 | [diff] [blame] | 436 | state->current_frequency = p->frequency; |
Michael Krufky | dc9ca2a | 2005-09-06 15:19:40 -0700 | [diff] [blame] | 437 | |
Mauro Carvalho Chehab | 467845a | 2018-03-09 10:53:27 -0500 | [diff] [blame] | 438 | lgdt330x_sw_reset(state); |
Mac Michaels | d8667cb | 2005-07-07 17:58:29 -0700 | [diff] [blame] | 439 | return 0; |
| 440 | } |
| 441 | |
Mauro Carvalho Chehab | 7e3e68b | 2016-02-04 12:58:30 -0200 | [diff] [blame] | 442 | static int lgdt330x_get_frontend(struct dvb_frontend *fe, |
| 443 | struct dtv_frontend_properties *p) |
Mac Michaels | d8667cb | 2005-07-07 17:58:29 -0700 | [diff] [blame] | 444 | { |
Michael Krufky | 6ddcc91 | 2005-07-27 11:46:00 -0700 | [diff] [blame] | 445 | struct lgdt330x_state *state = fe->demodulator_priv; |
Mauro Carvalho Chehab | 7e3e68b | 2016-02-04 12:58:30 -0200 | [diff] [blame] | 446 | |
Mauro Carvalho Chehab | ca7072d | 2011-12-26 11:25:21 -0300 | [diff] [blame] | 447 | p->frequency = state->current_frequency; |
Mac Michaels | d8667cb | 2005-07-07 17:58:29 -0700 | [diff] [blame] | 448 | return 0; |
| 449 | } |
| 450 | |
Mauro Carvalho Chehab | 467845a | 2018-03-09 10:53:27 -0500 | [diff] [blame] | 451 | /* |
| 452 | * Calculate SNR estimation (scaled by 2^24) |
| 453 | * |
| 454 | * 8-VSB SNR equations from LGDT3302 and LGDT3303 datasheets, QAM |
| 455 | * equations from LGDT3303 datasheet. VSB is the same between the '02 |
| 456 | * and '03, so maybe QAM is too? Perhaps someone with a newer datasheet |
| 457 | * that has QAM information could verify? |
| 458 | * |
| 459 | * For 8-VSB: (two ways, take your pick) |
| 460 | * LGDT3302: |
| 461 | * SNR_EQ = 10 * log10(25 * 24^2 / EQ_MSE) |
| 462 | * LGDT3303: |
| 463 | * SNR_EQ = 10 * log10(25 * 32^2 / EQ_MSE) |
| 464 | * LGDT3302 & LGDT3303: |
| 465 | * SNR_PT = 10 * log10(25 * 32^2 / PT_MSE) (we use this one) |
| 466 | * For 64-QAM: |
| 467 | * SNR = 10 * log10( 688128 / MSEQAM) |
| 468 | * For 256-QAM: |
| 469 | * SNR = 10 * log10( 696320 / MSEQAM) |
| 470 | * |
| 471 | * We re-write the snr equation as: |
| 472 | * SNR * 2^24 = 10*(c - intlog10(MSE)) |
| 473 | * Where for 256-QAM, c = log10(696320) * 2^24, and so on. |
| 474 | */ |
Trent Piepho | 19be685 | 2006-10-29 13:35:39 -0300 | [diff] [blame] | 475 | static u32 calculate_snr(u32 mse, u32 c) |
Michael Krufky | 1963c90 | 2005-08-08 09:22:43 -0700 | [diff] [blame] | 476 | { |
Trent Piepho | 19be685 | 2006-10-29 13:35:39 -0300 | [diff] [blame] | 477 | if (mse == 0) /* No signal */ |
| 478 | return 0; |
| 479 | |
| 480 | mse = intlog10(mse); |
| 481 | if (mse > c) { |
Mauro Carvalho Chehab | 467845a | 2018-03-09 10:53:27 -0500 | [diff] [blame] | 482 | /* |
| 483 | * Negative SNR, which is possible, but realisticly the |
| 484 | * demod will lose lock before the signal gets this bad. |
| 485 | * The API only allows for unsigned values, so just return 0 |
| 486 | */ |
Trent Piepho | 19be685 | 2006-10-29 13:35:39 -0300 | [diff] [blame] | 487 | return 0; |
| 488 | } |
Mauro Carvalho Chehab | 467845a | 2018-03-09 10:53:27 -0500 | [diff] [blame] | 489 | return 10 * (c - mse); |
Michael Krufky | 1963c90 | 2005-08-08 09:22:43 -0700 | [diff] [blame] | 490 | } |
| 491 | |
Mauro Carvalho Chehab | 19bdd0d | 2018-03-09 10:53:34 -0500 | [diff] [blame] | 492 | static int lgdt3302_read_snr(struct dvb_frontend *fe) |
Mac Michaels | d8667cb | 2005-07-07 17:58:29 -0700 | [diff] [blame] | 493 | { |
Mauro Carvalho Chehab | 467845a | 2018-03-09 10:53:27 -0500 | [diff] [blame] | 494 | struct lgdt330x_state *state = fe->demodulator_priv; |
Trent Piepho | 19be685 | 2006-10-29 13:35:39 -0300 | [diff] [blame] | 495 | u8 buf[5]; /* read data buffer */ |
| 496 | u32 noise; /* noise value */ |
| 497 | u32 c; /* per-modulation SNR calculation constant */ |
Mac Michaels | d8667cb | 2005-07-07 17:58:29 -0700 | [diff] [blame] | 498 | |
Mauro Carvalho Chehab | 467845a | 2018-03-09 10:53:27 -0500 | [diff] [blame] | 499 | switch (state->current_modulation) { |
Trent Piepho | 19be685 | 2006-10-29 13:35:39 -0300 | [diff] [blame] | 500 | case VSB_8: |
| 501 | i2c_read_demod_bytes(state, LGDT3302_EQPH_ERR0, buf, 5); |
| 502 | #ifdef USE_EQMSE |
| 503 | /* Use Equalizer Mean-Square Error Register */ |
| 504 | /* SNR for ranges from -15.61 to +41.58 */ |
Mac Michaels | d8667cb | 2005-07-07 17:58:29 -0700 | [diff] [blame] | 505 | noise = ((buf[0] & 7) << 16) | (buf[1] << 8) | buf[2]; |
Trent Piepho | 19be685 | 2006-10-29 13:35:39 -0300 | [diff] [blame] | 506 | c = 69765745; /* log10(25*24^2)*2^24 */ |
Mac Michaels | d8667cb | 2005-07-07 17:58:29 -0700 | [diff] [blame] | 507 | #else |
Trent Piepho | 19be685 | 2006-10-29 13:35:39 -0300 | [diff] [blame] | 508 | /* Use Phase Tracker Mean-Square Error Register */ |
| 509 | /* SNR for ranges from -13.11 to +44.08 */ |
Mauro Carvalho Chehab | 467845a | 2018-03-09 10:53:27 -0500 | [diff] [blame] | 510 | noise = ((buf[0] & 7 << 3) << 13) | (buf[3] << 8) | buf[4]; |
Trent Piepho | 19be685 | 2006-10-29 13:35:39 -0300 | [diff] [blame] | 511 | c = 73957994; /* log10(25*32^2)*2^24 */ |
| 512 | #endif |
| 513 | break; |
| 514 | case QAM_64: |
| 515 | case QAM_256: |
| 516 | i2c_read_demod_bytes(state, CARRIER_MSEQAM1, buf, 2); |
Michael Krufky | 1963c90 | 2005-08-08 09:22:43 -0700 | [diff] [blame] | 517 | noise = ((buf[0] & 3) << 8) | buf[1]; |
Trent Piepho | 19be685 | 2006-10-29 13:35:39 -0300 | [diff] [blame] | 518 | c = state->current_modulation == QAM_64 ? 97939837 : 98026066; |
| 519 | /* log10(688128)*2^24 and log10(696320)*2^24 */ |
| 520 | break; |
| 521 | default: |
Mauro Carvalho Chehab | 23ba635 | 2018-03-09 10:53:30 -0500 | [diff] [blame] | 522 | dev_err(&state->client->dev, |
| 523 | "%s: Modulation set to unsupported value\n", |
| 524 | __func__); |
Mauro Carvalho Chehab | 19bdd0d | 2018-03-09 10:53:34 -0500 | [diff] [blame] | 525 | |
| 526 | state->snr = 0; |
| 527 | |
Trent Piepho | 19be685 | 2006-10-29 13:35:39 -0300 | [diff] [blame] | 528 | return -EREMOTEIO; /* return -EDRIVER_IS_GIBBERED; */ |
Mac Michaels | d8667cb | 2005-07-07 17:58:29 -0700 | [diff] [blame] | 529 | } |
| 530 | |
Trent Piepho | 19be685 | 2006-10-29 13:35:39 -0300 | [diff] [blame] | 531 | state->snr = calculate_snr(noise, c); |
Mac Michaels | d8667cb | 2005-07-07 17:58:29 -0700 | [diff] [blame] | 532 | |
Mauro Carvalho Chehab | 23ba635 | 2018-03-09 10:53:30 -0500 | [diff] [blame] | 533 | dprintk(state, "noise = 0x%08x, snr = %d.%02d dB\n", noise, |
Mauro Carvalho Chehab | 467845a | 2018-03-09 10:53:27 -0500 | [diff] [blame] | 534 | state->snr >> 24, (((state->snr >> 8) & 0xffff) * 100) >> 16); |
Mac Michaels | d8667cb | 2005-07-07 17:58:29 -0700 | [diff] [blame] | 535 | |
| 536 | return 0; |
| 537 | } |
| 538 | |
Mauro Carvalho Chehab | 19bdd0d | 2018-03-09 10:53:34 -0500 | [diff] [blame] | 539 | static int lgdt3303_read_snr(struct dvb_frontend *fe) |
Michael Krufky | 1963c90 | 2005-08-08 09:22:43 -0700 | [diff] [blame] | 540 | { |
Mauro Carvalho Chehab | 467845a | 2018-03-09 10:53:27 -0500 | [diff] [blame] | 541 | struct lgdt330x_state *state = fe->demodulator_priv; |
Trent Piepho | 19be685 | 2006-10-29 13:35:39 -0300 | [diff] [blame] | 542 | u8 buf[5]; /* read data buffer */ |
| 543 | u32 noise; /* noise value */ |
| 544 | u32 c; /* per-modulation SNR calculation constant */ |
Michael Krufky | 1963c90 | 2005-08-08 09:22:43 -0700 | [diff] [blame] | 545 | |
Mauro Carvalho Chehab | 467845a | 2018-03-09 10:53:27 -0500 | [diff] [blame] | 546 | switch (state->current_modulation) { |
Trent Piepho | 19be685 | 2006-10-29 13:35:39 -0300 | [diff] [blame] | 547 | case VSB_8: |
| 548 | i2c_read_demod_bytes(state, LGDT3303_EQPH_ERR0, buf, 5); |
| 549 | #ifdef USE_EQMSE |
| 550 | /* Use Equalizer Mean-Square Error Register */ |
| 551 | /* SNR for ranges from -16.12 to +44.08 */ |
| 552 | noise = ((buf[0] & 0x78) << 13) | (buf[1] << 8) | buf[2]; |
| 553 | c = 73957994; /* log10(25*32^2)*2^24 */ |
| 554 | #else |
| 555 | /* Use Phase Tracker Mean-Square Error Register */ |
| 556 | /* SNR for ranges from -13.11 to +44.08 */ |
Michael Krufky | 1963c90 | 2005-08-08 09:22:43 -0700 | [diff] [blame] | 557 | noise = ((buf[0] & 7) << 16) | (buf[3] << 8) | buf[4]; |
Trent Piepho | 19be685 | 2006-10-29 13:35:39 -0300 | [diff] [blame] | 558 | c = 73957994; /* log10(25*32^2)*2^24 */ |
| 559 | #endif |
| 560 | break; |
| 561 | case QAM_64: |
| 562 | case QAM_256: |
| 563 | i2c_read_demod_bytes(state, CARRIER_MSEQAM1, buf, 2); |
Michael Krufky | 1963c90 | 2005-08-08 09:22:43 -0700 | [diff] [blame] | 564 | noise = (buf[0] << 8) | buf[1]; |
Trent Piepho | 19be685 | 2006-10-29 13:35:39 -0300 | [diff] [blame] | 565 | c = state->current_modulation == QAM_64 ? 97939837 : 98026066; |
| 566 | /* log10(688128)*2^24 and log10(696320)*2^24 */ |
| 567 | break; |
| 568 | default: |
Mauro Carvalho Chehab | 23ba635 | 2018-03-09 10:53:30 -0500 | [diff] [blame] | 569 | dev_err(&state->client->dev, |
| 570 | "%s: Modulation set to unsupported value\n", |
| 571 | __func__); |
Mauro Carvalho Chehab | 19bdd0d | 2018-03-09 10:53:34 -0500 | [diff] [blame] | 572 | state->snr = 0; |
Trent Piepho | 19be685 | 2006-10-29 13:35:39 -0300 | [diff] [blame] | 573 | return -EREMOTEIO; /* return -EDRIVER_IS_GIBBERED; */ |
Michael Krufky | 1963c90 | 2005-08-08 09:22:43 -0700 | [diff] [blame] | 574 | } |
| 575 | |
Trent Piepho | 19be685 | 2006-10-29 13:35:39 -0300 | [diff] [blame] | 576 | state->snr = calculate_snr(noise, c); |
Michael Krufky | 1963c90 | 2005-08-08 09:22:43 -0700 | [diff] [blame] | 577 | |
Mauro Carvalho Chehab | 23ba635 | 2018-03-09 10:53:30 -0500 | [diff] [blame] | 578 | dprintk(state, "noise = 0x%08x, snr = %d.%02d dB\n", noise, |
Trent Piepho | 19be685 | 2006-10-29 13:35:39 -0300 | [diff] [blame] | 579 | state->snr >> 24, (((state->snr >> 8) & 0xffff) * 100) >> 16); |
| 580 | |
| 581 | return 0; |
| 582 | } |
| 583 | |
Mauro Carvalho Chehab | 19bdd0d | 2018-03-09 10:53:34 -0500 | [diff] [blame] | 584 | static int lgdt330x_read_snr(struct dvb_frontend *fe, u16 *snr) |
| 585 | { |
| 586 | struct lgdt330x_state *state = fe->demodulator_priv; |
| 587 | |
| 588 | *snr = (state->snr) >> 16; /* Convert from 8.24 fixed-point to 8.8 */ |
| 589 | |
| 590 | return 0; |
| 591 | } |
| 592 | |
Mauro Carvalho Chehab | 467845a | 2018-03-09 10:53:27 -0500 | [diff] [blame] | 593 | static int lgdt330x_read_signal_strength(struct dvb_frontend *fe, u16 *strength) |
Trent Piepho | 19be685 | 2006-10-29 13:35:39 -0300 | [diff] [blame] | 594 | { |
| 595 | /* Calculate Strength from SNR up to 35dB */ |
Mauro Carvalho Chehab | 467845a | 2018-03-09 10:53:27 -0500 | [diff] [blame] | 596 | /* |
| 597 | * Even though the SNR can go higher than 35dB, there is some comfort |
| 598 | * factor in having a range of strong signals that can show at 100% |
| 599 | */ |
| 600 | struct lgdt330x_state *state = fe->demodulator_priv; |
Trent Piepho | 19be685 | 2006-10-29 13:35:39 -0300 | [diff] [blame] | 601 | u16 snr; |
| 602 | int ret; |
| 603 | |
| 604 | ret = fe->ops.read_snr(fe, &snr); |
| 605 | if (ret != 0) |
| 606 | return ret; |
| 607 | /* Rather than use the 8.8 value snr, use state->snr which is 8.24 */ |
| 608 | /* scale the range 0 - 35*2^24 into 0 - 65535 */ |
| 609 | if (state->snr >= 8960 * 0x10000) |
| 610 | *strength = 0xffff; |
| 611 | else |
| 612 | *strength = state->snr / 8960; |
Michael Krufky | 1963c90 | 2005-08-08 09:22:43 -0700 | [diff] [blame] | 613 | |
| 614 | return 0; |
| 615 | } |
| 616 | |
Mauro Carvalho Chehab | 53d4172 | 2018-03-09 10:53:33 -0500 | [diff] [blame] | 617 | |
| 618 | static int lgdt3302_read_status(struct dvb_frontend *fe, |
| 619 | enum fe_status *status) |
| 620 | { |
| 621 | struct lgdt330x_state *state = fe->demodulator_priv; |
Mauro Carvalho Chehab | 19bdd0d | 2018-03-09 10:53:34 -0500 | [diff] [blame] | 622 | struct dtv_frontend_properties *p = &fe->dtv_property_cache; |
Mauro Carvalho Chehab | 53d4172 | 2018-03-09 10:53:33 -0500 | [diff] [blame] | 623 | u8 buf[3]; |
Mauro Carvalho Chehab | 5f939c3 | 2018-03-09 11:15:41 -0500 | [diff] [blame] | 624 | int err; |
Mauro Carvalho Chehab | 53d4172 | 2018-03-09 10:53:33 -0500 | [diff] [blame] | 625 | |
| 626 | *status = 0; /* Reset status result */ |
| 627 | |
| 628 | /* AGC status register */ |
| 629 | i2c_read_demod_bytes(state, AGC_STATUS, buf, 1); |
| 630 | dprintk(state, "AGC_STATUS = 0x%02x\n", buf[0]); |
| 631 | if ((buf[0] & 0x0c) == 0x8) { |
| 632 | /* |
| 633 | * Test signal does not exist flag |
| 634 | * as well as the AGC lock flag. |
| 635 | */ |
| 636 | *status |= FE_HAS_SIGNAL; |
| 637 | } |
| 638 | |
| 639 | /* |
| 640 | * You must set the Mask bits to 1 in the IRQ_MASK in order |
| 641 | * to see that status bit in the IRQ_STATUS register. |
| 642 | * This is done in SwReset(); |
| 643 | */ |
| 644 | |
| 645 | /* signal status */ |
| 646 | i2c_read_demod_bytes(state, TOP_CONTROL, buf, sizeof(buf)); |
| 647 | dprintk(state, |
| 648 | "TOP_CONTROL = 0x%02x, IRO_MASK = 0x%02x, IRQ_STATUS = 0x%02x\n", |
| 649 | buf[0], buf[1], buf[2]); |
| 650 | |
| 651 | /* sync status */ |
| 652 | if ((buf[2] & 0x03) == 0x01) |
| 653 | *status |= FE_HAS_SYNC; |
| 654 | |
| 655 | /* FEC error status */ |
| 656 | if ((buf[2] & 0x0c) == 0x08) |
| 657 | *status |= FE_HAS_LOCK | FE_HAS_VITERBI; |
| 658 | |
| 659 | /* Carrier Recovery Lock Status Register */ |
| 660 | i2c_read_demod_bytes(state, CARRIER_LOCK, buf, 1); |
| 661 | dprintk(state, "CARRIER_LOCK = 0x%02x\n", buf[0]); |
| 662 | switch (state->current_modulation) { |
| 663 | case QAM_256: |
| 664 | case QAM_64: |
| 665 | /* Need to understand why there are 3 lock levels here */ |
| 666 | if ((buf[0] & 0x07) == 0x07) |
| 667 | *status |= FE_HAS_CARRIER; |
| 668 | break; |
| 669 | case VSB_8: |
| 670 | if ((buf[0] & 0x80) == 0x80) |
| 671 | *status |= FE_HAS_CARRIER; |
| 672 | break; |
| 673 | default: |
| 674 | dev_warn(&state->client->dev, |
| 675 | "%s: Modulation set to unsupported value\n", |
| 676 | __func__); |
| 677 | } |
| 678 | |
Mauro Carvalho Chehab | 5f939c3 | 2018-03-09 11:15:41 -0500 | [diff] [blame] | 679 | if (!(*status & FE_HAS_LOCK)) { |
| 680 | p->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE; |
| 681 | p->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE; |
| 682 | p->block_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE; |
| 683 | return 0; |
| 684 | } |
| 685 | |
| 686 | if (state->last_stats_time && |
| 687 | time_is_after_jiffies(state->last_stats_time)) |
| 688 | return 0; |
| 689 | |
| 690 | state->last_stats_time = jiffies + msecs_to_jiffies(1000); |
| 691 | |
| 692 | err = lgdt3302_read_snr(fe); |
| 693 | if (!err) { |
Mauro Carvalho Chehab | 19bdd0d | 2018-03-09 10:53:34 -0500 | [diff] [blame] | 694 | p->cnr.stat[0].scale = FE_SCALE_DECIBEL; |
| 695 | p->cnr.stat[0].svalue = (((u64)state->snr) * 1000) >> 24; |
| 696 | } else { |
| 697 | p->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE; |
| 698 | } |
| 699 | |
Mauro Carvalho Chehab | 5f939c3 | 2018-03-09 11:15:41 -0500 | [diff] [blame] | 700 | err = i2c_read_demod_bytes(state, LGDT3302_PACKET_ERR_COUNTER1, |
| 701 | buf, sizeof(buf)); |
| 702 | if (!err) { |
| 703 | state->ucblocks = (buf[0] << 8) | buf[1]; |
| 704 | |
| 705 | dprintk(state, "UCB = 0x%02x\n", state->ucblocks); |
| 706 | |
| 707 | p->block_error.stat[0].uvalue += state->ucblocks; |
| 708 | /* FIXME: what's the basis for block count */ |
| 709 | p->block_count.stat[0].uvalue += 10000; |
| 710 | |
| 711 | p->block_error.stat[0].scale = FE_SCALE_COUNTER; |
| 712 | p->block_count.stat[0].scale = FE_SCALE_COUNTER; |
| 713 | } else { |
| 714 | p->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE; |
| 715 | p->block_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE; |
| 716 | } |
| 717 | |
Mauro Carvalho Chehab | 53d4172 | 2018-03-09 10:53:33 -0500 | [diff] [blame] | 718 | return 0; |
| 719 | } |
| 720 | |
| 721 | static int lgdt3303_read_status(struct dvb_frontend *fe, |
| 722 | enum fe_status *status) |
| 723 | { |
| 724 | struct lgdt330x_state *state = fe->demodulator_priv; |
Mauro Carvalho Chehab | 19bdd0d | 2018-03-09 10:53:34 -0500 | [diff] [blame] | 725 | struct dtv_frontend_properties *p = &fe->dtv_property_cache; |
Mauro Carvalho Chehab | 53d4172 | 2018-03-09 10:53:33 -0500 | [diff] [blame] | 726 | u8 buf[3]; |
Mauro Carvalho Chehab | 5f939c3 | 2018-03-09 11:15:41 -0500 | [diff] [blame] | 727 | int err; |
Mauro Carvalho Chehab | 53d4172 | 2018-03-09 10:53:33 -0500 | [diff] [blame] | 728 | |
| 729 | *status = 0; /* Reset status result */ |
| 730 | |
| 731 | /* lgdt3303 AGC status register */ |
| 732 | err = i2c_read_demod_bytes(state, 0x58, buf, 1); |
| 733 | if (err < 0) |
| 734 | return err; |
| 735 | |
| 736 | dprintk(state, "AGC_STATUS = 0x%02x\n", buf[0]); |
| 737 | if ((buf[0] & 0x21) == 0x01) { |
| 738 | /* |
| 739 | * Test input signal does not exist flag |
| 740 | * as well as the AGC lock flag. |
| 741 | */ |
| 742 | *status |= FE_HAS_SIGNAL; |
| 743 | } |
| 744 | |
| 745 | /* Carrier Recovery Lock Status Register */ |
| 746 | i2c_read_demod_bytes(state, CARRIER_LOCK, buf, 1); |
| 747 | dprintk(state, "CARRIER_LOCK = 0x%02x\n", buf[0]); |
| 748 | switch (state->current_modulation) { |
| 749 | case QAM_256: |
| 750 | case QAM_64: |
| 751 | /* Need to understand why there are 3 lock levels here */ |
| 752 | if ((buf[0] & 0x07) == 0x07) |
| 753 | *status |= FE_HAS_CARRIER; |
| 754 | else |
| 755 | break; |
| 756 | i2c_read_demod_bytes(state, 0x8a, buf, 1); |
| 757 | dprintk(state, "QAM LOCK = 0x%02x\n", buf[0]); |
| 758 | |
| 759 | if ((buf[0] & 0x04) == 0x04) |
| 760 | *status |= FE_HAS_SYNC; |
| 761 | if ((buf[0] & 0x01) == 0x01) |
| 762 | *status |= FE_HAS_LOCK; |
| 763 | if ((buf[0] & 0x08) == 0x08) |
| 764 | *status |= FE_HAS_VITERBI; |
| 765 | break; |
| 766 | case VSB_8: |
| 767 | if ((buf[0] & 0x80) == 0x80) |
| 768 | *status |= FE_HAS_CARRIER; |
| 769 | else |
| 770 | break; |
| 771 | i2c_read_demod_bytes(state, 0x38, buf, 1); |
| 772 | dprintk(state, "8-VSB LOCK = 0x%02x\n", buf[0]); |
| 773 | |
| 774 | if ((buf[0] & 0x02) == 0x00) |
| 775 | *status |= FE_HAS_SYNC; |
French, Nicholas A | 1b4fd9d | 2018-12-09 02:11:18 -0500 | [diff] [blame] | 776 | if ((buf[0] & 0x01) == 0x01) |
Mauro Carvalho Chehab | 53d4172 | 2018-03-09 10:53:33 -0500 | [diff] [blame] | 777 | *status |= FE_HAS_VITERBI | FE_HAS_LOCK; |
| 778 | break; |
| 779 | default: |
| 780 | dev_warn(&state->client->dev, |
| 781 | "%s: Modulation set to unsupported value\n", |
| 782 | __func__); |
| 783 | } |
Mauro Carvalho Chehab | 19bdd0d | 2018-03-09 10:53:34 -0500 | [diff] [blame] | 784 | |
Mauro Carvalho Chehab | 5f939c3 | 2018-03-09 11:15:41 -0500 | [diff] [blame] | 785 | if (!(*status & FE_HAS_LOCK)) { |
| 786 | p->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE; |
| 787 | p->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE; |
| 788 | p->block_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE; |
| 789 | return 0; |
| 790 | } |
| 791 | |
| 792 | if (state->last_stats_time && |
| 793 | time_is_after_jiffies(state->last_stats_time)) |
| 794 | return 0; |
| 795 | |
| 796 | state->last_stats_time = jiffies + msecs_to_jiffies(1000); |
| 797 | |
| 798 | err = lgdt3303_read_snr(fe); |
| 799 | if (!err) { |
Mauro Carvalho Chehab | 19bdd0d | 2018-03-09 10:53:34 -0500 | [diff] [blame] | 800 | p->cnr.stat[0].scale = FE_SCALE_DECIBEL; |
| 801 | p->cnr.stat[0].svalue = (((u64)state->snr) * 1000) >> 24; |
| 802 | } else { |
| 803 | p->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE; |
| 804 | } |
| 805 | |
Mauro Carvalho Chehab | 5f939c3 | 2018-03-09 11:15:41 -0500 | [diff] [blame] | 806 | err = i2c_read_demod_bytes(state, LGDT3303_PACKET_ERR_COUNTER1, |
| 807 | buf, sizeof(buf)); |
| 808 | if (!err) { |
| 809 | state->ucblocks = (buf[0] << 8) | buf[1]; |
| 810 | |
| 811 | dprintk(state, "UCB = 0x%02x\n", state->ucblocks); |
| 812 | |
| 813 | p->block_error.stat[0].uvalue += state->ucblocks; |
| 814 | /* FIXME: what's the basis for block count */ |
| 815 | p->block_count.stat[0].uvalue += 10000; |
| 816 | |
| 817 | p->block_error.stat[0].scale = FE_SCALE_COUNTER; |
| 818 | p->block_count.stat[0].scale = FE_SCALE_COUNTER; |
| 819 | } else { |
| 820 | p->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE; |
| 821 | p->block_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE; |
| 822 | } |
| 823 | |
Mauro Carvalho Chehab | 53d4172 | 2018-03-09 10:53:33 -0500 | [diff] [blame] | 824 | return 0; |
| 825 | } |
| 826 | |
Mauro Carvalho Chehab | 467845a | 2018-03-09 10:53:27 -0500 | [diff] [blame] | 827 | static int |
| 828 | lgdt330x_get_tune_settings(struct dvb_frontend *fe, |
| 829 | struct dvb_frontend_tune_settings *fe_tune_settings) |
Mac Michaels | d8667cb | 2005-07-07 17:58:29 -0700 | [diff] [blame] | 830 | { |
| 831 | /* I have no idea about this - it may not be needed */ |
| 832 | fe_tune_settings->min_delay_ms = 500; |
| 833 | fe_tune_settings->step_size = 0; |
| 834 | fe_tune_settings->max_drift = 0; |
| 835 | return 0; |
| 836 | } |
| 837 | |
Mauro Carvalho Chehab | 467845a | 2018-03-09 10:53:27 -0500 | [diff] [blame] | 838 | static void lgdt330x_release(struct dvb_frontend *fe) |
Mac Michaels | d8667cb | 2005-07-07 17:58:29 -0700 | [diff] [blame] | 839 | { |
Mauro Carvalho Chehab | 467845a | 2018-03-09 10:53:27 -0500 | [diff] [blame] | 840 | struct lgdt330x_state *state = fe->demodulator_priv; |
Mauro Carvalho Chehab | 23ba635 | 2018-03-09 10:53:30 -0500 | [diff] [blame] | 841 | struct i2c_client *client = state->client; |
Mauro Carvalho Chehab | 467845a | 2018-03-09 10:53:27 -0500 | [diff] [blame] | 842 | |
Mauro Carvalho Chehab | 23ba635 | 2018-03-09 10:53:30 -0500 | [diff] [blame] | 843 | dev_dbg(&client->dev, "\n"); |
| 844 | |
| 845 | i2c_unregister_device(client); |
| 846 | } |
| 847 | |
| 848 | static struct dvb_frontend *lgdt330x_get_dvb_frontend(struct i2c_client *client) |
| 849 | { |
| 850 | struct lgdt330x_state *state = i2c_get_clientdata(client); |
| 851 | |
| 852 | dev_dbg(&client->dev, "\n"); |
| 853 | |
| 854 | return &state->frontend; |
Mac Michaels | d8667cb | 2005-07-07 17:58:29 -0700 | [diff] [blame] | 855 | } |
| 856 | |
Max Kellermann | bd336e6 | 2016-08-09 18:32:21 -0300 | [diff] [blame] | 857 | static const struct dvb_frontend_ops lgdt3302_ops; |
| 858 | static const struct dvb_frontend_ops lgdt3303_ops; |
Mac Michaels | d8667cb | 2005-07-07 17:58:29 -0700 | [diff] [blame] | 859 | |
Mauro Carvalho Chehab | 23ba635 | 2018-03-09 10:53:30 -0500 | [diff] [blame] | 860 | static int lgdt330x_probe(struct i2c_client *client, |
| 861 | const struct i2c_device_id *id) |
Mac Michaels | d8667cb | 2005-07-07 17:58:29 -0700 | [diff] [blame] | 862 | { |
Mauro Carvalho Chehab | 467845a | 2018-03-09 10:53:27 -0500 | [diff] [blame] | 863 | struct lgdt330x_state *state = NULL; |
Mac Michaels | d8667cb | 2005-07-07 17:58:29 -0700 | [diff] [blame] | 864 | u8 buf[1]; |
| 865 | |
| 866 | /* Allocate memory for the internal state */ |
Mauro Carvalho Chehab | 467845a | 2018-03-09 10:53:27 -0500 | [diff] [blame] | 867 | state = kzalloc(sizeof(*state), GFP_KERNEL); |
| 868 | if (!state) |
Mac Michaels | d8667cb | 2005-07-07 17:58:29 -0700 | [diff] [blame] | 869 | goto error; |
Mac Michaels | d8667cb | 2005-07-07 17:58:29 -0700 | [diff] [blame] | 870 | |
| 871 | /* Setup the state */ |
Mauro Carvalho Chehab | 23ba635 | 2018-03-09 10:53:30 -0500 | [diff] [blame] | 872 | memcpy(&state->config, client->dev.platform_data, |
| 873 | sizeof(state->config)); |
| 874 | i2c_set_clientdata(client, state); |
| 875 | state->client = client; |
Patrick Boettcher | dea7486 | 2006-05-14 05:01:31 -0300 | [diff] [blame] | 876 | |
| 877 | /* Create dvb_frontend */ |
Mauro Carvalho Chehab | 23ba635 | 2018-03-09 10:53:30 -0500 | [diff] [blame] | 878 | switch (state->config.demod_chip) { |
Michael Krufky | 1963c90 | 2005-08-08 09:22:43 -0700 | [diff] [blame] | 879 | case LGDT3302: |
Mauro Carvalho Chehab | 467845a | 2018-03-09 10:53:27 -0500 | [diff] [blame] | 880 | memcpy(&state->frontend.ops, &lgdt3302_ops, |
| 881 | sizeof(struct dvb_frontend_ops)); |
Michael Krufky | 1963c90 | 2005-08-08 09:22:43 -0700 | [diff] [blame] | 882 | break; |
| 883 | case LGDT3303: |
Mauro Carvalho Chehab | 467845a | 2018-03-09 10:53:27 -0500 | [diff] [blame] | 884 | memcpy(&state->frontend.ops, &lgdt3303_ops, |
| 885 | sizeof(struct dvb_frontend_ops)); |
Michael Krufky | 1963c90 | 2005-08-08 09:22:43 -0700 | [diff] [blame] | 886 | break; |
| 887 | default: |
| 888 | goto error; |
| 889 | } |
Patrick Boettcher | dea7486 | 2006-05-14 05:01:31 -0300 | [diff] [blame] | 890 | state->frontend.demodulator_priv = state; |
Michael Krufky | 1963c90 | 2005-08-08 09:22:43 -0700 | [diff] [blame] | 891 | |
Mauro Carvalho Chehab | 23ba635 | 2018-03-09 10:53:30 -0500 | [diff] [blame] | 892 | /* Setup get frontend callback */ |
| 893 | state->config.get_dvb_frontend = lgdt330x_get_dvb_frontend; |
| 894 | |
Mac Michaels | d8667cb | 2005-07-07 17:58:29 -0700 | [diff] [blame] | 895 | /* Verify communication with demod chip */ |
Michael Krufky | 1963c90 | 2005-08-08 09:22:43 -0700 | [diff] [blame] | 896 | if (i2c_read_demod_bytes(state, 2, buf, 1)) |
Mac Michaels | d8667cb | 2005-07-07 17:58:29 -0700 | [diff] [blame] | 897 | goto error; |
| 898 | |
| 899 | state->current_frequency = -1; |
| 900 | state->current_modulation = -1; |
| 901 | |
Mauro Carvalho Chehab | 23ba635 | 2018-03-09 10:53:30 -0500 | [diff] [blame] | 902 | dev_info(&state->client->dev, |
| 903 | "Demod loaded for LGDT330%s chip\n", |
| 904 | state->config.demod_chip == LGDT3302 ? "2" : "3"); |
Mauro Carvalho Chehab | ff09361 | 2018-03-09 10:53:29 -0500 | [diff] [blame] | 905 | |
Mauro Carvalho Chehab | 23ba635 | 2018-03-09 10:53:30 -0500 | [diff] [blame] | 906 | return 0; |
Mac Michaels | d8667cb | 2005-07-07 17:58:29 -0700 | [diff] [blame] | 907 | |
| 908 | error: |
Jesper Juhl | 2ea7533 | 2005-11-07 01:01:31 -0800 | [diff] [blame] | 909 | kfree(state); |
Mauro Carvalho Chehab | 89eaaf2 | 2018-05-04 11:16:49 -0400 | [diff] [blame] | 910 | if (debug) |
| 911 | dev_printk(KERN_DEBUG, &client->dev, "Error loading lgdt330x driver\n"); |
Mauro Carvalho Chehab | 23ba635 | 2018-03-09 10:53:30 -0500 | [diff] [blame] | 912 | return -ENODEV; |
| 913 | } |
| 914 | struct dvb_frontend *lgdt330x_attach(const struct lgdt330x_config *_config, |
| 915 | u8 demod_address, |
| 916 | struct i2c_adapter *i2c) |
| 917 | { |
| 918 | struct i2c_client *client; |
| 919 | struct i2c_board_info board_info = {}; |
| 920 | struct lgdt330x_config config = *_config; |
| 921 | |
Mauro Carvalho Chehab | c0decac | 2018-09-10 08:19:14 -0400 | [diff] [blame] | 922 | strscpy(board_info.type, "lgdt330x", sizeof(board_info.type)); |
Mauro Carvalho Chehab | 23ba635 | 2018-03-09 10:53:30 -0500 | [diff] [blame] | 923 | board_info.addr = demod_address; |
| 924 | board_info.platform_data = &config; |
Wolfram Sang | 4f7f5e0 | 2019-12-16 16:51:30 +0100 | [diff] [blame] | 925 | client = i2c_new_client_device(i2c, &board_info); |
| 926 | if (!i2c_client_has_driver(client)) |
Mauro Carvalho Chehab | 23ba635 | 2018-03-09 10:53:30 -0500 | [diff] [blame] | 927 | return NULL; |
| 928 | |
| 929 | return lgdt330x_get_dvb_frontend(client); |
Mac Michaels | d8667cb | 2005-07-07 17:58:29 -0700 | [diff] [blame] | 930 | } |
Mauro Carvalho Chehab | 467845a | 2018-03-09 10:53:27 -0500 | [diff] [blame] | 931 | EXPORT_SYMBOL(lgdt330x_attach); |
Mac Michaels | d8667cb | 2005-07-07 17:58:29 -0700 | [diff] [blame] | 932 | |
Max Kellermann | bd336e6 | 2016-08-09 18:32:21 -0300 | [diff] [blame] | 933 | static const struct dvb_frontend_ops lgdt3302_ops = { |
Mauro Carvalho Chehab | ca7072d | 2011-12-26 11:25:21 -0300 | [diff] [blame] | 934 | .delsys = { SYS_ATSC, SYS_DVBC_ANNEX_B }, |
Mac Michaels | d8667cb | 2005-07-07 17:58:29 -0700 | [diff] [blame] | 935 | .info = { |
Mauro Carvalho Chehab | 467845a | 2018-03-09 10:53:27 -0500 | [diff] [blame] | 936 | .name = "LG Electronics LGDT3302 VSB/QAM Frontend", |
Mauro Carvalho Chehab | f1b1eab | 2018-07-05 18:59:36 -0400 | [diff] [blame] | 937 | .frequency_min_hz = 54 * MHz, |
| 938 | .frequency_max_hz = 858 * MHz, |
| 939 | .frequency_stepsize_hz = 62500, |
Michael Krufky | 66944e9 | 2005-11-08 21:35:55 -0800 | [diff] [blame] | 940 | .symbol_rate_min = 5056941, /* QAM 64 */ |
| 941 | .symbol_rate_max = 10762000, /* VSB 8 */ |
Mac Michaels | d8667cb | 2005-07-07 17:58:29 -0700 | [diff] [blame] | 942 | .caps = FE_CAN_QAM_64 | FE_CAN_QAM_256 | FE_CAN_8VSB |
| 943 | }, |
Michael Krufky | 6ddcc91 | 2005-07-27 11:46:00 -0700 | [diff] [blame] | 944 | .init = lgdt330x_init, |
Mauro Carvalho Chehab | ca7072d | 2011-12-26 11:25:21 -0300 | [diff] [blame] | 945 | .set_frontend = lgdt330x_set_parameters, |
| 946 | .get_frontend = lgdt330x_get_frontend, |
Michael Krufky | 6ddcc91 | 2005-07-27 11:46:00 -0700 | [diff] [blame] | 947 | .get_tune_settings = lgdt330x_get_tune_settings, |
Michael Krufky | 1963c90 | 2005-08-08 09:22:43 -0700 | [diff] [blame] | 948 | .read_status = lgdt3302_read_status, |
Michael Krufky | 6ddcc91 | 2005-07-27 11:46:00 -0700 | [diff] [blame] | 949 | .read_signal_strength = lgdt330x_read_signal_strength, |
Mauro Carvalho Chehab | 19bdd0d | 2018-03-09 10:53:34 -0500 | [diff] [blame] | 950 | .read_snr = lgdt330x_read_snr, |
Michael Krufky | 6ddcc91 | 2005-07-27 11:46:00 -0700 | [diff] [blame] | 951 | .read_ucblocks = lgdt330x_read_ucblocks, |
| 952 | .release = lgdt330x_release, |
Mac Michaels | d8667cb | 2005-07-07 17:58:29 -0700 | [diff] [blame] | 953 | }; |
| 954 | |
Max Kellermann | bd336e6 | 2016-08-09 18:32:21 -0300 | [diff] [blame] | 955 | static const struct dvb_frontend_ops lgdt3303_ops = { |
Mauro Carvalho Chehab | ca7072d | 2011-12-26 11:25:21 -0300 | [diff] [blame] | 956 | .delsys = { SYS_ATSC, SYS_DVBC_ANNEX_B }, |
Michael Krufky | 1963c90 | 2005-08-08 09:22:43 -0700 | [diff] [blame] | 957 | .info = { |
Mauro Carvalho Chehab | 467845a | 2018-03-09 10:53:27 -0500 | [diff] [blame] | 958 | .name = "LG Electronics LGDT3303 VSB/QAM Frontend", |
Mauro Carvalho Chehab | f1b1eab | 2018-07-05 18:59:36 -0400 | [diff] [blame] | 959 | .frequency_min_hz = 54 * MHz, |
| 960 | .frequency_max_hz = 858 * MHz, |
| 961 | .frequency_stepsize_hz = 62500, |
Michael Krufky | 66944e9 | 2005-11-08 21:35:55 -0800 | [diff] [blame] | 962 | .symbol_rate_min = 5056941, /* QAM 64 */ |
| 963 | .symbol_rate_max = 10762000, /* VSB 8 */ |
Michael Krufky | 1963c90 | 2005-08-08 09:22:43 -0700 | [diff] [blame] | 964 | .caps = FE_CAN_QAM_64 | FE_CAN_QAM_256 | FE_CAN_8VSB |
| 965 | }, |
| 966 | .init = lgdt330x_init, |
Mauro Carvalho Chehab | ca7072d | 2011-12-26 11:25:21 -0300 | [diff] [blame] | 967 | .set_frontend = lgdt330x_set_parameters, |
| 968 | .get_frontend = lgdt330x_get_frontend, |
Michael Krufky | 1963c90 | 2005-08-08 09:22:43 -0700 | [diff] [blame] | 969 | .get_tune_settings = lgdt330x_get_tune_settings, |
| 970 | .read_status = lgdt3303_read_status, |
Michael Krufky | 1963c90 | 2005-08-08 09:22:43 -0700 | [diff] [blame] | 971 | .read_signal_strength = lgdt330x_read_signal_strength, |
Mauro Carvalho Chehab | 19bdd0d | 2018-03-09 10:53:34 -0500 | [diff] [blame] | 972 | .read_snr = lgdt330x_read_snr, |
Michael Krufky | 1963c90 | 2005-08-08 09:22:43 -0700 | [diff] [blame] | 973 | .read_ucblocks = lgdt330x_read_ucblocks, |
| 974 | .release = lgdt330x_release, |
| 975 | }; |
| 976 | |
Mauro Carvalho Chehab | 23ba635 | 2018-03-09 10:53:30 -0500 | [diff] [blame] | 977 | static int lgdt330x_remove(struct i2c_client *client) |
| 978 | { |
| 979 | struct lgdt330x_state *state = i2c_get_clientdata(client); |
| 980 | |
| 981 | dev_dbg(&client->dev, "\n"); |
| 982 | |
| 983 | kfree(state); |
| 984 | |
| 985 | return 0; |
| 986 | } |
| 987 | |
| 988 | static const struct i2c_device_id lgdt330x_id_table[] = { |
| 989 | {"lgdt330x", 0}, |
| 990 | {} |
| 991 | }; |
| 992 | MODULE_DEVICE_TABLE(i2c, lgdt330x_id_table); |
| 993 | |
| 994 | static struct i2c_driver lgdt330x_driver = { |
| 995 | .driver = { |
| 996 | .name = "lgdt330x", |
| 997 | .suppress_bind_attrs = true, |
| 998 | }, |
| 999 | .probe = lgdt330x_probe, |
| 1000 | .remove = lgdt330x_remove, |
| 1001 | .id_table = lgdt330x_id_table, |
| 1002 | }; |
| 1003 | |
| 1004 | module_i2c_driver(lgdt330x_driver); |
| 1005 | |
| 1006 | |
Michael Krufky | 1963c90 | 2005-08-08 09:22:43 -0700 | [diff] [blame] | 1007 | MODULE_DESCRIPTION("LGDT330X (ATSC 8VSB & ITU-T J.83 AnnexB 64/256 QAM) Demodulator Driver"); |
Mac Michaels | d8667cb | 2005-07-07 17:58:29 -0700 | [diff] [blame] | 1008 | MODULE_AUTHOR("Wilson Michaels"); |
| 1009 | MODULE_LICENSE("GPL"); |