4 * Sony Ascot3E DVB-T/T2/C/C2 tuner driver
6 * Copyright 2012 Sony Corporation
7 * Copyright (C) 2014 NetUP Inc.
8 * Copyright (C) 2014 Sergey Kozlov <serjk@netup.ru>
9 * Copyright (C) 2014 Abylay Ospan <aospan@netup.ru>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
22 #include <linux/slab.h>
23 #include <linux/module.h>
24 #include <linux/dvb/frontend.h>
25 #include <linux/types.h>
27 #include <media/dvb_frontend.h>
29 #define MAX_WRITE_REGSIZE 10
40 struct i2c_adapter
*i2c
;
41 enum ascot2e_state state
;
43 int (*set_tuner
)(void *, int);
46 enum ascot2e_tv_system_t
{
51 ASCOT2E_DTV_DVBT2_1_7
,
63 struct ascot2e_band_sett
{
76 #define ASCOT2E_AUTO 0xff
77 #define ASCOT2E_OFFSET(ofs) ((u8)(ofs) & 0x1F)
78 #define ASCOT2E_BW_6 0x00
79 #define ASCOT2E_BW_7 0x01
80 #define ASCOT2E_BW_8 0x02
81 #define ASCOT2E_BW_1_7 0x03
83 static struct ascot2e_band_sett ascot2e_sett
[] = {
84 { ASCOT2E_AUTO
, ASCOT2E_AUTO
, 0x03, ASCOT2E_AUTO
, 0x06,
85 ASCOT2E_OFFSET(-8), ASCOT2E_OFFSET(-6), ASCOT2E_BW_6
, 0x0B, 0x00 },
86 { ASCOT2E_AUTO
, ASCOT2E_AUTO
, 0x03, ASCOT2E_AUTO
, 0x06,
87 ASCOT2E_OFFSET(-8), ASCOT2E_OFFSET(-6), ASCOT2E_BW_6
, 0x0B, 0x00 },
88 { ASCOT2E_AUTO
, ASCOT2E_AUTO
, 0x03, ASCOT2E_AUTO
, 0x06,
89 ASCOT2E_OFFSET(-6), ASCOT2E_OFFSET(-4), ASCOT2E_BW_7
, 0x0B, 0x00 },
90 { ASCOT2E_AUTO
, ASCOT2E_AUTO
, 0x03, ASCOT2E_AUTO
, 0x06,
91 ASCOT2E_OFFSET(-4), ASCOT2E_OFFSET(-2), ASCOT2E_BW_8
, 0x0B, 0x00 },
92 { ASCOT2E_AUTO
, ASCOT2E_AUTO
, 0x03, ASCOT2E_AUTO
, 0x06,
93 ASCOT2E_OFFSET(-10), ASCOT2E_OFFSET(-16), ASCOT2E_BW_1_7
, 0x0B, 0x00 },
94 { ASCOT2E_AUTO
, ASCOT2E_AUTO
, 0x03, ASCOT2E_AUTO
, 0x06,
95 ASCOT2E_OFFSET(-8), ASCOT2E_OFFSET(-6), ASCOT2E_BW_6
, 0x0B, 0x00 },
96 { ASCOT2E_AUTO
, ASCOT2E_AUTO
, 0x03, ASCOT2E_AUTO
, 0x06,
97 ASCOT2E_OFFSET(-8), ASCOT2E_OFFSET(-6), ASCOT2E_BW_6
, 0x0B, 0x00 },
98 { ASCOT2E_AUTO
, ASCOT2E_AUTO
, 0x03, ASCOT2E_AUTO
, 0x06,
99 ASCOT2E_OFFSET(-6), ASCOT2E_OFFSET(-4), ASCOT2E_BW_7
, 0x0B, 0x00 },
100 { ASCOT2E_AUTO
, ASCOT2E_AUTO
, 0x03, ASCOT2E_AUTO
, 0x06,
101 ASCOT2E_OFFSET(-4), ASCOT2E_OFFSET(-2), ASCOT2E_BW_8
, 0x0B, 0x00 },
102 { ASCOT2E_AUTO
, ASCOT2E_AUTO
, 0x02, ASCOT2E_AUTO
, 0x03,
103 ASCOT2E_OFFSET(-6), ASCOT2E_OFFSET(-8), ASCOT2E_BW_6
, 0x09, 0x00 },
104 { ASCOT2E_AUTO
, ASCOT2E_AUTO
, 0x02, ASCOT2E_AUTO
, 0x03,
105 ASCOT2E_OFFSET(-2), ASCOT2E_OFFSET(-1), ASCOT2E_BW_8
, 0x09, 0x00 },
106 { ASCOT2E_AUTO
, ASCOT2E_AUTO
, 0x03, ASCOT2E_AUTO
, 0x01,
107 ASCOT2E_OFFSET(-6), ASCOT2E_OFFSET(-4), ASCOT2E_BW_6
, 0x09, 0x00 },
108 { ASCOT2E_AUTO
, ASCOT2E_AUTO
, 0x03, ASCOT2E_AUTO
, 0x01,
109 ASCOT2E_OFFSET(-2), ASCOT2E_OFFSET(2), ASCOT2E_BW_8
, 0x09, 0x00 }
112 static void ascot2e_i2c_debug(struct ascot2e_priv
*priv
,
113 u8 reg
, u8 write
, const u8
*data
, u32 len
)
115 dev_dbg(&priv
->i2c
->dev
, "ascot2e: I2C %s reg 0x%02x size %d\n",
116 (write
== 0 ? "read" : "write"), reg
, len
);
117 print_hex_dump_bytes("ascot2e: I2C data: ",
118 DUMP_PREFIX_OFFSET
, data
, len
);
121 static int ascot2e_write_regs(struct ascot2e_priv
*priv
,
122 u8 reg
, const u8
*data
, u32 len
)
125 u8 buf
[MAX_WRITE_REGSIZE
+ 1];
126 struct i2c_msg msg
[1] = {
128 .addr
= priv
->i2c_address
,
135 if (len
+ 1 > sizeof(buf
)) {
136 dev_warn(&priv
->i2c
->dev
,"wr reg=%04x: len=%d is too big!\n",
141 ascot2e_i2c_debug(priv
, reg
, 1, data
, len
);
143 memcpy(&buf
[1], data
, len
);
144 ret
= i2c_transfer(priv
->i2c
, msg
, 1);
145 if (ret
>= 0 && ret
!= 1)
148 dev_warn(&priv
->i2c
->dev
,
149 "%s: i2c wr failed=%d reg=%02x len=%d\n",
150 KBUILD_MODNAME
, ret
, reg
, len
);
156 static int ascot2e_write_reg(struct ascot2e_priv
*priv
, u8 reg
, u8 val
)
158 u8 tmp
= val
; /* see gcc.gnu.org/bugzilla/show_bug.cgi?id=81715 */
160 return ascot2e_write_regs(priv
, reg
, &tmp
, 1);
163 static int ascot2e_read_regs(struct ascot2e_priv
*priv
,
164 u8 reg
, u8
*val
, u32 len
)
167 struct i2c_msg msg
[2] = {
169 .addr
= priv
->i2c_address
,
174 .addr
= priv
->i2c_address
,
181 ret
= i2c_transfer(priv
->i2c
, &msg
[0], 1);
182 if (ret
>= 0 && ret
!= 1)
185 dev_warn(&priv
->i2c
->dev
,
186 "%s: I2C rw failed=%d addr=%02x reg=%02x\n",
187 KBUILD_MODNAME
, ret
, priv
->i2c_address
, reg
);
190 ret
= i2c_transfer(priv
->i2c
, &msg
[1], 1);
191 if (ret
>= 0 && ret
!= 1)
194 dev_warn(&priv
->i2c
->dev
,
195 "%s: i2c rd failed=%d addr=%02x reg=%02x\n",
196 KBUILD_MODNAME
, ret
, priv
->i2c_address
, reg
);
199 ascot2e_i2c_debug(priv
, reg
, 0, val
, len
);
203 static int ascot2e_read_reg(struct ascot2e_priv
*priv
, u8 reg
, u8
*val
)
205 return ascot2e_read_regs(priv
, reg
, val
, 1);
208 static int ascot2e_set_reg_bits(struct ascot2e_priv
*priv
,
209 u8 reg
, u8 data
, u8 mask
)
215 res
= ascot2e_read_reg(priv
, reg
, &rdata
);
218 data
= ((data
& mask
) | (rdata
& (mask
^ 0xFF)));
220 return ascot2e_write_reg(priv
, reg
, data
);
223 static int ascot2e_enter_power_save(struct ascot2e_priv
*priv
)
227 dev_dbg(&priv
->i2c
->dev
, "%s()\n", __func__
);
228 if (priv
->state
== STATE_SLEEP
)
232 ascot2e_write_regs(priv
, 0x14, data
, 2);
233 ascot2e_write_reg(priv
, 0x50, 0x01);
234 priv
->state
= STATE_SLEEP
;
238 static int ascot2e_leave_power_save(struct ascot2e_priv
*priv
)
240 u8 data
[2] = { 0xFB, 0x0F };
242 dev_dbg(&priv
->i2c
->dev
, "%s()\n", __func__
);
243 if (priv
->state
== STATE_ACTIVE
)
245 ascot2e_write_regs(priv
, 0x14, data
, 2);
246 ascot2e_write_reg(priv
, 0x50, 0x00);
247 priv
->state
= STATE_ACTIVE
;
251 static int ascot2e_init(struct dvb_frontend
*fe
)
253 struct ascot2e_priv
*priv
= fe
->tuner_priv
;
255 dev_dbg(&priv
->i2c
->dev
, "%s()\n", __func__
);
256 return ascot2e_leave_power_save(priv
);
259 static void ascot2e_release(struct dvb_frontend
*fe
)
261 struct ascot2e_priv
*priv
= fe
->tuner_priv
;
263 dev_dbg(&priv
->i2c
->dev
, "%s()\n", __func__
);
264 kfree(fe
->tuner_priv
);
265 fe
->tuner_priv
= NULL
;
268 static int ascot2e_sleep(struct dvb_frontend
*fe
)
270 struct ascot2e_priv
*priv
= fe
->tuner_priv
;
272 dev_dbg(&priv
->i2c
->dev
, "%s()\n", __func__
);
273 ascot2e_enter_power_save(priv
);
277 static enum ascot2e_tv_system_t
ascot2e_get_tv_system(struct dvb_frontend
*fe
)
279 enum ascot2e_tv_system_t system
= ASCOT2E_DTV_UNKNOWN
;
280 struct dtv_frontend_properties
*p
= &fe
->dtv_property_cache
;
281 struct ascot2e_priv
*priv
= fe
->tuner_priv
;
283 if (p
->delivery_system
== SYS_DVBT
) {
284 if (p
->bandwidth_hz
<= 5000000)
285 system
= ASCOT2E_DTV_DVBT_5
;
286 else if (p
->bandwidth_hz
<= 6000000)
287 system
= ASCOT2E_DTV_DVBT_6
;
288 else if (p
->bandwidth_hz
<= 7000000)
289 system
= ASCOT2E_DTV_DVBT_7
;
290 else if (p
->bandwidth_hz
<= 8000000)
291 system
= ASCOT2E_DTV_DVBT_8
;
293 system
= ASCOT2E_DTV_DVBT_8
;
294 p
->bandwidth_hz
= 8000000;
296 } else if (p
->delivery_system
== SYS_DVBT2
) {
297 if (p
->bandwidth_hz
<= 5000000)
298 system
= ASCOT2E_DTV_DVBT2_5
;
299 else if (p
->bandwidth_hz
<= 6000000)
300 system
= ASCOT2E_DTV_DVBT2_6
;
301 else if (p
->bandwidth_hz
<= 7000000)
302 system
= ASCOT2E_DTV_DVBT2_7
;
303 else if (p
->bandwidth_hz
<= 8000000)
304 system
= ASCOT2E_DTV_DVBT2_8
;
306 system
= ASCOT2E_DTV_DVBT2_8
;
307 p
->bandwidth_hz
= 8000000;
309 } else if (p
->delivery_system
== SYS_DVBC_ANNEX_A
) {
310 if (p
->bandwidth_hz
<= 6000000)
311 system
= ASCOT2E_DTV_DVBC_6
;
312 else if (p
->bandwidth_hz
<= 8000000)
313 system
= ASCOT2E_DTV_DVBC_8
;
315 dev_dbg(&priv
->i2c
->dev
,
316 "%s(): ASCOT2E DTV system %d (delsys %d, bandwidth %d)\n",
317 __func__
, (int)system
, p
->delivery_system
, p
->bandwidth_hz
);
321 static int ascot2e_set_params(struct dvb_frontend
*fe
)
325 enum ascot2e_tv_system_t tv_system
;
326 struct dtv_frontend_properties
*p
= &fe
->dtv_property_cache
;
327 struct ascot2e_priv
*priv
= fe
->tuner_priv
;
329 dev_dbg(&priv
->i2c
->dev
, "%s(): tune frequency %dkHz\n",
330 __func__
, p
->frequency
/ 1000);
331 tv_system
= ascot2e_get_tv_system(fe
);
333 if (tv_system
== ASCOT2E_DTV_UNKNOWN
) {
334 dev_dbg(&priv
->i2c
->dev
, "%s(): unknown DTV system\n",
339 priv
->set_tuner(priv
->set_tuner_data
, 1);
340 frequency
= roundup(p
->frequency
/ 1000, 25);
341 if (priv
->state
== STATE_SLEEP
)
342 ascot2e_leave_power_save(priv
);
344 /* IF_OUT_SEL / AGC_SEL setting */
346 if (ascot2e_sett
[tv_system
].agc_sel
!= ASCOT2E_AUTO
) {
347 /* AGC pin setting from parameter table */
349 (ascot2e_sett
[tv_system
].agc_sel
& 0x03) << 3);
351 if (ascot2e_sett
[tv_system
].if_out_sel
!= ASCOT2E_AUTO
) {
352 /* IFOUT pin setting from parameter table */
354 (ascot2e_sett
[tv_system
].if_out_sel
& 0x01) << 2);
356 /* Set bit[4:2] only */
357 ascot2e_set_reg_bits(priv
, 0x05, data
[0], 0x1c);
359 /* REF_R setting (0x06) */
360 if (tv_system
== ASCOT2E_DTV_DVBC_6
||
361 tv_system
== ASCOT2E_DTV_DVBC_8
) {
363 data
[0] = (frequency
> 500000) ? 16 : 32;
366 data
[0] = (frequency
> 500000) ? 2 : 4;
370 /* KBW setting (0x08), KC0 setting (0x09), KC1 setting (0x0A) */
371 if (tv_system
== ASCOT2E_DTV_DVBC_6
||
372 tv_system
== ASCOT2E_DTV_DVBC_8
) {
381 /* ORDER/R2_RANGE/R2_BANK/C2_BANK setting (0x0B) */
382 if (tv_system
== ASCOT2E_DTV_DVBC_6
||
383 tv_system
== ASCOT2E_DTV_DVBC_8
)
384 data
[5] = (frequency
> 500000) ? 0x08 : 0x0c;
386 data
[5] = (frequency
> 500000) ? 0x30 : 0x38;
387 /* Set MIX_OLL (0x0C) value from parameter table */
388 data
[6] = ascot2e_sett
[tv_system
].mix_oll
;
389 /* Set RF_GAIN (0x0D) setting from parameter table */
390 if (ascot2e_sett
[tv_system
].rf_gain
== ASCOT2E_AUTO
) {
391 /* RF_GAIN auto control enable */
392 ascot2e_write_reg(priv
, 0x4E, 0x01);
393 /* RF_GAIN Default value */
396 /* RF_GAIN auto control disable */
397 ascot2e_write_reg(priv
, 0x4E, 0x00);
398 data
[7] = ascot2e_sett
[tv_system
].rf_gain
;
400 /* Set IF_BPF_GC/FIF_OFFSET (0x0E) value from parameter table */
401 data
[8] = (u8
)((ascot2e_sett
[tv_system
].fif_offset
<< 3) |
402 (ascot2e_sett
[tv_system
].if_bpf_gc
& 0x07));
403 /* Set BW_OFFSET (0x0F) value from parameter table */
404 data
[9] = ascot2e_sett
[tv_system
].bw_offset
;
405 ascot2e_write_regs(priv
, 0x06, data
, 10);
408 * LNA optimization setting
409 * RF_LNA_DIST1-5, RF_LNA_CM
411 if (tv_system
== ASCOT2E_DTV_DVBC_6
||
412 tv_system
== ASCOT2E_DTV_DVBC_8
) {
421 ascot2e_write_regs(priv
, 0x45, data
, 3);
423 Set RF_OLDET_ENX/RF_OLDET_OLL value from parameter table */
424 data
[0] = ascot2e_sett
[tv_system
].rf_oldet
;
425 /* Set IF_BPF_F0 value from parameter table */
426 data
[1] = ascot2e_sett
[tv_system
].if_bpf_f0
;
427 ascot2e_write_regs(priv
, 0x49, data
, 2);
430 * RFAGC fast mode / RFAGC auto control enable
431 * (set bit[7], bit[5:4] only)
432 * vco_cal = 1, set MIX_OL_CPU_EN
434 ascot2e_set_reg_bits(priv
, 0x0c, 0x90, 0xb0);
435 /* Logic wake up, CPU wake up */
438 ascot2e_write_regs(priv
, 0x03, data
, 2);
440 data
[0] = (u8
)(frequency
& 0xFF); /* 0x10: FRF_L */
441 data
[1] = (u8
)((frequency
>> 8) & 0xFF); /* 0x11: FRF_M */
442 data
[2] = (u8
)((frequency
>> 16) & 0x0F); /* 0x12: FRF_H (bit[3:0]) */
443 /* 0x12: BW (bit[5:4]) */
444 data
[2] |= (u8
)(ascot2e_sett
[tv_system
].bw
<< 4);
445 data
[3] = 0xFF; /* 0x13: VCO calibration enable */
446 data
[4] = 0xFF; /* 0x14: Analog block enable */
447 /* Tune (Burst write) */
448 ascot2e_write_regs(priv
, 0x10, data
, 5);
451 ascot2e_write_reg(priv
, 0x04, 0x00);
453 ascot2e_write_reg(priv
, 0x03, 0xC0);
454 /* RFAGC normal mode (set bit[5:4] only) */
455 ascot2e_set_reg_bits(priv
, 0x0C, 0x00, 0x30);
456 priv
->frequency
= frequency
;
460 static int ascot2e_get_frequency(struct dvb_frontend
*fe
, u32
*frequency
)
462 struct ascot2e_priv
*priv
= fe
->tuner_priv
;
464 *frequency
= priv
->frequency
* 1000;
468 static const struct dvb_tuner_ops ascot2e_tuner_ops
= {
470 .name
= "Sony ASCOT2E",
471 .frequency_min
= 1000000,
472 .frequency_max
= 1200000000,
473 .frequency_step
= 25000,
475 .init
= ascot2e_init
,
476 .release
= ascot2e_release
,
477 .sleep
= ascot2e_sleep
,
478 .set_params
= ascot2e_set_params
,
479 .get_frequency
= ascot2e_get_frequency
,
482 struct dvb_frontend
*ascot2e_attach(struct dvb_frontend
*fe
,
483 const struct ascot2e_config
*config
,
484 struct i2c_adapter
*i2c
)
487 struct ascot2e_priv
*priv
= NULL
;
489 priv
= kzalloc(sizeof(struct ascot2e_priv
), GFP_KERNEL
);
492 priv
->i2c_address
= (config
->i2c_address
>> 1);
494 priv
->set_tuner_data
= config
->set_tuner_priv
;
495 priv
->set_tuner
= config
->set_tuner_callback
;
497 if (fe
->ops
.i2c_gate_ctrl
)
498 fe
->ops
.i2c_gate_ctrl(fe
, 1);
500 /* 16 MHz xTal frequency */
502 /* VCO current setting */
504 /* Logic wake up, CPU boot */
507 ascot2e_write_regs(priv
, 0x01, data
, 4);
508 /* RFVGA optimization setting (RF_DIST0 - RF_DIST2) */
512 ascot2e_write_regs(priv
, 0x22, data
, 3);
513 /* PLL mode setting */
514 ascot2e_write_reg(priv
, 0x28, 0x1e);
516 ascot2e_write_reg(priv
, 0x59, 0x04);
517 /* TODO check CPU HW error state here */
519 /* Xtal oscillator current control setting */
520 ascot2e_write_reg(priv
, 0x4c, 0x01);
522 ascot2e_write_reg(priv
, 0x07, 0x04);
524 ascot2e_write_reg(priv
, 0x04, 0x00);
526 ascot2e_write_reg(priv
, 0x03, 0xc0);
527 /* Power save setting */
530 ascot2e_write_regs(priv
, 0x14, data
, 2);
531 ascot2e_write_reg(priv
, 0x50, 0x01);
532 priv
->state
= STATE_SLEEP
;
534 if (fe
->ops
.i2c_gate_ctrl
)
535 fe
->ops
.i2c_gate_ctrl(fe
, 0);
537 memcpy(&fe
->ops
.tuner_ops
, &ascot2e_tuner_ops
,
538 sizeof(struct dvb_tuner_ops
));
539 fe
->tuner_priv
= priv
;
540 dev_info(&priv
->i2c
->dev
,
541 "Sony ASCOT2E attached on addr=%x at I2C adapter %p\n",
542 priv
->i2c_address
, priv
->i2c
);
545 EXPORT_SYMBOL(ascot2e_attach
);
547 MODULE_DESCRIPTION("Sony ASCOT2E terr/cab tuner driver");
548 MODULE_AUTHOR("info@netup.ru");
549 MODULE_LICENSE("GPL");