1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 2395d00d1SAntti Palosaari /* 37978b8a1SAntti Palosaari * Montage Technology M88DS3103/M88RS6000 demodulator driver 4395d00d1SAntti Palosaari * 5395d00d1SAntti Palosaari * Copyright (C) 2013 Antti Palosaari <crope@iki.fi> 6395d00d1SAntti Palosaari */ 7395d00d1SAntti Palosaari 8395d00d1SAntti Palosaari #include "m88ds3103_priv.h" 9395d00d1SAntti Palosaari 10bd336e63SMax Kellermann static const struct dvb_frontend_ops m88ds3103_ops; 11395d00d1SAntti Palosaari 1256ea37daSAntti Palosaari /* write single register with mask */ 1356ea37daSAntti Palosaari static int m88ds3103_update_bits(struct m88ds3103_dev *dev, 1456ea37daSAntti Palosaari u8 reg, u8 mask, u8 val) 1556ea37daSAntti Palosaari { 1656ea37daSAntti Palosaari int ret; 1756ea37daSAntti Palosaari u8 tmp; 1856ea37daSAntti Palosaari 1956ea37daSAntti Palosaari /* no need for read if whole reg is written */ 2056ea37daSAntti Palosaari if (mask != 0xff) { 2156ea37daSAntti Palosaari ret = regmap_bulk_read(dev->regmap, reg, &tmp, 1); 2256ea37daSAntti Palosaari if (ret) 2356ea37daSAntti Palosaari return ret; 2456ea37daSAntti Palosaari 2556ea37daSAntti Palosaari val &= mask; 2656ea37daSAntti Palosaari tmp &= ~mask; 2756ea37daSAntti Palosaari val |= tmp; 2856ea37daSAntti Palosaari } 2956ea37daSAntti Palosaari 3056ea37daSAntti Palosaari return regmap_bulk_write(dev->regmap, reg, &val, 1); 3156ea37daSAntti Palosaari } 3256ea37daSAntti Palosaari 3306487deeSAntti Palosaari /* write reg val table using reg addr auto increment */ 347978b8a1SAntti Palosaari static int m88ds3103_wr_reg_val_tab(struct m88ds3103_dev *dev, 3506487deeSAntti Palosaari const struct m88ds3103_reg_val *tab, int tab_len) 3606487deeSAntti Palosaari { 377978b8a1SAntti Palosaari struct i2c_client *client = dev->client; 3806487deeSAntti Palosaari int ret, i, j; 3906487deeSAntti Palosaari u8 buf[83]; 4041b9aa00SAntti Palosaari 417978b8a1SAntti Palosaari dev_dbg(&client->dev, "tab_len=%d\n", tab_len); 4206487deeSAntti Palosaari 43f4df95bcSnibble.max if (tab_len > 86) { 4406487deeSAntti Palosaari ret = -EINVAL; 4506487deeSAntti Palosaari goto err; 4606487deeSAntti Palosaari } 4706487deeSAntti Palosaari 4806487deeSAntti Palosaari for (i = 0, j = 0; i < tab_len; i++, j++) { 4906487deeSAntti Palosaari buf[j] = tab[i].val; 5006487deeSAntti Palosaari 5106487deeSAntti Palosaari if (i == tab_len - 1 || tab[i].reg != tab[i + 1].reg - 1 || 527978b8a1SAntti Palosaari !((j + 1) % (dev->cfg->i2c_wr_max - 1))) { 53478932b1SAntti Palosaari ret = regmap_bulk_write(dev->regmap, tab[i].reg - j, buf, j + 1); 5406487deeSAntti Palosaari if (ret) 5506487deeSAntti Palosaari goto err; 5606487deeSAntti Palosaari 5706487deeSAntti Palosaari j = -1; 5806487deeSAntti Palosaari } 5906487deeSAntti Palosaari } 6006487deeSAntti Palosaari 6106487deeSAntti Palosaari return 0; 6206487deeSAntti Palosaari err: 637978b8a1SAntti Palosaari dev_dbg(&client->dev, "failed=%d\n", ret); 6406487deeSAntti Palosaari return ret; 6506487deeSAntti Palosaari } 6606487deeSAntti Palosaari 670f91c9d6SDavid Howells /* 68*e6089fecSBrad Love * m88ds3103b demod has an internal device related to clocking. First the i2c 69*e6089fecSBrad Love * gate must be opened, for one transaction, then writes will be allowed. 70*e6089fecSBrad Love */ 71*e6089fecSBrad Love static int m88ds3103b_dt_write(struct m88ds3103_dev *dev, int reg, int data) 72*e6089fecSBrad Love { 73*e6089fecSBrad Love struct i2c_client *client = dev->client; 74*e6089fecSBrad Love u8 buf[] = {reg, data}; 75*e6089fecSBrad Love u8 val; 76*e6089fecSBrad Love int ret; 77*e6089fecSBrad Love struct i2c_msg msg = { 78*e6089fecSBrad Love .addr = dev->dt_addr, .flags = 0, .buf = buf, .len = 2 79*e6089fecSBrad Love }; 80*e6089fecSBrad Love 81*e6089fecSBrad Love m88ds3103_update_bits(dev, 0x11, 0x01, 0x00); 82*e6089fecSBrad Love 83*e6089fecSBrad Love val = 0x11; 84*e6089fecSBrad Love ret = regmap_write(dev->regmap, 0x03, val); 85*e6089fecSBrad Love if (ret) 86*e6089fecSBrad Love dev_dbg(&client->dev, "fail=%d\n", ret); 87*e6089fecSBrad Love 88*e6089fecSBrad Love ret = i2c_transfer(dev->dt_client->adapter, &msg, 1); 89*e6089fecSBrad Love if (ret != 1) { 90*e6089fecSBrad Love dev_err(&client->dev, "0x%02x (ret=%i, reg=0x%02x, value=0x%02x)\n", 91*e6089fecSBrad Love dev->dt_addr, ret, reg, data); 92*e6089fecSBrad Love 93*e6089fecSBrad Love m88ds3103_update_bits(dev, 0x11, 0x01, 0x01); 94*e6089fecSBrad Love return -EREMOTEIO; 95*e6089fecSBrad Love } 96*e6089fecSBrad Love m88ds3103_update_bits(dev, 0x11, 0x01, 0x01); 97*e6089fecSBrad Love 98*e6089fecSBrad Love dev_dbg(&client->dev, "0x%02x reg 0x%02x, value 0x%02x\n", 99*e6089fecSBrad Love dev->dt_addr, reg, data); 100*e6089fecSBrad Love 101*e6089fecSBrad Love return 0; 102*e6089fecSBrad Love } 103*e6089fecSBrad Love 104*e6089fecSBrad Love /* 105*e6089fecSBrad Love * m88ds3103b demod has an internal device related to clocking. First the i2c 106*e6089fecSBrad Love * gate must be opened, for two transactions, then reads will be allowed. 107*e6089fecSBrad Love */ 108*e6089fecSBrad Love static int m88ds3103b_dt_read(struct m88ds3103_dev *dev, u8 reg) 109*e6089fecSBrad Love { 110*e6089fecSBrad Love struct i2c_client *client = dev->client; 111*e6089fecSBrad Love int ret; 112*e6089fecSBrad Love u8 val; 113*e6089fecSBrad Love u8 b0[] = { reg }; 114*e6089fecSBrad Love u8 b1[] = { 0 }; 115*e6089fecSBrad Love struct i2c_msg msg[] = { 116*e6089fecSBrad Love { 117*e6089fecSBrad Love .addr = dev->dt_addr, 118*e6089fecSBrad Love .flags = 0, 119*e6089fecSBrad Love .buf = b0, 120*e6089fecSBrad Love .len = 1 121*e6089fecSBrad Love }, 122*e6089fecSBrad Love { 123*e6089fecSBrad Love .addr = dev->dt_addr, 124*e6089fecSBrad Love .flags = I2C_M_RD, 125*e6089fecSBrad Love .buf = b1, 126*e6089fecSBrad Love .len = 1 127*e6089fecSBrad Love } 128*e6089fecSBrad Love }; 129*e6089fecSBrad Love 130*e6089fecSBrad Love m88ds3103_update_bits(dev, 0x11, 0x01, 0x00); 131*e6089fecSBrad Love 132*e6089fecSBrad Love val = 0x12; 133*e6089fecSBrad Love ret = regmap_write(dev->regmap, 0x03, val); 134*e6089fecSBrad Love if (ret) 135*e6089fecSBrad Love dev_dbg(&client->dev, "fail=%d\n", ret); 136*e6089fecSBrad Love 137*e6089fecSBrad Love ret = i2c_transfer(dev->dt_client->adapter, msg, 2); 138*e6089fecSBrad Love if (ret != 2) { 139*e6089fecSBrad Love dev_err(&client->dev, "0x%02x (ret=%d, reg=0x%02x)\n", 140*e6089fecSBrad Love dev->dt_addr, ret, reg); 141*e6089fecSBrad Love 142*e6089fecSBrad Love m88ds3103_update_bits(dev, 0x11, 0x01, 0x01); 143*e6089fecSBrad Love return -EREMOTEIO; 144*e6089fecSBrad Love } 145*e6089fecSBrad Love m88ds3103_update_bits(dev, 0x11, 0x01, 0x01); 146*e6089fecSBrad Love 147*e6089fecSBrad Love dev_dbg(&client->dev, "0x%02x reg 0x%02x, value 0x%02x\n", 148*e6089fecSBrad Love dev->dt_addr, reg, b1[0]); 149*e6089fecSBrad Love 150*e6089fecSBrad Love return b1[0]; 151*e6089fecSBrad Love } 152*e6089fecSBrad Love 153*e6089fecSBrad Love /* 1540f91c9d6SDavid Howells * Get the demodulator AGC PWM voltage setting supplied to the tuner. 1550f91c9d6SDavid Howells */ 1560f91c9d6SDavid Howells int m88ds3103_get_agc_pwm(struct dvb_frontend *fe, u8 *_agc_pwm) 1570f91c9d6SDavid Howells { 1580f91c9d6SDavid Howells struct m88ds3103_dev *dev = fe->demodulator_priv; 1590f91c9d6SDavid Howells unsigned tmp; 1600f91c9d6SDavid Howells int ret; 1610f91c9d6SDavid Howells 1620f91c9d6SDavid Howells ret = regmap_read(dev->regmap, 0x3f, &tmp); 1630f91c9d6SDavid Howells if (ret == 0) 1640f91c9d6SDavid Howells *_agc_pwm = tmp; 1650f91c9d6SDavid Howells return ret; 1660f91c9d6SDavid Howells } 1670f91c9d6SDavid Howells EXPORT_SYMBOL(m88ds3103_get_agc_pwm); 1680f91c9d6SDavid Howells 1690df289a2SMauro Carvalho Chehab static int m88ds3103_read_status(struct dvb_frontend *fe, 1700df289a2SMauro Carvalho Chehab enum fe_status *status) 171395d00d1SAntti Palosaari { 1727978b8a1SAntti Palosaari struct m88ds3103_dev *dev = fe->demodulator_priv; 1737978b8a1SAntti Palosaari struct i2c_client *client = dev->client; 174395d00d1SAntti Palosaari struct dtv_frontend_properties *c = &fe->dtv_property_cache; 175c1daf651SAntti Palosaari int ret, i, itmp; 176478932b1SAntti Palosaari unsigned int utmp; 177c1daf651SAntti Palosaari u8 buf[3]; 178395d00d1SAntti Palosaari 179395d00d1SAntti Palosaari *status = 0; 180395d00d1SAntti Palosaari 1817978b8a1SAntti Palosaari if (!dev->warm) { 182395d00d1SAntti Palosaari ret = -EAGAIN; 183395d00d1SAntti Palosaari goto err; 184395d00d1SAntti Palosaari } 185395d00d1SAntti Palosaari 186395d00d1SAntti Palosaari switch (c->delivery_system) { 187395d00d1SAntti Palosaari case SYS_DVBS: 188478932b1SAntti Palosaari ret = regmap_read(dev->regmap, 0xd1, &utmp); 189395d00d1SAntti Palosaari if (ret) 190395d00d1SAntti Palosaari goto err; 191395d00d1SAntti Palosaari 192478932b1SAntti Palosaari if ((utmp & 0x07) == 0x07) 193395d00d1SAntti Palosaari *status = FE_HAS_SIGNAL | FE_HAS_CARRIER | 194395d00d1SAntti Palosaari FE_HAS_VITERBI | FE_HAS_SYNC | 195395d00d1SAntti Palosaari FE_HAS_LOCK; 196395d00d1SAntti Palosaari break; 197395d00d1SAntti Palosaari case SYS_DVBS2: 198478932b1SAntti Palosaari ret = regmap_read(dev->regmap, 0x0d, &utmp); 199395d00d1SAntti Palosaari if (ret) 200395d00d1SAntti Palosaari goto err; 201395d00d1SAntti Palosaari 202478932b1SAntti Palosaari if ((utmp & 0x8f) == 0x8f) 203395d00d1SAntti Palosaari *status = FE_HAS_SIGNAL | FE_HAS_CARRIER | 204395d00d1SAntti Palosaari FE_HAS_VITERBI | FE_HAS_SYNC | 205395d00d1SAntti Palosaari FE_HAS_LOCK; 206395d00d1SAntti Palosaari break; 207395d00d1SAntti Palosaari default: 2087978b8a1SAntti Palosaari dev_dbg(&client->dev, "invalid delivery_system\n"); 209395d00d1SAntti Palosaari ret = -EINVAL; 210395d00d1SAntti Palosaari goto err; 211395d00d1SAntti Palosaari } 212395d00d1SAntti Palosaari 2137978b8a1SAntti Palosaari dev->fe_status = *status; 214478932b1SAntti Palosaari dev_dbg(&client->dev, "lock=%02x status=%02x\n", utmp, *status); 215395d00d1SAntti Palosaari 216c1daf651SAntti Palosaari /* CNR */ 2177978b8a1SAntti Palosaari if (dev->fe_status & FE_HAS_VITERBI) { 218c1daf651SAntti Palosaari unsigned int cnr, noise, signal, noise_tot, signal_tot; 219c1daf651SAntti Palosaari 220c1daf651SAntti Palosaari cnr = 0; 221c1daf651SAntti Palosaari /* more iterations for more accurate estimation */ 222c1daf651SAntti Palosaari #define M88DS3103_SNR_ITERATIONS 3 223c1daf651SAntti Palosaari 224c1daf651SAntti Palosaari switch (c->delivery_system) { 225c1daf651SAntti Palosaari case SYS_DVBS: 226c1daf651SAntti Palosaari itmp = 0; 227c1daf651SAntti Palosaari 228c1daf651SAntti Palosaari for (i = 0; i < M88DS3103_SNR_ITERATIONS; i++) { 229478932b1SAntti Palosaari ret = regmap_read(dev->regmap, 0xff, &utmp); 230c1daf651SAntti Palosaari if (ret) 231c1daf651SAntti Palosaari goto err; 232c1daf651SAntti Palosaari 233478932b1SAntti Palosaari itmp += utmp; 234c1daf651SAntti Palosaari } 235c1daf651SAntti Palosaari 236c1daf651SAntti Palosaari /* use of single register limits max value to 15 dB */ 237c1daf651SAntti Palosaari /* SNR(X) dB = 10 * ln(X) / ln(10) dB */ 238c1daf651SAntti Palosaari itmp = DIV_ROUND_CLOSEST(itmp, 8 * M88DS3103_SNR_ITERATIONS); 239c1daf651SAntti Palosaari if (itmp) 240c1daf651SAntti Palosaari cnr = div_u64((u64) 10000 * intlog2(itmp), intlog2(10)); 241c1daf651SAntti Palosaari break; 242c1daf651SAntti Palosaari case SYS_DVBS2: 243c1daf651SAntti Palosaari noise_tot = 0; 244c1daf651SAntti Palosaari signal_tot = 0; 245c1daf651SAntti Palosaari 246c1daf651SAntti Palosaari for (i = 0; i < M88DS3103_SNR_ITERATIONS; i++) { 247478932b1SAntti Palosaari ret = regmap_bulk_read(dev->regmap, 0x8c, buf, 3); 248c1daf651SAntti Palosaari if (ret) 249c1daf651SAntti Palosaari goto err; 250c1daf651SAntti Palosaari 251c1daf651SAntti Palosaari noise = buf[1] << 6; /* [13:6] */ 252c1daf651SAntti Palosaari noise |= buf[0] & 0x3f; /* [5:0] */ 253c1daf651SAntti Palosaari noise >>= 2; 254c1daf651SAntti Palosaari signal = buf[2] * buf[2]; 255c1daf651SAntti Palosaari signal >>= 1; 256c1daf651SAntti Palosaari 257c1daf651SAntti Palosaari noise_tot += noise; 258c1daf651SAntti Palosaari signal_tot += signal; 259c1daf651SAntti Palosaari } 260c1daf651SAntti Palosaari 261c1daf651SAntti Palosaari noise = noise_tot / M88DS3103_SNR_ITERATIONS; 262c1daf651SAntti Palosaari signal = signal_tot / M88DS3103_SNR_ITERATIONS; 263c1daf651SAntti Palosaari 264c1daf651SAntti Palosaari /* SNR(X) dB = 10 * log10(X) dB */ 265c1daf651SAntti Palosaari if (signal > noise) { 266c1daf651SAntti Palosaari itmp = signal / noise; 267c1daf651SAntti Palosaari cnr = div_u64((u64) 10000 * intlog10(itmp), (1 << 24)); 268c1daf651SAntti Palosaari } 269c1daf651SAntti Palosaari break; 270c1daf651SAntti Palosaari default: 2717978b8a1SAntti Palosaari dev_dbg(&client->dev, "invalid delivery_system\n"); 272c1daf651SAntti Palosaari ret = -EINVAL; 273c1daf651SAntti Palosaari goto err; 274c1daf651SAntti Palosaari } 275c1daf651SAntti Palosaari 276c1daf651SAntti Palosaari if (cnr) { 277c1daf651SAntti Palosaari c->cnr.stat[0].scale = FE_SCALE_DECIBEL; 278c1daf651SAntti Palosaari c->cnr.stat[0].svalue = cnr; 279c1daf651SAntti Palosaari } else { 280c1daf651SAntti Palosaari c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE; 281c1daf651SAntti Palosaari } 282c1daf651SAntti Palosaari } else { 283c1daf651SAntti Palosaari c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE; 284c1daf651SAntti Palosaari } 285c1daf651SAntti Palosaari 286ce80d713SAntti Palosaari /* BER */ 2877978b8a1SAntti Palosaari if (dev->fe_status & FE_HAS_LOCK) { 288ce80d713SAntti Palosaari unsigned int utmp, post_bit_error, post_bit_count; 289ce80d713SAntti Palosaari 290ce80d713SAntti Palosaari switch (c->delivery_system) { 291ce80d713SAntti Palosaari case SYS_DVBS: 292478932b1SAntti Palosaari ret = regmap_write(dev->regmap, 0xf9, 0x04); 293ce80d713SAntti Palosaari if (ret) 294ce80d713SAntti Palosaari goto err; 295ce80d713SAntti Palosaari 296478932b1SAntti Palosaari ret = regmap_read(dev->regmap, 0xf8, &utmp); 297ce80d713SAntti Palosaari if (ret) 298ce80d713SAntti Palosaari goto err; 299ce80d713SAntti Palosaari 300ce80d713SAntti Palosaari /* measurement ready? */ 301478932b1SAntti Palosaari if (!(utmp & 0x10)) { 302478932b1SAntti Palosaari ret = regmap_bulk_read(dev->regmap, 0xf6, buf, 2); 303ce80d713SAntti Palosaari if (ret) 304ce80d713SAntti Palosaari goto err; 305ce80d713SAntti Palosaari 306ce80d713SAntti Palosaari post_bit_error = buf[1] << 8 | buf[0] << 0; 307ce80d713SAntti Palosaari post_bit_count = 0x800000; 3087978b8a1SAntti Palosaari dev->post_bit_error += post_bit_error; 3097978b8a1SAntti Palosaari dev->post_bit_count += post_bit_count; 3107978b8a1SAntti Palosaari dev->dvbv3_ber = post_bit_error; 311ce80d713SAntti Palosaari 312ce80d713SAntti Palosaari /* restart measurement */ 313478932b1SAntti Palosaari utmp |= 0x10; 314478932b1SAntti Palosaari ret = regmap_write(dev->regmap, 0xf8, utmp); 315ce80d713SAntti Palosaari if (ret) 316ce80d713SAntti Palosaari goto err; 317ce80d713SAntti Palosaari } 318ce80d713SAntti Palosaari break; 319ce80d713SAntti Palosaari case SYS_DVBS2: 320478932b1SAntti Palosaari ret = regmap_bulk_read(dev->regmap, 0xd5, buf, 3); 321ce80d713SAntti Palosaari if (ret) 322ce80d713SAntti Palosaari goto err; 323ce80d713SAntti Palosaari 324ce80d713SAntti Palosaari utmp = buf[2] << 16 | buf[1] << 8 | buf[0] << 0; 325ce80d713SAntti Palosaari 326ce80d713SAntti Palosaari /* enough data? */ 327ce80d713SAntti Palosaari if (utmp > 4000) { 328478932b1SAntti Palosaari ret = regmap_bulk_read(dev->regmap, 0xf7, buf, 2); 329ce80d713SAntti Palosaari if (ret) 330ce80d713SAntti Palosaari goto err; 331ce80d713SAntti Palosaari 332ce80d713SAntti Palosaari post_bit_error = buf[1] << 8 | buf[0] << 0; 333ce80d713SAntti Palosaari post_bit_count = 32 * utmp; /* TODO: FEC */ 3347978b8a1SAntti Palosaari dev->post_bit_error += post_bit_error; 3357978b8a1SAntti Palosaari dev->post_bit_count += post_bit_count; 3367978b8a1SAntti Palosaari dev->dvbv3_ber = post_bit_error; 337ce80d713SAntti Palosaari 338ce80d713SAntti Palosaari /* restart measurement */ 339478932b1SAntti Palosaari ret = regmap_write(dev->regmap, 0xd1, 0x01); 340ce80d713SAntti Palosaari if (ret) 341ce80d713SAntti Palosaari goto err; 342ce80d713SAntti Palosaari 343478932b1SAntti Palosaari ret = regmap_write(dev->regmap, 0xf9, 0x01); 344ce80d713SAntti Palosaari if (ret) 345ce80d713SAntti Palosaari goto err; 346ce80d713SAntti Palosaari 347478932b1SAntti Palosaari ret = regmap_write(dev->regmap, 0xf9, 0x00); 348ce80d713SAntti Palosaari if (ret) 349ce80d713SAntti Palosaari goto err; 350ce80d713SAntti Palosaari 351478932b1SAntti Palosaari ret = regmap_write(dev->regmap, 0xd1, 0x00); 352ce80d713SAntti Palosaari if (ret) 353ce80d713SAntti Palosaari goto err; 354ce80d713SAntti Palosaari } 355ce80d713SAntti Palosaari break; 356ce80d713SAntti Palosaari default: 3577978b8a1SAntti Palosaari dev_dbg(&client->dev, "invalid delivery_system\n"); 358ce80d713SAntti Palosaari ret = -EINVAL; 359ce80d713SAntti Palosaari goto err; 360ce80d713SAntti Palosaari } 361ce80d713SAntti Palosaari 362ce80d713SAntti Palosaari c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER; 3637978b8a1SAntti Palosaari c->post_bit_error.stat[0].uvalue = dev->post_bit_error; 364ce80d713SAntti Palosaari c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER; 3657978b8a1SAntti Palosaari c->post_bit_count.stat[0].uvalue = dev->post_bit_count; 366ce80d713SAntti Palosaari } else { 367ce80d713SAntti Palosaari c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE; 368ce80d713SAntti Palosaari c->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE; 369ce80d713SAntti Palosaari } 370ce80d713SAntti Palosaari 371395d00d1SAntti Palosaari return 0; 372395d00d1SAntti Palosaari err: 3737978b8a1SAntti Palosaari dev_dbg(&client->dev, "failed=%d\n", ret); 374395d00d1SAntti Palosaari return ret; 375395d00d1SAntti Palosaari } 376395d00d1SAntti Palosaari 377*e6089fecSBrad Love static int m88ds3103b_select_mclk(struct m88ds3103_dev *dev) 378*e6089fecSBrad Love { 379*e6089fecSBrad Love struct i2c_client *client = dev->client; 380*e6089fecSBrad Love struct dtv_frontend_properties *c = &dev->fe.dtv_property_cache; 381*e6089fecSBrad Love u32 adc_Freq_MHz[3] = {96, 93, 99}; 382*e6089fecSBrad Love u8 reg16_list[3] = {96, 92, 100}, reg16, reg15; 383*e6089fecSBrad Love u32 offset_MHz[3]; 384*e6089fecSBrad Love u32 max_offset = 0; 385*e6089fecSBrad Love u32 old_setting = dev->mclk; 386*e6089fecSBrad Love u32 tuner_freq_MHz = c->frequency / 1000; 387*e6089fecSBrad Love u8 i; 388*e6089fecSBrad Love char big_symbol = 0; 389*e6089fecSBrad Love 390*e6089fecSBrad Love big_symbol = (c->symbol_rate > 45010000) ? 1 : 0; 391*e6089fecSBrad Love 392*e6089fecSBrad Love if (big_symbol) { 393*e6089fecSBrad Love reg16 = 115; 394*e6089fecSBrad Love } else { 395*e6089fecSBrad Love reg16 = 96; 396*e6089fecSBrad Love 397*e6089fecSBrad Love /* TODO: IS THIS NECESSARY ? */ 398*e6089fecSBrad Love for (i = 0; i < 3; i++) { 399*e6089fecSBrad Love offset_MHz[i] = tuner_freq_MHz % adc_Freq_MHz[i]; 400*e6089fecSBrad Love 401*e6089fecSBrad Love if (offset_MHz[i] > (adc_Freq_MHz[i] / 2)) 402*e6089fecSBrad Love offset_MHz[i] = adc_Freq_MHz[i] - offset_MHz[i]; 403*e6089fecSBrad Love 404*e6089fecSBrad Love if (offset_MHz[i] > max_offset) { 405*e6089fecSBrad Love max_offset = offset_MHz[i]; 406*e6089fecSBrad Love reg16 = reg16_list[i]; 407*e6089fecSBrad Love dev->mclk = adc_Freq_MHz[i] * 1000 * 1000; 408*e6089fecSBrad Love 409*e6089fecSBrad Love if (big_symbol) 410*e6089fecSBrad Love dev->mclk /= 2; 411*e6089fecSBrad Love 412*e6089fecSBrad Love dev_dbg(&client->dev, "modifying mclk %u -> %u\n", 413*e6089fecSBrad Love old_setting, dev->mclk); 414*e6089fecSBrad Love } 415*e6089fecSBrad Love } 416*e6089fecSBrad Love } 417*e6089fecSBrad Love 418*e6089fecSBrad Love if (dev->mclk == 93000000) 419*e6089fecSBrad Love regmap_write(dev->regmap, 0xA0, 0x42); 420*e6089fecSBrad Love else if (dev->mclk == 96000000) 421*e6089fecSBrad Love regmap_write(dev->regmap, 0xA0, 0x44); 422*e6089fecSBrad Love else if (dev->mclk == 99000000) 423*e6089fecSBrad Love regmap_write(dev->regmap, 0xA0, 0x46); 424*e6089fecSBrad Love else if (dev->mclk == 110250000) 425*e6089fecSBrad Love regmap_write(dev->regmap, 0xA0, 0x4E); 426*e6089fecSBrad Love else 427*e6089fecSBrad Love regmap_write(dev->regmap, 0xA0, 0x44); 428*e6089fecSBrad Love 429*e6089fecSBrad Love reg15 = m88ds3103b_dt_read(dev, 0x15); 430*e6089fecSBrad Love 431*e6089fecSBrad Love m88ds3103b_dt_write(dev, 0x05, 0x40); 432*e6089fecSBrad Love m88ds3103b_dt_write(dev, 0x11, 0x08); 433*e6089fecSBrad Love 434*e6089fecSBrad Love if (big_symbol) 435*e6089fecSBrad Love reg15 |= 0x02; 436*e6089fecSBrad Love else 437*e6089fecSBrad Love reg15 &= ~0x02; 438*e6089fecSBrad Love 439*e6089fecSBrad Love m88ds3103b_dt_write(dev, 0x15, reg15); 440*e6089fecSBrad Love m88ds3103b_dt_write(dev, 0x16, reg16); 441*e6089fecSBrad Love 442*e6089fecSBrad Love usleep_range(5000, 5500); 443*e6089fecSBrad Love 444*e6089fecSBrad Love m88ds3103b_dt_write(dev, 0x05, 0x00); 445*e6089fecSBrad Love m88ds3103b_dt_write(dev, 0x11, (u8)(big_symbol ? 0x0E : 0x0A)); 446*e6089fecSBrad Love 447*e6089fecSBrad Love usleep_range(5000, 5500); 448*e6089fecSBrad Love 449*e6089fecSBrad Love return 0; 450*e6089fecSBrad Love } 451*e6089fecSBrad Love 452*e6089fecSBrad Love static int m88ds3103b_set_mclk(struct m88ds3103_dev *dev, u32 mclk_khz) 453*e6089fecSBrad Love { 454*e6089fecSBrad Love u8 reg11 = 0x0A, reg15, reg16, reg1D, reg1E, reg1F, tmp; 455*e6089fecSBrad Love u8 sm, f0 = 0, f1 = 0, f2 = 0, f3 = 0; 456*e6089fecSBrad Love u16 pll_div_fb, N; 457*e6089fecSBrad Love u32 div; 458*e6089fecSBrad Love 459*e6089fecSBrad Love reg15 = m88ds3103b_dt_read(dev, 0x15); 460*e6089fecSBrad Love reg16 = m88ds3103b_dt_read(dev, 0x16); 461*e6089fecSBrad Love reg1D = m88ds3103b_dt_read(dev, 0x1D); 462*e6089fecSBrad Love 463*e6089fecSBrad Love if (dev->cfg->ts_mode != M88DS3103_TS_SERIAL) { 464*e6089fecSBrad Love if (reg16 == 92) 465*e6089fecSBrad Love tmp = 93; 466*e6089fecSBrad Love else if (reg16 == 100) 467*e6089fecSBrad Love tmp = 99; 468*e6089fecSBrad Love else 469*e6089fecSBrad Love tmp = 96; 470*e6089fecSBrad Love 471*e6089fecSBrad Love mclk_khz *= tmp; 472*e6089fecSBrad Love mclk_khz /= 96; 473*e6089fecSBrad Love } 474*e6089fecSBrad Love 475*e6089fecSBrad Love pll_div_fb = (reg15 & 0x01) << 8; 476*e6089fecSBrad Love pll_div_fb += reg16; 477*e6089fecSBrad Love pll_div_fb += 32; 478*e6089fecSBrad Love 479*e6089fecSBrad Love div = 9000 * pll_div_fb * 4; 480*e6089fecSBrad Love div /= mclk_khz; 481*e6089fecSBrad Love 482*e6089fecSBrad Love if (dev->cfg->ts_mode == M88DS3103_TS_SERIAL) { 483*e6089fecSBrad Love reg11 |= 0x02; 484*e6089fecSBrad Love 485*e6089fecSBrad Love if (div <= 32) { 486*e6089fecSBrad Love N = 2; 487*e6089fecSBrad Love 488*e6089fecSBrad Love f0 = 0; 489*e6089fecSBrad Love f1 = div / N; 490*e6089fecSBrad Love f2 = div - f1; 491*e6089fecSBrad Love f3 = 0; 492*e6089fecSBrad Love } else if (div <= 34) { 493*e6089fecSBrad Love N = 3; 494*e6089fecSBrad Love 495*e6089fecSBrad Love f0 = div / N; 496*e6089fecSBrad Love f1 = (div - f0) / (N - 1); 497*e6089fecSBrad Love f2 = div - f0 - f1; 498*e6089fecSBrad Love f3 = 0; 499*e6089fecSBrad Love } else if (div <= 64) { 500*e6089fecSBrad Love N = 4; 501*e6089fecSBrad Love 502*e6089fecSBrad Love f0 = div / N; 503*e6089fecSBrad Love f1 = (div - f0) / (N - 1); 504*e6089fecSBrad Love f2 = (div - f0 - f1) / (N - 2); 505*e6089fecSBrad Love f3 = div - f0 - f1 - f2; 506*e6089fecSBrad Love } else { 507*e6089fecSBrad Love N = 4; 508*e6089fecSBrad Love 509*e6089fecSBrad Love f0 = 16; 510*e6089fecSBrad Love f1 = 16; 511*e6089fecSBrad Love f2 = 16; 512*e6089fecSBrad Love f3 = 16; 513*e6089fecSBrad Love } 514*e6089fecSBrad Love 515*e6089fecSBrad Love if (f0 == 16) 516*e6089fecSBrad Love f0 = 0; 517*e6089fecSBrad Love else if ((f0 < 8) && (f0 != 0)) 518*e6089fecSBrad Love f0 = 8; 519*e6089fecSBrad Love 520*e6089fecSBrad Love if (f1 == 16) 521*e6089fecSBrad Love f1 = 0; 522*e6089fecSBrad Love else if ((f1 < 8) && (f1 != 0)) 523*e6089fecSBrad Love f1 = 8; 524*e6089fecSBrad Love 525*e6089fecSBrad Love if (f2 == 16) 526*e6089fecSBrad Love f2 = 0; 527*e6089fecSBrad Love else if ((f2 < 8) && (f2 != 0)) 528*e6089fecSBrad Love f2 = 8; 529*e6089fecSBrad Love 530*e6089fecSBrad Love if (f3 == 16) 531*e6089fecSBrad Love f3 = 0; 532*e6089fecSBrad Love else if ((f3 < 8) && (f3 != 0)) 533*e6089fecSBrad Love f3 = 8; 534*e6089fecSBrad Love } else { 535*e6089fecSBrad Love reg11 &= ~0x02; 536*e6089fecSBrad Love 537*e6089fecSBrad Love if (div <= 32) { 538*e6089fecSBrad Love N = 2; 539*e6089fecSBrad Love 540*e6089fecSBrad Love f0 = 0; 541*e6089fecSBrad Love f1 = div / N; 542*e6089fecSBrad Love f2 = div - f1; 543*e6089fecSBrad Love f3 = 0; 544*e6089fecSBrad Love } else if (div <= 48) { 545*e6089fecSBrad Love N = 3; 546*e6089fecSBrad Love 547*e6089fecSBrad Love f0 = div / N; 548*e6089fecSBrad Love f1 = (div - f0) / (N - 1); 549*e6089fecSBrad Love f2 = div - f0 - f1; 550*e6089fecSBrad Love f3 = 0; 551*e6089fecSBrad Love } else if (div <= 64) { 552*e6089fecSBrad Love N = 4; 553*e6089fecSBrad Love 554*e6089fecSBrad Love f0 = div / N; 555*e6089fecSBrad Love f1 = (div - f0) / (N - 1); 556*e6089fecSBrad Love f2 = (div - f0 - f1) / (N - 2); 557*e6089fecSBrad Love f3 = div - f0 - f1 - f2; 558*e6089fecSBrad Love } else { 559*e6089fecSBrad Love N = 4; 560*e6089fecSBrad Love 561*e6089fecSBrad Love f0 = 16; 562*e6089fecSBrad Love f1 = 16; 563*e6089fecSBrad Love f2 = 16; 564*e6089fecSBrad Love f3 = 16; 565*e6089fecSBrad Love } 566*e6089fecSBrad Love 567*e6089fecSBrad Love if (f0 == 16) 568*e6089fecSBrad Love f0 = 0; 569*e6089fecSBrad Love else if ((f0 < 9) && (f0 != 0)) 570*e6089fecSBrad Love f0 = 9; 571*e6089fecSBrad Love 572*e6089fecSBrad Love if (f1 == 16) 573*e6089fecSBrad Love f1 = 0; 574*e6089fecSBrad Love else if ((f1 < 9) && (f1 != 0)) 575*e6089fecSBrad Love f1 = 9; 576*e6089fecSBrad Love 577*e6089fecSBrad Love if (f2 == 16) 578*e6089fecSBrad Love f2 = 0; 579*e6089fecSBrad Love else if ((f2 < 9) && (f2 != 0)) 580*e6089fecSBrad Love f2 = 9; 581*e6089fecSBrad Love 582*e6089fecSBrad Love if (f3 == 16) 583*e6089fecSBrad Love f3 = 0; 584*e6089fecSBrad Love else if ((f3 < 9) && (f3 != 0)) 585*e6089fecSBrad Love f3 = 9; 586*e6089fecSBrad Love } 587*e6089fecSBrad Love 588*e6089fecSBrad Love sm = N - 1; 589*e6089fecSBrad Love 590*e6089fecSBrad Love /* Write to registers */ 591*e6089fecSBrad Love //reg15 &= 0x01; 592*e6089fecSBrad Love //reg15 |= (pll_div_fb >> 8) & 0x01; 593*e6089fecSBrad Love 594*e6089fecSBrad Love //reg16 = pll_div_fb & 0xFF; 595*e6089fecSBrad Love 596*e6089fecSBrad Love reg1D &= ~0x03; 597*e6089fecSBrad Love reg1D |= sm; 598*e6089fecSBrad Love reg1D |= 0x80; 599*e6089fecSBrad Love 600*e6089fecSBrad Love reg1E = ((f3 << 4) + f2) & 0xFF; 601*e6089fecSBrad Love reg1F = ((f1 << 4) + f0) & 0xFF; 602*e6089fecSBrad Love 603*e6089fecSBrad Love m88ds3103b_dt_write(dev, 0x05, 0x40); 604*e6089fecSBrad Love m88ds3103b_dt_write(dev, 0x11, 0x08); 605*e6089fecSBrad Love m88ds3103b_dt_write(dev, 0x1D, reg1D); 606*e6089fecSBrad Love m88ds3103b_dt_write(dev, 0x1E, reg1E); 607*e6089fecSBrad Love m88ds3103b_dt_write(dev, 0x1F, reg1F); 608*e6089fecSBrad Love 609*e6089fecSBrad Love m88ds3103b_dt_write(dev, 0x17, 0xc1); 610*e6089fecSBrad Love m88ds3103b_dt_write(dev, 0x17, 0x81); 611*e6089fecSBrad Love 612*e6089fecSBrad Love usleep_range(5000, 5500); 613*e6089fecSBrad Love 614*e6089fecSBrad Love m88ds3103b_dt_write(dev, 0x05, 0x00); 615*e6089fecSBrad Love m88ds3103b_dt_write(dev, 0x11, 0x0A); 616*e6089fecSBrad Love 617*e6089fecSBrad Love usleep_range(5000, 5500); 618*e6089fecSBrad Love 619*e6089fecSBrad Love return 0; 620*e6089fecSBrad Love } 621*e6089fecSBrad Love 622395d00d1SAntti Palosaari static int m88ds3103_set_frontend(struct dvb_frontend *fe) 623395d00d1SAntti Palosaari { 6247978b8a1SAntti Palosaari struct m88ds3103_dev *dev = fe->demodulator_priv; 6257978b8a1SAntti Palosaari struct i2c_client *client = dev->client; 626395d00d1SAntti Palosaari struct dtv_frontend_properties *c = &fe->dtv_property_cache; 62706487deeSAntti Palosaari int ret, len; 628395d00d1SAntti Palosaari const struct m88ds3103_reg_val *init; 629b6851419Snibble.max u8 u8tmp, u8tmp1 = 0, u8tmp2 = 0; /* silence compiler warning */ 630f4df95bcSnibble.max u8 buf[3]; 631334ef18eSAntti Palosaari u16 u16tmp; 632*e6089fecSBrad Love u32 tuner_frequency_khz, target_mclk, u32tmp; 633395d00d1SAntti Palosaari s32 s32tmp; 634981fbe3dSJames Hutchinson static const struct reg_sequence reset_buf[] = { 635981fbe3dSJames Hutchinson {0x07, 0x80}, {0x07, 0x00} 636981fbe3dSJames Hutchinson }; 63741b9aa00SAntti Palosaari 6387978b8a1SAntti Palosaari dev_dbg(&client->dev, 6397978b8a1SAntti Palosaari "delivery_system=%d modulation=%d frequency=%u symbol_rate=%d inversion=%d pilot=%d rolloff=%d\n", 6407978b8a1SAntti Palosaari c->delivery_system, c->modulation, c->frequency, c->symbol_rate, 641395d00d1SAntti Palosaari c->inversion, c->pilot, c->rolloff); 642395d00d1SAntti Palosaari 6437978b8a1SAntti Palosaari if (!dev->warm) { 644395d00d1SAntti Palosaari ret = -EAGAIN; 645395d00d1SAntti Palosaari goto err; 646395d00d1SAntti Palosaari } 647395d00d1SAntti Palosaari 648f4df95bcSnibble.max /* reset */ 649981fbe3dSJames Hutchinson ret = regmap_multi_reg_write(dev->regmap, reset_buf, 2); 650f4df95bcSnibble.max if (ret) 651f4df95bcSnibble.max goto err; 652f4df95bcSnibble.max 653f4df95bcSnibble.max /* Disable demod clock path */ 6547978b8a1SAntti Palosaari if (dev->chip_id == M88RS6000_CHIP_ID) { 655*e6089fecSBrad Love if (dev->chiptype == M88DS3103_CHIPTYPE_3103B) { 656*e6089fecSBrad Love ret = regmap_read(dev->regmap, 0xb2, &u32tmp); 657*e6089fecSBrad Love if (ret) 658*e6089fecSBrad Love goto err; 659*e6089fecSBrad Love if (u32tmp == 0x01) { 660*e6089fecSBrad Love ret = regmap_write(dev->regmap, 0x00, 0x00); 661*e6089fecSBrad Love if (ret) 662*e6089fecSBrad Love goto err; 663*e6089fecSBrad Love ret = regmap_write(dev->regmap, 0xb2, 0x00); 664*e6089fecSBrad Love if (ret) 665*e6089fecSBrad Love goto err; 666*e6089fecSBrad Love } 667*e6089fecSBrad Love } 668*e6089fecSBrad Love 669478932b1SAntti Palosaari ret = regmap_write(dev->regmap, 0x06, 0xe0); 670f4df95bcSnibble.max if (ret) 671f4df95bcSnibble.max goto err; 672f4df95bcSnibble.max } 673f4df95bcSnibble.max 674395d00d1SAntti Palosaari /* program tuner */ 675395d00d1SAntti Palosaari if (fe->ops.tuner_ops.set_params) { 676395d00d1SAntti Palosaari ret = fe->ops.tuner_ops.set_params(fe); 677395d00d1SAntti Palosaari if (ret) 678395d00d1SAntti Palosaari goto err; 679395d00d1SAntti Palosaari } 680395d00d1SAntti Palosaari 681395d00d1SAntti Palosaari if (fe->ops.tuner_ops.get_frequency) { 682f5d9b88dSAntti Palosaari ret = fe->ops.tuner_ops.get_frequency(fe, &tuner_frequency_khz); 683395d00d1SAntti Palosaari if (ret) 684395d00d1SAntti Palosaari goto err; 6852f9dff3fSAntti Palosaari } else { 6862f9dff3fSAntti Palosaari /* 6872f9dff3fSAntti Palosaari * Use nominal target frequency as tuner driver does not provide 6882f9dff3fSAntti Palosaari * actual frequency used. Carrier offset calculation is not 6892f9dff3fSAntti Palosaari * valid. 6902f9dff3fSAntti Palosaari */ 691f5d9b88dSAntti Palosaari tuner_frequency_khz = c->frequency; 692395d00d1SAntti Palosaari } 693395d00d1SAntti Palosaari 694*e6089fecSBrad Love /* set M88RS6000/DS3103B demod main mclk and ts mclk from tuner die */ 6957978b8a1SAntti Palosaari if (dev->chip_id == M88RS6000_CHIP_ID) { 696f4df95bcSnibble.max if (c->symbol_rate > 45010000) 697f5d9b88dSAntti Palosaari dev->mclk = 110250000; 698f4df95bcSnibble.max else 699f5d9b88dSAntti Palosaari dev->mclk = 96000000; 700395d00d1SAntti Palosaari 701f4df95bcSnibble.max if (c->delivery_system == SYS_DVBS) 702f5d9b88dSAntti Palosaari target_mclk = 96000000; 703f4df95bcSnibble.max else 704f5d9b88dSAntti Palosaari target_mclk = 144000000; 705395d00d1SAntti Palosaari 706*e6089fecSBrad Love if (dev->chiptype == M88DS3103_CHIPTYPE_3103B) { 707*e6089fecSBrad Love m88ds3103b_select_mclk(dev); 708*e6089fecSBrad Love m88ds3103b_set_mclk(dev, target_mclk / 1000); 709*e6089fecSBrad Love } 710*e6089fecSBrad Love 711f4df95bcSnibble.max /* Enable demod clock path */ 712478932b1SAntti Palosaari ret = regmap_write(dev->regmap, 0x06, 0x00); 713f4df95bcSnibble.max if (ret) 714f4df95bcSnibble.max goto err; 715f4df95bcSnibble.max usleep_range(10000, 20000); 716f4df95bcSnibble.max } else { 717f4df95bcSnibble.max /* set M88DS3103 mclk and ts mclk. */ 718f5d9b88dSAntti Palosaari dev->mclk = 96000000; 719f4df95bcSnibble.max 7207978b8a1SAntti Palosaari switch (dev->cfg->ts_mode) { 721395d00d1SAntti Palosaari case M88DS3103_TS_SERIAL: 722395d00d1SAntti Palosaari case M88DS3103_TS_SERIAL_D7: 7237978b8a1SAntti Palosaari target_mclk = dev->cfg->ts_clk; 724395d00d1SAntti Palosaari break; 725395d00d1SAntti Palosaari case M88DS3103_TS_PARALLEL: 726395d00d1SAntti Palosaari case M88DS3103_TS_CI: 727b6851419Snibble.max if (c->delivery_system == SYS_DVBS) 728f5d9b88dSAntti Palosaari target_mclk = 96000000; 729b6851419Snibble.max else { 730395d00d1SAntti Palosaari if (c->symbol_rate < 18000000) 731f5d9b88dSAntti Palosaari target_mclk = 96000000; 732395d00d1SAntti Palosaari else if (c->symbol_rate < 28000000) 733f5d9b88dSAntti Palosaari target_mclk = 144000000; 734395d00d1SAntti Palosaari else 735f5d9b88dSAntti Palosaari target_mclk = 192000000; 736b6851419Snibble.max } 737395d00d1SAntti Palosaari break; 738395d00d1SAntti Palosaari default: 7397978b8a1SAntti Palosaari dev_dbg(&client->dev, "invalid ts_mode\n"); 740395d00d1SAntti Palosaari ret = -EINVAL; 741395d00d1SAntti Palosaari goto err; 742395d00d1SAntti Palosaari } 743f4df95bcSnibble.max 744f4df95bcSnibble.max switch (target_mclk) { 745f5d9b88dSAntti Palosaari case 96000000: 746f4df95bcSnibble.max u8tmp1 = 0x02; /* 0b10 */ 747f4df95bcSnibble.max u8tmp2 = 0x01; /* 0b01 */ 748f4df95bcSnibble.max break; 749f5d9b88dSAntti Palosaari case 144000000: 750f4df95bcSnibble.max u8tmp1 = 0x00; /* 0b00 */ 751f4df95bcSnibble.max u8tmp2 = 0x01; /* 0b01 */ 752f4df95bcSnibble.max break; 753f5d9b88dSAntti Palosaari case 192000000: 754f4df95bcSnibble.max u8tmp1 = 0x03; /* 0b11 */ 755f4df95bcSnibble.max u8tmp2 = 0x00; /* 0b00 */ 756f4df95bcSnibble.max break; 757f4df95bcSnibble.max } 75856ea37daSAntti Palosaari ret = m88ds3103_update_bits(dev, 0x22, 0xc0, u8tmp1 << 6); 759f4df95bcSnibble.max if (ret) 760f4df95bcSnibble.max goto err; 76156ea37daSAntti Palosaari ret = m88ds3103_update_bits(dev, 0x24, 0xc0, u8tmp2 << 6); 762f4df95bcSnibble.max if (ret) 763f4df95bcSnibble.max goto err; 764f4df95bcSnibble.max } 765f4df95bcSnibble.max 766478932b1SAntti Palosaari ret = regmap_write(dev->regmap, 0xb2, 0x01); 767f4df95bcSnibble.max if (ret) 768f4df95bcSnibble.max goto err; 769f4df95bcSnibble.max 770478932b1SAntti Palosaari ret = regmap_write(dev->regmap, 0x00, 0x01); 771f4df95bcSnibble.max if (ret) 772f4df95bcSnibble.max goto err; 773f4df95bcSnibble.max 774f4df95bcSnibble.max switch (c->delivery_system) { 775f4df95bcSnibble.max case SYS_DVBS: 7767978b8a1SAntti Palosaari if (dev->chip_id == M88RS6000_CHIP_ID) { 777f4df95bcSnibble.max len = ARRAY_SIZE(m88rs6000_dvbs_init_reg_vals); 778f4df95bcSnibble.max init = m88rs6000_dvbs_init_reg_vals; 779f4df95bcSnibble.max } else { 780f4df95bcSnibble.max len = ARRAY_SIZE(m88ds3103_dvbs_init_reg_vals); 781f4df95bcSnibble.max init = m88ds3103_dvbs_init_reg_vals; 782f4df95bcSnibble.max } 783f4df95bcSnibble.max break; 784f4df95bcSnibble.max case SYS_DVBS2: 7857978b8a1SAntti Palosaari if (dev->chip_id == M88RS6000_CHIP_ID) { 786f4df95bcSnibble.max len = ARRAY_SIZE(m88rs6000_dvbs2_init_reg_vals); 787f4df95bcSnibble.max init = m88rs6000_dvbs2_init_reg_vals; 788f4df95bcSnibble.max } else { 789f4df95bcSnibble.max len = ARRAY_SIZE(m88ds3103_dvbs2_init_reg_vals); 790f4df95bcSnibble.max init = m88ds3103_dvbs2_init_reg_vals; 791f4df95bcSnibble.max } 792395d00d1SAntti Palosaari break; 793395d00d1SAntti Palosaari default: 7947978b8a1SAntti Palosaari dev_dbg(&client->dev, "invalid delivery_system\n"); 795395d00d1SAntti Palosaari ret = -EINVAL; 796395d00d1SAntti Palosaari goto err; 797395d00d1SAntti Palosaari } 798395d00d1SAntti Palosaari 799395d00d1SAntti Palosaari /* program init table */ 8007978b8a1SAntti Palosaari if (c->delivery_system != dev->delivery_system) { 8017978b8a1SAntti Palosaari ret = m88ds3103_wr_reg_val_tab(dev, init, len); 802395d00d1SAntti Palosaari if (ret) 803395d00d1SAntti Palosaari goto err; 804395d00d1SAntti Palosaari } 805395d00d1SAntti Palosaari 8067978b8a1SAntti Palosaari if (dev->chip_id == M88RS6000_CHIP_ID) { 807f5d9b88dSAntti Palosaari if (c->delivery_system == SYS_DVBS2 && 808f5d9b88dSAntti Palosaari c->symbol_rate <= 5000000) { 809478932b1SAntti Palosaari ret = regmap_write(dev->regmap, 0xc0, 0x04); 810f4df95bcSnibble.max if (ret) 811f4df95bcSnibble.max goto err; 812f4df95bcSnibble.max buf[0] = 0x09; 813f4df95bcSnibble.max buf[1] = 0x22; 814f4df95bcSnibble.max buf[2] = 0x88; 815478932b1SAntti Palosaari ret = regmap_bulk_write(dev->regmap, 0x8a, buf, 3); 816f4df95bcSnibble.max if (ret) 817f4df95bcSnibble.max goto err; 818f4df95bcSnibble.max } 81956ea37daSAntti Palosaari ret = m88ds3103_update_bits(dev, 0x9d, 0x08, 0x08); 820f4df95bcSnibble.max if (ret) 821f4df95bcSnibble.max goto err; 822*e6089fecSBrad Love 823*e6089fecSBrad Love if (dev->chiptype == M88DS3103_CHIPTYPE_3103B) { 824*e6089fecSBrad Love buf[0] = m88ds3103b_dt_read(dev, 0x15); 825*e6089fecSBrad Love buf[1] = m88ds3103b_dt_read(dev, 0x16); 826*e6089fecSBrad Love 827*e6089fecSBrad Love if (c->symbol_rate > 45010000) { 828*e6089fecSBrad Love buf[0] &= ~0x03; 829*e6089fecSBrad Love buf[0] |= 0x02; 830*e6089fecSBrad Love buf[0] |= ((147 - 32) >> 8) & 0x01; 831*e6089fecSBrad Love buf[1] = (147 - 32) & 0xFF; 832*e6089fecSBrad Love 833*e6089fecSBrad Love dev->mclk = 110250 * 1000; 834*e6089fecSBrad Love } else { 835*e6089fecSBrad Love buf[0] &= ~0x03; 836*e6089fecSBrad Love buf[0] |= ((128 - 32) >> 8) & 0x01; 837*e6089fecSBrad Love buf[1] = (128 - 32) & 0xFF; 838*e6089fecSBrad Love 839*e6089fecSBrad Love dev->mclk = 96000 * 1000; 840*e6089fecSBrad Love } 841*e6089fecSBrad Love m88ds3103b_dt_write(dev, 0x15, buf[0]); 842*e6089fecSBrad Love m88ds3103b_dt_write(dev, 0x16, buf[1]); 843*e6089fecSBrad Love 844*e6089fecSBrad Love regmap_read(dev->regmap, 0x30, &u32tmp); 845*e6089fecSBrad Love u32tmp &= ~0x80; 846*e6089fecSBrad Love regmap_write(dev->regmap, 0x30, u32tmp & 0xff); 847*e6089fecSBrad Love } 848*e6089fecSBrad Love 849478932b1SAntti Palosaari ret = regmap_write(dev->regmap, 0xf1, 0x01); 850f4df95bcSnibble.max if (ret) 851f4df95bcSnibble.max goto err; 852*e6089fecSBrad Love 853*e6089fecSBrad Love if (dev->chiptype != M88DS3103_CHIPTYPE_3103B) { 85456ea37daSAntti Palosaari ret = m88ds3103_update_bits(dev, 0x30, 0x80, 0x80); 855f4df95bcSnibble.max if (ret) 856f4df95bcSnibble.max goto err; 857f4df95bcSnibble.max } 858*e6089fecSBrad Love } 859f4df95bcSnibble.max 8607978b8a1SAntti Palosaari switch (dev->cfg->ts_mode) { 861395d00d1SAntti Palosaari case M88DS3103_TS_SERIAL: 862395d00d1SAntti Palosaari u8tmp1 = 0x00; 86379d09330Snibble.max u8tmp = 0x06; 864395d00d1SAntti Palosaari break; 865395d00d1SAntti Palosaari case M88DS3103_TS_SERIAL_D7: 866395d00d1SAntti Palosaari u8tmp1 = 0x20; 86779d09330Snibble.max u8tmp = 0x06; 868395d00d1SAntti Palosaari break; 869395d00d1SAntti Palosaari case M88DS3103_TS_PARALLEL: 87079d09330Snibble.max u8tmp = 0x02; 871*e6089fecSBrad Love if (dev->chiptype == M88DS3103_CHIPTYPE_3103B) { 872*e6089fecSBrad Love u8tmp = 0x01; 873*e6089fecSBrad Love u8tmp1 = 0x01; 874*e6089fecSBrad Love } 875395d00d1SAntti Palosaari break; 876395d00d1SAntti Palosaari case M88DS3103_TS_CI: 87779d09330Snibble.max u8tmp = 0x03; 878395d00d1SAntti Palosaari break; 879395d00d1SAntti Palosaari default: 8807978b8a1SAntti Palosaari dev_dbg(&client->dev, "invalid ts_mode\n"); 881395d00d1SAntti Palosaari ret = -EINVAL; 882395d00d1SAntti Palosaari goto err; 883395d00d1SAntti Palosaari } 884395d00d1SAntti Palosaari 8857978b8a1SAntti Palosaari if (dev->cfg->ts_clk_pol) 88679d09330Snibble.max u8tmp |= 0x40; 88779d09330Snibble.max 888395d00d1SAntti Palosaari /* TS mode */ 889478932b1SAntti Palosaari ret = regmap_write(dev->regmap, 0xfd, u8tmp); 890395d00d1SAntti Palosaari if (ret) 891395d00d1SAntti Palosaari goto err; 892395d00d1SAntti Palosaari 8937978b8a1SAntti Palosaari switch (dev->cfg->ts_mode) { 894395d00d1SAntti Palosaari case M88DS3103_TS_SERIAL: 895395d00d1SAntti Palosaari case M88DS3103_TS_SERIAL_D7: 89656ea37daSAntti Palosaari ret = m88ds3103_update_bits(dev, 0x29, 0x20, u8tmp1); 897395d00d1SAntti Palosaari if (ret) 898395d00d1SAntti Palosaari goto err; 899334ef18eSAntti Palosaari u16tmp = 0; 900334ef18eSAntti Palosaari u8tmp1 = 0x3f; 901334ef18eSAntti Palosaari u8tmp2 = 0x3f; 902b6851419Snibble.max break; 903*e6089fecSBrad Love case M88DS3103_TS_PARALLEL: 904*e6089fecSBrad Love if (dev->chiptype == M88DS3103_CHIPTYPE_3103B) { 905*e6089fecSBrad Love ret = m88ds3103_update_bits(dev, 0x29, 0x01, u8tmp1); 906*e6089fecSBrad Love if (ret) 907*e6089fecSBrad Love goto err; 908*e6089fecSBrad Love } 909*e6089fecSBrad Love /* fall through */ 910b6851419Snibble.max default: 911334ef18eSAntti Palosaari u16tmp = DIV_ROUND_UP(target_mclk, dev->cfg->ts_clk); 912334ef18eSAntti Palosaari u8tmp1 = u16tmp / 2 - 1; 913334ef18eSAntti Palosaari u8tmp2 = DIV_ROUND_UP(u16tmp, 2) - 1; 914395d00d1SAntti Palosaari } 915395d00d1SAntti Palosaari 916f5d9b88dSAntti Palosaari dev_dbg(&client->dev, "target_mclk=%u ts_clk=%u ts_clk_divide_ratio=%u\n", 917334ef18eSAntti Palosaari target_mclk, dev->cfg->ts_clk, u16tmp); 918395d00d1SAntti Palosaari 919395d00d1SAntti Palosaari /* u8tmp1[5:2] => fe[3:0], u8tmp1[1:0] => ea[7:6] */ 920395d00d1SAntti Palosaari /* u8tmp2[5:0] => ea[5:0] */ 921334ef18eSAntti Palosaari u8tmp = (u8tmp1 >> 2) & 0x0f; 922334ef18eSAntti Palosaari ret = regmap_update_bits(dev->regmap, 0xfe, 0x0f, u8tmp); 923395d00d1SAntti Palosaari if (ret) 924395d00d1SAntti Palosaari goto err; 925395d00d1SAntti Palosaari u8tmp = ((u8tmp1 & 0x03) << 6) | u8tmp2 >> 0; 926478932b1SAntti Palosaari ret = regmap_write(dev->regmap, 0xea, u8tmp); 927395d00d1SAntti Palosaari if (ret) 928395d00d1SAntti Palosaari goto err; 929395d00d1SAntti Palosaari 930395d00d1SAntti Palosaari if (c->symbol_rate <= 3000000) 931395d00d1SAntti Palosaari u8tmp = 0x20; 932395d00d1SAntti Palosaari else if (c->symbol_rate <= 10000000) 933395d00d1SAntti Palosaari u8tmp = 0x10; 934395d00d1SAntti Palosaari else 935395d00d1SAntti Palosaari u8tmp = 0x06; 936395d00d1SAntti Palosaari 937*e6089fecSBrad Love if (dev->chiptype == M88DS3103_CHIPTYPE_3103B) 938*e6089fecSBrad Love m88ds3103b_set_mclk(dev, target_mclk / 1000); 939*e6089fecSBrad Love 940478932b1SAntti Palosaari ret = regmap_write(dev->regmap, 0xc3, 0x08); 941395d00d1SAntti Palosaari if (ret) 942395d00d1SAntti Palosaari goto err; 943395d00d1SAntti Palosaari 944478932b1SAntti Palosaari ret = regmap_write(dev->regmap, 0xc8, u8tmp); 945395d00d1SAntti Palosaari if (ret) 946395d00d1SAntti Palosaari goto err; 947395d00d1SAntti Palosaari 948478932b1SAntti Palosaari ret = regmap_write(dev->regmap, 0xc4, 0x08); 949395d00d1SAntti Palosaari if (ret) 950395d00d1SAntti Palosaari goto err; 951395d00d1SAntti Palosaari 952478932b1SAntti Palosaari ret = regmap_write(dev->regmap, 0xc7, 0x00); 953395d00d1SAntti Palosaari if (ret) 954395d00d1SAntti Palosaari goto err; 955395d00d1SAntti Palosaari 956f5d9b88dSAntti Palosaari u16tmp = DIV_ROUND_CLOSEST_ULL((u64)c->symbol_rate * 0x10000, dev->mclk); 957395d00d1SAntti Palosaari buf[0] = (u16tmp >> 0) & 0xff; 958395d00d1SAntti Palosaari buf[1] = (u16tmp >> 8) & 0xff; 959478932b1SAntti Palosaari ret = regmap_bulk_write(dev->regmap, 0x61, buf, 2); 960395d00d1SAntti Palosaari if (ret) 961395d00d1SAntti Palosaari goto err; 962395d00d1SAntti Palosaari 96356ea37daSAntti Palosaari ret = m88ds3103_update_bits(dev, 0x4d, 0x02, dev->cfg->spec_inv << 1); 964395d00d1SAntti Palosaari if (ret) 965395d00d1SAntti Palosaari goto err; 966395d00d1SAntti Palosaari 96756ea37daSAntti Palosaari ret = m88ds3103_update_bits(dev, 0x30, 0x10, dev->cfg->agc_inv << 4); 968395d00d1SAntti Palosaari if (ret) 969395d00d1SAntti Palosaari goto err; 970395d00d1SAntti Palosaari 971478932b1SAntti Palosaari ret = regmap_write(dev->regmap, 0x33, dev->cfg->agc); 972395d00d1SAntti Palosaari if (ret) 973395d00d1SAntti Palosaari goto err; 974395d00d1SAntti Palosaari 975*e6089fecSBrad Love if (dev->chiptype == M88DS3103_CHIPTYPE_3103B) { 976*e6089fecSBrad Love /* enable/disable 192M LDPC clock */ 977*e6089fecSBrad Love ret = m88ds3103_update_bits(dev, 0x29, 0x10, 978*e6089fecSBrad Love (c->delivery_system == SYS_DVBS) ? 0x10 : 0x0); 979*e6089fecSBrad Love if (ret) 980*e6089fecSBrad Love goto err; 981*e6089fecSBrad Love 982*e6089fecSBrad Love ret = m88ds3103_update_bits(dev, 0xc9, 0x08, 0x08); 983*e6089fecSBrad Love } 984*e6089fecSBrad Love 9857978b8a1SAntti Palosaari dev_dbg(&client->dev, "carrier offset=%d\n", 986f5d9b88dSAntti Palosaari (tuner_frequency_khz - c->frequency)); 987395d00d1SAntti Palosaari 988f5d9b88dSAntti Palosaari /* Use 32-bit calc as there is no s64 version of DIV_ROUND_CLOSEST() */ 989f5d9b88dSAntti Palosaari s32tmp = 0x10000 * (tuner_frequency_khz - c->frequency); 990f5d9b88dSAntti Palosaari s32tmp = DIV_ROUND_CLOSEST(s32tmp, dev->mclk / 1000); 991395d00d1SAntti Palosaari buf[0] = (s32tmp >> 0) & 0xff; 992395d00d1SAntti Palosaari buf[1] = (s32tmp >> 8) & 0xff; 993478932b1SAntti Palosaari ret = regmap_bulk_write(dev->regmap, 0x5e, buf, 2); 994395d00d1SAntti Palosaari if (ret) 995395d00d1SAntti Palosaari goto err; 996395d00d1SAntti Palosaari 997478932b1SAntti Palosaari ret = regmap_write(dev->regmap, 0x00, 0x00); 998395d00d1SAntti Palosaari if (ret) 999395d00d1SAntti Palosaari goto err; 1000395d00d1SAntti Palosaari 1001478932b1SAntti Palosaari ret = regmap_write(dev->regmap, 0xb2, 0x00); 1002395d00d1SAntti Palosaari if (ret) 1003395d00d1SAntti Palosaari goto err; 1004395d00d1SAntti Palosaari 10057978b8a1SAntti Palosaari dev->delivery_system = c->delivery_system; 1006395d00d1SAntti Palosaari 1007395d00d1SAntti Palosaari return 0; 1008395d00d1SAntti Palosaari err: 10097978b8a1SAntti Palosaari dev_dbg(&client->dev, "failed=%d\n", ret); 1010395d00d1SAntti Palosaari return ret; 1011395d00d1SAntti Palosaari } 1012395d00d1SAntti Palosaari 1013395d00d1SAntti Palosaari static int m88ds3103_init(struct dvb_frontend *fe) 1014395d00d1SAntti Palosaari { 10157978b8a1SAntti Palosaari struct m88ds3103_dev *dev = fe->demodulator_priv; 10167978b8a1SAntti Palosaari struct i2c_client *client = dev->client; 1017c1daf651SAntti Palosaari struct dtv_frontend_properties *c = &fe->dtv_property_cache; 101860701d5fSAntti Palosaari int ret, len, rem; 1019478932b1SAntti Palosaari unsigned int utmp; 102060701d5fSAntti Palosaari const struct firmware *firmware; 102160701d5fSAntti Palosaari const char *name; 102241b9aa00SAntti Palosaari 10237978b8a1SAntti Palosaari dev_dbg(&client->dev, "\n"); 1024395d00d1SAntti Palosaari 1025395d00d1SAntti Palosaari /* set cold state by default */ 10267978b8a1SAntti Palosaari dev->warm = false; 1027395d00d1SAntti Palosaari 1028395d00d1SAntti Palosaari /* wake up device from sleep */ 102956ea37daSAntti Palosaari ret = m88ds3103_update_bits(dev, 0x08, 0x01, 0x01); 1030395d00d1SAntti Palosaari if (ret) 1031395d00d1SAntti Palosaari goto err; 103256ea37daSAntti Palosaari ret = m88ds3103_update_bits(dev, 0x04, 0x01, 0x00); 1033395d00d1SAntti Palosaari if (ret) 1034395d00d1SAntti Palosaari goto err; 103556ea37daSAntti Palosaari ret = m88ds3103_update_bits(dev, 0x23, 0x10, 0x00); 1036395d00d1SAntti Palosaari if (ret) 1037395d00d1SAntti Palosaari goto err; 1038395d00d1SAntti Palosaari 1039395d00d1SAntti Palosaari /* firmware status */ 1040478932b1SAntti Palosaari ret = regmap_read(dev->regmap, 0xb9, &utmp); 1041395d00d1SAntti Palosaari if (ret) 1042395d00d1SAntti Palosaari goto err; 1043395d00d1SAntti Palosaari 1044478932b1SAntti Palosaari dev_dbg(&client->dev, "firmware=%02x\n", utmp); 1045395d00d1SAntti Palosaari 1046478932b1SAntti Palosaari if (utmp) 104760701d5fSAntti Palosaari goto warm; 1048395d00d1SAntti Palosaari 1049*e6089fecSBrad Love /* global reset, global diseqc reset, global fec reset */ 1050478932b1SAntti Palosaari ret = regmap_write(dev->regmap, 0x07, 0xe0); 1051f4df95bcSnibble.max if (ret) 1052f4df95bcSnibble.max goto err; 1053478932b1SAntti Palosaari ret = regmap_write(dev->regmap, 0x07, 0x00); 1054f4df95bcSnibble.max if (ret) 1055f4df95bcSnibble.max goto err; 1056f4df95bcSnibble.max 1057395d00d1SAntti Palosaari /* cold state - try to download firmware */ 10587978b8a1SAntti Palosaari dev_info(&client->dev, "found a '%s' in cold state\n", 1059*e6089fecSBrad Love dev->fe.ops.info.name); 1060395d00d1SAntti Palosaari 1061*e6089fecSBrad Love if (dev->chiptype == M88DS3103_CHIPTYPE_3103B) 1062*e6089fecSBrad Love name = M88DS3103B_FIRMWARE; 1063*e6089fecSBrad Love else if (dev->chip_id == M88RS6000_CHIP_ID) 106460701d5fSAntti Palosaari name = M88RS6000_FIRMWARE; 1065f4df95bcSnibble.max else 106660701d5fSAntti Palosaari name = M88DS3103_FIRMWARE; 1067*e6089fecSBrad Love 1068395d00d1SAntti Palosaari /* request the firmware, this will block and timeout */ 106960701d5fSAntti Palosaari ret = request_firmware(&firmware, name, &client->dev); 1070395d00d1SAntti Palosaari if (ret) { 107160701d5fSAntti Palosaari dev_err(&client->dev, "firmware file '%s' not found\n", name); 1072395d00d1SAntti Palosaari goto err; 1073395d00d1SAntti Palosaari } 1074395d00d1SAntti Palosaari 107560701d5fSAntti Palosaari dev_info(&client->dev, "downloading firmware from file '%s'\n", name); 1076395d00d1SAntti Palosaari 1077478932b1SAntti Palosaari ret = regmap_write(dev->regmap, 0xb2, 0x01); 1078395d00d1SAntti Palosaari if (ret) 107960701d5fSAntti Palosaari goto err_release_firmware; 1080395d00d1SAntti Palosaari 108160701d5fSAntti Palosaari for (rem = firmware->size; rem > 0; rem -= (dev->cfg->i2c_wr_max - 1)) { 108260701d5fSAntti Palosaari len = min(dev->cfg->i2c_wr_max - 1, rem); 1083478932b1SAntti Palosaari ret = regmap_bulk_write(dev->regmap, 0xb0, 108460701d5fSAntti Palosaari &firmware->data[firmware->size - rem], 108560701d5fSAntti Palosaari len); 1086395d00d1SAntti Palosaari if (ret) { 108760701d5fSAntti Palosaari dev_err(&client->dev, "firmware download failed %d\n", 10887978b8a1SAntti Palosaari ret); 108960701d5fSAntti Palosaari goto err_release_firmware; 1090395d00d1SAntti Palosaari } 1091395d00d1SAntti Palosaari } 1092395d00d1SAntti Palosaari 1093478932b1SAntti Palosaari ret = regmap_write(dev->regmap, 0xb2, 0x00); 1094395d00d1SAntti Palosaari if (ret) 109560701d5fSAntti Palosaari goto err_release_firmware; 1096395d00d1SAntti Palosaari 109760701d5fSAntti Palosaari release_firmware(firmware); 1098395d00d1SAntti Palosaari 1099478932b1SAntti Palosaari ret = regmap_read(dev->regmap, 0xb9, &utmp); 1100395d00d1SAntti Palosaari if (ret) 1101395d00d1SAntti Palosaari goto err; 1102395d00d1SAntti Palosaari 1103478932b1SAntti Palosaari if (!utmp) { 110460701d5fSAntti Palosaari ret = -EINVAL; 11057978b8a1SAntti Palosaari dev_info(&client->dev, "firmware did not run\n"); 1106395d00d1SAntti Palosaari goto err; 1107395d00d1SAntti Palosaari } 1108395d00d1SAntti Palosaari 11097978b8a1SAntti Palosaari dev_info(&client->dev, "found a '%s' in warm state\n", 1110*e6089fecSBrad Love dev->fe.ops.info.name); 11117978b8a1SAntti Palosaari dev_info(&client->dev, "firmware version: %X.%X\n", 1112478932b1SAntti Palosaari (utmp >> 4) & 0xf, (utmp >> 0 & 0xf)); 1113395d00d1SAntti Palosaari 1114*e6089fecSBrad Love if (dev->chiptype == M88DS3103_CHIPTYPE_3103B) { 1115*e6089fecSBrad Love m88ds3103b_dt_write(dev, 0x21, 0x92); 1116*e6089fecSBrad Love m88ds3103b_dt_write(dev, 0x15, 0x6C); 1117*e6089fecSBrad Love m88ds3103b_dt_write(dev, 0x17, 0xC1); 1118*e6089fecSBrad Love m88ds3103b_dt_write(dev, 0x17, 0x81); 1119*e6089fecSBrad Love } 112060701d5fSAntti Palosaari warm: 1121395d00d1SAntti Palosaari /* warm state */ 11227978b8a1SAntti Palosaari dev->warm = true; 11237978b8a1SAntti Palosaari 1124c1daf651SAntti Palosaari /* init stats here in order signal app which stats are supported */ 1125c1daf651SAntti Palosaari c->cnr.len = 1; 1126c1daf651SAntti Palosaari c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE; 1127ce80d713SAntti Palosaari c->post_bit_error.len = 1; 1128ce80d713SAntti Palosaari c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE; 1129ce80d713SAntti Palosaari c->post_bit_count.len = 1; 1130ce80d713SAntti Palosaari c->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE; 1131395d00d1SAntti Palosaari 11327978b8a1SAntti Palosaari return 0; 113360701d5fSAntti Palosaari err_release_firmware: 113460701d5fSAntti Palosaari release_firmware(firmware); 11355ed0cf88SMarkus Elfring err: 11367978b8a1SAntti Palosaari dev_dbg(&client->dev, "failed=%d\n", ret); 1137395d00d1SAntti Palosaari return ret; 1138395d00d1SAntti Palosaari } 1139395d00d1SAntti Palosaari 1140395d00d1SAntti Palosaari static int m88ds3103_sleep(struct dvb_frontend *fe) 1141395d00d1SAntti Palosaari { 11427978b8a1SAntti Palosaari struct m88ds3103_dev *dev = fe->demodulator_priv; 11437978b8a1SAntti Palosaari struct i2c_client *client = dev->client; 1144395d00d1SAntti Palosaari int ret; 1145478932b1SAntti Palosaari unsigned int utmp; 114641b9aa00SAntti Palosaari 11477978b8a1SAntti Palosaari dev_dbg(&client->dev, "\n"); 1148395d00d1SAntti Palosaari 11497978b8a1SAntti Palosaari dev->fe_status = 0; 11507978b8a1SAntti Palosaari dev->delivery_system = SYS_UNDEFINED; 1151395d00d1SAntti Palosaari 1152395d00d1SAntti Palosaari /* TS Hi-Z */ 11537978b8a1SAntti Palosaari if (dev->chip_id == M88RS6000_CHIP_ID) 1154478932b1SAntti Palosaari utmp = 0x29; 1155f4df95bcSnibble.max else 1156478932b1SAntti Palosaari utmp = 0x27; 115756ea37daSAntti Palosaari ret = m88ds3103_update_bits(dev, utmp, 0x01, 0x00); 1158395d00d1SAntti Palosaari if (ret) 1159395d00d1SAntti Palosaari goto err; 1160395d00d1SAntti Palosaari 1161395d00d1SAntti Palosaari /* sleep */ 116256ea37daSAntti Palosaari ret = m88ds3103_update_bits(dev, 0x08, 0x01, 0x00); 1163395d00d1SAntti Palosaari if (ret) 1164395d00d1SAntti Palosaari goto err; 116556ea37daSAntti Palosaari ret = m88ds3103_update_bits(dev, 0x04, 0x01, 0x01); 1166395d00d1SAntti Palosaari if (ret) 1167395d00d1SAntti Palosaari goto err; 116856ea37daSAntti Palosaari ret = m88ds3103_update_bits(dev, 0x23, 0x10, 0x10); 1169395d00d1SAntti Palosaari if (ret) 1170395d00d1SAntti Palosaari goto err; 1171395d00d1SAntti Palosaari 1172395d00d1SAntti Palosaari return 0; 1173395d00d1SAntti Palosaari err: 11747978b8a1SAntti Palosaari dev_dbg(&client->dev, "failed=%d\n", ret); 1175395d00d1SAntti Palosaari return ret; 1176395d00d1SAntti Palosaari } 1177395d00d1SAntti Palosaari 11787e3e68bcSMauro Carvalho Chehab static int m88ds3103_get_frontend(struct dvb_frontend *fe, 11797e3e68bcSMauro Carvalho Chehab struct dtv_frontend_properties *c) 1180395d00d1SAntti Palosaari { 11817978b8a1SAntti Palosaari struct m88ds3103_dev *dev = fe->demodulator_priv; 11827978b8a1SAntti Palosaari struct i2c_client *client = dev->client; 1183395d00d1SAntti Palosaari int ret; 1184395d00d1SAntti Palosaari u8 buf[3]; 118541b9aa00SAntti Palosaari 11867978b8a1SAntti Palosaari dev_dbg(&client->dev, "\n"); 1187395d00d1SAntti Palosaari 11887978b8a1SAntti Palosaari if (!dev->warm || !(dev->fe_status & FE_HAS_LOCK)) { 11899240c384SAntti Palosaari ret = 0; 1190395d00d1SAntti Palosaari goto err; 1191395d00d1SAntti Palosaari } 1192395d00d1SAntti Palosaari 1193395d00d1SAntti Palosaari switch (c->delivery_system) { 1194395d00d1SAntti Palosaari case SYS_DVBS: 1195478932b1SAntti Palosaari ret = regmap_bulk_read(dev->regmap, 0xe0, &buf[0], 1); 1196395d00d1SAntti Palosaari if (ret) 1197395d00d1SAntti Palosaari goto err; 1198395d00d1SAntti Palosaari 1199478932b1SAntti Palosaari ret = regmap_bulk_read(dev->regmap, 0xe6, &buf[1], 1); 1200395d00d1SAntti Palosaari if (ret) 1201395d00d1SAntti Palosaari goto err; 1202395d00d1SAntti Palosaari 1203395d00d1SAntti Palosaari switch ((buf[0] >> 2) & 0x01) { 1204395d00d1SAntti Palosaari case 0: 1205395d00d1SAntti Palosaari c->inversion = INVERSION_OFF; 1206395d00d1SAntti Palosaari break; 1207395d00d1SAntti Palosaari case 1: 1208395d00d1SAntti Palosaari c->inversion = INVERSION_ON; 1209395d00d1SAntti Palosaari break; 1210395d00d1SAntti Palosaari } 1211395d00d1SAntti Palosaari 1212395d00d1SAntti Palosaari switch ((buf[1] >> 5) & 0x07) { 1213395d00d1SAntti Palosaari case 0: 1214395d00d1SAntti Palosaari c->fec_inner = FEC_7_8; 1215395d00d1SAntti Palosaari break; 1216395d00d1SAntti Palosaari case 1: 1217395d00d1SAntti Palosaari c->fec_inner = FEC_5_6; 1218395d00d1SAntti Palosaari break; 1219395d00d1SAntti Palosaari case 2: 1220395d00d1SAntti Palosaari c->fec_inner = FEC_3_4; 1221395d00d1SAntti Palosaari break; 1222395d00d1SAntti Palosaari case 3: 1223395d00d1SAntti Palosaari c->fec_inner = FEC_2_3; 1224395d00d1SAntti Palosaari break; 1225395d00d1SAntti Palosaari case 4: 1226395d00d1SAntti Palosaari c->fec_inner = FEC_1_2; 1227395d00d1SAntti Palosaari break; 1228395d00d1SAntti Palosaari default: 12297978b8a1SAntti Palosaari dev_dbg(&client->dev, "invalid fec_inner\n"); 1230395d00d1SAntti Palosaari } 1231395d00d1SAntti Palosaari 1232395d00d1SAntti Palosaari c->modulation = QPSK; 1233395d00d1SAntti Palosaari 1234395d00d1SAntti Palosaari break; 1235395d00d1SAntti Palosaari case SYS_DVBS2: 1236478932b1SAntti Palosaari ret = regmap_bulk_read(dev->regmap, 0x7e, &buf[0], 1); 1237395d00d1SAntti Palosaari if (ret) 1238395d00d1SAntti Palosaari goto err; 1239395d00d1SAntti Palosaari 1240478932b1SAntti Palosaari ret = regmap_bulk_read(dev->regmap, 0x89, &buf[1], 1); 1241395d00d1SAntti Palosaari if (ret) 1242395d00d1SAntti Palosaari goto err; 1243395d00d1SAntti Palosaari 1244478932b1SAntti Palosaari ret = regmap_bulk_read(dev->regmap, 0xf2, &buf[2], 1); 1245395d00d1SAntti Palosaari if (ret) 1246395d00d1SAntti Palosaari goto err; 1247395d00d1SAntti Palosaari 1248395d00d1SAntti Palosaari switch ((buf[0] >> 0) & 0x0f) { 1249395d00d1SAntti Palosaari case 2: 1250395d00d1SAntti Palosaari c->fec_inner = FEC_2_5; 1251395d00d1SAntti Palosaari break; 1252395d00d1SAntti Palosaari case 3: 1253395d00d1SAntti Palosaari c->fec_inner = FEC_1_2; 1254395d00d1SAntti Palosaari break; 1255395d00d1SAntti Palosaari case 4: 1256395d00d1SAntti Palosaari c->fec_inner = FEC_3_5; 1257395d00d1SAntti Palosaari break; 1258395d00d1SAntti Palosaari case 5: 1259395d00d1SAntti Palosaari c->fec_inner = FEC_2_3; 1260395d00d1SAntti Palosaari break; 1261395d00d1SAntti Palosaari case 6: 1262395d00d1SAntti Palosaari c->fec_inner = FEC_3_4; 1263395d00d1SAntti Palosaari break; 1264395d00d1SAntti Palosaari case 7: 1265395d00d1SAntti Palosaari c->fec_inner = FEC_4_5; 1266395d00d1SAntti Palosaari break; 1267395d00d1SAntti Palosaari case 8: 1268395d00d1SAntti Palosaari c->fec_inner = FEC_5_6; 1269395d00d1SAntti Palosaari break; 1270395d00d1SAntti Palosaari case 9: 1271395d00d1SAntti Palosaari c->fec_inner = FEC_8_9; 1272395d00d1SAntti Palosaari break; 1273395d00d1SAntti Palosaari case 10: 1274395d00d1SAntti Palosaari c->fec_inner = FEC_9_10; 1275395d00d1SAntti Palosaari break; 1276395d00d1SAntti Palosaari default: 12777978b8a1SAntti Palosaari dev_dbg(&client->dev, "invalid fec_inner\n"); 1278395d00d1SAntti Palosaari } 1279395d00d1SAntti Palosaari 1280395d00d1SAntti Palosaari switch ((buf[0] >> 5) & 0x01) { 1281395d00d1SAntti Palosaari case 0: 1282395d00d1SAntti Palosaari c->pilot = PILOT_OFF; 1283395d00d1SAntti Palosaari break; 1284395d00d1SAntti Palosaari case 1: 1285395d00d1SAntti Palosaari c->pilot = PILOT_ON; 1286395d00d1SAntti Palosaari break; 1287395d00d1SAntti Palosaari } 1288395d00d1SAntti Palosaari 1289395d00d1SAntti Palosaari switch ((buf[0] >> 6) & 0x07) { 1290395d00d1SAntti Palosaari case 0: 1291395d00d1SAntti Palosaari c->modulation = QPSK; 1292395d00d1SAntti Palosaari break; 1293395d00d1SAntti Palosaari case 1: 1294395d00d1SAntti Palosaari c->modulation = PSK_8; 1295395d00d1SAntti Palosaari break; 1296395d00d1SAntti Palosaari case 2: 1297395d00d1SAntti Palosaari c->modulation = APSK_16; 1298395d00d1SAntti Palosaari break; 1299395d00d1SAntti Palosaari case 3: 1300395d00d1SAntti Palosaari c->modulation = APSK_32; 1301395d00d1SAntti Palosaari break; 1302395d00d1SAntti Palosaari default: 13037978b8a1SAntti Palosaari dev_dbg(&client->dev, "invalid modulation\n"); 1304395d00d1SAntti Palosaari } 1305395d00d1SAntti Palosaari 1306395d00d1SAntti Palosaari switch ((buf[1] >> 7) & 0x01) { 1307395d00d1SAntti Palosaari case 0: 1308395d00d1SAntti Palosaari c->inversion = INVERSION_OFF; 1309395d00d1SAntti Palosaari break; 1310395d00d1SAntti Palosaari case 1: 1311395d00d1SAntti Palosaari c->inversion = INVERSION_ON; 1312395d00d1SAntti Palosaari break; 1313395d00d1SAntti Palosaari } 1314395d00d1SAntti Palosaari 1315395d00d1SAntti Palosaari switch ((buf[2] >> 0) & 0x03) { 1316395d00d1SAntti Palosaari case 0: 1317395d00d1SAntti Palosaari c->rolloff = ROLLOFF_35; 1318395d00d1SAntti Palosaari break; 1319395d00d1SAntti Palosaari case 1: 1320395d00d1SAntti Palosaari c->rolloff = ROLLOFF_25; 1321395d00d1SAntti Palosaari break; 1322395d00d1SAntti Palosaari case 2: 1323395d00d1SAntti Palosaari c->rolloff = ROLLOFF_20; 1324395d00d1SAntti Palosaari break; 1325395d00d1SAntti Palosaari default: 13267978b8a1SAntti Palosaari dev_dbg(&client->dev, "invalid rolloff\n"); 1327395d00d1SAntti Palosaari } 1328395d00d1SAntti Palosaari break; 1329395d00d1SAntti Palosaari default: 13307978b8a1SAntti Palosaari dev_dbg(&client->dev, "invalid delivery_system\n"); 1331395d00d1SAntti Palosaari ret = -EINVAL; 1332395d00d1SAntti Palosaari goto err; 1333395d00d1SAntti Palosaari } 1334395d00d1SAntti Palosaari 1335478932b1SAntti Palosaari ret = regmap_bulk_read(dev->regmap, 0x6d, buf, 2); 1336395d00d1SAntti Palosaari if (ret) 1337395d00d1SAntti Palosaari goto err; 1338395d00d1SAntti Palosaari 1339f5d9b88dSAntti Palosaari c->symbol_rate = DIV_ROUND_CLOSEST_ULL((u64)(buf[1] << 8 | buf[0] << 0) * dev->mclk, 0x10000); 1340395d00d1SAntti Palosaari 1341395d00d1SAntti Palosaari return 0; 1342395d00d1SAntti Palosaari err: 13437978b8a1SAntti Palosaari dev_dbg(&client->dev, "failed=%d\n", ret); 1344395d00d1SAntti Palosaari return ret; 1345395d00d1SAntti Palosaari } 1346395d00d1SAntti Palosaari 1347395d00d1SAntti Palosaari static int m88ds3103_read_snr(struct dvb_frontend *fe, u16 *snr) 1348395d00d1SAntti Palosaari { 1349395d00d1SAntti Palosaari struct dtv_frontend_properties *c = &fe->dtv_property_cache; 135041b9aa00SAntti Palosaari 1351c1daf651SAntti Palosaari if (c->cnr.stat[0].scale == FE_SCALE_DECIBEL) 1352c1daf651SAntti Palosaari *snr = div_s64(c->cnr.stat[0].svalue, 100); 1353395d00d1SAntti Palosaari else 1354395d00d1SAntti Palosaari *snr = 0; 1355395d00d1SAntti Palosaari 1356395d00d1SAntti Palosaari return 0; 1357395d00d1SAntti Palosaari } 1358395d00d1SAntti Palosaari 13594423a2baSAntti Palosaari static int m88ds3103_read_ber(struct dvb_frontend *fe, u32 *ber) 13604423a2baSAntti Palosaari { 13617978b8a1SAntti Palosaari struct m88ds3103_dev *dev = fe->demodulator_priv; 136241b9aa00SAntti Palosaari 13637978b8a1SAntti Palosaari *ber = dev->dvbv3_ber; 13644423a2baSAntti Palosaari 13654423a2baSAntti Palosaari return 0; 13664423a2baSAntti Palosaari } 1367395d00d1SAntti Palosaari 1368395d00d1SAntti Palosaari static int m88ds3103_set_tone(struct dvb_frontend *fe, 13690df289a2SMauro Carvalho Chehab enum fe_sec_tone_mode fe_sec_tone_mode) 1370395d00d1SAntti Palosaari { 13717978b8a1SAntti Palosaari struct m88ds3103_dev *dev = fe->demodulator_priv; 13727978b8a1SAntti Palosaari struct i2c_client *client = dev->client; 1373395d00d1SAntti Palosaari int ret; 1374478932b1SAntti Palosaari unsigned int utmp, tone, reg_a1_mask; 137541b9aa00SAntti Palosaari 13767978b8a1SAntti Palosaari dev_dbg(&client->dev, "fe_sec_tone_mode=%d\n", fe_sec_tone_mode); 1377395d00d1SAntti Palosaari 13787978b8a1SAntti Palosaari if (!dev->warm) { 1379395d00d1SAntti Palosaari ret = -EAGAIN; 1380395d00d1SAntti Palosaari goto err; 1381395d00d1SAntti Palosaari } 1382395d00d1SAntti Palosaari 1383395d00d1SAntti Palosaari switch (fe_sec_tone_mode) { 1384395d00d1SAntti Palosaari case SEC_TONE_ON: 1385395d00d1SAntti Palosaari tone = 0; 1386418a97cbSAntti Palosaari reg_a1_mask = 0x47; 1387395d00d1SAntti Palosaari break; 1388395d00d1SAntti Palosaari case SEC_TONE_OFF: 1389395d00d1SAntti Palosaari tone = 1; 1390395d00d1SAntti Palosaari reg_a1_mask = 0x00; 1391395d00d1SAntti Palosaari break; 1392395d00d1SAntti Palosaari default: 13937978b8a1SAntti Palosaari dev_dbg(&client->dev, "invalid fe_sec_tone_mode\n"); 1394395d00d1SAntti Palosaari ret = -EINVAL; 1395395d00d1SAntti Palosaari goto err; 1396395d00d1SAntti Palosaari } 1397395d00d1SAntti Palosaari 1398478932b1SAntti Palosaari utmp = tone << 7 | dev->cfg->envelope_mode << 5; 139956ea37daSAntti Palosaari ret = m88ds3103_update_bits(dev, 0xa2, 0xe0, utmp); 1400395d00d1SAntti Palosaari if (ret) 1401395d00d1SAntti Palosaari goto err; 1402395d00d1SAntti Palosaari 1403478932b1SAntti Palosaari utmp = 1 << 2; 140456ea37daSAntti Palosaari ret = m88ds3103_update_bits(dev, 0xa1, reg_a1_mask, utmp); 1405395d00d1SAntti Palosaari if (ret) 1406395d00d1SAntti Palosaari goto err; 1407395d00d1SAntti Palosaari 1408395d00d1SAntti Palosaari return 0; 1409395d00d1SAntti Palosaari err: 14107978b8a1SAntti Palosaari dev_dbg(&client->dev, "failed=%d\n", ret); 1411395d00d1SAntti Palosaari return ret; 1412395d00d1SAntti Palosaari } 1413395d00d1SAntti Palosaari 141479d09330Snibble.max static int m88ds3103_set_voltage(struct dvb_frontend *fe, 14150df289a2SMauro Carvalho Chehab enum fe_sec_voltage fe_sec_voltage) 141679d09330Snibble.max { 14177978b8a1SAntti Palosaari struct m88ds3103_dev *dev = fe->demodulator_priv; 14187978b8a1SAntti Palosaari struct i2c_client *client = dev->client; 1419d28677ffSAntti Palosaari int ret; 1420478932b1SAntti Palosaari unsigned int utmp; 1421d28677ffSAntti Palosaari bool voltage_sel, voltage_dis; 142279d09330Snibble.max 14237978b8a1SAntti Palosaari dev_dbg(&client->dev, "fe_sec_voltage=%d\n", fe_sec_voltage); 142479d09330Snibble.max 14257978b8a1SAntti Palosaari if (!dev->warm) { 1426d28677ffSAntti Palosaari ret = -EAGAIN; 1427d28677ffSAntti Palosaari goto err; 1428d28677ffSAntti Palosaari } 142979d09330Snibble.max 1430d28677ffSAntti Palosaari switch (fe_sec_voltage) { 143179d09330Snibble.max case SEC_VOLTAGE_18: 1432afbd6eb4SMauro Carvalho Chehab voltage_sel = true; 1433afbd6eb4SMauro Carvalho Chehab voltage_dis = false; 143479d09330Snibble.max break; 143579d09330Snibble.max case SEC_VOLTAGE_13: 1436afbd6eb4SMauro Carvalho Chehab voltage_sel = false; 1437afbd6eb4SMauro Carvalho Chehab voltage_dis = false; 143879d09330Snibble.max break; 143979d09330Snibble.max case SEC_VOLTAGE_OFF: 1440afbd6eb4SMauro Carvalho Chehab voltage_sel = false; 1441afbd6eb4SMauro Carvalho Chehab voltage_dis = true; 144279d09330Snibble.max break; 1443d28677ffSAntti Palosaari default: 14447978b8a1SAntti Palosaari dev_dbg(&client->dev, "invalid fe_sec_voltage\n"); 1445d28677ffSAntti Palosaari ret = -EINVAL; 1446d28677ffSAntti Palosaari goto err; 144779d09330Snibble.max } 1448d28677ffSAntti Palosaari 1449d28677ffSAntti Palosaari /* output pin polarity */ 14507978b8a1SAntti Palosaari voltage_sel ^= dev->cfg->lnb_hv_pol; 14517978b8a1SAntti Palosaari voltage_dis ^= dev->cfg->lnb_en_pol; 1452d28677ffSAntti Palosaari 1453478932b1SAntti Palosaari utmp = voltage_dis << 1 | voltage_sel << 0; 145456ea37daSAntti Palosaari ret = m88ds3103_update_bits(dev, 0xa2, 0x03, utmp); 1455d28677ffSAntti Palosaari if (ret) 1456d28677ffSAntti Palosaari goto err; 145779d09330Snibble.max 145879d09330Snibble.max return 0; 1459d28677ffSAntti Palosaari err: 14607978b8a1SAntti Palosaari dev_dbg(&client->dev, "failed=%d\n", ret); 1461d28677ffSAntti Palosaari return ret; 146279d09330Snibble.max } 146379d09330Snibble.max 1464395d00d1SAntti Palosaari static int m88ds3103_diseqc_send_master_cmd(struct dvb_frontend *fe, 1465395d00d1SAntti Palosaari struct dvb_diseqc_master_cmd *diseqc_cmd) 1466395d00d1SAntti Palosaari { 14677978b8a1SAntti Palosaari struct m88ds3103_dev *dev = fe->demodulator_priv; 14687978b8a1SAntti Palosaari struct i2c_client *client = dev->client; 1469befa0cc1SAntti Palosaari int ret; 1470478932b1SAntti Palosaari unsigned int utmp; 1471befa0cc1SAntti Palosaari unsigned long timeout; 147241b9aa00SAntti Palosaari 14737978b8a1SAntti Palosaari dev_dbg(&client->dev, "msg=%*ph\n", 1474395d00d1SAntti Palosaari diseqc_cmd->msg_len, diseqc_cmd->msg); 1475395d00d1SAntti Palosaari 14767978b8a1SAntti Palosaari if (!dev->warm) { 1477395d00d1SAntti Palosaari ret = -EAGAIN; 1478395d00d1SAntti Palosaari goto err; 1479395d00d1SAntti Palosaari } 1480395d00d1SAntti Palosaari 1481395d00d1SAntti Palosaari if (diseqc_cmd->msg_len < 3 || diseqc_cmd->msg_len > 6) { 1482395d00d1SAntti Palosaari ret = -EINVAL; 1483395d00d1SAntti Palosaari goto err; 1484395d00d1SAntti Palosaari } 1485395d00d1SAntti Palosaari 1486478932b1SAntti Palosaari utmp = dev->cfg->envelope_mode << 5; 148756ea37daSAntti Palosaari ret = m88ds3103_update_bits(dev, 0xa2, 0xe0, utmp); 1488395d00d1SAntti Palosaari if (ret) 1489395d00d1SAntti Palosaari goto err; 1490395d00d1SAntti Palosaari 1491478932b1SAntti Palosaari ret = regmap_bulk_write(dev->regmap, 0xa3, diseqc_cmd->msg, 1492395d00d1SAntti Palosaari diseqc_cmd->msg_len); 1493395d00d1SAntti Palosaari if (ret) 1494395d00d1SAntti Palosaari goto err; 1495395d00d1SAntti Palosaari 1496478932b1SAntti Palosaari ret = regmap_write(dev->regmap, 0xa1, 1497395d00d1SAntti Palosaari (diseqc_cmd->msg_len - 1) << 3 | 0x07); 1498395d00d1SAntti Palosaari if (ret) 1499395d00d1SAntti Palosaari goto err; 1500395d00d1SAntti Palosaari 1501395d00d1SAntti Palosaari /* wait DiSEqC TX ready */ 1502befa0cc1SAntti Palosaari #define SEND_MASTER_CMD_TIMEOUT 120 1503befa0cc1SAntti Palosaari timeout = jiffies + msecs_to_jiffies(SEND_MASTER_CMD_TIMEOUT); 1504395d00d1SAntti Palosaari 15059ef3cdc1SAntti Palosaari /* DiSEqC message period is 13.5 ms per byte */ 15069ef3cdc1SAntti Palosaari utmp = diseqc_cmd->msg_len * 13500; 15079ef3cdc1SAntti Palosaari usleep_range(utmp - 4000, utmp); 1508befa0cc1SAntti Palosaari 1509478932b1SAntti Palosaari for (utmp = 1; !time_after(jiffies, timeout) && utmp;) { 1510478932b1SAntti Palosaari ret = regmap_read(dev->regmap, 0xa1, &utmp); 1511395d00d1SAntti Palosaari if (ret) 1512395d00d1SAntti Palosaari goto err; 1513478932b1SAntti Palosaari utmp = (utmp >> 6) & 0x1; 1514395d00d1SAntti Palosaari } 1515395d00d1SAntti Palosaari 1516478932b1SAntti Palosaari if (utmp == 0) { 15177978b8a1SAntti Palosaari dev_dbg(&client->dev, "diseqc tx took %u ms\n", 1518befa0cc1SAntti Palosaari jiffies_to_msecs(jiffies) - 1519befa0cc1SAntti Palosaari (jiffies_to_msecs(timeout) - SEND_MASTER_CMD_TIMEOUT)); 1520befa0cc1SAntti Palosaari } else { 15217978b8a1SAntti Palosaari dev_dbg(&client->dev, "diseqc tx timeout\n"); 1522395d00d1SAntti Palosaari 152356ea37daSAntti Palosaari ret = m88ds3103_update_bits(dev, 0xa1, 0xc0, 0x40); 1524395d00d1SAntti Palosaari if (ret) 1525395d00d1SAntti Palosaari goto err; 1526395d00d1SAntti Palosaari } 1527395d00d1SAntti Palosaari 152856ea37daSAntti Palosaari ret = m88ds3103_update_bits(dev, 0xa2, 0xc0, 0x80); 1529395d00d1SAntti Palosaari if (ret) 1530395d00d1SAntti Palosaari goto err; 1531395d00d1SAntti Palosaari 1532478932b1SAntti Palosaari if (utmp == 1) { 1533395d00d1SAntti Palosaari ret = -ETIMEDOUT; 1534395d00d1SAntti Palosaari goto err; 1535395d00d1SAntti Palosaari } 1536395d00d1SAntti Palosaari 1537395d00d1SAntti Palosaari return 0; 1538395d00d1SAntti Palosaari err: 15397978b8a1SAntti Palosaari dev_dbg(&client->dev, "failed=%d\n", ret); 1540395d00d1SAntti Palosaari return ret; 1541395d00d1SAntti Palosaari } 1542395d00d1SAntti Palosaari 1543395d00d1SAntti Palosaari static int m88ds3103_diseqc_send_burst(struct dvb_frontend *fe, 15440df289a2SMauro Carvalho Chehab enum fe_sec_mini_cmd fe_sec_mini_cmd) 1545395d00d1SAntti Palosaari { 15467978b8a1SAntti Palosaari struct m88ds3103_dev *dev = fe->demodulator_priv; 15477978b8a1SAntti Palosaari struct i2c_client *client = dev->client; 1548befa0cc1SAntti Palosaari int ret; 1549478932b1SAntti Palosaari unsigned int utmp, burst; 1550befa0cc1SAntti Palosaari unsigned long timeout; 155141b9aa00SAntti Palosaari 15527978b8a1SAntti Palosaari dev_dbg(&client->dev, "fe_sec_mini_cmd=%d\n", fe_sec_mini_cmd); 1553395d00d1SAntti Palosaari 15547978b8a1SAntti Palosaari if (!dev->warm) { 1555395d00d1SAntti Palosaari ret = -EAGAIN; 1556395d00d1SAntti Palosaari goto err; 1557395d00d1SAntti Palosaari } 1558395d00d1SAntti Palosaari 1559478932b1SAntti Palosaari utmp = dev->cfg->envelope_mode << 5; 156056ea37daSAntti Palosaari ret = m88ds3103_update_bits(dev, 0xa2, 0xe0, utmp); 1561395d00d1SAntti Palosaari if (ret) 1562395d00d1SAntti Palosaari goto err; 1563395d00d1SAntti Palosaari 1564395d00d1SAntti Palosaari switch (fe_sec_mini_cmd) { 1565395d00d1SAntti Palosaari case SEC_MINI_A: 1566395d00d1SAntti Palosaari burst = 0x02; 1567395d00d1SAntti Palosaari break; 1568395d00d1SAntti Palosaari case SEC_MINI_B: 1569395d00d1SAntti Palosaari burst = 0x01; 1570395d00d1SAntti Palosaari break; 1571395d00d1SAntti Palosaari default: 15727978b8a1SAntti Palosaari dev_dbg(&client->dev, "invalid fe_sec_mini_cmd\n"); 1573395d00d1SAntti Palosaari ret = -EINVAL; 1574395d00d1SAntti Palosaari goto err; 1575395d00d1SAntti Palosaari } 1576395d00d1SAntti Palosaari 1577478932b1SAntti Palosaari ret = regmap_write(dev->regmap, 0xa1, burst); 1578395d00d1SAntti Palosaari if (ret) 1579395d00d1SAntti Palosaari goto err; 1580395d00d1SAntti Palosaari 1581395d00d1SAntti Palosaari /* wait DiSEqC TX ready */ 1582befa0cc1SAntti Palosaari #define SEND_BURST_TIMEOUT 40 1583befa0cc1SAntti Palosaari timeout = jiffies + msecs_to_jiffies(SEND_BURST_TIMEOUT); 1584395d00d1SAntti Palosaari 1585befa0cc1SAntti Palosaari /* DiSEqC ToneBurst period is 12.5 ms */ 1586befa0cc1SAntti Palosaari usleep_range(8500, 12500); 1587befa0cc1SAntti Palosaari 1588478932b1SAntti Palosaari for (utmp = 1; !time_after(jiffies, timeout) && utmp;) { 1589478932b1SAntti Palosaari ret = regmap_read(dev->regmap, 0xa1, &utmp); 1590395d00d1SAntti Palosaari if (ret) 1591395d00d1SAntti Palosaari goto err; 1592478932b1SAntti Palosaari utmp = (utmp >> 6) & 0x1; 1593395d00d1SAntti Palosaari } 1594395d00d1SAntti Palosaari 1595478932b1SAntti Palosaari if (utmp == 0) { 15967978b8a1SAntti Palosaari dev_dbg(&client->dev, "diseqc tx took %u ms\n", 1597befa0cc1SAntti Palosaari jiffies_to_msecs(jiffies) - 1598befa0cc1SAntti Palosaari (jiffies_to_msecs(timeout) - SEND_BURST_TIMEOUT)); 1599befa0cc1SAntti Palosaari } else { 16007978b8a1SAntti Palosaari dev_dbg(&client->dev, "diseqc tx timeout\n"); 1601befa0cc1SAntti Palosaari 160256ea37daSAntti Palosaari ret = m88ds3103_update_bits(dev, 0xa1, 0xc0, 0x40); 1603befa0cc1SAntti Palosaari if (ret) 1604befa0cc1SAntti Palosaari goto err; 1605befa0cc1SAntti Palosaari } 1606395d00d1SAntti Palosaari 160756ea37daSAntti Palosaari ret = m88ds3103_update_bits(dev, 0xa2, 0xc0, 0x80); 1608395d00d1SAntti Palosaari if (ret) 1609395d00d1SAntti Palosaari goto err; 1610395d00d1SAntti Palosaari 1611478932b1SAntti Palosaari if (utmp == 1) { 1612395d00d1SAntti Palosaari ret = -ETIMEDOUT; 1613395d00d1SAntti Palosaari goto err; 1614395d00d1SAntti Palosaari } 1615395d00d1SAntti Palosaari 1616395d00d1SAntti Palosaari return 0; 1617395d00d1SAntti Palosaari err: 16187978b8a1SAntti Palosaari dev_dbg(&client->dev, "failed=%d\n", ret); 1619395d00d1SAntti Palosaari return ret; 1620395d00d1SAntti Palosaari } 1621395d00d1SAntti Palosaari 1622395d00d1SAntti Palosaari static int m88ds3103_get_tune_settings(struct dvb_frontend *fe, 1623395d00d1SAntti Palosaari struct dvb_frontend_tune_settings *s) 1624395d00d1SAntti Palosaari { 1625395d00d1SAntti Palosaari s->min_delay_ms = 3000; 1626395d00d1SAntti Palosaari 1627395d00d1SAntti Palosaari return 0; 1628395d00d1SAntti Palosaari } 1629395d00d1SAntti Palosaari 163044b9055bSAntti Palosaari static void m88ds3103_release(struct dvb_frontend *fe) 1631395d00d1SAntti Palosaari { 16327978b8a1SAntti Palosaari struct m88ds3103_dev *dev = fe->demodulator_priv; 16337978b8a1SAntti Palosaari struct i2c_client *client = dev->client; 163441b9aa00SAntti Palosaari 1635f01919e8SAntti Palosaari i2c_unregister_device(client); 1636395d00d1SAntti Palosaari } 1637395d00d1SAntti Palosaari 1638e00fed40SPeter Rosin static int m88ds3103_select(struct i2c_mux_core *muxc, u32 chan) 1639395d00d1SAntti Palosaari { 1640e00fed40SPeter Rosin struct m88ds3103_dev *dev = i2c_mux_priv(muxc); 16417978b8a1SAntti Palosaari struct i2c_client *client = dev->client; 1642395d00d1SAntti Palosaari int ret; 1643478932b1SAntti Palosaari struct i2c_msg msg = { 16447978b8a1SAntti Palosaari .addr = client->addr, 1645395d00d1SAntti Palosaari .flags = 0, 1646395d00d1SAntti Palosaari .len = 2, 1647395d00d1SAntti Palosaari .buf = "\x03\x11", 1648395d00d1SAntti Palosaari }; 1649395d00d1SAntti Palosaari 1650478932b1SAntti Palosaari /* Open tuner I2C repeater for 1 xfer, closes automatically */ 1651478932b1SAntti Palosaari ret = __i2c_transfer(client->adapter, &msg, 1); 1652395d00d1SAntti Palosaari if (ret != 1) { 16537978b8a1SAntti Palosaari dev_warn(&client->dev, "i2c wr failed=%d\n", ret); 165444b9055bSAntti Palosaari if (ret >= 0) 1655395d00d1SAntti Palosaari ret = -EREMOTEIO; 1656395d00d1SAntti Palosaari return ret; 1657395d00d1SAntti Palosaari } 1658395d00d1SAntti Palosaari 165944b9055bSAntti Palosaari return 0; 166044b9055bSAntti Palosaari } 1661395d00d1SAntti Palosaari 1662f01919e8SAntti Palosaari /* 1663f01919e8SAntti Palosaari * XXX: That is wrapper to m88ds3103_probe() via driver core in order to provide 1664f01919e8SAntti Palosaari * proper I2C client for legacy media attach binding. 1665f01919e8SAntti Palosaari * New users must use I2C client binding directly! 1666f01919e8SAntti Palosaari */ 1667395d00d1SAntti Palosaari struct dvb_frontend *m88ds3103_attach(const struct m88ds3103_config *cfg, 1668b9c97c67SMauro Carvalho Chehab struct i2c_adapter *i2c, 1669b9c97c67SMauro Carvalho Chehab struct i2c_adapter **tuner_i2c_adapter) 1670395d00d1SAntti Palosaari { 1671f01919e8SAntti Palosaari struct i2c_client *client; 1672f01919e8SAntti Palosaari struct i2c_board_info board_info; 1673b9c97c67SMauro Carvalho Chehab struct m88ds3103_platform_data pdata = {}; 1674395d00d1SAntti Palosaari 1675f01919e8SAntti Palosaari pdata.clk = cfg->clock; 1676f01919e8SAntti Palosaari pdata.i2c_wr_max = cfg->i2c_wr_max; 1677f01919e8SAntti Palosaari pdata.ts_mode = cfg->ts_mode; 1678f01919e8SAntti Palosaari pdata.ts_clk = cfg->ts_clk; 1679f01919e8SAntti Palosaari pdata.ts_clk_pol = cfg->ts_clk_pol; 1680f01919e8SAntti Palosaari pdata.spec_inv = cfg->spec_inv; 1681f01919e8SAntti Palosaari pdata.agc = cfg->agc; 1682f01919e8SAntti Palosaari pdata.agc_inv = cfg->agc_inv; 1683f01919e8SAntti Palosaari pdata.clk_out = cfg->clock_out; 1684f01919e8SAntti Palosaari pdata.envelope_mode = cfg->envelope_mode; 1685f01919e8SAntti Palosaari pdata.lnb_hv_pol = cfg->lnb_hv_pol; 1686f01919e8SAntti Palosaari pdata.lnb_en_pol = cfg->lnb_en_pol; 1687f01919e8SAntti Palosaari pdata.attach_in_use = true; 1688395d00d1SAntti Palosaari 1689f01919e8SAntti Palosaari memset(&board_info, 0, sizeof(board_info)); 1690c0decac1SMauro Carvalho Chehab strscpy(board_info.type, "m88ds3103", I2C_NAME_SIZE); 1691f01919e8SAntti Palosaari board_info.addr = cfg->i2c_addr; 1692f01919e8SAntti Palosaari board_info.platform_data = &pdata; 1693aace5926SWolfram Sang client = i2c_new_client_device(i2c, &board_info); 1694aace5926SWolfram Sang if (!i2c_client_has_driver(client)) 1695395d00d1SAntti Palosaari return NULL; 1696f01919e8SAntti Palosaari 1697f01919e8SAntti Palosaari *tuner_i2c_adapter = pdata.get_i2c_adapter(client); 1698f01919e8SAntti Palosaari return pdata.get_dvb_frontend(client); 1699395d00d1SAntti Palosaari } 1700395d00d1SAntti Palosaari EXPORT_SYMBOL(m88ds3103_attach); 1701395d00d1SAntti Palosaari 1702bd336e63SMax Kellermann static const struct dvb_frontend_ops m88ds3103_ops = { 1703395d00d1SAntti Palosaari .delsys = {SYS_DVBS, SYS_DVBS2}, 1704395d00d1SAntti Palosaari .info = { 17057978b8a1SAntti Palosaari .name = "Montage Technology M88DS3103", 1706f1b1eabfSMauro Carvalho Chehab .frequency_min_hz = 950 * MHz, 1707f1b1eabfSMauro Carvalho Chehab .frequency_max_hz = 2150 * MHz, 1708f1b1eabfSMauro Carvalho Chehab .frequency_tolerance_hz = 5 * MHz, 1709395d00d1SAntti Palosaari .symbol_rate_min = 1000000, 1710395d00d1SAntti Palosaari .symbol_rate_max = 45000000, 1711395d00d1SAntti Palosaari .caps = FE_CAN_INVERSION_AUTO | 1712395d00d1SAntti Palosaari FE_CAN_FEC_1_2 | 1713395d00d1SAntti Palosaari FE_CAN_FEC_2_3 | 1714395d00d1SAntti Palosaari FE_CAN_FEC_3_4 | 1715395d00d1SAntti Palosaari FE_CAN_FEC_4_5 | 1716395d00d1SAntti Palosaari FE_CAN_FEC_5_6 | 1717395d00d1SAntti Palosaari FE_CAN_FEC_6_7 | 1718395d00d1SAntti Palosaari FE_CAN_FEC_7_8 | 1719395d00d1SAntti Palosaari FE_CAN_FEC_8_9 | 1720395d00d1SAntti Palosaari FE_CAN_FEC_AUTO | 1721395d00d1SAntti Palosaari FE_CAN_QPSK | 1722395d00d1SAntti Palosaari FE_CAN_RECOVER | 1723395d00d1SAntti Palosaari FE_CAN_2G_MODULATION 1724395d00d1SAntti Palosaari }, 1725395d00d1SAntti Palosaari 1726395d00d1SAntti Palosaari .release = m88ds3103_release, 1727395d00d1SAntti Palosaari 1728395d00d1SAntti Palosaari .get_tune_settings = m88ds3103_get_tune_settings, 1729395d00d1SAntti Palosaari 1730395d00d1SAntti Palosaari .init = m88ds3103_init, 1731395d00d1SAntti Palosaari .sleep = m88ds3103_sleep, 1732395d00d1SAntti Palosaari 1733395d00d1SAntti Palosaari .set_frontend = m88ds3103_set_frontend, 1734395d00d1SAntti Palosaari .get_frontend = m88ds3103_get_frontend, 1735395d00d1SAntti Palosaari 1736395d00d1SAntti Palosaari .read_status = m88ds3103_read_status, 1737395d00d1SAntti Palosaari .read_snr = m88ds3103_read_snr, 17384423a2baSAntti Palosaari .read_ber = m88ds3103_read_ber, 1739395d00d1SAntti Palosaari 1740395d00d1SAntti Palosaari .diseqc_send_master_cmd = m88ds3103_diseqc_send_master_cmd, 1741395d00d1SAntti Palosaari .diseqc_send_burst = m88ds3103_diseqc_send_burst, 1742395d00d1SAntti Palosaari 1743395d00d1SAntti Palosaari .set_tone = m88ds3103_set_tone, 174479d09330Snibble.max .set_voltage = m88ds3103_set_voltage, 1745395d00d1SAntti Palosaari }; 1746395d00d1SAntti Palosaari 1747f01919e8SAntti Palosaari static struct dvb_frontend *m88ds3103_get_dvb_frontend(struct i2c_client *client) 1748f01919e8SAntti Palosaari { 17497978b8a1SAntti Palosaari struct m88ds3103_dev *dev = i2c_get_clientdata(client); 1750f01919e8SAntti Palosaari 1751f01919e8SAntti Palosaari dev_dbg(&client->dev, "\n"); 1752f01919e8SAntti Palosaari 1753f01919e8SAntti Palosaari return &dev->fe; 1754f01919e8SAntti Palosaari } 1755f01919e8SAntti Palosaari 1756f01919e8SAntti Palosaari static struct i2c_adapter *m88ds3103_get_i2c_adapter(struct i2c_client *client) 1757f01919e8SAntti Palosaari { 17587978b8a1SAntti Palosaari struct m88ds3103_dev *dev = i2c_get_clientdata(client); 1759f01919e8SAntti Palosaari 1760f01919e8SAntti Palosaari dev_dbg(&client->dev, "\n"); 1761f01919e8SAntti Palosaari 1762e00fed40SPeter Rosin return dev->muxc->adapter[0]; 1763f01919e8SAntti Palosaari } 1764f01919e8SAntti Palosaari 1765f01919e8SAntti Palosaari static int m88ds3103_probe(struct i2c_client *client, 1766f01919e8SAntti Palosaari const struct i2c_device_id *id) 1767f01919e8SAntti Palosaari { 17687978b8a1SAntti Palosaari struct m88ds3103_dev *dev; 1769f01919e8SAntti Palosaari struct m88ds3103_platform_data *pdata = client->dev.platform_data; 1770f01919e8SAntti Palosaari int ret; 1771478932b1SAntti Palosaari unsigned int utmp; 1772f01919e8SAntti Palosaari 1773f01919e8SAntti Palosaari dev = kzalloc(sizeof(*dev), GFP_KERNEL); 1774f01919e8SAntti Palosaari if (!dev) { 1775f01919e8SAntti Palosaari ret = -ENOMEM; 1776f01919e8SAntti Palosaari goto err; 1777f01919e8SAntti Palosaari } 1778f01919e8SAntti Palosaari 1779f01919e8SAntti Palosaari dev->client = client; 1780f01919e8SAntti Palosaari dev->config.clock = pdata->clk; 1781f01919e8SAntti Palosaari dev->config.i2c_wr_max = pdata->i2c_wr_max; 1782f01919e8SAntti Palosaari dev->config.ts_mode = pdata->ts_mode; 1783f5d9b88dSAntti Palosaari dev->config.ts_clk = pdata->ts_clk * 1000; 1784f01919e8SAntti Palosaari dev->config.ts_clk_pol = pdata->ts_clk_pol; 1785f01919e8SAntti Palosaari dev->config.spec_inv = pdata->spec_inv; 1786f01919e8SAntti Palosaari dev->config.agc_inv = pdata->agc_inv; 1787f01919e8SAntti Palosaari dev->config.clock_out = pdata->clk_out; 1788f01919e8SAntti Palosaari dev->config.envelope_mode = pdata->envelope_mode; 1789f01919e8SAntti Palosaari dev->config.agc = pdata->agc; 1790f01919e8SAntti Palosaari dev->config.lnb_hv_pol = pdata->lnb_hv_pol; 1791f01919e8SAntti Palosaari dev->config.lnb_en_pol = pdata->lnb_en_pol; 1792f01919e8SAntti Palosaari dev->cfg = &dev->config; 1793478932b1SAntti Palosaari /* create regmap */ 1794478932b1SAntti Palosaari dev->regmap_config.reg_bits = 8, 1795478932b1SAntti Palosaari dev->regmap_config.val_bits = 8, 1796478932b1SAntti Palosaari dev->regmap_config.lock_arg = dev, 1797478932b1SAntti Palosaari dev->regmap = devm_regmap_init_i2c(client, &dev->regmap_config); 1798478932b1SAntti Palosaari if (IS_ERR(dev->regmap)) { 1799478932b1SAntti Palosaari ret = PTR_ERR(dev->regmap); 1800478932b1SAntti Palosaari goto err_kfree; 1801478932b1SAntti Palosaari } 1802f01919e8SAntti Palosaari 1803f01919e8SAntti Palosaari /* 0x00: chip id[6:0], 0x01: chip ver[7:0], 0x02: chip ver[15:8] */ 1804478932b1SAntti Palosaari ret = regmap_read(dev->regmap, 0x00, &utmp); 1805f01919e8SAntti Palosaari if (ret) 1806f01919e8SAntti Palosaari goto err_kfree; 1807f01919e8SAntti Palosaari 1808478932b1SAntti Palosaari dev->chip_id = utmp >> 1; 1809*e6089fecSBrad Love dev->chiptype = (u8)id->driver_data; 1810*e6089fecSBrad Love 1811478932b1SAntti Palosaari dev_dbg(&client->dev, "chip_id=%02x\n", dev->chip_id); 1812f01919e8SAntti Palosaari 1813478932b1SAntti Palosaari switch (dev->chip_id) { 1814f01919e8SAntti Palosaari case M88RS6000_CHIP_ID: 1815f01919e8SAntti Palosaari case M88DS3103_CHIP_ID: 1816f01919e8SAntti Palosaari break; 1817f01919e8SAntti Palosaari default: 1818b9c97c67SMauro Carvalho Chehab ret = -ENODEV; 1819b9c97c67SMauro Carvalho Chehab dev_err(&client->dev, "Unknown device. Chip_id=%02x\n", dev->chip_id); 1820f01919e8SAntti Palosaari goto err_kfree; 1821f01919e8SAntti Palosaari } 1822f01919e8SAntti Palosaari 1823f01919e8SAntti Palosaari switch (dev->cfg->clock_out) { 1824f01919e8SAntti Palosaari case M88DS3103_CLOCK_OUT_DISABLED: 1825478932b1SAntti Palosaari utmp = 0x80; 1826f01919e8SAntti Palosaari break; 1827f01919e8SAntti Palosaari case M88DS3103_CLOCK_OUT_ENABLED: 1828478932b1SAntti Palosaari utmp = 0x00; 1829f01919e8SAntti Palosaari break; 1830f01919e8SAntti Palosaari case M88DS3103_CLOCK_OUT_ENABLED_DIV2: 1831478932b1SAntti Palosaari utmp = 0x10; 1832f01919e8SAntti Palosaari break; 1833f01919e8SAntti Palosaari default: 18344347df6aSDan Carpenter ret = -EINVAL; 1835f01919e8SAntti Palosaari goto err_kfree; 1836f01919e8SAntti Palosaari } 1837f01919e8SAntti Palosaari 1838334ef18eSAntti Palosaari if (!pdata->ts_clk) { 1839334ef18eSAntti Palosaari ret = -EINVAL; 1840334ef18eSAntti Palosaari goto err_kfree; 1841334ef18eSAntti Palosaari } 1842334ef18eSAntti Palosaari 1843f01919e8SAntti Palosaari /* 0x29 register is defined differently for m88rs6000. */ 1844f01919e8SAntti Palosaari /* set internal tuner address to 0x21 */ 1845478932b1SAntti Palosaari if (dev->chip_id == M88RS6000_CHIP_ID) 1846478932b1SAntti Palosaari utmp = 0x00; 1847f01919e8SAntti Palosaari 1848478932b1SAntti Palosaari ret = regmap_write(dev->regmap, 0x29, utmp); 1849f01919e8SAntti Palosaari if (ret) 1850f01919e8SAntti Palosaari goto err_kfree; 1851f01919e8SAntti Palosaari 1852f01919e8SAntti Palosaari /* sleep */ 185356ea37daSAntti Palosaari ret = m88ds3103_update_bits(dev, 0x08, 0x01, 0x00); 1854f01919e8SAntti Palosaari if (ret) 1855f01919e8SAntti Palosaari goto err_kfree; 185656ea37daSAntti Palosaari ret = m88ds3103_update_bits(dev, 0x04, 0x01, 0x01); 1857f01919e8SAntti Palosaari if (ret) 1858f01919e8SAntti Palosaari goto err_kfree; 185956ea37daSAntti Palosaari ret = m88ds3103_update_bits(dev, 0x23, 0x10, 0x10); 1860f01919e8SAntti Palosaari if (ret) 1861f01919e8SAntti Palosaari goto err_kfree; 1862f01919e8SAntti Palosaari 1863f01919e8SAntti Palosaari /* create mux i2c adapter for tuner */ 1864e00fed40SPeter Rosin dev->muxc = i2c_mux_alloc(client->adapter, &client->dev, 1, 0, 0, 1865e00fed40SPeter Rosin m88ds3103_select, NULL); 1866e00fed40SPeter Rosin if (!dev->muxc) { 18674347df6aSDan Carpenter ret = -ENOMEM; 1868f01919e8SAntti Palosaari goto err_kfree; 18694347df6aSDan Carpenter } 1870e00fed40SPeter Rosin dev->muxc->priv = dev; 1871e00fed40SPeter Rosin ret = i2c_mux_add_adapter(dev->muxc, 0, 0, 0); 1872e00fed40SPeter Rosin if (ret) 1873e00fed40SPeter Rosin goto err_kfree; 1874f01919e8SAntti Palosaari 1875f01919e8SAntti Palosaari /* create dvb_frontend */ 1876f01919e8SAntti Palosaari memcpy(&dev->fe.ops, &m88ds3103_ops, sizeof(struct dvb_frontend_ops)); 1877*e6089fecSBrad Love if (dev->chiptype == M88DS3103_CHIPTYPE_3103B) 1878*e6089fecSBrad Love strscpy(dev->fe.ops.info.name, "Montage Technology M88DS3103B", 1879*e6089fecSBrad Love sizeof(dev->fe.ops.info.name)); 1880*e6089fecSBrad Love else if (dev->chip_id == M88RS6000_CHIP_ID) 188185709cbfSMauro Carvalho Chehab strscpy(dev->fe.ops.info.name, "Montage Technology M88RS6000", 18827978b8a1SAntti Palosaari sizeof(dev->fe.ops.info.name)); 1883f01919e8SAntti Palosaari if (!pdata->attach_in_use) 1884f01919e8SAntti Palosaari dev->fe.ops.release = NULL; 1885f01919e8SAntti Palosaari dev->fe.demodulator_priv = dev; 1886f01919e8SAntti Palosaari i2c_set_clientdata(client, dev); 1887f01919e8SAntti Palosaari 1888f01919e8SAntti Palosaari /* setup callbacks */ 1889f01919e8SAntti Palosaari pdata->get_dvb_frontend = m88ds3103_get_dvb_frontend; 1890f01919e8SAntti Palosaari pdata->get_i2c_adapter = m88ds3103_get_i2c_adapter; 1891*e6089fecSBrad Love 1892*e6089fecSBrad Love if (dev->chiptype == M88DS3103_CHIPTYPE_3103B) { 1893*e6089fecSBrad Love /* enable i2c repeater for tuner */ 1894*e6089fecSBrad Love m88ds3103_update_bits(dev, 0x11, 0x01, 0x01); 1895*e6089fecSBrad Love 1896*e6089fecSBrad Love /* get frontend address */ 1897*e6089fecSBrad Love ret = regmap_read(dev->regmap, 0x29, &utmp); 1898*e6089fecSBrad Love if (ret) 1899*e6089fecSBrad Love goto err_kfree; 1900*e6089fecSBrad Love dev->dt_addr = ((utmp & 0x80) == 0) ? 0x42 >> 1 : 0x40 >> 1; 1901*e6089fecSBrad Love dev_err(&client->dev, "dt addr is 0x%02x", dev->dt_addr); 1902*e6089fecSBrad Love 1903*e6089fecSBrad Love dev->dt_client = i2c_new_dummy_device(client->adapter, 1904*e6089fecSBrad Love dev->dt_addr); 1905*e6089fecSBrad Love if (!dev->dt_client) { 1906*e6089fecSBrad Love ret = -ENODEV; 1907*e6089fecSBrad Love goto err_kfree; 1908*e6089fecSBrad Love } 1909*e6089fecSBrad Love } 1910*e6089fecSBrad Love 1911f01919e8SAntti Palosaari return 0; 1912f01919e8SAntti Palosaari err_kfree: 1913f01919e8SAntti Palosaari kfree(dev); 1914f01919e8SAntti Palosaari err: 1915f01919e8SAntti Palosaari dev_dbg(&client->dev, "failed=%d\n", ret); 1916f01919e8SAntti Palosaari return ret; 1917f01919e8SAntti Palosaari } 1918f01919e8SAntti Palosaari 1919f01919e8SAntti Palosaari static int m88ds3103_remove(struct i2c_client *client) 1920f01919e8SAntti Palosaari { 19217978b8a1SAntti Palosaari struct m88ds3103_dev *dev = i2c_get_clientdata(client); 1922f01919e8SAntti Palosaari 1923f01919e8SAntti Palosaari dev_dbg(&client->dev, "\n"); 1924f01919e8SAntti Palosaari 1925*e6089fecSBrad Love if (dev->dt_client) 1926*e6089fecSBrad Love i2c_unregister_device(dev->dt_client); 1927*e6089fecSBrad Love 1928e00fed40SPeter Rosin i2c_mux_del_adapters(dev->muxc); 1929f01919e8SAntti Palosaari 1930f01919e8SAntti Palosaari kfree(dev); 1931f01919e8SAntti Palosaari return 0; 1932f01919e8SAntti Palosaari } 1933f01919e8SAntti Palosaari 1934f01919e8SAntti Palosaari static const struct i2c_device_id m88ds3103_id_table[] = { 1935*e6089fecSBrad Love {"m88ds3103", M88DS3103_CHIPTYPE_3103}, 1936*e6089fecSBrad Love {"m88rs6000", M88DS3103_CHIPTYPE_RS6000}, 1937*e6089fecSBrad Love {"m88ds3103b", M88DS3103_CHIPTYPE_3103B}, 1938f01919e8SAntti Palosaari {} 1939f01919e8SAntti Palosaari }; 1940f01919e8SAntti Palosaari MODULE_DEVICE_TABLE(i2c, m88ds3103_id_table); 1941f01919e8SAntti Palosaari 1942f01919e8SAntti Palosaari static struct i2c_driver m88ds3103_driver = { 1943f01919e8SAntti Palosaari .driver = { 1944f01919e8SAntti Palosaari .name = "m88ds3103", 1945f01919e8SAntti Palosaari .suppress_bind_attrs = true, 1946f01919e8SAntti Palosaari }, 1947f01919e8SAntti Palosaari .probe = m88ds3103_probe, 1948f01919e8SAntti Palosaari .remove = m88ds3103_remove, 1949f01919e8SAntti Palosaari .id_table = m88ds3103_id_table, 1950f01919e8SAntti Palosaari }; 1951f01919e8SAntti Palosaari 1952f01919e8SAntti Palosaari module_i2c_driver(m88ds3103_driver); 1953f01919e8SAntti Palosaari 1954395d00d1SAntti Palosaari MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>"); 19557978b8a1SAntti Palosaari MODULE_DESCRIPTION("Montage Technology M88DS3103 DVB-S/S2 demodulator driver"); 1956395d00d1SAntti Palosaari MODULE_LICENSE("GPL"); 1957395d00d1SAntti Palosaari MODULE_FIRMWARE(M88DS3103_FIRMWARE); 1958f4df95bcSnibble.max MODULE_FIRMWARE(M88RS6000_FIRMWARE); 1959*e6089fecSBrad Love MODULE_FIRMWARE(M88DS3103B_FIRMWARE); 1960