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)
178 struct tda18212_config *cfg = client->dev.platform_data;
179 struct dvb_frontend *fe = cfg->fe;
180 struct tda18212_dev *dev;
182 unsigned int chip_id;
184 static const struct regmap_config regmap_config = {
189 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
192 dev_err(&client->dev, "kzalloc() failed\n");
196 memcpy(&dev->cfg, cfg, sizeof(struct tda18212_config));
197 dev->client = client;
198 dev->regmap = devm_regmap_init_i2c(client, ®map_config);
199 if (IS_ERR(dev->regmap)) {
200 ret = PTR_ERR(dev->regmap);
204 /* check if the tuner is there */
205 if (fe->ops.i2c_gate_ctrl)
206 fe->ops.i2c_gate_ctrl(fe, 1); /* open I2C-gate */
208 ret = regmap_read(dev->regmap, 0x00, &chip_id);
209 dev_dbg(&dev->client->dev, "chip_id=%02x\n", chip_id);
211 if (fe->ops.i2c_gate_ctrl)
212 fe->ops.i2c_gate_ctrl(fe, 0); /* close I2C-gate */
219 version = "M"; /* master */
222 version = "S"; /* slave */
229 dev_info(&dev->client->dev,
230 "NXP TDA18212HN/%s successfully identified\n", version);
232 fe->tuner_priv = dev;
233 memcpy(&fe->ops.tuner_ops, &tda18212_tuner_ops,
234 sizeof(struct dvb_tuner_ops));
235 i2c_set_clientdata(client, dev);
239 dev_dbg(&client->dev, "failed=%d\n", ret);
244 static void tda18212_remove(struct i2c_client *client)
246 struct tda18212_dev *dev = i2c_get_clientdata(client);
247 struct dvb_frontend *fe = dev->cfg.fe;
249 dev_dbg(&client->dev, "\n");
251 memset(&fe->ops.tuner_ops, 0, sizeof(struct dvb_tuner_ops));
252 fe->tuner_priv = NULL;
256 static const struct i2c_device_id tda18212_id[] = {
260 MODULE_DEVICE_TABLE(i2c, tda18212_id);
262 static struct i2c_driver tda18212_driver = {
266 .probe = tda18212_probe,
267 .remove = tda18212_remove,
268 .id_table = tda18212_id,
271 module_i2c_driver(tda18212_driver);
273 MODULE_DESCRIPTION("NXP TDA18212HN silicon tuner driver");
274 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
275 MODULE_LICENSE("GPL");