[media] V4L: soc-camera: make (almost) all client drivers re-usable outside of the...
[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
MCC
328 case TUNER_PHILIPS_FMD1216ME_MK3:
329 buffer[0] = 0x0b;
330 buffer[1] = 0xdc;
331 buffer[2] = 0x9c;
332 buffer[3] = 0x60;
f7ce3cc6 333 i2c_master_send(c, buffer, 4);
586b0cab
MCC
334 mdelay(1);
335 buffer[2] = 0x86;
336 buffer[3] = 0x54;
f7ce3cc6 337 i2c_master_send(c, buffer, 4);
a07c8779
MK
338 if (!dvb_attach(simple_tuner_attach, &t->fe,
339 t->i2c->adapter, t->i2c->addr, t->type))
b9ef6bbb 340 goto attach_failed;
586b0cab 341 break;
93df3413
HH
342 case TUNER_PHILIPS_TD1316:
343 buffer[0] = 0x0b;
344 buffer[1] = 0xdc;
345 buffer[2] = 0x86;
346 buffer[3] = 0xa4;
a07c8779
MK
347 i2c_master_send(c, buffer, 4);
348 if (!dvb_attach(simple_tuner_attach, &t->fe,
349 t->i2c->adapter, t->i2c->addr, t->type))
b9ef6bbb 350 goto attach_failed;
ac272ed7 351 break;
690c544c
MCC
352 case TUNER_XC2028:
353 {
a37b4c9b
ML
354 struct xc2028_config cfg = {
355 .i2c_adap = t->i2c->adapter,
356 .i2c_addr = t->i2c->addr,
a37b4c9b 357 };
a07c8779 358 if (!dvb_attach(xc2028_attach, &t->fe, &cfg))
b9ef6bbb 359 goto attach_failed;
d6eef494 360 tune_now = 0;
690c544c
MCC
361 break;
362 }
15396236 363 case TUNER_TDA9887:
09fee5f8
MCC
364 if (!dvb_attach(tda9887_attach,
365 &t->fe, t->i2c->adapter, t->i2c->addr))
366 goto attach_failed;
15396236 367 break;
27c685a4 368 case TUNER_XC5000:
b9ef6bbb 369 {
900f734b
MCC
370 struct xc5000_config xc5000_cfg = {
371 .i2c_address = t->i2c->addr,
372 /* if_khz will be set at dvb_attach() */
373 .if_khz = 0,
374 };
375
a07c8779 376 if (!dvb_attach(xc5000_attach,
30650961 377 &t->fe, t->i2c->adapter, &xc5000_cfg))
b9ef6bbb 378 goto attach_failed;
d6eef494 379 tune_now = 0;
27c685a4 380 break;
b9ef6bbb 381 }
93463895
MK
382 case TUNER_NXP_TDA18271:
383 {
384 struct tda18271_config cfg = {
385 .config = t->config,
e350d44f 386 .small_i2c = TDA18271_03_BYTE_CHUNK_INIT,
93463895
MK
387 };
388
9f3f71ef
MCC
389 if (!dvb_attach(tda18271_attach, &t->fe, t->i2c->addr,
390 t->i2c->adapter, &cfg))
391 goto attach_failed;
392 tune_now = 0;
393 break;
394 }
8d009a0c
DF
395 case TUNER_XC4000:
396 {
397 struct xc4000_config xc4000_cfg = {
398 .i2c_address = t->i2c->addr,
8edeb6eb 399 /* FIXME: the correct parameters will be set */
400 /* only when the digital dvb_attach() occurs */
401 .default_pm = 0,
402 .dvb_amplitude = 0,
403 .set_smoothedcvbs = 0,
404 .if_khz = 0
8d009a0c
DF
405 };
406 if (!dvb_attach(xc4000_attach,
407 &t->fe, t->i2c->adapter, &xc4000_cfg))
408 goto attach_failed;
409 tune_now = 0;
410 break;
411 }
9f3f71ef
MCC
412 default:
413 if (!dvb_attach(simple_tuner_attach, &t->fe,
414 t->i2c->adapter, t->i2c->addr, t->type))
415 goto attach_failed;
416
417 break;
418 }
419
420 if ((NULL == analog_ops->set_params) &&
421 (fe_tuner_ops->set_analog_params)) {
422
423 t->name = fe_tuner_ops->info.name;
424
425 t->fe.analog_demod_priv = t;
426 memcpy(analog_ops, &tuner_analog_ops,
427 sizeof(struct analog_demod_ops));
428
429 } else {
430 t->name = analog_ops->info.name;
431 }
432
433 tuner_dbg("type set to %s\n", t->name);
434
cbde6898 435 t->mode_mask = new_mode_mask;
9f3f71ef
MCC
436
437 /* Some tuners require more initialization setup before use,
438 such as firmware download or device calibration.
439 trying to set a frequency here will just fail
440 FIXME: better to move set_freq to the tuner code. This is needed
441 on analog tuners for PLL to properly work
442 */
443 if (tune_now) {
444 if (V4L2_TUNER_RADIO == t->mode)
445 set_radio_freq(c, t->radio_freq);
446 else
447 set_tv_freq(c, t->tv_freq);
448 }
449
450 tuner_dbg("%s %s I2C addr 0x%02x with type %d used for 0x%02x\n",
451 c->adapter->name, c->driver->driver.name, c->addr << 1, type,
452 t->mode_mask);
453 return;
454
455attach_failed:
456 tuner_dbg("Tuner attach for type = %d failed.\n", t->type);
457 t->type = TUNER_ABSENT;
9f3f71ef
MCC
458
459 return;
460}
461
0ae79d99
MCC
462/**
463 * tuner_s_type_addr - Sets the tuner type for a device
464 *
465 * @sd: subdev descriptor
466 * @tun_setup: type to be associated to a given tuner i2c address
467 *
468 * This function applys the tuner config to tuner specified
469 * by tun_setup structure.
470 * If tuner I2C address is UNSET, then it will only set the device
471 * if the tuner supports the mode specified in the call.
472 * If the address is specified, the change will be applied only if
473 * tuner I2C address matches.
474 * The call can change the tuner number and the tuner mode.
475 */
a34ec5f3
MCC
476static int tuner_s_type_addr(struct v4l2_subdev *sd,
477 struct tuner_setup *tun_setup)
9f3f71ef 478{
a34ec5f3
MCC
479 struct tuner *t = to_tuner(sd);
480 struct i2c_client *c = v4l2_get_subdevdata(sd);
481
482 tuner_dbg("Calling set_type_addr for type=%d, addr=0x%02x, mode=0x%02x, config=0x%02x\n",
483 tun_setup->type,
484 tun_setup->addr,
485 tun_setup->mode_mask,
486 tun_setup->config);
9f3f71ef 487
7d275bf8
MCC
488 if ((t->type == UNSET && ((tun_setup->addr == ADDR_UNSET) &&
489 (t->mode_mask & tun_setup->mode_mask))) ||
490 (tun_setup->addr == c->addr)) {
cbde6898
MCC
491 set_type(c, tun_setup->type, tun_setup->mode_mask,
492 tun_setup->config, tun_setup->tuner_callback);
9f3f71ef
MCC
493 } else
494 tuner_dbg("set addr discarded for type %i, mask %x. "
495 "Asked to change tuner at addr 0x%02x, with mask %x\n",
496 t->type, t->mode_mask,
497 tun_setup->addr, tun_setup->mode_mask);
9f3f71ef 498
9f3f71ef
MCC
499 return 0;
500}
501
a2894e3f
MCC
502/**
503 * tuner_s_config - Sets tuner configuration
504 *
505 * @sd: subdev descriptor
506 * @cfg: tuner configuration
507 *
508 * Calls tuner set_config() private function to set some tuner-internal
509 * parameters
510 */
7d275bf8
MCC
511static int tuner_s_config(struct v4l2_subdev *sd,
512 const struct v4l2_priv_tun_config *cfg)
9f3f71ef
MCC
513{
514 struct tuner *t = to_tuner(sd);
515 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
516
517 if (t->type != cfg->tuner)
518 return 0;
519
520 if (analog_ops->set_config) {
521 analog_ops->set_config(&t->fe, cfg->priv);
522 return 0;
523 }
524
525 tuner_dbg("Tuner frontend module has no way to set config\n");
526 return 0;
527}
528
a2894e3f
MCC
529/**
530 * tuner_lookup - Seek for tuner adapters
531 *
532 * @adap: i2c_adapter struct
533 * @radio: pointer to be filled if the adapter is radio
534 * @tv: pointer to be filled if the adapter is TV
535 *
536 * Search for existing radio and/or TV tuners on the given I2C adapter,
537 * discarding demod-only adapters (tda9887).
538 *
539 * Note that when this function is called from tuner_probe you can be
540 * certain no other devices will be added/deleted at the same time, I2C
541 * core protects against that.
542 */
9f3f71ef
MCC
543static void tuner_lookup(struct i2c_adapter *adap,
544 struct tuner **radio, struct tuner **tv)
545{
546 struct tuner *pos;
547
548 *radio = NULL;
549 *tv = NULL;
550
551 list_for_each_entry(pos, &tuner_list, list) {
552 int mode_mask;
553
554 if (pos->i2c->adapter != adap ||
555 strcmp(pos->i2c->driver->driver.name, "tuner"))
556 continue;
557
cbde6898 558 mode_mask = pos->mode_mask;
9f3f71ef
MCC
559 if (*radio == NULL && mode_mask == T_RADIO)
560 *radio = pos;
561 /* Note: currently TDA9887 is the only demod-only
562 device. If other devices appear then we need to
563 make this test more general. */
564 else if (*tv == NULL && pos->type != TUNER_TDA9887 &&
ad020dc2 565 (pos->mode_mask & T_ANALOG_TV))
9f3f71ef
MCC
566 *tv = pos;
567 }
568}
569
a2894e3f
MCC
570/**
571 *tuner_probe - Probes the existing tuners on an I2C bus
572 *
573 * @client: i2c_client descriptor
574 * @id: not used
575 *
576 * This routine probes for tuners at the expected I2C addresses. On most
577 * cases, if a device answers to a given I2C address, it assumes that the
578 * device is a tuner. On a few cases, however, an additional logic is needed
579 * to double check if the device is really a tuner, or to identify the tuner
580 * type, like on tea5767/5761 devices.
581 *
582 * During client attach, set_type is called by adapter's attach_inform callback.
583 * set_type must then be completed by tuner_probe.
9f3f71ef
MCC
584 */
585static int tuner_probe(struct i2c_client *client,
586 const struct i2c_device_id *id)
587{
588 struct tuner *t;
589 struct tuner *radio;
590 struct tuner *tv;
591
592 t = kzalloc(sizeof(struct tuner), GFP_KERNEL);
593 if (NULL == t)
594 return -ENOMEM;
595 v4l2_i2c_subdev_init(&t->sd, client, &tuner_ops);
596 t->i2c = client;
597 t->name = "(tuner unset)";
598 t->type = UNSET;
599 t->audmode = V4L2_TUNER_MODE_STEREO;
cbde6898
MCC
600 t->standby = 1;
601 t->radio_freq = 87.5 * 16000; /* Initial freq range */
602 t->tv_freq = 400 * 16; /* Sets freq to VHF High - needed for some PLL's to properly start */
9f3f71ef
MCC
603
604 if (show_i2c) {
605 unsigned char buffer[16];
606 int i, rc;
607
608 memset(buffer, 0, sizeof(buffer));
609 rc = i2c_master_recv(client, buffer, sizeof(buffer));
610 tuner_info("I2C RECV = ");
611 for (i = 0; i < rc; i++)
612 printk(KERN_CONT "%02x ", buffer[i]);
613 printk("\n");
614 }
615
616 /* autodetection code based on the i2c addr */
617 if (!no_autodetect) {
618 switch (client->addr) {
619 case 0x10:
620 if (tuner_symbol_probe(tea5761_autodetection,
621 t->i2c->adapter,
622 t->i2c->addr) >= 0) {
623 t->type = TUNER_TEA5761;
624 t->mode_mask = T_RADIO;
9f3f71ef
MCC
625 tuner_lookup(t->i2c->adapter, &radio, &tv);
626 if (tv)
627 tv->mode_mask &= ~T_RADIO;
628
629 goto register_client;
630 }
631 kfree(t);
632 return -ENODEV;
633 case 0x42:
634 case 0x43:
635 case 0x4a:
636 case 0x4b:
637 /* If chip is not tda8290, don't register.
638 since it can be tda9887*/
639 if (tuner_symbol_probe(tda829x_probe, t->i2c->adapter,
640 t->i2c->addr) >= 0) {
641 tuner_dbg("tda829x detected\n");
642 } else {
643 /* Default is being tda9887 */
644 t->type = TUNER_TDA9887;
ad020dc2 645 t->mode_mask = T_RADIO | T_ANALOG_TV;
9f3f71ef
MCC
646 goto register_client;
647 }
648 break;
649 case 0x60:
650 if (tuner_symbol_probe(tea5767_autodetection,
651 t->i2c->adapter, t->i2c->addr)
652 >= 0) {
653 t->type = TUNER_TEA5767;
654 t->mode_mask = T_RADIO;
9f3f71ef 655 /* Sets freq to FM range */
9f3f71ef
MCC
656 tuner_lookup(t->i2c->adapter, &radio, &tv);
657 if (tv)
658 tv->mode_mask &= ~T_RADIO;
659
660 goto register_client;
661 }
662 break;
663 }
93463895 664 }
b9ef6bbb 665
9f3f71ef
MCC
666 /* Initializes only the first TV tuner on this adapter. Why only the
667 first? Because there are some devices (notably the ones with TI
668 tuners) that have more than one i2c address for the *same* device.
669 Experience shows that, except for just one case, the first
670 address is the right one. The exception is a Russian tuner
671 (ACORP_Y878F). So, the desired behavior is just to enable the
672 first found TV tuner. */
673 tuner_lookup(t->i2c->adapter, &radio, &tv);
674 if (tv == NULL) {
ad020dc2 675 t->mode_mask = T_ANALOG_TV;
9f3f71ef
MCC
676 if (radio == NULL)
677 t->mode_mask |= T_RADIO;
678 tuner_dbg("Setting mode_mask to 0x%02x\n", t->mode_mask);
1da177e4 679 }
f7ce3cc6 680
9f3f71ef
MCC
681 /* Should be just before return */
682register_client:
9f3f71ef 683 /* Sets a default mode */
7d275bf8 684 if (t->mode_mask & T_ANALOG_TV)
9f3f71ef 685 t->mode = V4L2_TUNER_ANALOG_TV;
7d275bf8 686 else
ad020dc2 687 t->mode = V4L2_TUNER_RADIO;
9f3f71ef
MCC
688 set_type(client, t->type, t->mode_mask, t->config, t->fe.callback);
689 list_add_tail(&t->list, &tuner_list);
cbde6898 690
ad020dc2 691 tuner_info("Tuner %d found with type(s)%s%s.\n",
cbde6898 692 t->type,
ad020dc2
MCC
693 t->mode_mask & T_RADIO ? " Radio" : "",
694 t->mode_mask & T_ANALOG_TV ? " TV" : "");
9f3f71ef
MCC
695 return 0;
696}
e18f9444 697
a2894e3f
MCC
698/**
699 * tuner_remove - detaches a tuner
700 *
701 * @client: i2c_client descriptor
702 */
703
9f3f71ef
MCC
704static int tuner_remove(struct i2c_client *client)
705{
706 struct tuner *t = to_tuner(i2c_get_clientdata(client));
b9ef6bbb 707
9f3f71ef
MCC
708 v4l2_device_unregister_subdev(&t->sd);
709 tuner_detach(&t->fe);
710 t->fe.analog_demod_priv = NULL;
b9ef6bbb 711
9f3f71ef
MCC
712 list_del(&t->list);
713 kfree(t);
714 return 0;
1da177e4
LT
715}
716
0eec66c0
MCC
717/*
718 * Functions to switch between Radio and TV
719 *
720 * A few cards have a separate I2C tuner for radio. Those routines
721 * take care of switching between TV/Radio mode, filtering only the
722 * commands that apply to the Radio or TV tuner.
723 */
724
725/**
726 * check_mode - Verify if tuner supports the requested mode
727 * @t: a pointer to the module's internal struct_tuner
728 *
729 * This function checks if the tuner is capable of tuning analog TV,
730 * digital TV or radio, depending on what the caller wants. If the
731 * tuner can't support that mode, it returns -EINVAL. Otherwise, it
732 * returns 0.
733 * This function is needed for boards that have a separate tuner for
734 * radio (like devices with tea5767).
a1ad5ec7
MCC
735 * NOTE: mt20xx uses V4L2_TUNER_DIGITAL_TV and calls set_tv_freq to
736 * select a TV frequency. So, t_mode = T_ANALOG_TV could actually
737 * be used to represent a Digital TV too.
0eec66c0
MCC
738 */
739static inline int check_mode(struct tuner *t, enum v4l2_tuner_type mode)
740{
a1ad5ec7
MCC
741 int t_mode;
742 if (mode == V4L2_TUNER_RADIO)
743 t_mode = T_RADIO;
744 else
745 t_mode = T_ANALOG_TV;
746
747 if ((t_mode & t->mode_mask) == 0)
0eec66c0
MCC
748 return -EINVAL;
749
750 return 0;
751}
752
753/**
4e4a31fb 754 * set_mode - Switch tuner to other mode.
0eec66c0
MCC
755 * @t: a pointer to the module's internal struct_tuner
756 * @mode: enum v4l2_type (radio or TV)
0eec66c0
MCC
757 *
758 * If tuner doesn't support the needed mode (radio or TV), prints a
759 * debug message and returns -EINVAL, changing its state to standby.
4e4a31fb 760 * Otherwise, changes the mode and returns 0.
0eec66c0 761 */
4e4a31fb 762static int set_mode(struct tuner *t, enum v4l2_tuner_type mode)
0eec66c0
MCC
763{
764 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
765
766 if (mode != t->mode) {
767 if (check_mode(t, mode) == -EINVAL) {
768 tuner_dbg("Tuner doesn't support mode %d. "
769 "Putting tuner to sleep\n", mode);
770 t->standby = true;
771 if (analog_ops->standby)
772 analog_ops->standby(&t->fe);
773 return -EINVAL;
774 }
775 t->mode = mode;
776 tuner_dbg("Changing to mode %d\n", mode);
777 }
4e4a31fb
HV
778 return 0;
779}
780
781/**
782 * set_freq - Set the tuner to the desired frequency.
783 * @t: a pointer to the module's internal struct_tuner
784 * @freq: frequency to set (0 means to use the current frequency)
785 */
786static void set_freq(struct tuner *t, unsigned int freq)
787{
788 struct i2c_client *client = v4l2_get_subdevdata(&t->sd);
789
0eec66c0 790 if (t->mode == V4L2_TUNER_RADIO) {
4e4a31fb
HV
791 if (!freq)
792 freq = t->radio_freq;
793 set_radio_freq(client, freq);
0eec66c0 794 } else {
4e4a31fb
HV
795 if (!freq)
796 freq = t->tv_freq;
797 set_tv_freq(client, freq);
0eec66c0 798 }
0eec66c0
MCC
799}
800
f7ce3cc6 801/*
9f3f71ef
MCC
802 * Functions that are specific for TV mode
803 */
f7ce3cc6 804
a2894e3f
MCC
805/**
806 * set_tv_freq - Set tuner frequency, freq in Units of 62.5 kHz = 1/16MHz
807 *
808 * @c: i2c_client descriptor
809 * @freq: frequency
810 */
9f3f71ef 811static void set_tv_freq(struct i2c_client *c, unsigned int freq)
f7ce3cc6 812{
e8a4a9e7 813 struct tuner *t = to_tuner(i2c_get_clientdata(c));
9f3f71ef 814 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
f7ce3cc6 815
9f3f71ef
MCC
816 struct analog_parameters params = {
817 .mode = t->mode,
818 .audmode = t->audmode,
819 .std = t->std
820 };
56fc08ca 821
9f3f71ef 822 if (t->type == UNSET) {
7d275bf8 823 tuner_warn("tuner type not set\n");
9f3f71ef 824 return;
793cf9e6 825 }
9f3f71ef 826 if (NULL == analog_ops->set_params) {
7d275bf8 827 tuner_warn("Tuner has no way to set tv freq\n");
9f3f71ef 828 return;
56fc08ca 829 }
9f3f71ef 830 if (freq < tv_range[0] * 16 || freq > tv_range[1] * 16) {
7d275bf8 831 tuner_dbg("TV freq (%d.%02d) out of range (%d-%d)\n",
9f3f71ef
MCC
832 freq / 16, freq % 16 * 100 / 16, tv_range[0],
833 tv_range[1]);
834 /* V4L2 spec: if the freq is not possible then the closest
835 possible value should be selected */
836 if (freq < tv_range[0] * 16)
837 freq = tv_range[0] * 16;
838 else
839 freq = tv_range[1] * 16;
840 }
841 params.frequency = freq;
842 tuner_dbg("tv freq set to %d.%02d\n",
843 freq / 16, freq % 16 * 100 / 16);
844 t->tv_freq = freq;
cbde6898 845 t->standby = false;
9f3f71ef
MCC
846
847 analog_ops->set_params(&t->fe, &params);
56fc08ca 848}
56fc08ca 849
a2894e3f
MCC
850/**
851 * tuner_fixup_std - force a given video standard variant
852 *
48783301
HV
853 * @t: tuner internal struct
854 * @std: TV standard
a2894e3f
MCC
855 *
856 * A few devices or drivers have problem to detect some standard variations.
857 * On other operational systems, the drivers generally have a per-country
858 * code, and some logic to apply per-country hacks. V4L2 API doesn't provide
859 * such hacks. Instead, it relies on a proper video standard selection from
860 * the userspace application. However, as some apps are buggy, not allowing
861 * to distinguish all video standard variations, a modprobe parameter can
862 * be used to force a video standard match.
863 */
48783301 864static v4l2_std_id tuner_fixup_std(struct tuner *t, v4l2_std_id std)
1da177e4 865{
48783301 866 if (pal[0] != '-' && (std & V4L2_STD_PAL) == V4L2_STD_PAL) {
1da177e4 867 switch (pal[0]) {
e71ced1a 868 case '6':
48783301 869 return V4L2_STD_PAL_60;
1da177e4
LT
870 case 'b':
871 case 'B':
872 case 'g':
873 case 'G':
48783301 874 return V4L2_STD_PAL_BG;
1da177e4
LT
875 case 'i':
876 case 'I':
48783301 877 return V4L2_STD_PAL_I;
1da177e4
LT
878 case 'd':
879 case 'D':
880 case 'k':
881 case 'K':
48783301 882 return V4L2_STD_PAL_DK;
f7ce3cc6
MCC
883 case 'M':
884 case 'm':
48783301 885 return V4L2_STD_PAL_M;
f7ce3cc6
MCC
886 case 'N':
887 case 'n':
48783301
HV
888 if (pal[1] == 'c' || pal[1] == 'C')
889 return V4L2_STD_PAL_Nc;
890 return V4L2_STD_PAL_N;
21d4df37 891 default:
7d275bf8 892 tuner_warn("pal= argument not recognised\n");
21d4df37 893 break;
1da177e4
LT
894 }
895 }
48783301 896 if (secam[0] != '-' && (std & V4L2_STD_SECAM) == V4L2_STD_SECAM) {
f7ce3cc6 897 switch (secam[0]) {
7e578191
MCC
898 case 'b':
899 case 'B':
900 case 'g':
901 case 'G':
902 case 'h':
903 case 'H':
48783301
HV
904 return V4L2_STD_SECAM_B |
905 V4L2_STD_SECAM_G |
906 V4L2_STD_SECAM_H;
f7ce3cc6
MCC
907 case 'd':
908 case 'D':
909 case 'k':
910 case 'K':
48783301 911 return V4L2_STD_SECAM_DK;
f7ce3cc6
MCC
912 case 'l':
913 case 'L':
48783301
HV
914 if ((secam[1] == 'C') || (secam[1] == 'c'))
915 return V4L2_STD_SECAM_LC;
916 return V4L2_STD_SECAM_L;
21d4df37 917 default:
7d275bf8 918 tuner_warn("secam= argument not recognised\n");
21d4df37 919 break;
f7ce3cc6
MCC
920 }
921 }
922
48783301 923 if (ntsc[0] != '-' && (std & V4L2_STD_NTSC) == V4L2_STD_NTSC) {
7e578191
MCC
924 switch (ntsc[0]) {
925 case 'm':
926 case 'M':
48783301 927 return V4L2_STD_NTSC_M;
7e578191
MCC
928 case 'j':
929 case 'J':
48783301 930 return V4L2_STD_NTSC_M_JP;
d97a11e0
HV
931 case 'k':
932 case 'K':
48783301 933 return V4L2_STD_NTSC_M_KR;
7e578191
MCC
934 default:
935 tuner_info("ntsc= argument not recognised\n");
936 break;
937 }
938 }
48783301 939 return std;
9f3f71ef
MCC
940}
941
942/*
943 * Functions that are specific for Radio mode
944 */
945
a2894e3f
MCC
946/**
947 * set_radio_freq - Set tuner frequency, freq in Units of 62.5 Hz = 1/16kHz
948 *
949 * @c: i2c_client descriptor
950 * @freq: frequency
951 */
9f3f71ef
MCC
952static void set_radio_freq(struct i2c_client *c, unsigned int freq)
953{
954 struct tuner *t = to_tuner(i2c_get_clientdata(c));
955 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
956
957 struct analog_parameters params = {
958 .mode = t->mode,
959 .audmode = t->audmode,
960 .std = t->std
961 };
962
963 if (t->type == UNSET) {
7d275bf8 964 tuner_warn("tuner type not set\n");
9f3f71ef
MCC
965 return;
966 }
967 if (NULL == analog_ops->set_params) {
7d275bf8 968 tuner_warn("tuner has no way to set radio frequency\n");
9f3f71ef
MCC
969 return;
970 }
971 if (freq < radio_range[0] * 16000 || freq > radio_range[1] * 16000) {
7d275bf8 972 tuner_dbg("radio freq (%d.%02d) out of range (%d-%d)\n",
9f3f71ef
MCC
973 freq / 16000, freq % 16000 * 100 / 16000,
974 radio_range[0], radio_range[1]);
975 /* V4L2 spec: if the freq is not possible then the closest
976 possible value should be selected */
977 if (freq < radio_range[0] * 16000)
978 freq = radio_range[0] * 16000;
979 else
980 freq = radio_range[1] * 16000;
981 }
982 params.frequency = freq;
983 tuner_dbg("radio freq set to %d.%02d\n",
984 freq / 16000, freq % 16000 * 100 / 16000);
985 t->radio_freq = freq;
cbde6898 986 t->standby = false;
9f3f71ef
MCC
987
988 analog_ops->set_params(&t->fe, &params);
989}
990
0eec66c0
MCC
991/*
992 * Debug function for reporting tuner status to userspace
9f3f71ef 993 */
9f3f71ef 994
cbde6898
MCC
995/**
996 * tuner_status - Dumps the current tuner status at dmesg
997 * @fe: pointer to struct dvb_frontend
998 *
999 * This callback is used only for driver debug purposes, answering to
1000 * VIDIOC_LOG_STATUS. No changes should happen on this call.
1001 */
4e9154b8 1002static void tuner_status(struct dvb_frontend *fe)
7e578191 1003{
4e9154b8 1004 struct tuner *t = fe->analog_demod_priv;
7e578191 1005 unsigned long freq, freq_fraction;
a07c8779
MK
1006 struct dvb_tuner_ops *fe_tuner_ops = &fe->ops.tuner_ops;
1007 struct analog_demod_ops *analog_ops = &fe->ops.analog_ops;
7e578191
MCC
1008 const char *p;
1009
1010 switch (t->mode) {
7d275bf8
MCC
1011 case V4L2_TUNER_RADIO:
1012 p = "radio";
1013 break;
a1ad5ec7 1014 case V4L2_TUNER_DIGITAL_TV: /* Used by mt20xx */
7d275bf8
MCC
1015 p = "digital TV";
1016 break;
1017 case V4L2_TUNER_ANALOG_TV:
1018 default:
1019 p = "analog TV";
1020 break;
7e578191
MCC
1021 }
1022 if (t->mode == V4L2_TUNER_RADIO) {
27487d44
HV
1023 freq = t->radio_freq / 16000;
1024 freq_fraction = (t->radio_freq % 16000) * 100 / 16000;
7e578191 1025 } else {
27487d44
HV
1026 freq = t->tv_freq / 16;
1027 freq_fraction = (t->tv_freq % 16) * 100 / 16;
7e578191 1028 }
cbde6898
MCC
1029 tuner_info("Tuner mode: %s%s\n", p,
1030 t->standby ? " on standby mode" : "");
7e578191 1031 tuner_info("Frequency: %lu.%02lu MHz\n", freq, freq_fraction);
4ae5c2e5 1032 tuner_info("Standard: 0x%08lx\n", (unsigned long)t->std);
8a4b275f 1033 if (t->mode != V4L2_TUNER_RADIO)
7d275bf8 1034 return;
e18f9444
MK
1035 if (fe_tuner_ops->get_status) {
1036 u32 tuner_status;
1037
1038 fe_tuner_ops->get_status(&t->fe, &tuner_status);
1039 if (tuner_status & TUNER_STATUS_LOCKED)
1040 tuner_info("Tuner is locked.\n");
1041 if (tuner_status & TUNER_STATUS_STEREO)
1042 tuner_info("Stereo: yes\n");
1043 }
bc3e5c7f
MK
1044 if (analog_ops->has_signal)
1045 tuner_info("Signal strength: %d\n",
1046 analog_ops->has_signal(fe));
7e578191 1047}
8a4b275f 1048
0eec66c0
MCC
1049/*
1050 * Function to splicitly change mode to radio. Probably not needed anymore
1051 */
1052
1053static int tuner_s_radio(struct v4l2_subdev *sd)
1054{
1055 struct tuner *t = to_tuner(sd);
0eec66c0 1056
4e4a31fb
HV
1057 if (set_mode(t, V4L2_TUNER_RADIO) == 0)
1058 set_freq(t, 0);
0eec66c0
MCC
1059 return 0;
1060}
1061
1062/*
1063 * Tuner callbacks to handle userspace ioctl's
1064 */
1065
cbde6898
MCC
1066/**
1067 * tuner_s_power - controls the power state of the tuner
1068 * @sd: pointer to struct v4l2_subdev
d16625e7 1069 * @on: a zero value puts the tuner to sleep, non-zero wakes it up
cbde6898 1070 */
622b828a 1071static int tuner_s_power(struct v4l2_subdev *sd, int on)
e8a4a9e7
HV
1072{
1073 struct tuner *t = to_tuner(sd);
bc3e5c7f 1074 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
1da177e4 1075
d16625e7
HV
1076 if (on) {
1077 if (t->standby && set_mode(t, t->mode) == 0) {
1078 tuner_dbg("Waking up tuner\n");
1079 set_freq(t, 0);
1080 }
622b828a 1081 return 0;
d16625e7 1082 }
622b828a 1083
16a5e53d 1084 tuner_dbg("Putting tuner to sleep\n");
cbde6898 1085 t->standby = true;
e8a4a9e7
HV
1086 if (analog_ops->standby)
1087 analog_ops->standby(&t->fe);
1088 return 0;
1089}
5e453dc7 1090
e8a4a9e7
HV
1091static int tuner_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
1092{
1093 struct tuner *t = to_tuner(sd);
f7ce3cc6 1094
4e4a31fb 1095 if (set_mode(t, V4L2_TUNER_ANALOG_TV))
e8a4a9e7 1096 return 0;
f7ce3cc6 1097
48783301
HV
1098 t->std = tuner_fixup_std(t, std);
1099 if (t->std != std)
1100 tuner_dbg("Fixup standard %llx to %llx\n", std, t->std);
4e4a31fb 1101 set_freq(t, 0);
e8a4a9e7
HV
1102 return 0;
1103}
f7ce3cc6 1104
e8a4a9e7
HV
1105static int tuner_s_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f)
1106{
1107 struct tuner *t = to_tuner(sd);
8a4b275f 1108
4e4a31fb
HV
1109 if (set_mode(t, f->type) == 0)
1110 set_freq(t, f->frequency);
e8a4a9e7
HV
1111 return 0;
1112}
f7ce3cc6 1113
338e9e1a
HV
1114/**
1115 * tuner_g_frequency - Get the tuned frequency for the tuner
1116 * @sd: pointer to struct v4l2_subdev
1117 * @f: pointer to struct v4l2_frequency
1118 *
1119 * At return, the structure f will be filled with tuner frequency
1120 * if the tuner matches the f->type.
1121 * Note: f->type should be initialized before calling it.
1122 * This is done by either video_ioctl2 or by the bridge driver.
1123 */
e8a4a9e7
HV
1124static int tuner_g_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f)
1125{
1126 struct tuner *t = to_tuner(sd);
1127 struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops;
f7ce3cc6 1128
cbde6898 1129 if (check_mode(t, f->type) == -EINVAL)
e8a4a9e7 1130 return 0;
5ad339a2 1131 if (f->type == t->mode && fe_tuner_ops->get_frequency && !t->standby) {
e8a4a9e7
HV
1132 u32 abs_freq;
1133
1134 fe_tuner_ops->get_frequency(&t->fe, &abs_freq);
1135 f->frequency = (V4L2_TUNER_RADIO == t->mode) ?
75b697f7
JL
1136 DIV_ROUND_CLOSEST(abs_freq * 2, 125) :
1137 DIV_ROUND_CLOSEST(abs_freq, 62500);
cbde6898 1138 } else {
5ad339a2 1139 f->frequency = (V4L2_TUNER_RADIO == f->type) ?
cbde6898 1140 t->radio_freq : t->tv_freq;
e8a4a9e7 1141 }
e8a4a9e7
HV
1142 return 0;
1143}
f7ce3cc6 1144
338e9e1a
HV
1145/**
1146 * tuner_g_tuner - Fill in tuner information
1147 * @sd: pointer to struct v4l2_subdev
1148 * @vt: pointer to struct v4l2_tuner
1149 *
1150 * At return, the structure vt will be filled with tuner information
1151 * if the tuner matches vt->type.
1152 * Note: vt->type should be initialized before calling it.
1153 * This is done by either video_ioctl2 or by the bridge driver.
1154 */
e8a4a9e7
HV
1155static int tuner_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
1156{
1157 struct tuner *t = to_tuner(sd);
1158 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
1159 struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops;
1160
cbde6898 1161 if (check_mode(t, vt->type) == -EINVAL)
e8a4a9e7 1162 return 0;
5ad339a2 1163 if (vt->type == t->mode && analog_ops->get_afc)
e8a4a9e7 1164 vt->afc = analog_ops->get_afc(&t->fe);
a1ad5ec7 1165 if (t->mode != V4L2_TUNER_RADIO) {
e8a4a9e7 1166 vt->capability |= V4L2_TUNER_CAP_NORM;
e8a4a9e7
HV
1167 vt->rangelow = tv_range[0] * 16;
1168 vt->rangehigh = tv_range[1] * 16;
1169 return 0;
1170 }
1171
1172 /* radio mode */
5ad339a2
HV
1173 if (vt->type == t->mode) {
1174 vt->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
1175 if (fe_tuner_ops->get_status) {
1176 u32 tuner_status;
1177
1178 fe_tuner_ops->get_status(&t->fe, &tuner_status);
1179 vt->rxsubchans =
1180 (tuner_status & TUNER_STATUS_STEREO) ?
1181 V4L2_TUNER_SUB_STEREO :
1182 V4L2_TUNER_SUB_MONO;
1183 }
1184 if (analog_ops->has_signal)
1185 vt->signal = analog_ops->has_signal(&t->fe);
1186 vt->audmode = t->audmode;
1da177e4 1187 }
cbde6898 1188 vt->capability |= V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
e8a4a9e7
HV
1189 vt->rangelow = radio_range[0] * 16000;
1190 vt->rangehigh = radio_range[1] * 16000;
cbde6898 1191
e8a4a9e7
HV
1192 return 0;
1193}
1194
338e9e1a
HV
1195/**
1196 * tuner_s_tuner - Set the tuner's audio mode
1197 * @sd: pointer to struct v4l2_subdev
1198 * @vt: pointer to struct v4l2_tuner
1199 *
1200 * Sets the audio mode if the tuner matches vt->type.
1201 * Note: vt->type should be initialized before calling it.
1202 * This is done by either video_ioctl2 or by the bridge driver.
1203 */
e8a4a9e7
HV
1204static int tuner_s_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
1205{
1206 struct tuner *t = to_tuner(sd);
e8a4a9e7 1207
4e4a31fb 1208 if (set_mode(t, vt->type))
e8a4a9e7
HV
1209 return 0;
1210
cbde6898
MCC
1211 if (t->mode == V4L2_TUNER_RADIO)
1212 t->audmode = vt->audmode;
4e4a31fb 1213 set_freq(t, 0);
cbde6898 1214
e8a4a9e7
HV
1215 return 0;
1216}
1217
1218static int tuner_log_status(struct v4l2_subdev *sd)
1219{
1220 struct tuner *t = to_tuner(sd);
1221 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
1da177e4 1222
e8a4a9e7
HV
1223 if (analog_ops->tuner_status)
1224 analog_ops->tuner_status(&t->fe);
1da177e4
LT
1225 return 0;
1226}
1227
21b48a70 1228static int tuner_suspend(struct i2c_client *c, pm_message_t state)
1da177e4 1229{
e8a4a9e7 1230 struct tuner *t = to_tuner(i2c_get_clientdata(c));
e2f63d9b 1231 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
1da177e4 1232
9dd659de 1233 tuner_dbg("suspend\n");
e2f63d9b
MCC
1234
1235 if (!t->standby && analog_ops->standby)
1236 analog_ops->standby(&t->fe);
1237
1da177e4
LT
1238 return 0;
1239}
1240
21b48a70 1241static int tuner_resume(struct i2c_client *c)
1da177e4 1242{
e8a4a9e7 1243 struct tuner *t = to_tuner(i2c_get_clientdata(c));
1da177e4 1244
9dd659de 1245 tuner_dbg("resume\n");
e2f63d9b
MCC
1246
1247 if (!t->standby)
9bf0ef06 1248 if (set_mode(t, t->mode) == 0)
4e4a31fb 1249 set_freq(t, 0);
e2f63d9b 1250
1da177e4
LT
1251 return 0;
1252}
1253
75b4c260
HV
1254static int tuner_command(struct i2c_client *client, unsigned cmd, void *arg)
1255{
1256 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1257
1258 /* TUNER_SET_CONFIG is still called by tuner-simple.c, so we have
1259 to handle it here.
1260 There must be a better way of doing this... */
1261 switch (cmd) {
1262 case TUNER_SET_CONFIG:
1263 return tuner_s_config(sd, arg);
1264 }
1265 return -ENOIOCTLCMD;
1266}
1267
a2894e3f
MCC
1268/*
1269 * Callback structs
1270 */
e8a4a9e7
HV
1271
1272static const struct v4l2_subdev_core_ops tuner_core_ops = {
1273 .log_status = tuner_log_status,
f41737ec 1274 .s_std = tuner_s_std,
622b828a 1275 .s_power = tuner_s_power,
e8a4a9e7
HV
1276};
1277
1278static const struct v4l2_subdev_tuner_ops tuner_tuner_ops = {
e8a4a9e7
HV
1279 .s_radio = tuner_s_radio,
1280 .g_tuner = tuner_g_tuner,
1281 .s_tuner = tuner_s_tuner,
1282 .s_frequency = tuner_s_frequency,
1283 .g_frequency = tuner_g_frequency,
1284 .s_type_addr = tuner_s_type_addr,
1285 .s_config = tuner_s_config,
1286};
1287
1288static const struct v4l2_subdev_ops tuner_ops = {
1289 .core = &tuner_core_ops,
1290 .tuner = &tuner_tuner_ops,
1291};
1292
a2894e3f
MCC
1293/*
1294 * I2C structs and module init functions
1295 */
1da177e4 1296
af294867
JD
1297static const struct i2c_device_id tuner_id[] = {
1298 { "tuner", }, /* autodetect */
1299 { }
1300};
1301MODULE_DEVICE_TABLE(i2c, tuner_id);
1302
02a2098a
HV
1303static struct i2c_driver tuner_driver = {
1304 .driver = {
1305 .owner = THIS_MODULE,
1306 .name = "tuner",
1307 },
1308 .probe = tuner_probe,
1309 .remove = tuner_remove,
1310 .command = tuner_command,
1311 .suspend = tuner_suspend,
1312 .resume = tuner_resume,
1313 .id_table = tuner_id,
1da177e4 1314};
1da177e4 1315
02a2098a
HV
1316static __init int init_tuner(void)
1317{
1318 return i2c_add_driver(&tuner_driver);
1319}
1320
1321static __exit void exit_tuner(void)
1322{
1323 i2c_del_driver(&tuner_driver);
1324}
1325
1326module_init(init_tuner);
1327module_exit(exit_tuner);
1328
9f3f71ef
MCC
1329MODULE_DESCRIPTION("device driver for various TV and TV+FM radio tuners");
1330MODULE_AUTHOR("Ralph Metzler, Gerd Knorr, Gunther Mayer");
1331MODULE_LICENSE("GPL");
This page took 1.195925 seconds and 5 git commands to generate.