blob: a58c74f107a431e490b863a4a0eb8ae64ca848fe [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#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22
23#include "tda18212.h"
24
25struct tda18212_priv {
26 struct tda18212_config *cfg;
27 struct i2c_adapter *i2c;
Antti Palosaari835bf822011-11-13 11:20:08 -030028
29 u32 if_frequency;
Joe Perches2b507632011-07-31 04:30:10 -030030};
31
32#define dbg(fmt, arg...) \
33do { \
34 if (debug) \
35 pr_info("%s: " fmt, __func__, ##arg); \
36} while (0)
Antti Palosaari26eb7042011-04-09 20:07:30 -030037
38static int debug;
39module_param(debug, int, 0644);
40MODULE_PARM_DESC(debug, "Turn on/off debugging (default:off).");
41
42/* write multiple registers */
43static int tda18212_wr_regs(struct tda18212_priv *priv, u8 reg, u8 *val,
44 int len)
45{
46 int ret;
47 u8 buf[len+1];
48 struct i2c_msg msg[1] = {
49 {
50 .addr = priv->cfg->i2c_address,
51 .flags = 0,
52 .len = sizeof(buf),
53 .buf = buf,
54 }
55 };
56
57 buf[0] = reg;
58 memcpy(&buf[1], val, len);
59
60 ret = i2c_transfer(priv->i2c, msg, 1);
61 if (ret == 1) {
62 ret = 0;
63 } else {
Joe Perches2b507632011-07-31 04:30:10 -030064 pr_warn("i2c wr failed ret:%d reg:%02x len:%d\n",
65 ret, reg, len);
Antti Palosaari26eb7042011-04-09 20:07:30 -030066 ret = -EREMOTEIO;
67 }
68 return ret;
69}
70
71/* read multiple registers */
72static int tda18212_rd_regs(struct tda18212_priv *priv, u8 reg, u8 *val,
73 int len)
74{
75 int ret;
76 u8 buf[len];
77 struct i2c_msg msg[2] = {
78 {
79 .addr = priv->cfg->i2c_address,
80 .flags = 0,
81 .len = 1,
82 .buf = &reg,
83 }, {
84 .addr = priv->cfg->i2c_address,
85 .flags = I2C_M_RD,
86 .len = sizeof(buf),
87 .buf = buf,
88 }
89 };
90
91 ret = i2c_transfer(priv->i2c, msg, 2);
92 if (ret == 2) {
93 memcpy(val, buf, len);
94 ret = 0;
95 } else {
Joe Perches2b507632011-07-31 04:30:10 -030096 pr_warn("i2c rd failed ret:%d reg:%02x len:%d\n",
97 ret, reg, len);
Antti Palosaari26eb7042011-04-09 20:07:30 -030098 ret = -EREMOTEIO;
99 }
100
101 return ret;
102}
103
104/* write single register */
105static int tda18212_wr_reg(struct tda18212_priv *priv, u8 reg, u8 val)
106{
107 return tda18212_wr_regs(priv, reg, &val, 1);
108}
109
110/* read single register */
111static int tda18212_rd_reg(struct tda18212_priv *priv, u8 reg, u8 *val)
112{
113 return tda18212_rd_regs(priv, reg, val, 1);
114}
115
116#if 0 /* keep, useful when developing driver */
117static void tda18212_dump_regs(struct tda18212_priv *priv)
118{
119 int i;
120 u8 buf[256];
121
122 #define TDA18212_RD_LEN 32
123 for (i = 0; i < sizeof(buf); i += TDA18212_RD_LEN)
124 tda18212_rd_regs(priv, i, &buf[i], TDA18212_RD_LEN);
125
126 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET, 32, 1, buf,
127 sizeof(buf), true);
128
129 return;
130}
131#endif
132
133static int tda18212_set_params(struct dvb_frontend *fe,
134 struct dvb_frontend_parameters *p)
135{
136 struct tda18212_priv *priv = fe->tuner_priv;
137 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
138 int ret, i;
139 u32 if_khz;
140 u8 buf[9];
Antti Palosaarib682ad12011-08-12 18:29:10 -0300141 #define DVBT_6 0
142 #define DVBT_7 1
143 #define DVBT_8 2
144 #define DVBT2_6 3
145 #define DVBT2_7 4
146 #define DVBT2_8 5
147 #define DVBC_6 6
148 #define DVBC_8 7
Antti Palosaari26eb7042011-04-09 20:07:30 -0300149 static const u8 bw_params[][3] = {
Antti Palosaarib682ad12011-08-12 18:29:10 -0300150 /* reg: 0f 13 23 */
151 [DVBT_6] = { 0xb3, 0x20, 0x03 },
152 [DVBT_7] = { 0xb3, 0x31, 0x01 },
153 [DVBT_8] = { 0xb3, 0x22, 0x01 },
154 [DVBT2_6] = { 0xbc, 0x20, 0x03 },
155 [DVBT2_7] = { 0xbc, 0x72, 0x03 },
156 [DVBT2_8] = { 0xbc, 0x22, 0x01 },
157 [DVBC_6] = { 0x92, 0x50, 0x03 },
158 [DVBC_8] = { 0x92, 0x53, 0x03 },
Antti Palosaari26eb7042011-04-09 20:07:30 -0300159 };
160
Joe Perches2b507632011-07-31 04:30:10 -0300161 dbg("delsys=%d RF=%d BW=%d\n",
162 c->delivery_system, c->frequency, c->bandwidth_hz);
Antti Palosaari26eb7042011-04-09 20:07:30 -0300163
164 if (fe->ops.i2c_gate_ctrl)
165 fe->ops.i2c_gate_ctrl(fe, 1); /* open I2C-gate */
166
167 switch (c->delivery_system) {
168 case SYS_DVBT:
169 switch (c->bandwidth_hz) {
170 case 6000000:
171 if_khz = priv->cfg->if_dvbt_6;
Antti Palosaarib682ad12011-08-12 18:29:10 -0300172 i = DVBT_6;
Antti Palosaari26eb7042011-04-09 20:07:30 -0300173 break;
174 case 7000000:
175 if_khz = priv->cfg->if_dvbt_7;
Antti Palosaarib682ad12011-08-12 18:29:10 -0300176 i = DVBT_7;
Antti Palosaari26eb7042011-04-09 20:07:30 -0300177 break;
178 case 8000000:
179 if_khz = priv->cfg->if_dvbt_8;
Antti Palosaarib682ad12011-08-12 18:29:10 -0300180 i = DVBT_8;
181 break;
182 default:
183 ret = -EINVAL;
184 goto error;
185 }
186 break;
187 case SYS_DVBT2:
188 switch (c->bandwidth_hz) {
189 case 6000000:
190 if_khz = priv->cfg->if_dvbt2_6;
191 i = DVBT2_6;
192 break;
193 case 7000000:
194 if_khz = priv->cfg->if_dvbt2_7;
195 i = DVBT2_7;
196 break;
197 case 8000000:
198 if_khz = priv->cfg->if_dvbt2_8;
199 i = DVBT2_8;
Antti Palosaari26eb7042011-04-09 20:07:30 -0300200 break;
201 default:
202 ret = -EINVAL;
203 goto error;
204 }
205 break;
Mauro Carvalho Chehab03494712011-12-22 18:11:39 -0300206 case SYS_DVBC_ANNEX_A:
207 case SYS_DVBC_ANNEX_C:
Antti Palosaari26eb7042011-04-09 20:07:30 -0300208 if_khz = priv->cfg->if_dvbc;
Antti Palosaarib682ad12011-08-12 18:29:10 -0300209 i = DVBC_8;
Antti Palosaari26eb7042011-04-09 20:07:30 -0300210 break;
211 default:
212 ret = -EINVAL;
213 goto error;
214 }
215
216 ret = tda18212_wr_reg(priv, 0x23, bw_params[i][2]);
217 if (ret)
218 goto error;
219
220 ret = tda18212_wr_reg(priv, 0x06, 0x00);
221 if (ret)
222 goto error;
223
224 ret = tda18212_wr_reg(priv, 0x0f, bw_params[i][0]);
225 if (ret)
226 goto error;
227
228 buf[0] = 0x02;
229 buf[1] = bw_params[i][1];
230 buf[2] = 0x03; /* default value */
Antti Palosaari8cffcc72011-11-13 11:22:58 -0300231 buf[3] = DIV_ROUND_CLOSEST(if_khz, 50);
Antti Palosaari26eb7042011-04-09 20:07:30 -0300232 buf[4] = ((c->frequency / 1000) >> 16) & 0xff;
233 buf[5] = ((c->frequency / 1000) >> 8) & 0xff;
234 buf[6] = ((c->frequency / 1000) >> 0) & 0xff;
235 buf[7] = 0xc1;
236 buf[8] = 0x01;
237 ret = tda18212_wr_regs(priv, 0x12, buf, sizeof(buf));
238 if (ret)
239 goto error;
240
Antti Palosaari835bf822011-11-13 11:20:08 -0300241 /* actual IF rounded as it is on register */
242 priv->if_frequency = buf[3] * 50 * 1000;
243
Antti Palosaari26eb7042011-04-09 20:07:30 -0300244exit:
245 if (fe->ops.i2c_gate_ctrl)
246 fe->ops.i2c_gate_ctrl(fe, 0); /* close I2C-gate */
247
248 return ret;
249
250error:
Joe Perches2b507632011-07-31 04:30:10 -0300251 dbg("failed:%d\n", ret);
Antti Palosaari26eb7042011-04-09 20:07:30 -0300252 goto exit;
253}
254
Antti Palosaari835bf822011-11-13 11:20:08 -0300255static int tda18212_get_if_frequency(struct dvb_frontend *fe, u32 *frequency)
256{
257 struct tda18212_priv *priv = fe->tuner_priv;
258
259 *frequency = priv->if_frequency;
260
261 return 0;
262}
263
Antti Palosaari26eb7042011-04-09 20:07:30 -0300264static int tda18212_release(struct dvb_frontend *fe)
265{
266 kfree(fe->tuner_priv);
267 fe->tuner_priv = NULL;
268 return 0;
269}
270
271static const struct dvb_tuner_ops tda18212_tuner_ops = {
272 .info = {
273 .name = "NXP TDA18212",
274
275 .frequency_min = 48000000,
276 .frequency_max = 864000000,
277 .frequency_step = 1000,
278 },
279
280 .release = tda18212_release,
281
282 .set_params = tda18212_set_params,
Antti Palosaari835bf822011-11-13 11:20:08 -0300283 .get_if_frequency = tda18212_get_if_frequency,
Antti Palosaari26eb7042011-04-09 20:07:30 -0300284};
285
286struct dvb_frontend *tda18212_attach(struct dvb_frontend *fe,
287 struct i2c_adapter *i2c, struct tda18212_config *cfg)
288{
289 struct tda18212_priv *priv = NULL;
290 int ret;
291 u8 val;
292
293 priv = kzalloc(sizeof(struct tda18212_priv), GFP_KERNEL);
294 if (priv == NULL)
295 return NULL;
296
297 priv->cfg = cfg;
298 priv->i2c = i2c;
299 fe->tuner_priv = priv;
300
301 if (fe->ops.i2c_gate_ctrl)
302 fe->ops.i2c_gate_ctrl(fe, 1); /* open I2C-gate */
303
304 /* check if the tuner is there */
305 ret = tda18212_rd_reg(priv, 0x00, &val);
306
307 if (fe->ops.i2c_gate_ctrl)
308 fe->ops.i2c_gate_ctrl(fe, 0); /* close I2C-gate */
309
Joe Perches2b507632011-07-31 04:30:10 -0300310 dbg("ret:%d chip ID:%02x\n", ret, val);
Antti Palosaari26eb7042011-04-09 20:07:30 -0300311 if (ret || val != 0xc7) {
312 kfree(priv);
313 return NULL;
314 }
315
Joe Perches2b507632011-07-31 04:30:10 -0300316 pr_info("NXP TDA18212HN successfully identified\n");
Antti Palosaari26eb7042011-04-09 20:07:30 -0300317
318 memcpy(&fe->ops.tuner_ops, &tda18212_tuner_ops,
319 sizeof(struct dvb_tuner_ops));
320
321 return fe;
322}
323EXPORT_SYMBOL(tda18212_attach);
324
325MODULE_DESCRIPTION("NXP TDA18212HN silicon tuner driver");
326MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
327MODULE_LICENSE("GPL");