2 * ITE Tech IT9137 silicon tuner driver
4 * Copyright (C) 2011 Malcolm Priestley (tvboxspy@gmail.com)
5 * IT9137 Copyright (C) ITE Tech Inc.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.=
23 #include "tuner_it913x_priv.h"
26 struct i2c_adapter
*i2c_adap
;
37 /* read multiple registers */
38 static int it913x_rd_regs(struct it913x_state
*state
,
39 u32 reg
, u8
*data
, u8 count
)
43 struct i2c_msg msg
[2] = {
44 { .addr
= state
->i2c_addr
, .flags
= 0,
45 .buf
= b
, .len
= sizeof(b
) },
46 { .addr
= state
->i2c_addr
, .flags
= I2C_M_RD
,
47 .buf
= data
, .len
= count
}
49 b
[0] = (u8
)(reg
>> 16) & 0xff;
50 b
[1] = (u8
)(reg
>> 8) & 0xff;
51 b
[2] = (u8
) reg
& 0xff;
52 b
[0] |= 0x80; /* All reads from demodulator */
54 ret
= i2c_transfer(state
->i2c_adap
, msg
, 2);
59 /* read single register */
60 static int it913x_rd_reg(struct it913x_state
*state
, u32 reg
)
64 ret
= it913x_rd_regs(state
, reg
, &b
[0], sizeof(b
));
65 return (ret
< 0) ? -ENODEV
: b
[0];
68 /* write multiple registers */
69 static int it913x_wr_regs(struct it913x_state
*state
,
70 u8 pro
, u32 reg
, u8 buf
[], u8 count
)
73 struct i2c_msg msg
[1] = {
74 { .addr
= state
->i2c_addr
, .flags
= 0,
75 .buf
= b
, .len
= 3 + count
}
78 b
[0] = (u8
)(reg
>> 16) & 0xff;
79 b
[1] = (u8
)(reg
>> 8) & 0xff;
80 b
[2] = (u8
) reg
& 0xff;
81 memcpy(&b
[3], buf
, count
);
86 ret
= i2c_transfer(state
->i2c_adap
, msg
, 1);
94 /* write single register */
95 static int it913x_wr_reg(struct it913x_state
*state
,
96 u8 pro
, u32 reg
, u32 data
)
103 b
[1] = (data
>> 16) & 0xff;
104 b
[2] = (data
>> 8) & 0xff;
106 /* expand write as needed */
109 else if (data
< 0x1000)
111 else if (data
< 0x100000)
116 ret
= it913x_wr_regs(state
, pro
, reg
, &b
[s
], sizeof(b
) - s
);
121 static int it913x_script_loader(struct it913x_state
*state
,
122 struct it913xset
*loadscript
)
125 if (loadscript
== NULL
)
128 for (i
= 0; i
< 1000; ++i
) {
129 if (loadscript
[i
].pro
== 0xff)
131 ret
= it913x_wr_regs(state
, loadscript
[i
].pro
,
132 loadscript
[i
].address
,
133 loadscript
[i
].reg
, loadscript
[i
].count
);
140 static int it913x_init(struct dvb_frontend
*fe
)
142 struct it913x_state
*state
= fe
->tuner_priv
;
145 u8 nv
[] = {48, 32, 24, 16, 12, 8, 6, 4, 2};
148 reg
= it913x_rd_reg(state
, 0xec86);
151 state
->tun_clk_mode
= reg
;
152 state
->tun_xtal
= 2000;
160 state
->tun_clk_mode
= reg
;
161 state
->tun_xtal
= 640;
167 reg
= it913x_rd_reg(state
, 0xed03);
171 else if (reg
< ARRAY_SIZE(nv
))
176 for (i
= 0; i
< 50; i
++) {
177 ret
= it913x_rd_regs(state
, 0xed23, &b
[0], sizeof(b
));
178 reg
= (b
[1] << 8) + b
[0];
185 state
->tun_fn_min
= state
->tun_xtal
* reg
;
186 state
->tun_fn_min
/= (state
->tun_fdiv
* nv_val
);
187 dev_dbg(&state
->i2c_adap
->dev
, "%s: Tuner fn_min %d\n", __func__
,
190 if (state
->chip_ver
> 1)
193 for (i
= 0; i
< 50; i
++) {
194 reg
= it913x_rd_reg(state
, 0xec82);
203 /* Power Up Tuner - common all versions */
204 ret
= it913x_wr_reg(state
, PRO_DMOD
, 0xec40, 0x1);
205 ret
|= it913x_wr_reg(state
, PRO_DMOD
, 0xfba8, 0x0);
206 ret
|= it913x_wr_reg(state
, PRO_DMOD
, 0xec57, 0x0);
207 ret
|= it913x_wr_reg(state
, PRO_DMOD
, 0xec58, 0x0);
209 return it913x_wr_reg(state
, PRO_DMOD
, 0xed81, val
);
212 static int it9137_set_params(struct dvb_frontend
*fe
)
214 struct it913x_state
*state
= fe
->tuner_priv
;
215 struct it913xset
*set_tuner
= set_it9137_template
;
216 struct dtv_frontend_properties
*p
= &fe
->dtv_property_cache
;
217 u32 bandwidth
= p
->bandwidth_hz
;
218 u32 frequency_m
= p
->frequency
;
220 u32 frequency
= frequency_m
/ 1000;
221 u32 freq
, temp_f
, tmp
;
229 if (state
->firmware_ver
== 1)
230 set_tuner
= set_it9135_template
;
232 set_tuner
= set_it9137_template
;
234 dev_dbg(&state
->i2c_adap
->dev
, "%s: Tuner Frequency %d Bandwidth %d\n",
235 __func__
, frequency
, bandwidth
);
237 if (frequency
>= 51000 && frequency
<= 440000) {
240 } else if (frequency
> 440000 && frequency
<= 484000) {
243 } else if (frequency
> 484000 && frequency
<= 533000) {
246 } else if (frequency
> 533000 && frequency
<= 587000) {
249 } else if (frequency
> 587000 && frequency
<= 645000) {
252 } else if (frequency
> 645000 && frequency
<= 710000) {
255 } else if (frequency
> 710000 && frequency
<= 782000) {
258 } else if (frequency
> 782000 && frequency
<= 860000) {
261 } else if (frequency
> 1450000 && frequency
<= 1492000) {
264 } else if (frequency
> 1660000 && frequency
<= 1685000) {
269 set_tuner
[0].reg
[0] = lna_band
;
287 set_tuner
[1].reg
[0] = bw
;
288 set_tuner
[2].reg
[0] = 0xa0 | (l_band
<< 3);
290 if (frequency
> 53000 && frequency
<= 74000) {
293 } else if (frequency
> 74000 && frequency
<= 111000) {
296 } else if (frequency
> 111000 && frequency
<= 148000) {
299 } else if (frequency
> 148000 && frequency
<= 222000) {
302 } else if (frequency
> 222000 && frequency
<= 296000) {
305 } else if (frequency
> 296000 && frequency
<= 445000) {
308 } else if (frequency
> 445000 && frequency
<= state
->tun_fn_min
) {
311 } else if (frequency
> state
->tun_fn_min
&& frequency
<= 950000) {
314 } else if (frequency
> 1450000 && frequency
<= 1680000) {
320 reg
= it913x_rd_reg(state
, 0xed81);
321 iqik_m_cal
= (u16
)reg
* n_div
;
324 if (state
->tun_clk_mode
== 0)
325 iqik_m_cal
= (iqik_m_cal
* 9) >> 5;
329 iqik_m_cal
= 0x40 - iqik_m_cal
;
330 if (state
->tun_clk_mode
== 0)
331 iqik_m_cal
= ~((iqik_m_cal
* 9) >> 5);
333 iqik_m_cal
= ~(iqik_m_cal
>> 1);
336 temp_f
= frequency
* (u32
)n_div
* (u32
)state
->tun_fdiv
;
337 freq
= temp_f
/ state
->tun_xtal
;
338 tmp
= freq
* state
->tun_xtal
;
340 if ((temp_f
- tmp
) >= (state
->tun_xtal
>> 1))
343 freq
+= (u32
) n
<< 13;
344 /* Frequency OMEGA_IQIK_M_CAL_MID*/
345 temp_f
= freq
+ (u32
)iqik_m_cal
;
347 set_tuner
[3].reg
[0] = temp_f
& 0xff;
348 set_tuner
[4].reg
[0] = (temp_f
>> 8) & 0xff;
350 dev_dbg(&state
->i2c_adap
->dev
, "%s: High Frequency = %04x\n",
353 /* Lower frequency */
354 set_tuner
[5].reg
[0] = freq
& 0xff;
355 set_tuner
[6].reg
[0] = (freq
>> 8) & 0xff;
357 dev_dbg(&state
->i2c_adap
->dev
, "%s: low Frequency = %04x\n",
360 ret
= it913x_script_loader(state
, set_tuner
);
362 return (ret
< 0) ? -ENODEV
: 0;
366 /* Power Up Tuner on -> Frontend suspend off -> Tuner clk on */
367 /* Power Down Frontend suspend on -> Tuner clk off -> Tuner off */
369 static int it913x_sleep(struct dvb_frontend
*fe
)
371 struct it913x_state
*state
= fe
->tuner_priv
;
372 return it913x_script_loader(state
, it9137_tuner_off
);
375 static int it913x_release(struct dvb_frontend
*fe
)
377 kfree(fe
->tuner_priv
);
381 static const struct dvb_tuner_ops it913x_tuner_ops
= {
383 .name
= "ITE Tech IT913X",
384 .frequency_min
= 174000000,
385 .frequency_max
= 862000000,
388 .release
= it913x_release
,
391 .sleep
= it913x_sleep
,
392 .set_params
= it9137_set_params
,
395 struct dvb_frontend
*it913x_attach(struct dvb_frontend
*fe
,
396 struct i2c_adapter
*i2c_adap
, u8 i2c_addr
, u8 config
)
398 struct it913x_state
*state
= NULL
;
400 /* allocate memory for the internal state */
401 state
= kzalloc(sizeof(struct it913x_state
), GFP_KERNEL
);
405 state
->i2c_adap
= i2c_adap
;
406 state
->i2c_addr
= i2c_addr
;
409 case AF9033_TUNER_IT9135_38
:
410 case AF9033_TUNER_IT9135_51
:
411 case AF9033_TUNER_IT9135_52
:
412 state
->chip_ver
= 0x01;
414 case AF9033_TUNER_IT9135_60
:
415 case AF9033_TUNER_IT9135_61
:
416 case AF9033_TUNER_IT9135_62
:
417 state
->chip_ver
= 0x02;
420 dev_dbg(&i2c_adap
->dev
,
421 "%s: invalid config=%02x\n", __func__
, config
);
425 state
->tuner_type
= config
;
426 state
->firmware_ver
= 1;
428 fe
->tuner_priv
= state
;
429 memcpy(&fe
->ops
.tuner_ops
, &it913x_tuner_ops
,
430 sizeof(struct dvb_tuner_ops
));
432 dev_info(&i2c_adap
->dev
,
433 "%s: ITE Tech IT913X successfully attached\n",
435 dev_dbg(&i2c_adap
->dev
, "%s: config=%02x chip_ver=%02x\n",
436 __func__
, config
, state
->chip_ver
);
443 EXPORT_SYMBOL(it913x_attach
);
445 MODULE_DESCRIPTION("ITE Tech IT913X silicon tuner driver");
446 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
447 MODULE_LICENSE("GPL");