blob: d75b0776d5b5050b35633b369c515a51916a1d07 [file] [log] [blame]
Antti Palosaari27cfc852011-04-07 16:27:43 -03001/*
2 * Sony CXD2820R demodulator driver
3 *
4 * Copyright (C) 2010 Antti Palosaari <crope@iki.fi>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21
Steve Kerrison9ac51c52011-05-02 18:19:13 -030022#include "cxd2820r_priv.h"
23
Mauro Carvalho Chehabf311f682011-12-30 22:22:10 -030024int cxd2820r_set_frontend_c(struct dvb_frontend *fe)
Antti Palosaari27cfc852011-04-07 16:27:43 -030025{
26 struct cxd2820r_priv *priv = fe->demodulator_priv;
Antti Palosaaric98975f2016-08-09 22:00:37 -030027 struct i2c_client *client = priv->client[0];
Antti Palosaari27cfc852011-04-07 16:27:43 -030028 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Antti Palosaari43e2ea62016-08-13 13:19:05 -030029 int ret;
Antti Palosaarifcd09f62016-08-08 15:54:10 -030030 unsigned int utmp;
Antti Palosaari27cfc852011-04-07 16:27:43 -030031 u8 buf[2];
Antti Palosaarifcd09f62016-08-08 15:54:10 -030032 u32 if_frequency;
Antti Palosaari27cfc852011-04-07 16:27:43 -030033 struct reg_val_mask tab[] = {
34 { 0x00080, 0x01, 0xff },
35 { 0x00081, 0x05, 0xff },
36 { 0x00085, 0x07, 0xff },
37 { 0x00088, 0x01, 0xff },
38
39 { 0x00082, 0x20, 0x60 },
40 { 0x1016a, 0x48, 0xff },
41 { 0x100a5, 0x00, 0x01 },
42 { 0x10020, 0x06, 0x07 },
43 { 0x10059, 0x50, 0xff },
44 { 0x10087, 0x0c, 0x3c },
45 { 0x1008b, 0x07, 0xff },
Antti Palosaari07fdf7d2016-08-09 20:49:09 -030046 { 0x1001f, priv->if_agc_polarity << 7, 0x80 },
47 { 0x10070, priv->ts_mode, 0xff },
48 { 0x10071, !priv->ts_clk_inv << 4, 0x10 },
Antti Palosaari27cfc852011-04-07 16:27:43 -030049 };
50
Antti Palosaaric98975f2016-08-09 22:00:37 -030051 dev_dbg(&client->dev,
52 "delivery_system=%d modulation=%d frequency=%u symbol_rate=%u inversion=%d\n",
53 c->delivery_system, c->modulation, c->frequency,
54 c->symbol_rate, c->inversion);
Antti Palosaari27cfc852011-04-07 16:27:43 -030055
Antti Palosaari27cfc852011-04-07 16:27:43 -030056 /* program tuner */
57 if (fe->ops.tuner_ops.set_params)
Mauro Carvalho Chehab14d24d12011-12-24 12:24:33 -030058 fe->ops.tuner_ops.set_params(fe);
Antti Palosaari27cfc852011-04-07 16:27:43 -030059
Mauro Carvalho Chehab03494712011-12-22 18:11:39 -030060 if (priv->delivery_system != SYS_DVBC_ANNEX_A) {
Antti Palosaari43e2ea62016-08-13 13:19:05 -030061 ret = cxd2820r_wr_reg_val_mask_tab(priv, tab, ARRAY_SIZE(tab));
62 if (ret)
63 goto error;
Antti Palosaari27cfc852011-04-07 16:27:43 -030064 }
65
Mauro Carvalho Chehab03494712011-12-22 18:11:39 -030066 priv->delivery_system = SYS_DVBC_ANNEX_A;
Mauro Carvalho Chehab285c0b02014-09-03 15:22:02 -030067 priv->ber_running = false; /* tune stops BER counter */
Antti Palosaari27cfc852011-04-07 16:27:43 -030068
Antti Palosaarifda23fa2011-11-13 14:41:25 -030069 /* program IF frequency */
70 if (fe->ops.tuner_ops.get_if_frequency) {
Antti Palosaarifcd09f62016-08-08 15:54:10 -030071 ret = fe->ops.tuner_ops.get_if_frequency(fe, &if_frequency);
Antti Palosaarifda23fa2011-11-13 14:41:25 -030072 if (ret)
73 goto error;
Antti Palosaaric98975f2016-08-09 22:00:37 -030074 dev_dbg(&client->dev, "if_frequency=%u\n", if_frequency);
Antti Palosaarifcd09f62016-08-08 15:54:10 -030075 } else {
76 ret = -EINVAL;
77 goto error;
78 }
Antti Palosaarifda23fa2011-11-13 14:41:25 -030079
Antti Palosaarifcd09f62016-08-08 15:54:10 -030080 utmp = 0x4000 - DIV_ROUND_CLOSEST_ULL((u64)if_frequency * 0x4000, CXD2820R_CLK);
81 buf[0] = (utmp >> 8) & 0xff;
82 buf[1] = (utmp >> 0) & 0xff;
Antti Palosaari43e2ea62016-08-13 13:19:05 -030083 ret = regmap_bulk_write(priv->regmap[1], 0x0042, buf, 2);
Antti Palosaari27cfc852011-04-07 16:27:43 -030084 if (ret)
85 goto error;
86
Antti Palosaari43e2ea62016-08-13 13:19:05 -030087 ret = regmap_write(priv->regmap[0], 0x00ff, 0x08);
Antti Palosaari27cfc852011-04-07 16:27:43 -030088 if (ret)
89 goto error;
90
Antti Palosaari43e2ea62016-08-13 13:19:05 -030091 ret = regmap_write(priv->regmap[0], 0x00fe, 0x01);
Antti Palosaari27cfc852011-04-07 16:27:43 -030092 if (ret)
93 goto error;
94
95 return ret;
96error:
Antti Palosaaric98975f2016-08-09 22:00:37 -030097 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaari27cfc852011-04-07 16:27:43 -030098 return ret;
99}
100
Mauro Carvalho Chehab7e3e68b2016-02-04 12:58:30 -0200101int cxd2820r_get_frontend_c(struct dvb_frontend *fe,
102 struct dtv_frontend_properties *c)
Antti Palosaari27cfc852011-04-07 16:27:43 -0300103{
104 struct cxd2820r_priv *priv = fe->demodulator_priv;
Antti Palosaaric98975f2016-08-09 22:00:37 -0300105 struct i2c_client *client = priv->client[0];
Antti Palosaari27cfc852011-04-07 16:27:43 -0300106 int ret;
Antti Palosaari43e2ea62016-08-13 13:19:05 -0300107 unsigned int utmp;
Antti Palosaari27cfc852011-04-07 16:27:43 -0300108 u8 buf[2];
109
Antti Palosaaric98975f2016-08-09 22:00:37 -0300110 dev_dbg(&client->dev, "\n");
111
Antti Palosaari43e2ea62016-08-13 13:19:05 -0300112 ret = regmap_bulk_read(priv->regmap[1], 0x001a, buf, 2);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300113 if (ret)
114 goto error;
115
116 c->symbol_rate = 2500 * ((buf[0] & 0x0f) << 8 | buf[1]);
117
Antti Palosaari43e2ea62016-08-13 13:19:05 -0300118 ret = regmap_read(priv->regmap[1], 0x0019, &utmp);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300119 if (ret)
120 goto error;
121
Antti Palosaari43e2ea62016-08-13 13:19:05 -0300122 switch ((utmp >> 0) & 0x07) {
Antti Palosaari27cfc852011-04-07 16:27:43 -0300123 case 0:
124 c->modulation = QAM_16;
125 break;
126 case 1:
127 c->modulation = QAM_32;
128 break;
129 case 2:
130 c->modulation = QAM_64;
131 break;
132 case 3:
133 c->modulation = QAM_128;
134 break;
135 case 4:
136 c->modulation = QAM_256;
137 break;
138 }
139
Antti Palosaari43e2ea62016-08-13 13:19:05 -0300140 switch ((utmp >> 7) & 0x01) {
Antti Palosaari27cfc852011-04-07 16:27:43 -0300141 case 0:
142 c->inversion = INVERSION_OFF;
143 break;
144 case 1:
145 c->inversion = INVERSION_ON;
146 break;
147 }
148
149 return ret;
150error:
Antti Palosaaric98975f2016-08-09 22:00:37 -0300151 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300152 return ret;
153}
154
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -0300155int cxd2820r_read_status_c(struct dvb_frontend *fe, enum fe_status *status)
Antti Palosaari27cfc852011-04-07 16:27:43 -0300156{
157 struct cxd2820r_priv *priv = fe->demodulator_priv;
Antti Palosaaric98975f2016-08-09 22:00:37 -0300158 struct i2c_client *client = priv->client[0];
Antti Palosaari90d5d2e2016-08-08 23:03:32 -0300159 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Antti Palosaari27cfc852011-04-07 16:27:43 -0300160 int ret;
Antti Palosaarid51dc912016-08-12 16:58:05 -0300161 unsigned int utmp, utmp1, utmp2;
Antti Palosaari90d5d2e2016-08-08 23:03:32 -0300162 u8 buf[3];
Antti Palosaari27cfc852011-04-07 16:27:43 -0300163
Antti Palosaarid51dc912016-08-12 16:58:05 -0300164 /* Lock detection */
Antti Palosaari43e2ea62016-08-13 13:19:05 -0300165 ret = regmap_bulk_read(priv->regmap[1], 0x0088, &buf[0], 1);
Antti Palosaarid51dc912016-08-12 16:58:05 -0300166 if (ret)
167 goto error;
Antti Palosaari43e2ea62016-08-13 13:19:05 -0300168 ret = regmap_bulk_read(priv->regmap[1], 0x0073, &buf[1], 1);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300169 if (ret)
170 goto error;
171
Antti Palosaarid51dc912016-08-12 16:58:05 -0300172 utmp1 = (buf[0] >> 0) & 0x01;
173 utmp2 = (buf[1] >> 3) & 0x01;
Antti Palosaari27cfc852011-04-07 16:27:43 -0300174
Antti Palosaarid51dc912016-08-12 16:58:05 -0300175 if (utmp1 == 1 && utmp2 == 1) {
176 *status = FE_HAS_SIGNAL | FE_HAS_CARRIER |
177 FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK;
178 } else if (utmp1 == 1 || utmp2 == 1) {
179 *status = FE_HAS_SIGNAL | FE_HAS_CARRIER |
180 FE_HAS_VITERBI | FE_HAS_SYNC;
181 } else {
182 *status = 0;
Antti Palosaari27cfc852011-04-07 16:27:43 -0300183 }
184
Antti Palosaarid51dc912016-08-12 16:58:05 -0300185 dev_dbg(&client->dev, "status=%02x raw=%*ph sync=%u ts=%u\n",
186 *status, 2, buf, utmp1, utmp2);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300187
Antti Palosaari90d5d2e2016-08-08 23:03:32 -0300188 /* Signal strength */
189 if (*status & FE_HAS_SIGNAL) {
190 unsigned int strength;
191
Antti Palosaari43e2ea62016-08-13 13:19:05 -0300192 ret = regmap_bulk_read(priv->regmap[1], 0x0049, buf, 2);
Antti Palosaari90d5d2e2016-08-08 23:03:32 -0300193 if (ret)
194 goto error;
195
196 utmp = buf[0] << 8 | buf[1] << 0;
197 utmp = 511 - sign_extend32(utmp, 9);
198 /* Scale value to 0x0000-0xffff */
199 strength = utmp << 6 | utmp >> 4;
200
201 c->strength.len = 1;
202 c->strength.stat[0].scale = FE_SCALE_RELATIVE;
203 c->strength.stat[0].uvalue = strength;
204 } else {
205 c->strength.len = 1;
206 c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
207 }
208
209 /* CNR */
210 if (*status & FE_HAS_VITERBI) {
211 unsigned int cnr, const_a, const_b;
212
Antti Palosaari43e2ea62016-08-13 13:19:05 -0300213 ret = regmap_read(priv->regmap[1], 0x0019, &utmp);
Antti Palosaari90d5d2e2016-08-08 23:03:32 -0300214 if (ret)
215 goto error;
216
Antti Palosaari43e2ea62016-08-13 13:19:05 -0300217 if (((utmp >> 0) & 0x03) % 2) {
Antti Palosaari90d5d2e2016-08-08 23:03:32 -0300218 const_a = 8750;
219 const_b = 650;
220 } else {
221 const_a = 9500;
222 const_b = 760;
223 }
224
Antti Palosaari43e2ea62016-08-13 13:19:05 -0300225 ret = regmap_read(priv->regmap[1], 0x004d, &utmp);
Antti Palosaari90d5d2e2016-08-08 23:03:32 -0300226 if (ret)
227 goto error;
228
Antti Palosaari90d5d2e2016-08-08 23:03:32 -0300229 #define CXD2820R_LOG2_E_24 24204406 /* log2(e) << 24 */
230 if (utmp)
231 cnr = div_u64((u64)(intlog2(const_b) - intlog2(utmp))
232 * const_a, CXD2820R_LOG2_E_24);
233 else
234 cnr = 0;
235
236 c->cnr.len = 1;
237 c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
238 c->cnr.stat[0].svalue = cnr;
239 } else {
240 c->cnr.len = 1;
241 c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
242 }
243
244 /* BER */
245 if (*status & FE_HAS_SYNC) {
246 unsigned int post_bit_error;
247 bool start_ber;
248
249 if (priv->ber_running) {
Antti Palosaari43e2ea62016-08-13 13:19:05 -0300250 ret = regmap_bulk_read(priv->regmap[1], 0x0076, buf, 3);
Antti Palosaari90d5d2e2016-08-08 23:03:32 -0300251 if (ret)
252 goto error;
253
254 if ((buf[2] >> 7) & 0x01) {
255 post_bit_error = buf[2] << 16 | buf[1] << 8 |
256 buf[0] << 0;
257 post_bit_error &= 0x0fffff;
258 start_ber = true;
259 } else {
260 post_bit_error = 0;
261 start_ber = false;
262 }
263 } else {
264 post_bit_error = 0;
265 start_ber = true;
266 }
267
268 if (start_ber) {
Antti Palosaari43e2ea62016-08-13 13:19:05 -0300269 ret = regmap_write(priv->regmap[1], 0x0079, 0x01);
Antti Palosaari90d5d2e2016-08-08 23:03:32 -0300270 if (ret)
271 goto error;
272 priv->ber_running = true;
273 }
274
275 priv->post_bit_error += post_bit_error;
276
277 c->post_bit_error.len = 1;
278 c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
279 c->post_bit_error.stat[0].uvalue = priv->post_bit_error;
280 } else {
281 c->post_bit_error.len = 1;
282 c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
283 }
284
Antti Palosaari27cfc852011-04-07 16:27:43 -0300285 return ret;
286error:
Antti Palosaaric98975f2016-08-09 22:00:37 -0300287 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300288 return ret;
289}
290
Steve Kerrison9ac51c52011-05-02 18:19:13 -0300291int cxd2820r_init_c(struct dvb_frontend *fe)
Antti Palosaari27cfc852011-04-07 16:27:43 -0300292{
293 struct cxd2820r_priv *priv = fe->demodulator_priv;
Antti Palosaaric98975f2016-08-09 22:00:37 -0300294 struct i2c_client *client = priv->client[0];
Antti Palosaari27cfc852011-04-07 16:27:43 -0300295 int ret;
296
Antti Palosaaric98975f2016-08-09 22:00:37 -0300297 dev_dbg(&client->dev, "\n");
298
Antti Palosaari43e2ea62016-08-13 13:19:05 -0300299 ret = regmap_write(priv->regmap[0], 0x0085, 0x07);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300300 if (ret)
301 goto error;
302
303 return ret;
304error:
Antti Palosaaric98975f2016-08-09 22:00:37 -0300305 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300306 return ret;
307}
308
Steve Kerrison9ac51c52011-05-02 18:19:13 -0300309int cxd2820r_sleep_c(struct dvb_frontend *fe)
Antti Palosaari27cfc852011-04-07 16:27:43 -0300310{
311 struct cxd2820r_priv *priv = fe->demodulator_priv;
Antti Palosaaric98975f2016-08-09 22:00:37 -0300312 struct i2c_client *client = priv->client[0];
Antti Palosaari43e2ea62016-08-13 13:19:05 -0300313 int ret;
Antti Palosaari27cfc852011-04-07 16:27:43 -0300314 struct reg_val_mask tab[] = {
315 { 0x000ff, 0x1f, 0xff },
316 { 0x00085, 0x00, 0xff },
317 { 0x00088, 0x01, 0xff },
318 { 0x00081, 0x00, 0xff },
319 { 0x00080, 0x00, 0xff },
320 };
321
Antti Palosaaric98975f2016-08-09 22:00:37 -0300322 dev_dbg(&client->dev, "\n");
Antti Palosaari27cfc852011-04-07 16:27:43 -0300323
324 priv->delivery_system = SYS_UNDEFINED;
325
Antti Palosaari43e2ea62016-08-13 13:19:05 -0300326 ret = cxd2820r_wr_reg_val_mask_tab(priv, tab, ARRAY_SIZE(tab));
327 if (ret)
328 goto error;
Antti Palosaari27cfc852011-04-07 16:27:43 -0300329
330 return ret;
331error:
Antti Palosaaric98975f2016-08-09 22:00:37 -0300332 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300333 return ret;
334}
335
Steve Kerrison9ac51c52011-05-02 18:19:13 -0300336int cxd2820r_get_tune_settings_c(struct dvb_frontend *fe,
Antti Palosaari27cfc852011-04-07 16:27:43 -0300337 struct dvb_frontend_tune_settings *s)
338{
339 s->min_delay_ms = 500;
340 s->step_size = 0; /* no zigzag */
341 s->max_drift = 0;
342
343 return 0;
344}