Thomas Gleixner | 74ba920 | 2019-05-20 09:19:02 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Matthias Schwarzott | 6a5cbd5 | 2008-04-12 15:04:49 -0300 | [diff] [blame] | 3 | Driver for Zarlink VP310/MT312/ZL10313 Satellite Channel Decoder |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | |
| 5 | Copyright (C) 2003 Andreas Oberritter <obi@linuxtv.org> |
Matthias Schwarzott | 6a5cbd5 | 2008-04-12 15:04:49 -0300 | [diff] [blame] | 6 | Copyright (C) 2008 Matthias Schwarzott <zzam@gentoo.org> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | |
| 9 | References: |
| 10 | http://products.zarlink.com/product_profiles/MT312.htm |
| 11 | http://products.zarlink.com/product_profiles/SL1935.htm |
| 12 | */ |
| 13 | |
| 14 | #include <linux/delay.h> |
| 15 | #include <linux/errno.h> |
| 16 | #include <linux/init.h> |
| 17 | #include <linux/kernel.h> |
| 18 | #include <linux/module.h> |
Tim Schmielau | 4e57b68 | 2005-10-30 15:03:48 -0800 | [diff] [blame] | 19 | #include <linux/string.h> |
| 20 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
Mauro Carvalho Chehab | fada193 | 2017-12-28 13:03:51 -0500 | [diff] [blame] | 22 | #include <media/dvb_frontend.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include "mt312_priv.h" |
| 24 | #include "mt312.h" |
| 25 | |
Mauro Carvalho Chehab | 8393796 | 2013-11-02 05:05:18 -0300 | [diff] [blame] | 26 | /* Max transfer size done by I2C transfer functions */ |
| 27 | #define MAX_XFER_SIZE 64 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
| 29 | struct mt312_state { |
Matthias Schwarzott | 89f6475 | 2007-12-21 08:56:44 -0300 | [diff] [blame] | 30 | struct i2c_adapter *i2c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | /* configuration settings */ |
Matthias Schwarzott | 89f6475 | 2007-12-21 08:56:44 -0300 | [diff] [blame] | 32 | const struct mt312_config *config; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | struct dvb_frontend frontend; |
| 34 | |
| 35 | u8 id; |
Matthias Schwarzott | 111221f | 2008-04-12 15:04:48 -0300 | [diff] [blame] | 36 | unsigned long xtal; |
| 37 | u8 freq_mult; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | }; |
| 39 | |
| 40 | static int debug; |
| 41 | #define dprintk(args...) \ |
| 42 | do { \ |
Matthias Schwarzott | 89f6475 | 2007-12-21 08:56:44 -0300 | [diff] [blame] | 43 | if (debug) \ |
| 44 | printk(KERN_DEBUG "mt312: " args); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | } while (0) |
| 46 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | #define MT312_PLL_CLK 10000000UL /* 10 MHz */ |
Matthias Schwarzott | 6a5cbd5 | 2008-04-12 15:04:49 -0300 | [diff] [blame] | 48 | #define MT312_PLL_CLK_10_111 10111000UL /* 10.111 MHz */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | |
Matthias Schwarzott | 89f6475 | 2007-12-21 08:56:44 -0300 | [diff] [blame] | 50 | static int mt312_read(struct mt312_state *state, const enum mt312_reg_addr reg, |
Matthias Schwarzott | 1881ee8 | 2008-04-12 15:04:46 -0300 | [diff] [blame] | 51 | u8 *buf, const size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | { |
| 53 | int ret; |
| 54 | struct i2c_msg msg[2]; |
| 55 | u8 regbuf[1] = { reg }; |
| 56 | |
| 57 | msg[0].addr = state->config->demod_address; |
| 58 | msg[0].flags = 0; |
| 59 | msg[0].buf = regbuf; |
| 60 | msg[0].len = 1; |
| 61 | msg[1].addr = state->config->demod_address; |
| 62 | msg[1].flags = I2C_M_RD; |
| 63 | msg[1].buf = buf; |
| 64 | msg[1].len = count; |
| 65 | |
| 66 | ret = i2c_transfer(state->i2c, msg, 2); |
| 67 | |
| 68 | if (ret != 2) { |
Matthias Schwarzott | 302e8ac | 2009-05-20 04:57:10 -0300 | [diff] [blame] | 69 | printk(KERN_DEBUG "%s: ret == %d\n", __func__, ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | return -EREMOTEIO; |
| 71 | } |
| 72 | |
Matthias Schwarzott | 89f6475 | 2007-12-21 08:56:44 -0300 | [diff] [blame] | 73 | if (debug) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | int i; |
| 75 | dprintk("R(%d):", reg & 0x7f); |
| 76 | for (i = 0; i < count; i++) |
Matthias Schwarzott | 0389b34 | 2009-07-02 16:17:28 -0300 | [diff] [blame] | 77 | printk(KERN_CONT " %02x", buf[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | printk("\n"); |
| 79 | } |
| 80 | |
| 81 | return 0; |
| 82 | } |
| 83 | |
Matthias Schwarzott | 89f6475 | 2007-12-21 08:56:44 -0300 | [diff] [blame] | 84 | static int mt312_write(struct mt312_state *state, const enum mt312_reg_addr reg, |
Matthias Schwarzott | 1881ee8 | 2008-04-12 15:04:46 -0300 | [diff] [blame] | 85 | const u8 *src, const size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | { |
| 87 | int ret; |
Mauro Carvalho Chehab | 8393796 | 2013-11-02 05:05:18 -0300 | [diff] [blame] | 88 | u8 buf[MAX_XFER_SIZE]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | struct i2c_msg msg; |
| 90 | |
Mauro Carvalho Chehab | 8393796 | 2013-11-02 05:05:18 -0300 | [diff] [blame] | 91 | if (1 + count > sizeof(buf)) { |
| 92 | printk(KERN_WARNING |
Mauro Carvalho Chehab | 35f30f3 | 2014-09-24 20:35:12 -0300 | [diff] [blame] | 93 | "mt312: write: len=%zu is too big!\n", count); |
Mauro Carvalho Chehab | 8393796 | 2013-11-02 05:05:18 -0300 | [diff] [blame] | 94 | return -EINVAL; |
| 95 | } |
| 96 | |
Matthias Schwarzott | 89f6475 | 2007-12-21 08:56:44 -0300 | [diff] [blame] | 97 | if (debug) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | int i; |
| 99 | dprintk("W(%d):", reg & 0x7f); |
| 100 | for (i = 0; i < count; i++) |
Matthias Schwarzott | 0389b34 | 2009-07-02 16:17:28 -0300 | [diff] [blame] | 101 | printk(KERN_CONT " %02x", src[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | printk("\n"); |
| 103 | } |
| 104 | |
| 105 | buf[0] = reg; |
| 106 | memcpy(&buf[1], src, count); |
| 107 | |
| 108 | msg.addr = state->config->demod_address; |
| 109 | msg.flags = 0; |
| 110 | msg.buf = buf; |
| 111 | msg.len = count + 1; |
| 112 | |
| 113 | ret = i2c_transfer(state->i2c, &msg, 1); |
| 114 | |
| 115 | if (ret != 1) { |
Harvey Harrison | 271ddbf | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 116 | dprintk("%s: ret == %d\n", __func__, ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | return -EREMOTEIO; |
| 118 | } |
| 119 | |
| 120 | return 0; |
| 121 | } |
| 122 | |
Matthias Schwarzott | 89f6475 | 2007-12-21 08:56:44 -0300 | [diff] [blame] | 123 | static inline int mt312_readreg(struct mt312_state *state, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | const enum mt312_reg_addr reg, u8 *val) |
| 125 | { |
| 126 | return mt312_read(state, reg, val, 1); |
| 127 | } |
| 128 | |
Matthias Schwarzott | 89f6475 | 2007-12-21 08:56:44 -0300 | [diff] [blame] | 129 | static inline int mt312_writereg(struct mt312_state *state, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | const enum mt312_reg_addr reg, const u8 val) |
| 131 | { |
Arnd Bergmann | 3cd890d | 2017-11-30 11:55:46 -0500 | [diff] [blame] | 132 | u8 tmp = val; /* see gcc.gnu.org/bugzilla/show_bug.cgi?id=81715 */ |
| 133 | |
| 134 | |
| 135 | return mt312_write(state, reg, &tmp, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | static inline u32 mt312_div(u32 a, u32 b) |
| 139 | { |
| 140 | return (a + (b / 2)) / b; |
| 141 | } |
| 142 | |
Matthias Schwarzott | 89f6475 | 2007-12-21 08:56:44 -0300 | [diff] [blame] | 143 | static int mt312_reset(struct mt312_state *state, const u8 full) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | { |
| 145 | return mt312_writereg(state, RESET, full ? 0x80 : 0x40); |
| 146 | } |
| 147 | |
Matthias Schwarzott | 89f6475 | 2007-12-21 08:56:44 -0300 | [diff] [blame] | 148 | static int mt312_get_inversion(struct mt312_state *state, |
Mauro Carvalho Chehab | 0df289a | 2015-06-07 14:53:52 -0300 | [diff] [blame] | 149 | enum fe_spectral_inversion *i) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | { |
| 151 | int ret; |
| 152 | u8 vit_mode; |
| 153 | |
Matthias Schwarzott | 994fc28b | 2007-12-24 07:12:55 -0300 | [diff] [blame] | 154 | ret = mt312_readreg(state, VIT_MODE, &vit_mode); |
| 155 | if (ret < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | return ret; |
| 157 | |
| 158 | if (vit_mode & 0x80) /* auto inversion was used */ |
| 159 | *i = (vit_mode & 0x40) ? INVERSION_ON : INVERSION_OFF; |
| 160 | |
| 161 | return 0; |
| 162 | } |
| 163 | |
Matthias Schwarzott | 89f6475 | 2007-12-21 08:56:44 -0300 | [diff] [blame] | 164 | static int mt312_get_symbol_rate(struct mt312_state *state, u32 *sr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | { |
| 166 | int ret; |
| 167 | u8 sym_rate_h; |
| 168 | u8 dec_ratio; |
| 169 | u16 sym_rat_op; |
| 170 | u16 monitor; |
| 171 | u8 buf[2]; |
| 172 | |
Matthias Schwarzott | 994fc28b | 2007-12-24 07:12:55 -0300 | [diff] [blame] | 173 | ret = mt312_readreg(state, SYM_RATE_H, &sym_rate_h); |
| 174 | if (ret < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | return ret; |
| 176 | |
Matthias Schwarzott | 89f6475 | 2007-12-21 08:56:44 -0300 | [diff] [blame] | 177 | if (sym_rate_h & 0x80) { |
| 178 | /* symbol rate search was used */ |
Matthias Schwarzott | 994fc28b | 2007-12-24 07:12:55 -0300 | [diff] [blame] | 179 | ret = mt312_writereg(state, MON_CTRL, 0x03); |
| 180 | if (ret < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | return ret; |
| 182 | |
Matthias Schwarzott | 994fc28b | 2007-12-24 07:12:55 -0300 | [diff] [blame] | 183 | ret = mt312_read(state, MONITOR_H, buf, sizeof(buf)); |
| 184 | if (ret < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | return ret; |
| 186 | |
| 187 | monitor = (buf[0] << 8) | buf[1]; |
| 188 | |
Matthias Schwarzott | 0b6a334 | 2007-12-21 08:58:09 -0300 | [diff] [blame] | 189 | dprintk("sr(auto) = %u\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | mt312_div(monitor * 15625, 4)); |
| 191 | } else { |
Matthias Schwarzott | 994fc28b | 2007-12-24 07:12:55 -0300 | [diff] [blame] | 192 | ret = mt312_writereg(state, MON_CTRL, 0x05); |
| 193 | if (ret < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | return ret; |
| 195 | |
Matthias Schwarzott | 994fc28b | 2007-12-24 07:12:55 -0300 | [diff] [blame] | 196 | ret = mt312_read(state, MONITOR_H, buf, sizeof(buf)); |
| 197 | if (ret < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | return ret; |
| 199 | |
| 200 | dec_ratio = ((buf[0] >> 5) & 0x07) * 32; |
| 201 | |
Matthias Schwarzott | 994fc28b | 2007-12-24 07:12:55 -0300 | [diff] [blame] | 202 | ret = mt312_read(state, SYM_RAT_OP_H, buf, sizeof(buf)); |
| 203 | if (ret < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | return ret; |
| 205 | |
| 206 | sym_rat_op = (buf[0] << 8) | buf[1]; |
| 207 | |
Matthias Schwarzott | 0b6a334 | 2007-12-21 08:58:09 -0300 | [diff] [blame] | 208 | dprintk("sym_rat_op=%d dec_ratio=%d\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | sym_rat_op, dec_ratio); |
Matthias Schwarzott | 0b6a334 | 2007-12-21 08:58:09 -0300 | [diff] [blame] | 210 | dprintk("*sr(manual) = %lu\n", |
Matthias Schwarzott | 111221f | 2008-04-12 15:04:48 -0300 | [diff] [blame] | 211 | (((state->xtal * 8192) / (sym_rat_op + 8192)) * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | 2) - dec_ratio); |
| 213 | } |
| 214 | |
| 215 | return 0; |
| 216 | } |
| 217 | |
Mauro Carvalho Chehab | 0df289a | 2015-06-07 14:53:52 -0300 | [diff] [blame] | 218 | static int mt312_get_code_rate(struct mt312_state *state, enum fe_code_rate *cr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | { |
Mauro Carvalho Chehab | 0df289a | 2015-06-07 14:53:52 -0300 | [diff] [blame] | 220 | const enum fe_code_rate fec_tab[8] = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | { FEC_1_2, FEC_2_3, FEC_3_4, FEC_5_6, FEC_6_7, FEC_7_8, |
| 222 | FEC_AUTO, FEC_AUTO }; |
| 223 | |
| 224 | int ret; |
| 225 | u8 fec_status; |
| 226 | |
Matthias Schwarzott | 994fc28b | 2007-12-24 07:12:55 -0300 | [diff] [blame] | 227 | ret = mt312_readreg(state, FEC_STATUS, &fec_status); |
| 228 | if (ret < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | return ret; |
| 230 | |
| 231 | *cr = fec_tab[(fec_status >> 4) & 0x07]; |
| 232 | |
| 233 | return 0; |
| 234 | } |
| 235 | |
Matthias Schwarzott | 89f6475 | 2007-12-21 08:56:44 -0300 | [diff] [blame] | 236 | static int mt312_initfe(struct dvb_frontend *fe) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | { |
Johannes Stezenbach | b874270 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 238 | struct mt312_state *state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | int ret; |
| 240 | u8 buf[2]; |
| 241 | |
| 242 | /* wake up */ |
Matthias Schwarzott | 994fc28b | 2007-12-24 07:12:55 -0300 | [diff] [blame] | 243 | ret = mt312_writereg(state, CONFIG, |
Matthias Schwarzott | 111221f | 2008-04-12 15:04:48 -0300 | [diff] [blame] | 244 | (state->freq_mult == 6 ? 0x88 : 0x8c)); |
Matthias Schwarzott | 994fc28b | 2007-12-24 07:12:55 -0300 | [diff] [blame] | 245 | if (ret < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | return ret; |
| 247 | |
| 248 | /* wait at least 150 usec */ |
| 249 | udelay(150); |
| 250 | |
| 251 | /* full reset */ |
Matthias Schwarzott | 994fc28b | 2007-12-24 07:12:55 -0300 | [diff] [blame] | 252 | ret = mt312_reset(state, 1); |
| 253 | if (ret < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | return ret; |
| 255 | |
Matthias Schwarzott | 89f6475 | 2007-12-21 08:56:44 -0300 | [diff] [blame] | 256 | /* Per datasheet, write correct values. 09/28/03 ACCJr. |
| 257 | * If we don't do this, we won't get FE_HAS_VITERBI in the VP310. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | { |
Matthias Schwarzott | 89f6475 | 2007-12-21 08:56:44 -0300 | [diff] [blame] | 259 | u8 buf_def[8] = { 0x14, 0x12, 0x03, 0x02, |
| 260 | 0x01, 0x00, 0x00, 0x00 }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | |
Matthias Schwarzott | 994fc28b | 2007-12-24 07:12:55 -0300 | [diff] [blame] | 262 | ret = mt312_write(state, VIT_SETUP, buf_def, sizeof(buf_def)); |
| 263 | if (ret < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | return ret; |
| 265 | } |
| 266 | |
Matthias Schwarzott | 6a5cbd5 | 2008-04-12 15:04:49 -0300 | [diff] [blame] | 267 | switch (state->id) { |
| 268 | case ID_ZL10313: |
| 269 | /* enable ADC */ |
| 270 | ret = mt312_writereg(state, GPP_CTRL, 0x80); |
| 271 | if (ret < 0) |
| 272 | return ret; |
| 273 | |
| 274 | /* configure ZL10313 for optimal ADC performance */ |
| 275 | buf[0] = 0x80; |
| 276 | buf[1] = 0xB0; |
| 277 | ret = mt312_write(state, HW_CTRL, buf, 2); |
| 278 | if (ret < 0) |
| 279 | return ret; |
| 280 | |
| 281 | /* enable MPEG output and ADCs */ |
| 282 | ret = mt312_writereg(state, HW_CTRL, 0x00); |
| 283 | if (ret < 0) |
| 284 | return ret; |
| 285 | |
| 286 | ret = mt312_writereg(state, MPEG_CTRL, 0x00); |
| 287 | if (ret < 0) |
| 288 | return ret; |
| 289 | |
| 290 | break; |
| 291 | } |
| 292 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | /* SYS_CLK */ |
Matthias Schwarzott | 111221f | 2008-04-12 15:04:48 -0300 | [diff] [blame] | 294 | buf[0] = mt312_div(state->xtal * state->freq_mult * 2, 1000000); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | |
| 296 | /* DISEQC_RATIO */ |
Matthias Schwarzott | 111221f | 2008-04-12 15:04:48 -0300 | [diff] [blame] | 297 | buf[1] = mt312_div(state->xtal, 22000 * 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | |
Matthias Schwarzott | 994fc28b | 2007-12-24 07:12:55 -0300 | [diff] [blame] | 299 | ret = mt312_write(state, SYS_CLK, buf, sizeof(buf)); |
| 300 | if (ret < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | return ret; |
| 302 | |
Matthias Schwarzott | 994fc28b | 2007-12-24 07:12:55 -0300 | [diff] [blame] | 303 | ret = mt312_writereg(state, SNR_THS_HIGH, 0x32); |
| 304 | if (ret < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | return ret; |
| 306 | |
Matthias Schwarzott | 6a5cbd5 | 2008-04-12 15:04:49 -0300 | [diff] [blame] | 307 | /* different MOCLK polarity */ |
| 308 | switch (state->id) { |
| 309 | case ID_ZL10313: |
| 310 | buf[0] = 0x33; |
| 311 | break; |
| 312 | default: |
| 313 | buf[0] = 0x53; |
| 314 | break; |
| 315 | } |
| 316 | |
| 317 | ret = mt312_writereg(state, OP_CTRL, buf[0]); |
Matthias Schwarzott | 994fc28b | 2007-12-24 07:12:55 -0300 | [diff] [blame] | 318 | if (ret < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | return ret; |
| 320 | |
| 321 | /* TS_SW_LIM */ |
| 322 | buf[0] = 0x8c; |
| 323 | buf[1] = 0x98; |
| 324 | |
Matthias Schwarzott | 994fc28b | 2007-12-24 07:12:55 -0300 | [diff] [blame] | 325 | ret = mt312_write(state, TS_SW_LIM_L, buf, sizeof(buf)); |
| 326 | if (ret < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | return ret; |
| 328 | |
Matthias Schwarzott | 994fc28b | 2007-12-24 07:12:55 -0300 | [diff] [blame] | 329 | ret = mt312_writereg(state, CS_SW_LIM, 0x69); |
| 330 | if (ret < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | return ret; |
| 332 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | return 0; |
| 334 | } |
| 335 | |
Matthias Schwarzott | 89f6475 | 2007-12-21 08:56:44 -0300 | [diff] [blame] | 336 | static int mt312_send_master_cmd(struct dvb_frontend *fe, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | struct dvb_diseqc_master_cmd *c) |
| 338 | { |
Johannes Stezenbach | b874270 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 339 | struct mt312_state *state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | int ret; |
| 341 | u8 diseqc_mode; |
| 342 | |
| 343 | if ((c->msg_len == 0) || (c->msg_len > sizeof(c->msg))) |
| 344 | return -EINVAL; |
| 345 | |
Matthias Schwarzott | 994fc28b | 2007-12-24 07:12:55 -0300 | [diff] [blame] | 346 | ret = mt312_readreg(state, DISEQC_MODE, &diseqc_mode); |
| 347 | if (ret < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | return ret; |
| 349 | |
Matthias Schwarzott | 994fc28b | 2007-12-24 07:12:55 -0300 | [diff] [blame] | 350 | ret = mt312_write(state, (0x80 | DISEQC_INSTR), c->msg, c->msg_len); |
| 351 | if (ret < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | return ret; |
| 353 | |
Matthias Schwarzott | 994fc28b | 2007-12-24 07:12:55 -0300 | [diff] [blame] | 354 | ret = mt312_writereg(state, DISEQC_MODE, |
| 355 | (diseqc_mode & 0x40) | ((c->msg_len - 1) << 3) |
| 356 | | 0x04); |
| 357 | if (ret < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | return ret; |
| 359 | |
Matthias Schwarzott | 82cd2df | 2008-04-12 15:04:47 -0300 | [diff] [blame] | 360 | /* is there a better way to wait for message to be transmitted */ |
| 361 | msleep(100); |
| 362 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | /* set DISEQC_MODE[2:0] to zero if a return message is expected */ |
Matthias Schwarzott | 994fc28b | 2007-12-24 07:12:55 -0300 | [diff] [blame] | 364 | if (c->msg[0] & 0x02) { |
| 365 | ret = mt312_writereg(state, DISEQC_MODE, (diseqc_mode & 0x40)); |
| 366 | if (ret < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | return ret; |
Matthias Schwarzott | 994fc28b | 2007-12-24 07:12:55 -0300 | [diff] [blame] | 368 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | |
| 370 | return 0; |
| 371 | } |
| 372 | |
Mauro Carvalho Chehab | 0df289a | 2015-06-07 14:53:52 -0300 | [diff] [blame] | 373 | static int mt312_send_burst(struct dvb_frontend *fe, |
| 374 | const enum fe_sec_mini_cmd c) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | { |
Johannes Stezenbach | b874270 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 376 | struct mt312_state *state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | const u8 mini_tab[2] = { 0x02, 0x03 }; |
| 378 | |
| 379 | int ret; |
| 380 | u8 diseqc_mode; |
| 381 | |
| 382 | if (c > SEC_MINI_B) |
| 383 | return -EINVAL; |
| 384 | |
Matthias Schwarzott | 994fc28b | 2007-12-24 07:12:55 -0300 | [diff] [blame] | 385 | ret = mt312_readreg(state, DISEQC_MODE, &diseqc_mode); |
| 386 | if (ret < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | return ret; |
| 388 | |
Matthias Schwarzott | 994fc28b | 2007-12-24 07:12:55 -0300 | [diff] [blame] | 389 | ret = mt312_writereg(state, DISEQC_MODE, |
| 390 | (diseqc_mode & 0x40) | mini_tab[c]); |
| 391 | if (ret < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | return ret; |
| 393 | |
| 394 | return 0; |
| 395 | } |
| 396 | |
Mauro Carvalho Chehab | 0df289a | 2015-06-07 14:53:52 -0300 | [diff] [blame] | 397 | static int mt312_set_tone(struct dvb_frontend *fe, |
| 398 | const enum fe_sec_tone_mode t) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | { |
Johannes Stezenbach | b874270 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 400 | struct mt312_state *state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | const u8 tone_tab[2] = { 0x01, 0x00 }; |
| 402 | |
| 403 | int ret; |
| 404 | u8 diseqc_mode; |
| 405 | |
| 406 | if (t > SEC_TONE_OFF) |
| 407 | return -EINVAL; |
| 408 | |
Matthias Schwarzott | 994fc28b | 2007-12-24 07:12:55 -0300 | [diff] [blame] | 409 | ret = mt312_readreg(state, DISEQC_MODE, &diseqc_mode); |
| 410 | if (ret < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | return ret; |
| 412 | |
Matthias Schwarzott | 994fc28b | 2007-12-24 07:12:55 -0300 | [diff] [blame] | 413 | ret = mt312_writereg(state, DISEQC_MODE, |
| 414 | (diseqc_mode & 0x40) | tone_tab[t]); |
| 415 | if (ret < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | return ret; |
| 417 | |
| 418 | return 0; |
| 419 | } |
| 420 | |
Mauro Carvalho Chehab | 0df289a | 2015-06-07 14:53:52 -0300 | [diff] [blame] | 421 | static int mt312_set_voltage(struct dvb_frontend *fe, |
| 422 | const enum fe_sec_voltage v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | { |
Johannes Stezenbach | b874270 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 424 | struct mt312_state *state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | const u8 volt_tab[3] = { 0x00, 0x40, 0x00 }; |
Matthias Schwarzott | 11d3f32 | 2008-04-12 15:04:50 -0300 | [diff] [blame] | 426 | u8 val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | |
| 428 | if (v > SEC_VOLTAGE_OFF) |
| 429 | return -EINVAL; |
| 430 | |
Matthias Schwarzott | 11d3f32 | 2008-04-12 15:04:50 -0300 | [diff] [blame] | 431 | val = volt_tab[v]; |
| 432 | if (state->config->voltage_inverted) |
| 433 | val ^= 0x40; |
| 434 | |
| 435 | return mt312_writereg(state, DISEQC_MODE, val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | } |
| 437 | |
Mauro Carvalho Chehab | 0df289a | 2015-06-07 14:53:52 -0300 | [diff] [blame] | 438 | static int mt312_read_status(struct dvb_frontend *fe, enum fe_status *s) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | { |
Johannes Stezenbach | b874270 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 440 | struct mt312_state *state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | int ret; |
| 442 | u8 status[3]; |
| 443 | |
| 444 | *s = 0; |
| 445 | |
Matthias Schwarzott | 994fc28b | 2007-12-24 07:12:55 -0300 | [diff] [blame] | 446 | ret = mt312_read(state, QPSK_STAT_H, status, sizeof(status)); |
| 447 | if (ret < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | return ret; |
| 449 | |
Mauro Carvalho Chehab | 4bd69e7 | 2016-10-18 17:44:22 -0200 | [diff] [blame] | 450 | dprintk("QPSK_STAT_H: 0x%02x, QPSK_STAT_L: 0x%02x, FEC_STATUS: 0x%02x\n", |
| 451 | status[0], status[1], status[2]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | |
| 453 | if (status[0] & 0xc0) |
| 454 | *s |= FE_HAS_SIGNAL; /* signal noise ratio */ |
| 455 | if (status[0] & 0x04) |
| 456 | *s |= FE_HAS_CARRIER; /* qpsk carrier lock */ |
| 457 | if (status[2] & 0x02) |
| 458 | *s |= FE_HAS_VITERBI; /* viterbi lock */ |
| 459 | if (status[2] & 0x04) |
| 460 | *s |= FE_HAS_SYNC; /* byte align lock */ |
| 461 | if (status[0] & 0x01) |
| 462 | *s |= FE_HAS_LOCK; /* qpsk lock */ |
| 463 | |
| 464 | return 0; |
| 465 | } |
| 466 | |
Matthias Schwarzott | 89f6475 | 2007-12-21 08:56:44 -0300 | [diff] [blame] | 467 | static int mt312_read_ber(struct dvb_frontend *fe, u32 *ber) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | { |
Johannes Stezenbach | b874270 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 469 | struct mt312_state *state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | int ret; |
| 471 | u8 buf[3]; |
| 472 | |
Matthias Schwarzott | 994fc28b | 2007-12-24 07:12:55 -0300 | [diff] [blame] | 473 | ret = mt312_read(state, RS_BERCNT_H, buf, 3); |
| 474 | if (ret < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | return ret; |
| 476 | |
| 477 | *ber = ((buf[0] << 16) | (buf[1] << 8) | buf[2]) * 64; |
| 478 | |
| 479 | return 0; |
| 480 | } |
| 481 | |
Matthias Schwarzott | 89f6475 | 2007-12-21 08:56:44 -0300 | [diff] [blame] | 482 | static int mt312_read_signal_strength(struct dvb_frontend *fe, |
| 483 | u16 *signal_strength) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | { |
Johannes Stezenbach | b874270 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 485 | struct mt312_state *state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | int ret; |
| 487 | u8 buf[3]; |
| 488 | u16 agc; |
| 489 | s16 err_db; |
| 490 | |
Matthias Schwarzott | 994fc28b | 2007-12-24 07:12:55 -0300 | [diff] [blame] | 491 | ret = mt312_read(state, AGC_H, buf, sizeof(buf)); |
| 492 | if (ret < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | return ret; |
| 494 | |
| 495 | agc = (buf[0] << 6) | (buf[1] >> 2); |
| 496 | err_db = (s16) (((buf[1] & 0x03) << 14) | buf[2] << 6) >> 6; |
| 497 | |
| 498 | *signal_strength = agc; |
| 499 | |
Matthias Schwarzott | 0b6a334 | 2007-12-21 08:58:09 -0300 | [diff] [blame] | 500 | dprintk("agc=%08x err_db=%hd\n", agc, err_db); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | |
| 502 | return 0; |
| 503 | } |
| 504 | |
Matthias Schwarzott | 89f6475 | 2007-12-21 08:56:44 -0300 | [diff] [blame] | 505 | static int mt312_read_snr(struct dvb_frontend *fe, u16 *snr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | { |
Johannes Stezenbach | b874270 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 507 | struct mt312_state *state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | int ret; |
| 509 | u8 buf[2]; |
| 510 | |
Matthias Schwarzott | 1881ee8 | 2008-04-12 15:04:46 -0300 | [diff] [blame] | 511 | ret = mt312_read(state, M_SNR_H, buf, sizeof(buf)); |
Matthias Schwarzott | 994fc28b | 2007-12-24 07:12:55 -0300 | [diff] [blame] | 512 | if (ret < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | return ret; |
| 514 | |
| 515 | *snr = 0xFFFF - ((((buf[0] & 0x7f) << 8) | buf[1]) << 1); |
| 516 | |
| 517 | return 0; |
| 518 | } |
| 519 | |
Matthias Schwarzott | 89f6475 | 2007-12-21 08:56:44 -0300 | [diff] [blame] | 520 | static int mt312_read_ucblocks(struct dvb_frontend *fe, u32 *ubc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | { |
Johannes Stezenbach | b874270 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 522 | struct mt312_state *state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | int ret; |
| 524 | u8 buf[2]; |
| 525 | |
Matthias Schwarzott | 1881ee8 | 2008-04-12 15:04:46 -0300 | [diff] [blame] | 526 | ret = mt312_read(state, RS_UBC_H, buf, sizeof(buf)); |
Matthias Schwarzott | 994fc28b | 2007-12-24 07:12:55 -0300 | [diff] [blame] | 527 | if (ret < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | return ret; |
| 529 | |
| 530 | *ubc = (buf[0] << 8) | buf[1]; |
| 531 | |
| 532 | return 0; |
| 533 | } |
| 534 | |
Mauro Carvalho Chehab | 827b5f3 | 2011-12-26 13:50:05 -0300 | [diff] [blame] | 535 | static int mt312_set_frontend(struct dvb_frontend *fe) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | { |
Mauro Carvalho Chehab | 827b5f3 | 2011-12-26 13:50:05 -0300 | [diff] [blame] | 537 | struct dtv_frontend_properties *p = &fe->dtv_property_cache; |
Johannes Stezenbach | b874270 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 538 | struct mt312_state *state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | int ret; |
| 540 | u8 buf[5], config_val; |
| 541 | u16 sr; |
| 542 | |
| 543 | const u8 fec_tab[10] = |
| 544 | { 0x00, 0x01, 0x02, 0x04, 0x3f, 0x08, 0x10, 0x20, 0x3f, 0x3f }; |
| 545 | const u8 inv_tab[3] = { 0x00, 0x40, 0x80 }; |
| 546 | |
Harvey Harrison | 271ddbf | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 547 | dprintk("%s: Freq %d\n", __func__, p->frequency); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | |
Mauro Carvalho Chehab | f1b1eab | 2018-07-05 18:59:36 -0400 | [diff] [blame] | 549 | if ((p->frequency < fe->ops.info.frequency_min_hz / kHz) |
| 550 | || (p->frequency > fe->ops.info.frequency_max_hz / kHz)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | return -EINVAL; |
| 552 | |
Mauro Carvalho Chehab | 830e4b5 | 2012-10-27 16:14:01 -0300 | [diff] [blame] | 553 | if (((int)p->inversion < INVERSION_OFF) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | || (p->inversion > INVERSION_ON)) |
| 555 | return -EINVAL; |
| 556 | |
Mauro Carvalho Chehab | 827b5f3 | 2011-12-26 13:50:05 -0300 | [diff] [blame] | 557 | if ((p->symbol_rate < fe->ops.info.symbol_rate_min) |
| 558 | || (p->symbol_rate > fe->ops.info.symbol_rate_max)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | return -EINVAL; |
| 560 | |
Mauro Carvalho Chehab | 830e4b5 | 2012-10-27 16:14:01 -0300 | [diff] [blame] | 561 | if (((int)p->fec_inner < FEC_NONE) |
Mauro Carvalho Chehab | 827b5f3 | 2011-12-26 13:50:05 -0300 | [diff] [blame] | 562 | || (p->fec_inner > FEC_AUTO)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | return -EINVAL; |
| 564 | |
Mauro Carvalho Chehab | 827b5f3 | 2011-12-26 13:50:05 -0300 | [diff] [blame] | 565 | if ((p->fec_inner == FEC_4_5) |
| 566 | || (p->fec_inner == FEC_8_9)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | return -EINVAL; |
| 568 | |
| 569 | switch (state->id) { |
| 570 | case ID_VP310: |
Matthias Schwarzott | 89f6475 | 2007-12-21 08:56:44 -0300 | [diff] [blame] | 571 | /* For now we will do this only for the VP310. |
| 572 | * It should be better for the mt312 as well, |
| 573 | * but tuning will be slower. ACCJr 09/29/03 |
| 574 | */ |
Alexey Dobriyan | 682e852 | 2006-01-10 00:09:16 +0300 | [diff] [blame] | 575 | ret = mt312_readreg(state, CONFIG, &config_val); |
| 576 | if (ret < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | return ret; |
Mauro Carvalho Chehab | 827b5f3 | 2011-12-26 13:50:05 -0300 | [diff] [blame] | 578 | if (p->symbol_rate >= 30000000) { |
Matthias Schwarzott | 89f6475 | 2007-12-21 08:56:44 -0300 | [diff] [blame] | 579 | /* Note that 30MS/s should use 90MHz */ |
Matthias Schwarzott | 111221f | 2008-04-12 15:04:48 -0300 | [diff] [blame] | 580 | if (state->freq_mult == 6) { |
Matthias Schwarzott | 89f6475 | 2007-12-21 08:56:44 -0300 | [diff] [blame] | 581 | /* We are running 60MHz */ |
Matthias Schwarzott | 111221f | 2008-04-12 15:04:48 -0300 | [diff] [blame] | 582 | state->freq_mult = 9; |
Matthias Schwarzott | 994fc28b | 2007-12-24 07:12:55 -0300 | [diff] [blame] | 583 | ret = mt312_initfe(fe); |
| 584 | if (ret < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | return ret; |
| 586 | } |
Matthias Schwarzott | 89f6475 | 2007-12-21 08:56:44 -0300 | [diff] [blame] | 587 | } else { |
Matthias Schwarzott | 111221f | 2008-04-12 15:04:48 -0300 | [diff] [blame] | 588 | if (state->freq_mult == 9) { |
Matthias Schwarzott | 89f6475 | 2007-12-21 08:56:44 -0300 | [diff] [blame] | 589 | /* We are running 90MHz */ |
Matthias Schwarzott | 111221f | 2008-04-12 15:04:48 -0300 | [diff] [blame] | 590 | state->freq_mult = 6; |
Matthias Schwarzott | 994fc28b | 2007-12-24 07:12:55 -0300 | [diff] [blame] | 591 | ret = mt312_initfe(fe); |
| 592 | if (ret < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | return ret; |
| 594 | } |
| 595 | } |
| 596 | break; |
| 597 | |
| 598 | case ID_MT312: |
Matthias Schwarzott | 6a5cbd5 | 2008-04-12 15:04:49 -0300 | [diff] [blame] | 599 | case ID_ZL10313: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | break; |
| 601 | |
| 602 | default: |
| 603 | return -EINVAL; |
| 604 | } |
| 605 | |
Patrick Boettcher | dea7486 | 2006-05-14 05:01:31 -0300 | [diff] [blame] | 606 | if (fe->ops.tuner_ops.set_params) { |
Mauro Carvalho Chehab | 14d24d1 | 2011-12-24 12:24:33 -0300 | [diff] [blame] | 607 | fe->ops.tuner_ops.set_params(fe); |
Matthias Schwarzott | 89f6475 | 2007-12-21 08:56:44 -0300 | [diff] [blame] | 608 | if (fe->ops.i2c_gate_ctrl) |
| 609 | fe->ops.i2c_gate_ctrl(fe, 0); |
Andrew de Quincey | a81870e | 2006-04-18 17:47:09 -0300 | [diff] [blame] | 610 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | |
| 612 | /* sr = (u16)(sr * 256.0 / 1000000.0) */ |
Mauro Carvalho Chehab | 827b5f3 | 2011-12-26 13:50:05 -0300 | [diff] [blame] | 613 | sr = mt312_div(p->symbol_rate * 4, 15625); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | |
| 615 | /* SYM_RATE */ |
| 616 | buf[0] = (sr >> 8) & 0x3f; |
| 617 | buf[1] = (sr >> 0) & 0xff; |
| 618 | |
| 619 | /* VIT_MODE */ |
Mauro Carvalho Chehab | 827b5f3 | 2011-12-26 13:50:05 -0300 | [diff] [blame] | 620 | buf[2] = inv_tab[p->inversion] | fec_tab[p->fec_inner]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | |
| 622 | /* QPSK_CTRL */ |
| 623 | buf[3] = 0x40; /* swap I and Q before QPSK demodulation */ |
| 624 | |
Mauro Carvalho Chehab | 827b5f3 | 2011-12-26 13:50:05 -0300 | [diff] [blame] | 625 | if (p->symbol_rate < 10000000) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | buf[3] |= 0x04; /* use afc mode */ |
| 627 | |
| 628 | /* GO */ |
| 629 | buf[4] = 0x01; |
| 630 | |
Matthias Schwarzott | 994fc28b | 2007-12-24 07:12:55 -0300 | [diff] [blame] | 631 | ret = mt312_write(state, SYM_RATE_H, buf, sizeof(buf)); |
| 632 | if (ret < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | return ret; |
| 634 | |
Kangjie Lu | 9502cdf | 2018-12-21 02:07:20 -0500 | [diff] [blame] | 635 | ret = mt312_reset(state, 0); |
| 636 | if (ret < 0) |
| 637 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | |
| 639 | return 0; |
| 640 | } |
| 641 | |
Mauro Carvalho Chehab | 7e3e68b | 2016-02-04 12:58:30 -0200 | [diff] [blame] | 642 | static int mt312_get_frontend(struct dvb_frontend *fe, |
| 643 | struct dtv_frontend_properties *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | { |
Johannes Stezenbach | b874270 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 645 | struct mt312_state *state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | int ret; |
| 647 | |
Matthias Schwarzott | 994fc28b | 2007-12-24 07:12:55 -0300 | [diff] [blame] | 648 | ret = mt312_get_inversion(state, &p->inversion); |
| 649 | if (ret < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | return ret; |
| 651 | |
Mauro Carvalho Chehab | 827b5f3 | 2011-12-26 13:50:05 -0300 | [diff] [blame] | 652 | ret = mt312_get_symbol_rate(state, &p->symbol_rate); |
Matthias Schwarzott | 994fc28b | 2007-12-24 07:12:55 -0300 | [diff] [blame] | 653 | if (ret < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | return ret; |
| 655 | |
Mauro Carvalho Chehab | 827b5f3 | 2011-12-26 13:50:05 -0300 | [diff] [blame] | 656 | ret = mt312_get_code_rate(state, &p->fec_inner); |
Matthias Schwarzott | 994fc28b | 2007-12-24 07:12:55 -0300 | [diff] [blame] | 657 | if (ret < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | return ret; |
| 659 | |
| 660 | return 0; |
| 661 | } |
| 662 | |
Matthias Schwarzott | 89f6475 | 2007-12-21 08:56:44 -0300 | [diff] [blame] | 663 | static int mt312_i2c_gate_ctrl(struct dvb_frontend *fe, int enable) |
Andrew de Quincey | a81870e | 2006-04-18 17:47:09 -0300 | [diff] [blame] | 664 | { |
Matthias Schwarzott | 89f6475 | 2007-12-21 08:56:44 -0300 | [diff] [blame] | 665 | struct mt312_state *state = fe->demodulator_priv; |
Andrew de Quincey | a81870e | 2006-04-18 17:47:09 -0300 | [diff] [blame] | 666 | |
Matthias Schwarzott | 6a5cbd5 | 2008-04-12 15:04:49 -0300 | [diff] [blame] | 667 | u8 val = 0x00; |
| 668 | int ret; |
| 669 | |
| 670 | switch (state->id) { |
| 671 | case ID_ZL10313: |
| 672 | ret = mt312_readreg(state, GPP_CTRL, &val); |
| 673 | if (ret < 0) |
| 674 | goto error; |
| 675 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 676 | /* preserve this bit to not accidentally shutdown ADC */ |
Matthias Schwarzott | 6a5cbd5 | 2008-04-12 15:04:49 -0300 | [diff] [blame] | 677 | val &= 0x80; |
| 678 | break; |
Andrew de Quincey | a81870e | 2006-04-18 17:47:09 -0300 | [diff] [blame] | 679 | } |
Matthias Schwarzott | 6a5cbd5 | 2008-04-12 15:04:49 -0300 | [diff] [blame] | 680 | |
| 681 | if (enable) |
| 682 | val |= 0x40; |
| 683 | else |
| 684 | val &= ~0x40; |
| 685 | |
| 686 | ret = mt312_writereg(state, GPP_CTRL, val); |
| 687 | |
| 688 | error: |
| 689 | return ret; |
Andrew de Quincey | a81870e | 2006-04-18 17:47:09 -0300 | [diff] [blame] | 690 | } |
| 691 | |
Matthias Schwarzott | 89f6475 | 2007-12-21 08:56:44 -0300 | [diff] [blame] | 692 | static int mt312_sleep(struct dvb_frontend *fe) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | { |
Johannes Stezenbach | b874270 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 694 | struct mt312_state *state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | int ret; |
| 696 | u8 config; |
| 697 | |
| 698 | /* reset all registers to defaults */ |
Matthias Schwarzott | 994fc28b | 2007-12-24 07:12:55 -0300 | [diff] [blame] | 699 | ret = mt312_reset(state, 1); |
| 700 | if (ret < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | return ret; |
| 702 | |
Matthias Schwarzott | 6a5cbd5 | 2008-04-12 15:04:49 -0300 | [diff] [blame] | 703 | if (state->id == ID_ZL10313) { |
| 704 | /* reset ADC */ |
| 705 | ret = mt312_writereg(state, GPP_CTRL, 0x00); |
| 706 | if (ret < 0) |
| 707 | return ret; |
| 708 | |
| 709 | /* full shutdown of ADCs, mpeg bus tristated */ |
| 710 | ret = mt312_writereg(state, HW_CTRL, 0x0d); |
| 711 | if (ret < 0) |
| 712 | return ret; |
| 713 | } |
| 714 | |
Matthias Schwarzott | 994fc28b | 2007-12-24 07:12:55 -0300 | [diff] [blame] | 715 | ret = mt312_readreg(state, CONFIG, &config); |
| 716 | if (ret < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | return ret; |
| 718 | |
| 719 | /* enter standby */ |
Matthias Schwarzott | 994fc28b | 2007-12-24 07:12:55 -0300 | [diff] [blame] | 720 | ret = mt312_writereg(state, CONFIG, config & 0x7f); |
| 721 | if (ret < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 722 | return ret; |
| 723 | |
| 724 | return 0; |
| 725 | } |
| 726 | |
Matthias Schwarzott | 89f6475 | 2007-12-21 08:56:44 -0300 | [diff] [blame] | 727 | static int mt312_get_tune_settings(struct dvb_frontend *fe, |
| 728 | struct dvb_frontend_tune_settings *fesettings) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | { |
| 730 | fesettings->min_delay_ms = 50; |
| 731 | fesettings->step_size = 0; |
| 732 | fesettings->max_drift = 0; |
| 733 | return 0; |
| 734 | } |
| 735 | |
Matthias Schwarzott | 89f6475 | 2007-12-21 08:56:44 -0300 | [diff] [blame] | 736 | static void mt312_release(struct dvb_frontend *fe) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | { |
Matthias Schwarzott | 89f6475 | 2007-12-21 08:56:44 -0300 | [diff] [blame] | 738 | struct mt312_state *state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | kfree(state); |
| 740 | } |
| 741 | |
Matthias Schwarzott | 111221f | 2008-04-12 15:04:48 -0300 | [diff] [blame] | 742 | #define MT312_SYS_CLK 90000000UL /* 90 MHz */ |
Max Kellermann | bd336e6 | 2016-08-09 18:32:21 -0300 | [diff] [blame] | 743 | static const struct dvb_frontend_ops mt312_ops = { |
Mauro Carvalho Chehab | 827b5f3 | 2011-12-26 13:50:05 -0300 | [diff] [blame] | 744 | .delsys = { SYS_DVBS }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 745 | .info = { |
| 746 | .name = "Zarlink ???? DVB-S", |
Mauro Carvalho Chehab | f1b1eab | 2018-07-05 18:59:36 -0400 | [diff] [blame] | 747 | .frequency_min_hz = 950 * MHz, |
| 748 | .frequency_max_hz = 2150 * MHz, |
Matthias Schwarzott | 0389b34 | 2009-07-02 16:17:28 -0300 | [diff] [blame] | 749 | /* FIXME: adjust freq to real used xtal */ |
Mauro Carvalho Chehab | f1b1eab | 2018-07-05 18:59:36 -0400 | [diff] [blame] | 750 | .frequency_stepsize_hz = MT312_PLL_CLK / 128, |
Matthias Schwarzott | 111221f | 2008-04-12 15:04:48 -0300 | [diff] [blame] | 751 | .symbol_rate_min = MT312_SYS_CLK / 128, /* FIXME as above */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | .symbol_rate_max = MT312_SYS_CLK / 2, |
| 753 | .caps = |
| 754 | FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | |
| 755 | FE_CAN_FEC_3_4 | FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | |
| 756 | FE_CAN_FEC_AUTO | FE_CAN_QPSK | FE_CAN_MUTE_TS | |
Mauro Carvalho Chehab | 9101e62 | 2005-12-12 00:37:24 -0800 | [diff] [blame] | 757 | FE_CAN_RECOVER |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | }, |
| 759 | |
| 760 | .release = mt312_release, |
| 761 | |
| 762 | .init = mt312_initfe, |
| 763 | .sleep = mt312_sleep, |
Andrew de Quincey | a81870e | 2006-04-18 17:47:09 -0300 | [diff] [blame] | 764 | .i2c_gate_ctrl = mt312_i2c_gate_ctrl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | |
Mauro Carvalho Chehab | 827b5f3 | 2011-12-26 13:50:05 -0300 | [diff] [blame] | 766 | .set_frontend = mt312_set_frontend, |
| 767 | .get_frontend = mt312_get_frontend, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | .get_tune_settings = mt312_get_tune_settings, |
| 769 | |
| 770 | .read_status = mt312_read_status, |
| 771 | .read_ber = mt312_read_ber, |
| 772 | .read_signal_strength = mt312_read_signal_strength, |
| 773 | .read_snr = mt312_read_snr, |
| 774 | .read_ucblocks = mt312_read_ucblocks, |
| 775 | |
| 776 | .diseqc_send_master_cmd = mt312_send_master_cmd, |
| 777 | .diseqc_send_burst = mt312_send_burst, |
| 778 | .set_tone = mt312_set_tone, |
| 779 | .set_voltage = mt312_set_voltage, |
| 780 | }; |
| 781 | |
Matthias Schwarzott | e4671b6 | 2008-04-30 12:21:04 -0300 | [diff] [blame] | 782 | struct dvb_frontend *mt312_attach(const struct mt312_config *config, |
Matthias Schwarzott | 89f6475 | 2007-12-21 08:56:44 -0300 | [diff] [blame] | 783 | struct i2c_adapter *i2c) |
Adrian Bunk | 805e660 | 2006-02-27 00:07:49 -0300 | [diff] [blame] | 784 | { |
Matthias Schwarzott | 89f6475 | 2007-12-21 08:56:44 -0300 | [diff] [blame] | 785 | struct mt312_state *state = NULL; |
Adrian Bunk | 805e660 | 2006-02-27 00:07:49 -0300 | [diff] [blame] | 786 | |
| 787 | /* allocate memory for the internal state */ |
Matthias Schwarzott | 084e24a | 2009-08-10 22:51:01 -0300 | [diff] [blame] | 788 | state = kzalloc(sizeof(struct mt312_state), GFP_KERNEL); |
Adrian Bunk | 805e660 | 2006-02-27 00:07:49 -0300 | [diff] [blame] | 789 | if (state == NULL) |
| 790 | goto error; |
| 791 | |
| 792 | /* setup the state */ |
| 793 | state->config = config; |
| 794 | state->i2c = i2c; |
Adrian Bunk | 805e660 | 2006-02-27 00:07:49 -0300 | [diff] [blame] | 795 | |
| 796 | /* check if the demod is there */ |
| 797 | if (mt312_readreg(state, ID, &state->id) < 0) |
| 798 | goto error; |
| 799 | |
Patrick Boettcher | dea7486 | 2006-05-14 05:01:31 -0300 | [diff] [blame] | 800 | /* create dvb_frontend */ |
Matthias Schwarzott | e4671b6 | 2008-04-30 12:21:04 -0300 | [diff] [blame] | 801 | memcpy(&state->frontend.ops, &mt312_ops, |
Matthias Schwarzott | 89f6475 | 2007-12-21 08:56:44 -0300 | [diff] [blame] | 802 | sizeof(struct dvb_frontend_ops)); |
Patrick Boettcher | dea7486 | 2006-05-14 05:01:31 -0300 | [diff] [blame] | 803 | state->frontend.demodulator_priv = state; |
| 804 | |
Adrian Bunk | 805e660 | 2006-02-27 00:07:49 -0300 | [diff] [blame] | 805 | switch (state->id) { |
| 806 | case ID_VP310: |
Mauro Carvalho Chehab | cc1e631 | 2018-09-10 16:20:42 -0400 | [diff] [blame] | 807 | strscpy(state->frontend.ops.info.name, "Zarlink VP310 DVB-S", |
| 808 | sizeof(state->frontend.ops.info.name)); |
Matthias Schwarzott | 111221f | 2008-04-12 15:04:48 -0300 | [diff] [blame] | 809 | state->xtal = MT312_PLL_CLK; |
| 810 | state->freq_mult = 9; |
Adrian Bunk | 805e660 | 2006-02-27 00:07:49 -0300 | [diff] [blame] | 811 | break; |
| 812 | case ID_MT312: |
Mauro Carvalho Chehab | cc1e631 | 2018-09-10 16:20:42 -0400 | [diff] [blame] | 813 | strscpy(state->frontend.ops.info.name, "Zarlink MT312 DVB-S", |
| 814 | sizeof(state->frontend.ops.info.name)); |
Matthias Schwarzott | 111221f | 2008-04-12 15:04:48 -0300 | [diff] [blame] | 815 | state->xtal = MT312_PLL_CLK; |
| 816 | state->freq_mult = 6; |
Adrian Bunk | 805e660 | 2006-02-27 00:07:49 -0300 | [diff] [blame] | 817 | break; |
Matthias Schwarzott | 6a5cbd5 | 2008-04-12 15:04:49 -0300 | [diff] [blame] | 818 | case ID_ZL10313: |
Mauro Carvalho Chehab | cc1e631 | 2018-09-10 16:20:42 -0400 | [diff] [blame] | 819 | strscpy(state->frontend.ops.info.name, "Zarlink ZL10313 DVB-S", |
| 820 | sizeof(state->frontend.ops.info.name)); |
Matthias Schwarzott | 6a5cbd5 | 2008-04-12 15:04:49 -0300 | [diff] [blame] | 821 | state->xtal = MT312_PLL_CLK_10_111; |
| 822 | state->freq_mult = 9; |
| 823 | break; |
Adrian Bunk | 805e660 | 2006-02-27 00:07:49 -0300 | [diff] [blame] | 824 | default: |
Mauro Carvalho Chehab | 4bd69e7 | 2016-10-18 17:44:22 -0200 | [diff] [blame] | 825 | printk(KERN_WARNING "Only Zarlink VP310/MT312/ZL10313 are supported chips.\n"); |
Adrian Bunk | 805e660 | 2006-02-27 00:07:49 -0300 | [diff] [blame] | 826 | goto error; |
| 827 | } |
| 828 | |
Adrian Bunk | 805e660 | 2006-02-27 00:07:49 -0300 | [diff] [blame] | 829 | return &state->frontend; |
| 830 | |
| 831 | error: |
| 832 | kfree(state); |
| 833 | return NULL; |
| 834 | } |
Matthias Schwarzott | e4671b6 | 2008-04-30 12:21:04 -0300 | [diff] [blame] | 835 | EXPORT_SYMBOL(mt312_attach); |
Adrian Bunk | 805e660 | 2006-02-27 00:07:49 -0300 | [diff] [blame] | 836 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | module_param(debug, int, 0644); |
| 838 | MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off)."); |
| 839 | |
Matthias Schwarzott | 6a5cbd5 | 2008-04-12 15:04:49 -0300 | [diff] [blame] | 840 | MODULE_DESCRIPTION("Zarlink VP310/MT312/ZL10313 DVB-S Demodulator driver"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 841 | MODULE_AUTHOR("Andreas Oberritter <obi@linuxtv.org>"); |
Matthias Schwarzott | e4671b6 | 2008-04-30 12:21:04 -0300 | [diff] [blame] | 842 | MODULE_AUTHOR("Matthias Schwarzott <zzam@gentoo.org>"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | MODULE_LICENSE("GPL"); |
| 844 | |