blob: 7b8068354fea7dffef6583ac26945d2564bed3fe [file] [log] [blame]
Antti Palosaari26eb7042011-04-09 20:07:30 -03001/*
2 * NXP TDA18212HN silicon tuner driver
3 *
4 * Copyright (C) 2011 Antti Palosaari <crope@iki.fi>
5 *
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.
10 *
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.
15 *
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.
19 */
20
Joe Perches2b507632011-07-31 04:30:10 -030021#include "tda18212.h"
Antti Palosaari3b60b762014-08-04 01:00:46 -030022#include <linux/regmap.h>
Mauro Carvalho Chehabf1baab82013-11-02 06:07:42 -030023
Antti Palosaarie4a42e12014-08-03 23:40:23 -030024struct tda18212_dev {
Antti Palosaari0e584cc2014-08-03 23:05:31 -030025 struct tda18212_config cfg;
26 struct i2c_client *client;
Antti Palosaari3b60b762014-08-04 01:00:46 -030027 struct regmap *regmap;
Antti Palosaari835bf822011-11-13 11:20:08 -030028
29 u32 if_frequency;
Joe Perches2b507632011-07-31 04:30:10 -030030};
31
Mauro Carvalho Chehab14d24d12011-12-24 12:24:33 -030032static int tda18212_set_params(struct dvb_frontend *fe)
Antti Palosaari26eb7042011-04-09 20:07:30 -030033{
Antti Palosaarie4a42e12014-08-03 23:40:23 -030034 struct tda18212_dev *dev = fe->tuner_priv;
Antti Palosaari26eb7042011-04-09 20:07:30 -030035 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
36 int ret, i;
37 u32 if_khz;
38 u8 buf[9];
Antti Palosaarib682ad12011-08-12 18:29:10 -030039 #define DVBT_6 0
40 #define DVBT_7 1
41 #define DVBT_8 2
42 #define DVBT2_6 3
43 #define DVBT2_7 4
44 #define DVBT2_8 5
45 #define DVBC_6 6
46 #define DVBC_8 7
Mauro Carvalho Chehab6cff36b2014-03-03 16:27:38 -030047 #define ATSC_VSB 8
48 #define ATSC_QAM 9
Antti Palosaari26eb7042011-04-09 20:07:30 -030049 static const u8 bw_params[][3] = {
Antti Palosaarib682ad12011-08-12 18:29:10 -030050 /* reg: 0f 13 23 */
51 [DVBT_6] = { 0xb3, 0x20, 0x03 },
52 [DVBT_7] = { 0xb3, 0x31, 0x01 },
53 [DVBT_8] = { 0xb3, 0x22, 0x01 },
54 [DVBT2_6] = { 0xbc, 0x20, 0x03 },
55 [DVBT2_7] = { 0xbc, 0x72, 0x03 },
56 [DVBT2_8] = { 0xbc, 0x22, 0x01 },
57 [DVBC_6] = { 0x92, 0x50, 0x03 },
58 [DVBC_8] = { 0x92, 0x53, 0x03 },
Mauro Carvalho Chehab6cff36b2014-03-03 16:27:38 -030059 [ATSC_VSB] = { 0x7d, 0x20, 0x63 },
60 [ATSC_QAM] = { 0x7d, 0x20, 0x63 },
Antti Palosaari26eb7042011-04-09 20:07:30 -030061 };
62
Antti Palosaarie4a42e12014-08-03 23:40:23 -030063 dev_dbg(&dev->client->dev,
Antti Palosaaribdb32652014-08-03 23:26:27 -030064 "delivery_system=%d frequency=%d bandwidth_hz=%d\n",
65 c->delivery_system, c->frequency,
Antti Palosaarid46ddbe2012-08-09 20:50:36 -030066 c->bandwidth_hz);
Antti Palosaari26eb7042011-04-09 20:07:30 -030067
68 if (fe->ops.i2c_gate_ctrl)
69 fe->ops.i2c_gate_ctrl(fe, 1); /* open I2C-gate */
70
71 switch (c->delivery_system) {
Mauro Carvalho Chehab6cff36b2014-03-03 16:27:38 -030072 case SYS_ATSC:
Antti Palosaarie4a42e12014-08-03 23:40:23 -030073 if_khz = dev->cfg.if_atsc_vsb;
Mauro Carvalho Chehab6cff36b2014-03-03 16:27:38 -030074 i = ATSC_VSB;
75 break;
76 case SYS_DVBC_ANNEX_B:
Antti Palosaarie4a42e12014-08-03 23:40:23 -030077 if_khz = dev->cfg.if_atsc_qam;
Mauro Carvalho Chehab6cff36b2014-03-03 16:27:38 -030078 i = ATSC_QAM;
79 break;
Antti Palosaari26eb7042011-04-09 20:07:30 -030080 case SYS_DVBT:
81 switch (c->bandwidth_hz) {
82 case 6000000:
Antti Palosaarie4a42e12014-08-03 23:40:23 -030083 if_khz = dev->cfg.if_dvbt_6;
Antti Palosaarib682ad12011-08-12 18:29:10 -030084 i = DVBT_6;
Antti Palosaari26eb7042011-04-09 20:07:30 -030085 break;
86 case 7000000:
Antti Palosaarie4a42e12014-08-03 23:40:23 -030087 if_khz = dev->cfg.if_dvbt_7;
Antti Palosaarib682ad12011-08-12 18:29:10 -030088 i = DVBT_7;
Antti Palosaari26eb7042011-04-09 20:07:30 -030089 break;
90 case 8000000:
Antti Palosaarie4a42e12014-08-03 23:40:23 -030091 if_khz = dev->cfg.if_dvbt_8;
Antti Palosaarib682ad12011-08-12 18:29:10 -030092 i = DVBT_8;
93 break;
94 default:
95 ret = -EINVAL;
96 goto error;
97 }
98 break;
99 case SYS_DVBT2:
100 switch (c->bandwidth_hz) {
101 case 6000000:
Antti Palosaarie4a42e12014-08-03 23:40:23 -0300102 if_khz = dev->cfg.if_dvbt2_6;
Antti Palosaarib682ad12011-08-12 18:29:10 -0300103 i = DVBT2_6;
104 break;
105 case 7000000:
Antti Palosaarie4a42e12014-08-03 23:40:23 -0300106 if_khz = dev->cfg.if_dvbt2_7;
Antti Palosaarib682ad12011-08-12 18:29:10 -0300107 i = DVBT2_7;
108 break;
109 case 8000000:
Antti Palosaarie4a42e12014-08-03 23:40:23 -0300110 if_khz = dev->cfg.if_dvbt2_8;
Antti Palosaarib682ad12011-08-12 18:29:10 -0300111 i = DVBT2_8;
Antti Palosaari26eb7042011-04-09 20:07:30 -0300112 break;
113 default:
114 ret = -EINVAL;
115 goto error;
116 }
117 break;
Mauro Carvalho Chehab03494712011-12-22 18:11:39 -0300118 case SYS_DVBC_ANNEX_A:
119 case SYS_DVBC_ANNEX_C:
Antti Palosaarie4a42e12014-08-03 23:40:23 -0300120 if_khz = dev->cfg.if_dvbc;
Antti Palosaarib682ad12011-08-12 18:29:10 -0300121 i = DVBC_8;
Antti Palosaari26eb7042011-04-09 20:07:30 -0300122 break;
123 default:
124 ret = -EINVAL;
125 goto error;
126 }
127
Antti Palosaari3b60b762014-08-04 01:00:46 -0300128 ret = regmap_write(dev->regmap, 0x23, bw_params[i][2]);
Antti Palosaari26eb7042011-04-09 20:07:30 -0300129 if (ret)
130 goto error;
131
Antti Palosaari3b60b762014-08-04 01:00:46 -0300132 ret = regmap_write(dev->regmap, 0x06, 0x00);
Antti Palosaari26eb7042011-04-09 20:07:30 -0300133 if (ret)
134 goto error;
135
Antti Palosaari3b60b762014-08-04 01:00:46 -0300136 ret = regmap_write(dev->regmap, 0x0f, bw_params[i][0]);
Antti Palosaari26eb7042011-04-09 20:07:30 -0300137 if (ret)
138 goto error;
139
140 buf[0] = 0x02;
141 buf[1] = bw_params[i][1];
142 buf[2] = 0x03; /* default value */
Antti Palosaari8cffcc72011-11-13 11:22:58 -0300143 buf[3] = DIV_ROUND_CLOSEST(if_khz, 50);
Antti Palosaari26eb7042011-04-09 20:07:30 -0300144 buf[4] = ((c->frequency / 1000) >> 16) & 0xff;
145 buf[5] = ((c->frequency / 1000) >> 8) & 0xff;
146 buf[6] = ((c->frequency / 1000) >> 0) & 0xff;
147 buf[7] = 0xc1;
148 buf[8] = 0x01;
Antti Palosaari3b60b762014-08-04 01:00:46 -0300149 ret = regmap_bulk_write(dev->regmap, 0x12, buf, sizeof(buf));
Antti Palosaari26eb7042011-04-09 20:07:30 -0300150 if (ret)
151 goto error;
152
Antti Palosaari835bf822011-11-13 11:20:08 -0300153 /* actual IF rounded as it is on register */
Antti Palosaarie4a42e12014-08-03 23:40:23 -0300154 dev->if_frequency = buf[3] * 50 * 1000;
Antti Palosaari835bf822011-11-13 11:20:08 -0300155
Antti Palosaari26eb7042011-04-09 20:07:30 -0300156exit:
157 if (fe->ops.i2c_gate_ctrl)
158 fe->ops.i2c_gate_ctrl(fe, 0); /* close I2C-gate */
159
160 return ret;
161
162error:
Antti Palosaarie4a42e12014-08-03 23:40:23 -0300163 dev_dbg(&dev->client->dev, "failed=%d\n", ret);
Antti Palosaari26eb7042011-04-09 20:07:30 -0300164 goto exit;
165}
166
Antti Palosaari835bf822011-11-13 11:20:08 -0300167static int tda18212_get_if_frequency(struct dvb_frontend *fe, u32 *frequency)
168{
Antti Palosaarie4a42e12014-08-03 23:40:23 -0300169 struct tda18212_dev *dev = fe->tuner_priv;
Antti Palosaari835bf822011-11-13 11:20:08 -0300170
Antti Palosaarie4a42e12014-08-03 23:40:23 -0300171 *frequency = dev->if_frequency;
Antti Palosaari835bf822011-11-13 11:20:08 -0300172
173 return 0;
174}
175
Antti Palosaari26eb7042011-04-09 20:07:30 -0300176static const struct dvb_tuner_ops tda18212_tuner_ops = {
177 .info = {
178 .name = "NXP TDA18212",
179
180 .frequency_min = 48000000,
181 .frequency_max = 864000000,
182 .frequency_step = 1000,
183 },
184
Antti Palosaari26eb7042011-04-09 20:07:30 -0300185 .set_params = tda18212_set_params,
Antti Palosaari835bf822011-11-13 11:20:08 -0300186 .get_if_frequency = tda18212_get_if_frequency,
Antti Palosaari26eb7042011-04-09 20:07:30 -0300187};
188
Antti Palosaari0e584cc2014-08-03 23:05:31 -0300189static int tda18212_probe(struct i2c_client *client,
190 const struct i2c_device_id *id)
Antti Palosaari26eb7042011-04-09 20:07:30 -0300191{
Antti Palosaari0e584cc2014-08-03 23:05:31 -0300192 struct tda18212_config *cfg = client->dev.platform_data;
193 struct dvb_frontend *fe = cfg->fe;
Antti Palosaarie4a42e12014-08-03 23:40:23 -0300194 struct tda18212_dev *dev;
Antti Palosaari26eb7042011-04-09 20:07:30 -0300195 int ret;
Antti Palosaari3b60b762014-08-04 01:00:46 -0300196 unsigned int chip_id;
Antti Palosaari976bcb22014-07-31 16:35:56 -0300197 char *version;
Antti Palosaari3b60b762014-08-04 01:00:46 -0300198 static const struct regmap_config regmap_config = {
199 .reg_bits = 8,
200 .val_bits = 8,
201 };
Antti Palosaari26eb7042011-04-09 20:07:30 -0300202
Antti Palosaarie4a42e12014-08-03 23:40:23 -0300203 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
204 if (dev == NULL) {
Antti Palosaari0e584cc2014-08-03 23:05:31 -0300205 ret = -ENOMEM;
Antti Palosaaribdb32652014-08-03 23:26:27 -0300206 dev_err(&client->dev, "kzalloc() failed\n");
Antti Palosaari0e584cc2014-08-03 23:05:31 -0300207 goto err;
208 }
Antti Palosaari26eb7042011-04-09 20:07:30 -0300209
Antti Palosaarie4a42e12014-08-03 23:40:23 -0300210 memcpy(&dev->cfg, cfg, sizeof(struct tda18212_config));
211 dev->client = client;
Antti Palosaari3b60b762014-08-04 01:00:46 -0300212 dev->regmap = devm_regmap_init_i2c(client, &regmap_config);
213 if (IS_ERR(dev->regmap)) {
214 ret = PTR_ERR(dev->regmap);
215 goto err;
216 }
Antti Palosaari26eb7042011-04-09 20:07:30 -0300217
Antti Palosaari0e584cc2014-08-03 23:05:31 -0300218 /* check if the tuner is there */
Antti Palosaari26eb7042011-04-09 20:07:30 -0300219 if (fe->ops.i2c_gate_ctrl)
220 fe->ops.i2c_gate_ctrl(fe, 1); /* open I2C-gate */
221
Antti Palosaari3b60b762014-08-04 01:00:46 -0300222 ret = regmap_read(dev->regmap, 0x00, &chip_id);
Antti Palosaarie4a42e12014-08-03 23:40:23 -0300223 dev_dbg(&dev->client->dev, "chip_id=%02x\n", chip_id);
Antti Palosaari26eb7042011-04-09 20:07:30 -0300224
225 if (fe->ops.i2c_gate_ctrl)
226 fe->ops.i2c_gate_ctrl(fe, 0); /* close I2C-gate */
227
Antti Palosaari976bcb22014-07-31 16:35:56 -0300228 if (ret)
229 goto err;
230
231 switch (chip_id) {
232 case 0xc7:
233 version = "M"; /* master */
234 break;
235 case 0x47:
236 version = "S"; /* slave */
237 break;
238 default:
Antti Palosaari0e584cc2014-08-03 23:05:31 -0300239 ret = -ENODEV;
Antti Palosaari976bcb22014-07-31 16:35:56 -0300240 goto err;
Antti Palosaari26eb7042011-04-09 20:07:30 -0300241 }
242
Antti Palosaarie4a42e12014-08-03 23:40:23 -0300243 dev_info(&dev->client->dev,
Antti Palosaaribdb32652014-08-03 23:26:27 -0300244 "NXP TDA18212HN/%s successfully identified\n", version);
Antti Palosaari26eb7042011-04-09 20:07:30 -0300245
Antti Palosaarie4a42e12014-08-03 23:40:23 -0300246 fe->tuner_priv = dev;
Antti Palosaari26eb7042011-04-09 20:07:30 -0300247 memcpy(&fe->ops.tuner_ops, &tda18212_tuner_ops,
Antti Palosaari0e584cc2014-08-03 23:05:31 -0300248 sizeof(struct dvb_tuner_ops));
Antti Palosaarie4a42e12014-08-03 23:40:23 -0300249 i2c_set_clientdata(client, dev);
Antti Palosaari26eb7042011-04-09 20:07:30 -0300250
Antti Palosaari0e584cc2014-08-03 23:05:31 -0300251 return 0;
Antti Palosaari976bcb22014-07-31 16:35:56 -0300252err:
Antti Palosaaribdb32652014-08-03 23:26:27 -0300253 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaarie4a42e12014-08-03 23:40:23 -0300254 kfree(dev);
Antti Palosaari0e584cc2014-08-03 23:05:31 -0300255 return ret;
Antti Palosaari26eb7042011-04-09 20:07:30 -0300256}
Antti Palosaari0e584cc2014-08-03 23:05:31 -0300257
258static int tda18212_remove(struct i2c_client *client)
259{
Antti Palosaarie4a42e12014-08-03 23:40:23 -0300260 struct tda18212_dev *dev = i2c_get_clientdata(client);
261 struct dvb_frontend *fe = dev->cfg.fe;
Antti Palosaari0e584cc2014-08-03 23:05:31 -0300262
Antti Palosaaribdb32652014-08-03 23:26:27 -0300263 dev_dbg(&client->dev, "\n");
Antti Palosaari0e584cc2014-08-03 23:05:31 -0300264
265 memset(&fe->ops.tuner_ops, 0, sizeof(struct dvb_tuner_ops));
266 fe->tuner_priv = NULL;
Antti Palosaarie4a42e12014-08-03 23:40:23 -0300267 kfree(dev);
Antti Palosaari0e584cc2014-08-03 23:05:31 -0300268
269 return 0;
270}
271
272static const struct i2c_device_id tda18212_id[] = {
273 {"tda18212", 0},
274 {}
275};
276MODULE_DEVICE_TABLE(i2c, tda18212_id);
277
278static struct i2c_driver tda18212_driver = {
279 .driver = {
Antti Palosaari0e584cc2014-08-03 23:05:31 -0300280 .name = "tda18212",
281 },
282 .probe = tda18212_probe,
283 .remove = tda18212_remove,
284 .id_table = tda18212_id,
285};
286
287module_i2c_driver(tda18212_driver);
Antti Palosaari26eb7042011-04-09 20:07:30 -0300288
289MODULE_DESCRIPTION("NXP TDA18212HN silicon tuner driver");
290MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
291MODULE_LICENSE("GPL");