[media] xc5000: add XC5000C_DEFAULT_FIRMWARE: dvb-fe-xc5000c-41.024.5-31875.fw
[deliverable/linux.git] / drivers / media / video / tuner-core.c
CommitLineData
1da177e4 1/*
1da177e4
LT
2 * i2c tv tuner chip device driver
3 * core core, i.e. kernel interfaces, registering and so on
a2894e3f
MCC
4 *
5 * Copyright(c) by Ralph Metzler, Gerd Knorr, Gunther Mayer
6 *
7 * Copyright(c) 2005-2011 by Mauro Carvalho Chehab
8 * - Added support for a separate Radio tuner
9 * - Major rework and cleanups at the code
10 *
11 * This driver supports many devices and the idea is to let the driver
12 * detect which device is present. So rather than listing all supported
13 * devices here, we pretend to support a single, fake device type that will
14 * handle both radio and analog TV tuning.
1da177e4
LT
15 */
16
17#include <linux/module.h>
1da177e4 18#include <linux/kernel.h>
1da177e4
LT
19#include <linux/string.h>
20#include <linux/timer.h>
21#include <linux/delay.h>
22#include <linux/errno.h>
23#include <linux/slab.h>
24#include <linux/poll.h>
25#include <linux/i2c.h>
26#include <linux/types.h>
1da177e4 27#include <linux/init.h>
75b4c260 28#include <linux/videodev2.h>
1da177e4 29#include <media/tuner.h>
4adad287 30#include <media/tuner-types.h>
e8a4a9e7 31#include <media/v4l2-device.h>
35ea11ff 32#include <media/v4l2-ioctl.h>
96c0b7cf 33#include "mt20xx.h"
910bb3e3 34#include "tda8290.h"
7ab10bf7 35#include "tea5761.h"
8d0936ed 36#include "tea5767.h"
215b95ba 37#include "tuner-xc2028.h"
4adad287 38#include "tuner-simple.h"
31c9584c 39#include "tda9887.h"
27c685a4 40#include "xc5000.h"
93463895 41#include "tda18271.h"
8d009a0c 42#include "xc4000.h"
1da177e4
LT
43
44#define UNSET (-1U)
45
7d275bf8 46#define PREFIX (t->i2c->driver->driver.name)
241020d1 47
9f3f71ef
MCC
48/*
49 * Driver modprobe parameters
50 */
51
52/* insmod options used at init time => read/only */
53static unsigned int addr;
54static unsigned int no_autodetect;
55static unsigned int show_i2c;
56
57module_param(addr, int, 0444);
58module_param(no_autodetect, int, 0444);
59module_param(show_i2c, int, 0444);
60
61/* insmod options used at runtime => read/write */
62static int tuner_debug;
63static unsigned int tv_range[2] = { 44, 958 };
64static unsigned int radio_range[2] = { 65, 108 };
65static char pal[] = "--";
66static char secam[] = "--";
67static char ntsc[] = "-";
68
7d275bf8 69module_param_named(debug, tuner_debug, int, 0644);
9f3f71ef
MCC
70module_param_array(tv_range, int, NULL, 0644);
71module_param_array(radio_range, int, NULL, 0644);
72module_param_string(pal, pal, sizeof(pal), 0644);
73module_param_string(secam, secam, sizeof(secam), 0644);
74module_param_string(ntsc, ntsc, sizeof(ntsc), 0644);
75
76/*
77 * Static vars
78 */
79
9f3f71ef 80static LIST_HEAD(tuner_list);
a2894e3f 81static const struct v4l2_subdev_ops tuner_ops;
9f3f71ef
MCC
82
83/*
84 * Debug macros
85 */
86
87#define tuner_warn(fmt, arg...) do { \
88 printk(KERN_WARNING "%s %d-%04x: " fmt, PREFIX, \
89 i2c_adapter_id(t->i2c->adapter), \
90 t->i2c->addr, ##arg); \
91 } while (0)
92
93#define tuner_info(fmt, arg...) do { \
94 printk(KERN_INFO "%s %d-%04x: " fmt, PREFIX, \
95 i2c_adapter_id(t->i2c->adapter), \
96 t->i2c->addr, ##arg); \
97 } while (0)
98
99#define tuner_err(fmt, arg...) do { \
100 printk(KERN_ERR "%s %d-%04x: " fmt, PREFIX, \
101 i2c_adapter_id(t->i2c->adapter), \
102 t->i2c->addr, ##arg); \
103 } while (0)
104
105#define tuner_dbg(fmt, arg...) do { \
106 if (tuner_debug) \
107 printk(KERN_DEBUG "%s %d-%04x: " fmt, PREFIX, \
108 i2c_adapter_id(t->i2c->adapter), \
109 t->i2c->addr, ##arg); \
110 } while (0)
111
112/*
113 * Internal struct used inside the driver
114 */
115
116struct tuner {
117 /* device */
118 struct dvb_frontend fe;
119 struct i2c_client *i2c;
120 struct v4l2_subdev sd;
121 struct list_head list;
122
123 /* keep track of the current settings */
124 v4l2_std_id std;
125 unsigned int tv_freq;
126 unsigned int radio_freq;
127 unsigned int audmode;
128
cbde6898 129 enum v4l2_tuner_type mode;
9f3f71ef
MCC
130 unsigned int mode_mask; /* Combination of allowable modes */
131
cbde6898
MCC
132 bool standby; /* Standby mode */
133
9f3f71ef
MCC
134 unsigned int type; /* chip type id */
135 unsigned int config;
136 const char *name;
137};
138
a2894e3f
MCC
139/*
140 * Function prototypes
141 */
142
143static void set_tv_freq(struct i2c_client *c, unsigned int freq);
144static void set_radio_freq(struct i2c_client *c, unsigned int freq);
145
9f3f71ef
MCC
146/*
147 * tuner attach/detach logic
148 */
149
0ae79d99 150/* This macro allows us to probe dynamically, avoiding static links */
ff138171 151#ifdef CONFIG_MEDIA_ATTACH
a07c8779
MK
152#define tuner_symbol_probe(FUNCTION, ARGS...) ({ \
153 int __r = -EINVAL; \
154 typeof(&FUNCTION) __a = symbol_request(FUNCTION); \
155 if (__a) { \
156 __r = (int) __a(ARGS); \
a1355e53 157 symbol_put(FUNCTION); \
a07c8779
MK
158 } else { \
159 printk(KERN_ERR "TUNER: Unable to find " \
160 "symbol "#FUNCTION"()\n"); \
161 } \
a07c8779
MK
162 __r; \
163})
164
165static void tuner_detach(struct dvb_frontend *fe)
166{
167 if (fe->ops.tuner_ops.release) {
168 fe->ops.tuner_ops.release(fe);
169 symbol_put_addr(fe->ops.tuner_ops.release);
170 }
171 if (fe->ops.analog_ops.release) {
172 fe->ops.analog_ops.release(fe);
173 symbol_put_addr(fe->ops.analog_ops.release);
174 }
175}
176#else
177#define tuner_symbol_probe(FUNCTION, ARGS...) ({ \
178 FUNCTION(ARGS); \
179})
180
181static void tuner_detach(struct dvb_frontend *fe)
182{
183 if (fe->ops.tuner_ops.release)
184 fe->ops.tuner_ops.release(fe);
185 if (fe->ops.analog_ops.release)
186 fe->ops.analog_ops.release(fe);
187}
188#endif
189
f7f427e4 190
e8a4a9e7
HV
191static inline struct tuner *to_tuner(struct v4l2_subdev *sd)
192{
193 return container_of(sd, struct tuner, sd);
194}
195
9f3f71ef
MCC
196/*
197 * struct analog_demod_ops callbacks
198 */
1da177e4 199
c7919d52
MK
200static void fe_set_params(struct dvb_frontend *fe,
201 struct analog_parameters *params)
e18f9444 202{
4e9154b8
MK
203 struct dvb_tuner_ops *fe_tuner_ops = &fe->ops.tuner_ops;
204 struct tuner *t = fe->analog_demod_priv;
e18f9444 205
e18f9444
MK
206 if (NULL == fe_tuner_ops->set_analog_params) {
207 tuner_warn("Tuner frontend module has no way to set freq\n");
208 return;
209 }
c7919d52 210 fe_tuner_ops->set_analog_params(fe, params);
e18f9444
MK
211}
212
4e9154b8 213static void fe_standby(struct dvb_frontend *fe)
e18f9444 214{
4e9154b8 215 struct dvb_tuner_ops *fe_tuner_ops = &fe->ops.tuner_ops;
e18f9444
MK
216
217 if (fe_tuner_ops->sleep)
4e9154b8 218 fe_tuner_ops->sleep(fe);
e18f9444
MK
219}
220
4e9154b8 221static int fe_has_signal(struct dvb_frontend *fe)
1f5ef197 222{
1419683d 223 u16 strength = 0;
1f5ef197 224
4e9154b8
MK
225 if (fe->ops.tuner_ops.get_rf_strength)
226 fe->ops.tuner_ops.get_rf_strength(fe, &strength);
1f5ef197
MK
227
228 return strength;
229}
230
f1c9a281
MK
231static int fe_set_config(struct dvb_frontend *fe, void *priv_cfg)
232{
233 struct dvb_tuner_ops *fe_tuner_ops = &fe->ops.tuner_ops;
234 struct tuner *t = fe->analog_demod_priv;
235
236 if (fe_tuner_ops->set_config)
237 return fe_tuner_ops->set_config(fe, priv_cfg);
238
239 tuner_warn("Tuner frontend module has no way to set config\n");
240
241 return 0;
242}
243
4e9154b8 244static void tuner_status(struct dvb_frontend *fe);
1dde7a4f 245
e8a4a9e7 246static struct analog_demod_ops tuner_analog_ops = {
c7919d52 247 .set_params = fe_set_params,
1dde7a4f 248 .standby = fe_standby,
1dde7a4f 249 .has_signal = fe_has_signal,
f1c9a281 250 .set_config = fe_set_config,
1dde7a4f
MK
251 .tuner_status = tuner_status
252};
253
9f3f71ef 254/*
0eec66c0 255 * Functions to select between radio and TV and tuner probe/remove functions
9f3f71ef 256 */
1da177e4 257
a2894e3f
MCC
258/**
259 * set_type - Sets the tuner type for a given device
260 *
261 * @c: i2c_client descriptoy
262 * @type: type of the tuner (e. g. tuner number)
263 * @new_mode_mask: Indicates if tuner supports TV and/or Radio
264 * @new_config: an optional parameter ranging from 0-255 used by
265 a few tuners to adjust an internal parameter,
266 like LNA mode
267 * @tuner_callback: an optional function to be called when switching
268 * to analog mode
269 *
270 * This function applys the tuner config to tuner specified
271 * by tun_setup structure. It contains several per-tuner initialization "magic"
272 */
f7ce3cc6 273static void set_type(struct i2c_client *c, unsigned int type,
de956c1e 274 unsigned int new_mode_mask, unsigned int new_config,
d7cba043 275 int (*tuner_callback) (void *dev, int component, int cmd, int arg))
1da177e4 276{
e8a4a9e7 277 struct tuner *t = to_tuner(i2c_get_clientdata(c));
e18f9444 278 struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops;
bc3e5c7f 279 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
586b0cab 280 unsigned char buffer[4];
d6eef494 281 int tune_now = 1;
1da177e4 282
f7ce3cc6 283 if (type == UNSET || type == TUNER_ABSENT) {
7d275bf8 284 tuner_dbg("tuner 0x%02x: Tuner type absent\n", c->addr);
1da177e4 285 return;
f7ce3cc6
MCC
286 }
287
80f90fba 288 t->type = type;
e7ddcd98 289 /* prevent invalid config values */
f14a2972 290 t->config = new_config < 256 ? new_config : 0;
80f90fba
HH
291 if (tuner_callback != NULL) {
292 tuner_dbg("defining GPIO callback\n");
d7cba043 293 t->fe.callback = tuner_callback;
80f90fba
HH
294 }
295
b2083199 296 /* discard private data, in case set_type() was previously called */
a07c8779
MK
297 tuner_detach(&t->fe);
298 t->fe.analog_demod_priv = NULL;
be2b85a1 299
1da177e4
LT
300 switch (t->type) {
301 case TUNER_MT2032:
09fee5f8
MCC
302 if (!dvb_attach(microtune_attach,
303 &t->fe, t->i2c->adapter, t->i2c->addr))
304 goto attach_failed;
1da177e4
LT
305 break;
306 case TUNER_PHILIPS_TDA8290:
5bea1cd3 307 {
09fee5f8
MCC
308 struct tda829x_config cfg = {
309 .lna_cfg = t->config,
09fee5f8
MCC
310 };
311 if (!dvb_attach(tda829x_attach, &t->fe, t->i2c->adapter,
312 t->i2c->addr, &cfg))
313 goto attach_failed;
5bea1cd3
MK
314 break;
315 }
586b0cab 316 case TUNER_TEA5767:
a07c8779
MK
317 if (!dvb_attach(tea5767_attach, &t->fe,
318 t->i2c->adapter, t->i2c->addr))
b9ef6bbb 319 goto attach_failed;
f7ce3cc6 320 t->mode_mask = T_RADIO;
586b0cab 321 break;
8573a9e6 322 case TUNER_TEA5761:
a07c8779
MK
323 if (!dvb_attach(tea5761_attach, &t->fe,
324 t->i2c->adapter, t->i2c->addr))
b9ef6bbb 325 goto attach_failed;
8573a9e6
MCC
326 t->mode_mask = T_RADIO;
327 break;
586b0cab 328 case TUNER_PHILIPS_FMD1216ME_MK3:
27b93d8a 329 case TUNER_PHILIPS_FMD1216MEX_MK3:
586b0cab
MCC
330 buffer[0] = 0x0b;
331 buffer[1] = 0xdc;
332 buffer[2] = 0x9c;
333 buffer[3] = 0x60;
f7ce3cc6 334 i2c_master_send(c, buffer, 4);
586b0cab
MCC
335 mdelay(1);
336 buffer[2] = 0x86;
337 buffer[3] = 0x54;
f7ce3cc6 338 i2c_master_send(c, buffer, 4);
a07c8779
MK
339 if (!dvb_attach(simple_tuner_attach, &t->fe,
340 t->i2c->adapter, t->i2c->addr, t->type))
b9ef6bbb 341 goto attach_failed;
586b0cab 342 break;
93df3413
HH
343 case TUNER_PHILIPS_TD1316:
344 buffer[0] = 0x0b;
345 buffer[1] = 0xdc;
346 buffer[2] = 0x86;
347 buffer[3] = 0xa4;
a07c8779
MK
348 i2c_master_send(c, buffer, 4);
349 if (!dvb_attach(simple_tuner_attach, &t->fe,
350 t->i2c->adapter, t->i2c->addr, t->type))
b9ef6bbb 351 goto attach_failed;
ac272ed7 352 break;
690c544c
MCC
353 case TUNER_XC2028:
354 {
a37b4c9b
ML
355 struct xc2028_config cfg = {
356 .i2c_adap = t->i2c->adapter,
357 .i2c_addr = t->i2c->addr,
a37b4c9b 358 };
a07c8779 359 if (!dvb_attach(xc2028_attach, &t->fe, &cfg))
b9ef6bbb 360 goto attach_failed;
d6eef494 361 tune_now = 0;
690c544c
MCC
362 break;
363 }
15396236 364 case TUNER_TDA9887:
09fee5f8
MCC
365 if (!dvb_attach(tda9887_attach,
366 &t->fe, t->i2c->adapter, t->i2c->addr))
367 goto attach_failed;
15396236 368 break;
27c685a4 369 case TUNER_XC5000:
b9ef6bbb 370 {
900f734b
MCC
371 struct xc5000_config xc5000_cfg = {
372 .i2c_address = t->i2c->addr,
373 /* if_khz will be set at dvb_attach() */
374 .if_khz = 0,
375 };
376
a07c8779 377 if (!dvb_attach(xc5000_attach,
30650961 378 &t->fe, t->i2c->adapter, &xc5000_cfg))
b9ef6bbb 379 goto attach_failed;
d6eef494 380 tune_now = 0;
27c685a4 381 break;
b9ef6bbb 382 }
93463895
MK
383 case TUNER_NXP_TDA18271:
384 {
385 struct tda18271_config cfg = {
386 .config = t->config,
e350d44f 387 .small_i2c = TDA18271_03_BYTE_CHUNK_INIT,
93463895
MK
388 };
389
9f3f71ef
MCC
390 if (!dvb_attach(tda18271_attach, &t->fe, t->i2c->addr,
391 t->i2c->adapter, &cfg))
392 goto attach_failed;
393 tune_now = 0;
394 break;
395 }
8d009a0c
DF
396 case TUNER_XC4000:
397 {
398 struct xc4000_config xc4000_cfg = {
399 .i2c_address = t->i2c->addr,
8edeb6eb 400 /* FIXME: the correct parameters will be set */
401 /* only when the digital dvb_attach() occurs */
402 .default_pm = 0,
403 .dvb_amplitude = 0,
404 .set_smoothedcvbs = 0,
405 .if_khz = 0
8d009a0c
DF
406 };
407 if (!dvb_attach(xc4000_attach,
408 &t->fe, t->i2c->adapter, &xc4000_cfg))
409 goto attach_failed;
410 tune_now = 0;
411 break;
412 }
9f3f71ef
MCC
413 default:
414 if (!dvb_attach(simple_tuner_attach, &t->fe,
415 t->i2c->adapter, t->i2c->addr, t->type))
416 goto attach_failed;
417
418 break;
419 }
420
421 if ((NULL == analog_ops->set_params) &&
422 (fe_tuner_ops->set_analog_params)) {
423
424 t->name = fe_tuner_ops->info.name;
425
426 t->fe.analog_demod_priv = t;
427 memcpy(analog_ops, &tuner_analog_ops,
428 sizeof(struct analog_demod_ops));
429
430 } else {
431 t->name = analog_ops->info.name;
432 }
433
434 tuner_dbg("type set to %s\n", t->name);
435
cbde6898 436 t->mode_mask = new_mode_mask;
9f3f71ef
MCC
437
438 /* Some tuners require more initialization setup before use,
439 such as firmware download or device calibration.
440 trying to set a frequency here will just fail
441 FIXME: better to move set_freq to the tuner code. This is needed
442 on analog tuners for PLL to properly work
443 */
444 if (tune_now) {
445 if (V4L2_TUNER_RADIO == t->mode)
446 set_radio_freq(c, t->radio_freq);
447 else
448 set_tv_freq(c, t->tv_freq);
449 }
450
451 tuner_dbg("%s %s I2C addr 0x%02x with type %d used for 0x%02x\n",
452 c->adapter->name, c->driver->driver.name, c->addr << 1, type,
453 t->mode_mask);
454 return;
455
456attach_failed:
457 tuner_dbg("Tuner attach for type = %d failed.\n", t->type);
458 t->type = TUNER_ABSENT;
9f3f71ef
MCC
459
460 return;
461}
462
0ae79d99
MCC
463/**
464 * tuner_s_type_addr - Sets the tuner type for a device
465 *
466 * @sd: subdev descriptor
467 * @tun_setup: type to be associated to a given tuner i2c address
468 *
469 * This function applys the tuner config to tuner specified
470 * by tun_setup structure.
471 * If tuner I2C address is UNSET, then it will only set the device
472 * if the tuner supports the mode specified in the call.
473 * If the address is specified, the change will be applied only if
474 * tuner I2C address matches.
475 * The call can change the tuner number and the tuner mode.
476 */
a34ec5f3
MCC
477static int tuner_s_type_addr(struct v4l2_subdev *sd,
478 struct tuner_setup *tun_setup)
9f3f71ef 479{
a34ec5f3
MCC
480 struct tuner *t = to_tuner(sd);
481 struct i2c_client *c = v4l2_get_subdevdata(sd);
482
483 tuner_dbg("Calling set_type_addr for type=%d, addr=0x%02x, mode=0x%02x, config=0x%02x\n",
484 tun_setup->type,
485 tun_setup->addr,
486 tun_setup->mode_mask,
487 tun_setup->config);
9f3f71ef 488
7d275bf8
MCC
489 if ((t->type == UNSET && ((tun_setup->addr == ADDR_UNSET) &&
490 (t->mode_mask & tun_setup->mode_mask))) ||
491 (tun_setup->addr == c->addr)) {
cbde6898
MCC
492 set_type(c, tun_setup->type, tun_setup->mode_mask,
493 tun_setup->config, tun_setup->tuner_callback);
9f3f71ef
MCC
494 } else
495 tuner_dbg("set addr discarded for type %i, mask %x. "
496 "Asked to change tuner at addr 0x%02x, with mask %x\n",
497 t->type, t->mode_mask,
498 tun_setup->addr, tun_setup->mode_mask);
9f3f71ef 499
9f3f71ef
MCC
500 return 0;
501}
502
a2894e3f
MCC
503/**
504 * tuner_s_config - Sets tuner configuration
505 *
506 * @sd: subdev descriptor
507 * @cfg: tuner configuration
508 *
509 * Calls tuner set_config() private function to set some tuner-internal
510 * parameters
511 */
7d275bf8
MCC
512static int tuner_s_config(struct v4l2_subdev *sd,
513 const struct v4l2_priv_tun_config *cfg)
9f3f71ef
MCC
514{
515 struct tuner *t = to_tuner(sd);
516 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
517
518 if (t->type != cfg->tuner)
519 return 0;
520
521 if (analog_ops->set_config) {
522 analog_ops->set_config(&t->fe, cfg->priv);
523 return 0;
524 }
525
526 tuner_dbg("Tuner frontend module has no way to set config\n");
527 return 0;
528}
529
a2894e3f
MCC
530/**
531 * tuner_lookup - Seek for tuner adapters
532 *
533 * @adap: i2c_adapter struct
534 * @radio: pointer to be filled if the adapter is radio
535 * @tv: pointer to be filled if the adapter is TV
536 *
537 * Search for existing radio and/or TV tuners on the given I2C adapter,
538 * discarding demod-only adapters (tda9887).
539 *
540 * Note that when this function is called from tuner_probe you can be
541 * certain no other devices will be added/deleted at the same time, I2C
542 * core protects against that.
543 */
9f3f71ef
MCC
544static void tuner_lookup(struct i2c_adapter *adap,
545 struct tuner **radio, struct tuner **tv)
546{
547 struct tuner *pos;
548
549 *radio = NULL;
550 *tv = NULL;
551
552 list_for_each_entry(pos, &tuner_list, list) {
553 int mode_mask;
554
555 if (pos->i2c->adapter != adap ||
556 strcmp(pos->i2c->driver->driver.name, "tuner"))
557 continue;
558
cbde6898 559 mode_mask = pos->mode_mask;
9f3f71ef
MCC
560 if (*radio == NULL && mode_mask == T_RADIO)
561 *radio = pos;
562 /* Note: currently TDA9887 is the only demod-only
563 device. If other devices appear then we need to
564 make this test more general. */
565 else if (*tv == NULL && pos->type != TUNER_TDA9887 &&
ad020dc2 566 (pos->mode_mask & T_ANALOG_TV))
9f3f71ef
MCC
567 *tv = pos;
568 }
569}
570
a2894e3f
MCC
571/**
572 *tuner_probe - Probes the existing tuners on an I2C bus
573 *
574 * @client: i2c_client descriptor
575 * @id: not used
576 *
577 * This routine probes for tuners at the expected I2C addresses. On most
578 * cases, if a device answers to a given I2C address, it assumes that the
579 * device is a tuner. On a few cases, however, an additional logic is needed
580 * to double check if the device is really a tuner, or to identify the tuner
581 * type, like on tea5767/5761 devices.
582 *
583 * During client attach, set_type is called by adapter's attach_inform callback.
584 * set_type must then be completed by tuner_probe.
9f3f71ef
MCC
585 */
586static int tuner_probe(struct i2c_client *client,
587 const struct i2c_device_id *id)
588{
589 struct tuner *t;
590 struct tuner *radio;
591 struct tuner *tv;
592
593 t = kzalloc(sizeof(struct tuner), GFP_KERNEL);
594 if (NULL == t)
595 return -ENOMEM;
596 v4l2_i2c_subdev_init(&t->sd, client, &tuner_ops);
597 t->i2c = client;
598 t->name = "(tuner unset)";
599 t->type = UNSET;
600 t->audmode = V4L2_TUNER_MODE_STEREO;
cbde6898
MCC
601 t->standby = 1;
602 t->radio_freq = 87.5 * 16000; /* Initial freq range */
603 t->tv_freq = 400 * 16; /* Sets freq to VHF High - needed for some PLL's to properly start */
9f3f71ef
MCC
604
605 if (show_i2c) {
606 unsigned char buffer[16];
607 int i, rc;
608
609 memset(buffer, 0, sizeof(buffer));
610 rc = i2c_master_recv(client, buffer, sizeof(buffer));
611 tuner_info("I2C RECV = ");
612 for (i = 0; i < rc; i++)
613 printk(KERN_CONT "%02x ", buffer[i]);
614 printk("\n");
615 }
616
617 /* autodetection code based on the i2c addr */
618 if (!no_autodetect) {
619 switch (client->addr) {
620 case 0x10:
621 if (tuner_symbol_probe(tea5761_autodetection,
622 t->i2c->adapter,
623 t->i2c->addr) >= 0) {
624 t->type = TUNER_TEA5761;
625 t->mode_mask = T_RADIO;
9f3f71ef
MCC
626 tuner_lookup(t->i2c->adapter, &radio, &tv);
627 if (tv)
628 tv->mode_mask &= ~T_RADIO;
629
630 goto register_client;
631 }
632 kfree(t);
633 return -ENODEV;
634 case 0x42:
635 case 0x43:
636 case 0x4a:
637 case 0x4b:
638 /* If chip is not tda8290, don't register.
639 since it can be tda9887*/
640 if (tuner_symbol_probe(tda829x_probe, t->i2c->adapter,
641 t->i2c->addr) >= 0) {
642 tuner_dbg("tda829x detected\n");
643 } else {
644 /* Default is being tda9887 */
645 t->type = TUNER_TDA9887;
ad020dc2 646 t->mode_mask = T_RADIO | T_ANALOG_TV;
9f3f71ef
MCC
647 goto register_client;
648 }
649 break;
650 case 0x60:
651 if (tuner_symbol_probe(tea5767_autodetection,
652 t->i2c->adapter, t->i2c->addr)
653 >= 0) {
654 t->type = TUNER_TEA5767;
655 t->mode_mask = T_RADIO;
9f3f71ef 656 /* Sets freq to FM range */
9f3f71ef
MCC
657 tuner_lookup(t->i2c->adapter, &radio, &tv);
658 if (tv)
659 tv->mode_mask &= ~T_RADIO;
660
661 goto register_client;
662 }
663 break;
664 }
93463895 665 }
b9ef6bbb 666
9f3f71ef
MCC
667 /* Initializes only the first TV tuner on this adapter. Why only the
668 first? Because there are some devices (notably the ones with TI
669 tuners) that have more than one i2c address for the *same* device.
670 Experience shows that, except for just one case, the first
671 address is the right one. The exception is a Russian tuner
672 (ACORP_Y878F). So, the desired behavior is just to enable the
673 first found TV tuner. */
674 tuner_lookup(t->i2c->adapter, &radio, &tv);
675 if (tv == NULL) {
ad020dc2 676 t->mode_mask = T_ANALOG_TV;
9f3f71ef
MCC
677 if (radio == NULL)
678 t->mode_mask |= T_RADIO;
679 tuner_dbg("Setting mode_mask to 0x%02x\n", t->mode_mask);
1da177e4 680 }
f7ce3cc6 681
9f3f71ef
MCC
682 /* Should be just before return */
683register_client:
9f3f71ef 684 /* Sets a default mode */
7d275bf8 685 if (t->mode_mask & T_ANALOG_TV)
9f3f71ef 686 t->mode = V4L2_TUNER_ANALOG_TV;
7d275bf8 687 else
ad020dc2 688 t->mode = V4L2_TUNER_RADIO;
9f3f71ef
MCC
689 set_type(client, t->type, t->mode_mask, t->config, t->fe.callback);
690 list_add_tail(&t->list, &tuner_list);
cbde6898 691
ad020dc2 692 tuner_info("Tuner %d found with type(s)%s%s.\n",
cbde6898 693 t->type,
ad020dc2
MCC
694 t->mode_mask & T_RADIO ? " Radio" : "",
695 t->mode_mask & T_ANALOG_TV ? " TV" : "");
9f3f71ef
MCC
696 return 0;
697}
e18f9444 698
a2894e3f
MCC
699/**
700 * tuner_remove - detaches a tuner
701 *
702 * @client: i2c_client descriptor
703 */
704
9f3f71ef
MCC
705static int tuner_remove(struct i2c_client *client)
706{
707 struct tuner *t = to_tuner(i2c_get_clientdata(client));
b9ef6bbb 708
9f3f71ef
MCC
709 v4l2_device_unregister_subdev(&t->sd);
710 tuner_detach(&t->fe);
711 t->fe.analog_demod_priv = NULL;
b9ef6bbb 712
9f3f71ef
MCC
713 list_del(&t->list);
714 kfree(t);
715 return 0;
1da177e4
LT
716}
717
0eec66c0
MCC
718/*
719 * Functions to switch between Radio and TV
720 *
721 * A few cards have a separate I2C tuner for radio. Those routines
722 * take care of switching between TV/Radio mode, filtering only the
723 * commands that apply to the Radio or TV tuner.
724 */
725
726/**
727 * check_mode - Verify if tuner supports the requested mode
728 * @t: a pointer to the module's internal struct_tuner
729 *
730 * This function checks if the tuner is capable of tuning analog TV,
731 * digital TV or radio, depending on what the caller wants. If the
732 * tuner can't support that mode, it returns -EINVAL. Otherwise, it
733 * returns 0.
734 * This function is needed for boards that have a separate tuner for
735 * radio (like devices with tea5767).
a1ad5ec7
MCC
736 * NOTE: mt20xx uses V4L2_TUNER_DIGITAL_TV and calls set_tv_freq to
737 * select a TV frequency. So, t_mode = T_ANALOG_TV could actually
738 * be used to represent a Digital TV too.
0eec66c0
MCC
739 */
740static inline int check_mode(struct tuner *t, enum v4l2_tuner_type mode)
741{
a1ad5ec7
MCC
742 int t_mode;
743 if (mode == V4L2_TUNER_RADIO)
744 t_mode = T_RADIO;
745 else
746 t_mode = T_ANALOG_TV;
747
748 if ((t_mode & t->mode_mask) == 0)
0eec66c0
MCC
749 return -EINVAL;
750
751 return 0;
752}
753
754/**
4e4a31fb 755 * set_mode - Switch tuner to other mode.
0eec66c0
MCC
756 * @t: a pointer to the module's internal struct_tuner
757 * @mode: enum v4l2_type (radio or TV)
0eec66c0
MCC
758 *
759 * If tuner doesn't support the needed mode (radio or TV), prints a
760 * debug message and returns -EINVAL, changing its state to standby.
4e4a31fb 761 * Otherwise, changes the mode and returns 0.
0eec66c0 762 */
4e4a31fb 763static int set_mode(struct tuner *t, enum v4l2_tuner_type mode)
0eec66c0
MCC
764{
765 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
766
767 if (mode != t->mode) {
768 if (check_mode(t, mode) == -EINVAL) {
769 tuner_dbg("Tuner doesn't support mode %d. "
770 "Putting tuner to sleep\n", mode);
771 t->standby = true;
772 if (analog_ops->standby)
773 analog_ops->standby(&t->fe);
774 return -EINVAL;
775 }
776 t->mode = mode;
777 tuner_dbg("Changing to mode %d\n", mode);
778 }
4e4a31fb
HV
779 return 0;
780}
781
782/**
783 * set_freq - Set the tuner to the desired frequency.
784 * @t: a pointer to the module's internal struct_tuner
785 * @freq: frequency to set (0 means to use the current frequency)
786 */
787static void set_freq(struct tuner *t, unsigned int freq)
788{
789 struct i2c_client *client = v4l2_get_subdevdata(&t->sd);
790
0eec66c0 791 if (t->mode == V4L2_TUNER_RADIO) {
4e4a31fb
HV
792 if (!freq)
793 freq = t->radio_freq;
794 set_radio_freq(client, freq);
0eec66c0 795 } else {
4e4a31fb
HV
796 if (!freq)
797 freq = t->tv_freq;
798 set_tv_freq(client, freq);
0eec66c0 799 }
0eec66c0
MCC
800}
801
f7ce3cc6 802/*
9f3f71ef
MCC
803 * Functions that are specific for TV mode
804 */
f7ce3cc6 805
a2894e3f
MCC
806/**
807 * set_tv_freq - Set tuner frequency, freq in Units of 62.5 kHz = 1/16MHz
808 *
809 * @c: i2c_client descriptor
810 * @freq: frequency
811 */
9f3f71ef 812static void set_tv_freq(struct i2c_client *c, unsigned int freq)
f7ce3cc6 813{
e8a4a9e7 814 struct tuner *t = to_tuner(i2c_get_clientdata(c));
9f3f71ef 815 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
f7ce3cc6 816
9f3f71ef
MCC
817 struct analog_parameters params = {
818 .mode = t->mode,
819 .audmode = t->audmode,
820 .std = t->std
821 };
56fc08ca 822
9f3f71ef 823 if (t->type == UNSET) {
7d275bf8 824 tuner_warn("tuner type not set\n");
9f3f71ef 825 return;
793cf9e6 826 }
9f3f71ef 827 if (NULL == analog_ops->set_params) {
7d275bf8 828 tuner_warn("Tuner has no way to set tv freq\n");
9f3f71ef 829 return;
56fc08ca 830 }
9f3f71ef 831 if (freq < tv_range[0] * 16 || freq > tv_range[1] * 16) {
7d275bf8 832 tuner_dbg("TV freq (%d.%02d) out of range (%d-%d)\n",
9f3f71ef
MCC
833 freq / 16, freq % 16 * 100 / 16, tv_range[0],
834 tv_range[1]);
835 /* V4L2 spec: if the freq is not possible then the closest
836 possible value should be selected */
837 if (freq < tv_range[0] * 16)
838 freq = tv_range[0] * 16;
839 else
840 freq = tv_range[1] * 16;
841 }
842 params.frequency = freq;
843 tuner_dbg("tv freq set to %d.%02d\n",
844 freq / 16, freq % 16 * 100 / 16);
845 t->tv_freq = freq;
cbde6898 846 t->standby = false;
9f3f71ef
MCC
847
848 analog_ops->set_params(&t->fe, &params);
56fc08ca 849}
56fc08ca 850
a2894e3f
MCC
851/**
852 * tuner_fixup_std - force a given video standard variant
853 *
48783301
HV
854 * @t: tuner internal struct
855 * @std: TV standard
a2894e3f
MCC
856 *
857 * A few devices or drivers have problem to detect some standard variations.
858 * On other operational systems, the drivers generally have a per-country
859 * code, and some logic to apply per-country hacks. V4L2 API doesn't provide
860 * such hacks. Instead, it relies on a proper video standard selection from
861 * the userspace application. However, as some apps are buggy, not allowing
862 * to distinguish all video standard variations, a modprobe parameter can
863 * be used to force a video standard match.
864 */
48783301 865static v4l2_std_id tuner_fixup_std(struct tuner *t, v4l2_std_id std)
1da177e4 866{
48783301 867 if (pal[0] != '-' && (std & V4L2_STD_PAL) == V4L2_STD_PAL) {
1da177e4 868 switch (pal[0]) {
e71ced1a 869 case '6':
48783301 870 return V4L2_STD_PAL_60;
1da177e4
LT
871 case 'b':
872 case 'B':
873 case 'g':
874 case 'G':
48783301 875 return V4L2_STD_PAL_BG;
1da177e4
LT
876 case 'i':
877 case 'I':
48783301 878 return V4L2_STD_PAL_I;
1da177e4
LT
879 case 'd':
880 case 'D':
881 case 'k':
882 case 'K':
48783301 883 return V4L2_STD_PAL_DK;
f7ce3cc6
MCC
884 case 'M':
885 case 'm':
48783301 886 return V4L2_STD_PAL_M;
f7ce3cc6
MCC
887 case 'N':
888 case 'n':
48783301
HV
889 if (pal[1] == 'c' || pal[1] == 'C')
890 return V4L2_STD_PAL_Nc;
891 return V4L2_STD_PAL_N;
21d4df37 892 default:
7d275bf8 893 tuner_warn("pal= argument not recognised\n");
21d4df37 894 break;
1da177e4
LT
895 }
896 }
48783301 897 if (secam[0] != '-' && (std & V4L2_STD_SECAM) == V4L2_STD_SECAM) {
f7ce3cc6 898 switch (secam[0]) {
7e578191
MCC
899 case 'b':
900 case 'B':
901 case 'g':
902 case 'G':
903 case 'h':
904 case 'H':
48783301
HV
905 return V4L2_STD_SECAM_B |
906 V4L2_STD_SECAM_G |
907 V4L2_STD_SECAM_H;
f7ce3cc6
MCC
908 case 'd':
909 case 'D':
910 case 'k':
911 case 'K':
48783301 912 return V4L2_STD_SECAM_DK;
f7ce3cc6
MCC
913 case 'l':
914 case 'L':
48783301
HV
915 if ((secam[1] == 'C') || (secam[1] == 'c'))
916 return V4L2_STD_SECAM_LC;
917 return V4L2_STD_SECAM_L;
21d4df37 918 default:
7d275bf8 919 tuner_warn("secam= argument not recognised\n");
21d4df37 920 break;
f7ce3cc6
MCC
921 }
922 }
923
48783301 924 if (ntsc[0] != '-' && (std & V4L2_STD_NTSC) == V4L2_STD_NTSC) {
7e578191
MCC
925 switch (ntsc[0]) {
926 case 'm':
927 case 'M':
48783301 928 return V4L2_STD_NTSC_M;
7e578191
MCC
929 case 'j':
930 case 'J':
48783301 931 return V4L2_STD_NTSC_M_JP;
d97a11e0
HV
932 case 'k':
933 case 'K':
48783301 934 return V4L2_STD_NTSC_M_KR;
7e578191
MCC
935 default:
936 tuner_info("ntsc= argument not recognised\n");
937 break;
938 }
939 }
48783301 940 return std;
9f3f71ef
MCC
941}
942
943/*
944 * Functions that are specific for Radio mode
945 */
946
a2894e3f
MCC
947/**
948 * set_radio_freq - Set tuner frequency, freq in Units of 62.5 Hz = 1/16kHz
949 *
950 * @c: i2c_client descriptor
951 * @freq: frequency
952 */
9f3f71ef
MCC
953static void set_radio_freq(struct i2c_client *c, unsigned int freq)
954{
955 struct tuner *t = to_tuner(i2c_get_clientdata(c));
956 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
957
958 struct analog_parameters params = {
959 .mode = t->mode,
960 .audmode = t->audmode,
961 .std = t->std
962 };
963
964 if (t->type == UNSET) {
7d275bf8 965 tuner_warn("tuner type not set\n");
9f3f71ef
MCC
966 return;
967 }
968 if (NULL == analog_ops->set_params) {
7d275bf8 969 tuner_warn("tuner has no way to set radio frequency\n");
9f3f71ef
MCC
970 return;
971 }
972 if (freq < radio_range[0] * 16000 || freq > radio_range[1] * 16000) {
7d275bf8 973 tuner_dbg("radio freq (%d.%02d) out of range (%d-%d)\n",
9f3f71ef
MCC
974 freq / 16000, freq % 16000 * 100 / 16000,
975 radio_range[0], radio_range[1]);
976 /* V4L2 spec: if the freq is not possible then the closest
977 possible value should be selected */
978 if (freq < radio_range[0] * 16000)
979 freq = radio_range[0] * 16000;
980 else
981 freq = radio_range[1] * 16000;
982 }
983 params.frequency = freq;
984 tuner_dbg("radio freq set to %d.%02d\n",
985 freq / 16000, freq % 16000 * 100 / 16000);
986 t->radio_freq = freq;
cbde6898 987 t->standby = false;
9f3f71ef
MCC
988
989 analog_ops->set_params(&t->fe, &params);
990}
991
0eec66c0
MCC
992/*
993 * Debug function for reporting tuner status to userspace
9f3f71ef 994 */
9f3f71ef 995
cbde6898
MCC
996/**
997 * tuner_status - Dumps the current tuner status at dmesg
998 * @fe: pointer to struct dvb_frontend
999 *
1000 * This callback is used only for driver debug purposes, answering to
1001 * VIDIOC_LOG_STATUS. No changes should happen on this call.
1002 */
4e9154b8 1003static void tuner_status(struct dvb_frontend *fe)
7e578191 1004{
4e9154b8 1005 struct tuner *t = fe->analog_demod_priv;
7e578191 1006 unsigned long freq, freq_fraction;
a07c8779
MK
1007 struct dvb_tuner_ops *fe_tuner_ops = &fe->ops.tuner_ops;
1008 struct analog_demod_ops *analog_ops = &fe->ops.analog_ops;
7e578191
MCC
1009 const char *p;
1010
1011 switch (t->mode) {
7d275bf8
MCC
1012 case V4L2_TUNER_RADIO:
1013 p = "radio";
1014 break;
a1ad5ec7 1015 case V4L2_TUNER_DIGITAL_TV: /* Used by mt20xx */
7d275bf8
MCC
1016 p = "digital TV";
1017 break;
1018 case V4L2_TUNER_ANALOG_TV:
1019 default:
1020 p = "analog TV";
1021 break;
7e578191
MCC
1022 }
1023 if (t->mode == V4L2_TUNER_RADIO) {
27487d44
HV
1024 freq = t->radio_freq / 16000;
1025 freq_fraction = (t->radio_freq % 16000) * 100 / 16000;
7e578191 1026 } else {
27487d44
HV
1027 freq = t->tv_freq / 16;
1028 freq_fraction = (t->tv_freq % 16) * 100 / 16;
7e578191 1029 }
cbde6898
MCC
1030 tuner_info("Tuner mode: %s%s\n", p,
1031 t->standby ? " on standby mode" : "");
7e578191 1032 tuner_info("Frequency: %lu.%02lu MHz\n", freq, freq_fraction);
4ae5c2e5 1033 tuner_info("Standard: 0x%08lx\n", (unsigned long)t->std);
8a4b275f 1034 if (t->mode != V4L2_TUNER_RADIO)
7d275bf8 1035 return;
e18f9444
MK
1036 if (fe_tuner_ops->get_status) {
1037 u32 tuner_status;
1038
1039 fe_tuner_ops->get_status(&t->fe, &tuner_status);
1040 if (tuner_status & TUNER_STATUS_LOCKED)
1041 tuner_info("Tuner is locked.\n");
1042 if (tuner_status & TUNER_STATUS_STEREO)
1043 tuner_info("Stereo: yes\n");
1044 }
bc3e5c7f
MK
1045 if (analog_ops->has_signal)
1046 tuner_info("Signal strength: %d\n",
1047 analog_ops->has_signal(fe));
7e578191 1048}
8a4b275f 1049
0eec66c0
MCC
1050/*
1051 * Function to splicitly change mode to radio. Probably not needed anymore
1052 */
1053
1054static int tuner_s_radio(struct v4l2_subdev *sd)
1055{
1056 struct tuner *t = to_tuner(sd);
0eec66c0 1057
4e4a31fb
HV
1058 if (set_mode(t, V4L2_TUNER_RADIO) == 0)
1059 set_freq(t, 0);
0eec66c0
MCC
1060 return 0;
1061}
1062
1063/*
1064 * Tuner callbacks to handle userspace ioctl's
1065 */
1066
cbde6898
MCC
1067/**
1068 * tuner_s_power - controls the power state of the tuner
1069 * @sd: pointer to struct v4l2_subdev
d16625e7 1070 * @on: a zero value puts the tuner to sleep, non-zero wakes it up
cbde6898 1071 */
622b828a 1072static int tuner_s_power(struct v4l2_subdev *sd, int on)
e8a4a9e7
HV
1073{
1074 struct tuner *t = to_tuner(sd);
bc3e5c7f 1075 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
1da177e4 1076
d16625e7
HV
1077 if (on) {
1078 if (t->standby && set_mode(t, t->mode) == 0) {
1079 tuner_dbg("Waking up tuner\n");
1080 set_freq(t, 0);
1081 }
622b828a 1082 return 0;
d16625e7 1083 }
622b828a 1084
16a5e53d 1085 tuner_dbg("Putting tuner to sleep\n");
cbde6898 1086 t->standby = true;
e8a4a9e7
HV
1087 if (analog_ops->standby)
1088 analog_ops->standby(&t->fe);
1089 return 0;
1090}
5e453dc7 1091
e8a4a9e7
HV
1092static int tuner_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
1093{
1094 struct tuner *t = to_tuner(sd);
f7ce3cc6 1095
4e4a31fb 1096 if (set_mode(t, V4L2_TUNER_ANALOG_TV))
e8a4a9e7 1097 return 0;
f7ce3cc6 1098
48783301
HV
1099 t->std = tuner_fixup_std(t, std);
1100 if (t->std != std)
1101 tuner_dbg("Fixup standard %llx to %llx\n", std, t->std);
4e4a31fb 1102 set_freq(t, 0);
e8a4a9e7
HV
1103 return 0;
1104}
f7ce3cc6 1105
e8a4a9e7
HV
1106static int tuner_s_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f)
1107{
1108 struct tuner *t = to_tuner(sd);
8a4b275f 1109
4e4a31fb
HV
1110 if (set_mode(t, f->type) == 0)
1111 set_freq(t, f->frequency);
e8a4a9e7
HV
1112 return 0;
1113}
f7ce3cc6 1114
338e9e1a
HV
1115/**
1116 * tuner_g_frequency - Get the tuned frequency for the tuner
1117 * @sd: pointer to struct v4l2_subdev
1118 * @f: pointer to struct v4l2_frequency
1119 *
1120 * At return, the structure f will be filled with tuner frequency
1121 * if the tuner matches the f->type.
1122 * Note: f->type should be initialized before calling it.
1123 * This is done by either video_ioctl2 or by the bridge driver.
1124 */
e8a4a9e7
HV
1125static int tuner_g_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f)
1126{
1127 struct tuner *t = to_tuner(sd);
1128 struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops;
f7ce3cc6 1129
cbde6898 1130 if (check_mode(t, f->type) == -EINVAL)
e8a4a9e7 1131 return 0;
5ad339a2 1132 if (f->type == t->mode && fe_tuner_ops->get_frequency && !t->standby) {
e8a4a9e7
HV
1133 u32 abs_freq;
1134
1135 fe_tuner_ops->get_frequency(&t->fe, &abs_freq);
1136 f->frequency = (V4L2_TUNER_RADIO == t->mode) ?
75b697f7
JL
1137 DIV_ROUND_CLOSEST(abs_freq * 2, 125) :
1138 DIV_ROUND_CLOSEST(abs_freq, 62500);
cbde6898 1139 } else {
5ad339a2 1140 f->frequency = (V4L2_TUNER_RADIO == f->type) ?
cbde6898 1141 t->radio_freq : t->tv_freq;
e8a4a9e7 1142 }
e8a4a9e7
HV
1143 return 0;
1144}
f7ce3cc6 1145
338e9e1a
HV
1146/**
1147 * tuner_g_tuner - Fill in tuner information
1148 * @sd: pointer to struct v4l2_subdev
1149 * @vt: pointer to struct v4l2_tuner
1150 *
1151 * At return, the structure vt will be filled with tuner information
1152 * if the tuner matches vt->type.
1153 * Note: vt->type should be initialized before calling it.
1154 * This is done by either video_ioctl2 or by the bridge driver.
1155 */
e8a4a9e7
HV
1156static int tuner_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
1157{
1158 struct tuner *t = to_tuner(sd);
1159 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
1160 struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops;
1161
cbde6898 1162 if (check_mode(t, vt->type) == -EINVAL)
e8a4a9e7 1163 return 0;
5ad339a2 1164 if (vt->type == t->mode && analog_ops->get_afc)
e8a4a9e7 1165 vt->afc = analog_ops->get_afc(&t->fe);
a1ad5ec7 1166 if (t->mode != V4L2_TUNER_RADIO) {
e8a4a9e7 1167 vt->capability |= V4L2_TUNER_CAP_NORM;
e8a4a9e7
HV
1168 vt->rangelow = tv_range[0] * 16;
1169 vt->rangehigh = tv_range[1] * 16;
1170 return 0;
1171 }
1172
1173 /* radio mode */
5ad339a2
HV
1174 if (vt->type == t->mode) {
1175 vt->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
1176 if (fe_tuner_ops->get_status) {
1177 u32 tuner_status;
1178
1179 fe_tuner_ops->get_status(&t->fe, &tuner_status);
1180 vt->rxsubchans =
1181 (tuner_status & TUNER_STATUS_STEREO) ?
1182 V4L2_TUNER_SUB_STEREO :
1183 V4L2_TUNER_SUB_MONO;
1184 }
1185 if (analog_ops->has_signal)
1186 vt->signal = analog_ops->has_signal(&t->fe);
1187 vt->audmode = t->audmode;
1da177e4 1188 }
cbde6898 1189 vt->capability |= V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
e8a4a9e7
HV
1190 vt->rangelow = radio_range[0] * 16000;
1191 vt->rangehigh = radio_range[1] * 16000;
cbde6898 1192
e8a4a9e7
HV
1193 return 0;
1194}
1195
338e9e1a
HV
1196/**
1197 * tuner_s_tuner - Set the tuner's audio mode
1198 * @sd: pointer to struct v4l2_subdev
1199 * @vt: pointer to struct v4l2_tuner
1200 *
1201 * Sets the audio mode if the tuner matches vt->type.
1202 * Note: vt->type should be initialized before calling it.
1203 * This is done by either video_ioctl2 or by the bridge driver.
1204 */
e8a4a9e7
HV
1205static int tuner_s_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
1206{
1207 struct tuner *t = to_tuner(sd);
e8a4a9e7 1208
4e4a31fb 1209 if (set_mode(t, vt->type))
e8a4a9e7
HV
1210 return 0;
1211
cbde6898
MCC
1212 if (t->mode == V4L2_TUNER_RADIO)
1213 t->audmode = vt->audmode;
4e4a31fb 1214 set_freq(t, 0);
cbde6898 1215
e8a4a9e7
HV
1216 return 0;
1217}
1218
1219static int tuner_log_status(struct v4l2_subdev *sd)
1220{
1221 struct tuner *t = to_tuner(sd);
1222 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
1da177e4 1223
e8a4a9e7
HV
1224 if (analog_ops->tuner_status)
1225 analog_ops->tuner_status(&t->fe);
1da177e4
LT
1226 return 0;
1227}
1228
21b48a70 1229static int tuner_suspend(struct i2c_client *c, pm_message_t state)
1da177e4 1230{
e8a4a9e7 1231 struct tuner *t = to_tuner(i2c_get_clientdata(c));
e2f63d9b 1232 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
1da177e4 1233
9dd659de 1234 tuner_dbg("suspend\n");
e2f63d9b
MCC
1235
1236 if (!t->standby && analog_ops->standby)
1237 analog_ops->standby(&t->fe);
1238
1da177e4
LT
1239 return 0;
1240}
1241
21b48a70 1242static int tuner_resume(struct i2c_client *c)
1da177e4 1243{
e8a4a9e7 1244 struct tuner *t = to_tuner(i2c_get_clientdata(c));
1da177e4 1245
9dd659de 1246 tuner_dbg("resume\n");
e2f63d9b
MCC
1247
1248 if (!t->standby)
9bf0ef06 1249 if (set_mode(t, t->mode) == 0)
4e4a31fb 1250 set_freq(t, 0);
e2f63d9b 1251
1da177e4
LT
1252 return 0;
1253}
1254
75b4c260
HV
1255static int tuner_command(struct i2c_client *client, unsigned cmd, void *arg)
1256{
1257 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1258
1259 /* TUNER_SET_CONFIG is still called by tuner-simple.c, so we have
1260 to handle it here.
1261 There must be a better way of doing this... */
1262 switch (cmd) {
1263 case TUNER_SET_CONFIG:
1264 return tuner_s_config(sd, arg);
1265 }
1266 return -ENOIOCTLCMD;
1267}
1268
a2894e3f
MCC
1269/*
1270 * Callback structs
1271 */
e8a4a9e7
HV
1272
1273static const struct v4l2_subdev_core_ops tuner_core_ops = {
1274 .log_status = tuner_log_status,
f41737ec 1275 .s_std = tuner_s_std,
622b828a 1276 .s_power = tuner_s_power,
e8a4a9e7
HV
1277};
1278
1279static const struct v4l2_subdev_tuner_ops tuner_tuner_ops = {
e8a4a9e7
HV
1280 .s_radio = tuner_s_radio,
1281 .g_tuner = tuner_g_tuner,
1282 .s_tuner = tuner_s_tuner,
1283 .s_frequency = tuner_s_frequency,
1284 .g_frequency = tuner_g_frequency,
1285 .s_type_addr = tuner_s_type_addr,
1286 .s_config = tuner_s_config,
1287};
1288
1289static const struct v4l2_subdev_ops tuner_ops = {
1290 .core = &tuner_core_ops,
1291 .tuner = &tuner_tuner_ops,
1292};
1293
a2894e3f
MCC
1294/*
1295 * I2C structs and module init functions
1296 */
1da177e4 1297
af294867
JD
1298static const struct i2c_device_id tuner_id[] = {
1299 { "tuner", }, /* autodetect */
1300 { }
1301};
1302MODULE_DEVICE_TABLE(i2c, tuner_id);
1303
02a2098a
HV
1304static struct i2c_driver tuner_driver = {
1305 .driver = {
1306 .owner = THIS_MODULE,
1307 .name = "tuner",
1308 },
1309 .probe = tuner_probe,
1310 .remove = tuner_remove,
1311 .command = tuner_command,
1312 .suspend = tuner_suspend,
1313 .resume = tuner_resume,
1314 .id_table = tuner_id,
1da177e4 1315};
1da177e4 1316
02a2098a
HV
1317static __init int init_tuner(void)
1318{
1319 return i2c_add_driver(&tuner_driver);
1320}
1321
1322static __exit void exit_tuner(void)
1323{
1324 i2c_del_driver(&tuner_driver);
1325}
1326
1327module_init(init_tuner);
1328module_exit(exit_tuner);
1329
9f3f71ef
MCC
1330MODULE_DESCRIPTION("device driver for various TV and TV+FM radio tuners");
1331MODULE_AUTHOR("Ralph Metzler, Gerd Knorr, Gunther Mayer");
1332MODULE_LICENSE("GPL");
This page took 0.884314 seconds and 5 git commands to generate.