[media] tda18212: rename state from 'priv' to 'dev'
[deliverable/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_dev {
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_dev *dev, u8 reg, u8 *val, int len)
35 {
36 int ret;
37 u8 buf[MAX_XFER_SIZE];
38 struct i2c_msg msg[1] = {
39 {
40 .addr = dev->client->addr,
41 .flags = 0,
42 .len = 1 + len,
43 .buf = buf,
44 }
45 };
46
47 if (1 + len > sizeof(buf)) {
48 dev_warn(&dev->client->dev,
49 "i2c wr reg=%04x: len=%d is too big!\n",
50 reg, len);
51 return -EINVAL;
52 }
53
54 buf[0] = reg;
55 memcpy(&buf[1], val, len);
56
57 ret = i2c_transfer(dev->client->adapter, msg, 1);
58 if (ret == 1) {
59 ret = 0;
60 } else {
61 dev_warn(&dev->client->dev,
62 "i2c wr failed=%d reg=%02x len=%d\n",
63 ret, reg, len);
64 ret = -EREMOTEIO;
65 }
66 return ret;
67 }
68
69 /* read multiple registers */
70 static int tda18212_rd_regs(struct tda18212_dev *dev, u8 reg, u8 *val, int len)
71 {
72 int ret;
73 u8 buf[MAX_XFER_SIZE];
74 struct i2c_msg msg[2] = {
75 {
76 .addr = dev->client->addr,
77 .flags = 0,
78 .len = 1,
79 .buf = &reg,
80 }, {
81 .addr = dev->client->addr,
82 .flags = I2C_M_RD,
83 .len = len,
84 .buf = buf,
85 }
86 };
87
88 if (len > sizeof(buf)) {
89 dev_warn(&dev->client->dev,
90 "i2c rd reg=%04x: len=%d is too big!\n",
91 reg, len);
92 return -EINVAL;
93 }
94
95 ret = i2c_transfer(dev->client->adapter, msg, 2);
96 if (ret == 2) {
97 memcpy(val, buf, len);
98 ret = 0;
99 } else {
100 dev_warn(&dev->client->dev,
101 "i2c rd failed=%d reg=%02x len=%d\n",
102 ret, reg, len);
103 ret = -EREMOTEIO;
104 }
105
106 return ret;
107 }
108
109 /* write single register */
110 static int tda18212_wr_reg(struct tda18212_dev *dev, u8 reg, u8 val)
111 {
112 return tda18212_wr_regs(dev, reg, &val, 1);
113 }
114
115 /* read single register */
116 static int tda18212_rd_reg(struct tda18212_dev *dev, u8 reg, u8 *val)
117 {
118 return tda18212_rd_regs(dev, reg, val, 1);
119 }
120
121 #if 0 /* keep, useful when developing driver */
122 static void tda18212_dump_regs(struct tda18212_dev *dev)
123 {
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)
129 tda18212_rd_regs(dev, i, &buf[i], TDA18212_RD_LEN);
130
131 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET, 32, 1, buf,
132 sizeof(buf), true);
133
134 return;
135 }
136 #endif
137
138 static int tda18212_set_params(struct dvb_frontend *fe)
139 {
140 struct tda18212_dev *dev = fe->tuner_priv;
141 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
142 int ret, i;
143 u32 if_khz;
144 u8 buf[9];
145 #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
153 #define ATSC_VSB 8
154 #define ATSC_QAM 9
155 static const u8 bw_params[][3] = {
156 /* 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 },
165 [ATSC_VSB] = { 0x7d, 0x20, 0x63 },
166 [ATSC_QAM] = { 0x7d, 0x20, 0x63 },
167 };
168
169 dev_dbg(&dev->client->dev,
170 "delivery_system=%d frequency=%d bandwidth_hz=%d\n",
171 c->delivery_system, c->frequency,
172 c->bandwidth_hz);
173
174 if (fe->ops.i2c_gate_ctrl)
175 fe->ops.i2c_gate_ctrl(fe, 1); /* open I2C-gate */
176
177 switch (c->delivery_system) {
178 case SYS_ATSC:
179 if_khz = dev->cfg.if_atsc_vsb;
180 i = ATSC_VSB;
181 break;
182 case SYS_DVBC_ANNEX_B:
183 if_khz = dev->cfg.if_atsc_qam;
184 i = ATSC_QAM;
185 break;
186 case SYS_DVBT:
187 switch (c->bandwidth_hz) {
188 case 6000000:
189 if_khz = dev->cfg.if_dvbt_6;
190 i = DVBT_6;
191 break;
192 case 7000000:
193 if_khz = dev->cfg.if_dvbt_7;
194 i = DVBT_7;
195 break;
196 case 8000000:
197 if_khz = dev->cfg.if_dvbt_8;
198 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:
208 if_khz = dev->cfg.if_dvbt2_6;
209 i = DVBT2_6;
210 break;
211 case 7000000:
212 if_khz = dev->cfg.if_dvbt2_7;
213 i = DVBT2_7;
214 break;
215 case 8000000:
216 if_khz = dev->cfg.if_dvbt2_8;
217 i = DVBT2_8;
218 break;
219 default:
220 ret = -EINVAL;
221 goto error;
222 }
223 break;
224 case SYS_DVBC_ANNEX_A:
225 case SYS_DVBC_ANNEX_C:
226 if_khz = dev->cfg.if_dvbc;
227 i = DVBC_8;
228 break;
229 default:
230 ret = -EINVAL;
231 goto error;
232 }
233
234 ret = tda18212_wr_reg(dev, 0x23, bw_params[i][2]);
235 if (ret)
236 goto error;
237
238 ret = tda18212_wr_reg(dev, 0x06, 0x00);
239 if (ret)
240 goto error;
241
242 ret = tda18212_wr_reg(dev, 0x0f, bw_params[i][0]);
243 if (ret)
244 goto error;
245
246 buf[0] = 0x02;
247 buf[1] = bw_params[i][1];
248 buf[2] = 0x03; /* default value */
249 buf[3] = DIV_ROUND_CLOSEST(if_khz, 50);
250 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;
255 ret = tda18212_wr_regs(dev, 0x12, buf, sizeof(buf));
256 if (ret)
257 goto error;
258
259 /* actual IF rounded as it is on register */
260 dev->if_frequency = buf[3] * 50 * 1000;
261
262 exit:
263 if (fe->ops.i2c_gate_ctrl)
264 fe->ops.i2c_gate_ctrl(fe, 0); /* close I2C-gate */
265
266 return ret;
267
268 error:
269 dev_dbg(&dev->client->dev, "failed=%d\n", ret);
270 goto exit;
271 }
272
273 static int tda18212_get_if_frequency(struct dvb_frontend *fe, u32 *frequency)
274 {
275 struct tda18212_dev *dev = fe->tuner_priv;
276
277 *frequency = dev->if_frequency;
278
279 return 0;
280 }
281
282 static 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
291 .set_params = tda18212_set_params,
292 .get_if_frequency = tda18212_get_if_frequency,
293 };
294
295 static int tda18212_probe(struct i2c_client *client,
296 const struct i2c_device_id *id)
297 {
298 struct tda18212_config *cfg = client->dev.platform_data;
299 struct dvb_frontend *fe = cfg->fe;
300 struct tda18212_dev *dev;
301 int ret;
302 u8 chip_id = chip_id;
303 char *version;
304
305 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
306 if (dev == NULL) {
307 ret = -ENOMEM;
308 dev_err(&client->dev, "kzalloc() failed\n");
309 goto err;
310 }
311
312 memcpy(&dev->cfg, cfg, sizeof(struct tda18212_config));
313 dev->client = client;
314
315 /* check if the tuner is there */
316 if (fe->ops.i2c_gate_ctrl)
317 fe->ops.i2c_gate_ctrl(fe, 1); /* open I2C-gate */
318
319 ret = tda18212_rd_reg(dev, 0x00, &chip_id);
320 dev_dbg(&dev->client->dev, "chip_id=%02x\n", chip_id);
321
322 if (fe->ops.i2c_gate_ctrl)
323 fe->ops.i2c_gate_ctrl(fe, 0); /* close I2C-gate */
324
325 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:
336 ret = -ENODEV;
337 goto err;
338 }
339
340 dev_info(&dev->client->dev,
341 "NXP TDA18212HN/%s successfully identified\n", version);
342
343 fe->tuner_priv = dev;
344 memcpy(&fe->ops.tuner_ops, &tda18212_tuner_ops,
345 sizeof(struct dvb_tuner_ops));
346 i2c_set_clientdata(client, dev);
347
348 return 0;
349 err:
350 dev_dbg(&client->dev, "failed=%d\n", ret);
351 kfree(dev);
352 return ret;
353 }
354
355 static int tda18212_remove(struct i2c_client *client)
356 {
357 struct tda18212_dev *dev = i2c_get_clientdata(client);
358 struct dvb_frontend *fe = dev->cfg.fe;
359
360 dev_dbg(&client->dev, "\n");
361
362 memset(&fe->ops.tuner_ops, 0, sizeof(struct dvb_tuner_ops));
363 fe->tuner_priv = NULL;
364 kfree(dev);
365
366 return 0;
367 }
368
369 static const struct i2c_device_id tda18212_id[] = {
370 {"tda18212", 0},
371 {}
372 };
373 MODULE_DEVICE_TABLE(i2c, tda18212_id);
374
375 static 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
385 module_i2c_driver(tda18212_driver);
386
387 MODULE_DESCRIPTION("NXP TDA18212HN silicon tuner driver");
388 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
389 MODULE_LICENSE("GPL");
This page took 0.065342 seconds and 5 git commands to generate.