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 189*0df289a2SMauro Carvalho Chehab static int m88ds3103_read_status(struct dvb_frontend *fe, 190*0df289a2SMauro Carvalho Chehab enum fe_status *status) 191395d00d1SAntti Palosaari { 192395d00d1SAntti Palosaari struct m88ds3103_priv *priv = fe->demodulator_priv; 193395d00d1SAntti Palosaari struct dtv_frontend_properties *c = &fe->dtv_property_cache; 194c1daf651SAntti Palosaari int ret, i, itmp; 195395d00d1SAntti Palosaari u8 u8tmp; 196c1daf651SAntti Palosaari u8 buf[3]; 197395d00d1SAntti Palosaari 198395d00d1SAntti Palosaari *status = 0; 199395d00d1SAntti Palosaari 200395d00d1SAntti Palosaari if (!priv->warm) { 201395d00d1SAntti Palosaari ret = -EAGAIN; 202395d00d1SAntti Palosaari goto err; 203395d00d1SAntti Palosaari } 204395d00d1SAntti Palosaari 205395d00d1SAntti Palosaari switch (c->delivery_system) { 206395d00d1SAntti Palosaari case SYS_DVBS: 207395d00d1SAntti Palosaari ret = m88ds3103_rd_reg_mask(priv, 0xd1, &u8tmp, 0x07); 208395d00d1SAntti Palosaari if (ret) 209395d00d1SAntti Palosaari goto err; 210395d00d1SAntti Palosaari 211395d00d1SAntti Palosaari if (u8tmp == 0x07) 212395d00d1SAntti Palosaari *status = FE_HAS_SIGNAL | FE_HAS_CARRIER | 213395d00d1SAntti Palosaari FE_HAS_VITERBI | FE_HAS_SYNC | 214395d00d1SAntti Palosaari FE_HAS_LOCK; 215395d00d1SAntti Palosaari break; 216395d00d1SAntti Palosaari case SYS_DVBS2: 217395d00d1SAntti Palosaari ret = m88ds3103_rd_reg_mask(priv, 0x0d, &u8tmp, 0x8f); 218395d00d1SAntti Palosaari if (ret) 219395d00d1SAntti Palosaari goto err; 220395d00d1SAntti Palosaari 221395d00d1SAntti Palosaari if (u8tmp == 0x8f) 222395d00d1SAntti Palosaari *status = FE_HAS_SIGNAL | FE_HAS_CARRIER | 223395d00d1SAntti Palosaari FE_HAS_VITERBI | FE_HAS_SYNC | 224395d00d1SAntti Palosaari FE_HAS_LOCK; 225395d00d1SAntti Palosaari break; 226395d00d1SAntti Palosaari default: 227395d00d1SAntti Palosaari dev_dbg(&priv->i2c->dev, "%s: invalid delivery_system\n", 228395d00d1SAntti Palosaari __func__); 229395d00d1SAntti Palosaari ret = -EINVAL; 230395d00d1SAntti Palosaari goto err; 231395d00d1SAntti Palosaari } 232395d00d1SAntti Palosaari 233395d00d1SAntti Palosaari priv->fe_status = *status; 234395d00d1SAntti Palosaari 235395d00d1SAntti Palosaari dev_dbg(&priv->i2c->dev, "%s: lock=%02x status=%02x\n", 236395d00d1SAntti Palosaari __func__, u8tmp, *status); 237395d00d1SAntti Palosaari 238c1daf651SAntti Palosaari /* CNR */ 239c1daf651SAntti Palosaari if (priv->fe_status & FE_HAS_VITERBI) { 240c1daf651SAntti Palosaari unsigned int cnr, noise, signal, noise_tot, signal_tot; 241c1daf651SAntti Palosaari 242c1daf651SAntti Palosaari cnr = 0; 243c1daf651SAntti Palosaari /* more iterations for more accurate estimation */ 244c1daf651SAntti Palosaari #define M88DS3103_SNR_ITERATIONS 3 245c1daf651SAntti Palosaari 246c1daf651SAntti Palosaari switch (c->delivery_system) { 247c1daf651SAntti Palosaari case SYS_DVBS: 248c1daf651SAntti Palosaari itmp = 0; 249c1daf651SAntti Palosaari 250c1daf651SAntti Palosaari for (i = 0; i < M88DS3103_SNR_ITERATIONS; i++) { 251c1daf651SAntti Palosaari ret = m88ds3103_rd_reg(priv, 0xff, &buf[0]); 252c1daf651SAntti Palosaari if (ret) 253c1daf651SAntti Palosaari goto err; 254c1daf651SAntti Palosaari 255c1daf651SAntti Palosaari itmp += buf[0]; 256c1daf651SAntti Palosaari } 257c1daf651SAntti Palosaari 258c1daf651SAntti Palosaari /* use of single register limits max value to 15 dB */ 259c1daf651SAntti Palosaari /* SNR(X) dB = 10 * ln(X) / ln(10) dB */ 260c1daf651SAntti Palosaari itmp = DIV_ROUND_CLOSEST(itmp, 8 * M88DS3103_SNR_ITERATIONS); 261c1daf651SAntti Palosaari if (itmp) 262c1daf651SAntti Palosaari cnr = div_u64((u64) 10000 * intlog2(itmp), intlog2(10)); 263c1daf651SAntti Palosaari break; 264c1daf651SAntti Palosaari case SYS_DVBS2: 265c1daf651SAntti Palosaari noise_tot = 0; 266c1daf651SAntti Palosaari signal_tot = 0; 267c1daf651SAntti Palosaari 268c1daf651SAntti Palosaari for (i = 0; i < M88DS3103_SNR_ITERATIONS; i++) { 269c1daf651SAntti Palosaari ret = m88ds3103_rd_regs(priv, 0x8c, buf, 3); 270c1daf651SAntti Palosaari if (ret) 271c1daf651SAntti Palosaari goto err; 272c1daf651SAntti Palosaari 273c1daf651SAntti Palosaari noise = buf[1] << 6; /* [13:6] */ 274c1daf651SAntti Palosaari noise |= buf[0] & 0x3f; /* [5:0] */ 275c1daf651SAntti Palosaari noise >>= 2; 276c1daf651SAntti Palosaari signal = buf[2] * buf[2]; 277c1daf651SAntti Palosaari signal >>= 1; 278c1daf651SAntti Palosaari 279c1daf651SAntti Palosaari noise_tot += noise; 280c1daf651SAntti Palosaari signal_tot += signal; 281c1daf651SAntti Palosaari } 282c1daf651SAntti Palosaari 283c1daf651SAntti Palosaari noise = noise_tot / M88DS3103_SNR_ITERATIONS; 284c1daf651SAntti Palosaari signal = signal_tot / M88DS3103_SNR_ITERATIONS; 285c1daf651SAntti Palosaari 286c1daf651SAntti Palosaari /* SNR(X) dB = 10 * log10(X) dB */ 287c1daf651SAntti Palosaari if (signal > noise) { 288c1daf651SAntti Palosaari itmp = signal / noise; 289c1daf651SAntti Palosaari cnr = div_u64((u64) 10000 * intlog10(itmp), (1 << 24)); 290c1daf651SAntti Palosaari } 291c1daf651SAntti Palosaari break; 292c1daf651SAntti Palosaari default: 293c1daf651SAntti Palosaari dev_dbg(&priv->i2c->dev, 294c1daf651SAntti Palosaari "%s: invalid delivery_system\n", __func__); 295c1daf651SAntti Palosaari ret = -EINVAL; 296c1daf651SAntti Palosaari goto err; 297c1daf651SAntti Palosaari } 298c1daf651SAntti Palosaari 299c1daf651SAntti Palosaari if (cnr) { 300c1daf651SAntti Palosaari c->cnr.stat[0].scale = FE_SCALE_DECIBEL; 301c1daf651SAntti Palosaari c->cnr.stat[0].svalue = cnr; 302c1daf651SAntti Palosaari } else { 303c1daf651SAntti Palosaari c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE; 304c1daf651SAntti Palosaari } 305c1daf651SAntti Palosaari } else { 306c1daf651SAntti Palosaari c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE; 307c1daf651SAntti Palosaari } 308c1daf651SAntti Palosaari 309ce80d713SAntti Palosaari /* BER */ 310ce80d713SAntti Palosaari if (priv->fe_status & FE_HAS_LOCK) { 311ce80d713SAntti Palosaari unsigned int utmp, post_bit_error, post_bit_count; 312ce80d713SAntti Palosaari 313ce80d713SAntti Palosaari switch (c->delivery_system) { 314ce80d713SAntti Palosaari case SYS_DVBS: 315ce80d713SAntti Palosaari ret = m88ds3103_wr_reg(priv, 0xf9, 0x04); 316ce80d713SAntti Palosaari if (ret) 317ce80d713SAntti Palosaari goto err; 318ce80d713SAntti Palosaari 319ce80d713SAntti Palosaari ret = m88ds3103_rd_reg(priv, 0xf8, &u8tmp); 320ce80d713SAntti Palosaari if (ret) 321ce80d713SAntti Palosaari goto err; 322ce80d713SAntti Palosaari 323ce80d713SAntti Palosaari /* measurement ready? */ 324ce80d713SAntti Palosaari if (!(u8tmp & 0x10)) { 325ce80d713SAntti Palosaari ret = m88ds3103_rd_regs(priv, 0xf6, buf, 2); 326ce80d713SAntti Palosaari if (ret) 327ce80d713SAntti Palosaari goto err; 328ce80d713SAntti Palosaari 329ce80d713SAntti Palosaari post_bit_error = buf[1] << 8 | buf[0] << 0; 330ce80d713SAntti Palosaari post_bit_count = 0x800000; 331ce80d713SAntti Palosaari priv->post_bit_error += post_bit_error; 332ce80d713SAntti Palosaari priv->post_bit_count += post_bit_count; 333ce80d713SAntti Palosaari priv->dvbv3_ber = post_bit_error; 334ce80d713SAntti Palosaari 335ce80d713SAntti Palosaari /* restart measurement */ 336ce80d713SAntti Palosaari u8tmp |= 0x10; 337ce80d713SAntti Palosaari ret = m88ds3103_wr_reg(priv, 0xf8, u8tmp); 338ce80d713SAntti Palosaari if (ret) 339ce80d713SAntti Palosaari goto err; 340ce80d713SAntti Palosaari } 341ce80d713SAntti Palosaari break; 342ce80d713SAntti Palosaari case SYS_DVBS2: 343ce80d713SAntti Palosaari ret = m88ds3103_rd_regs(priv, 0xd5, buf, 3); 344ce80d713SAntti Palosaari if (ret) 345ce80d713SAntti Palosaari goto err; 346ce80d713SAntti Palosaari 347ce80d713SAntti Palosaari utmp = buf[2] << 16 | buf[1] << 8 | buf[0] << 0; 348ce80d713SAntti Palosaari 349ce80d713SAntti Palosaari /* enough data? */ 350ce80d713SAntti Palosaari if (utmp > 4000) { 351ce80d713SAntti Palosaari ret = m88ds3103_rd_regs(priv, 0xf7, buf, 2); 352ce80d713SAntti Palosaari if (ret) 353ce80d713SAntti Palosaari goto err; 354ce80d713SAntti Palosaari 355ce80d713SAntti Palosaari post_bit_error = buf[1] << 8 | buf[0] << 0; 356ce80d713SAntti Palosaari post_bit_count = 32 * utmp; /* TODO: FEC */ 357ce80d713SAntti Palosaari priv->post_bit_error += post_bit_error; 358ce80d713SAntti Palosaari priv->post_bit_count += post_bit_count; 359ce80d713SAntti Palosaari priv->dvbv3_ber = post_bit_error; 360ce80d713SAntti Palosaari 361ce80d713SAntti Palosaari /* restart measurement */ 362ce80d713SAntti Palosaari ret = m88ds3103_wr_reg(priv, 0xd1, 0x01); 363ce80d713SAntti Palosaari if (ret) 364ce80d713SAntti Palosaari goto err; 365ce80d713SAntti Palosaari 366ce80d713SAntti Palosaari ret = m88ds3103_wr_reg(priv, 0xf9, 0x01); 367ce80d713SAntti Palosaari if (ret) 368ce80d713SAntti Palosaari goto err; 369ce80d713SAntti Palosaari 370ce80d713SAntti Palosaari ret = m88ds3103_wr_reg(priv, 0xf9, 0x00); 371ce80d713SAntti Palosaari if (ret) 372ce80d713SAntti Palosaari goto err; 373ce80d713SAntti Palosaari 374ce80d713SAntti Palosaari ret = m88ds3103_wr_reg(priv, 0xd1, 0x00); 375ce80d713SAntti Palosaari if (ret) 376ce80d713SAntti Palosaari goto err; 377ce80d713SAntti Palosaari } 378ce80d713SAntti Palosaari break; 379ce80d713SAntti Palosaari default: 380ce80d713SAntti Palosaari dev_dbg(&priv->i2c->dev, 381ce80d713SAntti Palosaari "%s: invalid delivery_system\n", __func__); 382ce80d713SAntti Palosaari ret = -EINVAL; 383ce80d713SAntti Palosaari goto err; 384ce80d713SAntti Palosaari } 385ce80d713SAntti Palosaari 386ce80d713SAntti Palosaari c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER; 387ce80d713SAntti Palosaari c->post_bit_error.stat[0].uvalue = priv->post_bit_error; 388ce80d713SAntti Palosaari c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER; 389ce80d713SAntti Palosaari c->post_bit_count.stat[0].uvalue = priv->post_bit_count; 390ce80d713SAntti Palosaari } else { 391ce80d713SAntti Palosaari c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE; 392ce80d713SAntti Palosaari c->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE; 393ce80d713SAntti Palosaari } 394ce80d713SAntti Palosaari 395395d00d1SAntti Palosaari return 0; 396395d00d1SAntti Palosaari err: 397395d00d1SAntti Palosaari dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret); 398395d00d1SAntti Palosaari return ret; 399395d00d1SAntti Palosaari } 400395d00d1SAntti Palosaari 401395d00d1SAntti Palosaari static int m88ds3103_set_frontend(struct dvb_frontend *fe) 402395d00d1SAntti Palosaari { 403395d00d1SAntti Palosaari struct m88ds3103_priv *priv = fe->demodulator_priv; 404395d00d1SAntti Palosaari struct dtv_frontend_properties *c = &fe->dtv_property_cache; 40506487deeSAntti Palosaari int ret, len; 406395d00d1SAntti Palosaari const struct m88ds3103_reg_val *init; 407b6851419Snibble.max u8 u8tmp, u8tmp1 = 0, u8tmp2 = 0; /* silence compiler warning */ 408f4df95bcSnibble.max u8 buf[3]; 409b6851419Snibble.max u16 u16tmp, divide_ratio = 0; 41079d09330Snibble.max u32 tuner_frequency, target_mclk; 411395d00d1SAntti Palosaari s32 s32tmp; 41241b9aa00SAntti Palosaari 413395d00d1SAntti Palosaari dev_dbg(&priv->i2c->dev, 414395d00d1SAntti Palosaari "%s: delivery_system=%d modulation=%d frequency=%d symbol_rate=%d inversion=%d pilot=%d rolloff=%d\n", 415395d00d1SAntti Palosaari __func__, c->delivery_system, 416395d00d1SAntti Palosaari c->modulation, c->frequency, c->symbol_rate, 417395d00d1SAntti Palosaari c->inversion, c->pilot, c->rolloff); 418395d00d1SAntti Palosaari 419395d00d1SAntti Palosaari if (!priv->warm) { 420395d00d1SAntti Palosaari ret = -EAGAIN; 421395d00d1SAntti Palosaari goto err; 422395d00d1SAntti Palosaari } 423395d00d1SAntti Palosaari 424f4df95bcSnibble.max /* reset */ 425f4df95bcSnibble.max ret = m88ds3103_wr_reg(priv, 0x07, 0x80); 426f4df95bcSnibble.max if (ret) 427f4df95bcSnibble.max goto err; 428f4df95bcSnibble.max 429f4df95bcSnibble.max ret = m88ds3103_wr_reg(priv, 0x07, 0x00); 430f4df95bcSnibble.max if (ret) 431f4df95bcSnibble.max goto err; 432f4df95bcSnibble.max 433f4df95bcSnibble.max /* Disable demod clock path */ 434f4df95bcSnibble.max if (priv->chip_id == M88RS6000_CHIP_ID) { 435f4df95bcSnibble.max ret = m88ds3103_wr_reg(priv, 0x06, 0xe0); 436f4df95bcSnibble.max if (ret) 437f4df95bcSnibble.max goto err; 438f4df95bcSnibble.max } 439f4df95bcSnibble.max 440395d00d1SAntti Palosaari /* program tuner */ 441395d00d1SAntti Palosaari if (fe->ops.tuner_ops.set_params) { 442395d00d1SAntti Palosaari ret = fe->ops.tuner_ops.set_params(fe); 443395d00d1SAntti Palosaari if (ret) 444395d00d1SAntti Palosaari goto err; 445395d00d1SAntti Palosaari } 446395d00d1SAntti Palosaari 447395d00d1SAntti Palosaari if (fe->ops.tuner_ops.get_frequency) { 448395d00d1SAntti Palosaari ret = fe->ops.tuner_ops.get_frequency(fe, &tuner_frequency); 449395d00d1SAntti Palosaari if (ret) 450395d00d1SAntti Palosaari goto err; 4512f9dff3fSAntti Palosaari } else { 4522f9dff3fSAntti Palosaari /* 4532f9dff3fSAntti Palosaari * Use nominal target frequency as tuner driver does not provide 4542f9dff3fSAntti Palosaari * actual frequency used. Carrier offset calculation is not 4552f9dff3fSAntti Palosaari * valid. 4562f9dff3fSAntti Palosaari */ 4572f9dff3fSAntti Palosaari tuner_frequency = c->frequency; 458395d00d1SAntti Palosaari } 459395d00d1SAntti Palosaari 460f4df95bcSnibble.max /* select M88RS6000 demod main mclk and ts mclk from tuner die. */ 461f4df95bcSnibble.max if (priv->chip_id == M88RS6000_CHIP_ID) { 462f4df95bcSnibble.max if (c->symbol_rate > 45010000) 463f4df95bcSnibble.max priv->mclk_khz = 110250; 464f4df95bcSnibble.max else 465f4df95bcSnibble.max priv->mclk_khz = 96000; 466395d00d1SAntti Palosaari 467f4df95bcSnibble.max if (c->delivery_system == SYS_DVBS) 468395d00d1SAntti Palosaari target_mclk = 96000; 469f4df95bcSnibble.max else 470f4df95bcSnibble.max target_mclk = 144000; 471395d00d1SAntti Palosaari 472f4df95bcSnibble.max /* Enable demod clock path */ 473f4df95bcSnibble.max ret = m88ds3103_wr_reg(priv, 0x06, 0x00); 474f4df95bcSnibble.max if (ret) 475f4df95bcSnibble.max goto err; 476f4df95bcSnibble.max usleep_range(10000, 20000); 477f4df95bcSnibble.max } else { 478f4df95bcSnibble.max /* set M88DS3103 mclk and ts mclk. */ 479f4df95bcSnibble.max priv->mclk_khz = 96000; 480f4df95bcSnibble.max 481395d00d1SAntti Palosaari switch (priv->cfg->ts_mode) { 482395d00d1SAntti Palosaari case M88DS3103_TS_SERIAL: 483395d00d1SAntti Palosaari case M88DS3103_TS_SERIAL_D7: 484b6851419Snibble.max target_mclk = priv->cfg->ts_clk; 485395d00d1SAntti Palosaari break; 486395d00d1SAntti Palosaari case M88DS3103_TS_PARALLEL: 487395d00d1SAntti Palosaari case M88DS3103_TS_CI: 488b6851419Snibble.max if (c->delivery_system == SYS_DVBS) 489b6851419Snibble.max target_mclk = 96000; 490b6851419Snibble.max else { 491395d00d1SAntti Palosaari if (c->symbol_rate < 18000000) 492395d00d1SAntti Palosaari target_mclk = 96000; 493395d00d1SAntti Palosaari else if (c->symbol_rate < 28000000) 494395d00d1SAntti Palosaari target_mclk = 144000; 495395d00d1SAntti Palosaari else 496395d00d1SAntti Palosaari target_mclk = 192000; 497b6851419Snibble.max } 498395d00d1SAntti Palosaari break; 499395d00d1SAntti Palosaari default: 500395d00d1SAntti Palosaari dev_dbg(&priv->i2c->dev, "%s: invalid ts_mode\n", 501395d00d1SAntti Palosaari __func__); 502395d00d1SAntti Palosaari ret = -EINVAL; 503395d00d1SAntti Palosaari goto err; 504395d00d1SAntti Palosaari } 505f4df95bcSnibble.max 506f4df95bcSnibble.max switch (target_mclk) { 507f4df95bcSnibble.max case 96000: 508f4df95bcSnibble.max u8tmp1 = 0x02; /* 0b10 */ 509f4df95bcSnibble.max u8tmp2 = 0x01; /* 0b01 */ 510f4df95bcSnibble.max break; 511f4df95bcSnibble.max case 144000: 512f4df95bcSnibble.max u8tmp1 = 0x00; /* 0b00 */ 513f4df95bcSnibble.max u8tmp2 = 0x01; /* 0b01 */ 514f4df95bcSnibble.max break; 515f4df95bcSnibble.max case 192000: 516f4df95bcSnibble.max u8tmp1 = 0x03; /* 0b11 */ 517f4df95bcSnibble.max u8tmp2 = 0x00; /* 0b00 */ 518f4df95bcSnibble.max break; 519f4df95bcSnibble.max } 520f4df95bcSnibble.max ret = m88ds3103_wr_reg_mask(priv, 0x22, u8tmp1 << 6, 0xc0); 521f4df95bcSnibble.max if (ret) 522f4df95bcSnibble.max goto err; 523f4df95bcSnibble.max ret = m88ds3103_wr_reg_mask(priv, 0x24, u8tmp2 << 6, 0xc0); 524f4df95bcSnibble.max if (ret) 525f4df95bcSnibble.max goto err; 526f4df95bcSnibble.max } 527f4df95bcSnibble.max 528f4df95bcSnibble.max ret = m88ds3103_wr_reg(priv, 0xb2, 0x01); 529f4df95bcSnibble.max if (ret) 530f4df95bcSnibble.max goto err; 531f4df95bcSnibble.max 532f4df95bcSnibble.max ret = m88ds3103_wr_reg(priv, 0x00, 0x01); 533f4df95bcSnibble.max if (ret) 534f4df95bcSnibble.max goto err; 535f4df95bcSnibble.max 536f4df95bcSnibble.max switch (c->delivery_system) { 537f4df95bcSnibble.max case SYS_DVBS: 538f4df95bcSnibble.max if (priv->chip_id == M88RS6000_CHIP_ID) { 539f4df95bcSnibble.max len = ARRAY_SIZE(m88rs6000_dvbs_init_reg_vals); 540f4df95bcSnibble.max init = m88rs6000_dvbs_init_reg_vals; 541f4df95bcSnibble.max } else { 542f4df95bcSnibble.max len = ARRAY_SIZE(m88ds3103_dvbs_init_reg_vals); 543f4df95bcSnibble.max init = m88ds3103_dvbs_init_reg_vals; 544f4df95bcSnibble.max } 545f4df95bcSnibble.max break; 546f4df95bcSnibble.max case SYS_DVBS2: 547f4df95bcSnibble.max if (priv->chip_id == M88RS6000_CHIP_ID) { 548f4df95bcSnibble.max len = ARRAY_SIZE(m88rs6000_dvbs2_init_reg_vals); 549f4df95bcSnibble.max init = m88rs6000_dvbs2_init_reg_vals; 550f4df95bcSnibble.max } else { 551f4df95bcSnibble.max len = ARRAY_SIZE(m88ds3103_dvbs2_init_reg_vals); 552f4df95bcSnibble.max init = m88ds3103_dvbs2_init_reg_vals; 553f4df95bcSnibble.max } 554395d00d1SAntti Palosaari break; 555395d00d1SAntti Palosaari default: 556395d00d1SAntti Palosaari dev_dbg(&priv->i2c->dev, "%s: invalid delivery_system\n", 557395d00d1SAntti Palosaari __func__); 558395d00d1SAntti Palosaari ret = -EINVAL; 559395d00d1SAntti Palosaari goto err; 560395d00d1SAntti Palosaari } 561395d00d1SAntti Palosaari 562395d00d1SAntti Palosaari /* program init table */ 563395d00d1SAntti Palosaari if (c->delivery_system != priv->delivery_system) { 56406487deeSAntti Palosaari ret = m88ds3103_wr_reg_val_tab(priv, init, len); 565395d00d1SAntti Palosaari if (ret) 566395d00d1SAntti Palosaari goto err; 567395d00d1SAntti Palosaari } 568395d00d1SAntti Palosaari 569f4df95bcSnibble.max if (priv->chip_id == M88RS6000_CHIP_ID) { 570f4df95bcSnibble.max if ((c->delivery_system == SYS_DVBS2) 571f4df95bcSnibble.max && ((c->symbol_rate / 1000) <= 5000)) { 572f4df95bcSnibble.max ret = m88ds3103_wr_reg(priv, 0xc0, 0x04); 573f4df95bcSnibble.max if (ret) 574f4df95bcSnibble.max goto err; 575f4df95bcSnibble.max buf[0] = 0x09; 576f4df95bcSnibble.max buf[1] = 0x22; 577f4df95bcSnibble.max buf[2] = 0x88; 578f4df95bcSnibble.max ret = m88ds3103_wr_regs(priv, 0x8a, buf, 3); 579f4df95bcSnibble.max if (ret) 580f4df95bcSnibble.max goto err; 581f4df95bcSnibble.max } 582f4df95bcSnibble.max ret = m88ds3103_wr_reg_mask(priv, 0x9d, 0x08, 0x08); 583f4df95bcSnibble.max if (ret) 584f4df95bcSnibble.max goto err; 585f4df95bcSnibble.max ret = m88ds3103_wr_reg(priv, 0xf1, 0x01); 586f4df95bcSnibble.max if (ret) 587f4df95bcSnibble.max goto err; 588f4df95bcSnibble.max ret = m88ds3103_wr_reg_mask(priv, 0x30, 0x80, 0x80); 589f4df95bcSnibble.max if (ret) 590f4df95bcSnibble.max goto err; 591f4df95bcSnibble.max } 592f4df95bcSnibble.max 593395d00d1SAntti Palosaari switch (priv->cfg->ts_mode) { 594395d00d1SAntti Palosaari case M88DS3103_TS_SERIAL: 595395d00d1SAntti Palosaari u8tmp1 = 0x00; 59679d09330Snibble.max u8tmp = 0x06; 597395d00d1SAntti Palosaari break; 598395d00d1SAntti Palosaari case M88DS3103_TS_SERIAL_D7: 599395d00d1SAntti Palosaari u8tmp1 = 0x20; 60079d09330Snibble.max u8tmp = 0x06; 601395d00d1SAntti Palosaari break; 602395d00d1SAntti Palosaari case M88DS3103_TS_PARALLEL: 60379d09330Snibble.max u8tmp = 0x02; 604395d00d1SAntti Palosaari break; 605395d00d1SAntti Palosaari case M88DS3103_TS_CI: 60679d09330Snibble.max u8tmp = 0x03; 607395d00d1SAntti Palosaari break; 608395d00d1SAntti Palosaari default: 609395d00d1SAntti Palosaari dev_dbg(&priv->i2c->dev, "%s: invalid ts_mode\n", __func__); 610395d00d1SAntti Palosaari ret = -EINVAL; 611395d00d1SAntti Palosaari goto err; 612395d00d1SAntti Palosaari } 613395d00d1SAntti Palosaari 61479d09330Snibble.max if (priv->cfg->ts_clk_pol) 61579d09330Snibble.max u8tmp |= 0x40; 61679d09330Snibble.max 617395d00d1SAntti Palosaari /* TS mode */ 61892676ac9SAntti Palosaari ret = m88ds3103_wr_reg(priv, 0xfd, u8tmp); 619395d00d1SAntti Palosaari if (ret) 620395d00d1SAntti Palosaari goto err; 621395d00d1SAntti Palosaari 622395d00d1SAntti Palosaari switch (priv->cfg->ts_mode) { 623395d00d1SAntti Palosaari case M88DS3103_TS_SERIAL: 624395d00d1SAntti Palosaari case M88DS3103_TS_SERIAL_D7: 625395d00d1SAntti Palosaari ret = m88ds3103_wr_reg_mask(priv, 0x29, u8tmp1, 0x20); 626395d00d1SAntti Palosaari if (ret) 627395d00d1SAntti Palosaari goto err; 628b6851419Snibble.max u8tmp1 = 0; 629b6851419Snibble.max u8tmp2 = 0; 630b6851419Snibble.max break; 631b6851419Snibble.max default: 63279d09330Snibble.max if (priv->cfg->ts_clk) { 63379d09330Snibble.max divide_ratio = DIV_ROUND_UP(target_mclk, priv->cfg->ts_clk); 634395d00d1SAntti Palosaari u8tmp1 = divide_ratio / 2; 635395d00d1SAntti Palosaari u8tmp2 = DIV_ROUND_UP(divide_ratio, 2); 636b6851419Snibble.max } 637395d00d1SAntti Palosaari } 638395d00d1SAntti Palosaari 639395d00d1SAntti Palosaari dev_dbg(&priv->i2c->dev, 640395d00d1SAntti Palosaari "%s: target_mclk=%d ts_clk=%d divide_ratio=%d\n", 64179d09330Snibble.max __func__, target_mclk, priv->cfg->ts_clk, divide_ratio); 642395d00d1SAntti Palosaari 643395d00d1SAntti Palosaari u8tmp1--; 644395d00d1SAntti Palosaari u8tmp2--; 645395d00d1SAntti Palosaari /* u8tmp1[5:2] => fe[3:0], u8tmp1[1:0] => ea[7:6] */ 646395d00d1SAntti Palosaari u8tmp1 &= 0x3f; 647395d00d1SAntti Palosaari /* u8tmp2[5:0] => ea[5:0] */ 648395d00d1SAntti Palosaari u8tmp2 &= 0x3f; 649395d00d1SAntti Palosaari 650395d00d1SAntti Palosaari ret = m88ds3103_rd_reg(priv, 0xfe, &u8tmp); 651395d00d1SAntti Palosaari if (ret) 652395d00d1SAntti Palosaari goto err; 653395d00d1SAntti Palosaari 654395d00d1SAntti Palosaari u8tmp = ((u8tmp & 0xf0) << 0) | u8tmp1 >> 2; 655395d00d1SAntti Palosaari ret = m88ds3103_wr_reg(priv, 0xfe, u8tmp); 656395d00d1SAntti Palosaari if (ret) 657395d00d1SAntti Palosaari goto err; 658395d00d1SAntti Palosaari 659395d00d1SAntti Palosaari u8tmp = ((u8tmp1 & 0x03) << 6) | u8tmp2 >> 0; 660395d00d1SAntti Palosaari ret = m88ds3103_wr_reg(priv, 0xea, u8tmp); 661395d00d1SAntti Palosaari if (ret) 662395d00d1SAntti Palosaari goto err; 663395d00d1SAntti Palosaari 664395d00d1SAntti Palosaari if (c->symbol_rate <= 3000000) 665395d00d1SAntti Palosaari u8tmp = 0x20; 666395d00d1SAntti Palosaari else if (c->symbol_rate <= 10000000) 667395d00d1SAntti Palosaari u8tmp = 0x10; 668395d00d1SAntti Palosaari else 669395d00d1SAntti Palosaari u8tmp = 0x06; 670395d00d1SAntti Palosaari 671395d00d1SAntti Palosaari ret = m88ds3103_wr_reg(priv, 0xc3, 0x08); 672395d00d1SAntti Palosaari if (ret) 673395d00d1SAntti Palosaari goto err; 674395d00d1SAntti Palosaari 675395d00d1SAntti Palosaari ret = m88ds3103_wr_reg(priv, 0xc8, u8tmp); 676395d00d1SAntti Palosaari if (ret) 677395d00d1SAntti Palosaari goto err; 678395d00d1SAntti Palosaari 679395d00d1SAntti Palosaari ret = m88ds3103_wr_reg(priv, 0xc4, 0x08); 680395d00d1SAntti Palosaari if (ret) 681395d00d1SAntti Palosaari goto err; 682395d00d1SAntti Palosaari 683395d00d1SAntti Palosaari ret = m88ds3103_wr_reg(priv, 0xc7, 0x00); 684395d00d1SAntti Palosaari if (ret) 685395d00d1SAntti Palosaari goto err; 686395d00d1SAntti Palosaari 687f4df95bcSnibble.max u16tmp = DIV_ROUND_CLOSEST((c->symbol_rate / 1000) << 15, priv->mclk_khz / 2); 688395d00d1SAntti Palosaari buf[0] = (u16tmp >> 0) & 0xff; 689395d00d1SAntti Palosaari buf[1] = (u16tmp >> 8) & 0xff; 690395d00d1SAntti Palosaari ret = m88ds3103_wr_regs(priv, 0x61, buf, 2); 691395d00d1SAntti Palosaari if (ret) 692395d00d1SAntti Palosaari goto err; 693395d00d1SAntti Palosaari 694395d00d1SAntti Palosaari ret = m88ds3103_wr_reg_mask(priv, 0x4d, priv->cfg->spec_inv << 1, 0x02); 695395d00d1SAntti Palosaari if (ret) 696395d00d1SAntti Palosaari goto err; 697395d00d1SAntti Palosaari 698395d00d1SAntti Palosaari ret = m88ds3103_wr_reg_mask(priv, 0x30, priv->cfg->agc_inv << 4, 0x10); 699395d00d1SAntti Palosaari if (ret) 700395d00d1SAntti Palosaari goto err; 701395d00d1SAntti Palosaari 702395d00d1SAntti Palosaari ret = m88ds3103_wr_reg(priv, 0x33, priv->cfg->agc); 703395d00d1SAntti Palosaari if (ret) 704395d00d1SAntti Palosaari goto err; 705395d00d1SAntti Palosaari 706395d00d1SAntti Palosaari dev_dbg(&priv->i2c->dev, "%s: carrier offset=%d\n", __func__, 707395d00d1SAntti Palosaari (tuner_frequency - c->frequency)); 708395d00d1SAntti Palosaari 709395d00d1SAntti Palosaari s32tmp = 0x10000 * (tuner_frequency - c->frequency); 710f4df95bcSnibble.max s32tmp = DIV_ROUND_CLOSEST(s32tmp, priv->mclk_khz); 711395d00d1SAntti Palosaari if (s32tmp < 0) 712395d00d1SAntti Palosaari s32tmp += 0x10000; 713395d00d1SAntti Palosaari 714395d00d1SAntti Palosaari buf[0] = (s32tmp >> 0) & 0xff; 715395d00d1SAntti Palosaari buf[1] = (s32tmp >> 8) & 0xff; 716395d00d1SAntti Palosaari ret = m88ds3103_wr_regs(priv, 0x5e, buf, 2); 717395d00d1SAntti Palosaari if (ret) 718395d00d1SAntti Palosaari goto err; 719395d00d1SAntti Palosaari 720395d00d1SAntti Palosaari ret = m88ds3103_wr_reg(priv, 0x00, 0x00); 721395d00d1SAntti Palosaari if (ret) 722395d00d1SAntti Palosaari goto err; 723395d00d1SAntti Palosaari 724395d00d1SAntti Palosaari ret = m88ds3103_wr_reg(priv, 0xb2, 0x00); 725395d00d1SAntti Palosaari if (ret) 726395d00d1SAntti Palosaari goto err; 727395d00d1SAntti Palosaari 728395d00d1SAntti Palosaari priv->delivery_system = c->delivery_system; 729395d00d1SAntti Palosaari 730395d00d1SAntti Palosaari return 0; 731395d00d1SAntti Palosaari err: 732395d00d1SAntti Palosaari dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret); 733395d00d1SAntti Palosaari return ret; 734395d00d1SAntti Palosaari } 735395d00d1SAntti Palosaari 736395d00d1SAntti Palosaari static int m88ds3103_init(struct dvb_frontend *fe) 737395d00d1SAntti Palosaari { 738395d00d1SAntti Palosaari struct m88ds3103_priv *priv = fe->demodulator_priv; 739c1daf651SAntti Palosaari struct dtv_frontend_properties *c = &fe->dtv_property_cache; 740395d00d1SAntti Palosaari int ret, len, remaining; 741395d00d1SAntti Palosaari const struct firmware *fw = NULL; 742f4df95bcSnibble.max u8 *fw_file; 743395d00d1SAntti Palosaari u8 u8tmp; 74441b9aa00SAntti Palosaari 745395d00d1SAntti Palosaari dev_dbg(&priv->i2c->dev, "%s:\n", __func__); 746395d00d1SAntti Palosaari 747395d00d1SAntti Palosaari /* set cold state by default */ 748395d00d1SAntti Palosaari priv->warm = false; 749395d00d1SAntti Palosaari 750395d00d1SAntti Palosaari /* wake up device from sleep */ 751395d00d1SAntti Palosaari ret = m88ds3103_wr_reg_mask(priv, 0x08, 0x01, 0x01); 752395d00d1SAntti Palosaari if (ret) 753395d00d1SAntti Palosaari goto err; 754395d00d1SAntti Palosaari 755395d00d1SAntti Palosaari ret = m88ds3103_wr_reg_mask(priv, 0x04, 0x00, 0x01); 756395d00d1SAntti Palosaari if (ret) 757395d00d1SAntti Palosaari goto err; 758395d00d1SAntti Palosaari 759395d00d1SAntti Palosaari ret = m88ds3103_wr_reg_mask(priv, 0x23, 0x00, 0x10); 760395d00d1SAntti Palosaari if (ret) 761395d00d1SAntti Palosaari goto err; 762395d00d1SAntti Palosaari 763395d00d1SAntti Palosaari /* firmware status */ 764395d00d1SAntti Palosaari ret = m88ds3103_rd_reg(priv, 0xb9, &u8tmp); 765395d00d1SAntti Palosaari if (ret) 766395d00d1SAntti Palosaari goto err; 767395d00d1SAntti Palosaari 768395d00d1SAntti Palosaari dev_dbg(&priv->i2c->dev, "%s: firmware=%02x\n", __func__, u8tmp); 769395d00d1SAntti Palosaari 770395d00d1SAntti Palosaari if (u8tmp) 771395d00d1SAntti Palosaari goto skip_fw_download; 772395d00d1SAntti Palosaari 773f4df95bcSnibble.max /* global reset, global diseqc reset, golbal fec reset */ 774f4df95bcSnibble.max ret = m88ds3103_wr_reg(priv, 0x07, 0xe0); 775f4df95bcSnibble.max if (ret) 776f4df95bcSnibble.max goto err; 777f4df95bcSnibble.max 778f4df95bcSnibble.max ret = m88ds3103_wr_reg(priv, 0x07, 0x00); 779f4df95bcSnibble.max if (ret) 780f4df95bcSnibble.max goto err; 781f4df95bcSnibble.max 782395d00d1SAntti Palosaari /* cold state - try to download firmware */ 783395d00d1SAntti Palosaari dev_info(&priv->i2c->dev, "%s: found a '%s' in cold state\n", 784395d00d1SAntti Palosaari KBUILD_MODNAME, m88ds3103_ops.info.name); 785395d00d1SAntti Palosaari 786f4df95bcSnibble.max if (priv->chip_id == M88RS6000_CHIP_ID) 787f4df95bcSnibble.max fw_file = M88RS6000_FIRMWARE; 788f4df95bcSnibble.max else 789f4df95bcSnibble.max fw_file = M88DS3103_FIRMWARE; 790395d00d1SAntti Palosaari /* request the firmware, this will block and timeout */ 791395d00d1SAntti Palosaari ret = request_firmware(&fw, fw_file, priv->i2c->dev.parent); 792395d00d1SAntti Palosaari if (ret) { 793a87a4d34SYannick Guerrini dev_err(&priv->i2c->dev, "%s: firmware file '%s' not found\n", 794395d00d1SAntti Palosaari KBUILD_MODNAME, fw_file); 795395d00d1SAntti Palosaari goto err; 796395d00d1SAntti Palosaari } 797395d00d1SAntti Palosaari 798395d00d1SAntti Palosaari dev_info(&priv->i2c->dev, "%s: downloading firmware from file '%s'\n", 799395d00d1SAntti Palosaari KBUILD_MODNAME, fw_file); 800395d00d1SAntti Palosaari 801395d00d1SAntti Palosaari ret = m88ds3103_wr_reg(priv, 0xb2, 0x01); 802395d00d1SAntti Palosaari if (ret) 8035ed0cf88SMarkus Elfring goto error_fw_release; 804395d00d1SAntti Palosaari 805395d00d1SAntti Palosaari for (remaining = fw->size; remaining > 0; 806395d00d1SAntti Palosaari remaining -= (priv->cfg->i2c_wr_max - 1)) { 807395d00d1SAntti Palosaari len = remaining; 808395d00d1SAntti Palosaari if (len > (priv->cfg->i2c_wr_max - 1)) 809395d00d1SAntti Palosaari len = (priv->cfg->i2c_wr_max - 1); 810395d00d1SAntti Palosaari 811395d00d1SAntti Palosaari ret = m88ds3103_wr_regs(priv, 0xb0, 812395d00d1SAntti Palosaari &fw->data[fw->size - remaining], len); 813395d00d1SAntti Palosaari if (ret) { 814395d00d1SAntti Palosaari dev_err(&priv->i2c->dev, 815395d00d1SAntti Palosaari "%s: firmware download failed=%d\n", 816395d00d1SAntti Palosaari KBUILD_MODNAME, ret); 8175ed0cf88SMarkus Elfring goto error_fw_release; 818395d00d1SAntti Palosaari } 819395d00d1SAntti Palosaari } 820395d00d1SAntti Palosaari 821395d00d1SAntti Palosaari ret = m88ds3103_wr_reg(priv, 0xb2, 0x00); 822395d00d1SAntti Palosaari if (ret) 8235ed0cf88SMarkus Elfring goto error_fw_release; 824395d00d1SAntti Palosaari 825395d00d1SAntti Palosaari release_firmware(fw); 826395d00d1SAntti Palosaari fw = NULL; 827395d00d1SAntti Palosaari 828395d00d1SAntti Palosaari ret = m88ds3103_rd_reg(priv, 0xb9, &u8tmp); 829395d00d1SAntti Palosaari if (ret) 830395d00d1SAntti Palosaari goto err; 831395d00d1SAntti Palosaari 832395d00d1SAntti Palosaari if (!u8tmp) { 833395d00d1SAntti Palosaari dev_info(&priv->i2c->dev, "%s: firmware did not run\n", 834395d00d1SAntti Palosaari KBUILD_MODNAME); 835395d00d1SAntti Palosaari ret = -EFAULT; 836395d00d1SAntti Palosaari goto err; 837395d00d1SAntti Palosaari } 838395d00d1SAntti Palosaari 839395d00d1SAntti Palosaari dev_info(&priv->i2c->dev, "%s: found a '%s' in warm state\n", 840395d00d1SAntti Palosaari KBUILD_MODNAME, m88ds3103_ops.info.name); 841395d00d1SAntti Palosaari dev_info(&priv->i2c->dev, "%s: firmware version %X.%X\n", 842395d00d1SAntti Palosaari KBUILD_MODNAME, (u8tmp >> 4) & 0xf, (u8tmp >> 0 & 0xf)); 843395d00d1SAntti Palosaari 844395d00d1SAntti Palosaari skip_fw_download: 845395d00d1SAntti Palosaari /* warm state */ 846395d00d1SAntti Palosaari priv->warm = true; 847c1daf651SAntti Palosaari /* init stats here in order signal app which stats are supported */ 848c1daf651SAntti Palosaari c->cnr.len = 1; 849c1daf651SAntti Palosaari c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE; 850ce80d713SAntti Palosaari c->post_bit_error.len = 1; 851ce80d713SAntti Palosaari c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE; 852ce80d713SAntti Palosaari c->post_bit_count.len = 1; 853ce80d713SAntti Palosaari c->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE; 854395d00d1SAntti Palosaari return 0; 855395d00d1SAntti Palosaari 8565ed0cf88SMarkus Elfring error_fw_release: 8575ed0cf88SMarkus Elfring release_firmware(fw); 8585ed0cf88SMarkus Elfring err: 859395d00d1SAntti Palosaari dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret); 860395d00d1SAntti Palosaari return ret; 861395d00d1SAntti Palosaari } 862395d00d1SAntti Palosaari 863395d00d1SAntti Palosaari static int m88ds3103_sleep(struct dvb_frontend *fe) 864395d00d1SAntti Palosaari { 865395d00d1SAntti Palosaari struct m88ds3103_priv *priv = fe->demodulator_priv; 866395d00d1SAntti Palosaari int ret; 867f4df95bcSnibble.max u8 u8tmp; 86841b9aa00SAntti Palosaari 869395d00d1SAntti Palosaari dev_dbg(&priv->i2c->dev, "%s:\n", __func__); 870395d00d1SAntti Palosaari 871c1daf651SAntti Palosaari priv->fe_status = 0; 872395d00d1SAntti Palosaari priv->delivery_system = SYS_UNDEFINED; 873395d00d1SAntti Palosaari 874395d00d1SAntti Palosaari /* TS Hi-Z */ 875f4df95bcSnibble.max if (priv->chip_id == M88RS6000_CHIP_ID) 876f4df95bcSnibble.max u8tmp = 0x29; 877f4df95bcSnibble.max else 878f4df95bcSnibble.max u8tmp = 0x27; 879f4df95bcSnibble.max ret = m88ds3103_wr_reg_mask(priv, u8tmp, 0x00, 0x01); 880395d00d1SAntti Palosaari if (ret) 881395d00d1SAntti Palosaari goto err; 882395d00d1SAntti Palosaari 883395d00d1SAntti Palosaari /* sleep */ 884395d00d1SAntti Palosaari ret = m88ds3103_wr_reg_mask(priv, 0x08, 0x00, 0x01); 885395d00d1SAntti Palosaari if (ret) 886395d00d1SAntti Palosaari goto err; 887395d00d1SAntti Palosaari 888395d00d1SAntti Palosaari ret = m88ds3103_wr_reg_mask(priv, 0x04, 0x01, 0x01); 889395d00d1SAntti Palosaari if (ret) 890395d00d1SAntti Palosaari goto err; 891395d00d1SAntti Palosaari 892395d00d1SAntti Palosaari ret = m88ds3103_wr_reg_mask(priv, 0x23, 0x10, 0x10); 893395d00d1SAntti Palosaari if (ret) 894395d00d1SAntti Palosaari goto err; 895395d00d1SAntti Palosaari 896395d00d1SAntti Palosaari return 0; 897395d00d1SAntti Palosaari err: 898395d00d1SAntti Palosaari dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret); 899395d00d1SAntti Palosaari return ret; 900395d00d1SAntti Palosaari } 901395d00d1SAntti Palosaari 902395d00d1SAntti Palosaari static int m88ds3103_get_frontend(struct dvb_frontend *fe) 903395d00d1SAntti Palosaari { 904395d00d1SAntti Palosaari struct m88ds3103_priv *priv = fe->demodulator_priv; 905395d00d1SAntti Palosaari struct dtv_frontend_properties *c = &fe->dtv_property_cache; 906395d00d1SAntti Palosaari int ret; 907395d00d1SAntti Palosaari u8 buf[3]; 90841b9aa00SAntti Palosaari 909395d00d1SAntti Palosaari dev_dbg(&priv->i2c->dev, "%s:\n", __func__); 910395d00d1SAntti Palosaari 911395d00d1SAntti Palosaari if (!priv->warm || !(priv->fe_status & FE_HAS_LOCK)) { 9129240c384SAntti Palosaari ret = 0; 913395d00d1SAntti Palosaari goto err; 914395d00d1SAntti Palosaari } 915395d00d1SAntti Palosaari 916395d00d1SAntti Palosaari switch (c->delivery_system) { 917395d00d1SAntti Palosaari case SYS_DVBS: 918395d00d1SAntti Palosaari ret = m88ds3103_rd_reg(priv, 0xe0, &buf[0]); 919395d00d1SAntti Palosaari if (ret) 920395d00d1SAntti Palosaari goto err; 921395d00d1SAntti Palosaari 922395d00d1SAntti Palosaari ret = m88ds3103_rd_reg(priv, 0xe6, &buf[1]); 923395d00d1SAntti Palosaari if (ret) 924395d00d1SAntti Palosaari goto err; 925395d00d1SAntti Palosaari 926395d00d1SAntti Palosaari switch ((buf[0] >> 2) & 0x01) { 927395d00d1SAntti Palosaari case 0: 928395d00d1SAntti Palosaari c->inversion = INVERSION_OFF; 929395d00d1SAntti Palosaari break; 930395d00d1SAntti Palosaari case 1: 931395d00d1SAntti Palosaari c->inversion = INVERSION_ON; 932395d00d1SAntti Palosaari break; 933395d00d1SAntti Palosaari } 934395d00d1SAntti Palosaari 935395d00d1SAntti Palosaari switch ((buf[1] >> 5) & 0x07) { 936395d00d1SAntti Palosaari case 0: 937395d00d1SAntti Palosaari c->fec_inner = FEC_7_8; 938395d00d1SAntti Palosaari break; 939395d00d1SAntti Palosaari case 1: 940395d00d1SAntti Palosaari c->fec_inner = FEC_5_6; 941395d00d1SAntti Palosaari break; 942395d00d1SAntti Palosaari case 2: 943395d00d1SAntti Palosaari c->fec_inner = FEC_3_4; 944395d00d1SAntti Palosaari break; 945395d00d1SAntti Palosaari case 3: 946395d00d1SAntti Palosaari c->fec_inner = FEC_2_3; 947395d00d1SAntti Palosaari break; 948395d00d1SAntti Palosaari case 4: 949395d00d1SAntti Palosaari c->fec_inner = FEC_1_2; 950395d00d1SAntti Palosaari break; 951395d00d1SAntti Palosaari default: 952395d00d1SAntti Palosaari dev_dbg(&priv->i2c->dev, "%s: invalid fec_inner\n", 953395d00d1SAntti Palosaari __func__); 954395d00d1SAntti Palosaari } 955395d00d1SAntti Palosaari 956395d00d1SAntti Palosaari c->modulation = QPSK; 957395d00d1SAntti Palosaari 958395d00d1SAntti Palosaari break; 959395d00d1SAntti Palosaari case SYS_DVBS2: 960395d00d1SAntti Palosaari ret = m88ds3103_rd_reg(priv, 0x7e, &buf[0]); 961395d00d1SAntti Palosaari if (ret) 962395d00d1SAntti Palosaari goto err; 963395d00d1SAntti Palosaari 964395d00d1SAntti Palosaari ret = m88ds3103_rd_reg(priv, 0x89, &buf[1]); 965395d00d1SAntti Palosaari if (ret) 966395d00d1SAntti Palosaari goto err; 967395d00d1SAntti Palosaari 968395d00d1SAntti Palosaari ret = m88ds3103_rd_reg(priv, 0xf2, &buf[2]); 969395d00d1SAntti Palosaari if (ret) 970395d00d1SAntti Palosaari goto err; 971395d00d1SAntti Palosaari 972395d00d1SAntti Palosaari switch ((buf[0] >> 0) & 0x0f) { 973395d00d1SAntti Palosaari case 2: 974395d00d1SAntti Palosaari c->fec_inner = FEC_2_5; 975395d00d1SAntti Palosaari break; 976395d00d1SAntti Palosaari case 3: 977395d00d1SAntti Palosaari c->fec_inner = FEC_1_2; 978395d00d1SAntti Palosaari break; 979395d00d1SAntti Palosaari case 4: 980395d00d1SAntti Palosaari c->fec_inner = FEC_3_5; 981395d00d1SAntti Palosaari break; 982395d00d1SAntti Palosaari case 5: 983395d00d1SAntti Palosaari c->fec_inner = FEC_2_3; 984395d00d1SAntti Palosaari break; 985395d00d1SAntti Palosaari case 6: 986395d00d1SAntti Palosaari c->fec_inner = FEC_3_4; 987395d00d1SAntti Palosaari break; 988395d00d1SAntti Palosaari case 7: 989395d00d1SAntti Palosaari c->fec_inner = FEC_4_5; 990395d00d1SAntti Palosaari break; 991395d00d1SAntti Palosaari case 8: 992395d00d1SAntti Palosaari c->fec_inner = FEC_5_6; 993395d00d1SAntti Palosaari break; 994395d00d1SAntti Palosaari case 9: 995395d00d1SAntti Palosaari c->fec_inner = FEC_8_9; 996395d00d1SAntti Palosaari break; 997395d00d1SAntti Palosaari case 10: 998395d00d1SAntti Palosaari c->fec_inner = FEC_9_10; 999395d00d1SAntti Palosaari break; 1000395d00d1SAntti Palosaari default: 1001395d00d1SAntti Palosaari dev_dbg(&priv->i2c->dev, "%s: invalid fec_inner\n", 1002395d00d1SAntti Palosaari __func__); 1003395d00d1SAntti Palosaari } 1004395d00d1SAntti Palosaari 1005395d00d1SAntti Palosaari switch ((buf[0] >> 5) & 0x01) { 1006395d00d1SAntti Palosaari case 0: 1007395d00d1SAntti Palosaari c->pilot = PILOT_OFF; 1008395d00d1SAntti Palosaari break; 1009395d00d1SAntti Palosaari case 1: 1010395d00d1SAntti Palosaari c->pilot = PILOT_ON; 1011395d00d1SAntti Palosaari break; 1012395d00d1SAntti Palosaari } 1013395d00d1SAntti Palosaari 1014395d00d1SAntti Palosaari switch ((buf[0] >> 6) & 0x07) { 1015395d00d1SAntti Palosaari case 0: 1016395d00d1SAntti Palosaari c->modulation = QPSK; 1017395d00d1SAntti Palosaari break; 1018395d00d1SAntti Palosaari case 1: 1019395d00d1SAntti Palosaari c->modulation = PSK_8; 1020395d00d1SAntti Palosaari break; 1021395d00d1SAntti Palosaari case 2: 1022395d00d1SAntti Palosaari c->modulation = APSK_16; 1023395d00d1SAntti Palosaari break; 1024395d00d1SAntti Palosaari case 3: 1025395d00d1SAntti Palosaari c->modulation = APSK_32; 1026395d00d1SAntti Palosaari break; 1027395d00d1SAntti Palosaari default: 1028395d00d1SAntti Palosaari dev_dbg(&priv->i2c->dev, "%s: invalid modulation\n", 1029395d00d1SAntti Palosaari __func__); 1030395d00d1SAntti Palosaari } 1031395d00d1SAntti Palosaari 1032395d00d1SAntti Palosaari switch ((buf[1] >> 7) & 0x01) { 1033395d00d1SAntti Palosaari case 0: 1034395d00d1SAntti Palosaari c->inversion = INVERSION_OFF; 1035395d00d1SAntti Palosaari break; 1036395d00d1SAntti Palosaari case 1: 1037395d00d1SAntti Palosaari c->inversion = INVERSION_ON; 1038395d00d1SAntti Palosaari break; 1039395d00d1SAntti Palosaari } 1040395d00d1SAntti Palosaari 1041395d00d1SAntti Palosaari switch ((buf[2] >> 0) & 0x03) { 1042395d00d1SAntti Palosaari case 0: 1043395d00d1SAntti Palosaari c->rolloff = ROLLOFF_35; 1044395d00d1SAntti Palosaari break; 1045395d00d1SAntti Palosaari case 1: 1046395d00d1SAntti Palosaari c->rolloff = ROLLOFF_25; 1047395d00d1SAntti Palosaari break; 1048395d00d1SAntti Palosaari case 2: 1049395d00d1SAntti Palosaari c->rolloff = ROLLOFF_20; 1050395d00d1SAntti Palosaari break; 1051395d00d1SAntti Palosaari default: 1052395d00d1SAntti Palosaari dev_dbg(&priv->i2c->dev, "%s: invalid rolloff\n", 1053395d00d1SAntti Palosaari __func__); 1054395d00d1SAntti Palosaari } 1055395d00d1SAntti Palosaari break; 1056395d00d1SAntti Palosaari default: 1057395d00d1SAntti Palosaari dev_dbg(&priv->i2c->dev, "%s: invalid delivery_system\n", 1058395d00d1SAntti Palosaari __func__); 1059395d00d1SAntti Palosaari ret = -EINVAL; 1060395d00d1SAntti Palosaari goto err; 1061395d00d1SAntti Palosaari } 1062395d00d1SAntti Palosaari 1063395d00d1SAntti Palosaari ret = m88ds3103_rd_regs(priv, 0x6d, buf, 2); 1064395d00d1SAntti Palosaari if (ret) 1065395d00d1SAntti Palosaari goto err; 1066395d00d1SAntti Palosaari 1067395d00d1SAntti Palosaari c->symbol_rate = 1ull * ((buf[1] << 8) | (buf[0] << 0)) * 1068f4df95bcSnibble.max priv->mclk_khz * 1000 / 0x10000; 1069395d00d1SAntti Palosaari 1070395d00d1SAntti Palosaari return 0; 1071395d00d1SAntti Palosaari err: 1072395d00d1SAntti Palosaari dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret); 1073395d00d1SAntti Palosaari return ret; 1074395d00d1SAntti Palosaari } 1075395d00d1SAntti Palosaari 1076395d00d1SAntti Palosaari static int m88ds3103_read_snr(struct dvb_frontend *fe, u16 *snr) 1077395d00d1SAntti Palosaari { 1078395d00d1SAntti Palosaari struct dtv_frontend_properties *c = &fe->dtv_property_cache; 107941b9aa00SAntti Palosaari 1080c1daf651SAntti Palosaari if (c->cnr.stat[0].scale == FE_SCALE_DECIBEL) 1081c1daf651SAntti Palosaari *snr = div_s64(c->cnr.stat[0].svalue, 100); 1082395d00d1SAntti Palosaari else 1083395d00d1SAntti Palosaari *snr = 0; 1084395d00d1SAntti Palosaari 1085395d00d1SAntti Palosaari return 0; 1086395d00d1SAntti Palosaari } 1087395d00d1SAntti Palosaari 10884423a2baSAntti Palosaari static int m88ds3103_read_ber(struct dvb_frontend *fe, u32 *ber) 10894423a2baSAntti Palosaari { 10904423a2baSAntti Palosaari struct m88ds3103_priv *priv = fe->demodulator_priv; 109141b9aa00SAntti Palosaari 1092ce80d713SAntti Palosaari *ber = priv->dvbv3_ber; 10934423a2baSAntti Palosaari 10944423a2baSAntti Palosaari return 0; 10954423a2baSAntti Palosaari } 1096395d00d1SAntti Palosaari 1097395d00d1SAntti Palosaari static int m88ds3103_set_tone(struct dvb_frontend *fe, 1098*0df289a2SMauro Carvalho Chehab enum fe_sec_tone_mode fe_sec_tone_mode) 1099395d00d1SAntti Palosaari { 1100395d00d1SAntti Palosaari struct m88ds3103_priv *priv = fe->demodulator_priv; 1101395d00d1SAntti Palosaari int ret; 1102395d00d1SAntti Palosaari u8 u8tmp, tone, reg_a1_mask; 110341b9aa00SAntti Palosaari 1104395d00d1SAntti Palosaari dev_dbg(&priv->i2c->dev, "%s: fe_sec_tone_mode=%d\n", __func__, 1105395d00d1SAntti Palosaari fe_sec_tone_mode); 1106395d00d1SAntti Palosaari 1107395d00d1SAntti Palosaari if (!priv->warm) { 1108395d00d1SAntti Palosaari ret = -EAGAIN; 1109395d00d1SAntti Palosaari goto err; 1110395d00d1SAntti Palosaari } 1111395d00d1SAntti Palosaari 1112395d00d1SAntti Palosaari switch (fe_sec_tone_mode) { 1113395d00d1SAntti Palosaari case SEC_TONE_ON: 1114395d00d1SAntti Palosaari tone = 0; 1115418a97cbSAntti Palosaari reg_a1_mask = 0x47; 1116395d00d1SAntti Palosaari break; 1117395d00d1SAntti Palosaari case SEC_TONE_OFF: 1118395d00d1SAntti Palosaari tone = 1; 1119395d00d1SAntti Palosaari reg_a1_mask = 0x00; 1120395d00d1SAntti Palosaari break; 1121395d00d1SAntti Palosaari default: 1122395d00d1SAntti Palosaari dev_dbg(&priv->i2c->dev, "%s: invalid fe_sec_tone_mode\n", 1123395d00d1SAntti Palosaari __func__); 1124395d00d1SAntti Palosaari ret = -EINVAL; 1125395d00d1SAntti Palosaari goto err; 1126395d00d1SAntti Palosaari } 1127395d00d1SAntti Palosaari 1128395d00d1SAntti Palosaari u8tmp = tone << 7 | priv->cfg->envelope_mode << 5; 1129395d00d1SAntti Palosaari ret = m88ds3103_wr_reg_mask(priv, 0xa2, u8tmp, 0xe0); 1130395d00d1SAntti Palosaari if (ret) 1131395d00d1SAntti Palosaari goto err; 1132395d00d1SAntti Palosaari 1133395d00d1SAntti Palosaari u8tmp = 1 << 2; 1134395d00d1SAntti Palosaari ret = m88ds3103_wr_reg_mask(priv, 0xa1, u8tmp, reg_a1_mask); 1135395d00d1SAntti Palosaari if (ret) 1136395d00d1SAntti Palosaari goto err; 1137395d00d1SAntti Palosaari 1138395d00d1SAntti Palosaari return 0; 1139395d00d1SAntti Palosaari err: 1140395d00d1SAntti Palosaari dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret); 1141395d00d1SAntti Palosaari return ret; 1142395d00d1SAntti Palosaari } 1143395d00d1SAntti Palosaari 114479d09330Snibble.max static int m88ds3103_set_voltage(struct dvb_frontend *fe, 1145*0df289a2SMauro Carvalho Chehab enum fe_sec_voltage fe_sec_voltage) 114679d09330Snibble.max { 114779d09330Snibble.max struct m88ds3103_priv *priv = fe->demodulator_priv; 1148d28677ffSAntti Palosaari int ret; 1149d28677ffSAntti Palosaari u8 u8tmp; 1150d28677ffSAntti Palosaari bool voltage_sel, voltage_dis; 115179d09330Snibble.max 1152d28677ffSAntti Palosaari dev_dbg(&priv->i2c->dev, "%s: fe_sec_voltage=%d\n", __func__, 1153d28677ffSAntti Palosaari fe_sec_voltage); 115479d09330Snibble.max 1155d28677ffSAntti Palosaari if (!priv->warm) { 1156d28677ffSAntti Palosaari ret = -EAGAIN; 1157d28677ffSAntti Palosaari goto err; 1158d28677ffSAntti Palosaari } 115979d09330Snibble.max 1160d28677ffSAntti Palosaari switch (fe_sec_voltage) { 116179d09330Snibble.max case SEC_VOLTAGE_18: 1162afbd6eb4SMauro Carvalho Chehab voltage_sel = true; 1163afbd6eb4SMauro Carvalho Chehab voltage_dis = false; 116479d09330Snibble.max break; 116579d09330Snibble.max case SEC_VOLTAGE_13: 1166afbd6eb4SMauro Carvalho Chehab voltage_sel = false; 1167afbd6eb4SMauro Carvalho Chehab voltage_dis = false; 116879d09330Snibble.max break; 116979d09330Snibble.max case SEC_VOLTAGE_OFF: 1170afbd6eb4SMauro Carvalho Chehab voltage_sel = false; 1171afbd6eb4SMauro Carvalho Chehab voltage_dis = true; 117279d09330Snibble.max break; 1173d28677ffSAntti Palosaari default: 1174d28677ffSAntti Palosaari dev_dbg(&priv->i2c->dev, "%s: invalid fe_sec_voltage\n", 1175d28677ffSAntti Palosaari __func__); 1176d28677ffSAntti Palosaari ret = -EINVAL; 1177d28677ffSAntti Palosaari goto err; 117879d09330Snibble.max } 1179d28677ffSAntti Palosaari 1180d28677ffSAntti Palosaari /* output pin polarity */ 1181d28677ffSAntti Palosaari voltage_sel ^= priv->cfg->lnb_hv_pol; 1182d28677ffSAntti Palosaari voltage_dis ^= priv->cfg->lnb_en_pol; 1183d28677ffSAntti Palosaari 1184d28677ffSAntti Palosaari u8tmp = voltage_dis << 1 | voltage_sel << 0; 1185d28677ffSAntti Palosaari ret = m88ds3103_wr_reg_mask(priv, 0xa2, u8tmp, 0x03); 1186d28677ffSAntti Palosaari if (ret) 1187d28677ffSAntti Palosaari goto err; 118879d09330Snibble.max 118979d09330Snibble.max return 0; 1190d28677ffSAntti Palosaari err: 1191d28677ffSAntti Palosaari dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret); 1192d28677ffSAntti Palosaari return ret; 119379d09330Snibble.max } 119479d09330Snibble.max 1195395d00d1SAntti Palosaari static int m88ds3103_diseqc_send_master_cmd(struct dvb_frontend *fe, 1196395d00d1SAntti Palosaari struct dvb_diseqc_master_cmd *diseqc_cmd) 1197395d00d1SAntti Palosaari { 1198395d00d1SAntti Palosaari struct m88ds3103_priv *priv = fe->demodulator_priv; 1199befa0cc1SAntti Palosaari int ret; 1200befa0cc1SAntti Palosaari unsigned long timeout; 1201395d00d1SAntti Palosaari u8 u8tmp; 120241b9aa00SAntti Palosaari 1203395d00d1SAntti Palosaari dev_dbg(&priv->i2c->dev, "%s: msg=%*ph\n", __func__, 1204395d00d1SAntti Palosaari diseqc_cmd->msg_len, diseqc_cmd->msg); 1205395d00d1SAntti Palosaari 1206395d00d1SAntti Palosaari if (!priv->warm) { 1207395d00d1SAntti Palosaari ret = -EAGAIN; 1208395d00d1SAntti Palosaari goto err; 1209395d00d1SAntti Palosaari } 1210395d00d1SAntti Palosaari 1211395d00d1SAntti Palosaari if (diseqc_cmd->msg_len < 3 || diseqc_cmd->msg_len > 6) { 1212395d00d1SAntti Palosaari ret = -EINVAL; 1213395d00d1SAntti Palosaari goto err; 1214395d00d1SAntti Palosaari } 1215395d00d1SAntti Palosaari 1216395d00d1SAntti Palosaari u8tmp = priv->cfg->envelope_mode << 5; 1217395d00d1SAntti Palosaari ret = m88ds3103_wr_reg_mask(priv, 0xa2, u8tmp, 0xe0); 1218395d00d1SAntti Palosaari if (ret) 1219395d00d1SAntti Palosaari goto err; 1220395d00d1SAntti Palosaari 1221395d00d1SAntti Palosaari ret = m88ds3103_wr_regs(priv, 0xa3, diseqc_cmd->msg, 1222395d00d1SAntti Palosaari diseqc_cmd->msg_len); 1223395d00d1SAntti Palosaari if (ret) 1224395d00d1SAntti Palosaari goto err; 1225395d00d1SAntti Palosaari 1226395d00d1SAntti Palosaari ret = m88ds3103_wr_reg(priv, 0xa1, 1227395d00d1SAntti Palosaari (diseqc_cmd->msg_len - 1) << 3 | 0x07); 1228395d00d1SAntti Palosaari if (ret) 1229395d00d1SAntti Palosaari goto err; 1230395d00d1SAntti Palosaari 1231395d00d1SAntti Palosaari /* wait DiSEqC TX ready */ 1232befa0cc1SAntti Palosaari #define SEND_MASTER_CMD_TIMEOUT 120 1233befa0cc1SAntti Palosaari timeout = jiffies + msecs_to_jiffies(SEND_MASTER_CMD_TIMEOUT); 1234395d00d1SAntti Palosaari 1235befa0cc1SAntti Palosaari /* DiSEqC message typical period is 54 ms */ 1236befa0cc1SAntti Palosaari usleep_range(50000, 54000); 1237befa0cc1SAntti Palosaari 1238befa0cc1SAntti Palosaari for (u8tmp = 1; !time_after(jiffies, timeout) && u8tmp;) { 1239395d00d1SAntti Palosaari ret = m88ds3103_rd_reg_mask(priv, 0xa1, &u8tmp, 0x40); 1240395d00d1SAntti Palosaari if (ret) 1241395d00d1SAntti Palosaari goto err; 1242395d00d1SAntti Palosaari } 1243395d00d1SAntti Palosaari 1244befa0cc1SAntti Palosaari if (u8tmp == 0) { 1245befa0cc1SAntti Palosaari dev_dbg(&priv->i2c->dev, "%s: diseqc tx took %u ms\n", __func__, 1246befa0cc1SAntti Palosaari jiffies_to_msecs(jiffies) - 1247befa0cc1SAntti Palosaari (jiffies_to_msecs(timeout) - SEND_MASTER_CMD_TIMEOUT)); 1248befa0cc1SAntti Palosaari } else { 1249395d00d1SAntti Palosaari dev_dbg(&priv->i2c->dev, "%s: diseqc tx timeout\n", __func__); 1250395d00d1SAntti Palosaari 1251395d00d1SAntti Palosaari ret = m88ds3103_wr_reg_mask(priv, 0xa1, 0x40, 0xc0); 1252395d00d1SAntti Palosaari if (ret) 1253395d00d1SAntti Palosaari goto err; 1254395d00d1SAntti Palosaari } 1255395d00d1SAntti Palosaari 1256395d00d1SAntti Palosaari ret = m88ds3103_wr_reg_mask(priv, 0xa2, 0x80, 0xc0); 1257395d00d1SAntti Palosaari if (ret) 1258395d00d1SAntti Palosaari goto err; 1259395d00d1SAntti Palosaari 1260befa0cc1SAntti Palosaari if (u8tmp == 1) { 1261395d00d1SAntti Palosaari ret = -ETIMEDOUT; 1262395d00d1SAntti Palosaari goto err; 1263395d00d1SAntti Palosaari } 1264395d00d1SAntti Palosaari 1265395d00d1SAntti Palosaari return 0; 1266395d00d1SAntti Palosaari err: 1267395d00d1SAntti Palosaari dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret); 1268395d00d1SAntti Palosaari return ret; 1269395d00d1SAntti Palosaari } 1270395d00d1SAntti Palosaari 1271395d00d1SAntti Palosaari static int m88ds3103_diseqc_send_burst(struct dvb_frontend *fe, 1272*0df289a2SMauro Carvalho Chehab enum fe_sec_mini_cmd fe_sec_mini_cmd) 1273395d00d1SAntti Palosaari { 1274395d00d1SAntti Palosaari struct m88ds3103_priv *priv = fe->demodulator_priv; 1275befa0cc1SAntti Palosaari int ret; 1276befa0cc1SAntti Palosaari unsigned long timeout; 1277395d00d1SAntti Palosaari u8 u8tmp, burst; 127841b9aa00SAntti Palosaari 1279395d00d1SAntti Palosaari dev_dbg(&priv->i2c->dev, "%s: fe_sec_mini_cmd=%d\n", __func__, 1280395d00d1SAntti Palosaari fe_sec_mini_cmd); 1281395d00d1SAntti Palosaari 1282395d00d1SAntti Palosaari if (!priv->warm) { 1283395d00d1SAntti Palosaari ret = -EAGAIN; 1284395d00d1SAntti Palosaari goto err; 1285395d00d1SAntti Palosaari } 1286395d00d1SAntti Palosaari 1287395d00d1SAntti Palosaari u8tmp = priv->cfg->envelope_mode << 5; 1288395d00d1SAntti Palosaari ret = m88ds3103_wr_reg_mask(priv, 0xa2, u8tmp, 0xe0); 1289395d00d1SAntti Palosaari if (ret) 1290395d00d1SAntti Palosaari goto err; 1291395d00d1SAntti Palosaari 1292395d00d1SAntti Palosaari switch (fe_sec_mini_cmd) { 1293395d00d1SAntti Palosaari case SEC_MINI_A: 1294395d00d1SAntti Palosaari burst = 0x02; 1295395d00d1SAntti Palosaari break; 1296395d00d1SAntti Palosaari case SEC_MINI_B: 1297395d00d1SAntti Palosaari burst = 0x01; 1298395d00d1SAntti Palosaari break; 1299395d00d1SAntti Palosaari default: 1300395d00d1SAntti Palosaari dev_dbg(&priv->i2c->dev, "%s: invalid fe_sec_mini_cmd\n", 1301395d00d1SAntti Palosaari __func__); 1302395d00d1SAntti Palosaari ret = -EINVAL; 1303395d00d1SAntti Palosaari goto err; 1304395d00d1SAntti Palosaari } 1305395d00d1SAntti Palosaari 1306395d00d1SAntti Palosaari ret = m88ds3103_wr_reg(priv, 0xa1, burst); 1307395d00d1SAntti Palosaari if (ret) 1308395d00d1SAntti Palosaari goto err; 1309395d00d1SAntti Palosaari 1310395d00d1SAntti Palosaari /* wait DiSEqC TX ready */ 1311befa0cc1SAntti Palosaari #define SEND_BURST_TIMEOUT 40 1312befa0cc1SAntti Palosaari timeout = jiffies + msecs_to_jiffies(SEND_BURST_TIMEOUT); 1313395d00d1SAntti Palosaari 1314befa0cc1SAntti Palosaari /* DiSEqC ToneBurst period is 12.5 ms */ 1315befa0cc1SAntti Palosaari usleep_range(8500, 12500); 1316befa0cc1SAntti Palosaari 1317befa0cc1SAntti Palosaari for (u8tmp = 1; !time_after(jiffies, timeout) && u8tmp;) { 1318395d00d1SAntti Palosaari ret = m88ds3103_rd_reg_mask(priv, 0xa1, &u8tmp, 0x40); 1319395d00d1SAntti Palosaari if (ret) 1320395d00d1SAntti Palosaari goto err; 1321395d00d1SAntti Palosaari } 1322395d00d1SAntti Palosaari 1323befa0cc1SAntti Palosaari if (u8tmp == 0) { 1324befa0cc1SAntti Palosaari dev_dbg(&priv->i2c->dev, "%s: diseqc tx took %u ms\n", __func__, 1325befa0cc1SAntti Palosaari jiffies_to_msecs(jiffies) - 1326befa0cc1SAntti Palosaari (jiffies_to_msecs(timeout) - SEND_BURST_TIMEOUT)); 1327befa0cc1SAntti Palosaari } else { 1328befa0cc1SAntti Palosaari dev_dbg(&priv->i2c->dev, "%s: diseqc tx timeout\n", __func__); 1329befa0cc1SAntti Palosaari 1330befa0cc1SAntti Palosaari ret = m88ds3103_wr_reg_mask(priv, 0xa1, 0x40, 0xc0); 1331befa0cc1SAntti Palosaari if (ret) 1332befa0cc1SAntti Palosaari goto err; 1333befa0cc1SAntti Palosaari } 1334395d00d1SAntti Palosaari 1335395d00d1SAntti Palosaari ret = m88ds3103_wr_reg_mask(priv, 0xa2, 0x80, 0xc0); 1336395d00d1SAntti Palosaari if (ret) 1337395d00d1SAntti Palosaari goto err; 1338395d00d1SAntti Palosaari 1339befa0cc1SAntti Palosaari if (u8tmp == 1) { 1340395d00d1SAntti Palosaari ret = -ETIMEDOUT; 1341395d00d1SAntti Palosaari goto err; 1342395d00d1SAntti Palosaari } 1343395d00d1SAntti Palosaari 1344395d00d1SAntti Palosaari return 0; 1345395d00d1SAntti Palosaari err: 1346395d00d1SAntti Palosaari dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret); 1347395d00d1SAntti Palosaari return ret; 1348395d00d1SAntti Palosaari } 1349395d00d1SAntti Palosaari 1350395d00d1SAntti Palosaari static int m88ds3103_get_tune_settings(struct dvb_frontend *fe, 1351395d00d1SAntti Palosaari struct dvb_frontend_tune_settings *s) 1352395d00d1SAntti Palosaari { 1353395d00d1SAntti Palosaari s->min_delay_ms = 3000; 1354395d00d1SAntti Palosaari 1355395d00d1SAntti Palosaari return 0; 1356395d00d1SAntti Palosaari } 1357395d00d1SAntti Palosaari 135844b9055bSAntti Palosaari static void m88ds3103_release(struct dvb_frontend *fe) 1359395d00d1SAntti Palosaari { 136044b9055bSAntti Palosaari struct m88ds3103_priv *priv = fe->demodulator_priv; 1361f01919e8SAntti Palosaari struct i2c_client *client = priv->client; 136241b9aa00SAntti Palosaari 1363f01919e8SAntti Palosaari i2c_unregister_device(client); 1364395d00d1SAntti Palosaari } 1365395d00d1SAntti Palosaari 136644b9055bSAntti Palosaari static int m88ds3103_select(struct i2c_adapter *adap, void *mux_priv, u32 chan) 1367395d00d1SAntti Palosaari { 136844b9055bSAntti Palosaari struct m88ds3103_priv *priv = mux_priv; 1369395d00d1SAntti Palosaari int ret; 1370395d00d1SAntti Palosaari struct i2c_msg gate_open_msg[1] = { 1371395d00d1SAntti Palosaari { 1372395d00d1SAntti Palosaari .addr = priv->cfg->i2c_addr, 1373395d00d1SAntti Palosaari .flags = 0, 1374395d00d1SAntti Palosaari .len = 2, 1375395d00d1SAntti Palosaari .buf = "\x03\x11", 1376395d00d1SAntti Palosaari } 1377395d00d1SAntti Palosaari }; 1378395d00d1SAntti Palosaari 1379395d00d1SAntti Palosaari mutex_lock(&priv->i2c_mutex); 1380395d00d1SAntti Palosaari 138144b9055bSAntti Palosaari /* open tuner I2C repeater for 1 xfer, closes automatically */ 13824fc57876SAntti Palosaari ret = __i2c_transfer(priv->i2c, gate_open_msg, 1); 1383395d00d1SAntti Palosaari if (ret != 1) { 138444b9055bSAntti Palosaari dev_warn(&priv->i2c->dev, "%s: i2c wr failed=%d\n", 1385395d00d1SAntti Palosaari KBUILD_MODNAME, ret); 138644b9055bSAntti Palosaari if (ret >= 0) 1387395d00d1SAntti Palosaari ret = -EREMOTEIO; 1388395d00d1SAntti Palosaari 1389395d00d1SAntti Palosaari return ret; 1390395d00d1SAntti Palosaari } 1391395d00d1SAntti Palosaari 139244b9055bSAntti Palosaari return 0; 139344b9055bSAntti Palosaari } 1394395d00d1SAntti Palosaari 139544b9055bSAntti Palosaari static int m88ds3103_deselect(struct i2c_adapter *adap, void *mux_priv, 139644b9055bSAntti Palosaari u32 chan) 1397395d00d1SAntti Palosaari { 139844b9055bSAntti Palosaari struct m88ds3103_priv *priv = mux_priv; 139944b9055bSAntti Palosaari 140044b9055bSAntti Palosaari mutex_unlock(&priv->i2c_mutex); 140144b9055bSAntti Palosaari 140244b9055bSAntti Palosaari return 0; 1403395d00d1SAntti Palosaari } 1404395d00d1SAntti Palosaari 1405f01919e8SAntti Palosaari /* 1406f01919e8SAntti Palosaari * XXX: That is wrapper to m88ds3103_probe() via driver core in order to provide 1407f01919e8SAntti Palosaari * proper I2C client for legacy media attach binding. 1408f01919e8SAntti Palosaari * New users must use I2C client binding directly! 1409f01919e8SAntti Palosaari */ 1410395d00d1SAntti Palosaari struct dvb_frontend *m88ds3103_attach(const struct m88ds3103_config *cfg, 1411395d00d1SAntti Palosaari struct i2c_adapter *i2c, struct i2c_adapter **tuner_i2c_adapter) 1412395d00d1SAntti Palosaari { 1413f01919e8SAntti Palosaari struct i2c_client *client; 1414f01919e8SAntti Palosaari struct i2c_board_info board_info; 1415f01919e8SAntti Palosaari struct m88ds3103_platform_data pdata; 1416395d00d1SAntti Palosaari 1417f01919e8SAntti Palosaari pdata.clk = cfg->clock; 1418f01919e8SAntti Palosaari pdata.i2c_wr_max = cfg->i2c_wr_max; 1419f01919e8SAntti Palosaari pdata.ts_mode = cfg->ts_mode; 1420f01919e8SAntti Palosaari pdata.ts_clk = cfg->ts_clk; 1421f01919e8SAntti Palosaari pdata.ts_clk_pol = cfg->ts_clk_pol; 1422f01919e8SAntti Palosaari pdata.spec_inv = cfg->spec_inv; 1423f01919e8SAntti Palosaari pdata.agc = cfg->agc; 1424f01919e8SAntti Palosaari pdata.agc_inv = cfg->agc_inv; 1425f01919e8SAntti Palosaari pdata.clk_out = cfg->clock_out; 1426f01919e8SAntti Palosaari pdata.envelope_mode = cfg->envelope_mode; 1427f01919e8SAntti Palosaari pdata.lnb_hv_pol = cfg->lnb_hv_pol; 1428f01919e8SAntti Palosaari pdata.lnb_en_pol = cfg->lnb_en_pol; 1429f01919e8SAntti Palosaari pdata.attach_in_use = true; 1430395d00d1SAntti Palosaari 1431f01919e8SAntti Palosaari memset(&board_info, 0, sizeof(board_info)); 1432f01919e8SAntti Palosaari strlcpy(board_info.type, "m88ds3103", I2C_NAME_SIZE); 1433f01919e8SAntti Palosaari board_info.addr = cfg->i2c_addr; 1434f01919e8SAntti Palosaari board_info.platform_data = &pdata; 1435f01919e8SAntti Palosaari client = i2c_new_device(i2c, &board_info); 1436f01919e8SAntti Palosaari if (!client || !client->dev.driver) 1437395d00d1SAntti Palosaari return NULL; 1438f01919e8SAntti Palosaari 1439f01919e8SAntti Palosaari *tuner_i2c_adapter = pdata.get_i2c_adapter(client); 1440f01919e8SAntti Palosaari return pdata.get_dvb_frontend(client); 1441395d00d1SAntti Palosaari } 1442395d00d1SAntti Palosaari EXPORT_SYMBOL(m88ds3103_attach); 1443395d00d1SAntti Palosaari 1444395d00d1SAntti Palosaari static struct dvb_frontend_ops m88ds3103_ops = { 1445395d00d1SAntti Palosaari .delsys = { SYS_DVBS, SYS_DVBS2 }, 1446395d00d1SAntti Palosaari .info = { 1447395d00d1SAntti Palosaari .name = "Montage M88DS3103", 1448395d00d1SAntti Palosaari .frequency_min = 950000, 1449395d00d1SAntti Palosaari .frequency_max = 2150000, 1450395d00d1SAntti Palosaari .frequency_tolerance = 5000, 1451395d00d1SAntti Palosaari .symbol_rate_min = 1000000, 1452395d00d1SAntti Palosaari .symbol_rate_max = 45000000, 1453395d00d1SAntti Palosaari .caps = FE_CAN_INVERSION_AUTO | 1454395d00d1SAntti Palosaari FE_CAN_FEC_1_2 | 1455395d00d1SAntti Palosaari FE_CAN_FEC_2_3 | 1456395d00d1SAntti Palosaari FE_CAN_FEC_3_4 | 1457395d00d1SAntti Palosaari FE_CAN_FEC_4_5 | 1458395d00d1SAntti Palosaari FE_CAN_FEC_5_6 | 1459395d00d1SAntti Palosaari FE_CAN_FEC_6_7 | 1460395d00d1SAntti Palosaari FE_CAN_FEC_7_8 | 1461395d00d1SAntti Palosaari FE_CAN_FEC_8_9 | 1462395d00d1SAntti Palosaari FE_CAN_FEC_AUTO | 1463395d00d1SAntti Palosaari FE_CAN_QPSK | 1464395d00d1SAntti Palosaari FE_CAN_RECOVER | 1465395d00d1SAntti Palosaari FE_CAN_2G_MODULATION 1466395d00d1SAntti Palosaari }, 1467395d00d1SAntti Palosaari 1468395d00d1SAntti Palosaari .release = m88ds3103_release, 1469395d00d1SAntti Palosaari 1470395d00d1SAntti Palosaari .get_tune_settings = m88ds3103_get_tune_settings, 1471395d00d1SAntti Palosaari 1472395d00d1SAntti Palosaari .init = m88ds3103_init, 1473395d00d1SAntti Palosaari .sleep = m88ds3103_sleep, 1474395d00d1SAntti Palosaari 1475395d00d1SAntti Palosaari .set_frontend = m88ds3103_set_frontend, 1476395d00d1SAntti Palosaari .get_frontend = m88ds3103_get_frontend, 1477395d00d1SAntti Palosaari 1478395d00d1SAntti Palosaari .read_status = m88ds3103_read_status, 1479395d00d1SAntti Palosaari .read_snr = m88ds3103_read_snr, 14804423a2baSAntti Palosaari .read_ber = m88ds3103_read_ber, 1481395d00d1SAntti Palosaari 1482395d00d1SAntti Palosaari .diseqc_send_master_cmd = m88ds3103_diseqc_send_master_cmd, 1483395d00d1SAntti Palosaari .diseqc_send_burst = m88ds3103_diseqc_send_burst, 1484395d00d1SAntti Palosaari 1485395d00d1SAntti Palosaari .set_tone = m88ds3103_set_tone, 148679d09330Snibble.max .set_voltage = m88ds3103_set_voltage, 1487395d00d1SAntti Palosaari }; 1488395d00d1SAntti Palosaari 1489f01919e8SAntti Palosaari static struct dvb_frontend *m88ds3103_get_dvb_frontend(struct i2c_client *client) 1490f01919e8SAntti Palosaari { 1491f01919e8SAntti Palosaari struct m88ds3103_priv *dev = i2c_get_clientdata(client); 1492f01919e8SAntti Palosaari 1493f01919e8SAntti Palosaari dev_dbg(&client->dev, "\n"); 1494f01919e8SAntti Palosaari 1495f01919e8SAntti Palosaari return &dev->fe; 1496f01919e8SAntti Palosaari } 1497f01919e8SAntti Palosaari 1498f01919e8SAntti Palosaari static struct i2c_adapter *m88ds3103_get_i2c_adapter(struct i2c_client *client) 1499f01919e8SAntti Palosaari { 1500f01919e8SAntti Palosaari struct m88ds3103_priv *dev = i2c_get_clientdata(client); 1501f01919e8SAntti Palosaari 1502f01919e8SAntti Palosaari dev_dbg(&client->dev, "\n"); 1503f01919e8SAntti Palosaari 1504f01919e8SAntti Palosaari return dev->i2c_adapter; 1505f01919e8SAntti Palosaari } 1506f01919e8SAntti Palosaari 1507f01919e8SAntti Palosaari static int m88ds3103_probe(struct i2c_client *client, 1508f01919e8SAntti Palosaari const struct i2c_device_id *id) 1509f01919e8SAntti Palosaari { 1510f01919e8SAntti Palosaari struct m88ds3103_priv *dev; 1511f01919e8SAntti Palosaari struct m88ds3103_platform_data *pdata = client->dev.platform_data; 1512f01919e8SAntti Palosaari int ret; 1513f01919e8SAntti Palosaari u8 chip_id, u8tmp; 1514f01919e8SAntti Palosaari 1515f01919e8SAntti Palosaari dev = kzalloc(sizeof(*dev), GFP_KERNEL); 1516f01919e8SAntti Palosaari if (!dev) { 1517f01919e8SAntti Palosaari ret = -ENOMEM; 1518f01919e8SAntti Palosaari goto err; 1519f01919e8SAntti Palosaari } 1520f01919e8SAntti Palosaari 1521f01919e8SAntti Palosaari dev->client = client; 1522f01919e8SAntti Palosaari dev->i2c = client->adapter; 1523f01919e8SAntti Palosaari dev->config.i2c_addr = client->addr; 1524f01919e8SAntti Palosaari dev->config.clock = pdata->clk; 1525f01919e8SAntti Palosaari dev->config.i2c_wr_max = pdata->i2c_wr_max; 1526f01919e8SAntti Palosaari dev->config.ts_mode = pdata->ts_mode; 1527f01919e8SAntti Palosaari dev->config.ts_clk = pdata->ts_clk; 1528f01919e8SAntti Palosaari dev->config.ts_clk_pol = pdata->ts_clk_pol; 1529f01919e8SAntti Palosaari dev->config.spec_inv = pdata->spec_inv; 1530f01919e8SAntti Palosaari dev->config.agc_inv = pdata->agc_inv; 1531f01919e8SAntti Palosaari dev->config.clock_out = pdata->clk_out; 1532f01919e8SAntti Palosaari dev->config.envelope_mode = pdata->envelope_mode; 1533f01919e8SAntti Palosaari dev->config.agc = pdata->agc; 1534f01919e8SAntti Palosaari dev->config.lnb_hv_pol = pdata->lnb_hv_pol; 1535f01919e8SAntti Palosaari dev->config.lnb_en_pol = pdata->lnb_en_pol; 1536f01919e8SAntti Palosaari dev->cfg = &dev->config; 1537f01919e8SAntti Palosaari mutex_init(&dev->i2c_mutex); 1538f01919e8SAntti Palosaari 1539f01919e8SAntti Palosaari /* 0x00: chip id[6:0], 0x01: chip ver[7:0], 0x02: chip ver[15:8] */ 1540f01919e8SAntti Palosaari ret = m88ds3103_rd_reg(dev, 0x00, &chip_id); 1541f01919e8SAntti Palosaari if (ret) 1542f01919e8SAntti Palosaari goto err_kfree; 1543f01919e8SAntti Palosaari 1544f01919e8SAntti Palosaari chip_id >>= 1; 1545f01919e8SAntti Palosaari dev_dbg(&client->dev, "chip_id=%02x\n", chip_id); 1546f01919e8SAntti Palosaari 1547f01919e8SAntti Palosaari switch (chip_id) { 1548f01919e8SAntti Palosaari case M88RS6000_CHIP_ID: 1549f01919e8SAntti Palosaari case M88DS3103_CHIP_ID: 1550f01919e8SAntti Palosaari break; 1551f01919e8SAntti Palosaari default: 1552f01919e8SAntti Palosaari goto err_kfree; 1553f01919e8SAntti Palosaari } 1554f01919e8SAntti Palosaari dev->chip_id = chip_id; 1555f01919e8SAntti Palosaari 1556f01919e8SAntti Palosaari switch (dev->cfg->clock_out) { 1557f01919e8SAntti Palosaari case M88DS3103_CLOCK_OUT_DISABLED: 1558f01919e8SAntti Palosaari u8tmp = 0x80; 1559f01919e8SAntti Palosaari break; 1560f01919e8SAntti Palosaari case M88DS3103_CLOCK_OUT_ENABLED: 1561f01919e8SAntti Palosaari u8tmp = 0x00; 1562f01919e8SAntti Palosaari break; 1563f01919e8SAntti Palosaari case M88DS3103_CLOCK_OUT_ENABLED_DIV2: 1564f01919e8SAntti Palosaari u8tmp = 0x10; 1565f01919e8SAntti Palosaari break; 1566f01919e8SAntti Palosaari default: 15674347df6aSDan Carpenter ret = -EINVAL; 1568f01919e8SAntti Palosaari goto err_kfree; 1569f01919e8SAntti Palosaari } 1570f01919e8SAntti Palosaari 1571f01919e8SAntti Palosaari /* 0x29 register is defined differently for m88rs6000. */ 1572f01919e8SAntti Palosaari /* set internal tuner address to 0x21 */ 1573f01919e8SAntti Palosaari if (chip_id == M88RS6000_CHIP_ID) 1574f01919e8SAntti Palosaari u8tmp = 0x00; 1575f01919e8SAntti Palosaari 1576f01919e8SAntti Palosaari ret = m88ds3103_wr_reg(dev, 0x29, u8tmp); 1577f01919e8SAntti Palosaari if (ret) 1578f01919e8SAntti Palosaari goto err_kfree; 1579f01919e8SAntti Palosaari 1580f01919e8SAntti Palosaari /* sleep */ 1581f01919e8SAntti Palosaari ret = m88ds3103_wr_reg_mask(dev, 0x08, 0x00, 0x01); 1582f01919e8SAntti Palosaari if (ret) 1583f01919e8SAntti Palosaari goto err_kfree; 1584f01919e8SAntti Palosaari ret = m88ds3103_wr_reg_mask(dev, 0x04, 0x01, 0x01); 1585f01919e8SAntti Palosaari if (ret) 1586f01919e8SAntti Palosaari goto err_kfree; 1587f01919e8SAntti Palosaari ret = m88ds3103_wr_reg_mask(dev, 0x23, 0x10, 0x10); 1588f01919e8SAntti Palosaari if (ret) 1589f01919e8SAntti Palosaari goto err_kfree; 1590f01919e8SAntti Palosaari 1591f01919e8SAntti Palosaari /* create mux i2c adapter for tuner */ 1592f01919e8SAntti Palosaari dev->i2c_adapter = i2c_add_mux_adapter(client->adapter, &client->dev, 1593f01919e8SAntti Palosaari dev, 0, 0, 0, m88ds3103_select, 1594f01919e8SAntti Palosaari m88ds3103_deselect); 15954347df6aSDan Carpenter if (dev->i2c_adapter == NULL) { 15964347df6aSDan Carpenter ret = -ENOMEM; 1597f01919e8SAntti Palosaari goto err_kfree; 15984347df6aSDan Carpenter } 1599f01919e8SAntti Palosaari 1600f01919e8SAntti Palosaari /* create dvb_frontend */ 1601f01919e8SAntti Palosaari memcpy(&dev->fe.ops, &m88ds3103_ops, sizeof(struct dvb_frontend_ops)); 1602f01919e8SAntti Palosaari if (dev->chip_id == M88RS6000_CHIP_ID) 1603f01919e8SAntti Palosaari strncpy(dev->fe.ops.info.name, 1604f01919e8SAntti Palosaari "Montage M88RS6000", sizeof(dev->fe.ops.info.name)); 1605f01919e8SAntti Palosaari if (!pdata->attach_in_use) 1606f01919e8SAntti Palosaari dev->fe.ops.release = NULL; 1607f01919e8SAntti Palosaari dev->fe.demodulator_priv = dev; 1608f01919e8SAntti Palosaari i2c_set_clientdata(client, dev); 1609f01919e8SAntti Palosaari 1610f01919e8SAntti Palosaari /* setup callbacks */ 1611f01919e8SAntti Palosaari pdata->get_dvb_frontend = m88ds3103_get_dvb_frontend; 1612f01919e8SAntti Palosaari pdata->get_i2c_adapter = m88ds3103_get_i2c_adapter; 1613f01919e8SAntti Palosaari return 0; 1614f01919e8SAntti Palosaari err_kfree: 1615f01919e8SAntti Palosaari kfree(dev); 1616f01919e8SAntti Palosaari err: 1617f01919e8SAntti Palosaari dev_dbg(&client->dev, "failed=%d\n", ret); 1618f01919e8SAntti Palosaari return ret; 1619f01919e8SAntti Palosaari } 1620f01919e8SAntti Palosaari 1621f01919e8SAntti Palosaari static int m88ds3103_remove(struct i2c_client *client) 1622f01919e8SAntti Palosaari { 1623f01919e8SAntti Palosaari struct m88ds3103_priv *dev = i2c_get_clientdata(client); 1624f01919e8SAntti Palosaari 1625f01919e8SAntti Palosaari dev_dbg(&client->dev, "\n"); 1626f01919e8SAntti Palosaari 1627f01919e8SAntti Palosaari i2c_del_mux_adapter(dev->i2c_adapter); 1628f01919e8SAntti Palosaari 1629f01919e8SAntti Palosaari kfree(dev); 1630f01919e8SAntti Palosaari return 0; 1631f01919e8SAntti Palosaari } 1632f01919e8SAntti Palosaari 1633f01919e8SAntti Palosaari static const struct i2c_device_id m88ds3103_id_table[] = { 1634f01919e8SAntti Palosaari {"m88ds3103", 0}, 1635f01919e8SAntti Palosaari {} 1636f01919e8SAntti Palosaari }; 1637f01919e8SAntti Palosaari MODULE_DEVICE_TABLE(i2c, m88ds3103_id_table); 1638f01919e8SAntti Palosaari 1639f01919e8SAntti Palosaari static struct i2c_driver m88ds3103_driver = { 1640f01919e8SAntti Palosaari .driver = { 1641f01919e8SAntti Palosaari .owner = THIS_MODULE, 1642f01919e8SAntti Palosaari .name = "m88ds3103", 1643f01919e8SAntti Palosaari .suppress_bind_attrs = true, 1644f01919e8SAntti Palosaari }, 1645f01919e8SAntti Palosaari .probe = m88ds3103_probe, 1646f01919e8SAntti Palosaari .remove = m88ds3103_remove, 1647f01919e8SAntti Palosaari .id_table = m88ds3103_id_table, 1648f01919e8SAntti Palosaari }; 1649f01919e8SAntti Palosaari 1650f01919e8SAntti Palosaari module_i2c_driver(m88ds3103_driver); 1651f01919e8SAntti Palosaari 1652395d00d1SAntti Palosaari MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>"); 1653395d00d1SAntti Palosaari MODULE_DESCRIPTION("Montage M88DS3103 DVB-S/S2 demodulator driver"); 1654395d00d1SAntti Palosaari MODULE_LICENSE("GPL"); 1655395d00d1SAntti Palosaari MODULE_FIRMWARE(M88DS3103_FIRMWARE); 1656f4df95bcSnibble.max MODULE_FIRMWARE(M88RS6000_FIRMWARE); 1657