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