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 */
m88ds3103_update_bits(struct m88ds3103_dev * dev,u8 reg,u8 mask,u8 val)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 */
m88ds3103_wr_reg_val_tab(struct m88ds3103_dev * dev,const struct m88ds3103_reg_val * tab,int tab_len)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 /*
68e6089fecSBrad Love * m88ds3103b demod has an internal device related to clocking. First the i2c
69e6089fecSBrad Love * gate must be opened, for one transaction, then writes will be allowed.
70e6089fecSBrad Love */
m88ds3103b_dt_write(struct m88ds3103_dev * dev,int reg,int data)71e6089fecSBrad Love static int m88ds3103b_dt_write(struct m88ds3103_dev *dev, int reg, int data)
72e6089fecSBrad Love {
73e6089fecSBrad Love struct i2c_client *client = dev->client;
74e6089fecSBrad Love u8 buf[] = {reg, data};
75e6089fecSBrad Love u8 val;
76e6089fecSBrad Love int ret;
77e6089fecSBrad Love struct i2c_msg msg = {
78e6089fecSBrad Love .addr = dev->dt_addr, .flags = 0, .buf = buf, .len = 2
79e6089fecSBrad Love };
80e6089fecSBrad Love
81e6089fecSBrad Love m88ds3103_update_bits(dev, 0x11, 0x01, 0x00);
82e6089fecSBrad Love
83e6089fecSBrad Love val = 0x11;
84e6089fecSBrad Love ret = regmap_write(dev->regmap, 0x03, val);
85e6089fecSBrad Love if (ret)
86e6089fecSBrad Love dev_dbg(&client->dev, "fail=%d\n", ret);
87e6089fecSBrad Love
88e6089fecSBrad Love ret = i2c_transfer(dev->dt_client->adapter, &msg, 1);
89e6089fecSBrad Love if (ret != 1) {
90e6089fecSBrad Love dev_err(&client->dev, "0x%02x (ret=%i, reg=0x%02x, value=0x%02x)\n",
91e6089fecSBrad Love dev->dt_addr, ret, reg, data);
92e6089fecSBrad Love
93e6089fecSBrad Love m88ds3103_update_bits(dev, 0x11, 0x01, 0x01);
94e6089fecSBrad Love return -EREMOTEIO;
95e6089fecSBrad Love }
96e6089fecSBrad Love m88ds3103_update_bits(dev, 0x11, 0x01, 0x01);
97e6089fecSBrad Love
98e6089fecSBrad Love dev_dbg(&client->dev, "0x%02x reg 0x%02x, value 0x%02x\n",
99e6089fecSBrad Love dev->dt_addr, reg, data);
100e6089fecSBrad Love
101e6089fecSBrad Love return 0;
102e6089fecSBrad Love }
103e6089fecSBrad Love
104e6089fecSBrad Love /*
105e6089fecSBrad Love * m88ds3103b demod has an internal device related to clocking. First the i2c
106e6089fecSBrad Love * gate must be opened, for two transactions, then reads will be allowed.
107e6089fecSBrad Love */
m88ds3103b_dt_read(struct m88ds3103_dev * dev,u8 reg)108e6089fecSBrad Love static int m88ds3103b_dt_read(struct m88ds3103_dev *dev, u8 reg)
109e6089fecSBrad Love {
110e6089fecSBrad Love struct i2c_client *client = dev->client;
111e6089fecSBrad Love int ret;
112e6089fecSBrad Love u8 val;
113e6089fecSBrad Love u8 b0[] = { reg };
114e6089fecSBrad Love u8 b1[] = { 0 };
115e6089fecSBrad Love struct i2c_msg msg[] = {
116e6089fecSBrad Love {
117e6089fecSBrad Love .addr = dev->dt_addr,
118e6089fecSBrad Love .flags = 0,
119e6089fecSBrad Love .buf = b0,
120e6089fecSBrad Love .len = 1
121e6089fecSBrad Love },
122e6089fecSBrad Love {
123e6089fecSBrad Love .addr = dev->dt_addr,
124e6089fecSBrad Love .flags = I2C_M_RD,
125e6089fecSBrad Love .buf = b1,
126e6089fecSBrad Love .len = 1
127e6089fecSBrad Love }
128e6089fecSBrad Love };
129e6089fecSBrad Love
130e6089fecSBrad Love m88ds3103_update_bits(dev, 0x11, 0x01, 0x00);
131e6089fecSBrad Love
132e6089fecSBrad Love val = 0x12;
133e6089fecSBrad Love ret = regmap_write(dev->regmap, 0x03, val);
134e6089fecSBrad Love if (ret)
135e6089fecSBrad Love dev_dbg(&client->dev, "fail=%d\n", ret);
136e6089fecSBrad Love
137e6089fecSBrad Love ret = i2c_transfer(dev->dt_client->adapter, msg, 2);
138e6089fecSBrad Love if (ret != 2) {
139e6089fecSBrad Love dev_err(&client->dev, "0x%02x (ret=%d, reg=0x%02x)\n",
140e6089fecSBrad Love dev->dt_addr, ret, reg);
141e6089fecSBrad Love
142e6089fecSBrad Love m88ds3103_update_bits(dev, 0x11, 0x01, 0x01);
143e6089fecSBrad Love return -EREMOTEIO;
144e6089fecSBrad Love }
145e6089fecSBrad Love m88ds3103_update_bits(dev, 0x11, 0x01, 0x01);
146e6089fecSBrad Love
147e6089fecSBrad Love dev_dbg(&client->dev, "0x%02x reg 0x%02x, value 0x%02x\n",
148e6089fecSBrad Love dev->dt_addr, reg, b1[0]);
149e6089fecSBrad Love
150e6089fecSBrad Love return b1[0];
151e6089fecSBrad Love }
152e6089fecSBrad Love
153e6089fecSBrad Love /*
1540f91c9d6SDavid Howells * Get the demodulator AGC PWM voltage setting supplied to the tuner.
1550f91c9d6SDavid Howells */
m88ds3103_get_agc_pwm(struct dvb_frontend * fe,u8 * _agc_pwm)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
m88ds3103_read_status(struct dvb_frontend * fe,enum fe_status * status)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
m88ds3103b_select_mclk(struct m88ds3103_dev * dev)377e6089fecSBrad Love static int m88ds3103b_select_mclk(struct m88ds3103_dev *dev)
378e6089fecSBrad Love {
379e6089fecSBrad Love struct i2c_client *client = dev->client;
380e6089fecSBrad Love struct dtv_frontend_properties *c = &dev->fe.dtv_property_cache;
381e6089fecSBrad Love u32 adc_Freq_MHz[3] = {96, 93, 99};
382e6089fecSBrad Love u8 reg16_list[3] = {96, 92, 100}, reg16, reg15;
383e6089fecSBrad Love u32 offset_MHz[3];
384e6089fecSBrad Love u32 max_offset = 0;
385e6089fecSBrad Love u32 old_setting = dev->mclk;
386e6089fecSBrad Love u32 tuner_freq_MHz = c->frequency / 1000;
387e6089fecSBrad Love u8 i;
388e6089fecSBrad Love char big_symbol = 0;
389e6089fecSBrad Love
390e6089fecSBrad Love big_symbol = (c->symbol_rate > 45010000) ? 1 : 0;
391e6089fecSBrad Love
392e6089fecSBrad Love if (big_symbol) {
393e6089fecSBrad Love reg16 = 115;
394e6089fecSBrad Love } else {
395e6089fecSBrad Love reg16 = 96;
396e6089fecSBrad Love
397e6089fecSBrad Love /* TODO: IS THIS NECESSARY ? */
398e6089fecSBrad Love for (i = 0; i < 3; i++) {
399e6089fecSBrad Love offset_MHz[i] = tuner_freq_MHz % adc_Freq_MHz[i];
400e6089fecSBrad Love
401e6089fecSBrad Love if (offset_MHz[i] > (adc_Freq_MHz[i] / 2))
402e6089fecSBrad Love offset_MHz[i] = adc_Freq_MHz[i] - offset_MHz[i];
403e6089fecSBrad Love
404e6089fecSBrad Love if (offset_MHz[i] > max_offset) {
405e6089fecSBrad Love max_offset = offset_MHz[i];
406e6089fecSBrad Love reg16 = reg16_list[i];
407e6089fecSBrad Love dev->mclk = adc_Freq_MHz[i] * 1000 * 1000;
408e6089fecSBrad Love
409e6089fecSBrad Love if (big_symbol)
410e6089fecSBrad Love dev->mclk /= 2;
411e6089fecSBrad Love
412e6089fecSBrad Love dev_dbg(&client->dev, "modifying mclk %u -> %u\n",
413e6089fecSBrad Love old_setting, dev->mclk);
414e6089fecSBrad Love }
415e6089fecSBrad Love }
416e6089fecSBrad Love }
417e6089fecSBrad Love
418e6089fecSBrad Love if (dev->mclk == 93000000)
419e6089fecSBrad Love regmap_write(dev->regmap, 0xA0, 0x42);
420e6089fecSBrad Love else if (dev->mclk == 96000000)
421e6089fecSBrad Love regmap_write(dev->regmap, 0xA0, 0x44);
422e6089fecSBrad Love else if (dev->mclk == 99000000)
423e6089fecSBrad Love regmap_write(dev->regmap, 0xA0, 0x46);
424e6089fecSBrad Love else if (dev->mclk == 110250000)
425e6089fecSBrad Love regmap_write(dev->regmap, 0xA0, 0x4E);
426e6089fecSBrad Love else
427e6089fecSBrad Love regmap_write(dev->regmap, 0xA0, 0x44);
428e6089fecSBrad Love
429e6089fecSBrad Love reg15 = m88ds3103b_dt_read(dev, 0x15);
430e6089fecSBrad Love
431e6089fecSBrad Love m88ds3103b_dt_write(dev, 0x05, 0x40);
432e6089fecSBrad Love m88ds3103b_dt_write(dev, 0x11, 0x08);
433e6089fecSBrad Love
434e6089fecSBrad Love if (big_symbol)
435e6089fecSBrad Love reg15 |= 0x02;
436e6089fecSBrad Love else
437e6089fecSBrad Love reg15 &= ~0x02;
438e6089fecSBrad Love
439e6089fecSBrad Love m88ds3103b_dt_write(dev, 0x15, reg15);
440e6089fecSBrad Love m88ds3103b_dt_write(dev, 0x16, reg16);
441e6089fecSBrad Love
442e6089fecSBrad Love usleep_range(5000, 5500);
443e6089fecSBrad Love
444e6089fecSBrad Love m88ds3103b_dt_write(dev, 0x05, 0x00);
445e6089fecSBrad Love m88ds3103b_dt_write(dev, 0x11, (u8)(big_symbol ? 0x0E : 0x0A));
446e6089fecSBrad Love
447e6089fecSBrad Love usleep_range(5000, 5500);
448e6089fecSBrad Love
449e6089fecSBrad Love return 0;
450e6089fecSBrad Love }
451e6089fecSBrad Love
m88ds3103b_set_mclk(struct m88ds3103_dev * dev,u32 mclk_khz)452e6089fecSBrad Love static int m88ds3103b_set_mclk(struct m88ds3103_dev *dev, u32 mclk_khz)
453e6089fecSBrad Love {
454c41898e8SMauro Carvalho Chehab u8 reg15, reg16, reg1D, reg1E, reg1F, tmp;
455e6089fecSBrad Love u8 sm, f0 = 0, f1 = 0, f2 = 0, f3 = 0;
456e6089fecSBrad Love u16 pll_div_fb, N;
457e6089fecSBrad Love u32 div;
458e6089fecSBrad Love
459e6089fecSBrad Love reg15 = m88ds3103b_dt_read(dev, 0x15);
460e6089fecSBrad Love reg16 = m88ds3103b_dt_read(dev, 0x16);
461e6089fecSBrad Love reg1D = m88ds3103b_dt_read(dev, 0x1D);
462e6089fecSBrad Love
463e6089fecSBrad Love if (dev->cfg->ts_mode != M88DS3103_TS_SERIAL) {
464e6089fecSBrad Love if (reg16 == 92)
465e6089fecSBrad Love tmp = 93;
466e6089fecSBrad Love else if (reg16 == 100)
467e6089fecSBrad Love tmp = 99;
468e6089fecSBrad Love else
469e6089fecSBrad Love tmp = 96;
470e6089fecSBrad Love
471e6089fecSBrad Love mclk_khz *= tmp;
472e6089fecSBrad Love mclk_khz /= 96;
473e6089fecSBrad Love }
474e6089fecSBrad Love
475e6089fecSBrad Love pll_div_fb = (reg15 & 0x01) << 8;
476e6089fecSBrad Love pll_div_fb += reg16;
477e6089fecSBrad Love pll_div_fb += 32;
478e6089fecSBrad Love
479e6089fecSBrad Love div = 9000 * pll_div_fb * 4;
480e6089fecSBrad Love div /= mclk_khz;
481e6089fecSBrad Love
482e6089fecSBrad Love if (dev->cfg->ts_mode == M88DS3103_TS_SERIAL) {
483e6089fecSBrad Love if (div <= 32) {
484e6089fecSBrad Love N = 2;
485e6089fecSBrad Love
486e6089fecSBrad Love f0 = 0;
487e6089fecSBrad Love f1 = div / N;
488e6089fecSBrad Love f2 = div - f1;
489e6089fecSBrad Love f3 = 0;
490e6089fecSBrad Love } else if (div <= 34) {
491e6089fecSBrad Love N = 3;
492e6089fecSBrad Love
493e6089fecSBrad Love f0 = div / N;
494e6089fecSBrad Love f1 = (div - f0) / (N - 1);
495e6089fecSBrad Love f2 = div - f0 - f1;
496e6089fecSBrad Love f3 = 0;
497e6089fecSBrad Love } else if (div <= 64) {
498e6089fecSBrad Love N = 4;
499e6089fecSBrad Love
500e6089fecSBrad Love f0 = div / N;
501e6089fecSBrad Love f1 = (div - f0) / (N - 1);
502e6089fecSBrad Love f2 = (div - f0 - f1) / (N - 2);
503e6089fecSBrad Love f3 = div - f0 - f1 - f2;
504e6089fecSBrad Love } else {
505e6089fecSBrad Love N = 4;
506e6089fecSBrad Love
507e6089fecSBrad Love f0 = 16;
508e6089fecSBrad Love f1 = 16;
509e6089fecSBrad Love f2 = 16;
510e6089fecSBrad Love f3 = 16;
511e6089fecSBrad Love }
512e6089fecSBrad Love
513e6089fecSBrad Love if (f0 == 16)
514e6089fecSBrad Love f0 = 0;
515e6089fecSBrad Love else if ((f0 < 8) && (f0 != 0))
516e6089fecSBrad Love f0 = 8;
517e6089fecSBrad Love
518e6089fecSBrad Love if (f1 == 16)
519e6089fecSBrad Love f1 = 0;
520e6089fecSBrad Love else if ((f1 < 8) && (f1 != 0))
521e6089fecSBrad Love f1 = 8;
522e6089fecSBrad Love
523e6089fecSBrad Love if (f2 == 16)
524e6089fecSBrad Love f2 = 0;
525e6089fecSBrad Love else if ((f2 < 8) && (f2 != 0))
526e6089fecSBrad Love f2 = 8;
527e6089fecSBrad Love
528e6089fecSBrad Love if (f3 == 16)
529e6089fecSBrad Love f3 = 0;
530e6089fecSBrad Love else if ((f3 < 8) && (f3 != 0))
531e6089fecSBrad Love f3 = 8;
532e6089fecSBrad Love } else {
533e6089fecSBrad Love if (div <= 32) {
534e6089fecSBrad Love N = 2;
535e6089fecSBrad Love
536e6089fecSBrad Love f0 = 0;
537e6089fecSBrad Love f1 = div / N;
538e6089fecSBrad Love f2 = div - f1;
539e6089fecSBrad Love f3 = 0;
540e6089fecSBrad Love } else if (div <= 48) {
541e6089fecSBrad Love N = 3;
542e6089fecSBrad Love
543e6089fecSBrad Love f0 = div / N;
544e6089fecSBrad Love f1 = (div - f0) / (N - 1);
545e6089fecSBrad Love f2 = div - f0 - f1;
546e6089fecSBrad Love f3 = 0;
547e6089fecSBrad Love } else if (div <= 64) {
548e6089fecSBrad Love N = 4;
549e6089fecSBrad Love
550e6089fecSBrad Love f0 = div / N;
551e6089fecSBrad Love f1 = (div - f0) / (N - 1);
552e6089fecSBrad Love f2 = (div - f0 - f1) / (N - 2);
553e6089fecSBrad Love f3 = div - f0 - f1 - f2;
554e6089fecSBrad Love } else {
555e6089fecSBrad Love N = 4;
556e6089fecSBrad Love
557e6089fecSBrad Love f0 = 16;
558e6089fecSBrad Love f1 = 16;
559e6089fecSBrad Love f2 = 16;
560e6089fecSBrad Love f3 = 16;
561e6089fecSBrad Love }
562e6089fecSBrad Love
563e6089fecSBrad Love if (f0 == 16)
564e6089fecSBrad Love f0 = 0;
565e6089fecSBrad Love else if ((f0 < 9) && (f0 != 0))
566e6089fecSBrad Love f0 = 9;
567e6089fecSBrad Love
568e6089fecSBrad Love if (f1 == 16)
569e6089fecSBrad Love f1 = 0;
570e6089fecSBrad Love else if ((f1 < 9) && (f1 != 0))
571e6089fecSBrad Love f1 = 9;
572e6089fecSBrad Love
573e6089fecSBrad Love if (f2 == 16)
574e6089fecSBrad Love f2 = 0;
575e6089fecSBrad Love else if ((f2 < 9) && (f2 != 0))
576e6089fecSBrad Love f2 = 9;
577e6089fecSBrad Love
578e6089fecSBrad Love if (f3 == 16)
579e6089fecSBrad Love f3 = 0;
580e6089fecSBrad Love else if ((f3 < 9) && (f3 != 0))
581e6089fecSBrad Love f3 = 9;
582e6089fecSBrad Love }
583e6089fecSBrad Love
584e6089fecSBrad Love sm = N - 1;
585e6089fecSBrad Love
586e6089fecSBrad Love /* Write to registers */
587e6089fecSBrad Love //reg15 &= 0x01;
588e6089fecSBrad Love //reg15 |= (pll_div_fb >> 8) & 0x01;
589e6089fecSBrad Love
590e6089fecSBrad Love //reg16 = pll_div_fb & 0xFF;
591e6089fecSBrad Love
592e6089fecSBrad Love reg1D &= ~0x03;
593e6089fecSBrad Love reg1D |= sm;
594e6089fecSBrad Love reg1D |= 0x80;
595e6089fecSBrad Love
596e6089fecSBrad Love reg1E = ((f3 << 4) + f2) & 0xFF;
597e6089fecSBrad Love reg1F = ((f1 << 4) + f0) & 0xFF;
598e6089fecSBrad Love
599e6089fecSBrad Love m88ds3103b_dt_write(dev, 0x05, 0x40);
600e6089fecSBrad Love m88ds3103b_dt_write(dev, 0x11, 0x08);
601e6089fecSBrad Love m88ds3103b_dt_write(dev, 0x1D, reg1D);
602e6089fecSBrad Love m88ds3103b_dt_write(dev, 0x1E, reg1E);
603e6089fecSBrad Love m88ds3103b_dt_write(dev, 0x1F, reg1F);
604e6089fecSBrad Love
605e6089fecSBrad Love m88ds3103b_dt_write(dev, 0x17, 0xc1);
606e6089fecSBrad Love m88ds3103b_dt_write(dev, 0x17, 0x81);
607e6089fecSBrad Love
608e6089fecSBrad Love usleep_range(5000, 5500);
609e6089fecSBrad Love
610e6089fecSBrad Love m88ds3103b_dt_write(dev, 0x05, 0x00);
611e6089fecSBrad Love m88ds3103b_dt_write(dev, 0x11, 0x0A);
612e6089fecSBrad Love
613e6089fecSBrad Love usleep_range(5000, 5500);
614e6089fecSBrad Love
615e6089fecSBrad Love return 0;
616e6089fecSBrad Love }
617e6089fecSBrad Love
m88ds3103_set_frontend(struct dvb_frontend * fe)618395d00d1SAntti Palosaari static int m88ds3103_set_frontend(struct dvb_frontend *fe)
619395d00d1SAntti Palosaari {
6207978b8a1SAntti Palosaari struct m88ds3103_dev *dev = fe->demodulator_priv;
6217978b8a1SAntti Palosaari struct i2c_client *client = dev->client;
622395d00d1SAntti Palosaari struct dtv_frontend_properties *c = &fe->dtv_property_cache;
62306487deeSAntti Palosaari int ret, len;
624395d00d1SAntti Palosaari const struct m88ds3103_reg_val *init;
625b6851419Snibble.max u8 u8tmp, u8tmp1 = 0, u8tmp2 = 0; /* silence compiler warning */
626f4df95bcSnibble.max u8 buf[3];
627334ef18eSAntti Palosaari u16 u16tmp;
628e6089fecSBrad Love u32 tuner_frequency_khz, target_mclk, u32tmp;
629395d00d1SAntti Palosaari s32 s32tmp;
630981fbe3dSJames Hutchinson static const struct reg_sequence reset_buf[] = {
631981fbe3dSJames Hutchinson {0x07, 0x80}, {0x07, 0x00}
632981fbe3dSJames Hutchinson };
63341b9aa00SAntti Palosaari
6347978b8a1SAntti Palosaari dev_dbg(&client->dev,
6357978b8a1SAntti Palosaari "delivery_system=%d modulation=%d frequency=%u symbol_rate=%d inversion=%d pilot=%d rolloff=%d\n",
6367978b8a1SAntti Palosaari c->delivery_system, c->modulation, c->frequency, c->symbol_rate,
637395d00d1SAntti Palosaari c->inversion, c->pilot, c->rolloff);
638395d00d1SAntti Palosaari
6397978b8a1SAntti Palosaari if (!dev->warm) {
640395d00d1SAntti Palosaari ret = -EAGAIN;
641395d00d1SAntti Palosaari goto err;
642395d00d1SAntti Palosaari }
643395d00d1SAntti Palosaari
644f4df95bcSnibble.max /* reset */
645981fbe3dSJames Hutchinson ret = regmap_multi_reg_write(dev->regmap, reset_buf, 2);
646f4df95bcSnibble.max if (ret)
647f4df95bcSnibble.max goto err;
648f4df95bcSnibble.max
649f4df95bcSnibble.max /* Disable demod clock path */
6507978b8a1SAntti Palosaari if (dev->chip_id == M88RS6000_CHIP_ID) {
651e6089fecSBrad Love if (dev->chiptype == M88DS3103_CHIPTYPE_3103B) {
652e6089fecSBrad Love ret = regmap_read(dev->regmap, 0xb2, &u32tmp);
653e6089fecSBrad Love if (ret)
654e6089fecSBrad Love goto err;
655e6089fecSBrad Love if (u32tmp == 0x01) {
656e6089fecSBrad Love ret = regmap_write(dev->regmap, 0x00, 0x00);
657e6089fecSBrad Love if (ret)
658e6089fecSBrad Love goto err;
659e6089fecSBrad Love ret = regmap_write(dev->regmap, 0xb2, 0x00);
660e6089fecSBrad Love if (ret)
661e6089fecSBrad Love goto err;
662e6089fecSBrad Love }
663e6089fecSBrad Love }
664e6089fecSBrad Love
665478932b1SAntti Palosaari ret = regmap_write(dev->regmap, 0x06, 0xe0);
666f4df95bcSnibble.max if (ret)
667f4df95bcSnibble.max goto err;
668f4df95bcSnibble.max }
669f4df95bcSnibble.max
670395d00d1SAntti Palosaari /* program tuner */
671395d00d1SAntti Palosaari if (fe->ops.tuner_ops.set_params) {
672395d00d1SAntti Palosaari ret = fe->ops.tuner_ops.set_params(fe);
673395d00d1SAntti Palosaari if (ret)
674395d00d1SAntti Palosaari goto err;
675395d00d1SAntti Palosaari }
676395d00d1SAntti Palosaari
677395d00d1SAntti Palosaari if (fe->ops.tuner_ops.get_frequency) {
678f5d9b88dSAntti Palosaari ret = fe->ops.tuner_ops.get_frequency(fe, &tuner_frequency_khz);
679395d00d1SAntti Palosaari if (ret)
680395d00d1SAntti Palosaari goto err;
6812f9dff3fSAntti Palosaari } else {
6822f9dff3fSAntti Palosaari /*
6832f9dff3fSAntti Palosaari * Use nominal target frequency as tuner driver does not provide
6842f9dff3fSAntti Palosaari * actual frequency used. Carrier offset calculation is not
6852f9dff3fSAntti Palosaari * valid.
6862f9dff3fSAntti Palosaari */
687f5d9b88dSAntti Palosaari tuner_frequency_khz = c->frequency;
688395d00d1SAntti Palosaari }
689395d00d1SAntti Palosaari
690e6089fecSBrad Love /* set M88RS6000/DS3103B demod main mclk and ts mclk from tuner die */
6917978b8a1SAntti Palosaari if (dev->chip_id == M88RS6000_CHIP_ID) {
692f4df95bcSnibble.max if (c->symbol_rate > 45010000)
693f5d9b88dSAntti Palosaari dev->mclk = 110250000;
694f4df95bcSnibble.max else
695f5d9b88dSAntti Palosaari dev->mclk = 96000000;
696395d00d1SAntti Palosaari
697f4df95bcSnibble.max if (c->delivery_system == SYS_DVBS)
698f5d9b88dSAntti Palosaari target_mclk = 96000000;
699f4df95bcSnibble.max else
700f5d9b88dSAntti Palosaari target_mclk = 144000000;
701395d00d1SAntti Palosaari
702e6089fecSBrad Love if (dev->chiptype == M88DS3103_CHIPTYPE_3103B) {
703e6089fecSBrad Love m88ds3103b_select_mclk(dev);
704e6089fecSBrad Love m88ds3103b_set_mclk(dev, target_mclk / 1000);
705e6089fecSBrad Love }
706e6089fecSBrad Love
707f4df95bcSnibble.max /* Enable demod clock path */
708478932b1SAntti Palosaari ret = regmap_write(dev->regmap, 0x06, 0x00);
709f4df95bcSnibble.max if (ret)
710f4df95bcSnibble.max goto err;
711f4df95bcSnibble.max usleep_range(10000, 20000);
712f4df95bcSnibble.max } else {
713f4df95bcSnibble.max /* set M88DS3103 mclk and ts mclk. */
714f5d9b88dSAntti Palosaari dev->mclk = 96000000;
715f4df95bcSnibble.max
7167978b8a1SAntti Palosaari switch (dev->cfg->ts_mode) {
717395d00d1SAntti Palosaari case M88DS3103_TS_SERIAL:
718395d00d1SAntti Palosaari case M88DS3103_TS_SERIAL_D7:
7197978b8a1SAntti Palosaari target_mclk = dev->cfg->ts_clk;
720395d00d1SAntti Palosaari break;
721395d00d1SAntti Palosaari case M88DS3103_TS_PARALLEL:
722395d00d1SAntti Palosaari case M88DS3103_TS_CI:
723b6851419Snibble.max if (c->delivery_system == SYS_DVBS)
724f5d9b88dSAntti Palosaari target_mclk = 96000000;
725b6851419Snibble.max else {
726395d00d1SAntti Palosaari if (c->symbol_rate < 18000000)
727f5d9b88dSAntti Palosaari target_mclk = 96000000;
728395d00d1SAntti Palosaari else if (c->symbol_rate < 28000000)
729f5d9b88dSAntti Palosaari target_mclk = 144000000;
730395d00d1SAntti Palosaari else
731f5d9b88dSAntti Palosaari target_mclk = 192000000;
732b6851419Snibble.max }
733395d00d1SAntti Palosaari break;
734395d00d1SAntti Palosaari default:
7357978b8a1SAntti Palosaari dev_dbg(&client->dev, "invalid ts_mode\n");
736395d00d1SAntti Palosaari ret = -EINVAL;
737395d00d1SAntti Palosaari goto err;
738395d00d1SAntti Palosaari }
739f4df95bcSnibble.max
740f4df95bcSnibble.max switch (target_mclk) {
741f5d9b88dSAntti Palosaari case 96000000:
742f4df95bcSnibble.max u8tmp1 = 0x02; /* 0b10 */
743f4df95bcSnibble.max u8tmp2 = 0x01; /* 0b01 */
744f4df95bcSnibble.max break;
745f5d9b88dSAntti Palosaari case 144000000:
746f4df95bcSnibble.max u8tmp1 = 0x00; /* 0b00 */
747f4df95bcSnibble.max u8tmp2 = 0x01; /* 0b01 */
748f4df95bcSnibble.max break;
749f5d9b88dSAntti Palosaari case 192000000:
750f4df95bcSnibble.max u8tmp1 = 0x03; /* 0b11 */
751f4df95bcSnibble.max u8tmp2 = 0x00; /* 0b00 */
752f4df95bcSnibble.max break;
753f4df95bcSnibble.max }
75456ea37daSAntti Palosaari ret = m88ds3103_update_bits(dev, 0x22, 0xc0, u8tmp1 << 6);
755f4df95bcSnibble.max if (ret)
756f4df95bcSnibble.max goto err;
75756ea37daSAntti Palosaari ret = m88ds3103_update_bits(dev, 0x24, 0xc0, u8tmp2 << 6);
758f4df95bcSnibble.max if (ret)
759f4df95bcSnibble.max goto err;
760f4df95bcSnibble.max }
761f4df95bcSnibble.max
762478932b1SAntti Palosaari ret = regmap_write(dev->regmap, 0xb2, 0x01);
763f4df95bcSnibble.max if (ret)
764f4df95bcSnibble.max goto err;
765f4df95bcSnibble.max
766478932b1SAntti Palosaari ret = regmap_write(dev->regmap, 0x00, 0x01);
767f4df95bcSnibble.max if (ret)
768f4df95bcSnibble.max goto err;
769f4df95bcSnibble.max
770f4df95bcSnibble.max switch (c->delivery_system) {
771f4df95bcSnibble.max case SYS_DVBS:
7727978b8a1SAntti Palosaari if (dev->chip_id == M88RS6000_CHIP_ID) {
773f4df95bcSnibble.max len = ARRAY_SIZE(m88rs6000_dvbs_init_reg_vals);
774f4df95bcSnibble.max init = m88rs6000_dvbs_init_reg_vals;
775f4df95bcSnibble.max } else {
776f4df95bcSnibble.max len = ARRAY_SIZE(m88ds3103_dvbs_init_reg_vals);
777f4df95bcSnibble.max init = m88ds3103_dvbs_init_reg_vals;
778f4df95bcSnibble.max }
779f4df95bcSnibble.max break;
780f4df95bcSnibble.max case SYS_DVBS2:
7817978b8a1SAntti Palosaari if (dev->chip_id == M88RS6000_CHIP_ID) {
782f4df95bcSnibble.max len = ARRAY_SIZE(m88rs6000_dvbs2_init_reg_vals);
783f4df95bcSnibble.max init = m88rs6000_dvbs2_init_reg_vals;
784f4df95bcSnibble.max } else {
785f4df95bcSnibble.max len = ARRAY_SIZE(m88ds3103_dvbs2_init_reg_vals);
786f4df95bcSnibble.max init = m88ds3103_dvbs2_init_reg_vals;
787f4df95bcSnibble.max }
788395d00d1SAntti Palosaari break;
789395d00d1SAntti Palosaari default:
7907978b8a1SAntti Palosaari dev_dbg(&client->dev, "invalid delivery_system\n");
791395d00d1SAntti Palosaari ret = -EINVAL;
792395d00d1SAntti Palosaari goto err;
793395d00d1SAntti Palosaari }
794395d00d1SAntti Palosaari
795395d00d1SAntti Palosaari /* program init table */
7967978b8a1SAntti Palosaari if (c->delivery_system != dev->delivery_system) {
7977978b8a1SAntti Palosaari ret = m88ds3103_wr_reg_val_tab(dev, init, len);
798395d00d1SAntti Palosaari if (ret)
799395d00d1SAntti Palosaari goto err;
800395d00d1SAntti Palosaari }
801395d00d1SAntti Palosaari
8027978b8a1SAntti Palosaari if (dev->chip_id == M88RS6000_CHIP_ID) {
803f5d9b88dSAntti Palosaari if (c->delivery_system == SYS_DVBS2 &&
804f5d9b88dSAntti Palosaari c->symbol_rate <= 5000000) {
805478932b1SAntti Palosaari ret = regmap_write(dev->regmap, 0xc0, 0x04);
806f4df95bcSnibble.max if (ret)
807f4df95bcSnibble.max goto err;
808f4df95bcSnibble.max buf[0] = 0x09;
809f4df95bcSnibble.max buf[1] = 0x22;
810f4df95bcSnibble.max buf[2] = 0x88;
811478932b1SAntti Palosaari ret = regmap_bulk_write(dev->regmap, 0x8a, buf, 3);
812f4df95bcSnibble.max if (ret)
813f4df95bcSnibble.max goto err;
814f4df95bcSnibble.max }
81556ea37daSAntti Palosaari ret = m88ds3103_update_bits(dev, 0x9d, 0x08, 0x08);
816f4df95bcSnibble.max if (ret)
817f4df95bcSnibble.max goto err;
818e6089fecSBrad Love
819e6089fecSBrad Love if (dev->chiptype == M88DS3103_CHIPTYPE_3103B) {
820e6089fecSBrad Love buf[0] = m88ds3103b_dt_read(dev, 0x15);
821e6089fecSBrad Love buf[1] = m88ds3103b_dt_read(dev, 0x16);
822e6089fecSBrad Love
823e6089fecSBrad Love if (c->symbol_rate > 45010000) {
824e6089fecSBrad Love buf[0] &= ~0x03;
825e6089fecSBrad Love buf[0] |= 0x02;
826e6089fecSBrad Love buf[0] |= ((147 - 32) >> 8) & 0x01;
827e6089fecSBrad Love buf[1] = (147 - 32) & 0xFF;
828e6089fecSBrad Love
829e6089fecSBrad Love dev->mclk = 110250 * 1000;
830e6089fecSBrad Love } else {
831e6089fecSBrad Love buf[0] &= ~0x03;
832e6089fecSBrad Love buf[0] |= ((128 - 32) >> 8) & 0x01;
833e6089fecSBrad Love buf[1] = (128 - 32) & 0xFF;
834e6089fecSBrad Love
835e6089fecSBrad Love dev->mclk = 96000 * 1000;
836e6089fecSBrad Love }
837e6089fecSBrad Love m88ds3103b_dt_write(dev, 0x15, buf[0]);
838e6089fecSBrad Love m88ds3103b_dt_write(dev, 0x16, buf[1]);
839e6089fecSBrad Love
840e6089fecSBrad Love regmap_read(dev->regmap, 0x30, &u32tmp);
841e6089fecSBrad Love u32tmp &= ~0x80;
842e6089fecSBrad Love regmap_write(dev->regmap, 0x30, u32tmp & 0xff);
843e6089fecSBrad Love }
844e6089fecSBrad Love
845478932b1SAntti Palosaari ret = regmap_write(dev->regmap, 0xf1, 0x01);
846f4df95bcSnibble.max if (ret)
847f4df95bcSnibble.max goto err;
848e6089fecSBrad Love
849e6089fecSBrad Love if (dev->chiptype != M88DS3103_CHIPTYPE_3103B) {
85056ea37daSAntti Palosaari ret = m88ds3103_update_bits(dev, 0x30, 0x80, 0x80);
851f4df95bcSnibble.max if (ret)
852f4df95bcSnibble.max goto err;
853f4df95bcSnibble.max }
854e6089fecSBrad Love }
855f4df95bcSnibble.max
8567978b8a1SAntti Palosaari switch (dev->cfg->ts_mode) {
857395d00d1SAntti Palosaari case M88DS3103_TS_SERIAL:
858395d00d1SAntti Palosaari u8tmp1 = 0x00;
85979d09330Snibble.max u8tmp = 0x06;
860395d00d1SAntti Palosaari break;
861395d00d1SAntti Palosaari case M88DS3103_TS_SERIAL_D7:
862395d00d1SAntti Palosaari u8tmp1 = 0x20;
86379d09330Snibble.max u8tmp = 0x06;
864395d00d1SAntti Palosaari break;
865395d00d1SAntti Palosaari case M88DS3103_TS_PARALLEL:
86679d09330Snibble.max u8tmp = 0x02;
867e6089fecSBrad Love if (dev->chiptype == M88DS3103_CHIPTYPE_3103B) {
868e6089fecSBrad Love u8tmp = 0x01;
869e6089fecSBrad Love u8tmp1 = 0x01;
870e6089fecSBrad Love }
871395d00d1SAntti Palosaari break;
872395d00d1SAntti Palosaari case M88DS3103_TS_CI:
87379d09330Snibble.max u8tmp = 0x03;
874395d00d1SAntti Palosaari break;
875395d00d1SAntti Palosaari default:
8767978b8a1SAntti Palosaari dev_dbg(&client->dev, "invalid ts_mode\n");
877395d00d1SAntti Palosaari ret = -EINVAL;
878395d00d1SAntti Palosaari goto err;
879395d00d1SAntti Palosaari }
880395d00d1SAntti Palosaari
8817978b8a1SAntti Palosaari if (dev->cfg->ts_clk_pol)
88279d09330Snibble.max u8tmp |= 0x40;
88379d09330Snibble.max
884395d00d1SAntti Palosaari /* TS mode */
885478932b1SAntti Palosaari ret = regmap_write(dev->regmap, 0xfd, u8tmp);
886395d00d1SAntti Palosaari if (ret)
887395d00d1SAntti Palosaari goto err;
888395d00d1SAntti Palosaari
8897978b8a1SAntti Palosaari switch (dev->cfg->ts_mode) {
890395d00d1SAntti Palosaari case M88DS3103_TS_SERIAL:
891395d00d1SAntti Palosaari case M88DS3103_TS_SERIAL_D7:
89256ea37daSAntti Palosaari ret = m88ds3103_update_bits(dev, 0x29, 0x20, u8tmp1);
893395d00d1SAntti Palosaari if (ret)
894395d00d1SAntti Palosaari goto err;
895334ef18eSAntti Palosaari u16tmp = 0;
896334ef18eSAntti Palosaari u8tmp1 = 0x3f;
897334ef18eSAntti Palosaari u8tmp2 = 0x3f;
898b6851419Snibble.max break;
899e6089fecSBrad Love case M88DS3103_TS_PARALLEL:
900e6089fecSBrad Love if (dev->chiptype == M88DS3103_CHIPTYPE_3103B) {
901e6089fecSBrad Love ret = m88ds3103_update_bits(dev, 0x29, 0x01, u8tmp1);
902e6089fecSBrad Love if (ret)
903e6089fecSBrad Love goto err;
904e6089fecSBrad Love }
9051771e9fbSGustavo A. R. Silva fallthrough;
906b6851419Snibble.max default:
907334ef18eSAntti Palosaari u16tmp = DIV_ROUND_UP(target_mclk, dev->cfg->ts_clk);
908334ef18eSAntti Palosaari u8tmp1 = u16tmp / 2 - 1;
909334ef18eSAntti Palosaari u8tmp2 = DIV_ROUND_UP(u16tmp, 2) - 1;
910395d00d1SAntti Palosaari }
911395d00d1SAntti Palosaari
912f5d9b88dSAntti Palosaari dev_dbg(&client->dev, "target_mclk=%u ts_clk=%u ts_clk_divide_ratio=%u\n",
913334ef18eSAntti Palosaari target_mclk, dev->cfg->ts_clk, u16tmp);
914395d00d1SAntti Palosaari
915395d00d1SAntti Palosaari /* u8tmp1[5:2] => fe[3:0], u8tmp1[1:0] => ea[7:6] */
916395d00d1SAntti Palosaari /* u8tmp2[5:0] => ea[5:0] */
917334ef18eSAntti Palosaari u8tmp = (u8tmp1 >> 2) & 0x0f;
918334ef18eSAntti Palosaari ret = regmap_update_bits(dev->regmap, 0xfe, 0x0f, u8tmp);
919395d00d1SAntti Palosaari if (ret)
920395d00d1SAntti Palosaari goto err;
921395d00d1SAntti Palosaari u8tmp = ((u8tmp1 & 0x03) << 6) | u8tmp2 >> 0;
922478932b1SAntti Palosaari ret = regmap_write(dev->regmap, 0xea, u8tmp);
923395d00d1SAntti Palosaari if (ret)
924395d00d1SAntti Palosaari goto err;
925395d00d1SAntti Palosaari
926395d00d1SAntti Palosaari if (c->symbol_rate <= 3000000)
927395d00d1SAntti Palosaari u8tmp = 0x20;
928395d00d1SAntti Palosaari else if (c->symbol_rate <= 10000000)
929395d00d1SAntti Palosaari u8tmp = 0x10;
930395d00d1SAntti Palosaari else
931395d00d1SAntti Palosaari u8tmp = 0x06;
932395d00d1SAntti Palosaari
933e6089fecSBrad Love if (dev->chiptype == M88DS3103_CHIPTYPE_3103B)
934e6089fecSBrad Love m88ds3103b_set_mclk(dev, target_mclk / 1000);
935e6089fecSBrad Love
936478932b1SAntti Palosaari ret = regmap_write(dev->regmap, 0xc3, 0x08);
937395d00d1SAntti Palosaari if (ret)
938395d00d1SAntti Palosaari goto err;
939395d00d1SAntti Palosaari
940478932b1SAntti Palosaari ret = regmap_write(dev->regmap, 0xc8, u8tmp);
941395d00d1SAntti Palosaari if (ret)
942395d00d1SAntti Palosaari goto err;
943395d00d1SAntti Palosaari
944478932b1SAntti Palosaari ret = regmap_write(dev->regmap, 0xc4, 0x08);
945395d00d1SAntti Palosaari if (ret)
946395d00d1SAntti Palosaari goto err;
947395d00d1SAntti Palosaari
948478932b1SAntti Palosaari ret = regmap_write(dev->regmap, 0xc7, 0x00);
949395d00d1SAntti Palosaari if (ret)
950395d00d1SAntti Palosaari goto err;
951395d00d1SAntti Palosaari
952f5d9b88dSAntti Palosaari u16tmp = DIV_ROUND_CLOSEST_ULL((u64)c->symbol_rate * 0x10000, dev->mclk);
953395d00d1SAntti Palosaari buf[0] = (u16tmp >> 0) & 0xff;
954395d00d1SAntti Palosaari buf[1] = (u16tmp >> 8) & 0xff;
955478932b1SAntti Palosaari ret = regmap_bulk_write(dev->regmap, 0x61, buf, 2);
956395d00d1SAntti Palosaari if (ret)
957395d00d1SAntti Palosaari goto err;
958395d00d1SAntti Palosaari
95956ea37daSAntti Palosaari ret = m88ds3103_update_bits(dev, 0x4d, 0x02, dev->cfg->spec_inv << 1);
960395d00d1SAntti Palosaari if (ret)
961395d00d1SAntti Palosaari goto err;
962395d00d1SAntti Palosaari
96356ea37daSAntti Palosaari ret = m88ds3103_update_bits(dev, 0x30, 0x10, dev->cfg->agc_inv << 4);
964395d00d1SAntti Palosaari if (ret)
965395d00d1SAntti Palosaari goto err;
966395d00d1SAntti Palosaari
967478932b1SAntti Palosaari ret = regmap_write(dev->regmap, 0x33, dev->cfg->agc);
968395d00d1SAntti Palosaari if (ret)
969395d00d1SAntti Palosaari goto err;
970395d00d1SAntti Palosaari
971e6089fecSBrad Love if (dev->chiptype == M88DS3103_CHIPTYPE_3103B) {
972e6089fecSBrad Love /* enable/disable 192M LDPC clock */
973e6089fecSBrad Love ret = m88ds3103_update_bits(dev, 0x29, 0x10,
974e6089fecSBrad Love (c->delivery_system == SYS_DVBS) ? 0x10 : 0x0);
975e6089fecSBrad Love if (ret)
976e6089fecSBrad Love goto err;
977e6089fecSBrad Love
978e6089fecSBrad Love ret = m88ds3103_update_bits(dev, 0xc9, 0x08, 0x08);
979c4ed27cfSSean Young if (ret)
980c4ed27cfSSean Young goto err;
981e6089fecSBrad Love }
982e6089fecSBrad Love
9837978b8a1SAntti Palosaari dev_dbg(&client->dev, "carrier offset=%d\n",
984f5d9b88dSAntti Palosaari (tuner_frequency_khz - c->frequency));
985395d00d1SAntti Palosaari
986f5d9b88dSAntti Palosaari /* Use 32-bit calc as there is no s64 version of DIV_ROUND_CLOSEST() */
987f5d9b88dSAntti Palosaari s32tmp = 0x10000 * (tuner_frequency_khz - c->frequency);
988f5d9b88dSAntti Palosaari s32tmp = DIV_ROUND_CLOSEST(s32tmp, dev->mclk / 1000);
989395d00d1SAntti Palosaari buf[0] = (s32tmp >> 0) & 0xff;
990395d00d1SAntti Palosaari buf[1] = (s32tmp >> 8) & 0xff;
991478932b1SAntti Palosaari ret = regmap_bulk_write(dev->regmap, 0x5e, buf, 2);
992395d00d1SAntti Palosaari if (ret)
993395d00d1SAntti Palosaari goto err;
994395d00d1SAntti Palosaari
995478932b1SAntti Palosaari ret = regmap_write(dev->regmap, 0x00, 0x00);
996395d00d1SAntti Palosaari if (ret)
997395d00d1SAntti Palosaari goto err;
998395d00d1SAntti Palosaari
999478932b1SAntti Palosaari ret = regmap_write(dev->regmap, 0xb2, 0x00);
1000395d00d1SAntti Palosaari if (ret)
1001395d00d1SAntti Palosaari goto err;
1002395d00d1SAntti Palosaari
10037978b8a1SAntti Palosaari dev->delivery_system = c->delivery_system;
1004395d00d1SAntti Palosaari
1005395d00d1SAntti Palosaari return 0;
1006395d00d1SAntti Palosaari err:
10077978b8a1SAntti Palosaari dev_dbg(&client->dev, "failed=%d\n", ret);
1008395d00d1SAntti Palosaari return ret;
1009395d00d1SAntti Palosaari }
1010395d00d1SAntti Palosaari
m88ds3103_init(struct dvb_frontend * fe)1011395d00d1SAntti Palosaari static int m88ds3103_init(struct dvb_frontend *fe)
1012395d00d1SAntti Palosaari {
10137978b8a1SAntti Palosaari struct m88ds3103_dev *dev = fe->demodulator_priv;
10147978b8a1SAntti Palosaari struct i2c_client *client = dev->client;
1015c1daf651SAntti Palosaari struct dtv_frontend_properties *c = &fe->dtv_property_cache;
101660701d5fSAntti Palosaari int ret, len, rem;
1017478932b1SAntti Palosaari unsigned int utmp;
101860701d5fSAntti Palosaari const struct firmware *firmware;
101960701d5fSAntti Palosaari const char *name;
102041b9aa00SAntti Palosaari
10217978b8a1SAntti Palosaari dev_dbg(&client->dev, "\n");
1022395d00d1SAntti Palosaari
1023395d00d1SAntti Palosaari /* set cold state by default */
10247978b8a1SAntti Palosaari dev->warm = false;
1025395d00d1SAntti Palosaari
1026395d00d1SAntti Palosaari /* wake up device from sleep */
102756ea37daSAntti Palosaari ret = m88ds3103_update_bits(dev, 0x08, 0x01, 0x01);
1028395d00d1SAntti Palosaari if (ret)
1029395d00d1SAntti Palosaari goto err;
103056ea37daSAntti Palosaari ret = m88ds3103_update_bits(dev, 0x04, 0x01, 0x00);
1031395d00d1SAntti Palosaari if (ret)
1032395d00d1SAntti Palosaari goto err;
103356ea37daSAntti Palosaari ret = m88ds3103_update_bits(dev, 0x23, 0x10, 0x00);
1034395d00d1SAntti Palosaari if (ret)
1035395d00d1SAntti Palosaari goto err;
1036395d00d1SAntti Palosaari
1037395d00d1SAntti Palosaari /* firmware status */
1038478932b1SAntti Palosaari ret = regmap_read(dev->regmap, 0xb9, &utmp);
1039395d00d1SAntti Palosaari if (ret)
1040395d00d1SAntti Palosaari goto err;
1041395d00d1SAntti Palosaari
1042478932b1SAntti Palosaari dev_dbg(&client->dev, "firmware=%02x\n", utmp);
1043395d00d1SAntti Palosaari
1044478932b1SAntti Palosaari if (utmp)
104560701d5fSAntti Palosaari goto warm;
1046395d00d1SAntti Palosaari
1047e6089fecSBrad Love /* global reset, global diseqc reset, global fec reset */
1048478932b1SAntti Palosaari ret = regmap_write(dev->regmap, 0x07, 0xe0);
1049f4df95bcSnibble.max if (ret)
1050f4df95bcSnibble.max goto err;
1051478932b1SAntti Palosaari ret = regmap_write(dev->regmap, 0x07, 0x00);
1052f4df95bcSnibble.max if (ret)
1053f4df95bcSnibble.max goto err;
1054f4df95bcSnibble.max
1055395d00d1SAntti Palosaari /* cold state - try to download firmware */
10567978b8a1SAntti Palosaari dev_info(&client->dev, "found a '%s' in cold state\n",
1057e6089fecSBrad Love dev->fe.ops.info.name);
1058395d00d1SAntti Palosaari
1059e6089fecSBrad Love if (dev->chiptype == M88DS3103_CHIPTYPE_3103B)
1060e6089fecSBrad Love name = M88DS3103B_FIRMWARE;
1061e6089fecSBrad Love else if (dev->chip_id == M88RS6000_CHIP_ID)
106260701d5fSAntti Palosaari name = M88RS6000_FIRMWARE;
1063f4df95bcSnibble.max else
106460701d5fSAntti Palosaari name = M88DS3103_FIRMWARE;
1065e6089fecSBrad Love
1066395d00d1SAntti Palosaari /* request the firmware, this will block and timeout */
106760701d5fSAntti Palosaari ret = request_firmware(&firmware, name, &client->dev);
1068395d00d1SAntti Palosaari if (ret) {
106960701d5fSAntti Palosaari dev_err(&client->dev, "firmware file '%s' not found\n", name);
1070395d00d1SAntti Palosaari goto err;
1071395d00d1SAntti Palosaari }
1072395d00d1SAntti Palosaari
107360701d5fSAntti Palosaari dev_info(&client->dev, "downloading firmware from file '%s'\n", name);
1074395d00d1SAntti Palosaari
1075478932b1SAntti Palosaari ret = regmap_write(dev->regmap, 0xb2, 0x01);
1076395d00d1SAntti Palosaari if (ret)
107760701d5fSAntti Palosaari goto err_release_firmware;
1078395d00d1SAntti Palosaari
107960701d5fSAntti Palosaari for (rem = firmware->size; rem > 0; rem -= (dev->cfg->i2c_wr_max - 1)) {
108060701d5fSAntti Palosaari len = min(dev->cfg->i2c_wr_max - 1, rem);
1081478932b1SAntti Palosaari ret = regmap_bulk_write(dev->regmap, 0xb0,
108260701d5fSAntti Palosaari &firmware->data[firmware->size - rem],
108360701d5fSAntti Palosaari len);
1084395d00d1SAntti Palosaari if (ret) {
108560701d5fSAntti Palosaari dev_err(&client->dev, "firmware download failed %d\n",
10867978b8a1SAntti Palosaari ret);
108760701d5fSAntti Palosaari goto err_release_firmware;
1088395d00d1SAntti Palosaari }
1089395d00d1SAntti Palosaari }
1090395d00d1SAntti Palosaari
1091478932b1SAntti Palosaari ret = regmap_write(dev->regmap, 0xb2, 0x00);
1092395d00d1SAntti Palosaari if (ret)
109360701d5fSAntti Palosaari goto err_release_firmware;
1094395d00d1SAntti Palosaari
109560701d5fSAntti Palosaari release_firmware(firmware);
1096395d00d1SAntti Palosaari
1097478932b1SAntti Palosaari ret = regmap_read(dev->regmap, 0xb9, &utmp);
1098395d00d1SAntti Palosaari if (ret)
1099395d00d1SAntti Palosaari goto err;
1100395d00d1SAntti Palosaari
1101478932b1SAntti Palosaari if (!utmp) {
110260701d5fSAntti Palosaari ret = -EINVAL;
11037978b8a1SAntti Palosaari dev_info(&client->dev, "firmware did not run\n");
1104395d00d1SAntti Palosaari goto err;
1105395d00d1SAntti Palosaari }
1106395d00d1SAntti Palosaari
11077978b8a1SAntti Palosaari dev_info(&client->dev, "found a '%s' in warm state\n",
1108e6089fecSBrad Love dev->fe.ops.info.name);
11097978b8a1SAntti Palosaari dev_info(&client->dev, "firmware version: %X.%X\n",
1110478932b1SAntti Palosaari (utmp >> 4) & 0xf, (utmp >> 0 & 0xf));
1111395d00d1SAntti Palosaari
1112e6089fecSBrad Love if (dev->chiptype == M88DS3103_CHIPTYPE_3103B) {
1113e6089fecSBrad Love m88ds3103b_dt_write(dev, 0x21, 0x92);
1114e6089fecSBrad Love m88ds3103b_dt_write(dev, 0x15, 0x6C);
1115e6089fecSBrad Love m88ds3103b_dt_write(dev, 0x17, 0xC1);
1116e6089fecSBrad Love m88ds3103b_dt_write(dev, 0x17, 0x81);
1117e6089fecSBrad Love }
111860701d5fSAntti Palosaari warm:
1119395d00d1SAntti Palosaari /* warm state */
11207978b8a1SAntti Palosaari dev->warm = true;
11217978b8a1SAntti Palosaari
1122c1daf651SAntti Palosaari /* init stats here in order signal app which stats are supported */
1123c1daf651SAntti Palosaari c->cnr.len = 1;
1124c1daf651SAntti Palosaari c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
1125ce80d713SAntti Palosaari c->post_bit_error.len = 1;
1126ce80d713SAntti Palosaari c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
1127ce80d713SAntti Palosaari c->post_bit_count.len = 1;
1128ce80d713SAntti Palosaari c->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
1129395d00d1SAntti Palosaari
11307978b8a1SAntti Palosaari return 0;
113160701d5fSAntti Palosaari err_release_firmware:
113260701d5fSAntti Palosaari release_firmware(firmware);
11335ed0cf88SMarkus Elfring err:
11347978b8a1SAntti Palosaari dev_dbg(&client->dev, "failed=%d\n", ret);
1135395d00d1SAntti Palosaari return ret;
1136395d00d1SAntti Palosaari }
1137395d00d1SAntti Palosaari
m88ds3103_sleep(struct dvb_frontend * fe)1138395d00d1SAntti Palosaari static int m88ds3103_sleep(struct dvb_frontend *fe)
1139395d00d1SAntti Palosaari {
11407978b8a1SAntti Palosaari struct m88ds3103_dev *dev = fe->demodulator_priv;
11417978b8a1SAntti Palosaari struct i2c_client *client = dev->client;
1142395d00d1SAntti Palosaari int ret;
1143478932b1SAntti Palosaari unsigned int utmp;
114441b9aa00SAntti Palosaari
11457978b8a1SAntti Palosaari dev_dbg(&client->dev, "\n");
1146395d00d1SAntti Palosaari
11477978b8a1SAntti Palosaari dev->fe_status = 0;
11487978b8a1SAntti Palosaari dev->delivery_system = SYS_UNDEFINED;
1149395d00d1SAntti Palosaari
1150395d00d1SAntti Palosaari /* TS Hi-Z */
11517978b8a1SAntti Palosaari if (dev->chip_id == M88RS6000_CHIP_ID)
1152478932b1SAntti Palosaari utmp = 0x29;
1153f4df95bcSnibble.max else
1154478932b1SAntti Palosaari utmp = 0x27;
115556ea37daSAntti Palosaari ret = m88ds3103_update_bits(dev, utmp, 0x01, 0x00);
1156395d00d1SAntti Palosaari if (ret)
1157395d00d1SAntti Palosaari goto err;
1158395d00d1SAntti Palosaari
1159395d00d1SAntti Palosaari /* sleep */
116056ea37daSAntti Palosaari ret = m88ds3103_update_bits(dev, 0x08, 0x01, 0x00);
1161395d00d1SAntti Palosaari if (ret)
1162395d00d1SAntti Palosaari goto err;
116356ea37daSAntti Palosaari ret = m88ds3103_update_bits(dev, 0x04, 0x01, 0x01);
1164395d00d1SAntti Palosaari if (ret)
1165395d00d1SAntti Palosaari goto err;
116656ea37daSAntti Palosaari ret = m88ds3103_update_bits(dev, 0x23, 0x10, 0x10);
1167395d00d1SAntti Palosaari if (ret)
1168395d00d1SAntti Palosaari goto err;
1169395d00d1SAntti Palosaari
1170395d00d1SAntti Palosaari return 0;
1171395d00d1SAntti Palosaari err:
11727978b8a1SAntti Palosaari dev_dbg(&client->dev, "failed=%d\n", ret);
1173395d00d1SAntti Palosaari return ret;
1174395d00d1SAntti Palosaari }
1175395d00d1SAntti Palosaari
m88ds3103_get_frontend(struct dvb_frontend * fe,struct dtv_frontend_properties * c)11767e3e68bcSMauro Carvalho Chehab static int m88ds3103_get_frontend(struct dvb_frontend *fe,
11777e3e68bcSMauro Carvalho Chehab struct dtv_frontend_properties *c)
1178395d00d1SAntti Palosaari {
11797978b8a1SAntti Palosaari struct m88ds3103_dev *dev = fe->demodulator_priv;
11807978b8a1SAntti Palosaari struct i2c_client *client = dev->client;
1181395d00d1SAntti Palosaari int ret;
1182395d00d1SAntti Palosaari u8 buf[3];
118341b9aa00SAntti Palosaari
11847978b8a1SAntti Palosaari dev_dbg(&client->dev, "\n");
1185395d00d1SAntti Palosaari
11867978b8a1SAntti Palosaari if (!dev->warm || !(dev->fe_status & FE_HAS_LOCK)) {
11879240c384SAntti Palosaari ret = 0;
1188395d00d1SAntti Palosaari goto err;
1189395d00d1SAntti Palosaari }
1190395d00d1SAntti Palosaari
1191395d00d1SAntti Palosaari switch (c->delivery_system) {
1192395d00d1SAntti Palosaari case SYS_DVBS:
1193478932b1SAntti Palosaari ret = regmap_bulk_read(dev->regmap, 0xe0, &buf[0], 1);
1194395d00d1SAntti Palosaari if (ret)
1195395d00d1SAntti Palosaari goto err;
1196395d00d1SAntti Palosaari
1197478932b1SAntti Palosaari ret = regmap_bulk_read(dev->regmap, 0xe6, &buf[1], 1);
1198395d00d1SAntti Palosaari if (ret)
1199395d00d1SAntti Palosaari goto err;
1200395d00d1SAntti Palosaari
1201395d00d1SAntti Palosaari switch ((buf[0] >> 2) & 0x01) {
1202395d00d1SAntti Palosaari case 0:
1203395d00d1SAntti Palosaari c->inversion = INVERSION_OFF;
1204395d00d1SAntti Palosaari break;
1205395d00d1SAntti Palosaari case 1:
1206395d00d1SAntti Palosaari c->inversion = INVERSION_ON;
1207395d00d1SAntti Palosaari break;
1208395d00d1SAntti Palosaari }
1209395d00d1SAntti Palosaari
1210395d00d1SAntti Palosaari switch ((buf[1] >> 5) & 0x07) {
1211395d00d1SAntti Palosaari case 0:
1212395d00d1SAntti Palosaari c->fec_inner = FEC_7_8;
1213395d00d1SAntti Palosaari break;
1214395d00d1SAntti Palosaari case 1:
1215395d00d1SAntti Palosaari c->fec_inner = FEC_5_6;
1216395d00d1SAntti Palosaari break;
1217395d00d1SAntti Palosaari case 2:
1218395d00d1SAntti Palosaari c->fec_inner = FEC_3_4;
1219395d00d1SAntti Palosaari break;
1220395d00d1SAntti Palosaari case 3:
1221395d00d1SAntti Palosaari c->fec_inner = FEC_2_3;
1222395d00d1SAntti Palosaari break;
1223395d00d1SAntti Palosaari case 4:
1224395d00d1SAntti Palosaari c->fec_inner = FEC_1_2;
1225395d00d1SAntti Palosaari break;
1226395d00d1SAntti Palosaari default:
12277978b8a1SAntti Palosaari dev_dbg(&client->dev, "invalid fec_inner\n");
1228395d00d1SAntti Palosaari }
1229395d00d1SAntti Palosaari
1230395d00d1SAntti Palosaari c->modulation = QPSK;
1231395d00d1SAntti Palosaari
1232395d00d1SAntti Palosaari break;
1233395d00d1SAntti Palosaari case SYS_DVBS2:
1234478932b1SAntti Palosaari ret = regmap_bulk_read(dev->regmap, 0x7e, &buf[0], 1);
1235395d00d1SAntti Palosaari if (ret)
1236395d00d1SAntti Palosaari goto err;
1237395d00d1SAntti Palosaari
1238478932b1SAntti Palosaari ret = regmap_bulk_read(dev->regmap, 0x89, &buf[1], 1);
1239395d00d1SAntti Palosaari if (ret)
1240395d00d1SAntti Palosaari goto err;
1241395d00d1SAntti Palosaari
1242478932b1SAntti Palosaari ret = regmap_bulk_read(dev->regmap, 0xf2, &buf[2], 1);
1243395d00d1SAntti Palosaari if (ret)
1244395d00d1SAntti Palosaari goto err;
1245395d00d1SAntti Palosaari
1246395d00d1SAntti Palosaari switch ((buf[0] >> 0) & 0x0f) {
1247395d00d1SAntti Palosaari case 2:
1248395d00d1SAntti Palosaari c->fec_inner = FEC_2_5;
1249395d00d1SAntti Palosaari break;
1250395d00d1SAntti Palosaari case 3:
1251395d00d1SAntti Palosaari c->fec_inner = FEC_1_2;
1252395d00d1SAntti Palosaari break;
1253395d00d1SAntti Palosaari case 4:
1254395d00d1SAntti Palosaari c->fec_inner = FEC_3_5;
1255395d00d1SAntti Palosaari break;
1256395d00d1SAntti Palosaari case 5:
1257395d00d1SAntti Palosaari c->fec_inner = FEC_2_3;
1258395d00d1SAntti Palosaari break;
1259395d00d1SAntti Palosaari case 6:
1260395d00d1SAntti Palosaari c->fec_inner = FEC_3_4;
1261395d00d1SAntti Palosaari break;
1262395d00d1SAntti Palosaari case 7:
1263395d00d1SAntti Palosaari c->fec_inner = FEC_4_5;
1264395d00d1SAntti Palosaari break;
1265395d00d1SAntti Palosaari case 8:
1266395d00d1SAntti Palosaari c->fec_inner = FEC_5_6;
1267395d00d1SAntti Palosaari break;
1268395d00d1SAntti Palosaari case 9:
1269395d00d1SAntti Palosaari c->fec_inner = FEC_8_9;
1270395d00d1SAntti Palosaari break;
1271395d00d1SAntti Palosaari case 10:
1272395d00d1SAntti Palosaari c->fec_inner = FEC_9_10;
1273395d00d1SAntti Palosaari break;
1274395d00d1SAntti Palosaari default:
12757978b8a1SAntti Palosaari dev_dbg(&client->dev, "invalid fec_inner\n");
1276395d00d1SAntti Palosaari }
1277395d00d1SAntti Palosaari
1278395d00d1SAntti Palosaari switch ((buf[0] >> 5) & 0x01) {
1279395d00d1SAntti Palosaari case 0:
1280395d00d1SAntti Palosaari c->pilot = PILOT_OFF;
1281395d00d1SAntti Palosaari break;
1282395d00d1SAntti Palosaari case 1:
1283395d00d1SAntti Palosaari c->pilot = PILOT_ON;
1284395d00d1SAntti Palosaari break;
1285395d00d1SAntti Palosaari }
1286395d00d1SAntti Palosaari
1287395d00d1SAntti Palosaari switch ((buf[0] >> 6) & 0x07) {
1288395d00d1SAntti Palosaari case 0:
1289395d00d1SAntti Palosaari c->modulation = QPSK;
1290395d00d1SAntti Palosaari break;
1291395d00d1SAntti Palosaari case 1:
1292395d00d1SAntti Palosaari c->modulation = PSK_8;
1293395d00d1SAntti Palosaari break;
1294395d00d1SAntti Palosaari case 2:
1295395d00d1SAntti Palosaari c->modulation = APSK_16;
1296395d00d1SAntti Palosaari break;
1297395d00d1SAntti Palosaari case 3:
1298395d00d1SAntti Palosaari c->modulation = APSK_32;
1299395d00d1SAntti Palosaari break;
1300395d00d1SAntti Palosaari default:
13017978b8a1SAntti Palosaari dev_dbg(&client->dev, "invalid modulation\n");
1302395d00d1SAntti Palosaari }
1303395d00d1SAntti Palosaari
1304395d00d1SAntti Palosaari switch ((buf[1] >> 7) & 0x01) {
1305395d00d1SAntti Palosaari case 0:
1306395d00d1SAntti Palosaari c->inversion = INVERSION_OFF;
1307395d00d1SAntti Palosaari break;
1308395d00d1SAntti Palosaari case 1:
1309395d00d1SAntti Palosaari c->inversion = INVERSION_ON;
1310395d00d1SAntti Palosaari break;
1311395d00d1SAntti Palosaari }
1312395d00d1SAntti Palosaari
1313395d00d1SAntti Palosaari switch ((buf[2] >> 0) & 0x03) {
1314395d00d1SAntti Palosaari case 0:
1315395d00d1SAntti Palosaari c->rolloff = ROLLOFF_35;
1316395d00d1SAntti Palosaari break;
1317395d00d1SAntti Palosaari case 1:
1318395d00d1SAntti Palosaari c->rolloff = ROLLOFF_25;
1319395d00d1SAntti Palosaari break;
1320395d00d1SAntti Palosaari case 2:
1321395d00d1SAntti Palosaari c->rolloff = ROLLOFF_20;
1322395d00d1SAntti Palosaari break;
1323395d00d1SAntti Palosaari default:
13247978b8a1SAntti Palosaari dev_dbg(&client->dev, "invalid rolloff\n");
1325395d00d1SAntti Palosaari }
1326395d00d1SAntti Palosaari break;
1327395d00d1SAntti Palosaari default:
13287978b8a1SAntti Palosaari dev_dbg(&client->dev, "invalid delivery_system\n");
1329395d00d1SAntti Palosaari ret = -EINVAL;
1330395d00d1SAntti Palosaari goto err;
1331395d00d1SAntti Palosaari }
1332395d00d1SAntti Palosaari
1333478932b1SAntti Palosaari ret = regmap_bulk_read(dev->regmap, 0x6d, buf, 2);
1334395d00d1SAntti Palosaari if (ret)
1335395d00d1SAntti Palosaari goto err;
1336395d00d1SAntti Palosaari
1337f5d9b88dSAntti Palosaari c->symbol_rate = DIV_ROUND_CLOSEST_ULL((u64)(buf[1] << 8 | buf[0] << 0) * dev->mclk, 0x10000);
1338395d00d1SAntti Palosaari
1339395d00d1SAntti Palosaari return 0;
1340395d00d1SAntti Palosaari err:
13417978b8a1SAntti Palosaari dev_dbg(&client->dev, "failed=%d\n", ret);
1342395d00d1SAntti Palosaari return ret;
1343395d00d1SAntti Palosaari }
1344395d00d1SAntti Palosaari
m88ds3103_read_snr(struct dvb_frontend * fe,u16 * snr)1345395d00d1SAntti Palosaari static int m88ds3103_read_snr(struct dvb_frontend *fe, u16 *snr)
1346395d00d1SAntti Palosaari {
1347395d00d1SAntti Palosaari struct dtv_frontend_properties *c = &fe->dtv_property_cache;
134841b9aa00SAntti Palosaari
1349c1daf651SAntti Palosaari if (c->cnr.stat[0].scale == FE_SCALE_DECIBEL)
1350c1daf651SAntti Palosaari *snr = div_s64(c->cnr.stat[0].svalue, 100);
1351395d00d1SAntti Palosaari else
1352395d00d1SAntti Palosaari *snr = 0;
1353395d00d1SAntti Palosaari
1354395d00d1SAntti Palosaari return 0;
1355395d00d1SAntti Palosaari }
1356395d00d1SAntti Palosaari
m88ds3103_read_ber(struct dvb_frontend * fe,u32 * ber)13574423a2baSAntti Palosaari static int m88ds3103_read_ber(struct dvb_frontend *fe, u32 *ber)
13584423a2baSAntti Palosaari {
13597978b8a1SAntti Palosaari struct m88ds3103_dev *dev = fe->demodulator_priv;
136041b9aa00SAntti Palosaari
13617978b8a1SAntti Palosaari *ber = dev->dvbv3_ber;
13624423a2baSAntti Palosaari
13634423a2baSAntti Palosaari return 0;
13644423a2baSAntti Palosaari }
1365395d00d1SAntti Palosaari
m88ds3103_set_tone(struct dvb_frontend * fe,enum fe_sec_tone_mode fe_sec_tone_mode)1366395d00d1SAntti Palosaari static int m88ds3103_set_tone(struct dvb_frontend *fe,
13670df289a2SMauro Carvalho Chehab enum fe_sec_tone_mode fe_sec_tone_mode)
1368395d00d1SAntti Palosaari {
13697978b8a1SAntti Palosaari struct m88ds3103_dev *dev = fe->demodulator_priv;
13707978b8a1SAntti Palosaari struct i2c_client *client = dev->client;
1371395d00d1SAntti Palosaari int ret;
1372478932b1SAntti Palosaari unsigned int utmp, tone, reg_a1_mask;
137341b9aa00SAntti Palosaari
13747978b8a1SAntti Palosaari dev_dbg(&client->dev, "fe_sec_tone_mode=%d\n", fe_sec_tone_mode);
1375395d00d1SAntti Palosaari
13767978b8a1SAntti Palosaari if (!dev->warm) {
1377395d00d1SAntti Palosaari ret = -EAGAIN;
1378395d00d1SAntti Palosaari goto err;
1379395d00d1SAntti Palosaari }
1380395d00d1SAntti Palosaari
1381395d00d1SAntti Palosaari switch (fe_sec_tone_mode) {
1382395d00d1SAntti Palosaari case SEC_TONE_ON:
1383395d00d1SAntti Palosaari tone = 0;
1384418a97cbSAntti Palosaari reg_a1_mask = 0x47;
1385395d00d1SAntti Palosaari break;
1386395d00d1SAntti Palosaari case SEC_TONE_OFF:
1387395d00d1SAntti Palosaari tone = 1;
1388395d00d1SAntti Palosaari reg_a1_mask = 0x00;
1389395d00d1SAntti Palosaari break;
1390395d00d1SAntti Palosaari default:
13917978b8a1SAntti Palosaari dev_dbg(&client->dev, "invalid fe_sec_tone_mode\n");
1392395d00d1SAntti Palosaari ret = -EINVAL;
1393395d00d1SAntti Palosaari goto err;
1394395d00d1SAntti Palosaari }
1395395d00d1SAntti Palosaari
1396478932b1SAntti Palosaari utmp = tone << 7 | dev->cfg->envelope_mode << 5;
139756ea37daSAntti Palosaari ret = m88ds3103_update_bits(dev, 0xa2, 0xe0, utmp);
1398395d00d1SAntti Palosaari if (ret)
1399395d00d1SAntti Palosaari goto err;
1400395d00d1SAntti Palosaari
1401478932b1SAntti Palosaari utmp = 1 << 2;
140256ea37daSAntti Palosaari ret = m88ds3103_update_bits(dev, 0xa1, reg_a1_mask, utmp);
1403395d00d1SAntti Palosaari if (ret)
1404395d00d1SAntti Palosaari goto err;
1405395d00d1SAntti Palosaari
1406395d00d1SAntti Palosaari return 0;
1407395d00d1SAntti Palosaari err:
14087978b8a1SAntti Palosaari dev_dbg(&client->dev, "failed=%d\n", ret);
1409395d00d1SAntti Palosaari return ret;
1410395d00d1SAntti Palosaari }
1411395d00d1SAntti Palosaari
m88ds3103_set_voltage(struct dvb_frontend * fe,enum fe_sec_voltage fe_sec_voltage)141279d09330Snibble.max static int m88ds3103_set_voltage(struct dvb_frontend *fe,
14130df289a2SMauro Carvalho Chehab enum fe_sec_voltage fe_sec_voltage)
141479d09330Snibble.max {
14157978b8a1SAntti Palosaari struct m88ds3103_dev *dev = fe->demodulator_priv;
14167978b8a1SAntti Palosaari struct i2c_client *client = dev->client;
1417d28677ffSAntti Palosaari int ret;
1418478932b1SAntti Palosaari unsigned int utmp;
1419d28677ffSAntti Palosaari bool voltage_sel, voltage_dis;
142079d09330Snibble.max
14217978b8a1SAntti Palosaari dev_dbg(&client->dev, "fe_sec_voltage=%d\n", fe_sec_voltage);
142279d09330Snibble.max
14237978b8a1SAntti Palosaari if (!dev->warm) {
1424d28677ffSAntti Palosaari ret = -EAGAIN;
1425d28677ffSAntti Palosaari goto err;
1426d28677ffSAntti Palosaari }
142779d09330Snibble.max
1428d28677ffSAntti Palosaari switch (fe_sec_voltage) {
142979d09330Snibble.max case SEC_VOLTAGE_18:
1430afbd6eb4SMauro Carvalho Chehab voltage_sel = true;
1431afbd6eb4SMauro Carvalho Chehab voltage_dis = false;
143279d09330Snibble.max break;
143379d09330Snibble.max case SEC_VOLTAGE_13:
1434afbd6eb4SMauro Carvalho Chehab voltage_sel = false;
1435afbd6eb4SMauro Carvalho Chehab voltage_dis = false;
143679d09330Snibble.max break;
143779d09330Snibble.max case SEC_VOLTAGE_OFF:
1438afbd6eb4SMauro Carvalho Chehab voltage_sel = false;
1439afbd6eb4SMauro Carvalho Chehab voltage_dis = true;
144079d09330Snibble.max break;
1441d28677ffSAntti Palosaari default:
14427978b8a1SAntti Palosaari dev_dbg(&client->dev, "invalid fe_sec_voltage\n");
1443d28677ffSAntti Palosaari ret = -EINVAL;
1444d28677ffSAntti Palosaari goto err;
144579d09330Snibble.max }
1446d28677ffSAntti Palosaari
1447d28677ffSAntti Palosaari /* output pin polarity */
14487978b8a1SAntti Palosaari voltage_sel ^= dev->cfg->lnb_hv_pol;
14497978b8a1SAntti Palosaari voltage_dis ^= dev->cfg->lnb_en_pol;
1450d28677ffSAntti Palosaari
1451478932b1SAntti Palosaari utmp = voltage_dis << 1 | voltage_sel << 0;
145256ea37daSAntti Palosaari ret = m88ds3103_update_bits(dev, 0xa2, 0x03, utmp);
1453d28677ffSAntti Palosaari if (ret)
1454d28677ffSAntti Palosaari goto err;
145579d09330Snibble.max
145679d09330Snibble.max return 0;
1457d28677ffSAntti Palosaari err:
14587978b8a1SAntti Palosaari dev_dbg(&client->dev, "failed=%d\n", ret);
1459d28677ffSAntti Palosaari return ret;
146079d09330Snibble.max }
146179d09330Snibble.max
m88ds3103_diseqc_send_master_cmd(struct dvb_frontend * fe,struct dvb_diseqc_master_cmd * diseqc_cmd)1462395d00d1SAntti Palosaari static int m88ds3103_diseqc_send_master_cmd(struct dvb_frontend *fe,
1463395d00d1SAntti Palosaari struct dvb_diseqc_master_cmd *diseqc_cmd)
1464395d00d1SAntti Palosaari {
14657978b8a1SAntti Palosaari struct m88ds3103_dev *dev = fe->demodulator_priv;
14667978b8a1SAntti Palosaari struct i2c_client *client = dev->client;
1467befa0cc1SAntti Palosaari int ret;
1468478932b1SAntti Palosaari unsigned int utmp;
1469befa0cc1SAntti Palosaari unsigned long timeout;
147041b9aa00SAntti Palosaari
14717978b8a1SAntti Palosaari dev_dbg(&client->dev, "msg=%*ph\n",
1472395d00d1SAntti Palosaari diseqc_cmd->msg_len, diseqc_cmd->msg);
1473395d00d1SAntti Palosaari
14747978b8a1SAntti Palosaari if (!dev->warm) {
1475395d00d1SAntti Palosaari ret = -EAGAIN;
1476395d00d1SAntti Palosaari goto err;
1477395d00d1SAntti Palosaari }
1478395d00d1SAntti Palosaari
1479395d00d1SAntti Palosaari if (diseqc_cmd->msg_len < 3 || diseqc_cmd->msg_len > 6) {
1480395d00d1SAntti Palosaari ret = -EINVAL;
1481395d00d1SAntti Palosaari goto err;
1482395d00d1SAntti Palosaari }
1483395d00d1SAntti Palosaari
1484478932b1SAntti Palosaari utmp = dev->cfg->envelope_mode << 5;
148556ea37daSAntti Palosaari ret = m88ds3103_update_bits(dev, 0xa2, 0xe0, utmp);
1486395d00d1SAntti Palosaari if (ret)
1487395d00d1SAntti Palosaari goto err;
1488395d00d1SAntti Palosaari
1489478932b1SAntti Palosaari ret = regmap_bulk_write(dev->regmap, 0xa3, diseqc_cmd->msg,
1490395d00d1SAntti Palosaari diseqc_cmd->msg_len);
1491395d00d1SAntti Palosaari if (ret)
1492395d00d1SAntti Palosaari goto err;
1493395d00d1SAntti Palosaari
1494478932b1SAntti Palosaari ret = regmap_write(dev->regmap, 0xa1,
1495395d00d1SAntti Palosaari (diseqc_cmd->msg_len - 1) << 3 | 0x07);
1496395d00d1SAntti Palosaari if (ret)
1497395d00d1SAntti Palosaari goto err;
1498395d00d1SAntti Palosaari
1499395d00d1SAntti Palosaari /* wait DiSEqC TX ready */
1500befa0cc1SAntti Palosaari #define SEND_MASTER_CMD_TIMEOUT 120
1501befa0cc1SAntti Palosaari timeout = jiffies + msecs_to_jiffies(SEND_MASTER_CMD_TIMEOUT);
1502395d00d1SAntti Palosaari
15039ef3cdc1SAntti Palosaari /* DiSEqC message period is 13.5 ms per byte */
15049ef3cdc1SAntti Palosaari utmp = diseqc_cmd->msg_len * 13500;
15059ef3cdc1SAntti Palosaari usleep_range(utmp - 4000, utmp);
1506befa0cc1SAntti Palosaari
1507478932b1SAntti Palosaari for (utmp = 1; !time_after(jiffies, timeout) && utmp;) {
1508478932b1SAntti Palosaari ret = regmap_read(dev->regmap, 0xa1, &utmp);
1509395d00d1SAntti Palosaari if (ret)
1510395d00d1SAntti Palosaari goto err;
1511478932b1SAntti Palosaari utmp = (utmp >> 6) & 0x1;
1512395d00d1SAntti Palosaari }
1513395d00d1SAntti Palosaari
1514478932b1SAntti Palosaari if (utmp == 0) {
15157978b8a1SAntti Palosaari dev_dbg(&client->dev, "diseqc tx took %u ms\n",
1516befa0cc1SAntti Palosaari jiffies_to_msecs(jiffies) -
1517befa0cc1SAntti Palosaari (jiffies_to_msecs(timeout) - SEND_MASTER_CMD_TIMEOUT));
1518befa0cc1SAntti Palosaari } else {
15197978b8a1SAntti Palosaari dev_dbg(&client->dev, "diseqc tx timeout\n");
1520395d00d1SAntti Palosaari
152156ea37daSAntti Palosaari ret = m88ds3103_update_bits(dev, 0xa1, 0xc0, 0x40);
1522395d00d1SAntti Palosaari if (ret)
1523395d00d1SAntti Palosaari goto err;
1524395d00d1SAntti Palosaari }
1525395d00d1SAntti Palosaari
152656ea37daSAntti Palosaari ret = m88ds3103_update_bits(dev, 0xa2, 0xc0, 0x80);
1527395d00d1SAntti Palosaari if (ret)
1528395d00d1SAntti Palosaari goto err;
1529395d00d1SAntti Palosaari
1530478932b1SAntti Palosaari if (utmp == 1) {
1531395d00d1SAntti Palosaari ret = -ETIMEDOUT;
1532395d00d1SAntti Palosaari goto err;
1533395d00d1SAntti Palosaari }
1534395d00d1SAntti Palosaari
1535395d00d1SAntti Palosaari return 0;
1536395d00d1SAntti Palosaari err:
15377978b8a1SAntti Palosaari dev_dbg(&client->dev, "failed=%d\n", ret);
1538395d00d1SAntti Palosaari return ret;
1539395d00d1SAntti Palosaari }
1540395d00d1SAntti Palosaari
m88ds3103_diseqc_send_burst(struct dvb_frontend * fe,enum fe_sec_mini_cmd fe_sec_mini_cmd)1541395d00d1SAntti Palosaari static int m88ds3103_diseqc_send_burst(struct dvb_frontend *fe,
15420df289a2SMauro Carvalho Chehab enum fe_sec_mini_cmd fe_sec_mini_cmd)
1543395d00d1SAntti Palosaari {
15447978b8a1SAntti Palosaari struct m88ds3103_dev *dev = fe->demodulator_priv;
15457978b8a1SAntti Palosaari struct i2c_client *client = dev->client;
1546befa0cc1SAntti Palosaari int ret;
1547478932b1SAntti Palosaari unsigned int utmp, burst;
1548befa0cc1SAntti Palosaari unsigned long timeout;
154941b9aa00SAntti Palosaari
15507978b8a1SAntti Palosaari dev_dbg(&client->dev, "fe_sec_mini_cmd=%d\n", fe_sec_mini_cmd);
1551395d00d1SAntti Palosaari
15527978b8a1SAntti Palosaari if (!dev->warm) {
1553395d00d1SAntti Palosaari ret = -EAGAIN;
1554395d00d1SAntti Palosaari goto err;
1555395d00d1SAntti Palosaari }
1556395d00d1SAntti Palosaari
1557478932b1SAntti Palosaari utmp = dev->cfg->envelope_mode << 5;
155856ea37daSAntti Palosaari ret = m88ds3103_update_bits(dev, 0xa2, 0xe0, utmp);
1559395d00d1SAntti Palosaari if (ret)
1560395d00d1SAntti Palosaari goto err;
1561395d00d1SAntti Palosaari
1562395d00d1SAntti Palosaari switch (fe_sec_mini_cmd) {
1563395d00d1SAntti Palosaari case SEC_MINI_A:
1564395d00d1SAntti Palosaari burst = 0x02;
1565395d00d1SAntti Palosaari break;
1566395d00d1SAntti Palosaari case SEC_MINI_B:
1567395d00d1SAntti Palosaari burst = 0x01;
1568395d00d1SAntti Palosaari break;
1569395d00d1SAntti Palosaari default:
15707978b8a1SAntti Palosaari dev_dbg(&client->dev, "invalid fe_sec_mini_cmd\n");
1571395d00d1SAntti Palosaari ret = -EINVAL;
1572395d00d1SAntti Palosaari goto err;
1573395d00d1SAntti Palosaari }
1574395d00d1SAntti Palosaari
1575478932b1SAntti Palosaari ret = regmap_write(dev->regmap, 0xa1, burst);
1576395d00d1SAntti Palosaari if (ret)
1577395d00d1SAntti Palosaari goto err;
1578395d00d1SAntti Palosaari
1579395d00d1SAntti Palosaari /* wait DiSEqC TX ready */
1580befa0cc1SAntti Palosaari #define SEND_BURST_TIMEOUT 40
1581befa0cc1SAntti Palosaari timeout = jiffies + msecs_to_jiffies(SEND_BURST_TIMEOUT);
1582395d00d1SAntti Palosaari
1583befa0cc1SAntti Palosaari /* DiSEqC ToneBurst period is 12.5 ms */
1584befa0cc1SAntti Palosaari usleep_range(8500, 12500);
1585befa0cc1SAntti Palosaari
1586478932b1SAntti Palosaari for (utmp = 1; !time_after(jiffies, timeout) && utmp;) {
1587478932b1SAntti Palosaari ret = regmap_read(dev->regmap, 0xa1, &utmp);
1588395d00d1SAntti Palosaari if (ret)
1589395d00d1SAntti Palosaari goto err;
1590478932b1SAntti Palosaari utmp = (utmp >> 6) & 0x1;
1591395d00d1SAntti Palosaari }
1592395d00d1SAntti Palosaari
1593478932b1SAntti Palosaari if (utmp == 0) {
15947978b8a1SAntti Palosaari dev_dbg(&client->dev, "diseqc tx took %u ms\n",
1595befa0cc1SAntti Palosaari jiffies_to_msecs(jiffies) -
1596befa0cc1SAntti Palosaari (jiffies_to_msecs(timeout) - SEND_BURST_TIMEOUT));
1597befa0cc1SAntti Palosaari } else {
15987978b8a1SAntti Palosaari dev_dbg(&client->dev, "diseqc tx timeout\n");
1599befa0cc1SAntti Palosaari
160056ea37daSAntti Palosaari ret = m88ds3103_update_bits(dev, 0xa1, 0xc0, 0x40);
1601befa0cc1SAntti Palosaari if (ret)
1602befa0cc1SAntti Palosaari goto err;
1603befa0cc1SAntti Palosaari }
1604395d00d1SAntti Palosaari
160556ea37daSAntti Palosaari ret = m88ds3103_update_bits(dev, 0xa2, 0xc0, 0x80);
1606395d00d1SAntti Palosaari if (ret)
1607395d00d1SAntti Palosaari goto err;
1608395d00d1SAntti Palosaari
1609478932b1SAntti Palosaari if (utmp == 1) {
1610395d00d1SAntti Palosaari ret = -ETIMEDOUT;
1611395d00d1SAntti Palosaari goto err;
1612395d00d1SAntti Palosaari }
1613395d00d1SAntti Palosaari
1614395d00d1SAntti Palosaari return 0;
1615395d00d1SAntti Palosaari err:
16167978b8a1SAntti Palosaari dev_dbg(&client->dev, "failed=%d\n", ret);
1617395d00d1SAntti Palosaari return ret;
1618395d00d1SAntti Palosaari }
1619395d00d1SAntti Palosaari
m88ds3103_get_tune_settings(struct dvb_frontend * fe,struct dvb_frontend_tune_settings * s)1620395d00d1SAntti Palosaari static int m88ds3103_get_tune_settings(struct dvb_frontend *fe,
1621395d00d1SAntti Palosaari struct dvb_frontend_tune_settings *s)
1622395d00d1SAntti Palosaari {
1623395d00d1SAntti Palosaari s->min_delay_ms = 3000;
1624395d00d1SAntti Palosaari
1625395d00d1SAntti Palosaari return 0;
1626395d00d1SAntti Palosaari }
1627395d00d1SAntti Palosaari
m88ds3103_release(struct dvb_frontend * fe)162844b9055bSAntti Palosaari static void m88ds3103_release(struct dvb_frontend *fe)
1629395d00d1SAntti Palosaari {
16307978b8a1SAntti Palosaari struct m88ds3103_dev *dev = fe->demodulator_priv;
16317978b8a1SAntti Palosaari struct i2c_client *client = dev->client;
163241b9aa00SAntti Palosaari
1633f01919e8SAntti Palosaari i2c_unregister_device(client);
1634395d00d1SAntti Palosaari }
1635395d00d1SAntti Palosaari
m88ds3103_select(struct i2c_mux_core * muxc,u32 chan)1636e00fed40SPeter Rosin static int m88ds3103_select(struct i2c_mux_core *muxc, u32 chan)
1637395d00d1SAntti Palosaari {
1638e00fed40SPeter Rosin struct m88ds3103_dev *dev = i2c_mux_priv(muxc);
16397978b8a1SAntti Palosaari struct i2c_client *client = dev->client;
1640395d00d1SAntti Palosaari int ret;
1641478932b1SAntti Palosaari struct i2c_msg msg = {
16427978b8a1SAntti Palosaari .addr = client->addr,
1643395d00d1SAntti Palosaari .flags = 0,
1644395d00d1SAntti Palosaari .len = 2,
1645395d00d1SAntti Palosaari .buf = "\x03\x11",
1646395d00d1SAntti Palosaari };
1647395d00d1SAntti Palosaari
1648478932b1SAntti Palosaari /* Open tuner I2C repeater for 1 xfer, closes automatically */
1649478932b1SAntti Palosaari ret = __i2c_transfer(client->adapter, &msg, 1);
1650395d00d1SAntti Palosaari if (ret != 1) {
16517978b8a1SAntti Palosaari dev_warn(&client->dev, "i2c wr failed=%d\n", ret);
165244b9055bSAntti Palosaari if (ret >= 0)
1653395d00d1SAntti Palosaari ret = -EREMOTEIO;
1654395d00d1SAntti Palosaari return ret;
1655395d00d1SAntti Palosaari }
1656395d00d1SAntti Palosaari
165744b9055bSAntti Palosaari return 0;
165844b9055bSAntti Palosaari }
1659395d00d1SAntti Palosaari
1660f01919e8SAntti Palosaari /*
1661f01919e8SAntti Palosaari * XXX: That is wrapper to m88ds3103_probe() via driver core in order to provide
1662f01919e8SAntti Palosaari * proper I2C client for legacy media attach binding.
1663f01919e8SAntti Palosaari * New users must use I2C client binding directly!
1664f01919e8SAntti Palosaari */
m88ds3103_attach(const struct m88ds3103_config * cfg,struct i2c_adapter * i2c,struct i2c_adapter ** tuner_i2c_adapter)1665395d00d1SAntti Palosaari struct dvb_frontend *m88ds3103_attach(const struct m88ds3103_config *cfg,
1666b9c97c67SMauro Carvalho Chehab struct i2c_adapter *i2c,
1667b9c97c67SMauro Carvalho Chehab struct i2c_adapter **tuner_i2c_adapter)
1668395d00d1SAntti Palosaari {
1669f01919e8SAntti Palosaari struct i2c_client *client;
1670f01919e8SAntti Palosaari struct i2c_board_info board_info;
1671b9c97c67SMauro Carvalho Chehab struct m88ds3103_platform_data pdata = {};
1672395d00d1SAntti Palosaari
1673f01919e8SAntti Palosaari pdata.clk = cfg->clock;
1674f01919e8SAntti Palosaari pdata.i2c_wr_max = cfg->i2c_wr_max;
1675f01919e8SAntti Palosaari pdata.ts_mode = cfg->ts_mode;
1676f01919e8SAntti Palosaari pdata.ts_clk = cfg->ts_clk;
1677f01919e8SAntti Palosaari pdata.ts_clk_pol = cfg->ts_clk_pol;
1678f01919e8SAntti Palosaari pdata.spec_inv = cfg->spec_inv;
1679f01919e8SAntti Palosaari pdata.agc = cfg->agc;
1680f01919e8SAntti Palosaari pdata.agc_inv = cfg->agc_inv;
1681f01919e8SAntti Palosaari pdata.clk_out = cfg->clock_out;
1682f01919e8SAntti Palosaari pdata.envelope_mode = cfg->envelope_mode;
1683f01919e8SAntti Palosaari pdata.lnb_hv_pol = cfg->lnb_hv_pol;
1684f01919e8SAntti Palosaari pdata.lnb_en_pol = cfg->lnb_en_pol;
1685f01919e8SAntti Palosaari pdata.attach_in_use = true;
1686395d00d1SAntti Palosaari
1687f01919e8SAntti Palosaari memset(&board_info, 0, sizeof(board_info));
1688c0decac1SMauro Carvalho Chehab strscpy(board_info.type, "m88ds3103", I2C_NAME_SIZE);
1689f01919e8SAntti Palosaari board_info.addr = cfg->i2c_addr;
1690f01919e8SAntti Palosaari board_info.platform_data = &pdata;
1691aace5926SWolfram Sang client = i2c_new_client_device(i2c, &board_info);
1692aace5926SWolfram Sang if (!i2c_client_has_driver(client))
1693395d00d1SAntti Palosaari return NULL;
1694f01919e8SAntti Palosaari
1695f01919e8SAntti Palosaari *tuner_i2c_adapter = pdata.get_i2c_adapter(client);
1696f01919e8SAntti Palosaari return pdata.get_dvb_frontend(client);
1697395d00d1SAntti Palosaari }
169886495af1SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(m88ds3103_attach);
1699395d00d1SAntti Palosaari
1700bd336e63SMax Kellermann static const struct dvb_frontend_ops m88ds3103_ops = {
1701395d00d1SAntti Palosaari .delsys = {SYS_DVBS, SYS_DVBS2},
1702395d00d1SAntti Palosaari .info = {
17037978b8a1SAntti Palosaari .name = "Montage Technology M88DS3103",
1704f1b1eabfSMauro Carvalho Chehab .frequency_min_hz = 950 * MHz,
1705f1b1eabfSMauro Carvalho Chehab .frequency_max_hz = 2150 * MHz,
1706f1b1eabfSMauro Carvalho Chehab .frequency_tolerance_hz = 5 * MHz,
1707395d00d1SAntti Palosaari .symbol_rate_min = 1000000,
1708395d00d1SAntti Palosaari .symbol_rate_max = 45000000,
1709395d00d1SAntti Palosaari .caps = FE_CAN_INVERSION_AUTO |
1710395d00d1SAntti Palosaari FE_CAN_FEC_1_2 |
1711395d00d1SAntti Palosaari FE_CAN_FEC_2_3 |
1712395d00d1SAntti Palosaari FE_CAN_FEC_3_4 |
1713395d00d1SAntti Palosaari FE_CAN_FEC_4_5 |
1714395d00d1SAntti Palosaari FE_CAN_FEC_5_6 |
1715395d00d1SAntti Palosaari FE_CAN_FEC_6_7 |
1716395d00d1SAntti Palosaari FE_CAN_FEC_7_8 |
1717395d00d1SAntti Palosaari FE_CAN_FEC_8_9 |
1718395d00d1SAntti Palosaari FE_CAN_FEC_AUTO |
1719395d00d1SAntti Palosaari FE_CAN_QPSK |
1720395d00d1SAntti Palosaari FE_CAN_RECOVER |
1721395d00d1SAntti Palosaari FE_CAN_2G_MODULATION
1722395d00d1SAntti Palosaari },
1723395d00d1SAntti Palosaari
1724395d00d1SAntti Palosaari .release = m88ds3103_release,
1725395d00d1SAntti Palosaari
1726395d00d1SAntti Palosaari .get_tune_settings = m88ds3103_get_tune_settings,
1727395d00d1SAntti Palosaari
1728395d00d1SAntti Palosaari .init = m88ds3103_init,
1729395d00d1SAntti Palosaari .sleep = m88ds3103_sleep,
1730395d00d1SAntti Palosaari
1731395d00d1SAntti Palosaari .set_frontend = m88ds3103_set_frontend,
1732395d00d1SAntti Palosaari .get_frontend = m88ds3103_get_frontend,
1733395d00d1SAntti Palosaari
1734395d00d1SAntti Palosaari .read_status = m88ds3103_read_status,
1735395d00d1SAntti Palosaari .read_snr = m88ds3103_read_snr,
17364423a2baSAntti Palosaari .read_ber = m88ds3103_read_ber,
1737395d00d1SAntti Palosaari
1738395d00d1SAntti Palosaari .diseqc_send_master_cmd = m88ds3103_diseqc_send_master_cmd,
1739395d00d1SAntti Palosaari .diseqc_send_burst = m88ds3103_diseqc_send_burst,
1740395d00d1SAntti Palosaari
1741395d00d1SAntti Palosaari .set_tone = m88ds3103_set_tone,
174279d09330Snibble.max .set_voltage = m88ds3103_set_voltage,
1743395d00d1SAntti Palosaari };
1744395d00d1SAntti Palosaari
m88ds3103_get_dvb_frontend(struct i2c_client * client)1745f01919e8SAntti Palosaari static struct dvb_frontend *m88ds3103_get_dvb_frontend(struct i2c_client *client)
1746f01919e8SAntti Palosaari {
17477978b8a1SAntti Palosaari struct m88ds3103_dev *dev = i2c_get_clientdata(client);
1748f01919e8SAntti Palosaari
1749f01919e8SAntti Palosaari dev_dbg(&client->dev, "\n");
1750f01919e8SAntti Palosaari
1751f01919e8SAntti Palosaari return &dev->fe;
1752f01919e8SAntti Palosaari }
1753f01919e8SAntti Palosaari
m88ds3103_get_i2c_adapter(struct i2c_client * client)1754f01919e8SAntti Palosaari static struct i2c_adapter *m88ds3103_get_i2c_adapter(struct i2c_client *client)
1755f01919e8SAntti Palosaari {
17567978b8a1SAntti Palosaari struct m88ds3103_dev *dev = i2c_get_clientdata(client);
1757f01919e8SAntti Palosaari
1758f01919e8SAntti Palosaari dev_dbg(&client->dev, "\n");
1759f01919e8SAntti Palosaari
1760e00fed40SPeter Rosin return dev->muxc->adapter[0];
1761f01919e8SAntti Palosaari }
1762f01919e8SAntti Palosaari
m88ds3103_probe(struct i2c_client * client)176373f3cb66SUwe Kleine-König static int m88ds3103_probe(struct i2c_client *client)
1764f01919e8SAntti Palosaari {
176573f3cb66SUwe Kleine-König const struct i2c_device_id *id = i2c_client_get_device_id(client);
17667978b8a1SAntti Palosaari struct m88ds3103_dev *dev;
1767f01919e8SAntti Palosaari struct m88ds3103_platform_data *pdata = client->dev.platform_data;
1768f01919e8SAntti Palosaari int ret;
1769478932b1SAntti Palosaari unsigned int utmp;
1770f01919e8SAntti Palosaari
1771f01919e8SAntti Palosaari dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1772f01919e8SAntti Palosaari if (!dev) {
1773f01919e8SAntti Palosaari ret = -ENOMEM;
1774f01919e8SAntti Palosaari goto err;
1775f01919e8SAntti Palosaari }
1776f01919e8SAntti Palosaari
1777f01919e8SAntti Palosaari dev->client = client;
1778f01919e8SAntti Palosaari dev->config.clock = pdata->clk;
1779f01919e8SAntti Palosaari dev->config.i2c_wr_max = pdata->i2c_wr_max;
1780f01919e8SAntti Palosaari dev->config.ts_mode = pdata->ts_mode;
1781f5d9b88dSAntti Palosaari dev->config.ts_clk = pdata->ts_clk * 1000;
1782f01919e8SAntti Palosaari dev->config.ts_clk_pol = pdata->ts_clk_pol;
1783f01919e8SAntti Palosaari dev->config.spec_inv = pdata->spec_inv;
1784f01919e8SAntti Palosaari dev->config.agc_inv = pdata->agc_inv;
1785f01919e8SAntti Palosaari dev->config.clock_out = pdata->clk_out;
1786f01919e8SAntti Palosaari dev->config.envelope_mode = pdata->envelope_mode;
1787f01919e8SAntti Palosaari dev->config.agc = pdata->agc;
1788f01919e8SAntti Palosaari dev->config.lnb_hv_pol = pdata->lnb_hv_pol;
1789f01919e8SAntti Palosaari dev->config.lnb_en_pol = pdata->lnb_en_pol;
1790f01919e8SAntti Palosaari dev->cfg = &dev->config;
1791478932b1SAntti Palosaari /* create regmap */
17923a3c72d6SJulia Lawall dev->regmap_config.reg_bits = 8;
17933a3c72d6SJulia Lawall dev->regmap_config.val_bits = 8;
17943a3c72d6SJulia Lawall dev->regmap_config.lock_arg = dev;
1795478932b1SAntti Palosaari dev->regmap = devm_regmap_init_i2c(client, &dev->regmap_config);
1796478932b1SAntti Palosaari if (IS_ERR(dev->regmap)) {
1797478932b1SAntti Palosaari ret = PTR_ERR(dev->regmap);
1798478932b1SAntti Palosaari goto err_kfree;
1799478932b1SAntti Palosaari }
1800f01919e8SAntti Palosaari
1801f01919e8SAntti Palosaari /* 0x00: chip id[6:0], 0x01: chip ver[7:0], 0x02: chip ver[15:8] */
1802478932b1SAntti Palosaari ret = regmap_read(dev->regmap, 0x00, &utmp);
1803f01919e8SAntti Palosaari if (ret)
1804f01919e8SAntti Palosaari goto err_kfree;
1805f01919e8SAntti Palosaari
1806478932b1SAntti Palosaari dev->chip_id = utmp >> 1;
1807e6089fecSBrad Love dev->chiptype = (u8)id->driver_data;
1808e6089fecSBrad Love
1809478932b1SAntti Palosaari dev_dbg(&client->dev, "chip_id=%02x\n", dev->chip_id);
1810f01919e8SAntti Palosaari
1811478932b1SAntti Palosaari switch (dev->chip_id) {
1812f01919e8SAntti Palosaari case M88RS6000_CHIP_ID:
1813f01919e8SAntti Palosaari case M88DS3103_CHIP_ID:
1814f01919e8SAntti Palosaari break;
1815f01919e8SAntti Palosaari default:
1816b9c97c67SMauro Carvalho Chehab ret = -ENODEV;
1817b9c97c67SMauro Carvalho Chehab dev_err(&client->dev, "Unknown device. Chip_id=%02x\n", dev->chip_id);
1818f01919e8SAntti Palosaari goto err_kfree;
1819f01919e8SAntti Palosaari }
1820f01919e8SAntti Palosaari
1821f01919e8SAntti Palosaari switch (dev->cfg->clock_out) {
1822f01919e8SAntti Palosaari case M88DS3103_CLOCK_OUT_DISABLED:
1823478932b1SAntti Palosaari utmp = 0x80;
1824f01919e8SAntti Palosaari break;
1825f01919e8SAntti Palosaari case M88DS3103_CLOCK_OUT_ENABLED:
1826478932b1SAntti Palosaari utmp = 0x00;
1827f01919e8SAntti Palosaari break;
1828f01919e8SAntti Palosaari case M88DS3103_CLOCK_OUT_ENABLED_DIV2:
1829478932b1SAntti Palosaari utmp = 0x10;
1830f01919e8SAntti Palosaari break;
1831f01919e8SAntti Palosaari default:
18324347df6aSDan Carpenter ret = -EINVAL;
1833f01919e8SAntti Palosaari goto err_kfree;
1834f01919e8SAntti Palosaari }
1835f01919e8SAntti Palosaari
1836334ef18eSAntti Palosaari if (!pdata->ts_clk) {
1837334ef18eSAntti Palosaari ret = -EINVAL;
1838334ef18eSAntti Palosaari goto err_kfree;
1839334ef18eSAntti Palosaari }
1840334ef18eSAntti Palosaari
1841f01919e8SAntti Palosaari /* 0x29 register is defined differently for m88rs6000. */
1842f01919e8SAntti Palosaari /* set internal tuner address to 0x21 */
1843478932b1SAntti Palosaari if (dev->chip_id == M88RS6000_CHIP_ID)
1844478932b1SAntti Palosaari utmp = 0x00;
1845f01919e8SAntti Palosaari
1846478932b1SAntti Palosaari ret = regmap_write(dev->regmap, 0x29, utmp);
1847f01919e8SAntti Palosaari if (ret)
1848f01919e8SAntti Palosaari goto err_kfree;
1849f01919e8SAntti Palosaari
1850f01919e8SAntti Palosaari /* sleep */
185156ea37daSAntti Palosaari ret = m88ds3103_update_bits(dev, 0x08, 0x01, 0x00);
1852f01919e8SAntti Palosaari if (ret)
1853f01919e8SAntti Palosaari goto err_kfree;
185456ea37daSAntti Palosaari ret = m88ds3103_update_bits(dev, 0x04, 0x01, 0x01);
1855f01919e8SAntti Palosaari if (ret)
1856f01919e8SAntti Palosaari goto err_kfree;
185756ea37daSAntti Palosaari ret = m88ds3103_update_bits(dev, 0x23, 0x10, 0x10);
1858f01919e8SAntti Palosaari if (ret)
1859f01919e8SAntti Palosaari goto err_kfree;
1860f01919e8SAntti Palosaari
1861f01919e8SAntti Palosaari /* create mux i2c adapter for tuner */
1862e00fed40SPeter Rosin dev->muxc = i2c_mux_alloc(client->adapter, &client->dev, 1, 0, 0,
1863e00fed40SPeter Rosin m88ds3103_select, NULL);
1864e00fed40SPeter Rosin if (!dev->muxc) {
18654347df6aSDan Carpenter ret = -ENOMEM;
1866f01919e8SAntti Palosaari goto err_kfree;
18674347df6aSDan Carpenter }
1868e00fed40SPeter Rosin dev->muxc->priv = dev;
1869e00fed40SPeter Rosin ret = i2c_mux_add_adapter(dev->muxc, 0, 0, 0);
1870e00fed40SPeter Rosin if (ret)
1871e00fed40SPeter Rosin goto err_kfree;
1872f01919e8SAntti Palosaari
1873f01919e8SAntti Palosaari /* create dvb_frontend */
1874f01919e8SAntti Palosaari memcpy(&dev->fe.ops, &m88ds3103_ops, sizeof(struct dvb_frontend_ops));
1875e6089fecSBrad Love if (dev->chiptype == M88DS3103_CHIPTYPE_3103B)
1876e6089fecSBrad Love strscpy(dev->fe.ops.info.name, "Montage Technology M88DS3103B",
1877e6089fecSBrad Love sizeof(dev->fe.ops.info.name));
1878e6089fecSBrad Love else if (dev->chip_id == M88RS6000_CHIP_ID)
187985709cbfSMauro Carvalho Chehab strscpy(dev->fe.ops.info.name, "Montage Technology M88RS6000",
18807978b8a1SAntti Palosaari sizeof(dev->fe.ops.info.name));
1881f01919e8SAntti Palosaari if (!pdata->attach_in_use)
1882f01919e8SAntti Palosaari dev->fe.ops.release = NULL;
1883f01919e8SAntti Palosaari dev->fe.demodulator_priv = dev;
1884f01919e8SAntti Palosaari i2c_set_clientdata(client, dev);
1885f01919e8SAntti Palosaari
1886f01919e8SAntti Palosaari /* setup callbacks */
1887f01919e8SAntti Palosaari pdata->get_dvb_frontend = m88ds3103_get_dvb_frontend;
1888f01919e8SAntti Palosaari pdata->get_i2c_adapter = m88ds3103_get_i2c_adapter;
1889e6089fecSBrad Love
1890e6089fecSBrad Love if (dev->chiptype == M88DS3103_CHIPTYPE_3103B) {
1891e6089fecSBrad Love /* enable i2c repeater for tuner */
1892e6089fecSBrad Love m88ds3103_update_bits(dev, 0x11, 0x01, 0x01);
1893e6089fecSBrad Love
1894e6089fecSBrad Love /* get frontend address */
1895e6089fecSBrad Love ret = regmap_read(dev->regmap, 0x29, &utmp);
1896e6089fecSBrad Love if (ret)
1897*ee3e64d1SChristophe JAILLET goto err_del_adapters;
1898e6089fecSBrad Love dev->dt_addr = ((utmp & 0x80) == 0) ? 0x42 >> 1 : 0x40 >> 1;
1899c77591a8SChristophe JAILLET dev_dbg(&client->dev, "dt addr is 0x%02x\n", dev->dt_addr);
1900e6089fecSBrad Love
1901e6089fecSBrad Love dev->dt_client = i2c_new_dummy_device(client->adapter,
1902e6089fecSBrad Love dev->dt_addr);
1903e61f9ea2SWei Yongjun if (IS_ERR(dev->dt_client)) {
1904e61f9ea2SWei Yongjun ret = PTR_ERR(dev->dt_client);
1905*ee3e64d1SChristophe JAILLET goto err_del_adapters;
1906e6089fecSBrad Love }
1907e6089fecSBrad Love }
1908e6089fecSBrad Love
1909f01919e8SAntti Palosaari return 0;
1910*ee3e64d1SChristophe JAILLET
1911*ee3e64d1SChristophe JAILLET err_del_adapters:
1912*ee3e64d1SChristophe JAILLET i2c_mux_del_adapters(dev->muxc);
1913f01919e8SAntti Palosaari err_kfree:
1914f01919e8SAntti Palosaari kfree(dev);
1915f01919e8SAntti Palosaari err:
1916f01919e8SAntti Palosaari dev_dbg(&client->dev, "failed=%d\n", ret);
1917f01919e8SAntti Palosaari return ret;
1918f01919e8SAntti Palosaari }
1919f01919e8SAntti Palosaari
m88ds3103_remove(struct i2c_client * client)1920ed5c2f5fSUwe Kleine-König static void m88ds3103_remove(struct i2c_client *client)
1921f01919e8SAntti Palosaari {
19227978b8a1SAntti Palosaari struct m88ds3103_dev *dev = i2c_get_clientdata(client);
1923f01919e8SAntti Palosaari
1924f01919e8SAntti Palosaari dev_dbg(&client->dev, "\n");
1925f01919e8SAntti Palosaari
1926e6089fecSBrad Love if (dev->dt_client)
1927e6089fecSBrad Love i2c_unregister_device(dev->dt_client);
1928e6089fecSBrad Love
1929e00fed40SPeter Rosin i2c_mux_del_adapters(dev->muxc);
1930f01919e8SAntti Palosaari
1931f01919e8SAntti Palosaari kfree(dev);
1932f01919e8SAntti Palosaari }
1933f01919e8SAntti Palosaari
1934f01919e8SAntti Palosaari static const struct i2c_device_id m88ds3103_id_table[] = {
1935e6089fecSBrad Love {"m88ds3103", M88DS3103_CHIPTYPE_3103},
1936e6089fecSBrad Love {"m88rs6000", M88DS3103_CHIPTYPE_RS6000},
1937e6089fecSBrad 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 },
1947aaeb31c0SUwe Kleine-König .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);
1959e6089fecSBrad Love MODULE_FIRMWARE(M88DS3103B_FIRMWARE);
1960