blob: 6f7eedb4c00e322e46b2504651c1917e5f896d48 [file] [log] [blame]
Thomas Gleixner16216332019-05-19 15:51:31 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Antti Palosaari27cfc852011-04-07 16:27:43 -03002/*
3 * Sony CXD2820R demodulator driver
4 *
5 * Copyright (C) 2010 Antti Palosaari <crope@iki.fi>
Antti Palosaari27cfc852011-04-07 16:27:43 -03006 */
7
8
Steve Kerrison9ac51c52011-05-02 18:19:13 -03009#include "cxd2820r_priv.h"
10
Mauro Carvalho Chehabf311f682011-12-30 22:22:10 -030011int cxd2820r_set_frontend_c(struct dvb_frontend *fe)
Antti Palosaari27cfc852011-04-07 16:27:43 -030012{
13 struct cxd2820r_priv *priv = fe->demodulator_priv;
Antti Palosaaric98975f2016-08-09 22:00:37 -030014 struct i2c_client *client = priv->client[0];
Antti Palosaari27cfc852011-04-07 16:27:43 -030015 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Antti Palosaari43e2ea62016-08-13 13:19:05 -030016 int ret;
Antti Palosaarifcd09f62016-08-08 15:54:10 -030017 unsigned int utmp;
Antti Palosaari27cfc852011-04-07 16:27:43 -030018 u8 buf[2];
Antti Palosaarifcd09f62016-08-08 15:54:10 -030019 u32 if_frequency;
Antti Palosaari27cfc852011-04-07 16:27:43 -030020 struct reg_val_mask tab[] = {
21 { 0x00080, 0x01, 0xff },
22 { 0x00081, 0x05, 0xff },
23 { 0x00085, 0x07, 0xff },
24 { 0x00088, 0x01, 0xff },
25
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 },
Antti Palosaari07fdf7d2016-08-09 20:49:09 -030033 { 0x1001f, priv->if_agc_polarity << 7, 0x80 },
34 { 0x10070, priv->ts_mode, 0xff },
35 { 0x10071, !priv->ts_clk_inv << 4, 0x10 },
Antti Palosaari27cfc852011-04-07 16:27:43 -030036 };
37
Antti Palosaaric98975f2016-08-09 22:00:37 -030038 dev_dbg(&client->dev,
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);
Antti Palosaari27cfc852011-04-07 16:27:43 -030042
Antti Palosaari27cfc852011-04-07 16:27:43 -030043 /* program tuner */
44 if (fe->ops.tuner_ops.set_params)
Mauro Carvalho Chehab14d24d12011-12-24 12:24:33 -030045 fe->ops.tuner_ops.set_params(fe);
Antti Palosaari27cfc852011-04-07 16:27:43 -030046
Mauro Carvalho Chehab03494712011-12-22 18:11:39 -030047 if (priv->delivery_system != SYS_DVBC_ANNEX_A) {
Antti Palosaari43e2ea62016-08-13 13:19:05 -030048 ret = cxd2820r_wr_reg_val_mask_tab(priv, tab, ARRAY_SIZE(tab));
49 if (ret)
50 goto error;
Antti Palosaari27cfc852011-04-07 16:27:43 -030051 }
52
Mauro Carvalho Chehab03494712011-12-22 18:11:39 -030053 priv->delivery_system = SYS_DVBC_ANNEX_A;
Mauro Carvalho Chehab285c0b02014-09-03 15:22:02 -030054 priv->ber_running = false; /* tune stops BER counter */
Antti Palosaari27cfc852011-04-07 16:27:43 -030055
Antti Palosaarifda23fa2011-11-13 14:41:25 -030056 /* program IF frequency */
57 if (fe->ops.tuner_ops.get_if_frequency) {
Antti Palosaarifcd09f62016-08-08 15:54:10 -030058 ret = fe->ops.tuner_ops.get_if_frequency(fe, &if_frequency);
Antti Palosaarifda23fa2011-11-13 14:41:25 -030059 if (ret)
60 goto error;
Antti Palosaaric98975f2016-08-09 22:00:37 -030061 dev_dbg(&client->dev, "if_frequency=%u\n", if_frequency);
Antti Palosaarifcd09f62016-08-08 15:54:10 -030062 } else {
63 ret = -EINVAL;
64 goto error;
65 }
Antti Palosaarifda23fa2011-11-13 14:41:25 -030066
Antti Palosaarifcd09f62016-08-08 15:54:10 -030067 utmp = 0x4000 - DIV_ROUND_CLOSEST_ULL((u64)if_frequency * 0x4000, CXD2820R_CLK);
68 buf[0] = (utmp >> 8) & 0xff;
69 buf[1] = (utmp >> 0) & 0xff;
Antti Palosaari43e2ea62016-08-13 13:19:05 -030070 ret = regmap_bulk_write(priv->regmap[1], 0x0042, buf, 2);
Antti Palosaari27cfc852011-04-07 16:27:43 -030071 if (ret)
72 goto error;
73
Antti Palosaari43e2ea62016-08-13 13:19:05 -030074 ret = regmap_write(priv->regmap[0], 0x00ff, 0x08);
Antti Palosaari27cfc852011-04-07 16:27:43 -030075 if (ret)
76 goto error;
77
Antti Palosaari43e2ea62016-08-13 13:19:05 -030078 ret = regmap_write(priv->regmap[0], 0x00fe, 0x01);
Antti Palosaari27cfc852011-04-07 16:27:43 -030079 if (ret)
80 goto error;
81
82 return ret;
83error:
Antti Palosaaric98975f2016-08-09 22:00:37 -030084 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaari27cfc852011-04-07 16:27:43 -030085 return ret;
86}
87
Mauro Carvalho Chehab7e3e68b2016-02-04 12:58:30 -020088int cxd2820r_get_frontend_c(struct dvb_frontend *fe,
89 struct dtv_frontend_properties *c)
Antti Palosaari27cfc852011-04-07 16:27:43 -030090{
91 struct cxd2820r_priv *priv = fe->demodulator_priv;
Antti Palosaaric98975f2016-08-09 22:00:37 -030092 struct i2c_client *client = priv->client[0];
Antti Palosaari27cfc852011-04-07 16:27:43 -030093 int ret;
Antti Palosaari43e2ea62016-08-13 13:19:05 -030094 unsigned int utmp;
Antti Palosaari27cfc852011-04-07 16:27:43 -030095 u8 buf[2];
96
Antti Palosaaric98975f2016-08-09 22:00:37 -030097 dev_dbg(&client->dev, "\n");
98
Antti Palosaari43e2ea62016-08-13 13:19:05 -030099 ret = regmap_bulk_read(priv->regmap[1], 0x001a, buf, 2);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300100 if (ret)
101 goto error;
102
103 c->symbol_rate = 2500 * ((buf[0] & 0x0f) << 8 | buf[1]);
104
Antti Palosaari43e2ea62016-08-13 13:19:05 -0300105 ret = regmap_read(priv->regmap[1], 0x0019, &utmp);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300106 if (ret)
107 goto error;
108
Antti Palosaari43e2ea62016-08-13 13:19:05 -0300109 switch ((utmp >> 0) & 0x07) {
Antti Palosaari27cfc852011-04-07 16:27:43 -0300110 case 0:
111 c->modulation = QAM_16;
112 break;
113 case 1:
114 c->modulation = QAM_32;
115 break;
116 case 2:
117 c->modulation = QAM_64;
118 break;
119 case 3:
120 c->modulation = QAM_128;
121 break;
122 case 4:
123 c->modulation = QAM_256;
124 break;
125 }
126
Antti Palosaari43e2ea62016-08-13 13:19:05 -0300127 switch ((utmp >> 7) & 0x01) {
Antti Palosaari27cfc852011-04-07 16:27:43 -0300128 case 0:
129 c->inversion = INVERSION_OFF;
130 break;
131 case 1:
132 c->inversion = INVERSION_ON;
133 break;
134 }
135
136 return ret;
137error:
Antti Palosaaric98975f2016-08-09 22:00:37 -0300138 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300139 return ret;
140}
141
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -0300142int cxd2820r_read_status_c(struct dvb_frontend *fe, enum fe_status *status)
Antti Palosaari27cfc852011-04-07 16:27:43 -0300143{
144 struct cxd2820r_priv *priv = fe->demodulator_priv;
Antti Palosaaric98975f2016-08-09 22:00:37 -0300145 struct i2c_client *client = priv->client[0];
Antti Palosaari90d5d2e2016-08-08 23:03:32 -0300146 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Antti Palosaari27cfc852011-04-07 16:27:43 -0300147 int ret;
Antti Palosaarid51dc912016-08-12 16:58:05 -0300148 unsigned int utmp, utmp1, utmp2;
Antti Palosaari90d5d2e2016-08-08 23:03:32 -0300149 u8 buf[3];
Antti Palosaari27cfc852011-04-07 16:27:43 -0300150
Antti Palosaarid51dc912016-08-12 16:58:05 -0300151 /* Lock detection */
Antti Palosaari43e2ea62016-08-13 13:19:05 -0300152 ret = regmap_bulk_read(priv->regmap[1], 0x0088, &buf[0], 1);
Antti Palosaarid51dc912016-08-12 16:58:05 -0300153 if (ret)
154 goto error;
Antti Palosaari43e2ea62016-08-13 13:19:05 -0300155 ret = regmap_bulk_read(priv->regmap[1], 0x0073, &buf[1], 1);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300156 if (ret)
157 goto error;
158
Antti Palosaarid51dc912016-08-12 16:58:05 -0300159 utmp1 = (buf[0] >> 0) & 0x01;
160 utmp2 = (buf[1] >> 3) & 0x01;
Antti Palosaari27cfc852011-04-07 16:27:43 -0300161
Antti Palosaarid51dc912016-08-12 16:58:05 -0300162 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;
168 } else {
169 *status = 0;
Antti Palosaari27cfc852011-04-07 16:27:43 -0300170 }
171
Antti Palosaarid51dc912016-08-12 16:58:05 -0300172 dev_dbg(&client->dev, "status=%02x raw=%*ph sync=%u ts=%u\n",
173 *status, 2, buf, utmp1, utmp2);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300174
Antti Palosaari90d5d2e2016-08-08 23:03:32 -0300175 /* Signal strength */
176 if (*status & FE_HAS_SIGNAL) {
177 unsigned int strength;
178
Antti Palosaari43e2ea62016-08-13 13:19:05 -0300179 ret = regmap_bulk_read(priv->regmap[1], 0x0049, buf, 2);
Antti Palosaari90d5d2e2016-08-08 23:03:32 -0300180 if (ret)
181 goto error;
182
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;
187
188 c->strength.len = 1;
189 c->strength.stat[0].scale = FE_SCALE_RELATIVE;
190 c->strength.stat[0].uvalue = strength;
191 } else {
192 c->strength.len = 1;
193 c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
194 }
195
196 /* CNR */
197 if (*status & FE_HAS_VITERBI) {
198 unsigned int cnr, const_a, const_b;
199
Antti Palosaari43e2ea62016-08-13 13:19:05 -0300200 ret = regmap_read(priv->regmap[1], 0x0019, &utmp);
Antti Palosaari90d5d2e2016-08-08 23:03:32 -0300201 if (ret)
202 goto error;
203
Antti Palosaari43e2ea62016-08-13 13:19:05 -0300204 if (((utmp >> 0) & 0x03) % 2) {
Antti Palosaari90d5d2e2016-08-08 23:03:32 -0300205 const_a = 8750;
206 const_b = 650;
207 } else {
208 const_a = 9500;
209 const_b = 760;
210 }
211
Antti Palosaari43e2ea62016-08-13 13:19:05 -0300212 ret = regmap_read(priv->regmap[1], 0x004d, &utmp);
Antti Palosaari90d5d2e2016-08-08 23:03:32 -0300213 if (ret)
214 goto error;
215
Antti Palosaari90d5d2e2016-08-08 23:03:32 -0300216 #define CXD2820R_LOG2_E_24 24204406 /* log2(e) << 24 */
217 if (utmp)
218 cnr = div_u64((u64)(intlog2(const_b) - intlog2(utmp))
219 * const_a, CXD2820R_LOG2_E_24);
220 else
221 cnr = 0;
222
223 c->cnr.len = 1;
224 c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
225 c->cnr.stat[0].svalue = cnr;
226 } else {
227 c->cnr.len = 1;
228 c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
229 }
230
231 /* BER */
232 if (*status & FE_HAS_SYNC) {
233 unsigned int post_bit_error;
234 bool start_ber;
235
236 if (priv->ber_running) {
Antti Palosaari43e2ea62016-08-13 13:19:05 -0300237 ret = regmap_bulk_read(priv->regmap[1], 0x0076, buf, 3);
Antti Palosaari90d5d2e2016-08-08 23:03:32 -0300238 if (ret)
239 goto error;
240
241 if ((buf[2] >> 7) & 0x01) {
242 post_bit_error = buf[2] << 16 | buf[1] << 8 |
243 buf[0] << 0;
244 post_bit_error &= 0x0fffff;
245 start_ber = true;
246 } else {
247 post_bit_error = 0;
248 start_ber = false;
249 }
250 } else {
251 post_bit_error = 0;
252 start_ber = true;
253 }
254
255 if (start_ber) {
Antti Palosaari43e2ea62016-08-13 13:19:05 -0300256 ret = regmap_write(priv->regmap[1], 0x0079, 0x01);
Antti Palosaari90d5d2e2016-08-08 23:03:32 -0300257 if (ret)
258 goto error;
259 priv->ber_running = true;
260 }
261
262 priv->post_bit_error += post_bit_error;
263
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;
267 } else {
268 c->post_bit_error.len = 1;
269 c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
270 }
271
Antti Palosaari27cfc852011-04-07 16:27:43 -0300272 return ret;
273error:
Antti Palosaaric98975f2016-08-09 22:00:37 -0300274 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300275 return ret;
276}
277
Steve Kerrison9ac51c52011-05-02 18:19:13 -0300278int cxd2820r_init_c(struct dvb_frontend *fe)
Antti Palosaari27cfc852011-04-07 16:27:43 -0300279{
280 struct cxd2820r_priv *priv = fe->demodulator_priv;
Antti Palosaaric98975f2016-08-09 22:00:37 -0300281 struct i2c_client *client = priv->client[0];
Antti Palosaari27cfc852011-04-07 16:27:43 -0300282 int ret;
283
Antti Palosaaric98975f2016-08-09 22:00:37 -0300284 dev_dbg(&client->dev, "\n");
285
Antti Palosaari43e2ea62016-08-13 13:19:05 -0300286 ret = regmap_write(priv->regmap[0], 0x0085, 0x07);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300287 if (ret)
288 goto error;
289
290 return ret;
291error:
Antti Palosaaric98975f2016-08-09 22:00:37 -0300292 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300293 return ret;
294}
295
Steve Kerrison9ac51c52011-05-02 18:19:13 -0300296int cxd2820r_sleep_c(struct dvb_frontend *fe)
Antti Palosaari27cfc852011-04-07 16:27:43 -0300297{
298 struct cxd2820r_priv *priv = fe->demodulator_priv;
Antti Palosaaric98975f2016-08-09 22:00:37 -0300299 struct i2c_client *client = priv->client[0];
Antti Palosaari43e2ea62016-08-13 13:19:05 -0300300 int ret;
Antti Palosaari27cfc852011-04-07 16:27:43 -0300301 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 },
307 };
308
Antti Palosaaric98975f2016-08-09 22:00:37 -0300309 dev_dbg(&client->dev, "\n");
Antti Palosaari27cfc852011-04-07 16:27:43 -0300310
311 priv->delivery_system = SYS_UNDEFINED;
312
Antti Palosaari43e2ea62016-08-13 13:19:05 -0300313 ret = cxd2820r_wr_reg_val_mask_tab(priv, tab, ARRAY_SIZE(tab));
314 if (ret)
315 goto error;
Antti Palosaari27cfc852011-04-07 16:27:43 -0300316
317 return ret;
318error:
Antti Palosaaric98975f2016-08-09 22:00:37 -0300319 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300320 return ret;
321}
322
Steve Kerrison9ac51c52011-05-02 18:19:13 -0300323int cxd2820r_get_tune_settings_c(struct dvb_frontend *fe,
Antti Palosaari27cfc852011-04-07 16:27:43 -0300324 struct dvb_frontend_tune_settings *s)
325{
326 s->min_delay_ms = 500;
327 s->step_size = 0; /* no zigzag */
328 s->max_drift = 0;
329
330 return 0;
331}