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