2 * NXP TDA18218HN silicon tuner driver
4 * Copyright (C) 2010 Antti Palosaari <crope@iki.fi>
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.
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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include "tda18218_priv.h"
23 /* write multiple registers */
24 static int tda18218_wr_regs(struct tda18218_priv
*priv
, u8 reg
, u8
*val
, u8 len
)
26 int ret
= 0, len2
, remaining
;
28 struct i2c_msg msg
[1] = {
30 .addr
= priv
->cfg
->i2c_address
,
36 for (remaining
= len
; remaining
> 0;
37 remaining
-= (priv
->cfg
->i2c_wr_max
- 1)) {
39 if (len2
> (priv
->cfg
->i2c_wr_max
- 1))
40 len2
= (priv
->cfg
->i2c_wr_max
- 1);
42 msg
[0].len
= 1 + len2
;
43 buf
[0] = reg
+ len
- remaining
;
44 memcpy(&buf
[1], &val
[len
- remaining
], len2
);
46 ret
= i2c_transfer(priv
->i2c
, msg
, 1);
54 dev_warn(&priv
->i2c
->dev
, "%s: i2c wr failed=%d reg=%02x " \
55 "len=%d\n", KBUILD_MODNAME
, ret
, reg
, len
);
62 /* read multiple registers */
63 static int tda18218_rd_regs(struct tda18218_priv
*priv
, u8 reg
, u8
*val
, u8 len
)
66 u8 buf
[reg
+len
]; /* we must start read always from reg 0x00 */
67 struct i2c_msg msg
[2] = {
69 .addr
= priv
->cfg
->i2c_address
,
74 .addr
= priv
->cfg
->i2c_address
,
81 ret
= i2c_transfer(priv
->i2c
, msg
, 2);
83 memcpy(val
, &buf
[reg
], len
);
86 dev_warn(&priv
->i2c
->dev
, "%s: i2c rd failed=%d reg=%02x " \
87 "len=%d\n", KBUILD_MODNAME
, ret
, reg
, len
);
94 /* write single register */
95 static int tda18218_wr_reg(struct tda18218_priv
*priv
, u8 reg
, u8 val
)
97 return tda18218_wr_regs(priv
, reg
, &val
, 1);
100 /* read single register */
102 static int tda18218_rd_reg(struct tda18218_priv
*priv
, u8 reg
, u8
*val
)
104 return tda18218_rd_regs(priv
, reg
, val
, 1);
107 static int tda18218_set_params(struct dvb_frontend
*fe
)
109 struct tda18218_priv
*priv
= fe
->tuner_priv
;
110 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
111 u32 bw
= c
->bandwidth_hz
;
113 u8 buf
[3], i
, BP_Filter
, LP_Fc
;
115 /* TODO: find out correct AGC algorithm */
133 if (fe
->ops
.i2c_gate_ctrl
)
134 fe
->ops
.i2c_gate_ctrl(fe
, 1); /* open I2C-gate */
136 /* low-pass filter cut-off frequency */
139 priv
->if_frequency
= 3000000;
140 } else if (bw
<= 7000000) {
142 priv
->if_frequency
= 3500000;
145 priv
->if_frequency
= 4000000;
148 LO_Frac
= c
->frequency
+ priv
->if_frequency
;
150 /* band-pass filter */
151 if (LO_Frac
< 188000000)
153 else if (LO_Frac
< 253000000)
155 else if (LO_Frac
< 343000000)
160 buf
[0] = (priv
->regs
[R1A_IF1
] & ~7) | BP_Filter
; /* BP_Filter */
161 buf
[1] = (priv
->regs
[R1B_IF2
] & ~3) | LP_Fc
; /* LP_Fc */
162 buf
[2] = priv
->regs
[R1C_AGC2B
];
163 ret
= tda18218_wr_regs(priv
, R1A_IF1
, buf
, 3);
167 buf
[0] = (LO_Frac
/ 1000) >> 12; /* LO_Frac_0 */
168 buf
[1] = (LO_Frac
/ 1000) >> 4; /* LO_Frac_1 */
169 buf
[2] = (LO_Frac
/ 1000) << 4 |
170 (priv
->regs
[R0C_MD5
] & 0x0f); /* LO_Frac_2 */
171 ret
= tda18218_wr_regs(priv
, R0A_MD3
, buf
, 3);
175 buf
[0] = priv
->regs
[R0F_MD8
] | (1 << 6); /* Freq_prog_Start */
176 ret
= tda18218_wr_regs(priv
, R0F_MD8
, buf
, 1);
180 buf
[0] = priv
->regs
[R0F_MD8
] & ~(1 << 6); /* Freq_prog_Start */
181 ret
= tda18218_wr_regs(priv
, R0F_MD8
, buf
, 1);
186 for (i
= 0; i
< ARRAY_SIZE(agc
); i
++) {
187 ret
= tda18218_wr_reg(priv
, agc
[i
][0], agc
[i
][1]);
193 if (fe
->ops
.i2c_gate_ctrl
)
194 fe
->ops
.i2c_gate_ctrl(fe
, 0); /* close I2C-gate */
197 dev_dbg(&priv
->i2c
->dev
, "%s: failed=%d\n", __func__
, ret
);
202 static int tda18218_get_if_frequency(struct dvb_frontend
*fe
, u32
*frequency
)
204 struct tda18218_priv
*priv
= fe
->tuner_priv
;
205 *frequency
= priv
->if_frequency
;
206 dev_dbg(&priv
->i2c
->dev
, "%s: if_frequency=%d\n", __func__
, *frequency
);
210 static int tda18218_sleep(struct dvb_frontend
*fe
)
212 struct tda18218_priv
*priv
= fe
->tuner_priv
;
215 if (fe
->ops
.i2c_gate_ctrl
)
216 fe
->ops
.i2c_gate_ctrl(fe
, 1); /* open I2C-gate */
219 ret
= tda18218_wr_reg(priv
, R17_PD1
, priv
->regs
[R17_PD1
] | (1 << 0));
221 if (fe
->ops
.i2c_gate_ctrl
)
222 fe
->ops
.i2c_gate_ctrl(fe
, 0); /* close I2C-gate */
225 dev_dbg(&priv
->i2c
->dev
, "%s: failed=%d\n", __func__
, ret
);
230 static int tda18218_init(struct dvb_frontend
*fe
)
232 struct tda18218_priv
*priv
= fe
->tuner_priv
;
235 /* TODO: calibrations */
237 if (fe
->ops
.i2c_gate_ctrl
)
238 fe
->ops
.i2c_gate_ctrl(fe
, 1); /* open I2C-gate */
240 ret
= tda18218_wr_regs(priv
, R00_ID
, priv
->regs
, TDA18218_NUM_REGS
);
242 if (fe
->ops
.i2c_gate_ctrl
)
243 fe
->ops
.i2c_gate_ctrl(fe
, 0); /* close I2C-gate */
246 dev_dbg(&priv
->i2c
->dev
, "%s: failed=%d\n", __func__
, ret
);
251 static int tda18218_release(struct dvb_frontend
*fe
)
253 kfree(fe
->tuner_priv
);
254 fe
->tuner_priv
= NULL
;
258 static const struct dvb_tuner_ops tda18218_tuner_ops
= {
260 .name
= "NXP TDA18218",
262 .frequency_min
= 174000000,
263 .frequency_max
= 864000000,
264 .frequency_step
= 1000,
267 .release
= tda18218_release
,
268 .init
= tda18218_init
,
269 .sleep
= tda18218_sleep
,
271 .set_params
= tda18218_set_params
,
273 .get_if_frequency
= tda18218_get_if_frequency
,
276 struct dvb_frontend
*tda18218_attach(struct dvb_frontend
*fe
,
277 struct i2c_adapter
*i2c
, struct tda18218_config
*cfg
)
279 struct tda18218_priv
*priv
= NULL
;
282 /* chip default registers values */
283 static u8 def_regs
[] = {
284 0xc0, 0x88, 0x00, 0x8e, 0x03, 0x00, 0x00, 0xd0, 0x00, 0x40,
285 0x00, 0x00, 0x07, 0xff, 0x84, 0x09, 0x00, 0x13, 0x00, 0x00,
286 0x01, 0x84, 0x09, 0xf0, 0x19, 0x0a, 0x8e, 0x69, 0x98, 0x01,
287 0x00, 0x58, 0x10, 0x40, 0x8c, 0x00, 0x0c, 0x48, 0x85, 0xc9,
288 0xa7, 0x00, 0x00, 0x00, 0x30, 0x81, 0x80, 0x00, 0x39, 0x00,
289 0x8a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf6, 0xf6
292 priv
= kzalloc(sizeof(struct tda18218_priv
), GFP_KERNEL
);
298 fe
->tuner_priv
= priv
;
300 if (fe
->ops
.i2c_gate_ctrl
)
301 fe
->ops
.i2c_gate_ctrl(fe
, 1); /* open I2C-gate */
303 /* check if the tuner is there */
304 ret
= tda18218_rd_reg(priv
, R00_ID
, &val
);
306 dev_dbg(&priv
->i2c
->dev
, "%s: chip id=%02x\n", __func__
, val
);
307 if (ret
|| val
!= def_regs
[R00_ID
]) {
312 dev_info(&priv
->i2c
->dev
,
313 "%s: NXP TDA18218HN successfully identified\n",
316 memcpy(&fe
->ops
.tuner_ops
, &tda18218_tuner_ops
,
317 sizeof(struct dvb_tuner_ops
));
318 memcpy(priv
->regs
, def_regs
, sizeof(def_regs
));
320 /* loop-through enabled chip default register values */
321 if (priv
->cfg
->loop_through
) {
322 priv
->regs
[R17_PD1
] = 0xb0;
323 priv
->regs
[R18_PD2
] = 0x59;
327 ret
= tda18218_wr_reg(priv
, R17_PD1
, priv
->regs
[R17_PD1
] | (1 << 0));
329 dev_dbg(&priv
->i2c
->dev
, "%s: failed=%d\n", __func__
, ret
);
331 if (fe
->ops
.i2c_gate_ctrl
)
332 fe
->ops
.i2c_gate_ctrl(fe
, 0); /* close I2C-gate */
336 EXPORT_SYMBOL(tda18218_attach
);
338 MODULE_DESCRIPTION("NXP TDA18218HN silicon tuner driver");
339 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
340 MODULE_LICENSE("GPL");