blob: bf48f1cd83d23f559c8002179f2eba1c909251f6 [file] [log] [blame]
Thomas Gleixner16216332019-05-19 15:51:31 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Antti Palosaari26eb7042011-04-09 20:07:30 -03002/*
3 * NXP TDA18212HN silicon tuner driver
4 *
5 * Copyright (C) 2011 Antti Palosaari <crope@iki.fi>
Antti Palosaari26eb7042011-04-09 20:07:30 -03006 */
7
Joe Perches2b507632011-07-31 04:30:10 -03008#include "tda18212.h"
Antti Palosaari3b60b762014-08-04 01:00:46 -03009#include <linux/regmap.h>
Mauro Carvalho Chehabf1baab82013-11-02 06:07:42 -030010
Antti Palosaarie4a42e12014-08-03 23:40:23 -030011struct tda18212_dev {
Antti Palosaari0e584cc2014-08-03 23:05:31 -030012 struct tda18212_config cfg;
13 struct i2c_client *client;
Antti Palosaari3b60b762014-08-04 01:00:46 -030014 struct regmap *regmap;
Antti Palosaari835bf822011-11-13 11:20:08 -030015
16 u32 if_frequency;
Joe Perches2b507632011-07-31 04:30:10 -030017};
18
Mauro Carvalho Chehab14d24d12011-12-24 12:24:33 -030019static int tda18212_set_params(struct dvb_frontend *fe)
Antti Palosaari26eb7042011-04-09 20:07:30 -030020{
Antti Palosaarie4a42e12014-08-03 23:40:23 -030021 struct tda18212_dev *dev = fe->tuner_priv;
Antti Palosaari26eb7042011-04-09 20:07:30 -030022 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
23 int ret, i;
24 u32 if_khz;
25 u8 buf[9];
Antti Palosaarib682ad12011-08-12 18:29:10 -030026 #define DVBT_6 0
27 #define DVBT_7 1
28 #define DVBT_8 2
29 #define DVBT2_6 3
30 #define DVBT2_7 4
31 #define DVBT2_8 5
32 #define DVBC_6 6
33 #define DVBC_8 7
Mauro Carvalho Chehab6cff36b2014-03-03 16:27:38 -030034 #define ATSC_VSB 8
35 #define ATSC_QAM 9
Antti Palosaari26eb7042011-04-09 20:07:30 -030036 static const u8 bw_params[][3] = {
Antti Palosaarib682ad12011-08-12 18:29:10 -030037 /* reg: 0f 13 23 */
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 },
Mauro Carvalho Chehab6cff36b2014-03-03 16:27:38 -030046 [ATSC_VSB] = { 0x7d, 0x20, 0x63 },
47 [ATSC_QAM] = { 0x7d, 0x20, 0x63 },
Antti Palosaari26eb7042011-04-09 20:07:30 -030048 };
49
Antti Palosaarie4a42e12014-08-03 23:40:23 -030050 dev_dbg(&dev->client->dev,
Antti Palosaaribdb32652014-08-03 23:26:27 -030051 "delivery_system=%d frequency=%d bandwidth_hz=%d\n",
52 c->delivery_system, c->frequency,
Antti Palosaarid46ddbe2012-08-09 20:50:36 -030053 c->bandwidth_hz);
Antti Palosaari26eb7042011-04-09 20:07:30 -030054
55 if (fe->ops.i2c_gate_ctrl)
56 fe->ops.i2c_gate_ctrl(fe, 1); /* open I2C-gate */
57
58 switch (c->delivery_system) {
Mauro Carvalho Chehab6cff36b2014-03-03 16:27:38 -030059 case SYS_ATSC:
Antti Palosaarie4a42e12014-08-03 23:40:23 -030060 if_khz = dev->cfg.if_atsc_vsb;
Mauro Carvalho Chehab6cff36b2014-03-03 16:27:38 -030061 i = ATSC_VSB;
62 break;
63 case SYS_DVBC_ANNEX_B:
Antti Palosaarie4a42e12014-08-03 23:40:23 -030064 if_khz = dev->cfg.if_atsc_qam;
Mauro Carvalho Chehab6cff36b2014-03-03 16:27:38 -030065 i = ATSC_QAM;
66 break;
Antti Palosaari26eb7042011-04-09 20:07:30 -030067 case SYS_DVBT:
68 switch (c->bandwidth_hz) {
69 case 6000000:
Antti Palosaarie4a42e12014-08-03 23:40:23 -030070 if_khz = dev->cfg.if_dvbt_6;
Antti Palosaarib682ad12011-08-12 18:29:10 -030071 i = DVBT_6;
Antti Palosaari26eb7042011-04-09 20:07:30 -030072 break;
73 case 7000000:
Antti Palosaarie4a42e12014-08-03 23:40:23 -030074 if_khz = dev->cfg.if_dvbt_7;
Antti Palosaarib682ad12011-08-12 18:29:10 -030075 i = DVBT_7;
Antti Palosaari26eb7042011-04-09 20:07:30 -030076 break;
77 case 8000000:
Antti Palosaarie4a42e12014-08-03 23:40:23 -030078 if_khz = dev->cfg.if_dvbt_8;
Antti Palosaarib682ad12011-08-12 18:29:10 -030079 i = DVBT_8;
80 break;
81 default:
82 ret = -EINVAL;
83 goto error;
84 }
85 break;
86 case SYS_DVBT2:
87 switch (c->bandwidth_hz) {
88 case 6000000:
Antti Palosaarie4a42e12014-08-03 23:40:23 -030089 if_khz = dev->cfg.if_dvbt2_6;
Antti Palosaarib682ad12011-08-12 18:29:10 -030090 i = DVBT2_6;
91 break;
92 case 7000000:
Antti Palosaarie4a42e12014-08-03 23:40:23 -030093 if_khz = dev->cfg.if_dvbt2_7;
Antti Palosaarib682ad12011-08-12 18:29:10 -030094 i = DVBT2_7;
95 break;
96 case 8000000:
Antti Palosaarie4a42e12014-08-03 23:40:23 -030097 if_khz = dev->cfg.if_dvbt2_8;
Antti Palosaarib682ad12011-08-12 18:29:10 -030098 i = DVBT2_8;
Antti Palosaari26eb7042011-04-09 20:07:30 -030099 break;
100 default:
101 ret = -EINVAL;
102 goto error;
103 }
104 break;
Mauro Carvalho Chehab03494712011-12-22 18:11:39 -0300105 case SYS_DVBC_ANNEX_A:
106 case SYS_DVBC_ANNEX_C:
Antti Palosaarie4a42e12014-08-03 23:40:23 -0300107 if_khz = dev->cfg.if_dvbc;
Antti Palosaarib682ad12011-08-12 18:29:10 -0300108 i = DVBC_8;
Antti Palosaari26eb7042011-04-09 20:07:30 -0300109 break;
110 default:
111 ret = -EINVAL;
112 goto error;
113 }
114
Antti Palosaari3b60b762014-08-04 01:00:46 -0300115 ret = regmap_write(dev->regmap, 0x23, bw_params[i][2]);
Antti Palosaari26eb7042011-04-09 20:07:30 -0300116 if (ret)
117 goto error;
118
Antti Palosaari3b60b762014-08-04 01:00:46 -0300119 ret = regmap_write(dev->regmap, 0x06, 0x00);
Antti Palosaari26eb7042011-04-09 20:07:30 -0300120 if (ret)
121 goto error;
122
Antti Palosaari3b60b762014-08-04 01:00:46 -0300123 ret = regmap_write(dev->regmap, 0x0f, bw_params[i][0]);
Antti Palosaari26eb7042011-04-09 20:07:30 -0300124 if (ret)
125 goto error;
126
127 buf[0] = 0x02;
128 buf[1] = bw_params[i][1];
129 buf[2] = 0x03; /* default value */
Antti Palosaari8cffcc72011-11-13 11:22:58 -0300130 buf[3] = DIV_ROUND_CLOSEST(if_khz, 50);
Antti Palosaari26eb7042011-04-09 20:07:30 -0300131 buf[4] = ((c->frequency / 1000) >> 16) & 0xff;
132 buf[5] = ((c->frequency / 1000) >> 8) & 0xff;
133 buf[6] = ((c->frequency / 1000) >> 0) & 0xff;
134 buf[7] = 0xc1;
135 buf[8] = 0x01;
Antti Palosaari3b60b762014-08-04 01:00:46 -0300136 ret = regmap_bulk_write(dev->regmap, 0x12, buf, sizeof(buf));
Antti Palosaari26eb7042011-04-09 20:07:30 -0300137 if (ret)
138 goto error;
139
Antti Palosaari835bf822011-11-13 11:20:08 -0300140 /* actual IF rounded as it is on register */
Antti Palosaarie4a42e12014-08-03 23:40:23 -0300141 dev->if_frequency = buf[3] * 50 * 1000;
Antti Palosaari835bf822011-11-13 11:20:08 -0300142
Antti Palosaari26eb7042011-04-09 20:07:30 -0300143exit:
144 if (fe->ops.i2c_gate_ctrl)
145 fe->ops.i2c_gate_ctrl(fe, 0); /* close I2C-gate */
146
147 return ret;
148
149error:
Antti Palosaarie4a42e12014-08-03 23:40:23 -0300150 dev_dbg(&dev->client->dev, "failed=%d\n", ret);
Antti Palosaari26eb7042011-04-09 20:07:30 -0300151 goto exit;
152}
153
Antti Palosaari835bf822011-11-13 11:20:08 -0300154static int tda18212_get_if_frequency(struct dvb_frontend *fe, u32 *frequency)
155{
Antti Palosaarie4a42e12014-08-03 23:40:23 -0300156 struct tda18212_dev *dev = fe->tuner_priv;
Antti Palosaari835bf822011-11-13 11:20:08 -0300157
Antti Palosaarie4a42e12014-08-03 23:40:23 -0300158 *frequency = dev->if_frequency;
Antti Palosaari835bf822011-11-13 11:20:08 -0300159
160 return 0;
161}
162
Antti Palosaari26eb7042011-04-09 20:07:30 -0300163static const struct dvb_tuner_ops tda18212_tuner_ops = {
164 .info = {
Mauro Carvalho Chehaba3f90c72018-07-05 18:59:35 -0400165 .name = "NXP TDA18212",
Antti Palosaari26eb7042011-04-09 20:07:30 -0300166
Mauro Carvalho Chehaba3f90c72018-07-05 18:59:35 -0400167 .frequency_min_hz = 48 * MHz,
168 .frequency_max_hz = 864 * MHz,
169 .frequency_step_hz = 1 * kHz,
Antti Palosaari26eb7042011-04-09 20:07:30 -0300170 },
171
Antti Palosaari26eb7042011-04-09 20:07:30 -0300172 .set_params = tda18212_set_params,
Antti Palosaari835bf822011-11-13 11:20:08 -0300173 .get_if_frequency = tda18212_get_if_frequency,
Antti Palosaari26eb7042011-04-09 20:07:30 -0300174};
175
Antti Palosaari0e584cc2014-08-03 23:05:31 -0300176static int tda18212_probe(struct i2c_client *client,
177 const struct i2c_device_id *id)
Antti Palosaari26eb7042011-04-09 20:07:30 -0300178{
Antti Palosaari0e584cc2014-08-03 23:05:31 -0300179 struct tda18212_config *cfg = client->dev.platform_data;
180 struct dvb_frontend *fe = cfg->fe;
Antti Palosaarie4a42e12014-08-03 23:40:23 -0300181 struct tda18212_dev *dev;
Antti Palosaari26eb7042011-04-09 20:07:30 -0300182 int ret;
Antti Palosaari3b60b762014-08-04 01:00:46 -0300183 unsigned int chip_id;
Antti Palosaari976bcb22014-07-31 16:35:56 -0300184 char *version;
Antti Palosaari3b60b762014-08-04 01:00:46 -0300185 static const struct regmap_config regmap_config = {
186 .reg_bits = 8,
187 .val_bits = 8,
188 };
Antti Palosaari26eb7042011-04-09 20:07:30 -0300189
Antti Palosaarie4a42e12014-08-03 23:40:23 -0300190 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
191 if (dev == NULL) {
Antti Palosaari0e584cc2014-08-03 23:05:31 -0300192 ret = -ENOMEM;
Antti Palosaaribdb32652014-08-03 23:26:27 -0300193 dev_err(&client->dev, "kzalloc() failed\n");
Antti Palosaari0e584cc2014-08-03 23:05:31 -0300194 goto err;
195 }
Antti Palosaari26eb7042011-04-09 20:07:30 -0300196
Antti Palosaarie4a42e12014-08-03 23:40:23 -0300197 memcpy(&dev->cfg, cfg, sizeof(struct tda18212_config));
198 dev->client = client;
Antti Palosaari3b60b762014-08-04 01:00:46 -0300199 dev->regmap = devm_regmap_init_i2c(client, &regmap_config);
200 if (IS_ERR(dev->regmap)) {
201 ret = PTR_ERR(dev->regmap);
202 goto err;
203 }
Antti Palosaari26eb7042011-04-09 20:07:30 -0300204
Antti Palosaari0e584cc2014-08-03 23:05:31 -0300205 /* check if the tuner is there */
Antti Palosaari26eb7042011-04-09 20:07:30 -0300206 if (fe->ops.i2c_gate_ctrl)
207 fe->ops.i2c_gate_ctrl(fe, 1); /* open I2C-gate */
208
Antti Palosaari3b60b762014-08-04 01:00:46 -0300209 ret = regmap_read(dev->regmap, 0x00, &chip_id);
Antti Palosaarie4a42e12014-08-03 23:40:23 -0300210 dev_dbg(&dev->client->dev, "chip_id=%02x\n", chip_id);
Antti Palosaari26eb7042011-04-09 20:07:30 -0300211
212 if (fe->ops.i2c_gate_ctrl)
213 fe->ops.i2c_gate_ctrl(fe, 0); /* close I2C-gate */
214
Antti Palosaari976bcb22014-07-31 16:35:56 -0300215 if (ret)
216 goto err;
217
218 switch (chip_id) {
219 case 0xc7:
220 version = "M"; /* master */
221 break;
222 case 0x47:
223 version = "S"; /* slave */
224 break;
225 default:
Antti Palosaari0e584cc2014-08-03 23:05:31 -0300226 ret = -ENODEV;
Antti Palosaari976bcb22014-07-31 16:35:56 -0300227 goto err;
Antti Palosaari26eb7042011-04-09 20:07:30 -0300228 }
229
Antti Palosaarie4a42e12014-08-03 23:40:23 -0300230 dev_info(&dev->client->dev,
Antti Palosaaribdb32652014-08-03 23:26:27 -0300231 "NXP TDA18212HN/%s successfully identified\n", version);
Antti Palosaari26eb7042011-04-09 20:07:30 -0300232
Antti Palosaarie4a42e12014-08-03 23:40:23 -0300233 fe->tuner_priv = dev;
Antti Palosaari26eb7042011-04-09 20:07:30 -0300234 memcpy(&fe->ops.tuner_ops, &tda18212_tuner_ops,
Antti Palosaari0e584cc2014-08-03 23:05:31 -0300235 sizeof(struct dvb_tuner_ops));
Antti Palosaarie4a42e12014-08-03 23:40:23 -0300236 i2c_set_clientdata(client, dev);
Antti Palosaari26eb7042011-04-09 20:07:30 -0300237
Antti Palosaari0e584cc2014-08-03 23:05:31 -0300238 return 0;
Antti Palosaari976bcb22014-07-31 16:35:56 -0300239err:
Antti Palosaaribdb32652014-08-03 23:26:27 -0300240 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaarie4a42e12014-08-03 23:40:23 -0300241 kfree(dev);
Antti Palosaari0e584cc2014-08-03 23:05:31 -0300242 return ret;
Antti Palosaari26eb7042011-04-09 20:07:30 -0300243}
Antti Palosaari0e584cc2014-08-03 23:05:31 -0300244
245static int tda18212_remove(struct i2c_client *client)
246{
Antti Palosaarie4a42e12014-08-03 23:40:23 -0300247 struct tda18212_dev *dev = i2c_get_clientdata(client);
248 struct dvb_frontend *fe = dev->cfg.fe;
Antti Palosaari0e584cc2014-08-03 23:05:31 -0300249
Antti Palosaaribdb32652014-08-03 23:26:27 -0300250 dev_dbg(&client->dev, "\n");
Antti Palosaari0e584cc2014-08-03 23:05:31 -0300251
252 memset(&fe->ops.tuner_ops, 0, sizeof(struct dvb_tuner_ops));
253 fe->tuner_priv = NULL;
Antti Palosaarie4a42e12014-08-03 23:40:23 -0300254 kfree(dev);
Antti Palosaari0e584cc2014-08-03 23:05:31 -0300255
256 return 0;
257}
258
259static const struct i2c_device_id tda18212_id[] = {
260 {"tda18212", 0},
261 {}
262};
263MODULE_DEVICE_TABLE(i2c, tda18212_id);
264
265static struct i2c_driver tda18212_driver = {
266 .driver = {
Antti Palosaari0e584cc2014-08-03 23:05:31 -0300267 .name = "tda18212",
268 },
269 .probe = tda18212_probe,
270 .remove = tda18212_remove,
271 .id_table = tda18212_id,
272};
273
274module_i2c_driver(tda18212_driver);
Antti Palosaari26eb7042011-04-09 20:07:30 -0300275
276MODULE_DESCRIPTION("NXP TDA18212HN silicon tuner driver");
277MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
278MODULE_LICENSE("GPL");