1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * NXP TDA18212HN silicon tuner driver
5 * Copyright (C) 2011 Antti Palosaari <crope@iki.fi>
9 #include <linux/regmap.h>
12 struct tda18212_config cfg
;
13 struct i2c_client
*client
;
14 struct regmap
*regmap
;
19 static int tda18212_set_params(struct dvb_frontend
*fe
)
21 struct tda18212_dev
*dev
= fe
->tuner_priv
;
22 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
36 static const u8 bw_params
[][3] = {
38 [DVBT_6
] = { 0xb3, 0x20, 0x03 },
39 [DVBT_7
] = { 0xb3, 0x31, 0x01 },
40 [DVBT_8
] = { 0xb3, 0x22, 0x01 },
41 [DVBT2_6
] = { 0xbc, 0x20, 0x03 },
42 [DVBT2_7
] = { 0xbc, 0x72, 0x03 },
43 [DVBT2_8
] = { 0xbc, 0x22, 0x01 },
44 [DVBC_6
] = { 0x92, 0x50, 0x03 },
45 [DVBC_8
] = { 0x92, 0x53, 0x03 },
46 [ATSC_VSB
] = { 0x7d, 0x20, 0x63 },
47 [ATSC_QAM
] = { 0x7d, 0x20, 0x63 },
50 dev_dbg(&dev
->client
->dev
,
51 "delivery_system=%d frequency=%d bandwidth_hz=%d\n",
52 c
->delivery_system
, c
->frequency
,
55 if (fe
->ops
.i2c_gate_ctrl
)
56 fe
->ops
.i2c_gate_ctrl(fe
, 1); /* open I2C-gate */
58 switch (c
->delivery_system
) {
60 if_khz
= dev
->cfg
.if_atsc_vsb
;
63 case SYS_DVBC_ANNEX_B
:
64 if_khz
= dev
->cfg
.if_atsc_qam
;
68 switch (c
->bandwidth_hz
) {
70 if_khz
= dev
->cfg
.if_dvbt_6
;
74 if_khz
= dev
->cfg
.if_dvbt_7
;
78 if_khz
= dev
->cfg
.if_dvbt_8
;
87 switch (c
->bandwidth_hz
) {
89 if_khz
= dev
->cfg
.if_dvbt2_6
;
93 if_khz
= dev
->cfg
.if_dvbt2_7
;
97 if_khz
= dev
->cfg
.if_dvbt2_8
;
105 case SYS_DVBC_ANNEX_A
:
106 case SYS_DVBC_ANNEX_C
:
107 if_khz
= dev
->cfg
.if_dvbc
;
115 ret
= regmap_write(dev
->regmap
, 0x23, bw_params
[i
][2]);
119 ret
= regmap_write(dev
->regmap
, 0x06, 0x00);
123 ret
= regmap_write(dev
->regmap
, 0x0f, bw_params
[i
][0]);
128 buf
[1] = bw_params
[i
][1];
129 buf
[2] = 0x03; /* default value */
130 buf
[3] = DIV_ROUND_CLOSEST(if_khz
, 50);
131 buf
[4] = ((c
->frequency
/ 1000) >> 16) & 0xff;
132 buf
[5] = ((c
->frequency
/ 1000) >> 8) & 0xff;
133 buf
[6] = ((c
->frequency
/ 1000) >> 0) & 0xff;
136 ret
= regmap_bulk_write(dev
->regmap
, 0x12, buf
, sizeof(buf
));
140 /* actual IF rounded as it is on register */
141 dev
->if_frequency
= buf
[3] * 50 * 1000;
144 if (fe
->ops
.i2c_gate_ctrl
)
145 fe
->ops
.i2c_gate_ctrl(fe
, 0); /* close I2C-gate */
150 dev_dbg(&dev
->client
->dev
, "failed=%d\n", ret
);
154 static int tda18212_get_if_frequency(struct dvb_frontend
*fe
, u32
*frequency
)
156 struct tda18212_dev
*dev
= fe
->tuner_priv
;
158 *frequency
= dev
->if_frequency
;
163 static const struct dvb_tuner_ops tda18212_tuner_ops
= {
165 .name
= "NXP TDA18212",
167 .frequency_min_hz
= 48 * MHz
,
168 .frequency_max_hz
= 864 * MHz
,
169 .frequency_step_hz
= 1 * kHz
,
172 .set_params
= tda18212_set_params
,
173 .get_if_frequency
= tda18212_get_if_frequency
,
176 static int tda18212_probe(struct i2c_client
*client
,
177 const struct i2c_device_id
*id
)
179 struct tda18212_config
*cfg
= client
->dev
.platform_data
;
180 struct dvb_frontend
*fe
= cfg
->fe
;
181 struct tda18212_dev
*dev
;
183 unsigned int chip_id
;
185 static const struct regmap_config regmap_config
= {
190 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
193 dev_err(&client
->dev
, "kzalloc() failed\n");
197 memcpy(&dev
->cfg
, cfg
, sizeof(struct tda18212_config
));
198 dev
->client
= client
;
199 dev
->regmap
= devm_regmap_init_i2c(client
, ®map_config
);
200 if (IS_ERR(dev
->regmap
)) {
201 ret
= PTR_ERR(dev
->regmap
);
205 /* check if the tuner is there */
206 if (fe
->ops
.i2c_gate_ctrl
)
207 fe
->ops
.i2c_gate_ctrl(fe
, 1); /* open I2C-gate */
209 ret
= regmap_read(dev
->regmap
, 0x00, &chip_id
);
210 dev_dbg(&dev
->client
->dev
, "chip_id=%02x\n", chip_id
);
212 if (fe
->ops
.i2c_gate_ctrl
)
213 fe
->ops
.i2c_gate_ctrl(fe
, 0); /* close I2C-gate */
220 version
= "M"; /* master */
223 version
= "S"; /* slave */
230 dev_info(&dev
->client
->dev
,
231 "NXP TDA18212HN/%s successfully identified\n", version
);
233 fe
->tuner_priv
= dev
;
234 memcpy(&fe
->ops
.tuner_ops
, &tda18212_tuner_ops
,
235 sizeof(struct dvb_tuner_ops
));
236 i2c_set_clientdata(client
, dev
);
240 dev_dbg(&client
->dev
, "failed=%d\n", ret
);
245 static int tda18212_remove(struct i2c_client
*client
)
247 struct tda18212_dev
*dev
= i2c_get_clientdata(client
);
248 struct dvb_frontend
*fe
= dev
->cfg
.fe
;
250 dev_dbg(&client
->dev
, "\n");
252 memset(&fe
->ops
.tuner_ops
, 0, sizeof(struct dvb_tuner_ops
));
253 fe
->tuner_priv
= NULL
;
259 static const struct i2c_device_id tda18212_id
[] = {
263 MODULE_DEVICE_TABLE(i2c
, tda18212_id
);
265 static struct i2c_driver tda18212_driver
= {
269 .probe
= tda18212_probe
,
270 .remove
= tda18212_remove
,
271 .id_table
= tda18212_id
,
274 module_i2c_driver(tda18212_driver
);
276 MODULE_DESCRIPTION("NXP TDA18212HN silicon tuner driver");
277 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
278 MODULE_LICENSE("GPL");