1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Sony CXD2820R demodulator driver
5 * Copyright (C) 2010 Antti Palosaari <crope@iki.fi>
9 #include "cxd2820r_priv.h"
11 int cxd2820r_set_frontend_c(struct dvb_frontend *fe)
13 struct cxd2820r_priv *priv = fe->demodulator_priv;
14 struct i2c_client *client = priv->client[0];
15 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
20 struct reg_val_mask tab[] = {
21 { 0x00080, 0x01, 0xff },
22 { 0x00081, 0x05, 0xff },
23 { 0x00085, 0x07, 0xff },
24 { 0x00088, 0x01, 0xff },
26 { 0x00082, 0x20, 0x60 },
27 { 0x1016a, 0x48, 0xff },
28 { 0x100a5, 0x00, 0x01 },
29 { 0x10020, 0x06, 0x07 },
30 { 0x10059, 0x50, 0xff },
31 { 0x10087, 0x0c, 0x3c },
32 { 0x1008b, 0x07, 0xff },
33 { 0x1001f, priv->if_agc_polarity << 7, 0x80 },
34 { 0x10070, priv->ts_mode, 0xff },
35 { 0x10071, !priv->ts_clk_inv << 4, 0x10 },
39 "delivery_system=%d modulation=%d frequency=%u symbol_rate=%u inversion=%d\n",
40 c->delivery_system, c->modulation, c->frequency,
41 c->symbol_rate, c->inversion);
44 if (fe->ops.tuner_ops.set_params)
45 fe->ops.tuner_ops.set_params(fe);
47 if (priv->delivery_system != SYS_DVBC_ANNEX_A) {
48 ret = cxd2820r_wr_reg_val_mask_tab(priv, tab, ARRAY_SIZE(tab));
53 priv->delivery_system = SYS_DVBC_ANNEX_A;
54 priv->ber_running = false; /* tune stops BER counter */
56 /* program IF frequency */
57 if (fe->ops.tuner_ops.get_if_frequency) {
58 ret = fe->ops.tuner_ops.get_if_frequency(fe, &if_frequency);
61 dev_dbg(&client->dev, "if_frequency=%u\n", if_frequency);
67 utmp = 0x4000 - DIV_ROUND_CLOSEST_ULL((u64)if_frequency * 0x4000, CXD2820R_CLK);
68 buf[0] = (utmp >> 8) & 0xff;
69 buf[1] = (utmp >> 0) & 0xff;
70 ret = regmap_bulk_write(priv->regmap[1], 0x0042, buf, 2);
74 ret = regmap_write(priv->regmap[0], 0x00ff, 0x08);
78 ret = regmap_write(priv->regmap[0], 0x00fe, 0x01);
84 dev_dbg(&client->dev, "failed=%d\n", ret);
88 int cxd2820r_get_frontend_c(struct dvb_frontend *fe,
89 struct dtv_frontend_properties *c)
91 struct cxd2820r_priv *priv = fe->demodulator_priv;
92 struct i2c_client *client = priv->client[0];
97 dev_dbg(&client->dev, "\n");
99 ret = regmap_bulk_read(priv->regmap[1], 0x001a, buf, 2);
103 c->symbol_rate = 2500 * ((buf[0] & 0x0f) << 8 | buf[1]);
105 ret = regmap_read(priv->regmap[1], 0x0019, &utmp);
109 switch ((utmp >> 0) & 0x07) {
111 c->modulation = QAM_16;
114 c->modulation = QAM_32;
117 c->modulation = QAM_64;
120 c->modulation = QAM_128;
123 c->modulation = QAM_256;
127 switch ((utmp >> 7) & 0x01) {
129 c->inversion = INVERSION_OFF;
132 c->inversion = INVERSION_ON;
138 dev_dbg(&client->dev, "failed=%d\n", ret);
142 int cxd2820r_read_status_c(struct dvb_frontend *fe, enum fe_status *status)
144 struct cxd2820r_priv *priv = fe->demodulator_priv;
145 struct i2c_client *client = priv->client[0];
146 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
148 unsigned int utmp, utmp1, utmp2;
152 ret = regmap_bulk_read(priv->regmap[1], 0x0088, &buf[0], 1);
155 ret = regmap_bulk_read(priv->regmap[1], 0x0073, &buf[1], 1);
159 utmp1 = (buf[0] >> 0) & 0x01;
160 utmp2 = (buf[1] >> 3) & 0x01;
162 if (utmp1 == 1 && utmp2 == 1) {
163 *status = FE_HAS_SIGNAL | FE_HAS_CARRIER |
164 FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK;
165 } else if (utmp1 == 1 || utmp2 == 1) {
166 *status = FE_HAS_SIGNAL | FE_HAS_CARRIER |
167 FE_HAS_VITERBI | FE_HAS_SYNC;
172 dev_dbg(&client->dev, "status=%02x raw=%*ph sync=%u ts=%u\n",
173 *status, 2, buf, utmp1, utmp2);
175 /* Signal strength */
176 if (*status & FE_HAS_SIGNAL) {
177 unsigned int strength;
179 ret = regmap_bulk_read(priv->regmap[1], 0x0049, buf, 2);
183 utmp = buf[0] << 8 | buf[1] << 0;
184 utmp = 511 - sign_extend32(utmp, 9);
185 /* Scale value to 0x0000-0xffff */
186 strength = utmp << 6 | utmp >> 4;
189 c->strength.stat[0].scale = FE_SCALE_RELATIVE;
190 c->strength.stat[0].uvalue = strength;
193 c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
197 if (*status & FE_HAS_VITERBI) {
198 unsigned int cnr, const_a, const_b;
200 ret = regmap_read(priv->regmap[1], 0x0019, &utmp);
204 if (((utmp >> 0) & 0x03) % 2) {
212 ret = regmap_read(priv->regmap[1], 0x004d, &utmp);
216 #define CXD2820R_LOG2_E_24 24204406 /* log2(e) << 24 */
218 cnr = div_u64((u64)(intlog2(const_b) - intlog2(utmp))
219 * const_a, CXD2820R_LOG2_E_24);
224 c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
225 c->cnr.stat[0].svalue = cnr;
228 c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
232 if (*status & FE_HAS_SYNC) {
233 unsigned int post_bit_error;
236 if (priv->ber_running) {
237 ret = regmap_bulk_read(priv->regmap[1], 0x0076, buf, 3);
241 if ((buf[2] >> 7) & 0x01) {
242 post_bit_error = buf[2] << 16 | buf[1] << 8 |
244 post_bit_error &= 0x0fffff;
256 ret = regmap_write(priv->regmap[1], 0x0079, 0x01);
259 priv->ber_running = true;
262 priv->post_bit_error += post_bit_error;
264 c->post_bit_error.len = 1;
265 c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
266 c->post_bit_error.stat[0].uvalue = priv->post_bit_error;
268 c->post_bit_error.len = 1;
269 c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
274 dev_dbg(&client->dev, "failed=%d\n", ret);
278 int cxd2820r_init_c(struct dvb_frontend *fe)
280 struct cxd2820r_priv *priv = fe->demodulator_priv;
281 struct i2c_client *client = priv->client[0];
284 dev_dbg(&client->dev, "\n");
286 ret = regmap_write(priv->regmap[0], 0x0085, 0x07);
292 dev_dbg(&client->dev, "failed=%d\n", ret);
296 int cxd2820r_sleep_c(struct dvb_frontend *fe)
298 struct cxd2820r_priv *priv = fe->demodulator_priv;
299 struct i2c_client *client = priv->client[0];
301 static const struct reg_val_mask tab[] = {
302 { 0x000ff, 0x1f, 0xff },
303 { 0x00085, 0x00, 0xff },
304 { 0x00088, 0x01, 0xff },
305 { 0x00081, 0x00, 0xff },
306 { 0x00080, 0x00, 0xff },
309 dev_dbg(&client->dev, "\n");
311 priv->delivery_system = SYS_UNDEFINED;
313 ret = cxd2820r_wr_reg_val_mask_tab(priv, tab, ARRAY_SIZE(tab));
319 dev_dbg(&client->dev, "failed=%d\n", ret);
323 int cxd2820r_get_tune_settings_c(struct dvb_frontend *fe,
324 struct dvb_frontend_tune_settings *s)
326 s->min_delay_ms = 500;
327 s->step_size = 0; /* no zigzag */