blob: 24948c7971e3b11ad0d124f00bdeb620e1afdf94 [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"
22
Mauro Carvalho Chehabf1baab82013-11-02 06:07:42 -030023/* Max transfer size done by I2C transfer functions */
24#define MAX_XFER_SIZE 64
25
Antti Palosaarie4a42e12014-08-03 23:40:23 -030026struct tda18212_dev {
Antti Palosaari0e584cc2014-08-03 23:05:31 -030027 struct tda18212_config cfg;
28 struct i2c_client *client;
Antti Palosaari835bf822011-11-13 11:20:08 -030029
30 u32 if_frequency;
Joe Perches2b507632011-07-31 04:30:10 -030031};
32
Antti Palosaari26eb7042011-04-09 20:07:30 -030033/* write multiple registers */
Antti Palosaarie4a42e12014-08-03 23:40:23 -030034static int tda18212_wr_regs(struct tda18212_dev *dev, u8 reg, u8 *val, int len)
Antti Palosaari26eb7042011-04-09 20:07:30 -030035{
36 int ret;
Mauro Carvalho Chehabf1baab82013-11-02 06:07:42 -030037 u8 buf[MAX_XFER_SIZE];
Antti Palosaari26eb7042011-04-09 20:07:30 -030038 struct i2c_msg msg[1] = {
39 {
Antti Palosaarie4a42e12014-08-03 23:40:23 -030040 .addr = dev->client->addr,
Antti Palosaari26eb7042011-04-09 20:07:30 -030041 .flags = 0,
Mauro Carvalho Chehabf1baab82013-11-02 06:07:42 -030042 .len = 1 + len,
Antti Palosaari26eb7042011-04-09 20:07:30 -030043 .buf = buf,
44 }
45 };
46
Mauro Carvalho Chehabf1baab82013-11-02 06:07:42 -030047 if (1 + len > sizeof(buf)) {
Antti Palosaarie4a42e12014-08-03 23:40:23 -030048 dev_warn(&dev->client->dev,
Antti Palosaaribdb32652014-08-03 23:26:27 -030049 "i2c wr reg=%04x: len=%d is too big!\n",
50 reg, len);
Mauro Carvalho Chehabf1baab82013-11-02 06:07:42 -030051 return -EINVAL;
52 }
53
Antti Palosaari26eb7042011-04-09 20:07:30 -030054 buf[0] = reg;
55 memcpy(&buf[1], val, len);
56
Antti Palosaarie4a42e12014-08-03 23:40:23 -030057 ret = i2c_transfer(dev->client->adapter, msg, 1);
Antti Palosaari26eb7042011-04-09 20:07:30 -030058 if (ret == 1) {
59 ret = 0;
60 } else {
Antti Palosaarie4a42e12014-08-03 23:40:23 -030061 dev_warn(&dev->client->dev,
Antti Palosaaribdb32652014-08-03 23:26:27 -030062 "i2c wr failed=%d reg=%02x len=%d\n",
63 ret, reg, len);
Antti Palosaari26eb7042011-04-09 20:07:30 -030064 ret = -EREMOTEIO;
65 }
66 return ret;
67}
68
69/* read multiple registers */
Antti Palosaarie4a42e12014-08-03 23:40:23 -030070static int tda18212_rd_regs(struct tda18212_dev *dev, u8 reg, u8 *val, int len)
Antti Palosaari26eb7042011-04-09 20:07:30 -030071{
72 int ret;
Mauro Carvalho Chehabf1baab82013-11-02 06:07:42 -030073 u8 buf[MAX_XFER_SIZE];
Antti Palosaari26eb7042011-04-09 20:07:30 -030074 struct i2c_msg msg[2] = {
75 {
Antti Palosaarie4a42e12014-08-03 23:40:23 -030076 .addr = dev->client->addr,
Antti Palosaari26eb7042011-04-09 20:07:30 -030077 .flags = 0,
78 .len = 1,
79 .buf = &reg,
80 }, {
Antti Palosaarie4a42e12014-08-03 23:40:23 -030081 .addr = dev->client->addr,
Antti Palosaari26eb7042011-04-09 20:07:30 -030082 .flags = I2C_M_RD,
Mauro Carvalho Chehabf1baab82013-11-02 06:07:42 -030083 .len = len,
Antti Palosaari26eb7042011-04-09 20:07:30 -030084 .buf = buf,
85 }
86 };
87
Mauro Carvalho Chehabf1baab82013-11-02 06:07:42 -030088 if (len > sizeof(buf)) {
Antti Palosaarie4a42e12014-08-03 23:40:23 -030089 dev_warn(&dev->client->dev,
Antti Palosaaribdb32652014-08-03 23:26:27 -030090 "i2c rd reg=%04x: len=%d is too big!\n",
91 reg, len);
Mauro Carvalho Chehabf1baab82013-11-02 06:07:42 -030092 return -EINVAL;
93 }
94
Antti Palosaarie4a42e12014-08-03 23:40:23 -030095 ret = i2c_transfer(dev->client->adapter, msg, 2);
Antti Palosaari26eb7042011-04-09 20:07:30 -030096 if (ret == 2) {
97 memcpy(val, buf, len);
98 ret = 0;
99 } else {
Antti Palosaarie4a42e12014-08-03 23:40:23 -0300100 dev_warn(&dev->client->dev,
Antti Palosaaribdb32652014-08-03 23:26:27 -0300101 "i2c rd failed=%d reg=%02x len=%d\n",
102 ret, reg, len);
Antti Palosaari26eb7042011-04-09 20:07:30 -0300103 ret = -EREMOTEIO;
104 }
105
106 return ret;
107}
108
109/* write single register */
Antti Palosaarie4a42e12014-08-03 23:40:23 -0300110static int tda18212_wr_reg(struct tda18212_dev *dev, u8 reg, u8 val)
Antti Palosaari26eb7042011-04-09 20:07:30 -0300111{
Antti Palosaarie4a42e12014-08-03 23:40:23 -0300112 return tda18212_wr_regs(dev, reg, &val, 1);
Antti Palosaari26eb7042011-04-09 20:07:30 -0300113}
114
115/* read single register */
Antti Palosaarie4a42e12014-08-03 23:40:23 -0300116static int tda18212_rd_reg(struct tda18212_dev *dev, u8 reg, u8 *val)
Antti Palosaari26eb7042011-04-09 20:07:30 -0300117{
Antti Palosaarie4a42e12014-08-03 23:40:23 -0300118 return tda18212_rd_regs(dev, reg, val, 1);
Antti Palosaari26eb7042011-04-09 20:07:30 -0300119}
120
121#if 0 /* keep, useful when developing driver */
Antti Palosaarie4a42e12014-08-03 23:40:23 -0300122static void tda18212_dump_regs(struct tda18212_dev *dev)
Antti Palosaari26eb7042011-04-09 20:07:30 -0300123{
124 int i;
125 u8 buf[256];
126
127 #define TDA18212_RD_LEN 32
128 for (i = 0; i < sizeof(buf); i += TDA18212_RD_LEN)
Antti Palosaarie4a42e12014-08-03 23:40:23 -0300129 tda18212_rd_regs(dev, i, &buf[i], TDA18212_RD_LEN);
Antti Palosaari26eb7042011-04-09 20:07:30 -0300130
131 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET, 32, 1, buf,
132 sizeof(buf), true);
133
134 return;
135}
136#endif
137
Mauro Carvalho Chehab14d24d12011-12-24 12:24:33 -0300138static int tda18212_set_params(struct dvb_frontend *fe)
Antti Palosaari26eb7042011-04-09 20:07:30 -0300139{
Antti Palosaarie4a42e12014-08-03 23:40:23 -0300140 struct tda18212_dev *dev = fe->tuner_priv;
Antti Palosaari26eb7042011-04-09 20:07:30 -0300141 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
142 int ret, i;
143 u32 if_khz;
144 u8 buf[9];
Antti Palosaarib682ad12011-08-12 18:29:10 -0300145 #define DVBT_6 0
146 #define DVBT_7 1
147 #define DVBT_8 2
148 #define DVBT2_6 3
149 #define DVBT2_7 4
150 #define DVBT2_8 5
151 #define DVBC_6 6
152 #define DVBC_8 7
Mauro Carvalho Chehab6cff36b2014-03-03 16:27:38 -0300153 #define ATSC_VSB 8
154 #define ATSC_QAM 9
Antti Palosaari26eb7042011-04-09 20:07:30 -0300155 static const u8 bw_params[][3] = {
Antti Palosaarib682ad12011-08-12 18:29:10 -0300156 /* reg: 0f 13 23 */
157 [DVBT_6] = { 0xb3, 0x20, 0x03 },
158 [DVBT_7] = { 0xb3, 0x31, 0x01 },
159 [DVBT_8] = { 0xb3, 0x22, 0x01 },
160 [DVBT2_6] = { 0xbc, 0x20, 0x03 },
161 [DVBT2_7] = { 0xbc, 0x72, 0x03 },
162 [DVBT2_8] = { 0xbc, 0x22, 0x01 },
163 [DVBC_6] = { 0x92, 0x50, 0x03 },
164 [DVBC_8] = { 0x92, 0x53, 0x03 },
Mauro Carvalho Chehab6cff36b2014-03-03 16:27:38 -0300165 [ATSC_VSB] = { 0x7d, 0x20, 0x63 },
166 [ATSC_QAM] = { 0x7d, 0x20, 0x63 },
Antti Palosaari26eb7042011-04-09 20:07:30 -0300167 };
168
Antti Palosaarie4a42e12014-08-03 23:40:23 -0300169 dev_dbg(&dev->client->dev,
Antti Palosaaribdb32652014-08-03 23:26:27 -0300170 "delivery_system=%d frequency=%d bandwidth_hz=%d\n",
171 c->delivery_system, c->frequency,
Antti Palosaarid46ddbe2012-08-09 20:50:36 -0300172 c->bandwidth_hz);
Antti Palosaari26eb7042011-04-09 20:07:30 -0300173
174 if (fe->ops.i2c_gate_ctrl)
175 fe->ops.i2c_gate_ctrl(fe, 1); /* open I2C-gate */
176
177 switch (c->delivery_system) {
Mauro Carvalho Chehab6cff36b2014-03-03 16:27:38 -0300178 case SYS_ATSC:
Antti Palosaarie4a42e12014-08-03 23:40:23 -0300179 if_khz = dev->cfg.if_atsc_vsb;
Mauro Carvalho Chehab6cff36b2014-03-03 16:27:38 -0300180 i = ATSC_VSB;
181 break;
182 case SYS_DVBC_ANNEX_B:
Antti Palosaarie4a42e12014-08-03 23:40:23 -0300183 if_khz = dev->cfg.if_atsc_qam;
Mauro Carvalho Chehab6cff36b2014-03-03 16:27:38 -0300184 i = ATSC_QAM;
185 break;
Antti Palosaari26eb7042011-04-09 20:07:30 -0300186 case SYS_DVBT:
187 switch (c->bandwidth_hz) {
188 case 6000000:
Antti Palosaarie4a42e12014-08-03 23:40:23 -0300189 if_khz = dev->cfg.if_dvbt_6;
Antti Palosaarib682ad12011-08-12 18:29:10 -0300190 i = DVBT_6;
Antti Palosaari26eb7042011-04-09 20:07:30 -0300191 break;
192 case 7000000:
Antti Palosaarie4a42e12014-08-03 23:40:23 -0300193 if_khz = dev->cfg.if_dvbt_7;
Antti Palosaarib682ad12011-08-12 18:29:10 -0300194 i = DVBT_7;
Antti Palosaari26eb7042011-04-09 20:07:30 -0300195 break;
196 case 8000000:
Antti Palosaarie4a42e12014-08-03 23:40:23 -0300197 if_khz = dev->cfg.if_dvbt_8;
Antti Palosaarib682ad12011-08-12 18:29:10 -0300198 i = DVBT_8;
199 break;
200 default:
201 ret = -EINVAL;
202 goto error;
203 }
204 break;
205 case SYS_DVBT2:
206 switch (c->bandwidth_hz) {
207 case 6000000:
Antti Palosaarie4a42e12014-08-03 23:40:23 -0300208 if_khz = dev->cfg.if_dvbt2_6;
Antti Palosaarib682ad12011-08-12 18:29:10 -0300209 i = DVBT2_6;
210 break;
211 case 7000000:
Antti Palosaarie4a42e12014-08-03 23:40:23 -0300212 if_khz = dev->cfg.if_dvbt2_7;
Antti Palosaarib682ad12011-08-12 18:29:10 -0300213 i = DVBT2_7;
214 break;
215 case 8000000:
Antti Palosaarie4a42e12014-08-03 23:40:23 -0300216 if_khz = dev->cfg.if_dvbt2_8;
Antti Palosaarib682ad12011-08-12 18:29:10 -0300217 i = DVBT2_8;
Antti Palosaari26eb7042011-04-09 20:07:30 -0300218 break;
219 default:
220 ret = -EINVAL;
221 goto error;
222 }
223 break;
Mauro Carvalho Chehab03494712011-12-22 18:11:39 -0300224 case SYS_DVBC_ANNEX_A:
225 case SYS_DVBC_ANNEX_C:
Antti Palosaarie4a42e12014-08-03 23:40:23 -0300226 if_khz = dev->cfg.if_dvbc;
Antti Palosaarib682ad12011-08-12 18:29:10 -0300227 i = DVBC_8;
Antti Palosaari26eb7042011-04-09 20:07:30 -0300228 break;
229 default:
230 ret = -EINVAL;
231 goto error;
232 }
233
Antti Palosaarie4a42e12014-08-03 23:40:23 -0300234 ret = tda18212_wr_reg(dev, 0x23, bw_params[i][2]);
Antti Palosaari26eb7042011-04-09 20:07:30 -0300235 if (ret)
236 goto error;
237
Antti Palosaarie4a42e12014-08-03 23:40:23 -0300238 ret = tda18212_wr_reg(dev, 0x06, 0x00);
Antti Palosaari26eb7042011-04-09 20:07:30 -0300239 if (ret)
240 goto error;
241
Antti Palosaarie4a42e12014-08-03 23:40:23 -0300242 ret = tda18212_wr_reg(dev, 0x0f, bw_params[i][0]);
Antti Palosaari26eb7042011-04-09 20:07:30 -0300243 if (ret)
244 goto error;
245
246 buf[0] = 0x02;
247 buf[1] = bw_params[i][1];
248 buf[2] = 0x03; /* default value */
Antti Palosaari8cffcc72011-11-13 11:22:58 -0300249 buf[3] = DIV_ROUND_CLOSEST(if_khz, 50);
Antti Palosaari26eb7042011-04-09 20:07:30 -0300250 buf[4] = ((c->frequency / 1000) >> 16) & 0xff;
251 buf[5] = ((c->frequency / 1000) >> 8) & 0xff;
252 buf[6] = ((c->frequency / 1000) >> 0) & 0xff;
253 buf[7] = 0xc1;
254 buf[8] = 0x01;
Antti Palosaarie4a42e12014-08-03 23:40:23 -0300255 ret = tda18212_wr_regs(dev, 0x12, buf, sizeof(buf));
Antti Palosaari26eb7042011-04-09 20:07:30 -0300256 if (ret)
257 goto error;
258
Antti Palosaari835bf822011-11-13 11:20:08 -0300259 /* actual IF rounded as it is on register */
Antti Palosaarie4a42e12014-08-03 23:40:23 -0300260 dev->if_frequency = buf[3] * 50 * 1000;
Antti Palosaari835bf822011-11-13 11:20:08 -0300261
Antti Palosaari26eb7042011-04-09 20:07:30 -0300262exit:
263 if (fe->ops.i2c_gate_ctrl)
264 fe->ops.i2c_gate_ctrl(fe, 0); /* close I2C-gate */
265
266 return ret;
267
268error:
Antti Palosaarie4a42e12014-08-03 23:40:23 -0300269 dev_dbg(&dev->client->dev, "failed=%d\n", ret);
Antti Palosaari26eb7042011-04-09 20:07:30 -0300270 goto exit;
271}
272
Antti Palosaari835bf822011-11-13 11:20:08 -0300273static int tda18212_get_if_frequency(struct dvb_frontend *fe, u32 *frequency)
274{
Antti Palosaarie4a42e12014-08-03 23:40:23 -0300275 struct tda18212_dev *dev = fe->tuner_priv;
Antti Palosaari835bf822011-11-13 11:20:08 -0300276
Antti Palosaarie4a42e12014-08-03 23:40:23 -0300277 *frequency = dev->if_frequency;
Antti Palosaari835bf822011-11-13 11:20:08 -0300278
279 return 0;
280}
281
Antti Palosaari26eb7042011-04-09 20:07:30 -0300282static const struct dvb_tuner_ops tda18212_tuner_ops = {
283 .info = {
284 .name = "NXP TDA18212",
285
286 .frequency_min = 48000000,
287 .frequency_max = 864000000,
288 .frequency_step = 1000,
289 },
290
Antti Palosaari26eb7042011-04-09 20:07:30 -0300291 .set_params = tda18212_set_params,
Antti Palosaari835bf822011-11-13 11:20:08 -0300292 .get_if_frequency = tda18212_get_if_frequency,
Antti Palosaari26eb7042011-04-09 20:07:30 -0300293};
294
Antti Palosaari0e584cc2014-08-03 23:05:31 -0300295static int tda18212_probe(struct i2c_client *client,
296 const struct i2c_device_id *id)
Antti Palosaari26eb7042011-04-09 20:07:30 -0300297{
Antti Palosaari0e584cc2014-08-03 23:05:31 -0300298 struct tda18212_config *cfg = client->dev.platform_data;
299 struct dvb_frontend *fe = cfg->fe;
Antti Palosaarie4a42e12014-08-03 23:40:23 -0300300 struct tda18212_dev *dev;
Antti Palosaari26eb7042011-04-09 20:07:30 -0300301 int ret;
Antti Palosaari976bcb22014-07-31 16:35:56 -0300302 u8 chip_id = chip_id;
303 char *version;
Antti Palosaari26eb7042011-04-09 20:07:30 -0300304
Antti Palosaarie4a42e12014-08-03 23:40:23 -0300305 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
306 if (dev == NULL) {
Antti Palosaari0e584cc2014-08-03 23:05:31 -0300307 ret = -ENOMEM;
Antti Palosaaribdb32652014-08-03 23:26:27 -0300308 dev_err(&client->dev, "kzalloc() failed\n");
Antti Palosaari0e584cc2014-08-03 23:05:31 -0300309 goto err;
310 }
Antti Palosaari26eb7042011-04-09 20:07:30 -0300311
Antti Palosaarie4a42e12014-08-03 23:40:23 -0300312 memcpy(&dev->cfg, cfg, sizeof(struct tda18212_config));
313 dev->client = client;
Antti Palosaari26eb7042011-04-09 20:07:30 -0300314
Antti Palosaari0e584cc2014-08-03 23:05:31 -0300315 /* check if the tuner is there */
Antti Palosaari26eb7042011-04-09 20:07:30 -0300316 if (fe->ops.i2c_gate_ctrl)
317 fe->ops.i2c_gate_ctrl(fe, 1); /* open I2C-gate */
318
Antti Palosaarie4a42e12014-08-03 23:40:23 -0300319 ret = tda18212_rd_reg(dev, 0x00, &chip_id);
320 dev_dbg(&dev->client->dev, "chip_id=%02x\n", chip_id);
Antti Palosaari26eb7042011-04-09 20:07:30 -0300321
322 if (fe->ops.i2c_gate_ctrl)
323 fe->ops.i2c_gate_ctrl(fe, 0); /* close I2C-gate */
324
Antti Palosaari976bcb22014-07-31 16:35:56 -0300325 if (ret)
326 goto err;
327
328 switch (chip_id) {
329 case 0xc7:
330 version = "M"; /* master */
331 break;
332 case 0x47:
333 version = "S"; /* slave */
334 break;
335 default:
Antti Palosaari0e584cc2014-08-03 23:05:31 -0300336 ret = -ENODEV;
Antti Palosaari976bcb22014-07-31 16:35:56 -0300337 goto err;
Antti Palosaari26eb7042011-04-09 20:07:30 -0300338 }
339
Antti Palosaarie4a42e12014-08-03 23:40:23 -0300340 dev_info(&dev->client->dev,
Antti Palosaaribdb32652014-08-03 23:26:27 -0300341 "NXP TDA18212HN/%s successfully identified\n", version);
Antti Palosaari26eb7042011-04-09 20:07:30 -0300342
Antti Palosaarie4a42e12014-08-03 23:40:23 -0300343 fe->tuner_priv = dev;
Antti Palosaari26eb7042011-04-09 20:07:30 -0300344 memcpy(&fe->ops.tuner_ops, &tda18212_tuner_ops,
Antti Palosaari0e584cc2014-08-03 23:05:31 -0300345 sizeof(struct dvb_tuner_ops));
Antti Palosaarie4a42e12014-08-03 23:40:23 -0300346 i2c_set_clientdata(client, dev);
Antti Palosaari26eb7042011-04-09 20:07:30 -0300347
Antti Palosaari0e584cc2014-08-03 23:05:31 -0300348 return 0;
Antti Palosaari976bcb22014-07-31 16:35:56 -0300349err:
Antti Palosaaribdb32652014-08-03 23:26:27 -0300350 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaarie4a42e12014-08-03 23:40:23 -0300351 kfree(dev);
Antti Palosaari0e584cc2014-08-03 23:05:31 -0300352 return ret;
Antti Palosaari26eb7042011-04-09 20:07:30 -0300353}
Antti Palosaari0e584cc2014-08-03 23:05:31 -0300354
355static int tda18212_remove(struct i2c_client *client)
356{
Antti Palosaarie4a42e12014-08-03 23:40:23 -0300357 struct tda18212_dev *dev = i2c_get_clientdata(client);
358 struct dvb_frontend *fe = dev->cfg.fe;
Antti Palosaari0e584cc2014-08-03 23:05:31 -0300359
Antti Palosaaribdb32652014-08-03 23:26:27 -0300360 dev_dbg(&client->dev, "\n");
Antti Palosaari0e584cc2014-08-03 23:05:31 -0300361
362 memset(&fe->ops.tuner_ops, 0, sizeof(struct dvb_tuner_ops));
363 fe->tuner_priv = NULL;
Antti Palosaarie4a42e12014-08-03 23:40:23 -0300364 kfree(dev);
Antti Palosaari0e584cc2014-08-03 23:05:31 -0300365
366 return 0;
367}
368
369static const struct i2c_device_id tda18212_id[] = {
370 {"tda18212", 0},
371 {}
372};
373MODULE_DEVICE_TABLE(i2c, tda18212_id);
374
375static struct i2c_driver tda18212_driver = {
376 .driver = {
377 .owner = THIS_MODULE,
378 .name = "tda18212",
379 },
380 .probe = tda18212_probe,
381 .remove = tda18212_remove,
382 .id_table = tda18212_id,
383};
384
385module_i2c_driver(tda18212_driver);
Antti Palosaari26eb7042011-04-09 20:07:30 -0300386
387MODULE_DESCRIPTION("NXP TDA18212HN silicon tuner driver");
388MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
389MODULE_LICENSE("GPL");