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