]>
Commit | Line | Data |
---|---|---|
c942fddf | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
3e54a169 | 2 | /* |
18349f40 MS |
3 | * Driver for Silicon Labs Si2161 DVB-T and Si2165 DVB-C/-T Demodulator |
4 | * | |
77f887cd | 5 | * Copyright (C) 2013-2017 Matthias Schwarzott <[email protected]> |
18349f40 | 6 | * |
18349f40 | 7 | * References: |
965045ca | 8 | * https://www.silabs.com/Support%20Documents/TechnicalDocs/Si2165-short.pdf |
18349f40 | 9 | */ |
3e54a169 MS |
10 | |
11 | #include <linux/delay.h> | |
12 | #include <linux/errno.h> | |
13 | #include <linux/init.h> | |
14 | #include <linux/kernel.h> | |
15 | #include <linux/module.h> | |
16 | #include <linux/string.h> | |
17 | #include <linux/slab.h> | |
18 | #include <linux/firmware.h> | |
e3ea5e94 | 19 | #include <linux/regmap.h> |
3e54a169 | 20 | |
fada1935 | 21 | #include <media/dvb_frontend.h> |
f97fa3dc | 22 | #include <linux/int_log.h> |
3e54a169 MS |
23 | #include "si2165_priv.h" |
24 | #include "si2165.h" | |
25 | ||
18349f40 MS |
26 | /* |
27 | * Hauppauge WinTV-HVR-930C-HD B130 / PCTV QuatroStick 521e 1113xx | |
28 | * uses 16 MHz xtal | |
29 | * | |
30 | * Hauppauge WinTV-HVR-930C-HD B131 / PCTV QuatroStick 522e 1114xx | |
31 | * uses 24 MHz clock provided by tuner | |
32 | */ | |
3e54a169 MS |
33 | |
34 | struct si2165_state { | |
7cd785ad MS |
35 | struct i2c_client *client; |
36 | ||
e3ea5e94 | 37 | struct regmap *regmap; |
3e54a169 | 38 | |
d9a201df | 39 | struct dvb_frontend fe; |
3e54a169 MS |
40 | |
41 | struct si2165_config config; | |
42 | ||
55bea400 | 43 | u8 chip_revcode; |
3e54a169 MS |
44 | u8 chip_type; |
45 | ||
46 | /* calculated by xtal and div settings */ | |
47 | u32 fvco_hz; | |
48 | u32 sys_clk; | |
49 | u32 adc_clk; | |
50 | ||
e9c7d19a MS |
51 | /* DVBv3 stats */ |
52 | u64 ber_prev; | |
53 | ||
3e54a169 MS |
54 | bool has_dvbc; |
55 | bool has_dvbt; | |
56 | bool firmware_loaded; | |
57 | }; | |
58 | ||
3e54a169 | 59 | static int si2165_write(struct si2165_state *state, const u16 reg, |
7dbbb4bf | 60 | const u8 *src, const int count) |
3e54a169 MS |
61 | { |
62 | int ret; | |
3e54a169 | 63 | |
7dbbb4bf MS |
64 | dev_dbg(&state->client->dev, "i2c write: reg: 0x%04x, data: %*ph\n", |
65 | reg, count, src); | |
3e54a169 | 66 | |
e3ea5e94 | 67 | ret = regmap_bulk_write(state->regmap, reg, src, count); |
3e54a169 | 68 | |
e3ea5e94 | 69 | if (ret) |
aa155449 | 70 | dev_err(&state->client->dev, "%s: ret == %d\n", __func__, ret); |
3e54a169 | 71 | |
e3ea5e94 | 72 | return ret; |
3e54a169 MS |
73 | } |
74 | ||
75 | static int si2165_read(struct si2165_state *state, | |
76 | const u16 reg, u8 *val, const int count) | |
77 | { | |
e3ea5e94 | 78 | int ret = regmap_bulk_read(state->regmap, reg, val, count); |
3e54a169 | 79 | |
e3ea5e94 | 80 | if (ret) { |
aa155449 | 81 | dev_err(&state->client->dev, "%s: error (addr %02x reg %04x error (ret == %i)\n", |
3e54a169 | 82 | __func__, state->config.i2c_addr, reg, ret); |
e3ea5e94 | 83 | return ret; |
3e54a169 MS |
84 | } |
85 | ||
7dbbb4bf MS |
86 | dev_dbg(&state->client->dev, "i2c read: reg: 0x%04x, data: %*ph\n", |
87 | reg, count, val); | |
3e54a169 MS |
88 | |
89 | return 0; | |
90 | } | |
91 | ||
92 | static int si2165_readreg8(struct si2165_state *state, | |
7dbbb4bf | 93 | const u16 reg, u8 *val) |
3e54a169 | 94 | { |
e3ea5e94 MS |
95 | unsigned int val_tmp; |
96 | int ret = regmap_read(state->regmap, reg, &val_tmp); | |
97 | *val = (u8)val_tmp; | |
1b54da77 | 98 | dev_dbg(&state->client->dev, "reg read: R(0x%04x)=0x%02x\n", reg, *val); |
3e54a169 MS |
99 | return ret; |
100 | } | |
101 | ||
102 | static int si2165_readreg16(struct si2165_state *state, | |
7dbbb4bf | 103 | const u16 reg, u16 *val) |
3e54a169 MS |
104 | { |
105 | u8 buf[2]; | |
106 | ||
107 | int ret = si2165_read(state, reg, buf, 2); | |
108 | *val = buf[0] | buf[1] << 8; | |
1b54da77 | 109 | dev_dbg(&state->client->dev, "reg read: R(0x%04x)=0x%04x\n", reg, *val); |
3e54a169 MS |
110 | return ret; |
111 | } | |
112 | ||
c0675d0b MS |
113 | static int si2165_readreg24(struct si2165_state *state, |
114 | const u16 reg, u32 *val) | |
115 | { | |
116 | u8 buf[3]; | |
117 | ||
118 | int ret = si2165_read(state, reg, buf, 3); | |
119 | *val = buf[0] | buf[1] << 8 | buf[2] << 16; | |
120 | dev_dbg(&state->client->dev, "reg read: R(0x%04x)=0x%06x\n", reg, *val); | |
121 | return ret; | |
122 | } | |
123 | ||
3e54a169 MS |
124 | static int si2165_writereg8(struct si2165_state *state, const u16 reg, u8 val) |
125 | { | |
e3ea5e94 | 126 | return regmap_write(state->regmap, reg, val); |
3e54a169 MS |
127 | } |
128 | ||
129 | static int si2165_writereg16(struct si2165_state *state, const u16 reg, u16 val) | |
130 | { | |
131 | u8 buf[2] = { val & 0xff, (val >> 8) & 0xff }; | |
132 | ||
133 | return si2165_write(state, reg, buf, 2); | |
134 | } | |
135 | ||
136 | static int si2165_writereg24(struct si2165_state *state, const u16 reg, u32 val) | |
137 | { | |
138 | u8 buf[3] = { val & 0xff, (val >> 8) & 0xff, (val >> 16) & 0xff }; | |
139 | ||
140 | return si2165_write(state, reg, buf, 3); | |
141 | } | |
142 | ||
143 | static int si2165_writereg32(struct si2165_state *state, const u16 reg, u32 val) | |
144 | { | |
145 | u8 buf[4] = { | |
146 | val & 0xff, | |
147 | (val >> 8) & 0xff, | |
148 | (val >> 16) & 0xff, | |
149 | (val >> 24) & 0xff | |
150 | }; | |
151 | return si2165_write(state, reg, buf, 4); | |
152 | } | |
153 | ||
154 | static int si2165_writereg_mask8(struct si2165_state *state, const u16 reg, | |
155 | u8 val, u8 mask) | |
156 | { | |
3e54a169 | 157 | if (mask != 0xff) { |
c2e5c951 ME |
158 | u8 tmp; |
159 | int ret = si2165_readreg8(state, reg, &tmp); | |
160 | ||
3e54a169 | 161 | if (ret < 0) |
c2e5c951 | 162 | return ret; |
3e54a169 MS |
163 | |
164 | val &= mask; | |
165 | tmp &= ~mask; | |
166 | val |= tmp; | |
167 | } | |
c2e5c951 | 168 | return si2165_writereg8(state, reg, val); |
3e54a169 MS |
169 | } |
170 | ||
7dbbb4bf MS |
171 | #define REG16(reg, val) \ |
172 | { (reg), (val) & 0xff }, \ | |
173 | { (reg) + 1, (val) >> 8 & 0xff } | |
a5293dbd MS |
174 | struct si2165_reg_value_pair { |
175 | u16 reg; | |
176 | u8 val; | |
177 | }; | |
178 | ||
179 | static int si2165_write_reg_list(struct si2165_state *state, | |
180 | const struct si2165_reg_value_pair *regs, | |
181 | int count) | |
182 | { | |
183 | int i; | |
184 | int ret; | |
185 | ||
186 | for (i = 0; i < count; i++) { | |
187 | ret = si2165_writereg8(state, regs[i].reg, regs[i].val); | |
188 | if (ret < 0) | |
189 | return ret; | |
190 | } | |
191 | return 0; | |
192 | } | |
193 | ||
3e54a169 MS |
194 | static int si2165_get_tune_settings(struct dvb_frontend *fe, |
195 | struct dvb_frontend_tune_settings *s) | |
196 | { | |
197 | s->min_delay_ms = 1000; | |
198 | return 0; | |
199 | } | |
200 | ||
201 | static int si2165_init_pll(struct si2165_state *state) | |
202 | { | |
7dbbb4bf | 203 | u32 ref_freq_hz = state->config.ref_freq_hz; |
3e54a169 MS |
204 | u8 divr = 1; /* 1..7 */ |
205 | u8 divp = 1; /* only 1 or 4 */ | |
206 | u8 divn = 56; /* 1..63 */ | |
207 | u8 divm = 8; | |
208 | u8 divl = 12; | |
209 | u8 buf[4]; | |
210 | ||
18349f40 MS |
211 | /* |
212 | * hardcoded values can be deleted if calculation is verified | |
213 | * or it yields the same values as the windows driver | |
214 | */ | |
7dbbb4bf | 215 | switch (ref_freq_hz) { |
3e54a169 MS |
216 | case 16000000u: |
217 | divn = 56; | |
218 | break; | |
219 | case 24000000u: | |
220 | divr = 2; | |
221 | divp = 4; | |
222 | divn = 19; | |
223 | break; | |
224 | default: | |
225 | /* ref_freq / divr must be between 4 and 16 MHz */ | |
7dbbb4bf | 226 | if (ref_freq_hz > 16000000u) |
3e54a169 MS |
227 | divr = 2; |
228 | ||
18349f40 MS |
229 | /* |
230 | * now select divn and divp such that | |
231 | * fvco is in 1624..1824 MHz | |
232 | */ | |
7dbbb4bf | 233 | if (1624000000u * divr > ref_freq_hz * 2u * 63u) |
3e54a169 MS |
234 | divp = 4; |
235 | ||
236 | /* is this already correct regarding rounding? */ | |
7dbbb4bf | 237 | divn = 1624000000u * divr / (ref_freq_hz * 2u * divp); |
3e54a169 MS |
238 | break; |
239 | } | |
240 | ||
241 | /* adc_clk and sys_clk depend on xtal and pll settings */ | |
7dbbb4bf | 242 | state->fvco_hz = ref_freq_hz / divr |
3e54a169 MS |
243 | * 2u * divn * divp; |
244 | state->adc_clk = state->fvco_hz / (divm * 4u); | |
245 | state->sys_clk = state->fvco_hz / (divl * 2u); | |
246 | ||
814f86c9 | 247 | /* write all 4 pll registers 0x00a0..0x00a3 at once */ |
3e54a169 MS |
248 | buf[0] = divl; |
249 | buf[1] = divm; | |
250 | buf[2] = (divn & 0x3f) | ((divp == 1) ? 0x40 : 0x00) | 0x80; | |
251 | buf[3] = divr; | |
814f86c9 | 252 | return si2165_write(state, REG_PLL_DIVL, buf, 4); |
3e54a169 MS |
253 | } |
254 | ||
255 | static int si2165_adjust_pll_divl(struct si2165_state *state, u8 divl) | |
256 | { | |
257 | state->sys_clk = state->fvco_hz / (divl * 2u); | |
814f86c9 | 258 | return si2165_writereg8(state, REG_PLL_DIVL, divl); |
3e54a169 MS |
259 | } |
260 | ||
261 | static u32 si2165_get_fe_clk(struct si2165_state *state) | |
262 | { | |
263 | /* assume Oversampling mode Ovr4 is used */ | |
264 | return state->adc_clk; | |
265 | } | |
266 | ||
e73c7bfe | 267 | static int si2165_wait_init_done(struct si2165_state *state) |
3e54a169 | 268 | { |
0ab34a08 | 269 | int ret; |
3e54a169 MS |
270 | u8 val = 0; |
271 | int i; | |
272 | ||
273 | for (i = 0; i < 3; ++i) { | |
0ab34a08 KL |
274 | ret = si2165_readreg8(state, REG_INIT_DONE, &val); |
275 | if (ret < 0) | |
276 | return ret; | |
3e54a169 MS |
277 | if (val == 0x01) |
278 | return 0; | |
279 | usleep_range(1000, 50000); | |
280 | } | |
77f887cd | 281 | dev_err(&state->client->dev, "init_done was not set\n"); |
0ab34a08 | 282 | return -EINVAL; |
3e54a169 MS |
283 | } |
284 | ||
285 | static int si2165_upload_firmware_block(struct si2165_state *state, | |
7dbbb4bf MS |
286 | const u8 *data, u32 len, u32 *poffset, |
287 | u32 block_count) | |
3e54a169 MS |
288 | { |
289 | int ret; | |
290 | u8 buf_ctrl[4] = { 0x00, 0x00, 0x00, 0xc0 }; | |
291 | u8 wordcount; | |
292 | u32 cur_block = 0; | |
293 | u32 offset = poffset ? *poffset : 0; | |
294 | ||
295 | if (len < 4) | |
296 | return -EINVAL; | |
297 | if (len % 4 != 0) | |
298 | return -EINVAL; | |
299 | ||
1b54da77 | 300 | dev_dbg(&state->client->dev, |
7dbbb4bf MS |
301 | "fw load: %s: called with len=0x%x offset=0x%x blockcount=0x%x\n", |
302 | __func__, len, offset, block_count); | |
303 | while (offset + 12 <= len && cur_block < block_count) { | |
1b54da77 | 304 | dev_dbg(&state->client->dev, |
7dbbb4bf MS |
305 | "fw load: %s: in while len=0x%x offset=0x%x cur_block=0x%x blockcount=0x%x\n", |
306 | __func__, len, offset, cur_block, block_count); | |
3e54a169 | 307 | wordcount = data[offset]; |
7dbbb4bf MS |
308 | if (wordcount < 1 || data[offset + 1] || |
309 | data[offset + 2] || data[offset + 3]) { | |
aa155449 | 310 | dev_warn(&state->client->dev, |
77f887cd MS |
311 | "bad fw data[0..3] = %*ph\n", |
312 | 4, data); | |
3e54a169 MS |
313 | return -EINVAL; |
314 | } | |
315 | ||
316 | if (offset + 8 + wordcount * 4 > len) { | |
aa155449 | 317 | dev_warn(&state->client->dev, |
77f887cd MS |
318 | "len is too small for block len=%d, wordcount=%d\n", |
319 | len, wordcount); | |
3e54a169 MS |
320 | return -EINVAL; |
321 | } | |
322 | ||
323 | buf_ctrl[0] = wordcount - 1; | |
324 | ||
814f86c9 | 325 | ret = si2165_write(state, REG_DCOM_CONTROL_BYTE, buf_ctrl, 4); |
3e54a169 MS |
326 | if (ret < 0) |
327 | goto error; | |
814f86c9 | 328 | ret = si2165_write(state, REG_DCOM_ADDR, data + offset + 4, 4); |
3e54a169 MS |
329 | if (ret < 0) |
330 | goto error; | |
331 | ||
332 | offset += 8; | |
333 | ||
334 | while (wordcount > 0) { | |
814f86c9 MS |
335 | ret = si2165_write(state, REG_DCOM_DATA, |
336 | data + offset, 4); | |
3e54a169 MS |
337 | if (ret < 0) |
338 | goto error; | |
339 | wordcount--; | |
340 | offset += 4; | |
341 | } | |
342 | cur_block++; | |
343 | } | |
344 | ||
1b54da77 | 345 | dev_dbg(&state->client->dev, |
7dbbb4bf MS |
346 | "fw load: %s: after while len=0x%x offset=0x%x cur_block=0x%x blockcount=0x%x\n", |
347 | __func__, len, offset, cur_block, block_count); | |
3e54a169 MS |
348 | |
349 | if (poffset) | |
350 | *poffset = offset; | |
351 | ||
1b54da77 | 352 | dev_dbg(&state->client->dev, |
7dbbb4bf MS |
353 | "fw load: %s: returned offset=0x%x\n", |
354 | __func__, offset); | |
3e54a169 MS |
355 | |
356 | return 0; | |
357 | error: | |
358 | return ret; | |
359 | } | |
360 | ||
361 | static int si2165_upload_firmware(struct si2165_state *state) | |
362 | { | |
363 | /* int ret; */ | |
364 | u8 val[3]; | |
365 | u16 val16; | |
366 | int ret; | |
367 | ||
368 | const struct firmware *fw = NULL; | |
55bea400 | 369 | u8 *fw_file; |
3e54a169 MS |
370 | const u8 *data; |
371 | u32 len; | |
372 | u32 offset; | |
373 | u8 patch_version; | |
374 | u8 block_count; | |
375 | u16 crc_expected; | |
376 | ||
55bea400 MS |
377 | switch (state->chip_revcode) { |
378 | case 0x03: /* revision D */ | |
379 | fw_file = SI2165_FIRMWARE_REV_D; | |
380 | break; | |
381 | default: | |
77f887cd | 382 | dev_info(&state->client->dev, "no firmware file for revision=%d\n", |
7dbbb4bf | 383 | state->chip_revcode); |
55bea400 MS |
384 | return 0; |
385 | } | |
386 | ||
3e54a169 | 387 | /* request the firmware, this will block and timeout */ |
aa155449 | 388 | ret = request_firmware(&fw, fw_file, &state->client->dev); |
3e54a169 | 389 | if (ret) { |
77f887cd | 390 | dev_warn(&state->client->dev, "firmware file '%s' not found\n", |
7dbbb4bf | 391 | fw_file); |
3e54a169 MS |
392 | goto error; |
393 | } | |
394 | ||
395 | data = fw->data; | |
396 | len = fw->size; | |
397 | ||
77f887cd | 398 | dev_info(&state->client->dev, "downloading firmware from file '%s' size=%d\n", |
7dbbb4bf | 399 | fw_file, len); |
3e54a169 MS |
400 | |
401 | if (len % 4 != 0) { | |
77f887cd | 402 | dev_warn(&state->client->dev, "firmware size is not multiple of 4\n"); |
3e54a169 MS |
403 | ret = -EINVAL; |
404 | goto error; | |
405 | } | |
406 | ||
407 | /* check header (8 bytes) */ | |
408 | if (len < 8) { | |
77f887cd | 409 | dev_warn(&state->client->dev, "firmware header is missing\n"); |
3e54a169 MS |
410 | ret = -EINVAL; |
411 | goto error; | |
412 | } | |
413 | ||
414 | if (data[0] != 1 || data[1] != 0) { | |
77f887cd | 415 | dev_warn(&state->client->dev, "firmware file version is wrong\n"); |
3e54a169 MS |
416 | ret = -EINVAL; |
417 | goto error; | |
418 | } | |
419 | ||
420 | patch_version = data[2]; | |
421 | block_count = data[4]; | |
422 | crc_expected = data[7] << 8 | data[6]; | |
423 | ||
424 | /* start uploading fw */ | |
425 | /* boot/wdog status */ | |
814f86c9 | 426 | ret = si2165_writereg8(state, REG_WDOG_AND_BOOT, 0x00); |
3e54a169 MS |
427 | if (ret < 0) |
428 | goto error; | |
429 | /* reset */ | |
814f86c9 | 430 | ret = si2165_writereg8(state, REG_RST_ALL, 0x00); |
3e54a169 MS |
431 | if (ret < 0) |
432 | goto error; | |
433 | /* boot/wdog status */ | |
814f86c9 | 434 | ret = si2165_readreg8(state, REG_WDOG_AND_BOOT, val); |
3e54a169 MS |
435 | if (ret < 0) |
436 | goto error; | |
437 | ||
438 | /* enable reset on error */ | |
814f86c9 | 439 | ret = si2165_readreg8(state, REG_EN_RST_ERROR, val); |
3e54a169 MS |
440 | if (ret < 0) |
441 | goto error; | |
814f86c9 | 442 | ret = si2165_readreg8(state, REG_EN_RST_ERROR, val); |
3e54a169 MS |
443 | if (ret < 0) |
444 | goto error; | |
814f86c9 | 445 | ret = si2165_writereg8(state, REG_EN_RST_ERROR, 0x02); |
3e54a169 MS |
446 | if (ret < 0) |
447 | goto error; | |
448 | ||
449 | /* start right after the header */ | |
450 | offset = 8; | |
451 | ||
7dbbb4bf MS |
452 | dev_info(&state->client->dev, "%s: extracted patch_version=0x%02x, block_count=0x%02x, crc_expected=0x%04x\n", |
453 | __func__, patch_version, block_count, crc_expected); | |
3e54a169 MS |
454 | |
455 | ret = si2165_upload_firmware_block(state, data, len, &offset, 1); | |
456 | if (ret < 0) | |
457 | goto error; | |
458 | ||
814f86c9 | 459 | ret = si2165_writereg8(state, REG_PATCH_VERSION, patch_version); |
3e54a169 MS |
460 | if (ret < 0) |
461 | goto error; | |
462 | ||
463 | /* reset crc */ | |
814f86c9 | 464 | ret = si2165_writereg8(state, REG_RST_CRC, 0x01); |
3e54a169 | 465 | if (ret) |
ec73b9fd | 466 | goto error; |
3e54a169 MS |
467 | |
468 | ret = si2165_upload_firmware_block(state, data, len, | |
469 | &offset, block_count); | |
470 | if (ret < 0) { | |
aa155449 | 471 | dev_err(&state->client->dev, |
77f887cd | 472 | "firmware could not be uploaded\n"); |
3e54a169 MS |
473 | goto error; |
474 | } | |
475 | ||
476 | /* read crc */ | |
814f86c9 | 477 | ret = si2165_readreg16(state, REG_CRC, &val16); |
3e54a169 MS |
478 | if (ret) |
479 | goto error; | |
480 | ||
481 | if (val16 != crc_expected) { | |
aa155449 | 482 | dev_err(&state->client->dev, |
77f887cd MS |
483 | "firmware crc mismatch %04x != %04x\n", |
484 | val16, crc_expected); | |
3e54a169 MS |
485 | ret = -EINVAL; |
486 | goto error; | |
487 | } | |
488 | ||
489 | ret = si2165_upload_firmware_block(state, data, len, &offset, 5); | |
490 | if (ret) | |
491 | goto error; | |
492 | ||
493 | if (len != offset) { | |
aa155449 | 494 | dev_err(&state->client->dev, |
77f887cd MS |
495 | "firmware len mismatch %04x != %04x\n", |
496 | len, offset); | |
3e54a169 MS |
497 | ret = -EINVAL; |
498 | goto error; | |
499 | } | |
500 | ||
501 | /* reset watchdog error register */ | |
814f86c9 | 502 | ret = si2165_writereg_mask8(state, REG_WDOG_AND_BOOT, 0x02, 0x02); |
3e54a169 MS |
503 | if (ret < 0) |
504 | goto error; | |
505 | ||
506 | /* enable reset on error */ | |
814f86c9 | 507 | ret = si2165_writereg_mask8(state, REG_EN_RST_ERROR, 0x01, 0x01); |
3e54a169 MS |
508 | if (ret < 0) |
509 | goto error; | |
510 | ||
77f887cd | 511 | dev_info(&state->client->dev, "fw load finished\n"); |
3e54a169 MS |
512 | |
513 | ret = 0; | |
514 | state->firmware_loaded = true; | |
515 | error: | |
cefc10d0 MC |
516 | release_firmware(fw); |
517 | fw = NULL; | |
3e54a169 MS |
518 | |
519 | return ret; | |
520 | } | |
521 | ||
522 | static int si2165_init(struct dvb_frontend *fe) | |
523 | { | |
524 | int ret = 0; | |
525 | struct si2165_state *state = fe->demodulator_priv; | |
c0675d0b | 526 | struct dtv_frontend_properties *c = &fe->dtv_property_cache; |
3e54a169 MS |
527 | u8 val; |
528 | u8 patch_version = 0x00; | |
529 | ||
1b54da77 | 530 | dev_dbg(&state->client->dev, "%s: called\n", __func__); |
3e54a169 MS |
531 | |
532 | /* powerup */ | |
814f86c9 | 533 | ret = si2165_writereg8(state, REG_CHIP_MODE, state->config.chip_mode); |
3e54a169 MS |
534 | if (ret < 0) |
535 | goto error; | |
536 | /* dsp_clock_enable */ | |
814f86c9 | 537 | ret = si2165_writereg8(state, REG_DSP_CLOCK, 0x01); |
3e54a169 MS |
538 | if (ret < 0) |
539 | goto error; | |
814f86c9 MS |
540 | /* verify chip_mode */ |
541 | ret = si2165_readreg8(state, REG_CHIP_MODE, &val); | |
3e54a169 MS |
542 | if (ret < 0) |
543 | goto error; | |
544 | if (val != state->config.chip_mode) { | |
77f887cd | 545 | dev_err(&state->client->dev, "could not set chip_mode\n"); |
3e54a169 MS |
546 | return -EINVAL; |
547 | } | |
548 | ||
549 | /* agc */ | |
814f86c9 | 550 | ret = si2165_writereg8(state, REG_AGC_IF_TRI, 0x00); |
3e54a169 MS |
551 | if (ret < 0) |
552 | goto error; | |
814f86c9 | 553 | ret = si2165_writereg8(state, REG_AGC_IF_SLR, 0x01); |
3e54a169 MS |
554 | if (ret < 0) |
555 | goto error; | |
814f86c9 | 556 | ret = si2165_writereg8(state, REG_AGC2_OUTPUT, 0x00); |
3e54a169 MS |
557 | if (ret < 0) |
558 | goto error; | |
814f86c9 | 559 | ret = si2165_writereg8(state, REG_AGC2_CLKDIV, 0x07); |
3e54a169 MS |
560 | if (ret < 0) |
561 | goto error; | |
562 | /* rssi pad */ | |
814f86c9 | 563 | ret = si2165_writereg8(state, REG_RSSI_PAD_CTRL, 0x00); |
3e54a169 MS |
564 | if (ret < 0) |
565 | goto error; | |
814f86c9 | 566 | ret = si2165_writereg8(state, REG_RSSI_ENABLE, 0x00); |
3e54a169 MS |
567 | if (ret < 0) |
568 | goto error; | |
569 | ||
570 | ret = si2165_init_pll(state); | |
571 | if (ret < 0) | |
572 | goto error; | |
573 | ||
574 | /* enable chip_init */ | |
814f86c9 | 575 | ret = si2165_writereg8(state, REG_CHIP_INIT, 0x01); |
3e54a169 MS |
576 | if (ret < 0) |
577 | goto error; | |
578 | /* set start_init */ | |
814f86c9 | 579 | ret = si2165_writereg8(state, REG_START_INIT, 0x01); |
3e54a169 MS |
580 | if (ret < 0) |
581 | goto error; | |
582 | ret = si2165_wait_init_done(state); | |
583 | if (ret < 0) | |
584 | goto error; | |
585 | ||
586 | /* disable chip_init */ | |
814f86c9 | 587 | ret = si2165_writereg8(state, REG_CHIP_INIT, 0x00); |
3e54a169 MS |
588 | if (ret < 0) |
589 | goto error; | |
590 | ||
964b3727 MS |
591 | /* ber_pkt - default 65535 */ |
592 | ret = si2165_writereg16(state, REG_BER_PKT, | |
593 | STATISTICS_PERIOD_PKT_COUNT); | |
3e54a169 MS |
594 | if (ret < 0) |
595 | goto error; | |
596 | ||
814f86c9 | 597 | ret = si2165_readreg8(state, REG_PATCH_VERSION, &patch_version); |
3e54a169 MS |
598 | if (ret < 0) |
599 | goto error; | |
600 | ||
814f86c9 | 601 | ret = si2165_writereg8(state, REG_AUTO_RESET, 0x00); |
3e54a169 MS |
602 | if (ret < 0) |
603 | goto error; | |
604 | ||
605 | /* dsp_addr_jump */ | |
814f86c9 | 606 | ret = si2165_writereg32(state, REG_ADDR_JUMP, 0xf4000000); |
3e54a169 MS |
607 | if (ret < 0) |
608 | goto error; | |
609 | /* boot/wdog status */ | |
814f86c9 | 610 | ret = si2165_readreg8(state, REG_WDOG_AND_BOOT, &val); |
3e54a169 MS |
611 | if (ret < 0) |
612 | goto error; | |
613 | ||
614 | if (patch_version == 0x00) { | |
615 | ret = si2165_upload_firmware(state); | |
616 | if (ret < 0) | |
617 | goto error; | |
618 | } | |
619 | ||
75d62fc0 | 620 | /* ts output config */ |
814f86c9 | 621 | ret = si2165_writereg8(state, REG_TS_DATA_MODE, 0x20); |
75d62fc0 MS |
622 | if (ret < 0) |
623 | return ret; | |
814f86c9 | 624 | ret = si2165_writereg16(state, REG_TS_TRI, 0x00fe); |
75d62fc0 MS |
625 | if (ret < 0) |
626 | return ret; | |
814f86c9 | 627 | ret = si2165_writereg24(state, REG_TS_SLR, 0x555555); |
75d62fc0 MS |
628 | if (ret < 0) |
629 | return ret; | |
814f86c9 | 630 | ret = si2165_writereg8(state, REG_TS_CLK_MODE, 0x01); |
ec278f87 MS |
631 | if (ret < 0) |
632 | return ret; | |
633 | ret = si2165_writereg8(state, REG_TS_PARALLEL_MODE, 0x00); | |
75d62fc0 MS |
634 | if (ret < 0) |
635 | return ret; | |
636 | ||
c0675d0b MS |
637 | c = &state->fe.dtv_property_cache; |
638 | c->cnr.len = 1; | |
639 | c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE; | |
964b3727 MS |
640 | c->post_bit_error.len = 1; |
641 | c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE; | |
642 | c->post_bit_count.len = 1; | |
643 | c->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE; | |
c0675d0b | 644 | |
3e54a169 MS |
645 | return 0; |
646 | error: | |
647 | return ret; | |
648 | } | |
649 | ||
650 | static int si2165_sleep(struct dvb_frontend *fe) | |
651 | { | |
652 | int ret; | |
653 | struct si2165_state *state = fe->demodulator_priv; | |
654 | ||
655 | /* dsp clock disable */ | |
814f86c9 | 656 | ret = si2165_writereg8(state, REG_DSP_CLOCK, 0x00); |
3e54a169 MS |
657 | if (ret < 0) |
658 | return ret; | |
659 | /* chip mode */ | |
814f86c9 | 660 | ret = si2165_writereg8(state, REG_CHIP_MODE, SI2165_MODE_OFF); |
3e54a169 MS |
661 | if (ret < 0) |
662 | return ret; | |
663 | return 0; | |
664 | } | |
665 | ||
0df289a2 | 666 | static int si2165_read_status(struct dvb_frontend *fe, enum fe_status *status) |
3e54a169 MS |
667 | { |
668 | int ret; | |
1e5fde1b | 669 | u8 u8tmp; |
c0675d0b | 670 | u32 u32tmp; |
3e54a169 | 671 | struct si2165_state *state = fe->demodulator_priv; |
c0675d0b MS |
672 | struct dtv_frontend_properties *c = &fe->dtv_property_cache; |
673 | u32 delsys = c->delivery_system; | |
3e54a169 | 674 | |
1e5fde1b MS |
675 | *status = 0; |
676 | ||
677 | switch (delsys) { | |
678 | case SYS_DVBT: | |
679 | /* check fast signal type */ | |
680 | ret = si2165_readreg8(state, REG_CHECK_SIGNAL, &u8tmp); | |
681 | if (ret < 0) | |
682 | return ret; | |
683 | switch (u8tmp & 0x3) { | |
684 | case 0: /* searching */ | |
685 | case 1: /* nothing */ | |
686 | break; | |
687 | case 2: /* digital signal */ | |
688 | *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER; | |
689 | break; | |
690 | } | |
691 | break; | |
692 | case SYS_DVBC_ANNEX_A: | |
693 | /* check packet sync lock */ | |
694 | ret = si2165_readreg8(state, REG_PS_LOCK, &u8tmp); | |
695 | if (ret < 0) | |
696 | return ret; | |
697 | if (u8tmp & 0x01) { | |
698 | *status |= FE_HAS_SIGNAL; | |
699 | *status |= FE_HAS_CARRIER; | |
700 | *status |= FE_HAS_VITERBI; | |
701 | *status |= FE_HAS_SYNC; | |
702 | } | |
703 | break; | |
704 | } | |
3e54a169 MS |
705 | |
706 | /* check fec_lock */ | |
1e5fde1b | 707 | ret = si2165_readreg8(state, REG_FEC_LOCK, &u8tmp); |
3e54a169 MS |
708 | if (ret < 0) |
709 | return ret; | |
1e5fde1b | 710 | if (u8tmp & 0x01) { |
3e54a169 MS |
711 | *status |= FE_HAS_SIGNAL; |
712 | *status |= FE_HAS_CARRIER; | |
713 | *status |= FE_HAS_VITERBI; | |
714 | *status |= FE_HAS_SYNC; | |
715 | *status |= FE_HAS_LOCK; | |
716 | } | |
717 | ||
c0675d0b MS |
718 | /* CNR */ |
719 | if (delsys == SYS_DVBC_ANNEX_A && *status & FE_HAS_VITERBI) { | |
720 | ret = si2165_readreg24(state, REG_C_N, &u32tmp); | |
721 | if (ret < 0) | |
722 | return ret; | |
723 | /* | |
724 | * svalue = | |
725 | * 1000 * c_n/dB = | |
726 | * 1000 * 10 * log10(2^24 / regval) = | |
727 | * 1000 * 10 * (log10(2^24) - log10(regval)) = | |
728 | * 1000 * 10 * (intlog10(2^24) - intlog10(regval)) / 2^24 | |
729 | * | |
730 | * intlog10(x) = log10(x) * 2^24 | |
731 | * intlog10(2^24) = log10(2^24) * 2^24 = 121210686 | |
732 | */ | |
733 | u32tmp = (1000 * 10 * (121210686 - (u64)intlog10(u32tmp))) | |
734 | >> 24; | |
735 | c->cnr.stat[0].scale = FE_SCALE_DECIBEL; | |
736 | c->cnr.stat[0].svalue = u32tmp; | |
737 | } else | |
738 | c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE; | |
739 | ||
964b3727 MS |
740 | /* BER */ |
741 | if (*status & FE_HAS_VITERBI) { | |
742 | if (c->post_bit_error.stat[0].scale == FE_SCALE_NOT_AVAILABLE) { | |
743 | /* start new sampling period to get rid of old data*/ | |
744 | ret = si2165_writereg8(state, REG_BER_RST, 0x01); | |
745 | if (ret < 0) | |
746 | return ret; | |
747 | ||
748 | /* set scale to enter read code on next call */ | |
749 | c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER; | |
750 | c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER; | |
751 | c->post_bit_error.stat[0].uvalue = 0; | |
752 | c->post_bit_count.stat[0].uvalue = 0; | |
753 | ||
e9c7d19a MS |
754 | /* |
755 | * reset DVBv3 value to deliver a good result | |
756 | * for the first call | |
757 | */ | |
758 | state->ber_prev = 0; | |
759 | ||
964b3727 MS |
760 | } else { |
761 | ret = si2165_readreg8(state, REG_BER_AVAIL, &u8tmp); | |
762 | if (ret < 0) | |
763 | return ret; | |
764 | ||
765 | if (u8tmp & 1) { | |
766 | u32 biterrcnt; | |
767 | ||
768 | ret = si2165_readreg24(state, REG_BER_BIT, | |
769 | &biterrcnt); | |
770 | if (ret < 0) | |
771 | return ret; | |
772 | ||
773 | c->post_bit_error.stat[0].uvalue += | |
774 | biterrcnt; | |
775 | c->post_bit_count.stat[0].uvalue += | |
776 | STATISTICS_PERIOD_BIT_COUNT; | |
777 | ||
778 | /* start new sampling period */ | |
779 | ret = si2165_writereg8(state, | |
780 | REG_BER_RST, 0x01); | |
781 | if (ret < 0) | |
782 | return ret; | |
783 | ||
784 | dev_dbg(&state->client->dev, | |
785 | "post_bit_error=%u post_bit_count=%u\n", | |
786 | biterrcnt, STATISTICS_PERIOD_BIT_COUNT); | |
787 | } | |
788 | } | |
789 | } else { | |
790 | c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE; | |
791 | c->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE; | |
792 | } | |
793 | ||
3e54a169 MS |
794 | return 0; |
795 | } | |
796 | ||
2e687a6d MS |
797 | static int si2165_read_snr(struct dvb_frontend *fe, u16 *snr) |
798 | { | |
799 | struct dtv_frontend_properties *c = &fe->dtv_property_cache; | |
800 | ||
801 | if (c->cnr.stat[0].scale == FE_SCALE_DECIBEL) | |
802 | *snr = div_s64(c->cnr.stat[0].svalue, 100); | |
803 | else | |
804 | *snr = 0; | |
805 | return 0; | |
806 | } | |
807 | ||
e9c7d19a MS |
808 | static int si2165_read_ber(struct dvb_frontend *fe, u32 *ber) |
809 | { | |
810 | struct si2165_state *state = fe->demodulator_priv; | |
811 | struct dtv_frontend_properties *c = &fe->dtv_property_cache; | |
812 | ||
813 | if (c->post_bit_error.stat[0].scale != FE_SCALE_COUNTER) { | |
814 | *ber = 0; | |
815 | return 0; | |
816 | } | |
817 | ||
818 | *ber = c->post_bit_error.stat[0].uvalue - state->ber_prev; | |
819 | state->ber_prev = c->post_bit_error.stat[0].uvalue; | |
820 | ||
821 | return 0; | |
822 | } | |
823 | ||
3e54a169 MS |
824 | static int si2165_set_oversamp(struct si2165_state *state, u32 dvb_rate) |
825 | { | |
826 | u64 oversamp; | |
827 | u32 reg_value; | |
828 | ||
2df9dda0 MS |
829 | if (!dvb_rate) |
830 | return -EINVAL; | |
831 | ||
3e54a169 MS |
832 | oversamp = si2165_get_fe_clk(state); |
833 | oversamp <<= 23; | |
834 | do_div(oversamp, dvb_rate); | |
835 | reg_value = oversamp & 0x3fffffff; | |
836 | ||
1b54da77 | 837 | dev_dbg(&state->client->dev, "Write oversamp=%#x\n", reg_value); |
814f86c9 | 838 | return si2165_writereg32(state, REG_OVERSAMP, reg_value); |
3e54a169 MS |
839 | } |
840 | ||
542fb3c5 | 841 | static int si2165_set_if_freq_shift(struct si2165_state *state) |
3e54a169 | 842 | { |
542fb3c5 | 843 | struct dvb_frontend *fe = &state->fe; |
3e54a169 MS |
844 | u64 if_freq_shift; |
845 | s32 reg_value = 0; | |
846 | u32 fe_clk = si2165_get_fe_clk(state); | |
542fb3c5 | 847 | u32 IF = 0; |
3e54a169 | 848 | |
542fb3c5 | 849 | if (!fe->ops.tuner_ops.get_if_frequency) { |
aa155449 | 850 | dev_err(&state->client->dev, |
77f887cd | 851 | "Error: get_if_frequency() not defined at tuner. Can't work without it!\n"); |
542fb3c5 MS |
852 | return -EINVAL; |
853 | } | |
854 | ||
2df9dda0 MS |
855 | if (!fe_clk) |
856 | return -EINVAL; | |
857 | ||
542fb3c5 | 858 | fe->ops.tuner_ops.get_if_frequency(fe, &IF); |
3e54a169 MS |
859 | if_freq_shift = IF; |
860 | if_freq_shift <<= 29; | |
861 | ||
862 | do_div(if_freq_shift, fe_clk); | |
863 | reg_value = (s32)if_freq_shift; | |
864 | ||
865 | if (state->config.inversion) | |
866 | reg_value = -reg_value; | |
867 | ||
868 | reg_value = reg_value & 0x1fffffff; | |
869 | ||
870 | /* if_freq_shift, usbdump contained 0x023ee08f; */ | |
814f86c9 | 871 | return si2165_writereg32(state, REG_IF_FREQ_SHIFT, reg_value); |
3e54a169 MS |
872 | } |
873 | ||
25e73753 MS |
874 | static const struct si2165_reg_value_pair dvbt_regs[] = { |
875 | /* standard = DVB-T */ | |
814f86c9 | 876 | { REG_DVB_STANDARD, 0x01 }, |
25e73753 | 877 | /* impulsive_noise_remover */ |
814f86c9 MS |
878 | { REG_IMPULSIVE_NOISE_REM, 0x01 }, |
879 | { REG_AUTO_RESET, 0x00 }, | |
25e73753 | 880 | /* agc2 */ |
814f86c9 MS |
881 | { REG_AGC2_MIN, 0x41 }, |
882 | { REG_AGC2_KACQ, 0x0e }, | |
883 | { REG_AGC2_KLOC, 0x10 }, | |
25e73753 | 884 | /* agc */ |
814f86c9 MS |
885 | { REG_AGC_UNFREEZE_THR, 0x03 }, |
886 | { REG_AGC_CRESTF_DBX8, 0x78 }, | |
25e73753 | 887 | /* agc */ |
814f86c9 MS |
888 | { REG_AAF_CRESTF_DBX8, 0x78 }, |
889 | { REG_ACI_CRESTF_DBX8, 0x68 }, | |
25e73753 | 890 | /* freq_sync_range */ |
814f86c9 | 891 | REG16(REG_FREQ_SYNC_RANGE, 0x0064), |
25e73753 | 892 | /* gp_reg0 */ |
814f86c9 | 893 | { REG_GP_REG0_MSB, 0x00 } |
25e73753 MS |
894 | }; |
895 | ||
3b0c9807 | 896 | static int si2165_set_frontend_dvbt(struct dvb_frontend *fe) |
3e54a169 MS |
897 | { |
898 | int ret; | |
899 | struct dtv_frontend_properties *p = &fe->dtv_property_cache; | |
900 | struct si2165_state *state = fe->demodulator_priv; | |
3e54a169 MS |
901 | u32 dvb_rate = 0; |
902 | u16 bw10k; | |
7655a3ae | 903 | u32 bw_hz = p->bandwidth_hz; |
3e54a169 | 904 | |
1b54da77 | 905 | dev_dbg(&state->client->dev, "%s: called\n", __func__); |
3e54a169 | 906 | |
3e54a169 MS |
907 | if (!state->has_dvbt) |
908 | return -EINVAL; | |
909 | ||
7655a3ae MS |
910 | /* no bandwidth auto-detection */ |
911 | if (bw_hz == 0) | |
912 | return -EINVAL; | |
913 | ||
914 | dvb_rate = bw_hz * 8 / 7; | |
915 | bw10k = bw_hz / 10000; | |
3e54a169 | 916 | |
3e54a169 MS |
917 | ret = si2165_adjust_pll_divl(state, 12); |
918 | if (ret < 0) | |
919 | return ret; | |
920 | ||
3e54a169 | 921 | /* bandwidth in 10KHz steps */ |
814f86c9 | 922 | ret = si2165_writereg16(state, REG_T_BANDWIDTH, bw10k); |
3e54a169 MS |
923 | if (ret < 0) |
924 | return ret; | |
925 | ret = si2165_set_oversamp(state, dvb_rate); | |
926 | if (ret < 0) | |
927 | return ret; | |
25e73753 MS |
928 | |
929 | ret = si2165_write_reg_list(state, dvbt_regs, ARRAY_SIZE(dvbt_regs)); | |
3e54a169 MS |
930 | if (ret < 0) |
931 | return ret; | |
25e73753 | 932 | |
3b0c9807 MS |
933 | return 0; |
934 | } | |
935 | ||
94c17334 MS |
936 | static const struct si2165_reg_value_pair dvbc_regs[] = { |
937 | /* standard = DVB-C */ | |
814f86c9 | 938 | { REG_DVB_STANDARD, 0x05 }, |
94c17334 MS |
939 | |
940 | /* agc2 */ | |
814f86c9 MS |
941 | { REG_AGC2_MIN, 0x50 }, |
942 | { REG_AGC2_KACQ, 0x0e }, | |
943 | { REG_AGC2_KLOC, 0x10 }, | |
94c17334 | 944 | /* agc */ |
814f86c9 MS |
945 | { REG_AGC_UNFREEZE_THR, 0x03 }, |
946 | { REG_AGC_CRESTF_DBX8, 0x68 }, | |
94c17334 | 947 | /* agc */ |
814f86c9 MS |
948 | { REG_AAF_CRESTF_DBX8, 0x68 }, |
949 | { REG_ACI_CRESTF_DBX8, 0x50 }, | |
950 | ||
951 | { REG_EQ_AUTO_CONTROL, 0x0d }, | |
952 | ||
953 | { REG_KP_LOCK, 0x05 }, | |
954 | { REG_CENTRAL_TAP, 0x09 }, | |
955 | REG16(REG_UNKNOWN_350, 0x3e80), | |
814f86c9 MS |
956 | |
957 | { REG_AUTO_RESET, 0x01 }, | |
958 | REG16(REG_UNKNOWN_24C, 0x0000), | |
959 | REG16(REG_UNKNOWN_27C, 0x0000), | |
960 | { REG_SWEEP_STEP, 0x03 }, | |
814f86c9 | 961 | { REG_AGC_IF_TRI, 0x00 }, |
94c17334 MS |
962 | }; |
963 | ||
964 | static int si2165_set_frontend_dvbc(struct dvb_frontend *fe) | |
965 | { | |
966 | struct si2165_state *state = fe->demodulator_priv; | |
967 | int ret; | |
968 | struct dtv_frontend_properties *p = &fe->dtv_property_cache; | |
969 | const u32 dvb_rate = p->symbol_rate; | |
548b1f94 | 970 | u8 u8tmp; |
94c17334 MS |
971 | |
972 | if (!state->has_dvbc) | |
973 | return -EINVAL; | |
974 | ||
975 | if (dvb_rate == 0) | |
976 | return -EINVAL; | |
977 | ||
978 | ret = si2165_adjust_pll_divl(state, 14); | |
979 | if (ret < 0) | |
980 | return ret; | |
981 | ||
982 | /* Oversampling */ | |
983 | ret = si2165_set_oversamp(state, dvb_rate); | |
984 | if (ret < 0) | |
985 | return ret; | |
986 | ||
548b1f94 MS |
987 | switch (p->modulation) { |
988 | case QPSK: | |
989 | u8tmp = 0x3; | |
990 | break; | |
991 | case QAM_16: | |
992 | u8tmp = 0x7; | |
993 | break; | |
994 | case QAM_32: | |
995 | u8tmp = 0x8; | |
996 | break; | |
997 | case QAM_64: | |
998 | u8tmp = 0x9; | |
999 | break; | |
1000 | case QAM_128: | |
1001 | u8tmp = 0xa; | |
1002 | break; | |
1003 | case QAM_256: | |
1004 | default: | |
1005 | u8tmp = 0xb; | |
1006 | break; | |
1007 | } | |
1008 | ret = si2165_writereg8(state, REG_REQ_CONSTELLATION, u8tmp); | |
1009 | if (ret < 0) | |
1010 | return ret; | |
1011 | ||
f4d90518 | 1012 | ret = si2165_writereg32(state, REG_LOCK_TIMEOUT, 0x007a1200); |
94c17334 MS |
1013 | if (ret < 0) |
1014 | return ret; | |
1015 | ||
1016 | ret = si2165_write_reg_list(state, dvbc_regs, ARRAY_SIZE(dvbc_regs)); | |
1017 | if (ret < 0) | |
1018 | return ret; | |
1019 | ||
1020 | return 0; | |
1021 | } | |
1022 | ||
814f86c9 MS |
1023 | static const struct si2165_reg_value_pair adc_rewrite[] = { |
1024 | { REG_ADC_RI1, 0x46 }, | |
1025 | { REG_ADC_RI3, 0x00 }, | |
1026 | { REG_ADC_RI5, 0x0a }, | |
1027 | { REG_ADC_RI6, 0xff }, | |
1028 | { REG_ADC_RI8, 0x70 } | |
3b0c9807 MS |
1029 | }; |
1030 | ||
1031 | static int si2165_set_frontend(struct dvb_frontend *fe) | |
1032 | { | |
1033 | struct si2165_state *state = fe->demodulator_priv; | |
1034 | struct dtv_frontend_properties *p = &fe->dtv_property_cache; | |
1035 | u32 delsys = p->delivery_system; | |
1036 | int ret; | |
1037 | u8 val[3]; | |
1038 | ||
1039 | /* initial setting of if freq shift */ | |
1040 | ret = si2165_set_if_freq_shift(state); | |
1041 | if (ret < 0) | |
1042 | return ret; | |
1043 | ||
1044 | switch (delsys) { | |
1045 | case SYS_DVBT: | |
1046 | ret = si2165_set_frontend_dvbt(fe); | |
1047 | if (ret < 0) | |
1048 | return ret; | |
1049 | break; | |
94c17334 MS |
1050 | case SYS_DVBC_ANNEX_A: |
1051 | ret = si2165_set_frontend_dvbc(fe); | |
1052 | if (ret < 0) | |
1053 | return ret; | |
1054 | break; | |
3b0c9807 MS |
1055 | default: |
1056 | return -EINVAL; | |
1057 | } | |
1058 | ||
3e54a169 | 1059 | /* dsp_addr_jump */ |
814f86c9 | 1060 | ret = si2165_writereg32(state, REG_ADDR_JUMP, 0xf4000000); |
3e54a169 MS |
1061 | if (ret < 0) |
1062 | return ret; | |
1063 | ||
1064 | if (fe->ops.tuner_ops.set_params) | |
1065 | fe->ops.tuner_ops.set_params(fe); | |
1066 | ||
1067 | /* recalc if_freq_shift if IF might has changed */ | |
542fb3c5 | 1068 | ret = si2165_set_if_freq_shift(state); |
3e54a169 MS |
1069 | if (ret < 0) |
1070 | return ret; | |
1071 | ||
1072 | /* boot/wdog status */ | |
814f86c9 | 1073 | ret = si2165_readreg8(state, REG_WDOG_AND_BOOT, val); |
3e54a169 MS |
1074 | if (ret < 0) |
1075 | return ret; | |
814f86c9 | 1076 | ret = si2165_writereg8(state, REG_WDOG_AND_BOOT, 0x00); |
3e54a169 MS |
1077 | if (ret < 0) |
1078 | return ret; | |
3b0c9807 | 1079 | |
3e54a169 | 1080 | /* reset all */ |
814f86c9 | 1081 | ret = si2165_writereg8(state, REG_RST_ALL, 0x00); |
3e54a169 MS |
1082 | if (ret < 0) |
1083 | return ret; | |
1084 | /* gp_reg0 */ | |
814f86c9 | 1085 | ret = si2165_writereg32(state, REG_GP_REG0_LSB, 0x00000000); |
3e54a169 MS |
1086 | if (ret < 0) |
1087 | return ret; | |
eae56684 MS |
1088 | |
1089 | /* write adc values after each reset*/ | |
814f86c9 MS |
1090 | ret = si2165_write_reg_list(state, adc_rewrite, |
1091 | ARRAY_SIZE(adc_rewrite)); | |
eae56684 MS |
1092 | if (ret < 0) |
1093 | return ret; | |
1094 | ||
3e54a169 | 1095 | /* start_synchro */ |
814f86c9 | 1096 | ret = si2165_writereg8(state, REG_START_SYNCHRO, 0x01); |
3e54a169 MS |
1097 | if (ret < 0) |
1098 | return ret; | |
1099 | /* boot/wdog status */ | |
814f86c9 | 1100 | ret = si2165_readreg8(state, REG_WDOG_AND_BOOT, val); |
3e54a169 MS |
1101 | if (ret < 0) |
1102 | return ret; | |
1103 | ||
1104 | return 0; | |
1105 | } | |
1106 | ||
bd336e63 | 1107 | static const struct dvb_frontend_ops si2165_ops = { |
3e54a169 | 1108 | .info = { |
119bd82e | 1109 | .name = "Silicon Labs ", |
94c17334 MS |
1110 | /* For DVB-C */ |
1111 | .symbol_rate_min = 1000000, | |
1112 | .symbol_rate_max = 7200000, | |
1113 | /* For DVB-T */ | |
f1b1eabf | 1114 | .frequency_stepsize_hz = 166667, |
94c17334 | 1115 | .caps = FE_CAN_FEC_1_2 | |
3e54a169 MS |
1116 | FE_CAN_FEC_2_3 | |
1117 | FE_CAN_FEC_3_4 | | |
1118 | FE_CAN_FEC_5_6 | | |
1119 | FE_CAN_FEC_7_8 | | |
1120 | FE_CAN_FEC_AUTO | | |
1121 | FE_CAN_QPSK | | |
1122 | FE_CAN_QAM_16 | | |
1123 | FE_CAN_QAM_32 | | |
1124 | FE_CAN_QAM_64 | | |
1125 | FE_CAN_QAM_128 | | |
1126 | FE_CAN_QAM_256 | | |
3e54a169 MS |
1127 | FE_CAN_GUARD_INTERVAL_AUTO | |
1128 | FE_CAN_HIERARCHY_AUTO | | |
1129 | FE_CAN_MUTE_TS | | |
1130 | FE_CAN_TRANSMISSION_MODE_AUTO | | |
1131 | FE_CAN_RECOVER | |
1132 | }, | |
1133 | ||
1134 | .get_tune_settings = si2165_get_tune_settings, | |
1135 | ||
1136 | .init = si2165_init, | |
1137 | .sleep = si2165_sleep, | |
1138 | ||
c1c49674 | 1139 | .set_frontend = si2165_set_frontend, |
3e54a169 | 1140 | .read_status = si2165_read_status, |
2e687a6d | 1141 | .read_snr = si2165_read_snr, |
e9c7d19a | 1142 | .read_ber = si2165_read_ber, |
3e54a169 MS |
1143 | }; |
1144 | ||
3be25b9e | 1145 | static int si2165_probe(struct i2c_client *client) |
7cd785ad MS |
1146 | { |
1147 | struct si2165_state *state = NULL; | |
1148 | struct si2165_platform_data *pdata = client->dev.platform_data; | |
1149 | int n; | |
1150 | int ret = 0; | |
1151 | u8 val; | |
1152 | char rev_char; | |
1153 | const char *chip_name; | |
e3ea5e94 MS |
1154 | static const struct regmap_config regmap_config = { |
1155 | .reg_bits = 16, | |
1156 | .val_bits = 8, | |
1157 | .max_register = 0x08ff, | |
1158 | }; | |
7cd785ad MS |
1159 | |
1160 | /* allocate memory for the internal state */ | |
7dbbb4bf MS |
1161 | state = kzalloc(sizeof(*state), GFP_KERNEL); |
1162 | if (!state) { | |
7cd785ad MS |
1163 | ret = -ENOMEM; |
1164 | goto error; | |
1165 | } | |
1166 | ||
e3ea5e94 MS |
1167 | /* create regmap */ |
1168 | state->regmap = devm_regmap_init_i2c(client, ®map_config); | |
1169 | if (IS_ERR(state->regmap)) { | |
1170 | ret = PTR_ERR(state->regmap); | |
1171 | goto error; | |
1172 | } | |
1173 | ||
7cd785ad MS |
1174 | /* setup the state */ |
1175 | state->client = client; | |
7cd785ad MS |
1176 | state->config.i2c_addr = client->addr; |
1177 | state->config.chip_mode = pdata->chip_mode; | |
7dbbb4bf | 1178 | state->config.ref_freq_hz = pdata->ref_freq_hz; |
7cd785ad MS |
1179 | state->config.inversion = pdata->inversion; |
1180 | ||
7dbbb4bf MS |
1181 | if (state->config.ref_freq_hz < 4000000 || |
1182 | state->config.ref_freq_hz > 27000000) { | |
77f887cd | 1183 | dev_err(&state->client->dev, "ref_freq of %d Hz not supported by this driver\n", |
7dbbb4bf | 1184 | state->config.ref_freq_hz); |
7cd785ad MS |
1185 | ret = -EINVAL; |
1186 | goto error; | |
1187 | } | |
1188 | ||
1189 | /* create dvb_frontend */ | |
1190 | memcpy(&state->fe.ops, &si2165_ops, | |
7dbbb4bf | 1191 | sizeof(struct dvb_frontend_ops)); |
7cd785ad MS |
1192 | state->fe.ops.release = NULL; |
1193 | state->fe.demodulator_priv = state; | |
1194 | i2c_set_clientdata(client, state); | |
1195 | ||
1196 | /* powerup */ | |
814f86c9 | 1197 | ret = si2165_writereg8(state, REG_CHIP_MODE, state->config.chip_mode); |
7cd785ad MS |
1198 | if (ret < 0) |
1199 | goto nodev_error; | |
1200 | ||
814f86c9 | 1201 | ret = si2165_readreg8(state, REG_CHIP_MODE, &val); |
7cd785ad MS |
1202 | if (ret < 0) |
1203 | goto nodev_error; | |
1204 | if (val != state->config.chip_mode) | |
1205 | goto nodev_error; | |
1206 | ||
814f86c9 | 1207 | ret = si2165_readreg8(state, REG_CHIP_REVCODE, &state->chip_revcode); |
7cd785ad MS |
1208 | if (ret < 0) |
1209 | goto nodev_error; | |
1210 | ||
814f86c9 | 1211 | ret = si2165_readreg8(state, REV_CHIP_TYPE, &state->chip_type); |
7cd785ad MS |
1212 | if (ret < 0) |
1213 | goto nodev_error; | |
1214 | ||
1215 | /* powerdown */ | |
814f86c9 | 1216 | ret = si2165_writereg8(state, REG_CHIP_MODE, SI2165_MODE_OFF); |
7cd785ad MS |
1217 | if (ret < 0) |
1218 | goto nodev_error; | |
1219 | ||
1220 | if (state->chip_revcode < 26) | |
1221 | rev_char = 'A' + state->chip_revcode; | |
1222 | else | |
1223 | rev_char = '?'; | |
1224 | ||
1225 | switch (state->chip_type) { | |
1226 | case 0x06: | |
1227 | chip_name = "Si2161"; | |
1228 | state->has_dvbt = true; | |
1229 | break; | |
1230 | case 0x07: | |
1231 | chip_name = "Si2165"; | |
1232 | state->has_dvbt = true; | |
1233 | state->has_dvbc = true; | |
1234 | break; | |
1235 | default: | |
77f887cd MS |
1236 | dev_err(&state->client->dev, "Unsupported Silicon Labs chip (type %d, rev %d)\n", |
1237 | state->chip_type, state->chip_revcode); | |
7cd785ad MS |
1238 | goto nodev_error; |
1239 | } | |
1240 | ||
aa155449 | 1241 | dev_info(&state->client->dev, |
7dbbb4bf | 1242 | "Detected Silicon Labs %s-%c (type %d, rev %d)\n", |
77f887cd | 1243 | chip_name, rev_char, state->chip_type, |
7cd785ad MS |
1244 | state->chip_revcode); |
1245 | ||
1246 | strlcat(state->fe.ops.info.name, chip_name, | |
7dbbb4bf | 1247 | sizeof(state->fe.ops.info.name)); |
7cd785ad MS |
1248 | |
1249 | n = 0; | |
1250 | if (state->has_dvbt) { | |
1251 | state->fe.ops.delsys[n++] = SYS_DVBT; | |
1252 | strlcat(state->fe.ops.info.name, " DVB-T", | |
1253 | sizeof(state->fe.ops.info.name)); | |
1254 | } | |
1255 | if (state->has_dvbc) { | |
1256 | state->fe.ops.delsys[n++] = SYS_DVBC_ANNEX_A; | |
1257 | strlcat(state->fe.ops.info.name, " DVB-C", | |
1258 | sizeof(state->fe.ops.info.name)); | |
1259 | } | |
1260 | ||
1261 | /* return fe pointer */ | |
1262 | *pdata->fe = &state->fe; | |
1263 | ||
1264 | return 0; | |
1265 | ||
1266 | nodev_error: | |
1267 | ret = -ENODEV; | |
1268 | error: | |
1269 | kfree(state); | |
1270 | dev_dbg(&client->dev, "failed=%d\n", ret); | |
1271 | return ret; | |
1272 | } | |
1273 | ||
ed5c2f5f | 1274 | static void si2165_remove(struct i2c_client *client) |
7cd785ad MS |
1275 | { |
1276 | struct si2165_state *state = i2c_get_clientdata(client); | |
1277 | ||
1278 | dev_dbg(&client->dev, "\n"); | |
1279 | ||
1280 | kfree(state); | |
7cd785ad MS |
1281 | } |
1282 | ||
1283 | static const struct i2c_device_id si2165_id_table[] = { | |
cc4cbd4b | 1284 | { "si2165" }, |
7cd785ad MS |
1285 | {} |
1286 | }; | |
1287 | MODULE_DEVICE_TABLE(i2c, si2165_id_table); | |
1288 | ||
1289 | static struct i2c_driver si2165_driver = { | |
1290 | .driver = { | |
7cd785ad MS |
1291 | .name = "si2165", |
1292 | }, | |
aaeb31c0 | 1293 | .probe = si2165_probe, |
7cd785ad MS |
1294 | .remove = si2165_remove, |
1295 | .id_table = si2165_id_table, | |
1296 | }; | |
1297 | ||
1298 | module_i2c_driver(si2165_driver); | |
1299 | ||
3e54a169 MS |
1300 | MODULE_DESCRIPTION("Silicon Labs Si2165 DVB-C/-T Demodulator driver"); |
1301 | MODULE_AUTHOR("Matthias Schwarzott <[email protected]>"); | |
1302 | MODULE_LICENSE("GPL"); | |
55bea400 | 1303 | MODULE_FIRMWARE(SI2165_FIRMWARE_REV_D); |