2 * NXP TDA18212HN silicon tuner driver
4 * Copyright (C) 2011 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 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.
23 struct tda18212_priv
{
24 struct tda18212_config
*cfg
;
25 struct i2c_adapter
*i2c
;
30 /* write multiple registers */
31 static int tda18212_wr_regs(struct tda18212_priv
*priv
, u8 reg
, u8
*val
,
36 struct i2c_msg msg
[1] = {
38 .addr
= priv
->cfg
->i2c_address
,
46 memcpy(&buf
[1], val
, len
);
48 ret
= i2c_transfer(priv
->i2c
, msg
, 1);
52 dev_warn(&priv
->i2c
->dev
, "%s: i2c wr failed=%d reg=%02x " \
53 "len=%d\n", KBUILD_MODNAME
, ret
, reg
, len
);
59 /* read multiple registers */
60 static int tda18212_rd_regs(struct tda18212_priv
*priv
, u8 reg
, u8
*val
,
65 struct i2c_msg msg
[2] = {
67 .addr
= priv
->cfg
->i2c_address
,
72 .addr
= priv
->cfg
->i2c_address
,
79 ret
= i2c_transfer(priv
->i2c
, msg
, 2);
81 memcpy(val
, buf
, len
);
84 dev_warn(&priv
->i2c
->dev
, "%s: i2c rd failed=%d reg=%02x " \
85 "len=%d\n", KBUILD_MODNAME
, ret
, reg
, len
);
92 /* write single register */
93 static int tda18212_wr_reg(struct tda18212_priv
*priv
, u8 reg
, u8 val
)
95 return tda18212_wr_regs(priv
, reg
, &val
, 1);
98 /* read single register */
99 static int tda18212_rd_reg(struct tda18212_priv
*priv
, u8 reg
, u8
*val
)
101 return tda18212_rd_regs(priv
, reg
, val
, 1);
104 #if 0 /* keep, useful when developing driver */
105 static void tda18212_dump_regs(struct tda18212_priv
*priv
)
110 #define TDA18212_RD_LEN 32
111 for (i
= 0; i
< sizeof(buf
); i
+= TDA18212_RD_LEN
)
112 tda18212_rd_regs(priv
, i
, &buf
[i
], TDA18212_RD_LEN
);
114 print_hex_dump(KERN_INFO
, "", DUMP_PREFIX_OFFSET
, 32, 1, buf
,
121 static int tda18212_set_params(struct dvb_frontend
*fe
)
123 struct tda18212_priv
*priv
= fe
->tuner_priv
;
124 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
136 static const u8 bw_params
[][3] = {
138 [DVBT_6
] = { 0xb3, 0x20, 0x03 },
139 [DVBT_7
] = { 0xb3, 0x31, 0x01 },
140 [DVBT_8
] = { 0xb3, 0x22, 0x01 },
141 [DVBT2_6
] = { 0xbc, 0x20, 0x03 },
142 [DVBT2_7
] = { 0xbc, 0x72, 0x03 },
143 [DVBT2_8
] = { 0xbc, 0x22, 0x01 },
144 [DVBC_6
] = { 0x92, 0x50, 0x03 },
145 [DVBC_8
] = { 0x92, 0x53, 0x03 },
148 dev_dbg(&priv
->i2c
->dev
,
149 "%s: delivery_system=%d frequency=%d bandwidth_hz=%d\n",
150 __func__
, c
->delivery_system
, c
->frequency
,
153 if (fe
->ops
.i2c_gate_ctrl
)
154 fe
->ops
.i2c_gate_ctrl(fe
, 1); /* open I2C-gate */
156 switch (c
->delivery_system
) {
158 switch (c
->bandwidth_hz
) {
160 if_khz
= priv
->cfg
->if_dvbt_6
;
164 if_khz
= priv
->cfg
->if_dvbt_7
;
168 if_khz
= priv
->cfg
->if_dvbt_8
;
177 switch (c
->bandwidth_hz
) {
179 if_khz
= priv
->cfg
->if_dvbt2_6
;
183 if_khz
= priv
->cfg
->if_dvbt2_7
;
187 if_khz
= priv
->cfg
->if_dvbt2_8
;
195 case SYS_DVBC_ANNEX_A
:
196 case SYS_DVBC_ANNEX_C
:
197 if_khz
= priv
->cfg
->if_dvbc
;
205 ret
= tda18212_wr_reg(priv
, 0x23, bw_params
[i
][2]);
209 ret
= tda18212_wr_reg(priv
, 0x06, 0x00);
213 ret
= tda18212_wr_reg(priv
, 0x0f, bw_params
[i
][0]);
218 buf
[1] = bw_params
[i
][1];
219 buf
[2] = 0x03; /* default value */
220 buf
[3] = DIV_ROUND_CLOSEST(if_khz
, 50);
221 buf
[4] = ((c
->frequency
/ 1000) >> 16) & 0xff;
222 buf
[5] = ((c
->frequency
/ 1000) >> 8) & 0xff;
223 buf
[6] = ((c
->frequency
/ 1000) >> 0) & 0xff;
226 ret
= tda18212_wr_regs(priv
, 0x12, buf
, sizeof(buf
));
230 /* actual IF rounded as it is on register */
231 priv
->if_frequency
= buf
[3] * 50 * 1000;
234 if (fe
->ops
.i2c_gate_ctrl
)
235 fe
->ops
.i2c_gate_ctrl(fe
, 0); /* close I2C-gate */
240 dev_dbg(&priv
->i2c
->dev
, "%s: failed=%d\n", __func__
, ret
);
244 static int tda18212_get_if_frequency(struct dvb_frontend
*fe
, u32
*frequency
)
246 struct tda18212_priv
*priv
= fe
->tuner_priv
;
248 *frequency
= priv
->if_frequency
;
253 static int tda18212_release(struct dvb_frontend
*fe
)
255 kfree(fe
->tuner_priv
);
256 fe
->tuner_priv
= NULL
;
260 static const struct dvb_tuner_ops tda18212_tuner_ops
= {
262 .name
= "NXP TDA18212",
264 .frequency_min
= 48000000,
265 .frequency_max
= 864000000,
266 .frequency_step
= 1000,
269 .release
= tda18212_release
,
271 .set_params
= tda18212_set_params
,
272 .get_if_frequency
= tda18212_get_if_frequency
,
275 struct dvb_frontend
*tda18212_attach(struct dvb_frontend
*fe
,
276 struct i2c_adapter
*i2c
, struct tda18212_config
*cfg
)
278 struct tda18212_priv
*priv
= NULL
;
282 priv
= kzalloc(sizeof(struct tda18212_priv
), GFP_KERNEL
);
288 fe
->tuner_priv
= priv
;
290 if (fe
->ops
.i2c_gate_ctrl
)
291 fe
->ops
.i2c_gate_ctrl(fe
, 1); /* open I2C-gate */
293 /* check if the tuner is there */
294 ret
= tda18212_rd_reg(priv
, 0x00, &val
);
296 if (fe
->ops
.i2c_gate_ctrl
)
297 fe
->ops
.i2c_gate_ctrl(fe
, 0); /* close I2C-gate */
300 dev_dbg(&priv
->i2c
->dev
, "%s: chip id=%02x\n", __func__
, val
);
301 if (ret
|| val
!= 0xc7) {
306 dev_info(&priv
->i2c
->dev
,
307 "%s: NXP TDA18212HN successfully identified\n",
310 memcpy(&fe
->ops
.tuner_ops
, &tda18212_tuner_ops
,
311 sizeof(struct dvb_tuner_ops
));
315 EXPORT_SYMBOL(tda18212_attach
);
317 MODULE_DESCRIPTION("NXP TDA18212HN silicon tuner driver");
318 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
319 MODULE_LICENSE("GPL");