ima: fix bug in argument order
[sfrench/cifs-2.6.git] / drivers / media / dvb / frontends / cxd2820r_c.c
1 /*
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
22 #include "cxd2820r_priv.h"
23
24 int cxd2820r_set_frontend_c(struct dvb_frontend *fe)
25 {
26         struct cxd2820r_priv *priv = fe->demodulator_priv;
27         struct dtv_frontend_properties *c = &fe->dtv_property_cache;
28         int ret, i;
29         u8 buf[2];
30         u32 if_freq;
31         u16 if_ctl;
32         u64 num;
33         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 },
46                 { 0x1001f, priv->cfg.if_agc_polarity << 7, 0x80 },
47                 { 0x10070, priv->cfg.ts_mode, 0xff },
48         };
49
50         dbg("%s: RF=%d SR=%d", __func__, c->frequency, c->symbol_rate);
51
52         /* update GPIOs */
53         ret = cxd2820r_gpio(fe);
54         if (ret)
55                 goto error;
56
57         /* program tuner */
58         if (fe->ops.tuner_ops.set_params)
59                 fe->ops.tuner_ops.set_params(fe);
60
61         if (priv->delivery_system !=  SYS_DVBC_ANNEX_A) {
62                 for (i = 0; i < ARRAY_SIZE(tab); i++) {
63                         ret = cxd2820r_wr_reg_mask(priv, tab[i].reg,
64                                 tab[i].val, tab[i].mask);
65                         if (ret)
66                                 goto error;
67                 }
68         }
69
70         priv->delivery_system = SYS_DVBC_ANNEX_A;
71         priv->ber_running = 0; /* tune stops BER counter */
72
73         /* program IF frequency */
74         if (fe->ops.tuner_ops.get_if_frequency) {
75                 ret = fe->ops.tuner_ops.get_if_frequency(fe, &if_freq);
76                 if (ret)
77                         goto error;
78         } else
79                 if_freq = 0;
80
81         dbg("%s: if_freq=%d", __func__, if_freq);
82
83         num = if_freq / 1000; /* Hz => kHz */
84         num *= 0x4000;
85         if_ctl = cxd2820r_div_u64_round_closest(num, 41000);
86         buf[0] = (if_ctl >> 8) & 0x3f;
87         buf[1] = (if_ctl >> 0) & 0xff;
88
89         ret = cxd2820r_wr_regs(priv, 0x10042, buf, 2);
90         if (ret)
91                 goto error;
92
93         ret = cxd2820r_wr_reg(priv, 0x000ff, 0x08);
94         if (ret)
95                 goto error;
96
97         ret = cxd2820r_wr_reg(priv, 0x000fe, 0x01);
98         if (ret)
99                 goto error;
100
101         return ret;
102 error:
103         dbg("%s: failed:%d", __func__, ret);
104         return ret;
105 }
106
107 int cxd2820r_get_frontend_c(struct dvb_frontend *fe)
108 {
109         struct cxd2820r_priv *priv = fe->demodulator_priv;
110         struct dtv_frontend_properties *c = &fe->dtv_property_cache;
111         int ret;
112         u8 buf[2];
113
114         ret = cxd2820r_rd_regs(priv, 0x1001a, buf, 2);
115         if (ret)
116                 goto error;
117
118         c->symbol_rate = 2500 * ((buf[0] & 0x0f) << 8 | buf[1]);
119
120         ret = cxd2820r_rd_reg(priv, 0x10019, &buf[0]);
121         if (ret)
122                 goto error;
123
124         switch ((buf[0] >> 0) & 0x07) {
125         case 0:
126                 c->modulation = QAM_16;
127                 break;
128         case 1:
129                 c->modulation = QAM_32;
130                 break;
131         case 2:
132                 c->modulation = QAM_64;
133                 break;
134         case 3:
135                 c->modulation = QAM_128;
136                 break;
137         case 4:
138                 c->modulation = QAM_256;
139                 break;
140         }
141
142         switch ((buf[0] >> 7) & 0x01) {
143         case 0:
144                 c->inversion = INVERSION_OFF;
145                 break;
146         case 1:
147                 c->inversion = INVERSION_ON;
148                 break;
149         }
150
151         return ret;
152 error:
153         dbg("%s: failed:%d", __func__, ret);
154         return ret;
155 }
156
157 int cxd2820r_read_ber_c(struct dvb_frontend *fe, u32 *ber)
158 {
159         struct cxd2820r_priv *priv = fe->demodulator_priv;
160         int ret;
161         u8 buf[3], start_ber = 0;
162         *ber = 0;
163
164         if (priv->ber_running) {
165                 ret = cxd2820r_rd_regs(priv, 0x10076, buf, sizeof(buf));
166                 if (ret)
167                         goto error;
168
169                 if ((buf[2] >> 7) & 0x01 || (buf[2] >> 4) & 0x01) {
170                         *ber = (buf[2] & 0x0f) << 16 | buf[1] << 8 | buf[0];
171                         start_ber = 1;
172                 }
173         } else {
174                 priv->ber_running = 1;
175                 start_ber = 1;
176         }
177
178         if (start_ber) {
179                 /* (re)start BER */
180                 ret = cxd2820r_wr_reg(priv, 0x10079, 0x01);
181                 if (ret)
182                         goto error;
183         }
184
185         return ret;
186 error:
187         dbg("%s: failed:%d", __func__, ret);
188         return ret;
189 }
190
191 int cxd2820r_read_signal_strength_c(struct dvb_frontend *fe,
192         u16 *strength)
193 {
194         struct cxd2820r_priv *priv = fe->demodulator_priv;
195         int ret;
196         u8 buf[2];
197         u16 tmp;
198
199         ret = cxd2820r_rd_regs(priv, 0x10049, buf, sizeof(buf));
200         if (ret)
201                 goto error;
202
203         tmp = (buf[0] & 0x03) << 8 | buf[1];
204         tmp = (~tmp & 0x03ff);
205
206         if (tmp == 512)
207                 /* ~no signal */
208                 tmp = 0;
209         else if (tmp > 350)
210                 tmp = 350;
211
212         /* scale value to 0x0000-0xffff */
213         *strength = tmp * 0xffff / (350-0);
214
215         return ret;
216 error:
217         dbg("%s: failed:%d", __func__, ret);
218         return ret;
219 }
220
221 int cxd2820r_read_snr_c(struct dvb_frontend *fe, u16 *snr)
222 {
223         struct cxd2820r_priv *priv = fe->demodulator_priv;
224         int ret;
225         u8 tmp;
226         unsigned int A, B;
227         /* report SNR in dB * 10 */
228
229         ret = cxd2820r_rd_reg(priv, 0x10019, &tmp);
230         if (ret)
231                 goto error;
232
233         if (((tmp >> 0) & 0x03) % 2) {
234                 A = 875;
235                 B = 650;
236         } else {
237                 A = 950;
238                 B = 760;
239         }
240
241         ret = cxd2820r_rd_reg(priv, 0x1004d, &tmp);
242         if (ret)
243                 goto error;
244
245         #define CXD2820R_LOG2_E_24 24204406 /* log2(e) << 24 */
246         if (tmp)
247                 *snr = A * (intlog2(B / tmp) >> 5) / (CXD2820R_LOG2_E_24 >> 5)
248                         / 10;
249         else
250                 *snr = 0;
251
252         return ret;
253 error:
254         dbg("%s: failed:%d", __func__, ret);
255         return ret;
256 }
257
258 int cxd2820r_read_ucblocks_c(struct dvb_frontend *fe, u32 *ucblocks)
259 {
260         *ucblocks = 0;
261         /* no way to read ? */
262         return 0;
263 }
264
265 int cxd2820r_read_status_c(struct dvb_frontend *fe, fe_status_t *status)
266 {
267         struct cxd2820r_priv *priv = fe->demodulator_priv;
268         int ret;
269         u8 buf[2];
270         *status = 0;
271
272         ret = cxd2820r_rd_regs(priv, 0x10088, buf, sizeof(buf));
273         if (ret)
274                 goto error;
275
276         if (((buf[0] >> 0) & 0x01) == 1) {
277                 *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
278                         FE_HAS_VITERBI | FE_HAS_SYNC;
279
280                 if (((buf[1] >> 3) & 0x01) == 1) {
281                         *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
282                                 FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK;
283                 }
284         }
285
286         dbg("%s: lock=%02x %02x", __func__, buf[0], buf[1]);
287
288         return ret;
289 error:
290         dbg("%s: failed:%d", __func__, ret);
291         return ret;
292 }
293
294 int cxd2820r_init_c(struct dvb_frontend *fe)
295 {
296         struct cxd2820r_priv *priv = fe->demodulator_priv;
297         int ret;
298
299         ret = cxd2820r_wr_reg(priv, 0x00085, 0x07);
300         if (ret)
301                 goto error;
302
303         return ret;
304 error:
305         dbg("%s: failed:%d", __func__, ret);
306         return ret;
307 }
308
309 int cxd2820r_sleep_c(struct dvb_frontend *fe)
310 {
311         struct cxd2820r_priv *priv = fe->demodulator_priv;
312         int ret, i;
313         struct reg_val_mask tab[] = {
314                 { 0x000ff, 0x1f, 0xff },
315                 { 0x00085, 0x00, 0xff },
316                 { 0x00088, 0x01, 0xff },
317                 { 0x00081, 0x00, 0xff },
318                 { 0x00080, 0x00, 0xff },
319         };
320
321         dbg("%s", __func__);
322
323         priv->delivery_system = SYS_UNDEFINED;
324
325         for (i = 0; i < ARRAY_SIZE(tab); i++) {
326                 ret = cxd2820r_wr_reg_mask(priv, tab[i].reg, tab[i].val,
327                         tab[i].mask);
328                 if (ret)
329                         goto error;
330         }
331
332         return ret;
333 error:
334         dbg("%s: failed:%d", __func__, ret);
335         return ret;
336 }
337
338 int cxd2820r_get_tune_settings_c(struct dvb_frontend *fe,
339         struct dvb_frontend_tune_settings *s)
340 {
341         s->min_delay_ms = 500;
342         s->step_size = 0; /* no zigzag */
343         s->max_drift = 0;
344
345         return 0;
346 }