1395d00d1SAntti Palosaari /* 2f4df95bcSnibble.max * Montage M88DS3103/M88RS6000 demodulator driver 3395d00d1SAntti Palosaari * 4395d00d1SAntti Palosaari * Copyright (C) 2013 Antti Palosaari <crope@iki.fi> 5395d00d1SAntti Palosaari * 6395d00d1SAntti Palosaari * This program is free software; you can redistribute it and/or modify 7395d00d1SAntti Palosaari * it under the terms of the GNU General Public License as published by 8395d00d1SAntti Palosaari * the Free Software Foundation; either version 2 of the License, or 9395d00d1SAntti Palosaari * (at your option) any later version. 10395d00d1SAntti Palosaari * 11395d00d1SAntti Palosaari * This program is distributed in the hope that it will be useful, 12395d00d1SAntti Palosaari * but WITHOUT ANY WARRANTY; without even the implied warranty of 13395d00d1SAntti Palosaari * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14395d00d1SAntti Palosaari * GNU General Public License for more details. 15395d00d1SAntti Palosaari */ 16395d00d1SAntti Palosaari 17395d00d1SAntti Palosaari #include "m88ds3103_priv.h" 18395d00d1SAntti Palosaari 19395d00d1SAntti Palosaari static struct dvb_frontend_ops m88ds3103_ops; 20395d00d1SAntti Palosaari 21395d00d1SAntti Palosaari /* write multiple registers */ 22395d00d1SAntti Palosaari static int m88ds3103_wr_regs(struct m88ds3103_priv *priv, 23395d00d1SAntti Palosaari u8 reg, const u8 *val, int len) 24395d00d1SAntti Palosaari { 2563c80f70SAntti Palosaari #define MAX_WR_LEN 32 2663c80f70SAntti Palosaari #define MAX_WR_XFER_LEN (MAX_WR_LEN + 1) 27395d00d1SAntti Palosaari int ret; 2863c80f70SAntti Palosaari u8 buf[MAX_WR_XFER_LEN]; 29395d00d1SAntti Palosaari struct i2c_msg msg[1] = { 30395d00d1SAntti Palosaari { 31395d00d1SAntti Palosaari .addr = priv->cfg->i2c_addr, 32395d00d1SAntti Palosaari .flags = 0, 3363c80f70SAntti Palosaari .len = 1 + len, 34395d00d1SAntti Palosaari .buf = buf, 35395d00d1SAntti Palosaari } 36395d00d1SAntti Palosaari }; 37395d00d1SAntti Palosaari 3863c80f70SAntti Palosaari if (WARN_ON(len > MAX_WR_LEN)) 3963c80f70SAntti Palosaari return -EINVAL; 4063c80f70SAntti Palosaari 41395d00d1SAntti Palosaari buf[0] = reg; 42395d00d1SAntti Palosaari memcpy(&buf[1], val, len); 43395d00d1SAntti Palosaari 44395d00d1SAntti Palosaari mutex_lock(&priv->i2c_mutex); 45395d00d1SAntti Palosaari ret = i2c_transfer(priv->i2c, msg, 1); 46395d00d1SAntti Palosaari mutex_unlock(&priv->i2c_mutex); 47395d00d1SAntti Palosaari if (ret == 1) { 48395d00d1SAntti Palosaari ret = 0; 49395d00d1SAntti Palosaari } else { 50395d00d1SAntti Palosaari dev_warn(&priv->i2c->dev, 51395d00d1SAntti Palosaari "%s: i2c wr failed=%d reg=%02x len=%d\n", 52395d00d1SAntti Palosaari KBUILD_MODNAME, ret, reg, len); 53395d00d1SAntti Palosaari ret = -EREMOTEIO; 54395d00d1SAntti Palosaari } 55395d00d1SAntti Palosaari 56395d00d1SAntti Palosaari return ret; 57395d00d1SAntti Palosaari } 58395d00d1SAntti Palosaari 59395d00d1SAntti Palosaari /* read multiple registers */ 60395d00d1SAntti Palosaari static int m88ds3103_rd_regs(struct m88ds3103_priv *priv, 61395d00d1SAntti Palosaari u8 reg, u8 *val, int len) 62395d00d1SAntti Palosaari { 6363c80f70SAntti Palosaari #define MAX_RD_LEN 3 6463c80f70SAntti Palosaari #define MAX_RD_XFER_LEN (MAX_RD_LEN) 65395d00d1SAntti Palosaari int ret; 6663c80f70SAntti Palosaari u8 buf[MAX_RD_XFER_LEN]; 67395d00d1SAntti Palosaari struct i2c_msg msg[2] = { 68395d00d1SAntti Palosaari { 69395d00d1SAntti Palosaari .addr = priv->cfg->i2c_addr, 70395d00d1SAntti Palosaari .flags = 0, 71395d00d1SAntti Palosaari .len = 1, 72395d00d1SAntti Palosaari .buf = ®, 73395d00d1SAntti Palosaari }, { 74395d00d1SAntti Palosaari .addr = priv->cfg->i2c_addr, 75395d00d1SAntti Palosaari .flags = I2C_M_RD, 7663c80f70SAntti Palosaari .len = len, 77395d00d1SAntti Palosaari .buf = buf, 78395d00d1SAntti Palosaari } 79395d00d1SAntti Palosaari }; 80395d00d1SAntti Palosaari 8163c80f70SAntti Palosaari if (WARN_ON(len > MAX_RD_LEN)) 8263c80f70SAntti Palosaari return -EINVAL; 8363c80f70SAntti Palosaari 84395d00d1SAntti Palosaari mutex_lock(&priv->i2c_mutex); 85395d00d1SAntti Palosaari ret = i2c_transfer(priv->i2c, msg, 2); 86395d00d1SAntti Palosaari mutex_unlock(&priv->i2c_mutex); 87395d00d1SAntti Palosaari if (ret == 2) { 88395d00d1SAntti Palosaari memcpy(val, buf, len); 89395d00d1SAntti Palosaari ret = 0; 90395d00d1SAntti Palosaari } else { 91395d00d1SAntti Palosaari dev_warn(&priv->i2c->dev, 92395d00d1SAntti Palosaari "%s: i2c rd failed=%d reg=%02x len=%d\n", 93395d00d1SAntti Palosaari KBUILD_MODNAME, ret, reg, len); 94395d00d1SAntti Palosaari ret = -EREMOTEIO; 95395d00d1SAntti Palosaari } 96395d00d1SAntti Palosaari 97395d00d1SAntti Palosaari return ret; 98395d00d1SAntti Palosaari } 99395d00d1SAntti Palosaari 100395d00d1SAntti Palosaari /* write single register */ 101395d00d1SAntti Palosaari static int m88ds3103_wr_reg(struct m88ds3103_priv *priv, u8 reg, u8 val) 102395d00d1SAntti Palosaari { 103395d00d1SAntti Palosaari return m88ds3103_wr_regs(priv, reg, &val, 1); 104395d00d1SAntti Palosaari } 105395d00d1SAntti Palosaari 106395d00d1SAntti Palosaari /* read single register */ 107395d00d1SAntti Palosaari static int m88ds3103_rd_reg(struct m88ds3103_priv *priv, u8 reg, u8 *val) 108395d00d1SAntti Palosaari { 109395d00d1SAntti Palosaari return m88ds3103_rd_regs(priv, reg, val, 1); 110395d00d1SAntti Palosaari } 111395d00d1SAntti Palosaari 112395d00d1SAntti Palosaari /* write single register with mask */ 113395d00d1SAntti Palosaari static int m88ds3103_wr_reg_mask(struct m88ds3103_priv *priv, 114395d00d1SAntti Palosaari u8 reg, u8 val, u8 mask) 115395d00d1SAntti Palosaari { 116395d00d1SAntti Palosaari int ret; 117395d00d1SAntti Palosaari u8 u8tmp; 118395d00d1SAntti Palosaari 119395d00d1SAntti Palosaari /* no need for read if whole reg is written */ 120395d00d1SAntti Palosaari if (mask != 0xff) { 121395d00d1SAntti Palosaari ret = m88ds3103_rd_regs(priv, reg, &u8tmp, 1); 122395d00d1SAntti Palosaari if (ret) 123395d00d1SAntti Palosaari return ret; 124395d00d1SAntti Palosaari 125395d00d1SAntti Palosaari val &= mask; 126395d00d1SAntti Palosaari u8tmp &= ~mask; 127395d00d1SAntti Palosaari val |= u8tmp; 128395d00d1SAntti Palosaari } 129395d00d1SAntti Palosaari 130395d00d1SAntti Palosaari return m88ds3103_wr_regs(priv, reg, &val, 1); 131395d00d1SAntti Palosaari } 132395d00d1SAntti Palosaari 133395d00d1SAntti Palosaari /* read single register with mask */ 134395d00d1SAntti Palosaari static int m88ds3103_rd_reg_mask(struct m88ds3103_priv *priv, 135395d00d1SAntti Palosaari u8 reg, u8 *val, u8 mask) 136395d00d1SAntti Palosaari { 137395d00d1SAntti Palosaari int ret, i; 138395d00d1SAntti Palosaari u8 u8tmp; 139395d00d1SAntti Palosaari 140395d00d1SAntti Palosaari ret = m88ds3103_rd_regs(priv, reg, &u8tmp, 1); 141395d00d1SAntti Palosaari if (ret) 142395d00d1SAntti Palosaari return ret; 143395d00d1SAntti Palosaari 144395d00d1SAntti Palosaari u8tmp &= mask; 145395d00d1SAntti Palosaari 146395d00d1SAntti Palosaari /* find position of the first bit */ 147395d00d1SAntti Palosaari for (i = 0; i < 8; i++) { 148395d00d1SAntti Palosaari if ((mask >> i) & 0x01) 149395d00d1SAntti Palosaari break; 150395d00d1SAntti Palosaari } 151395d00d1SAntti Palosaari *val = u8tmp >> i; 152395d00d1SAntti Palosaari 153395d00d1SAntti Palosaari return 0; 154395d00d1SAntti Palosaari } 155395d00d1SAntti Palosaari 15606487deeSAntti Palosaari /* write reg val table using reg addr auto increment */ 15706487deeSAntti Palosaari static int m88ds3103_wr_reg_val_tab(struct m88ds3103_priv *priv, 15806487deeSAntti Palosaari const struct m88ds3103_reg_val *tab, int tab_len) 15906487deeSAntti Palosaari { 16006487deeSAntti Palosaari int ret, i, j; 16106487deeSAntti Palosaari u8 buf[83]; 16241b9aa00SAntti Palosaari 16306487deeSAntti Palosaari dev_dbg(&priv->i2c->dev, "%s: tab_len=%d\n", __func__, tab_len); 16406487deeSAntti Palosaari 165f4df95bcSnibble.max if (tab_len > 86) { 16606487deeSAntti Palosaari ret = -EINVAL; 16706487deeSAntti Palosaari goto err; 16806487deeSAntti Palosaari } 16906487deeSAntti Palosaari 17006487deeSAntti Palosaari for (i = 0, j = 0; i < tab_len; i++, j++) { 17106487deeSAntti Palosaari buf[j] = tab[i].val; 17206487deeSAntti Palosaari 17306487deeSAntti Palosaari if (i == tab_len - 1 || tab[i].reg != tab[i + 1].reg - 1 || 17406487deeSAntti Palosaari !((j + 1) % (priv->cfg->i2c_wr_max - 1))) { 17506487deeSAntti Palosaari ret = m88ds3103_wr_regs(priv, tab[i].reg - j, buf, j + 1); 17606487deeSAntti Palosaari if (ret) 17706487deeSAntti Palosaari goto err; 17806487deeSAntti Palosaari 17906487deeSAntti Palosaari j = -1; 18006487deeSAntti Palosaari } 18106487deeSAntti Palosaari } 18206487deeSAntti Palosaari 18306487deeSAntti Palosaari return 0; 18406487deeSAntti Palosaari err: 18506487deeSAntti Palosaari dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret); 18606487deeSAntti Palosaari return ret; 18706487deeSAntti Palosaari } 18806487deeSAntti Palosaari 189395d00d1SAntti Palosaari static int m88ds3103_read_status(struct dvb_frontend *fe, fe_status_t *status) 190395d00d1SAntti Palosaari { 191395d00d1SAntti Palosaari struct m88ds3103_priv *priv = fe->demodulator_priv; 192395d00d1SAntti Palosaari struct dtv_frontend_properties *c = &fe->dtv_property_cache; 193c1daf651SAntti Palosaari int ret, i, itmp; 194395d00d1SAntti Palosaari u8 u8tmp; 195c1daf651SAntti Palosaari u8 buf[3]; 196395d00d1SAntti Palosaari 197395d00d1SAntti Palosaari *status = 0; 198395d00d1SAntti Palosaari 199395d00d1SAntti Palosaari if (!priv->warm) { 200395d00d1SAntti Palosaari ret = -EAGAIN; 201395d00d1SAntti Palosaari goto err; 202395d00d1SAntti Palosaari } 203395d00d1SAntti Palosaari 204395d00d1SAntti Palosaari switch (c->delivery_system) { 205395d00d1SAntti Palosaari case SYS_DVBS: 206395d00d1SAntti Palosaari ret = m88ds3103_rd_reg_mask(priv, 0xd1, &u8tmp, 0x07); 207395d00d1SAntti Palosaari if (ret) 208395d00d1SAntti Palosaari goto err; 209395d00d1SAntti Palosaari 210395d00d1SAntti Palosaari if (u8tmp == 0x07) 211395d00d1SAntti Palosaari *status = FE_HAS_SIGNAL | FE_HAS_CARRIER | 212395d00d1SAntti Palosaari FE_HAS_VITERBI | FE_HAS_SYNC | 213395d00d1SAntti Palosaari FE_HAS_LOCK; 214395d00d1SAntti Palosaari break; 215395d00d1SAntti Palosaari case SYS_DVBS2: 216395d00d1SAntti Palosaari ret = m88ds3103_rd_reg_mask(priv, 0x0d, &u8tmp, 0x8f); 217395d00d1SAntti Palosaari if (ret) 218395d00d1SAntti Palosaari goto err; 219395d00d1SAntti Palosaari 220395d00d1SAntti Palosaari if (u8tmp == 0x8f) 221395d00d1SAntti Palosaari *status = FE_HAS_SIGNAL | FE_HAS_CARRIER | 222395d00d1SAntti Palosaari FE_HAS_VITERBI | FE_HAS_SYNC | 223395d00d1SAntti Palosaari FE_HAS_LOCK; 224395d00d1SAntti Palosaari break; 225395d00d1SAntti Palosaari default: 226395d00d1SAntti Palosaari dev_dbg(&priv->i2c->dev, "%s: invalid delivery_system\n", 227395d00d1SAntti Palosaari __func__); 228395d00d1SAntti Palosaari ret = -EINVAL; 229395d00d1SAntti Palosaari goto err; 230395d00d1SAntti Palosaari } 231395d00d1SAntti Palosaari 232395d00d1SAntti Palosaari priv->fe_status = *status; 233395d00d1SAntti Palosaari 234395d00d1SAntti Palosaari dev_dbg(&priv->i2c->dev, "%s: lock=%02x status=%02x\n", 235395d00d1SAntti Palosaari __func__, u8tmp, *status); 236395d00d1SAntti Palosaari 237c1daf651SAntti Palosaari /* CNR */ 238c1daf651SAntti Palosaari if (priv->fe_status & FE_HAS_VITERBI) { 239c1daf651SAntti Palosaari unsigned int cnr, noise, signal, noise_tot, signal_tot; 240c1daf651SAntti Palosaari 241c1daf651SAntti Palosaari cnr = 0; 242c1daf651SAntti Palosaari /* more iterations for more accurate estimation */ 243c1daf651SAntti Palosaari #define M88DS3103_SNR_ITERATIONS 3 244c1daf651SAntti Palosaari 245c1daf651SAntti Palosaari switch (c->delivery_system) { 246c1daf651SAntti Palosaari case SYS_DVBS: 247c1daf651SAntti Palosaari itmp = 0; 248c1daf651SAntti Palosaari 249c1daf651SAntti Palosaari for (i = 0; i < M88DS3103_SNR_ITERATIONS; i++) { 250c1daf651SAntti Palosaari ret = m88ds3103_rd_reg(priv, 0xff, &buf[0]); 251c1daf651SAntti Palosaari if (ret) 252c1daf651SAntti Palosaari goto err; 253c1daf651SAntti Palosaari 254c1daf651SAntti Palosaari itmp += buf[0]; 255c1daf651SAntti Palosaari } 256c1daf651SAntti Palosaari 257c1daf651SAntti Palosaari /* use of single register limits max value to 15 dB */ 258c1daf651SAntti Palosaari /* SNR(X) dB = 10 * ln(X) / ln(10) dB */ 259c1daf651SAntti Palosaari itmp = DIV_ROUND_CLOSEST(itmp, 8 * M88DS3103_SNR_ITERATIONS); 260c1daf651SAntti Palosaari if (itmp) 261c1daf651SAntti Palosaari cnr = div_u64((u64) 10000 * intlog2(itmp), intlog2(10)); 262c1daf651SAntti Palosaari break; 263c1daf651SAntti Palosaari case SYS_DVBS2: 264c1daf651SAntti Palosaari noise_tot = 0; 265c1daf651SAntti Palosaari signal_tot = 0; 266c1daf651SAntti Palosaari 267c1daf651SAntti Palosaari for (i = 0; i < M88DS3103_SNR_ITERATIONS; i++) { 268c1daf651SAntti Palosaari ret = m88ds3103_rd_regs(priv, 0x8c, buf, 3); 269c1daf651SAntti Palosaari if (ret) 270c1daf651SAntti Palosaari goto err; 271c1daf651SAntti Palosaari 272c1daf651SAntti Palosaari noise = buf[1] << 6; /* [13:6] */ 273c1daf651SAntti Palosaari noise |= buf[0] & 0x3f; /* [5:0] */ 274c1daf651SAntti Palosaari noise >>= 2; 275c1daf651SAntti Palosaari signal = buf[2] * buf[2]; 276c1daf651SAntti Palosaari signal >>= 1; 277c1daf651SAntti Palosaari 278c1daf651SAntti Palosaari noise_tot += noise; 279c1daf651SAntti Palosaari signal_tot += signal; 280c1daf651SAntti Palosaari } 281c1daf651SAntti Palosaari 282c1daf651SAntti Palosaari noise = noise_tot / M88DS3103_SNR_ITERATIONS; 283c1daf651SAntti Palosaari signal = signal_tot / M88DS3103_SNR_ITERATIONS; 284c1daf651SAntti Palosaari 285c1daf651SAntti Palosaari /* SNR(X) dB = 10 * log10(X) dB */ 286c1daf651SAntti Palosaari if (signal > noise) { 287c1daf651SAntti Palosaari itmp = signal / noise; 288c1daf651SAntti Palosaari cnr = div_u64((u64) 10000 * intlog10(itmp), (1 << 24)); 289c1daf651SAntti Palosaari } 290c1daf651SAntti Palosaari break; 291c1daf651SAntti Palosaari default: 292c1daf651SAntti Palosaari dev_dbg(&priv->i2c->dev, 293c1daf651SAntti Palosaari "%s: invalid delivery_system\n", __func__); 294c1daf651SAntti Palosaari ret = -EINVAL; 295c1daf651SAntti Palosaari goto err; 296c1daf651SAntti Palosaari } 297c1daf651SAntti Palosaari 298c1daf651SAntti Palosaari if (cnr) { 299c1daf651SAntti Palosaari c->cnr.stat[0].scale = FE_SCALE_DECIBEL; 300c1daf651SAntti Palosaari c->cnr.stat[0].svalue = cnr; 301c1daf651SAntti Palosaari } else { 302c1daf651SAntti Palosaari c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE; 303c1daf651SAntti Palosaari } 304c1daf651SAntti Palosaari } else { 305c1daf651SAntti Palosaari c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE; 306c1daf651SAntti Palosaari } 307c1daf651SAntti Palosaari 308ce80d713SAntti Palosaari /* BER */ 309ce80d713SAntti Palosaari if (priv->fe_status & FE_HAS_LOCK) { 310ce80d713SAntti Palosaari unsigned int utmp, post_bit_error, post_bit_count; 311ce80d713SAntti Palosaari 312ce80d713SAntti Palosaari switch (c->delivery_system) { 313ce80d713SAntti Palosaari case SYS_DVBS: 314ce80d713SAntti Palosaari ret = m88ds3103_wr_reg(priv, 0xf9, 0x04); 315ce80d713SAntti Palosaari if (ret) 316ce80d713SAntti Palosaari goto err; 317ce80d713SAntti Palosaari 318ce80d713SAntti Palosaari ret = m88ds3103_rd_reg(priv, 0xf8, &u8tmp); 319ce80d713SAntti Palosaari if (ret) 320ce80d713SAntti Palosaari goto err; 321ce80d713SAntti Palosaari 322ce80d713SAntti Palosaari /* measurement ready? */ 323ce80d713SAntti Palosaari if (!(u8tmp & 0x10)) { 324ce80d713SAntti Palosaari ret = m88ds3103_rd_regs(priv, 0xf6, buf, 2); 325ce80d713SAntti Palosaari if (ret) 326ce80d713SAntti Palosaari goto err; 327ce80d713SAntti Palosaari 328ce80d713SAntti Palosaari post_bit_error = buf[1] << 8 | buf[0] << 0; 329ce80d713SAntti Palosaari post_bit_count = 0x800000; 330ce80d713SAntti Palosaari priv->post_bit_error += post_bit_error; 331ce80d713SAntti Palosaari priv->post_bit_count += post_bit_count; 332ce80d713SAntti Palosaari priv->dvbv3_ber = post_bit_error; 333ce80d713SAntti Palosaari 334ce80d713SAntti Palosaari /* restart measurement */ 335ce80d713SAntti Palosaari u8tmp |= 0x10; 336ce80d713SAntti Palosaari ret = m88ds3103_wr_reg(priv, 0xf8, u8tmp); 337ce80d713SAntti Palosaari if (ret) 338ce80d713SAntti Palosaari goto err; 339ce80d713SAntti Palosaari } 340ce80d713SAntti Palosaari break; 341ce80d713SAntti Palosaari case SYS_DVBS2: 342ce80d713SAntti Palosaari ret = m88ds3103_rd_regs(priv, 0xd5, buf, 3); 343ce80d713SAntti Palosaari if (ret) 344ce80d713SAntti Palosaari goto err; 345ce80d713SAntti Palosaari 346ce80d713SAntti Palosaari utmp = buf[2] << 16 | buf[1] << 8 | buf[0] << 0; 347ce80d713SAntti Palosaari 348ce80d713SAntti Palosaari /* enough data? */ 349ce80d713SAntti Palosaari if (utmp > 4000) { 350ce80d713SAntti Palosaari ret = m88ds3103_rd_regs(priv, 0xf7, buf, 2); 351ce80d713SAntti Palosaari if (ret) 352ce80d713SAntti Palosaari goto err; 353ce80d713SAntti Palosaari 354ce80d713SAntti Palosaari post_bit_error = buf[1] << 8 | buf[0] << 0; 355ce80d713SAntti Palosaari post_bit_count = 32 * utmp; /* TODO: FEC */ 356ce80d713SAntti Palosaari priv->post_bit_error += post_bit_error; 357ce80d713SAntti Palosaari priv->post_bit_count += post_bit_count; 358ce80d713SAntti Palosaari priv->dvbv3_ber = post_bit_error; 359ce80d713SAntti Palosaari 360ce80d713SAntti Palosaari /* restart measurement */ 361ce80d713SAntti Palosaari ret = m88ds3103_wr_reg(priv, 0xd1, 0x01); 362ce80d713SAntti Palosaari if (ret) 363ce80d713SAntti Palosaari goto err; 364ce80d713SAntti Palosaari 365ce80d713SAntti Palosaari ret = m88ds3103_wr_reg(priv, 0xf9, 0x01); 366ce80d713SAntti Palosaari if (ret) 367ce80d713SAntti Palosaari goto err; 368ce80d713SAntti Palosaari 369ce80d713SAntti Palosaari ret = m88ds3103_wr_reg(priv, 0xf9, 0x00); 370ce80d713SAntti Palosaari if (ret) 371ce80d713SAntti Palosaari goto err; 372ce80d713SAntti Palosaari 373ce80d713SAntti Palosaari ret = m88ds3103_wr_reg(priv, 0xd1, 0x00); 374ce80d713SAntti Palosaari if (ret) 375ce80d713SAntti Palosaari goto err; 376ce80d713SAntti Palosaari } 377ce80d713SAntti Palosaari break; 378ce80d713SAntti Palosaari default: 379ce80d713SAntti Palosaari dev_dbg(&priv->i2c->dev, 380ce80d713SAntti Palosaari "%s: invalid delivery_system\n", __func__); 381ce80d713SAntti Palosaari ret = -EINVAL; 382ce80d713SAntti Palosaari goto err; 383ce80d713SAntti Palosaari } 384ce80d713SAntti Palosaari 385ce80d713SAntti Palosaari c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER; 386ce80d713SAntti Palosaari c->post_bit_error.stat[0].uvalue = priv->post_bit_error; 387ce80d713SAntti Palosaari c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER; 388ce80d713SAntti Palosaari c->post_bit_count.stat[0].uvalue = priv->post_bit_count; 389ce80d713SAntti Palosaari } else { 390ce80d713SAntti Palosaari c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE; 391ce80d713SAntti Palosaari c->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE; 392ce80d713SAntti Palosaari } 393ce80d713SAntti Palosaari 394395d00d1SAntti Palosaari return 0; 395395d00d1SAntti Palosaari err: 396395d00d1SAntti Palosaari dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret); 397395d00d1SAntti Palosaari return ret; 398395d00d1SAntti Palosaari } 399395d00d1SAntti Palosaari 400395d00d1SAntti Palosaari static int m88ds3103_set_frontend(struct dvb_frontend *fe) 401395d00d1SAntti Palosaari { 402395d00d1SAntti Palosaari struct m88ds3103_priv *priv = fe->demodulator_priv; 403395d00d1SAntti Palosaari struct dtv_frontend_properties *c = &fe->dtv_property_cache; 40406487deeSAntti Palosaari int ret, len; 405395d00d1SAntti Palosaari const struct m88ds3103_reg_val *init; 406b6851419Snibble.max u8 u8tmp, u8tmp1 = 0, u8tmp2 = 0; /* silence compiler warning */ 407f4df95bcSnibble.max u8 buf[3]; 408b6851419Snibble.max u16 u16tmp, divide_ratio = 0; 40979d09330Snibble.max u32 tuner_frequency, target_mclk; 410395d00d1SAntti Palosaari s32 s32tmp; 41141b9aa00SAntti Palosaari 412395d00d1SAntti Palosaari dev_dbg(&priv->i2c->dev, 413395d00d1SAntti Palosaari "%s: delivery_system=%d modulation=%d frequency=%d symbol_rate=%d inversion=%d pilot=%d rolloff=%d\n", 414395d00d1SAntti Palosaari __func__, c->delivery_system, 415395d00d1SAntti Palosaari c->modulation, c->frequency, c->symbol_rate, 416395d00d1SAntti Palosaari c->inversion, c->pilot, c->rolloff); 417395d00d1SAntti Palosaari 418395d00d1SAntti Palosaari if (!priv->warm) { 419395d00d1SAntti Palosaari ret = -EAGAIN; 420395d00d1SAntti Palosaari goto err; 421395d00d1SAntti Palosaari } 422395d00d1SAntti Palosaari 423f4df95bcSnibble.max /* reset */ 424f4df95bcSnibble.max ret = m88ds3103_wr_reg(priv, 0x07, 0x80); 425f4df95bcSnibble.max if (ret) 426f4df95bcSnibble.max goto err; 427f4df95bcSnibble.max 428f4df95bcSnibble.max ret = m88ds3103_wr_reg(priv, 0x07, 0x00); 429f4df95bcSnibble.max if (ret) 430f4df95bcSnibble.max goto err; 431f4df95bcSnibble.max 432f4df95bcSnibble.max /* Disable demod clock path */ 433f4df95bcSnibble.max if (priv->chip_id == M88RS6000_CHIP_ID) { 434f4df95bcSnibble.max ret = m88ds3103_wr_reg(priv, 0x06, 0xe0); 435f4df95bcSnibble.max if (ret) 436f4df95bcSnibble.max goto err; 437f4df95bcSnibble.max } 438f4df95bcSnibble.max 439395d00d1SAntti Palosaari /* program tuner */ 440395d00d1SAntti Palosaari if (fe->ops.tuner_ops.set_params) { 441395d00d1SAntti Palosaari ret = fe->ops.tuner_ops.set_params(fe); 442395d00d1SAntti Palosaari if (ret) 443395d00d1SAntti Palosaari goto err; 444395d00d1SAntti Palosaari } 445395d00d1SAntti Palosaari 446395d00d1SAntti Palosaari if (fe->ops.tuner_ops.get_frequency) { 447395d00d1SAntti Palosaari ret = fe->ops.tuner_ops.get_frequency(fe, &tuner_frequency); 448395d00d1SAntti Palosaari if (ret) 449395d00d1SAntti Palosaari goto err; 4502f9dff3fSAntti Palosaari } else { 4512f9dff3fSAntti Palosaari /* 4522f9dff3fSAntti Palosaari * Use nominal target frequency as tuner driver does not provide 4532f9dff3fSAntti Palosaari * actual frequency used. Carrier offset calculation is not 4542f9dff3fSAntti Palosaari * valid. 4552f9dff3fSAntti Palosaari */ 4562f9dff3fSAntti Palosaari tuner_frequency = c->frequency; 457395d00d1SAntti Palosaari } 458395d00d1SAntti Palosaari 459f4df95bcSnibble.max /* select M88RS6000 demod main mclk and ts mclk from tuner die. */ 460f4df95bcSnibble.max if (priv->chip_id == M88RS6000_CHIP_ID) { 461f4df95bcSnibble.max if (c->symbol_rate > 45010000) 462f4df95bcSnibble.max priv->mclk_khz = 110250; 463f4df95bcSnibble.max else 464f4df95bcSnibble.max priv->mclk_khz = 96000; 465395d00d1SAntti Palosaari 466f4df95bcSnibble.max if (c->delivery_system == SYS_DVBS) 467395d00d1SAntti Palosaari target_mclk = 96000; 468f4df95bcSnibble.max else 469f4df95bcSnibble.max target_mclk = 144000; 470395d00d1SAntti Palosaari 471f4df95bcSnibble.max /* Enable demod clock path */ 472f4df95bcSnibble.max ret = m88ds3103_wr_reg(priv, 0x06, 0x00); 473f4df95bcSnibble.max if (ret) 474f4df95bcSnibble.max goto err; 475f4df95bcSnibble.max usleep_range(10000, 20000); 476f4df95bcSnibble.max } else { 477f4df95bcSnibble.max /* set M88DS3103 mclk and ts mclk. */ 478f4df95bcSnibble.max priv->mclk_khz = 96000; 479f4df95bcSnibble.max 480395d00d1SAntti Palosaari switch (priv->cfg->ts_mode) { 481395d00d1SAntti Palosaari case M88DS3103_TS_SERIAL: 482395d00d1SAntti Palosaari case M88DS3103_TS_SERIAL_D7: 483b6851419Snibble.max target_mclk = priv->cfg->ts_clk; 484395d00d1SAntti Palosaari break; 485395d00d1SAntti Palosaari case M88DS3103_TS_PARALLEL: 486395d00d1SAntti Palosaari case M88DS3103_TS_CI: 487b6851419Snibble.max if (c->delivery_system == SYS_DVBS) 488b6851419Snibble.max target_mclk = 96000; 489b6851419Snibble.max else { 490395d00d1SAntti Palosaari if (c->symbol_rate < 18000000) 491395d00d1SAntti Palosaari target_mclk = 96000; 492395d00d1SAntti Palosaari else if (c->symbol_rate < 28000000) 493395d00d1SAntti Palosaari target_mclk = 144000; 494395d00d1SAntti Palosaari else 495395d00d1SAntti Palosaari target_mclk = 192000; 496b6851419Snibble.max } 497395d00d1SAntti Palosaari break; 498395d00d1SAntti Palosaari default: 499395d00d1SAntti Palosaari dev_dbg(&priv->i2c->dev, "%s: invalid ts_mode\n", 500395d00d1SAntti Palosaari __func__); 501395d00d1SAntti Palosaari ret = -EINVAL; 502395d00d1SAntti Palosaari goto err; 503395d00d1SAntti Palosaari } 504f4df95bcSnibble.max 505f4df95bcSnibble.max switch (target_mclk) { 506f4df95bcSnibble.max case 96000: 507f4df95bcSnibble.max u8tmp1 = 0x02; /* 0b10 */ 508f4df95bcSnibble.max u8tmp2 = 0x01; /* 0b01 */ 509f4df95bcSnibble.max break; 510f4df95bcSnibble.max case 144000: 511f4df95bcSnibble.max u8tmp1 = 0x00; /* 0b00 */ 512f4df95bcSnibble.max u8tmp2 = 0x01; /* 0b01 */ 513f4df95bcSnibble.max break; 514f4df95bcSnibble.max case 192000: 515f4df95bcSnibble.max u8tmp1 = 0x03; /* 0b11 */ 516f4df95bcSnibble.max u8tmp2 = 0x00; /* 0b00 */ 517f4df95bcSnibble.max break; 518f4df95bcSnibble.max } 519f4df95bcSnibble.max ret = m88ds3103_wr_reg_mask(priv, 0x22, u8tmp1 << 6, 0xc0); 520f4df95bcSnibble.max if (ret) 521f4df95bcSnibble.max goto err; 522f4df95bcSnibble.max ret = m88ds3103_wr_reg_mask(priv, 0x24, u8tmp2 << 6, 0xc0); 523f4df95bcSnibble.max if (ret) 524f4df95bcSnibble.max goto err; 525f4df95bcSnibble.max } 526f4df95bcSnibble.max 527f4df95bcSnibble.max ret = m88ds3103_wr_reg(priv, 0xb2, 0x01); 528f4df95bcSnibble.max if (ret) 529f4df95bcSnibble.max goto err; 530f4df95bcSnibble.max 531f4df95bcSnibble.max ret = m88ds3103_wr_reg(priv, 0x00, 0x01); 532f4df95bcSnibble.max if (ret) 533f4df95bcSnibble.max goto err; 534f4df95bcSnibble.max 535f4df95bcSnibble.max switch (c->delivery_system) { 536f4df95bcSnibble.max case SYS_DVBS: 537f4df95bcSnibble.max if (priv->chip_id == M88RS6000_CHIP_ID) { 538f4df95bcSnibble.max len = ARRAY_SIZE(m88rs6000_dvbs_init_reg_vals); 539f4df95bcSnibble.max init = m88rs6000_dvbs_init_reg_vals; 540f4df95bcSnibble.max } else { 541f4df95bcSnibble.max len = ARRAY_SIZE(m88ds3103_dvbs_init_reg_vals); 542f4df95bcSnibble.max init = m88ds3103_dvbs_init_reg_vals; 543f4df95bcSnibble.max } 544f4df95bcSnibble.max break; 545f4df95bcSnibble.max case SYS_DVBS2: 546f4df95bcSnibble.max if (priv->chip_id == M88RS6000_CHIP_ID) { 547f4df95bcSnibble.max len = ARRAY_SIZE(m88rs6000_dvbs2_init_reg_vals); 548f4df95bcSnibble.max init = m88rs6000_dvbs2_init_reg_vals; 549f4df95bcSnibble.max } else { 550f4df95bcSnibble.max len = ARRAY_SIZE(m88ds3103_dvbs2_init_reg_vals); 551f4df95bcSnibble.max init = m88ds3103_dvbs2_init_reg_vals; 552f4df95bcSnibble.max } 553395d00d1SAntti Palosaari break; 554395d00d1SAntti Palosaari default: 555395d00d1SAntti Palosaari dev_dbg(&priv->i2c->dev, "%s: invalid delivery_system\n", 556395d00d1SAntti Palosaari __func__); 557395d00d1SAntti Palosaari ret = -EINVAL; 558395d00d1SAntti Palosaari goto err; 559395d00d1SAntti Palosaari } 560395d00d1SAntti Palosaari 561395d00d1SAntti Palosaari /* program init table */ 562395d00d1SAntti Palosaari if (c->delivery_system != priv->delivery_system) { 56306487deeSAntti Palosaari ret = m88ds3103_wr_reg_val_tab(priv, init, len); 564395d00d1SAntti Palosaari if (ret) 565395d00d1SAntti Palosaari goto err; 566395d00d1SAntti Palosaari } 567395d00d1SAntti Palosaari 568f4df95bcSnibble.max if (priv->chip_id == M88RS6000_CHIP_ID) { 569f4df95bcSnibble.max if ((c->delivery_system == SYS_DVBS2) 570f4df95bcSnibble.max && ((c->symbol_rate / 1000) <= 5000)) { 571f4df95bcSnibble.max ret = m88ds3103_wr_reg(priv, 0xc0, 0x04); 572f4df95bcSnibble.max if (ret) 573f4df95bcSnibble.max goto err; 574f4df95bcSnibble.max buf[0] = 0x09; 575f4df95bcSnibble.max buf[1] = 0x22; 576f4df95bcSnibble.max buf[2] = 0x88; 577f4df95bcSnibble.max ret = m88ds3103_wr_regs(priv, 0x8a, buf, 3); 578f4df95bcSnibble.max if (ret) 579f4df95bcSnibble.max goto err; 580f4df95bcSnibble.max } 581f4df95bcSnibble.max ret = m88ds3103_wr_reg_mask(priv, 0x9d, 0x08, 0x08); 582f4df95bcSnibble.max if (ret) 583f4df95bcSnibble.max goto err; 584f4df95bcSnibble.max ret = m88ds3103_wr_reg(priv, 0xf1, 0x01); 585f4df95bcSnibble.max if (ret) 586f4df95bcSnibble.max goto err; 587f4df95bcSnibble.max ret = m88ds3103_wr_reg_mask(priv, 0x30, 0x80, 0x80); 588f4df95bcSnibble.max if (ret) 589f4df95bcSnibble.max goto err; 590f4df95bcSnibble.max } 591f4df95bcSnibble.max 592395d00d1SAntti Palosaari switch (priv->cfg->ts_mode) { 593395d00d1SAntti Palosaari case M88DS3103_TS_SERIAL: 594395d00d1SAntti Palosaari u8tmp1 = 0x00; 59579d09330Snibble.max u8tmp = 0x06; 596395d00d1SAntti Palosaari break; 597395d00d1SAntti Palosaari case M88DS3103_TS_SERIAL_D7: 598395d00d1SAntti Palosaari u8tmp1 = 0x20; 59979d09330Snibble.max u8tmp = 0x06; 600395d00d1SAntti Palosaari break; 601395d00d1SAntti Palosaari case M88DS3103_TS_PARALLEL: 60279d09330Snibble.max u8tmp = 0x02; 603395d00d1SAntti Palosaari break; 604395d00d1SAntti Palosaari case M88DS3103_TS_CI: 60579d09330Snibble.max u8tmp = 0x03; 606395d00d1SAntti Palosaari break; 607395d00d1SAntti Palosaari default: 608395d00d1SAntti Palosaari dev_dbg(&priv->i2c->dev, "%s: invalid ts_mode\n", __func__); 609395d00d1SAntti Palosaari ret = -EINVAL; 610395d00d1SAntti Palosaari goto err; 611395d00d1SAntti Palosaari } 612395d00d1SAntti Palosaari 61379d09330Snibble.max if (priv->cfg->ts_clk_pol) 61479d09330Snibble.max u8tmp |= 0x40; 61579d09330Snibble.max 616395d00d1SAntti Palosaari /* TS mode */ 61792676ac9SAntti Palosaari ret = m88ds3103_wr_reg(priv, 0xfd, u8tmp); 618395d00d1SAntti Palosaari if (ret) 619395d00d1SAntti Palosaari goto err; 620395d00d1SAntti Palosaari 621395d00d1SAntti Palosaari switch (priv->cfg->ts_mode) { 622395d00d1SAntti Palosaari case M88DS3103_TS_SERIAL: 623395d00d1SAntti Palosaari case M88DS3103_TS_SERIAL_D7: 624395d00d1SAntti Palosaari ret = m88ds3103_wr_reg_mask(priv, 0x29, u8tmp1, 0x20); 625395d00d1SAntti Palosaari if (ret) 626395d00d1SAntti Palosaari goto err; 627b6851419Snibble.max u8tmp1 = 0; 628b6851419Snibble.max u8tmp2 = 0; 629b6851419Snibble.max break; 630b6851419Snibble.max default: 63179d09330Snibble.max if (priv->cfg->ts_clk) { 63279d09330Snibble.max divide_ratio = DIV_ROUND_UP(target_mclk, priv->cfg->ts_clk); 633395d00d1SAntti Palosaari u8tmp1 = divide_ratio / 2; 634395d00d1SAntti Palosaari u8tmp2 = DIV_ROUND_UP(divide_ratio, 2); 635b6851419Snibble.max } 636395d00d1SAntti Palosaari } 637395d00d1SAntti Palosaari 638395d00d1SAntti Palosaari dev_dbg(&priv->i2c->dev, 639395d00d1SAntti Palosaari "%s: target_mclk=%d ts_clk=%d divide_ratio=%d\n", 64079d09330Snibble.max __func__, target_mclk, priv->cfg->ts_clk, divide_ratio); 641395d00d1SAntti Palosaari 642395d00d1SAntti Palosaari u8tmp1--; 643395d00d1SAntti Palosaari u8tmp2--; 644395d00d1SAntti Palosaari /* u8tmp1[5:2] => fe[3:0], u8tmp1[1:0] => ea[7:6] */ 645395d00d1SAntti Palosaari u8tmp1 &= 0x3f; 646395d00d1SAntti Palosaari /* u8tmp2[5:0] => ea[5:0] */ 647395d00d1SAntti Palosaari u8tmp2 &= 0x3f; 648395d00d1SAntti Palosaari 649395d00d1SAntti Palosaari ret = m88ds3103_rd_reg(priv, 0xfe, &u8tmp); 650395d00d1SAntti Palosaari if (ret) 651395d00d1SAntti Palosaari goto err; 652395d00d1SAntti Palosaari 653395d00d1SAntti Palosaari u8tmp = ((u8tmp & 0xf0) << 0) | u8tmp1 >> 2; 654395d00d1SAntti Palosaari ret = m88ds3103_wr_reg(priv, 0xfe, u8tmp); 655395d00d1SAntti Palosaari if (ret) 656395d00d1SAntti Palosaari goto err; 657395d00d1SAntti Palosaari 658395d00d1SAntti Palosaari u8tmp = ((u8tmp1 & 0x03) << 6) | u8tmp2 >> 0; 659395d00d1SAntti Palosaari ret = m88ds3103_wr_reg(priv, 0xea, u8tmp); 660395d00d1SAntti Palosaari if (ret) 661395d00d1SAntti Palosaari goto err; 662395d00d1SAntti Palosaari 663395d00d1SAntti Palosaari if (c->symbol_rate <= 3000000) 664395d00d1SAntti Palosaari u8tmp = 0x20; 665395d00d1SAntti Palosaari else if (c->symbol_rate <= 10000000) 666395d00d1SAntti Palosaari u8tmp = 0x10; 667395d00d1SAntti Palosaari else 668395d00d1SAntti Palosaari u8tmp = 0x06; 669395d00d1SAntti Palosaari 670395d00d1SAntti Palosaari ret = m88ds3103_wr_reg(priv, 0xc3, 0x08); 671395d00d1SAntti Palosaari if (ret) 672395d00d1SAntti Palosaari goto err; 673395d00d1SAntti Palosaari 674395d00d1SAntti Palosaari ret = m88ds3103_wr_reg(priv, 0xc8, u8tmp); 675395d00d1SAntti Palosaari if (ret) 676395d00d1SAntti Palosaari goto err; 677395d00d1SAntti Palosaari 678395d00d1SAntti Palosaari ret = m88ds3103_wr_reg(priv, 0xc4, 0x08); 679395d00d1SAntti Palosaari if (ret) 680395d00d1SAntti Palosaari goto err; 681395d00d1SAntti Palosaari 682395d00d1SAntti Palosaari ret = m88ds3103_wr_reg(priv, 0xc7, 0x00); 683395d00d1SAntti Palosaari if (ret) 684395d00d1SAntti Palosaari goto err; 685395d00d1SAntti Palosaari 686f4df95bcSnibble.max u16tmp = DIV_ROUND_CLOSEST((c->symbol_rate / 1000) << 15, priv->mclk_khz / 2); 687395d00d1SAntti Palosaari buf[0] = (u16tmp >> 0) & 0xff; 688395d00d1SAntti Palosaari buf[1] = (u16tmp >> 8) & 0xff; 689395d00d1SAntti Palosaari ret = m88ds3103_wr_regs(priv, 0x61, buf, 2); 690395d00d1SAntti Palosaari if (ret) 691395d00d1SAntti Palosaari goto err; 692395d00d1SAntti Palosaari 693395d00d1SAntti Palosaari ret = m88ds3103_wr_reg_mask(priv, 0x4d, priv->cfg->spec_inv << 1, 0x02); 694395d00d1SAntti Palosaari if (ret) 695395d00d1SAntti Palosaari goto err; 696395d00d1SAntti Palosaari 697395d00d1SAntti Palosaari ret = m88ds3103_wr_reg_mask(priv, 0x30, priv->cfg->agc_inv << 4, 0x10); 698395d00d1SAntti Palosaari if (ret) 699395d00d1SAntti Palosaari goto err; 700395d00d1SAntti Palosaari 701395d00d1SAntti Palosaari ret = m88ds3103_wr_reg(priv, 0x33, priv->cfg->agc); 702395d00d1SAntti Palosaari if (ret) 703395d00d1SAntti Palosaari goto err; 704395d00d1SAntti Palosaari 705395d00d1SAntti Palosaari dev_dbg(&priv->i2c->dev, "%s: carrier offset=%d\n", __func__, 706395d00d1SAntti Palosaari (tuner_frequency - c->frequency)); 707395d00d1SAntti Palosaari 708395d00d1SAntti Palosaari s32tmp = 0x10000 * (tuner_frequency - c->frequency); 709f4df95bcSnibble.max s32tmp = DIV_ROUND_CLOSEST(s32tmp, priv->mclk_khz); 710395d00d1SAntti Palosaari if (s32tmp < 0) 711395d00d1SAntti Palosaari s32tmp += 0x10000; 712395d00d1SAntti Palosaari 713395d00d1SAntti Palosaari buf[0] = (s32tmp >> 0) & 0xff; 714395d00d1SAntti Palosaari buf[1] = (s32tmp >> 8) & 0xff; 715395d00d1SAntti Palosaari ret = m88ds3103_wr_regs(priv, 0x5e, buf, 2); 716395d00d1SAntti Palosaari if (ret) 717395d00d1SAntti Palosaari goto err; 718395d00d1SAntti Palosaari 719395d00d1SAntti Palosaari ret = m88ds3103_wr_reg(priv, 0x00, 0x00); 720395d00d1SAntti Palosaari if (ret) 721395d00d1SAntti Palosaari goto err; 722395d00d1SAntti Palosaari 723395d00d1SAntti Palosaari ret = m88ds3103_wr_reg(priv, 0xb2, 0x00); 724395d00d1SAntti Palosaari if (ret) 725395d00d1SAntti Palosaari goto err; 726395d00d1SAntti Palosaari 727395d00d1SAntti Palosaari priv->delivery_system = c->delivery_system; 728395d00d1SAntti Palosaari 729395d00d1SAntti Palosaari return 0; 730395d00d1SAntti Palosaari err: 731395d00d1SAntti Palosaari dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret); 732395d00d1SAntti Palosaari return ret; 733395d00d1SAntti Palosaari } 734395d00d1SAntti Palosaari 735395d00d1SAntti Palosaari static int m88ds3103_init(struct dvb_frontend *fe) 736395d00d1SAntti Palosaari { 737395d00d1SAntti Palosaari struct m88ds3103_priv *priv = fe->demodulator_priv; 738c1daf651SAntti Palosaari struct dtv_frontend_properties *c = &fe->dtv_property_cache; 739395d00d1SAntti Palosaari int ret, len, remaining; 740395d00d1SAntti Palosaari const struct firmware *fw = NULL; 741f4df95bcSnibble.max u8 *fw_file; 742395d00d1SAntti Palosaari u8 u8tmp; 74341b9aa00SAntti Palosaari 744395d00d1SAntti Palosaari dev_dbg(&priv->i2c->dev, "%s:\n", __func__); 745395d00d1SAntti Palosaari 746395d00d1SAntti Palosaari /* set cold state by default */ 747395d00d1SAntti Palosaari priv->warm = false; 748395d00d1SAntti Palosaari 749395d00d1SAntti Palosaari /* wake up device from sleep */ 750395d00d1SAntti Palosaari ret = m88ds3103_wr_reg_mask(priv, 0x08, 0x01, 0x01); 751395d00d1SAntti Palosaari if (ret) 752395d00d1SAntti Palosaari goto err; 753395d00d1SAntti Palosaari 754395d00d1SAntti Palosaari ret = m88ds3103_wr_reg_mask(priv, 0x04, 0x00, 0x01); 755395d00d1SAntti Palosaari if (ret) 756395d00d1SAntti Palosaari goto err; 757395d00d1SAntti Palosaari 758395d00d1SAntti Palosaari ret = m88ds3103_wr_reg_mask(priv, 0x23, 0x00, 0x10); 759395d00d1SAntti Palosaari if (ret) 760395d00d1SAntti Palosaari goto err; 761395d00d1SAntti Palosaari 762395d00d1SAntti Palosaari /* firmware status */ 763395d00d1SAntti Palosaari ret = m88ds3103_rd_reg(priv, 0xb9, &u8tmp); 764395d00d1SAntti Palosaari if (ret) 765395d00d1SAntti Palosaari goto err; 766395d00d1SAntti Palosaari 767395d00d1SAntti Palosaari dev_dbg(&priv->i2c->dev, "%s: firmware=%02x\n", __func__, u8tmp); 768395d00d1SAntti Palosaari 769395d00d1SAntti Palosaari if (u8tmp) 770395d00d1SAntti Palosaari goto skip_fw_download; 771395d00d1SAntti Palosaari 772f4df95bcSnibble.max /* global reset, global diseqc reset, golbal fec reset */ 773f4df95bcSnibble.max ret = m88ds3103_wr_reg(priv, 0x07, 0xe0); 774f4df95bcSnibble.max if (ret) 775f4df95bcSnibble.max goto err; 776f4df95bcSnibble.max 777f4df95bcSnibble.max ret = m88ds3103_wr_reg(priv, 0x07, 0x00); 778f4df95bcSnibble.max if (ret) 779f4df95bcSnibble.max goto err; 780f4df95bcSnibble.max 781395d00d1SAntti Palosaari /* cold state - try to download firmware */ 782395d00d1SAntti Palosaari dev_info(&priv->i2c->dev, "%s: found a '%s' in cold state\n", 783395d00d1SAntti Palosaari KBUILD_MODNAME, m88ds3103_ops.info.name); 784395d00d1SAntti Palosaari 785f4df95bcSnibble.max if (priv->chip_id == M88RS6000_CHIP_ID) 786f4df95bcSnibble.max fw_file = M88RS6000_FIRMWARE; 787f4df95bcSnibble.max else 788f4df95bcSnibble.max fw_file = M88DS3103_FIRMWARE; 789395d00d1SAntti Palosaari /* request the firmware, this will block and timeout */ 790395d00d1SAntti Palosaari ret = request_firmware(&fw, fw_file, priv->i2c->dev.parent); 791395d00d1SAntti Palosaari if (ret) { 792a87a4d34SYannick Guerrini dev_err(&priv->i2c->dev, "%s: firmware file '%s' not found\n", 793395d00d1SAntti Palosaari KBUILD_MODNAME, fw_file); 794395d00d1SAntti Palosaari goto err; 795395d00d1SAntti Palosaari } 796395d00d1SAntti Palosaari 797395d00d1SAntti Palosaari dev_info(&priv->i2c->dev, "%s: downloading firmware from file '%s'\n", 798395d00d1SAntti Palosaari KBUILD_MODNAME, fw_file); 799395d00d1SAntti Palosaari 800395d00d1SAntti Palosaari ret = m88ds3103_wr_reg(priv, 0xb2, 0x01); 801395d00d1SAntti Palosaari if (ret) 8025ed0cf88SMarkus Elfring goto error_fw_release; 803395d00d1SAntti Palosaari 804395d00d1SAntti Palosaari for (remaining = fw->size; remaining > 0; 805395d00d1SAntti Palosaari remaining -= (priv->cfg->i2c_wr_max - 1)) { 806395d00d1SAntti Palosaari len = remaining; 807395d00d1SAntti Palosaari if (len > (priv->cfg->i2c_wr_max - 1)) 808395d00d1SAntti Palosaari len = (priv->cfg->i2c_wr_max - 1); 809395d00d1SAntti Palosaari 810395d00d1SAntti Palosaari ret = m88ds3103_wr_regs(priv, 0xb0, 811395d00d1SAntti Palosaari &fw->data[fw->size - remaining], len); 812395d00d1SAntti Palosaari if (ret) { 813395d00d1SAntti Palosaari dev_err(&priv->i2c->dev, 814395d00d1SAntti Palosaari "%s: firmware download failed=%d\n", 815395d00d1SAntti Palosaari KBUILD_MODNAME, ret); 8165ed0cf88SMarkus Elfring goto error_fw_release; 817395d00d1SAntti Palosaari } 818395d00d1SAntti Palosaari } 819395d00d1SAntti Palosaari 820395d00d1SAntti Palosaari ret = m88ds3103_wr_reg(priv, 0xb2, 0x00); 821395d00d1SAntti Palosaari if (ret) 8225ed0cf88SMarkus Elfring goto error_fw_release; 823395d00d1SAntti Palosaari 824395d00d1SAntti Palosaari release_firmware(fw); 825395d00d1SAntti Palosaari fw = NULL; 826395d00d1SAntti Palosaari 827395d00d1SAntti Palosaari ret = m88ds3103_rd_reg(priv, 0xb9, &u8tmp); 828395d00d1SAntti Palosaari if (ret) 829395d00d1SAntti Palosaari goto err; 830395d00d1SAntti Palosaari 831395d00d1SAntti Palosaari if (!u8tmp) { 832395d00d1SAntti Palosaari dev_info(&priv->i2c->dev, "%s: firmware did not run\n", 833395d00d1SAntti Palosaari KBUILD_MODNAME); 834395d00d1SAntti Palosaari ret = -EFAULT; 835395d00d1SAntti Palosaari goto err; 836395d00d1SAntti Palosaari } 837395d00d1SAntti Palosaari 838395d00d1SAntti Palosaari dev_info(&priv->i2c->dev, "%s: found a '%s' in warm state\n", 839395d00d1SAntti Palosaari KBUILD_MODNAME, m88ds3103_ops.info.name); 840395d00d1SAntti Palosaari dev_info(&priv->i2c->dev, "%s: firmware version %X.%X\n", 841395d00d1SAntti Palosaari KBUILD_MODNAME, (u8tmp >> 4) & 0xf, (u8tmp >> 0 & 0xf)); 842395d00d1SAntti Palosaari 843395d00d1SAntti Palosaari skip_fw_download: 844395d00d1SAntti Palosaari /* warm state */ 845395d00d1SAntti Palosaari priv->warm = true; 846c1daf651SAntti Palosaari /* init stats here in order signal app which stats are supported */ 847c1daf651SAntti Palosaari c->cnr.len = 1; 848c1daf651SAntti Palosaari c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE; 849ce80d713SAntti Palosaari c->post_bit_error.len = 1; 850ce80d713SAntti Palosaari c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE; 851ce80d713SAntti Palosaari c->post_bit_count.len = 1; 852ce80d713SAntti Palosaari c->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE; 853395d00d1SAntti Palosaari return 0; 854395d00d1SAntti Palosaari 8555ed0cf88SMarkus Elfring error_fw_release: 8565ed0cf88SMarkus Elfring release_firmware(fw); 8575ed0cf88SMarkus Elfring err: 858395d00d1SAntti Palosaari dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret); 859395d00d1SAntti Palosaari return ret; 860395d00d1SAntti Palosaari } 861395d00d1SAntti Palosaari 862395d00d1SAntti Palosaari static int m88ds3103_sleep(struct dvb_frontend *fe) 863395d00d1SAntti Palosaari { 864395d00d1SAntti Palosaari struct m88ds3103_priv *priv = fe->demodulator_priv; 865395d00d1SAntti Palosaari int ret; 866f4df95bcSnibble.max u8 u8tmp; 86741b9aa00SAntti Palosaari 868395d00d1SAntti Palosaari dev_dbg(&priv->i2c->dev, "%s:\n", __func__); 869395d00d1SAntti Palosaari 870c1daf651SAntti Palosaari priv->fe_status = 0; 871395d00d1SAntti Palosaari priv->delivery_system = SYS_UNDEFINED; 872395d00d1SAntti Palosaari 873395d00d1SAntti Palosaari /* TS Hi-Z */ 874f4df95bcSnibble.max if (priv->chip_id == M88RS6000_CHIP_ID) 875f4df95bcSnibble.max u8tmp = 0x29; 876f4df95bcSnibble.max else 877f4df95bcSnibble.max u8tmp = 0x27; 878f4df95bcSnibble.max ret = m88ds3103_wr_reg_mask(priv, u8tmp, 0x00, 0x01); 879395d00d1SAntti Palosaari if (ret) 880395d00d1SAntti Palosaari goto err; 881395d00d1SAntti Palosaari 882395d00d1SAntti Palosaari /* sleep */ 883395d00d1SAntti Palosaari ret = m88ds3103_wr_reg_mask(priv, 0x08, 0x00, 0x01); 884395d00d1SAntti Palosaari if (ret) 885395d00d1SAntti Palosaari goto err; 886395d00d1SAntti Palosaari 887395d00d1SAntti Palosaari ret = m88ds3103_wr_reg_mask(priv, 0x04, 0x01, 0x01); 888395d00d1SAntti Palosaari if (ret) 889395d00d1SAntti Palosaari goto err; 890395d00d1SAntti Palosaari 891395d00d1SAntti Palosaari ret = m88ds3103_wr_reg_mask(priv, 0x23, 0x10, 0x10); 892395d00d1SAntti Palosaari if (ret) 893395d00d1SAntti Palosaari goto err; 894395d00d1SAntti Palosaari 895395d00d1SAntti Palosaari return 0; 896395d00d1SAntti Palosaari err: 897395d00d1SAntti Palosaari dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret); 898395d00d1SAntti Palosaari return ret; 899395d00d1SAntti Palosaari } 900395d00d1SAntti Palosaari 901395d00d1SAntti Palosaari static int m88ds3103_get_frontend(struct dvb_frontend *fe) 902395d00d1SAntti Palosaari { 903395d00d1SAntti Palosaari struct m88ds3103_priv *priv = fe->demodulator_priv; 904395d00d1SAntti Palosaari struct dtv_frontend_properties *c = &fe->dtv_property_cache; 905395d00d1SAntti Palosaari int ret; 906395d00d1SAntti Palosaari u8 buf[3]; 90741b9aa00SAntti Palosaari 908395d00d1SAntti Palosaari dev_dbg(&priv->i2c->dev, "%s:\n", __func__); 909395d00d1SAntti Palosaari 910395d00d1SAntti Palosaari if (!priv->warm || !(priv->fe_status & FE_HAS_LOCK)) { 9119240c384SAntti Palosaari ret = 0; 912395d00d1SAntti Palosaari goto err; 913395d00d1SAntti Palosaari } 914395d00d1SAntti Palosaari 915395d00d1SAntti Palosaari switch (c->delivery_system) { 916395d00d1SAntti Palosaari case SYS_DVBS: 917395d00d1SAntti Palosaari ret = m88ds3103_rd_reg(priv, 0xe0, &buf[0]); 918395d00d1SAntti Palosaari if (ret) 919395d00d1SAntti Palosaari goto err; 920395d00d1SAntti Palosaari 921395d00d1SAntti Palosaari ret = m88ds3103_rd_reg(priv, 0xe6, &buf[1]); 922395d00d1SAntti Palosaari if (ret) 923395d00d1SAntti Palosaari goto err; 924395d00d1SAntti Palosaari 925395d00d1SAntti Palosaari switch ((buf[0] >> 2) & 0x01) { 926395d00d1SAntti Palosaari case 0: 927395d00d1SAntti Palosaari c->inversion = INVERSION_OFF; 928395d00d1SAntti Palosaari break; 929395d00d1SAntti Palosaari case 1: 930395d00d1SAntti Palosaari c->inversion = INVERSION_ON; 931395d00d1SAntti Palosaari break; 932395d00d1SAntti Palosaari } 933395d00d1SAntti Palosaari 934395d00d1SAntti Palosaari switch ((buf[1] >> 5) & 0x07) { 935395d00d1SAntti Palosaari case 0: 936395d00d1SAntti Palosaari c->fec_inner = FEC_7_8; 937395d00d1SAntti Palosaari break; 938395d00d1SAntti Palosaari case 1: 939395d00d1SAntti Palosaari c->fec_inner = FEC_5_6; 940395d00d1SAntti Palosaari break; 941395d00d1SAntti Palosaari case 2: 942395d00d1SAntti Palosaari c->fec_inner = FEC_3_4; 943395d00d1SAntti Palosaari break; 944395d00d1SAntti Palosaari case 3: 945395d00d1SAntti Palosaari c->fec_inner = FEC_2_3; 946395d00d1SAntti Palosaari break; 947395d00d1SAntti Palosaari case 4: 948395d00d1SAntti Palosaari c->fec_inner = FEC_1_2; 949395d00d1SAntti Palosaari break; 950395d00d1SAntti Palosaari default: 951395d00d1SAntti Palosaari dev_dbg(&priv->i2c->dev, "%s: invalid fec_inner\n", 952395d00d1SAntti Palosaari __func__); 953395d00d1SAntti Palosaari } 954395d00d1SAntti Palosaari 955395d00d1SAntti Palosaari c->modulation = QPSK; 956395d00d1SAntti Palosaari 957395d00d1SAntti Palosaari break; 958395d00d1SAntti Palosaari case SYS_DVBS2: 959395d00d1SAntti Palosaari ret = m88ds3103_rd_reg(priv, 0x7e, &buf[0]); 960395d00d1SAntti Palosaari if (ret) 961395d00d1SAntti Palosaari goto err; 962395d00d1SAntti Palosaari 963395d00d1SAntti Palosaari ret = m88ds3103_rd_reg(priv, 0x89, &buf[1]); 964395d00d1SAntti Palosaari if (ret) 965395d00d1SAntti Palosaari goto err; 966395d00d1SAntti Palosaari 967395d00d1SAntti Palosaari ret = m88ds3103_rd_reg(priv, 0xf2, &buf[2]); 968395d00d1SAntti Palosaari if (ret) 969395d00d1SAntti Palosaari goto err; 970395d00d1SAntti Palosaari 971395d00d1SAntti Palosaari switch ((buf[0] >> 0) & 0x0f) { 972395d00d1SAntti Palosaari case 2: 973395d00d1SAntti Palosaari c->fec_inner = FEC_2_5; 974395d00d1SAntti Palosaari break; 975395d00d1SAntti Palosaari case 3: 976395d00d1SAntti Palosaari c->fec_inner = FEC_1_2; 977395d00d1SAntti Palosaari break; 978395d00d1SAntti Palosaari case 4: 979395d00d1SAntti Palosaari c->fec_inner = FEC_3_5; 980395d00d1SAntti Palosaari break; 981395d00d1SAntti Palosaari case 5: 982395d00d1SAntti Palosaari c->fec_inner = FEC_2_3; 983395d00d1SAntti Palosaari break; 984395d00d1SAntti Palosaari case 6: 985395d00d1SAntti Palosaari c->fec_inner = FEC_3_4; 986395d00d1SAntti Palosaari break; 987395d00d1SAntti Palosaari case 7: 988395d00d1SAntti Palosaari c->fec_inner = FEC_4_5; 989395d00d1SAntti Palosaari break; 990395d00d1SAntti Palosaari case 8: 991395d00d1SAntti Palosaari c->fec_inner = FEC_5_6; 992395d00d1SAntti Palosaari break; 993395d00d1SAntti Palosaari case 9: 994395d00d1SAntti Palosaari c->fec_inner = FEC_8_9; 995395d00d1SAntti Palosaari break; 996395d00d1SAntti Palosaari case 10: 997395d00d1SAntti Palosaari c->fec_inner = FEC_9_10; 998395d00d1SAntti Palosaari break; 999395d00d1SAntti Palosaari default: 1000395d00d1SAntti Palosaari dev_dbg(&priv->i2c->dev, "%s: invalid fec_inner\n", 1001395d00d1SAntti Palosaari __func__); 1002395d00d1SAntti Palosaari } 1003395d00d1SAntti Palosaari 1004395d00d1SAntti Palosaari switch ((buf[0] >> 5) & 0x01) { 1005395d00d1SAntti Palosaari case 0: 1006395d00d1SAntti Palosaari c->pilot = PILOT_OFF; 1007395d00d1SAntti Palosaari break; 1008395d00d1SAntti Palosaari case 1: 1009395d00d1SAntti Palosaari c->pilot = PILOT_ON; 1010395d00d1SAntti Palosaari break; 1011395d00d1SAntti Palosaari } 1012395d00d1SAntti Palosaari 1013395d00d1SAntti Palosaari switch ((buf[0] >> 6) & 0x07) { 1014395d00d1SAntti Palosaari case 0: 1015395d00d1SAntti Palosaari c->modulation = QPSK; 1016395d00d1SAntti Palosaari break; 1017395d00d1SAntti Palosaari case 1: 1018395d00d1SAntti Palosaari c->modulation = PSK_8; 1019395d00d1SAntti Palosaari break; 1020395d00d1SAntti Palosaari case 2: 1021395d00d1SAntti Palosaari c->modulation = APSK_16; 1022395d00d1SAntti Palosaari break; 1023395d00d1SAntti Palosaari case 3: 1024395d00d1SAntti Palosaari c->modulation = APSK_32; 1025395d00d1SAntti Palosaari break; 1026395d00d1SAntti Palosaari default: 1027395d00d1SAntti Palosaari dev_dbg(&priv->i2c->dev, "%s: invalid modulation\n", 1028395d00d1SAntti Palosaari __func__); 1029395d00d1SAntti Palosaari } 1030395d00d1SAntti Palosaari 1031395d00d1SAntti Palosaari switch ((buf[1] >> 7) & 0x01) { 1032395d00d1SAntti Palosaari case 0: 1033395d00d1SAntti Palosaari c->inversion = INVERSION_OFF; 1034395d00d1SAntti Palosaari break; 1035395d00d1SAntti Palosaari case 1: 1036395d00d1SAntti Palosaari c->inversion = INVERSION_ON; 1037395d00d1SAntti Palosaari break; 1038395d00d1SAntti Palosaari } 1039395d00d1SAntti Palosaari 1040395d00d1SAntti Palosaari switch ((buf[2] >> 0) & 0x03) { 1041395d00d1SAntti Palosaari case 0: 1042395d00d1SAntti Palosaari c->rolloff = ROLLOFF_35; 1043395d00d1SAntti Palosaari break; 1044395d00d1SAntti Palosaari case 1: 1045395d00d1SAntti Palosaari c->rolloff = ROLLOFF_25; 1046395d00d1SAntti Palosaari break; 1047395d00d1SAntti Palosaari case 2: 1048395d00d1SAntti Palosaari c->rolloff = ROLLOFF_20; 1049395d00d1SAntti Palosaari break; 1050395d00d1SAntti Palosaari default: 1051395d00d1SAntti Palosaari dev_dbg(&priv->i2c->dev, "%s: invalid rolloff\n", 1052395d00d1SAntti Palosaari __func__); 1053395d00d1SAntti Palosaari } 1054395d00d1SAntti Palosaari break; 1055395d00d1SAntti Palosaari default: 1056395d00d1SAntti Palosaari dev_dbg(&priv->i2c->dev, "%s: invalid delivery_system\n", 1057395d00d1SAntti Palosaari __func__); 1058395d00d1SAntti Palosaari ret = -EINVAL; 1059395d00d1SAntti Palosaari goto err; 1060395d00d1SAntti Palosaari } 1061395d00d1SAntti Palosaari 1062395d00d1SAntti Palosaari ret = m88ds3103_rd_regs(priv, 0x6d, buf, 2); 1063395d00d1SAntti Palosaari if (ret) 1064395d00d1SAntti Palosaari goto err; 1065395d00d1SAntti Palosaari 1066395d00d1SAntti Palosaari c->symbol_rate = 1ull * ((buf[1] << 8) | (buf[0] << 0)) * 1067f4df95bcSnibble.max priv->mclk_khz * 1000 / 0x10000; 1068395d00d1SAntti Palosaari 1069395d00d1SAntti Palosaari return 0; 1070395d00d1SAntti Palosaari err: 1071395d00d1SAntti Palosaari dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret); 1072395d00d1SAntti Palosaari return ret; 1073395d00d1SAntti Palosaari } 1074395d00d1SAntti Palosaari 1075395d00d1SAntti Palosaari static int m88ds3103_read_snr(struct dvb_frontend *fe, u16 *snr) 1076395d00d1SAntti Palosaari { 1077395d00d1SAntti Palosaari struct dtv_frontend_properties *c = &fe->dtv_property_cache; 107841b9aa00SAntti Palosaari 1079c1daf651SAntti Palosaari if (c->cnr.stat[0].scale == FE_SCALE_DECIBEL) 1080c1daf651SAntti Palosaari *snr = div_s64(c->cnr.stat[0].svalue, 100); 1081395d00d1SAntti Palosaari else 1082395d00d1SAntti Palosaari *snr = 0; 1083395d00d1SAntti Palosaari 1084395d00d1SAntti Palosaari return 0; 1085395d00d1SAntti Palosaari } 1086395d00d1SAntti Palosaari 10874423a2baSAntti Palosaari static int m88ds3103_read_ber(struct dvb_frontend *fe, u32 *ber) 10884423a2baSAntti Palosaari { 10894423a2baSAntti Palosaari struct m88ds3103_priv *priv = fe->demodulator_priv; 109041b9aa00SAntti Palosaari 1091ce80d713SAntti Palosaari *ber = priv->dvbv3_ber; 10924423a2baSAntti Palosaari 10934423a2baSAntti Palosaari return 0; 10944423a2baSAntti Palosaari } 1095395d00d1SAntti Palosaari 1096395d00d1SAntti Palosaari static int m88ds3103_set_tone(struct dvb_frontend *fe, 1097395d00d1SAntti Palosaari fe_sec_tone_mode_t fe_sec_tone_mode) 1098395d00d1SAntti Palosaari { 1099395d00d1SAntti Palosaari struct m88ds3103_priv *priv = fe->demodulator_priv; 1100395d00d1SAntti Palosaari int ret; 1101395d00d1SAntti Palosaari u8 u8tmp, tone, reg_a1_mask; 110241b9aa00SAntti Palosaari 1103395d00d1SAntti Palosaari dev_dbg(&priv->i2c->dev, "%s: fe_sec_tone_mode=%d\n", __func__, 1104395d00d1SAntti Palosaari fe_sec_tone_mode); 1105395d00d1SAntti Palosaari 1106395d00d1SAntti Palosaari if (!priv->warm) { 1107395d00d1SAntti Palosaari ret = -EAGAIN; 1108395d00d1SAntti Palosaari goto err; 1109395d00d1SAntti Palosaari } 1110395d00d1SAntti Palosaari 1111395d00d1SAntti Palosaari switch (fe_sec_tone_mode) { 1112395d00d1SAntti Palosaari case SEC_TONE_ON: 1113395d00d1SAntti Palosaari tone = 0; 1114418a97cbSAntti Palosaari reg_a1_mask = 0x47; 1115395d00d1SAntti Palosaari break; 1116395d00d1SAntti Palosaari case SEC_TONE_OFF: 1117395d00d1SAntti Palosaari tone = 1; 1118395d00d1SAntti Palosaari reg_a1_mask = 0x00; 1119395d00d1SAntti Palosaari break; 1120395d00d1SAntti Palosaari default: 1121395d00d1SAntti Palosaari dev_dbg(&priv->i2c->dev, "%s: invalid fe_sec_tone_mode\n", 1122395d00d1SAntti Palosaari __func__); 1123395d00d1SAntti Palosaari ret = -EINVAL; 1124395d00d1SAntti Palosaari goto err; 1125395d00d1SAntti Palosaari } 1126395d00d1SAntti Palosaari 1127395d00d1SAntti Palosaari u8tmp = tone << 7 | priv->cfg->envelope_mode << 5; 1128395d00d1SAntti Palosaari ret = m88ds3103_wr_reg_mask(priv, 0xa2, u8tmp, 0xe0); 1129395d00d1SAntti Palosaari if (ret) 1130395d00d1SAntti Palosaari goto err; 1131395d00d1SAntti Palosaari 1132395d00d1SAntti Palosaari u8tmp = 1 << 2; 1133395d00d1SAntti Palosaari ret = m88ds3103_wr_reg_mask(priv, 0xa1, u8tmp, reg_a1_mask); 1134395d00d1SAntti Palosaari if (ret) 1135395d00d1SAntti Palosaari goto err; 1136395d00d1SAntti Palosaari 1137395d00d1SAntti Palosaari return 0; 1138395d00d1SAntti Palosaari err: 1139395d00d1SAntti Palosaari dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret); 1140395d00d1SAntti Palosaari return ret; 1141395d00d1SAntti Palosaari } 1142395d00d1SAntti Palosaari 114379d09330Snibble.max static int m88ds3103_set_voltage(struct dvb_frontend *fe, 1144d28677ffSAntti Palosaari fe_sec_voltage_t fe_sec_voltage) 114579d09330Snibble.max { 114679d09330Snibble.max struct m88ds3103_priv *priv = fe->demodulator_priv; 1147d28677ffSAntti Palosaari int ret; 1148d28677ffSAntti Palosaari u8 u8tmp; 1149d28677ffSAntti Palosaari bool voltage_sel, voltage_dis; 115079d09330Snibble.max 1151d28677ffSAntti Palosaari dev_dbg(&priv->i2c->dev, "%s: fe_sec_voltage=%d\n", __func__, 1152d28677ffSAntti Palosaari fe_sec_voltage); 115379d09330Snibble.max 1154d28677ffSAntti Palosaari if (!priv->warm) { 1155d28677ffSAntti Palosaari ret = -EAGAIN; 1156d28677ffSAntti Palosaari goto err; 1157d28677ffSAntti Palosaari } 115879d09330Snibble.max 1159d28677ffSAntti Palosaari switch (fe_sec_voltage) { 116079d09330Snibble.max case SEC_VOLTAGE_18: 1161afbd6eb4SMauro Carvalho Chehab voltage_sel = true; 1162afbd6eb4SMauro Carvalho Chehab voltage_dis = false; 116379d09330Snibble.max break; 116479d09330Snibble.max case SEC_VOLTAGE_13: 1165afbd6eb4SMauro Carvalho Chehab voltage_sel = false; 1166afbd6eb4SMauro Carvalho Chehab voltage_dis = false; 116779d09330Snibble.max break; 116879d09330Snibble.max case SEC_VOLTAGE_OFF: 1169afbd6eb4SMauro Carvalho Chehab voltage_sel = false; 1170afbd6eb4SMauro Carvalho Chehab voltage_dis = true; 117179d09330Snibble.max break; 1172d28677ffSAntti Palosaari default: 1173d28677ffSAntti Palosaari dev_dbg(&priv->i2c->dev, "%s: invalid fe_sec_voltage\n", 1174d28677ffSAntti Palosaari __func__); 1175d28677ffSAntti Palosaari ret = -EINVAL; 1176d28677ffSAntti Palosaari goto err; 117779d09330Snibble.max } 1178d28677ffSAntti Palosaari 1179d28677ffSAntti Palosaari /* output pin polarity */ 1180d28677ffSAntti Palosaari voltage_sel ^= priv->cfg->lnb_hv_pol; 1181d28677ffSAntti Palosaari voltage_dis ^= priv->cfg->lnb_en_pol; 1182d28677ffSAntti Palosaari 1183d28677ffSAntti Palosaari u8tmp = voltage_dis << 1 | voltage_sel << 0; 1184d28677ffSAntti Palosaari ret = m88ds3103_wr_reg_mask(priv, 0xa2, u8tmp, 0x03); 1185d28677ffSAntti Palosaari if (ret) 1186d28677ffSAntti Palosaari goto err; 118779d09330Snibble.max 118879d09330Snibble.max return 0; 1189d28677ffSAntti Palosaari err: 1190d28677ffSAntti Palosaari dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret); 1191d28677ffSAntti Palosaari return ret; 119279d09330Snibble.max } 119379d09330Snibble.max 1194395d00d1SAntti Palosaari static int m88ds3103_diseqc_send_master_cmd(struct dvb_frontend *fe, 1195395d00d1SAntti Palosaari struct dvb_diseqc_master_cmd *diseqc_cmd) 1196395d00d1SAntti Palosaari { 1197395d00d1SAntti Palosaari struct m88ds3103_priv *priv = fe->demodulator_priv; 1198befa0cc1SAntti Palosaari int ret; 1199befa0cc1SAntti Palosaari unsigned long timeout; 1200395d00d1SAntti Palosaari u8 u8tmp; 120141b9aa00SAntti Palosaari 1202395d00d1SAntti Palosaari dev_dbg(&priv->i2c->dev, "%s: msg=%*ph\n", __func__, 1203395d00d1SAntti Palosaari diseqc_cmd->msg_len, diseqc_cmd->msg); 1204395d00d1SAntti Palosaari 1205395d00d1SAntti Palosaari if (!priv->warm) { 1206395d00d1SAntti Palosaari ret = -EAGAIN; 1207395d00d1SAntti Palosaari goto err; 1208395d00d1SAntti Palosaari } 1209395d00d1SAntti Palosaari 1210395d00d1SAntti Palosaari if (diseqc_cmd->msg_len < 3 || diseqc_cmd->msg_len > 6) { 1211395d00d1SAntti Palosaari ret = -EINVAL; 1212395d00d1SAntti Palosaari goto err; 1213395d00d1SAntti Palosaari } 1214395d00d1SAntti Palosaari 1215395d00d1SAntti Palosaari u8tmp = priv->cfg->envelope_mode << 5; 1216395d00d1SAntti Palosaari ret = m88ds3103_wr_reg_mask(priv, 0xa2, u8tmp, 0xe0); 1217395d00d1SAntti Palosaari if (ret) 1218395d00d1SAntti Palosaari goto err; 1219395d00d1SAntti Palosaari 1220395d00d1SAntti Palosaari ret = m88ds3103_wr_regs(priv, 0xa3, diseqc_cmd->msg, 1221395d00d1SAntti Palosaari diseqc_cmd->msg_len); 1222395d00d1SAntti Palosaari if (ret) 1223395d00d1SAntti Palosaari goto err; 1224395d00d1SAntti Palosaari 1225395d00d1SAntti Palosaari ret = m88ds3103_wr_reg(priv, 0xa1, 1226395d00d1SAntti Palosaari (diseqc_cmd->msg_len - 1) << 3 | 0x07); 1227395d00d1SAntti Palosaari if (ret) 1228395d00d1SAntti Palosaari goto err; 1229395d00d1SAntti Palosaari 1230395d00d1SAntti Palosaari /* wait DiSEqC TX ready */ 1231befa0cc1SAntti Palosaari #define SEND_MASTER_CMD_TIMEOUT 120 1232befa0cc1SAntti Palosaari timeout = jiffies + msecs_to_jiffies(SEND_MASTER_CMD_TIMEOUT); 1233395d00d1SAntti Palosaari 1234befa0cc1SAntti Palosaari /* DiSEqC message typical period is 54 ms */ 1235befa0cc1SAntti Palosaari usleep_range(50000, 54000); 1236befa0cc1SAntti Palosaari 1237befa0cc1SAntti Palosaari for (u8tmp = 1; !time_after(jiffies, timeout) && u8tmp;) { 1238395d00d1SAntti Palosaari ret = m88ds3103_rd_reg_mask(priv, 0xa1, &u8tmp, 0x40); 1239395d00d1SAntti Palosaari if (ret) 1240395d00d1SAntti Palosaari goto err; 1241395d00d1SAntti Palosaari } 1242395d00d1SAntti Palosaari 1243befa0cc1SAntti Palosaari if (u8tmp == 0) { 1244befa0cc1SAntti Palosaari dev_dbg(&priv->i2c->dev, "%s: diseqc tx took %u ms\n", __func__, 1245befa0cc1SAntti Palosaari jiffies_to_msecs(jiffies) - 1246befa0cc1SAntti Palosaari (jiffies_to_msecs(timeout) - SEND_MASTER_CMD_TIMEOUT)); 1247befa0cc1SAntti Palosaari } else { 1248395d00d1SAntti Palosaari dev_dbg(&priv->i2c->dev, "%s: diseqc tx timeout\n", __func__); 1249395d00d1SAntti Palosaari 1250395d00d1SAntti Palosaari ret = m88ds3103_wr_reg_mask(priv, 0xa1, 0x40, 0xc0); 1251395d00d1SAntti Palosaari if (ret) 1252395d00d1SAntti Palosaari goto err; 1253395d00d1SAntti Palosaari } 1254395d00d1SAntti Palosaari 1255395d00d1SAntti Palosaari ret = m88ds3103_wr_reg_mask(priv, 0xa2, 0x80, 0xc0); 1256395d00d1SAntti Palosaari if (ret) 1257395d00d1SAntti Palosaari goto err; 1258395d00d1SAntti Palosaari 1259befa0cc1SAntti Palosaari if (u8tmp == 1) { 1260395d00d1SAntti Palosaari ret = -ETIMEDOUT; 1261395d00d1SAntti Palosaari goto err; 1262395d00d1SAntti Palosaari } 1263395d00d1SAntti Palosaari 1264395d00d1SAntti Palosaari return 0; 1265395d00d1SAntti Palosaari err: 1266395d00d1SAntti Palosaari dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret); 1267395d00d1SAntti Palosaari return ret; 1268395d00d1SAntti Palosaari } 1269395d00d1SAntti Palosaari 1270395d00d1SAntti Palosaari static int m88ds3103_diseqc_send_burst(struct dvb_frontend *fe, 1271395d00d1SAntti Palosaari fe_sec_mini_cmd_t fe_sec_mini_cmd) 1272395d00d1SAntti Palosaari { 1273395d00d1SAntti Palosaari struct m88ds3103_priv *priv = fe->demodulator_priv; 1274befa0cc1SAntti Palosaari int ret; 1275befa0cc1SAntti Palosaari unsigned long timeout; 1276395d00d1SAntti Palosaari u8 u8tmp, burst; 127741b9aa00SAntti Palosaari 1278395d00d1SAntti Palosaari dev_dbg(&priv->i2c->dev, "%s: fe_sec_mini_cmd=%d\n", __func__, 1279395d00d1SAntti Palosaari fe_sec_mini_cmd); 1280395d00d1SAntti Palosaari 1281395d00d1SAntti Palosaari if (!priv->warm) { 1282395d00d1SAntti Palosaari ret = -EAGAIN; 1283395d00d1SAntti Palosaari goto err; 1284395d00d1SAntti Palosaari } 1285395d00d1SAntti Palosaari 1286395d00d1SAntti Palosaari u8tmp = priv->cfg->envelope_mode << 5; 1287395d00d1SAntti Palosaari ret = m88ds3103_wr_reg_mask(priv, 0xa2, u8tmp, 0xe0); 1288395d00d1SAntti Palosaari if (ret) 1289395d00d1SAntti Palosaari goto err; 1290395d00d1SAntti Palosaari 1291395d00d1SAntti Palosaari switch (fe_sec_mini_cmd) { 1292395d00d1SAntti Palosaari case SEC_MINI_A: 1293395d00d1SAntti Palosaari burst = 0x02; 1294395d00d1SAntti Palosaari break; 1295395d00d1SAntti Palosaari case SEC_MINI_B: 1296395d00d1SAntti Palosaari burst = 0x01; 1297395d00d1SAntti Palosaari break; 1298395d00d1SAntti Palosaari default: 1299395d00d1SAntti Palosaari dev_dbg(&priv->i2c->dev, "%s: invalid fe_sec_mini_cmd\n", 1300395d00d1SAntti Palosaari __func__); 1301395d00d1SAntti Palosaari ret = -EINVAL; 1302395d00d1SAntti Palosaari goto err; 1303395d00d1SAntti Palosaari } 1304395d00d1SAntti Palosaari 1305395d00d1SAntti Palosaari ret = m88ds3103_wr_reg(priv, 0xa1, burst); 1306395d00d1SAntti Palosaari if (ret) 1307395d00d1SAntti Palosaari goto err; 1308395d00d1SAntti Palosaari 1309395d00d1SAntti Palosaari /* wait DiSEqC TX ready */ 1310befa0cc1SAntti Palosaari #define SEND_BURST_TIMEOUT 40 1311befa0cc1SAntti Palosaari timeout = jiffies + msecs_to_jiffies(SEND_BURST_TIMEOUT); 1312395d00d1SAntti Palosaari 1313befa0cc1SAntti Palosaari /* DiSEqC ToneBurst period is 12.5 ms */ 1314befa0cc1SAntti Palosaari usleep_range(8500, 12500); 1315befa0cc1SAntti Palosaari 1316befa0cc1SAntti Palosaari for (u8tmp = 1; !time_after(jiffies, timeout) && u8tmp;) { 1317395d00d1SAntti Palosaari ret = m88ds3103_rd_reg_mask(priv, 0xa1, &u8tmp, 0x40); 1318395d00d1SAntti Palosaari if (ret) 1319395d00d1SAntti Palosaari goto err; 1320395d00d1SAntti Palosaari } 1321395d00d1SAntti Palosaari 1322befa0cc1SAntti Palosaari if (u8tmp == 0) { 1323befa0cc1SAntti Palosaari dev_dbg(&priv->i2c->dev, "%s: diseqc tx took %u ms\n", __func__, 1324befa0cc1SAntti Palosaari jiffies_to_msecs(jiffies) - 1325befa0cc1SAntti Palosaari (jiffies_to_msecs(timeout) - SEND_BURST_TIMEOUT)); 1326befa0cc1SAntti Palosaari } else { 1327befa0cc1SAntti Palosaari dev_dbg(&priv->i2c->dev, "%s: diseqc tx timeout\n", __func__); 1328befa0cc1SAntti Palosaari 1329befa0cc1SAntti Palosaari ret = m88ds3103_wr_reg_mask(priv, 0xa1, 0x40, 0xc0); 1330befa0cc1SAntti Palosaari if (ret) 1331befa0cc1SAntti Palosaari goto err; 1332befa0cc1SAntti Palosaari } 1333395d00d1SAntti Palosaari 1334395d00d1SAntti Palosaari ret = m88ds3103_wr_reg_mask(priv, 0xa2, 0x80, 0xc0); 1335395d00d1SAntti Palosaari if (ret) 1336395d00d1SAntti Palosaari goto err; 1337395d00d1SAntti Palosaari 1338befa0cc1SAntti Palosaari if (u8tmp == 1) { 1339395d00d1SAntti Palosaari ret = -ETIMEDOUT; 1340395d00d1SAntti Palosaari goto err; 1341395d00d1SAntti Palosaari } 1342395d00d1SAntti Palosaari 1343395d00d1SAntti Palosaari return 0; 1344395d00d1SAntti Palosaari err: 1345395d00d1SAntti Palosaari dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret); 1346395d00d1SAntti Palosaari return ret; 1347395d00d1SAntti Palosaari } 1348395d00d1SAntti Palosaari 1349395d00d1SAntti Palosaari static int m88ds3103_get_tune_settings(struct dvb_frontend *fe, 1350395d00d1SAntti Palosaari struct dvb_frontend_tune_settings *s) 1351395d00d1SAntti Palosaari { 1352395d00d1SAntti Palosaari s->min_delay_ms = 3000; 1353395d00d1SAntti Palosaari 1354395d00d1SAntti Palosaari return 0; 1355395d00d1SAntti Palosaari } 1356395d00d1SAntti Palosaari 135744b9055bSAntti Palosaari static void m88ds3103_release(struct dvb_frontend *fe) 1358395d00d1SAntti Palosaari { 135944b9055bSAntti Palosaari struct m88ds3103_priv *priv = fe->demodulator_priv; 1360f01919e8SAntti Palosaari struct i2c_client *client = priv->client; 136141b9aa00SAntti Palosaari 1362f01919e8SAntti Palosaari i2c_unregister_device(client); 1363395d00d1SAntti Palosaari } 1364395d00d1SAntti Palosaari 136544b9055bSAntti Palosaari static int m88ds3103_select(struct i2c_adapter *adap, void *mux_priv, u32 chan) 1366395d00d1SAntti Palosaari { 136744b9055bSAntti Palosaari struct m88ds3103_priv *priv = mux_priv; 1368395d00d1SAntti Palosaari int ret; 1369395d00d1SAntti Palosaari struct i2c_msg gate_open_msg[1] = { 1370395d00d1SAntti Palosaari { 1371395d00d1SAntti Palosaari .addr = priv->cfg->i2c_addr, 1372395d00d1SAntti Palosaari .flags = 0, 1373395d00d1SAntti Palosaari .len = 2, 1374395d00d1SAntti Palosaari .buf = "\x03\x11", 1375395d00d1SAntti Palosaari } 1376395d00d1SAntti Palosaari }; 1377395d00d1SAntti Palosaari 1378395d00d1SAntti Palosaari mutex_lock(&priv->i2c_mutex); 1379395d00d1SAntti Palosaari 138044b9055bSAntti Palosaari /* open tuner I2C repeater for 1 xfer, closes automatically */ 13814fc57876SAntti Palosaari ret = __i2c_transfer(priv->i2c, gate_open_msg, 1); 1382395d00d1SAntti Palosaari if (ret != 1) { 138344b9055bSAntti Palosaari dev_warn(&priv->i2c->dev, "%s: i2c wr failed=%d\n", 1384395d00d1SAntti Palosaari KBUILD_MODNAME, ret); 138544b9055bSAntti Palosaari if (ret >= 0) 1386395d00d1SAntti Palosaari ret = -EREMOTEIO; 1387395d00d1SAntti Palosaari 1388395d00d1SAntti Palosaari return ret; 1389395d00d1SAntti Palosaari } 1390395d00d1SAntti Palosaari 139144b9055bSAntti Palosaari return 0; 139244b9055bSAntti Palosaari } 1393395d00d1SAntti Palosaari 139444b9055bSAntti Palosaari static int m88ds3103_deselect(struct i2c_adapter *adap, void *mux_priv, 139544b9055bSAntti Palosaari u32 chan) 1396395d00d1SAntti Palosaari { 139744b9055bSAntti Palosaari struct m88ds3103_priv *priv = mux_priv; 139844b9055bSAntti Palosaari 139944b9055bSAntti Palosaari mutex_unlock(&priv->i2c_mutex); 140044b9055bSAntti Palosaari 140144b9055bSAntti Palosaari return 0; 1402395d00d1SAntti Palosaari } 1403395d00d1SAntti Palosaari 1404f01919e8SAntti Palosaari /* 1405f01919e8SAntti Palosaari * XXX: That is wrapper to m88ds3103_probe() via driver core in order to provide 1406f01919e8SAntti Palosaari * proper I2C client for legacy media attach binding. 1407f01919e8SAntti Palosaari * New users must use I2C client binding directly! 1408f01919e8SAntti Palosaari */ 1409395d00d1SAntti Palosaari struct dvb_frontend *m88ds3103_attach(const struct m88ds3103_config *cfg, 1410395d00d1SAntti Palosaari struct i2c_adapter *i2c, struct i2c_adapter **tuner_i2c_adapter) 1411395d00d1SAntti Palosaari { 1412f01919e8SAntti Palosaari struct i2c_client *client; 1413f01919e8SAntti Palosaari struct i2c_board_info board_info; 1414f01919e8SAntti Palosaari struct m88ds3103_platform_data pdata; 1415395d00d1SAntti Palosaari 1416f01919e8SAntti Palosaari pdata.clk = cfg->clock; 1417f01919e8SAntti Palosaari pdata.i2c_wr_max = cfg->i2c_wr_max; 1418f01919e8SAntti Palosaari pdata.ts_mode = cfg->ts_mode; 1419f01919e8SAntti Palosaari pdata.ts_clk = cfg->ts_clk; 1420f01919e8SAntti Palosaari pdata.ts_clk_pol = cfg->ts_clk_pol; 1421f01919e8SAntti Palosaari pdata.spec_inv = cfg->spec_inv; 1422f01919e8SAntti Palosaari pdata.agc = cfg->agc; 1423f01919e8SAntti Palosaari pdata.agc_inv = cfg->agc_inv; 1424f01919e8SAntti Palosaari pdata.clk_out = cfg->clock_out; 1425f01919e8SAntti Palosaari pdata.envelope_mode = cfg->envelope_mode; 1426f01919e8SAntti Palosaari pdata.lnb_hv_pol = cfg->lnb_hv_pol; 1427f01919e8SAntti Palosaari pdata.lnb_en_pol = cfg->lnb_en_pol; 1428f01919e8SAntti Palosaari pdata.attach_in_use = true; 1429395d00d1SAntti Palosaari 1430f01919e8SAntti Palosaari memset(&board_info, 0, sizeof(board_info)); 1431f01919e8SAntti Palosaari strlcpy(board_info.type, "m88ds3103", I2C_NAME_SIZE); 1432f01919e8SAntti Palosaari board_info.addr = cfg->i2c_addr; 1433f01919e8SAntti Palosaari board_info.platform_data = &pdata; 1434f01919e8SAntti Palosaari client = i2c_new_device(i2c, &board_info); 1435f01919e8SAntti Palosaari if (!client || !client->dev.driver) 1436395d00d1SAntti Palosaari return NULL; 1437f01919e8SAntti Palosaari 1438f01919e8SAntti Palosaari *tuner_i2c_adapter = pdata.get_i2c_adapter(client); 1439f01919e8SAntti Palosaari return pdata.get_dvb_frontend(client); 1440395d00d1SAntti Palosaari } 1441395d00d1SAntti Palosaari EXPORT_SYMBOL(m88ds3103_attach); 1442395d00d1SAntti Palosaari 1443395d00d1SAntti Palosaari static struct dvb_frontend_ops m88ds3103_ops = { 1444395d00d1SAntti Palosaari .delsys = { SYS_DVBS, SYS_DVBS2 }, 1445395d00d1SAntti Palosaari .info = { 1446395d00d1SAntti Palosaari .name = "Montage M88DS3103", 1447395d00d1SAntti Palosaari .frequency_min = 950000, 1448395d00d1SAntti Palosaari .frequency_max = 2150000, 1449395d00d1SAntti Palosaari .frequency_tolerance = 5000, 1450395d00d1SAntti Palosaari .symbol_rate_min = 1000000, 1451395d00d1SAntti Palosaari .symbol_rate_max = 45000000, 1452395d00d1SAntti Palosaari .caps = FE_CAN_INVERSION_AUTO | 1453395d00d1SAntti Palosaari FE_CAN_FEC_1_2 | 1454395d00d1SAntti Palosaari FE_CAN_FEC_2_3 | 1455395d00d1SAntti Palosaari FE_CAN_FEC_3_4 | 1456395d00d1SAntti Palosaari FE_CAN_FEC_4_5 | 1457395d00d1SAntti Palosaari FE_CAN_FEC_5_6 | 1458395d00d1SAntti Palosaari FE_CAN_FEC_6_7 | 1459395d00d1SAntti Palosaari FE_CAN_FEC_7_8 | 1460395d00d1SAntti Palosaari FE_CAN_FEC_8_9 | 1461395d00d1SAntti Palosaari FE_CAN_FEC_AUTO | 1462395d00d1SAntti Palosaari FE_CAN_QPSK | 1463395d00d1SAntti Palosaari FE_CAN_RECOVER | 1464395d00d1SAntti Palosaari FE_CAN_2G_MODULATION 1465395d00d1SAntti Palosaari }, 1466395d00d1SAntti Palosaari 1467395d00d1SAntti Palosaari .release = m88ds3103_release, 1468395d00d1SAntti Palosaari 1469395d00d1SAntti Palosaari .get_tune_settings = m88ds3103_get_tune_settings, 1470395d00d1SAntti Palosaari 1471395d00d1SAntti Palosaari .init = m88ds3103_init, 1472395d00d1SAntti Palosaari .sleep = m88ds3103_sleep, 1473395d00d1SAntti Palosaari 1474395d00d1SAntti Palosaari .set_frontend = m88ds3103_set_frontend, 1475395d00d1SAntti Palosaari .get_frontend = m88ds3103_get_frontend, 1476395d00d1SAntti Palosaari 1477395d00d1SAntti Palosaari .read_status = m88ds3103_read_status, 1478395d00d1SAntti Palosaari .read_snr = m88ds3103_read_snr, 14794423a2baSAntti Palosaari .read_ber = m88ds3103_read_ber, 1480395d00d1SAntti Palosaari 1481395d00d1SAntti Palosaari .diseqc_send_master_cmd = m88ds3103_diseqc_send_master_cmd, 1482395d00d1SAntti Palosaari .diseqc_send_burst = m88ds3103_diseqc_send_burst, 1483395d00d1SAntti Palosaari 1484395d00d1SAntti Palosaari .set_tone = m88ds3103_set_tone, 148579d09330Snibble.max .set_voltage = m88ds3103_set_voltage, 1486395d00d1SAntti Palosaari }; 1487395d00d1SAntti Palosaari 1488f01919e8SAntti Palosaari static struct dvb_frontend *m88ds3103_get_dvb_frontend(struct i2c_client *client) 1489f01919e8SAntti Palosaari { 1490f01919e8SAntti Palosaari struct m88ds3103_priv *dev = i2c_get_clientdata(client); 1491f01919e8SAntti Palosaari 1492f01919e8SAntti Palosaari dev_dbg(&client->dev, "\n"); 1493f01919e8SAntti Palosaari 1494f01919e8SAntti Palosaari return &dev->fe; 1495f01919e8SAntti Palosaari } 1496f01919e8SAntti Palosaari 1497f01919e8SAntti Palosaari static struct i2c_adapter *m88ds3103_get_i2c_adapter(struct i2c_client *client) 1498f01919e8SAntti Palosaari { 1499f01919e8SAntti Palosaari struct m88ds3103_priv *dev = i2c_get_clientdata(client); 1500f01919e8SAntti Palosaari 1501f01919e8SAntti Palosaari dev_dbg(&client->dev, "\n"); 1502f01919e8SAntti Palosaari 1503f01919e8SAntti Palosaari return dev->i2c_adapter; 1504f01919e8SAntti Palosaari } 1505f01919e8SAntti Palosaari 1506f01919e8SAntti Palosaari static int m88ds3103_probe(struct i2c_client *client, 1507f01919e8SAntti Palosaari const struct i2c_device_id *id) 1508f01919e8SAntti Palosaari { 1509f01919e8SAntti Palosaari struct m88ds3103_priv *dev; 1510f01919e8SAntti Palosaari struct m88ds3103_platform_data *pdata = client->dev.platform_data; 1511f01919e8SAntti Palosaari int ret; 1512f01919e8SAntti Palosaari u8 chip_id, u8tmp; 1513f01919e8SAntti Palosaari 1514f01919e8SAntti Palosaari dev = kzalloc(sizeof(*dev), GFP_KERNEL); 1515f01919e8SAntti Palosaari if (!dev) { 1516f01919e8SAntti Palosaari ret = -ENOMEM; 1517f01919e8SAntti Palosaari goto err; 1518f01919e8SAntti Palosaari } 1519f01919e8SAntti Palosaari 1520f01919e8SAntti Palosaari dev->client = client; 1521f01919e8SAntti Palosaari dev->i2c = client->adapter; 1522f01919e8SAntti Palosaari dev->config.i2c_addr = client->addr; 1523f01919e8SAntti Palosaari dev->config.clock = pdata->clk; 1524f01919e8SAntti Palosaari dev->config.i2c_wr_max = pdata->i2c_wr_max; 1525f01919e8SAntti Palosaari dev->config.ts_mode = pdata->ts_mode; 1526f01919e8SAntti Palosaari dev->config.ts_clk = pdata->ts_clk; 1527f01919e8SAntti Palosaari dev->config.ts_clk_pol = pdata->ts_clk_pol; 1528f01919e8SAntti Palosaari dev->config.spec_inv = pdata->spec_inv; 1529f01919e8SAntti Palosaari dev->config.agc_inv = pdata->agc_inv; 1530f01919e8SAntti Palosaari dev->config.clock_out = pdata->clk_out; 1531f01919e8SAntti Palosaari dev->config.envelope_mode = pdata->envelope_mode; 1532f01919e8SAntti Palosaari dev->config.agc = pdata->agc; 1533f01919e8SAntti Palosaari dev->config.lnb_hv_pol = pdata->lnb_hv_pol; 1534f01919e8SAntti Palosaari dev->config.lnb_en_pol = pdata->lnb_en_pol; 1535f01919e8SAntti Palosaari dev->cfg = &dev->config; 1536f01919e8SAntti Palosaari mutex_init(&dev->i2c_mutex); 1537f01919e8SAntti Palosaari 1538f01919e8SAntti Palosaari /* 0x00: chip id[6:0], 0x01: chip ver[7:0], 0x02: chip ver[15:8] */ 1539f01919e8SAntti Palosaari ret = m88ds3103_rd_reg(dev, 0x00, &chip_id); 1540f01919e8SAntti Palosaari if (ret) 1541f01919e8SAntti Palosaari goto err_kfree; 1542f01919e8SAntti Palosaari 1543f01919e8SAntti Palosaari chip_id >>= 1; 1544f01919e8SAntti Palosaari dev_dbg(&client->dev, "chip_id=%02x\n", chip_id); 1545f01919e8SAntti Palosaari 1546f01919e8SAntti Palosaari switch (chip_id) { 1547f01919e8SAntti Palosaari case M88RS6000_CHIP_ID: 1548f01919e8SAntti Palosaari case M88DS3103_CHIP_ID: 1549f01919e8SAntti Palosaari break; 1550f01919e8SAntti Palosaari default: 1551f01919e8SAntti Palosaari goto err_kfree; 1552f01919e8SAntti Palosaari } 1553f01919e8SAntti Palosaari dev->chip_id = chip_id; 1554f01919e8SAntti Palosaari 1555f01919e8SAntti Palosaari switch (dev->cfg->clock_out) { 1556f01919e8SAntti Palosaari case M88DS3103_CLOCK_OUT_DISABLED: 1557f01919e8SAntti Palosaari u8tmp = 0x80; 1558f01919e8SAntti Palosaari break; 1559f01919e8SAntti Palosaari case M88DS3103_CLOCK_OUT_ENABLED: 1560f01919e8SAntti Palosaari u8tmp = 0x00; 1561f01919e8SAntti Palosaari break; 1562f01919e8SAntti Palosaari case M88DS3103_CLOCK_OUT_ENABLED_DIV2: 1563f01919e8SAntti Palosaari u8tmp = 0x10; 1564f01919e8SAntti Palosaari break; 1565f01919e8SAntti Palosaari default: 1566*4347df6aSDan Carpenter ret = -EINVAL; 1567f01919e8SAntti Palosaari goto err_kfree; 1568f01919e8SAntti Palosaari } 1569f01919e8SAntti Palosaari 1570f01919e8SAntti Palosaari /* 0x29 register is defined differently for m88rs6000. */ 1571f01919e8SAntti Palosaari /* set internal tuner address to 0x21 */ 1572f01919e8SAntti Palosaari if (chip_id == M88RS6000_CHIP_ID) 1573f01919e8SAntti Palosaari u8tmp = 0x00; 1574f01919e8SAntti Palosaari 1575f01919e8SAntti Palosaari ret = m88ds3103_wr_reg(dev, 0x29, u8tmp); 1576f01919e8SAntti Palosaari if (ret) 1577f01919e8SAntti Palosaari goto err_kfree; 1578f01919e8SAntti Palosaari 1579f01919e8SAntti Palosaari /* sleep */ 1580f01919e8SAntti Palosaari ret = m88ds3103_wr_reg_mask(dev, 0x08, 0x00, 0x01); 1581f01919e8SAntti Palosaari if (ret) 1582f01919e8SAntti Palosaari goto err_kfree; 1583f01919e8SAntti Palosaari ret = m88ds3103_wr_reg_mask(dev, 0x04, 0x01, 0x01); 1584f01919e8SAntti Palosaari if (ret) 1585f01919e8SAntti Palosaari goto err_kfree; 1586f01919e8SAntti Palosaari ret = m88ds3103_wr_reg_mask(dev, 0x23, 0x10, 0x10); 1587f01919e8SAntti Palosaari if (ret) 1588f01919e8SAntti Palosaari goto err_kfree; 1589f01919e8SAntti Palosaari 1590f01919e8SAntti Palosaari /* create mux i2c adapter for tuner */ 1591f01919e8SAntti Palosaari dev->i2c_adapter = i2c_add_mux_adapter(client->adapter, &client->dev, 1592f01919e8SAntti Palosaari dev, 0, 0, 0, m88ds3103_select, 1593f01919e8SAntti Palosaari m88ds3103_deselect); 1594*4347df6aSDan Carpenter if (dev->i2c_adapter == NULL) { 1595*4347df6aSDan Carpenter ret = -ENOMEM; 1596f01919e8SAntti Palosaari goto err_kfree; 1597*4347df6aSDan Carpenter } 1598f01919e8SAntti Palosaari 1599f01919e8SAntti Palosaari /* create dvb_frontend */ 1600f01919e8SAntti Palosaari memcpy(&dev->fe.ops, &m88ds3103_ops, sizeof(struct dvb_frontend_ops)); 1601f01919e8SAntti Palosaari if (dev->chip_id == M88RS6000_CHIP_ID) 1602f01919e8SAntti Palosaari strncpy(dev->fe.ops.info.name, 1603f01919e8SAntti Palosaari "Montage M88RS6000", sizeof(dev->fe.ops.info.name)); 1604f01919e8SAntti Palosaari if (!pdata->attach_in_use) 1605f01919e8SAntti Palosaari dev->fe.ops.release = NULL; 1606f01919e8SAntti Palosaari dev->fe.demodulator_priv = dev; 1607f01919e8SAntti Palosaari i2c_set_clientdata(client, dev); 1608f01919e8SAntti Palosaari 1609f01919e8SAntti Palosaari /* setup callbacks */ 1610f01919e8SAntti Palosaari pdata->get_dvb_frontend = m88ds3103_get_dvb_frontend; 1611f01919e8SAntti Palosaari pdata->get_i2c_adapter = m88ds3103_get_i2c_adapter; 1612f01919e8SAntti Palosaari return 0; 1613f01919e8SAntti Palosaari err_kfree: 1614f01919e8SAntti Palosaari kfree(dev); 1615f01919e8SAntti Palosaari err: 1616f01919e8SAntti Palosaari dev_dbg(&client->dev, "failed=%d\n", ret); 1617f01919e8SAntti Palosaari return ret; 1618f01919e8SAntti Palosaari } 1619f01919e8SAntti Palosaari 1620f01919e8SAntti Palosaari static int m88ds3103_remove(struct i2c_client *client) 1621f01919e8SAntti Palosaari { 1622f01919e8SAntti Palosaari struct m88ds3103_priv *dev = i2c_get_clientdata(client); 1623f01919e8SAntti Palosaari 1624f01919e8SAntti Palosaari dev_dbg(&client->dev, "\n"); 1625f01919e8SAntti Palosaari 1626f01919e8SAntti Palosaari i2c_del_mux_adapter(dev->i2c_adapter); 1627f01919e8SAntti Palosaari 1628f01919e8SAntti Palosaari kfree(dev); 1629f01919e8SAntti Palosaari return 0; 1630f01919e8SAntti Palosaari } 1631f01919e8SAntti Palosaari 1632f01919e8SAntti Palosaari static const struct i2c_device_id m88ds3103_id_table[] = { 1633f01919e8SAntti Palosaari {"m88ds3103", 0}, 1634f01919e8SAntti Palosaari {} 1635f01919e8SAntti Palosaari }; 1636f01919e8SAntti Palosaari MODULE_DEVICE_TABLE(i2c, m88ds3103_id_table); 1637f01919e8SAntti Palosaari 1638f01919e8SAntti Palosaari static struct i2c_driver m88ds3103_driver = { 1639f01919e8SAntti Palosaari .driver = { 1640f01919e8SAntti Palosaari .owner = THIS_MODULE, 1641f01919e8SAntti Palosaari .name = "m88ds3103", 1642f01919e8SAntti Palosaari .suppress_bind_attrs = true, 1643f01919e8SAntti Palosaari }, 1644f01919e8SAntti Palosaari .probe = m88ds3103_probe, 1645f01919e8SAntti Palosaari .remove = m88ds3103_remove, 1646f01919e8SAntti Palosaari .id_table = m88ds3103_id_table, 1647f01919e8SAntti Palosaari }; 1648f01919e8SAntti Palosaari 1649f01919e8SAntti Palosaari module_i2c_driver(m88ds3103_driver); 1650f01919e8SAntti Palosaari 1651395d00d1SAntti Palosaari MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>"); 1652395d00d1SAntti Palosaari MODULE_DESCRIPTION("Montage M88DS3103 DVB-S/S2 demodulator driver"); 1653395d00d1SAntti Palosaari MODULE_LICENSE("GPL"); 1654395d00d1SAntti Palosaari MODULE_FIRMWARE(M88DS3103_FIRMWARE); 1655f4df95bcSnibble.max MODULE_FIRMWARE(M88RS6000_FIRMWARE); 1656