[media] tda18212: clean logging
[cascardo/linux.git] / drivers / media / tuners / tda18212.c
1 /*
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
21 #include "tda18212.h"
22
23 /* Max transfer size done by I2C transfer functions */
24 #define MAX_XFER_SIZE  64
25
26 struct tda18212_priv {
27         struct tda18212_config cfg;
28         struct i2c_client *client;
29
30         u32 if_frequency;
31 };
32
33 /* write multiple registers */
34 static int tda18212_wr_regs(struct tda18212_priv *priv, u8 reg, u8 *val,
35         int len)
36 {
37         int ret;
38         u8 buf[MAX_XFER_SIZE];
39         struct i2c_msg msg[1] = {
40                 {
41                         .addr = priv->client->addr,
42                         .flags = 0,
43                         .len = 1 + len,
44                         .buf = buf,
45                 }
46         };
47
48         if (1 + len > sizeof(buf)) {
49                 dev_warn(&priv->client->dev,
50                                 "i2c wr reg=%04x: len=%d is too big!\n",
51                                 reg, len);
52                 return -EINVAL;
53         }
54
55         buf[0] = reg;
56         memcpy(&buf[1], val, len);
57
58         ret = i2c_transfer(priv->client->adapter, msg, 1);
59         if (ret == 1) {
60                 ret = 0;
61         } else {
62                 dev_warn(&priv->client->dev,
63                                 "i2c wr failed=%d reg=%02x len=%d\n",
64                                 ret, reg, len);
65                 ret = -EREMOTEIO;
66         }
67         return ret;
68 }
69
70 /* read multiple registers */
71 static int tda18212_rd_regs(struct tda18212_priv *priv, u8 reg, u8 *val,
72         int len)
73 {
74         int ret;
75         u8 buf[MAX_XFER_SIZE];
76         struct i2c_msg msg[2] = {
77                 {
78                         .addr = priv->client->addr,
79                         .flags = 0,
80                         .len = 1,
81                         .buf = &reg,
82                 }, {
83                         .addr = priv->client->addr,
84                         .flags = I2C_M_RD,
85                         .len = len,
86                         .buf = buf,
87                 }
88         };
89
90         if (len > sizeof(buf)) {
91                 dev_warn(&priv->client->dev,
92                                 "i2c rd reg=%04x: len=%d is too big!\n",
93                                 reg, len);
94                 return -EINVAL;
95         }
96
97         ret = i2c_transfer(priv->client->adapter, msg, 2);
98         if (ret == 2) {
99                 memcpy(val, buf, len);
100                 ret = 0;
101         } else {
102                 dev_warn(&priv->client->dev,
103                                 "i2c rd failed=%d reg=%02x len=%d\n",
104                                 ret, reg, len);
105                 ret = -EREMOTEIO;
106         }
107
108         return ret;
109 }
110
111 /* write single register */
112 static int tda18212_wr_reg(struct tda18212_priv *priv, u8 reg, u8 val)
113 {
114         return tda18212_wr_regs(priv, reg, &val, 1);
115 }
116
117 /* read single register */
118 static int tda18212_rd_reg(struct tda18212_priv *priv, u8 reg, u8 *val)
119 {
120         return tda18212_rd_regs(priv, reg, val, 1);
121 }
122
123 #if 0 /* keep, useful when developing driver */
124 static void tda18212_dump_regs(struct tda18212_priv *priv)
125 {
126         int i;
127         u8 buf[256];
128
129         #define TDA18212_RD_LEN 32
130         for (i = 0; i < sizeof(buf); i += TDA18212_RD_LEN)
131                 tda18212_rd_regs(priv, i, &buf[i], TDA18212_RD_LEN);
132
133         print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET, 32, 1, buf,
134                 sizeof(buf), true);
135
136         return;
137 }
138 #endif
139
140 static int tda18212_set_params(struct dvb_frontend *fe)
141 {
142         struct tda18212_priv *priv = fe->tuner_priv;
143         struct dtv_frontend_properties *c = &fe->dtv_property_cache;
144         int ret, i;
145         u32 if_khz;
146         u8 buf[9];
147         #define DVBT_6   0
148         #define DVBT_7   1
149         #define DVBT_8   2
150         #define DVBT2_6  3
151         #define DVBT2_7  4
152         #define DVBT2_8  5
153         #define DVBC_6   6
154         #define DVBC_8   7
155         #define ATSC_VSB 8
156         #define ATSC_QAM 9
157         static const u8 bw_params[][3] = {
158                      /* reg:   0f    13    23 */
159                 [DVBT_6]  = { 0xb3, 0x20, 0x03 },
160                 [DVBT_7]  = { 0xb3, 0x31, 0x01 },
161                 [DVBT_8]  = { 0xb3, 0x22, 0x01 },
162                 [DVBT2_6] = { 0xbc, 0x20, 0x03 },
163                 [DVBT2_7] = { 0xbc, 0x72, 0x03 },
164                 [DVBT2_8] = { 0xbc, 0x22, 0x01 },
165                 [DVBC_6]  = { 0x92, 0x50, 0x03 },
166                 [DVBC_8]  = { 0x92, 0x53, 0x03 },
167                 [ATSC_VSB] = { 0x7d, 0x20, 0x63 },
168                 [ATSC_QAM] = { 0x7d, 0x20, 0x63 },
169         };
170
171         dev_dbg(&priv->client->dev,
172                         "delivery_system=%d frequency=%d bandwidth_hz=%d\n",
173                         c->delivery_system, c->frequency,
174                         c->bandwidth_hz);
175
176         if (fe->ops.i2c_gate_ctrl)
177                 fe->ops.i2c_gate_ctrl(fe, 1); /* open I2C-gate */
178
179         switch (c->delivery_system) {
180         case SYS_ATSC:
181                 if_khz = priv->cfg.if_atsc_vsb;
182                 i = ATSC_VSB;
183                 break;
184         case SYS_DVBC_ANNEX_B:
185                 if_khz = priv->cfg.if_atsc_qam;
186                 i = ATSC_QAM;
187                 break;
188         case SYS_DVBT:
189                 switch (c->bandwidth_hz) {
190                 case 6000000:
191                         if_khz = priv->cfg.if_dvbt_6;
192                         i = DVBT_6;
193                         break;
194                 case 7000000:
195                         if_khz = priv->cfg.if_dvbt_7;
196                         i = DVBT_7;
197                         break;
198                 case 8000000:
199                         if_khz = priv->cfg.if_dvbt_8;
200                         i = DVBT_8;
201                         break;
202                 default:
203                         ret = -EINVAL;
204                         goto error;
205                 }
206                 break;
207         case SYS_DVBT2:
208                 switch (c->bandwidth_hz) {
209                 case 6000000:
210                         if_khz = priv->cfg.if_dvbt2_6;
211                         i = DVBT2_6;
212                         break;
213                 case 7000000:
214                         if_khz = priv->cfg.if_dvbt2_7;
215                         i = DVBT2_7;
216                         break;
217                 case 8000000:
218                         if_khz = priv->cfg.if_dvbt2_8;
219                         i = DVBT2_8;
220                         break;
221                 default:
222                         ret = -EINVAL;
223                         goto error;
224                 }
225                 break;
226         case SYS_DVBC_ANNEX_A:
227         case SYS_DVBC_ANNEX_C:
228                 if_khz = priv->cfg.if_dvbc;
229                 i = DVBC_8;
230                 break;
231         default:
232                 ret = -EINVAL;
233                 goto error;
234         }
235
236         ret = tda18212_wr_reg(priv, 0x23, bw_params[i][2]);
237         if (ret)
238                 goto error;
239
240         ret = tda18212_wr_reg(priv, 0x06, 0x00);
241         if (ret)
242                 goto error;
243
244         ret = tda18212_wr_reg(priv, 0x0f, bw_params[i][0]);
245         if (ret)
246                 goto error;
247
248         buf[0] = 0x02;
249         buf[1] = bw_params[i][1];
250         buf[2] = 0x03; /* default value */
251         buf[3] = DIV_ROUND_CLOSEST(if_khz, 50);
252         buf[4] = ((c->frequency / 1000) >> 16) & 0xff;
253         buf[5] = ((c->frequency / 1000) >>  8) & 0xff;
254         buf[6] = ((c->frequency / 1000) >>  0) & 0xff;
255         buf[7] = 0xc1;
256         buf[8] = 0x01;
257         ret = tda18212_wr_regs(priv, 0x12, buf, sizeof(buf));
258         if (ret)
259                 goto error;
260
261         /* actual IF rounded as it is on register */
262         priv->if_frequency = buf[3] * 50 * 1000;
263
264 exit:
265         if (fe->ops.i2c_gate_ctrl)
266                 fe->ops.i2c_gate_ctrl(fe, 0); /* close I2C-gate */
267
268         return ret;
269
270 error:
271         dev_dbg(&priv->client->dev, "failed=%d\n", ret);
272         goto exit;
273 }
274
275 static int tda18212_get_if_frequency(struct dvb_frontend *fe, u32 *frequency)
276 {
277         struct tda18212_priv *priv = fe->tuner_priv;
278
279         *frequency = priv->if_frequency;
280
281         return 0;
282 }
283
284 static const struct dvb_tuner_ops tda18212_tuner_ops = {
285         .info = {
286                 .name           = "NXP TDA18212",
287
288                 .frequency_min  =  48000000,
289                 .frequency_max  = 864000000,
290                 .frequency_step =      1000,
291         },
292
293         .set_params    = tda18212_set_params,
294         .get_if_frequency = tda18212_get_if_frequency,
295 };
296
297 static int tda18212_probe(struct i2c_client *client,
298                 const struct i2c_device_id *id)
299 {
300         struct tda18212_config *cfg = client->dev.platform_data;
301         struct dvb_frontend *fe = cfg->fe;
302         struct tda18212_priv *priv;
303         int ret;
304         u8 chip_id = chip_id;
305         char *version;
306
307         priv = kzalloc(sizeof(*priv), GFP_KERNEL);
308         if (!priv) {
309                 ret = -ENOMEM;
310                 dev_err(&client->dev, "kzalloc() failed\n");
311                 goto err;
312         }
313
314         memcpy(&priv->cfg, cfg, sizeof(struct tda18212_config));
315         priv->client = client;
316
317         /* check if the tuner is there */
318         if (fe->ops.i2c_gate_ctrl)
319                 fe->ops.i2c_gate_ctrl(fe, 1); /* open I2C-gate */
320
321         ret = tda18212_rd_reg(priv, 0x00, &chip_id);
322         dev_dbg(&priv->client->dev, "chip_id=%02x\n", chip_id);
323
324         if (fe->ops.i2c_gate_ctrl)
325                 fe->ops.i2c_gate_ctrl(fe, 0); /* close I2C-gate */
326
327         if (ret)
328                 goto err;
329
330         switch (chip_id) {
331         case 0xc7:
332                 version = "M"; /* master */
333                 break;
334         case 0x47:
335                 version = "S"; /* slave */
336                 break;
337         default:
338                 ret = -ENODEV;
339                 goto err;
340         }
341
342         dev_info(&priv->client->dev,
343                         "NXP TDA18212HN/%s successfully identified\n", version);
344
345         fe->tuner_priv = priv;
346         memcpy(&fe->ops.tuner_ops, &tda18212_tuner_ops,
347                         sizeof(struct dvb_tuner_ops));
348         i2c_set_clientdata(client, priv);
349
350         return 0;
351 err:
352         dev_dbg(&client->dev, "failed=%d\n", ret);
353         kfree(priv);
354         return ret;
355 }
356
357 static int tda18212_remove(struct i2c_client *client)
358 {
359         struct tda18212_priv *priv = i2c_get_clientdata(client);
360         struct dvb_frontend *fe = priv->cfg.fe;
361
362         dev_dbg(&client->dev, "\n");
363
364         memset(&fe->ops.tuner_ops, 0, sizeof(struct dvb_tuner_ops));
365         fe->tuner_priv = NULL;
366         kfree(priv);
367
368         return 0;
369 }
370
371 static const struct i2c_device_id tda18212_id[] = {
372         {"tda18212", 0},
373         {}
374 };
375 MODULE_DEVICE_TABLE(i2c, tda18212_id);
376
377 static struct i2c_driver tda18212_driver = {
378         .driver = {
379                 .owner  = THIS_MODULE,
380                 .name   = "tda18212",
381         },
382         .probe          = tda18212_probe,
383         .remove         = tda18212_remove,
384         .id_table       = tda18212_id,
385 };
386
387 module_i2c_driver(tda18212_driver);
388
389 MODULE_DESCRIPTION("NXP TDA18212HN silicon tuner driver");
390 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
391 MODULE_LICENSE("GPL");