blob: 11cc980b0cd5e7113da25b6b39297de8e33038da [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * i2c tv tuner chip device driver
3 * core core, i.e. kernel interfaces, registering and so on
Mauro Carvalho Chehaba2894e32011-02-15 10:15:19 -02004 *
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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 */
16
17#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/init.h>
Hans Verkuil75b4c262009-04-01 03:32:22 -030028#include <linux/videodev2.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <media/tuner.h>
Michael Krufky4adad282007-08-27 21:59:08 -030030#include <media/tuner-types.h>
Hans Verkuile8a4a9e2008-11-24 18:21:40 -030031#include <media/v4l2-device.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030032#include <media/v4l2-ioctl.h>
Michael Krufky96c0b7c2007-08-27 21:23:08 -030033#include "mt20xx.h"
Michael Krufky910bb3e2007-08-27 21:22:20 -030034#include "tda8290.h"
Michael Krufky7ab10bf2007-08-27 21:23:40 -030035#include "tea5761.h"
Michael Krufky8d0936e2007-08-27 21:24:27 -030036#include "tea5767.h"
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -030037#include "tuner-xc2028.h"
Michael Krufky4adad282007-08-27 21:59:08 -030038#include "tuner-simple.h"
Michael Krufky31c95842007-10-21 20:48:48 -030039#include "tda9887.h"
Steven Toth27c685a2008-01-05 16:50:14 -030040#include "xc5000.h"
Michael Krufky93463892009-09-15 23:04:18 -030041#include "tda18271.h"
Davide Ferri8d009a02009-06-23 22:34:06 -030042#include "xc4000.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44#define UNSET (-1U)
45
Mauro Carvalho Chehab7d275bf2011-02-04 11:28:00 -030046#define PREFIX (t->i2c->driver->driver.name)
Michael Krufky241020d2007-10-30 09:46:10 -030047
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -030048/*
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
Mauro Carvalho Chehab7d275bf2011-02-04 11:28:00 -030069module_param_named(debug, tuner_debug, int, 0644);
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -030070module_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
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -030080static LIST_HEAD(tuner_list);
Mauro Carvalho Chehaba2894e32011-02-15 10:15:19 -020081static const struct v4l2_subdev_ops tuner_ops;
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -030082
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
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -0300129 enum v4l2_tuner_type mode;
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300130 unsigned int mode_mask; /* Combination of allowable modes */
131
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -0300132 bool standby; /* Standby mode */
133
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300134 unsigned int type; /* chip type id */
135 unsigned int config;
136 const char *name;
137};
138
139/*
Mauro Carvalho Chehaba2894e32011-02-15 10:15:19 -0200140 * 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
146/*
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300147 * tuner attach/detach logic
148 */
149
Mauro Carvalho Chehab0ae79d92011-02-15 01:10:20 -0300150/* This macro allows us to probe dynamically, avoiding static links */
Mauro Carvalho Chehabff138172008-04-29 23:02:33 -0300151#ifdef CONFIG_MEDIA_ATTACH
Michael Krufkya07c8772008-04-29 03:54:19 -0300152#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); \
Andrew Mortona1355e52008-04-30 11:40:17 -0300157 symbol_put(FUNCTION); \
Michael Krufkya07c8772008-04-29 03:54:19 -0300158 } else { \
159 printk(KERN_ERR "TUNER: Unable to find " \
160 "symbol "#FUNCTION"()\n"); \
161 } \
Michael Krufkya07c8772008-04-29 03:54:19 -0300162 __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
Michael Krufkyf7f427e2007-12-16 22:02:26 -0300190
Hans Verkuile8a4a9e2008-11-24 18:21:40 -0300191static inline struct tuner *to_tuner(struct v4l2_subdev *sd)
192{
193 return container_of(sd, struct tuner, sd);
194}
195
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300196/*
197 * struct analog_demod_ops callbacks
198 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Michael Krufkyc7919d52007-12-08 17:06:30 -0300200static void fe_set_params(struct dvb_frontend *fe,
201 struct analog_parameters *params)
Michael Krufkye18f9442007-08-21 01:25:48 -0300202{
Michael Krufky4e9154b2007-10-21 19:39:50 -0300203 struct dvb_tuner_ops *fe_tuner_ops = &fe->ops.tuner_ops;
204 struct tuner *t = fe->analog_demod_priv;
Michael Krufkye18f9442007-08-21 01:25:48 -0300205
Michael Krufkye18f9442007-08-21 01:25:48 -0300206 if (NULL == fe_tuner_ops->set_analog_params) {
207 tuner_warn("Tuner frontend module has no way to set freq\n");
208 return;
209 }
Michael Krufkyc7919d52007-12-08 17:06:30 -0300210 fe_tuner_ops->set_analog_params(fe, params);
Michael Krufkye18f9442007-08-21 01:25:48 -0300211}
212
Michael Krufky4e9154b2007-10-21 19:39:50 -0300213static void fe_standby(struct dvb_frontend *fe)
Michael Krufkye18f9442007-08-21 01:25:48 -0300214{
Michael Krufky4e9154b2007-10-21 19:39:50 -0300215 struct dvb_tuner_ops *fe_tuner_ops = &fe->ops.tuner_ops;
Michael Krufkye18f9442007-08-21 01:25:48 -0300216
217 if (fe_tuner_ops->sleep)
Michael Krufky4e9154b2007-10-21 19:39:50 -0300218 fe_tuner_ops->sleep(fe);
Michael Krufkye18f9442007-08-21 01:25:48 -0300219}
220
Michael Krufky4e9154b2007-10-21 19:39:50 -0300221static int fe_has_signal(struct dvb_frontend *fe)
Michael Krufky1f5ef192007-08-31 17:38:02 -0300222{
Michael Krufky14196832007-10-14 18:11:53 -0300223 u16 strength = 0;
Michael Krufky1f5ef192007-08-31 17:38:02 -0300224
Michael Krufky4e9154b2007-10-21 19:39:50 -0300225 if (fe->ops.tuner_ops.get_rf_strength)
226 fe->ops.tuner_ops.get_rf_strength(fe, &strength);
Michael Krufky1f5ef192007-08-31 17:38:02 -0300227
228 return strength;
229}
230
Michael Krufkyf1c9a282007-12-16 19:27:23 -0300231static 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
Michael Krufky4e9154b2007-10-21 19:39:50 -0300244static void tuner_status(struct dvb_frontend *fe);
Michael Krufky1dde7a42007-10-21 13:40:56 -0300245
Hans Verkuile8a4a9e2008-11-24 18:21:40 -0300246static struct analog_demod_ops tuner_analog_ops = {
Michael Krufkyc7919d52007-12-08 17:06:30 -0300247 .set_params = fe_set_params,
Michael Krufky1dde7a42007-10-21 13:40:56 -0300248 .standby = fe_standby,
Michael Krufky1dde7a42007-10-21 13:40:56 -0300249 .has_signal = fe_has_signal,
Michael Krufkyf1c9a282007-12-16 19:27:23 -0300250 .set_config = fe_set_config,
Michael Krufky1dde7a42007-10-21 13:40:56 -0300251 .tuner_status = tuner_status
252};
253
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300254/*
Mauro Carvalho Chehab0eec66c2011-02-15 10:27:03 -0200255 * Functions to select between radio and TV and tuner probe/remove functions
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300256 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Mauro Carvalho Chehaba2894e32011-02-15 10:15:19 -0200258/**
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 */
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700273static void set_type(struct i2c_client *c, unsigned int type,
Hartmut Hackmannde956c12007-04-27 12:31:12 -0300274 unsigned int new_mode_mask, unsigned int new_config,
Michael Krufkyd7cba042008-09-12 13:31:45 -0300275 int (*tuner_callback) (void *dev, int component, int cmd, int arg))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
Hans Verkuile8a4a9e2008-11-24 18:21:40 -0300277 struct tuner *t = to_tuner(i2c_get_clientdata(c));
Michael Krufkye18f9442007-08-21 01:25:48 -0300278 struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops;
Michael Krufkybc3e5c72007-12-21 11:18:32 -0300279 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700280 unsigned char buffer[4];
Michael Krufkyd6eef492009-10-24 16:42:16 -0300281 int tune_now = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700283 if (type == UNSET || type == TUNER_ABSENT) {
Mauro Carvalho Chehab7d275bf2011-02-04 11:28:00 -0300284 tuner_dbg("tuner 0x%02x: Tuner type absent\n", c->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 return;
286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 t->type = type;
Michael Krufkye7ddcd92009-03-28 15:35:26 -0300289 /* prevent invalid config values */
Roel Kluinf14a2972009-10-23 07:59:42 -0300290 t->config = new_config < 256 ? new_config : 0;
Hartmut Hackmanncfeb8832007-04-27 12:31:17 -0300291 if (tuner_callback != NULL) {
292 tuner_dbg("defining GPIO callback\n");
Michael Krufkyd7cba042008-09-12 13:31:45 -0300293 t->fe.callback = tuner_callback;
Hartmut Hackmanncfeb8832007-04-27 12:31:17 -0300294 }
Hartmut Hackmann80f90fb2007-04-27 12:31:18 -0300295
Michael Krufkyb2083192007-05-29 22:54:06 -0300296 /* discard private data, in case set_type() was previously called */
Michael Krufkya07c8772008-04-29 03:54:19 -0300297 tuner_detach(&t->fe);
298 t->fe.analog_demod_priv = NULL;
Michael Krufkybe2b85a2007-06-04 14:40:27 -0300299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 switch (t->type) {
301 case TUNER_MT2032:
Mauro Carvalho Chehab09fee5f2008-04-30 15:29:57 -0300302 if (!dvb_attach(microtune_attach,
303 &t->fe, t->i2c->adapter, t->i2c->addr))
304 goto attach_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 break;
306 case TUNER_PHILIPS_TDA8290:
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300307 {
Mauro Carvalho Chehab09fee5f2008-04-30 15:29:57 -0300308 struct tda829x_config cfg = {
309 .lna_cfg = t->config,
Mauro Carvalho Chehab09fee5f2008-04-30 15:29:57 -0300310 };
311 if (!dvb_attach(tda829x_attach, &t->fe, t->i2c->adapter,
312 t->i2c->addr, &cfg))
313 goto attach_failed;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300314 break;
315 }
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700316 case TUNER_TEA5767:
Michael Krufkya07c8772008-04-29 03:54:19 -0300317 if (!dvb_attach(tea5767_attach, &t->fe,
318 t->i2c->adapter, t->i2c->addr))
Mauro Carvalho Chehabb9ef6bb2008-04-26 11:29:34 -0300319 goto attach_failed;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700320 t->mode_mask = T_RADIO;
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700321 break;
Mauro Carvalho Chehab8573a9e2007-04-08 01:09:11 -0300322 case TUNER_TEA5761:
Michael Krufkya07c8772008-04-29 03:54:19 -0300323 if (!dvb_attach(tea5761_attach, &t->fe,
324 t->i2c->adapter, t->i2c->addr))
Mauro Carvalho Chehabb9ef6bb2008-04-26 11:29:34 -0300325 goto attach_failed;
Mauro Carvalho Chehab8573a9e2007-04-08 01:09:11 -0300326 t->mode_mask = T_RADIO;
327 break;
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700328 case TUNER_PHILIPS_FMD1216ME_MK3:
329 buffer[0] = 0x0b;
330 buffer[1] = 0xdc;
331 buffer[2] = 0x9c;
332 buffer[3] = 0x60;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700333 i2c_master_send(c, buffer, 4);
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700334 mdelay(1);
335 buffer[2] = 0x86;
336 buffer[3] = 0x54;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700337 i2c_master_send(c, buffer, 4);
Michael Krufkya07c8772008-04-29 03:54:19 -0300338 if (!dvb_attach(simple_tuner_attach, &t->fe,
339 t->i2c->adapter, t->i2c->addr, t->type))
Mauro Carvalho Chehabb9ef6bb2008-04-26 11:29:34 -0300340 goto attach_failed;
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700341 break;
Hartmut Hackmann93df3412005-11-08 21:36:31 -0800342 case TUNER_PHILIPS_TD1316:
343 buffer[0] = 0x0b;
344 buffer[1] = 0xdc;
345 buffer[2] = 0x86;
346 buffer[3] = 0xa4;
Michael Krufkya07c8772008-04-29 03:54:19 -0300347 i2c_master_send(c, buffer, 4);
348 if (!dvb_attach(simple_tuner_attach, &t->fe,
349 t->i2c->adapter, t->i2c->addr, t->type))
Mauro Carvalho Chehabb9ef6bb2008-04-26 11:29:34 -0300350 goto attach_failed;
Markus Rechbergerac272ed2006-01-23 17:11:09 -0200351 break;
Mauro Carvalho Chehab690c5442007-10-29 11:33:18 -0300352 case TUNER_XC2028:
353 {
Michel Ludwiga37b4c92007-11-16 07:46:14 -0300354 struct xc2028_config cfg = {
355 .i2c_adap = t->i2c->adapter,
356 .i2c_addr = t->i2c->addr,
Michel Ludwiga37b4c92007-11-16 07:46:14 -0300357 };
Michael Krufkya07c8772008-04-29 03:54:19 -0300358 if (!dvb_attach(xc2028_attach, &t->fe, &cfg))
Mauro Carvalho Chehabb9ef6bb2008-04-26 11:29:34 -0300359 goto attach_failed;
Michael Krufkyd6eef492009-10-24 16:42:16 -0300360 tune_now = 0;
Mauro Carvalho Chehab690c5442007-10-29 11:33:18 -0300361 break;
362 }
Mauro Carvalho Chehab15396232006-06-23 16:13:56 -0300363 case TUNER_TDA9887:
Mauro Carvalho Chehab09fee5f2008-04-30 15:29:57 -0300364 if (!dvb_attach(tda9887_attach,
365 &t->fe, t->i2c->adapter, t->i2c->addr))
366 goto attach_failed;
Mauro Carvalho Chehab15396232006-06-23 16:13:56 -0300367 break;
Steven Toth27c685a2008-01-05 16:50:14 -0300368 case TUNER_XC5000:
Mauro Carvalho Chehabb9ef6bb2008-04-26 11:29:34 -0300369 {
Mauro Carvalho Chehab900f7342011-02-04 12:56:39 -0300370 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
Michael Krufkya07c8772008-04-29 03:54:19 -0300376 if (!dvb_attach(xc5000_attach,
Michael Krufky30650962008-09-06 14:56:58 -0300377 &t->fe, t->i2c->adapter, &xc5000_cfg))
Mauro Carvalho Chehabb9ef6bb2008-04-26 11:29:34 -0300378 goto attach_failed;
Michael Krufkyd6eef492009-10-24 16:42:16 -0300379 tune_now = 0;
Steven Toth27c685a2008-01-05 16:50:14 -0300380 break;
Mauro Carvalho Chehabb9ef6bb2008-04-26 11:29:34 -0300381 }
Michael Krufky93463892009-09-15 23:04:18 -0300382 case TUNER_NXP_TDA18271:
383 {
384 struct tda18271_config cfg = {
385 .config = t->config,
Mauro Carvalho Chehabe350d442010-09-26 22:58:28 -0300386 .small_i2c = TDA18271_03_BYTE_CHUNK_INIT,
Michael Krufky93463892009-09-15 23:04:18 -0300387 };
388
389 if (!dvb_attach(tda18271_attach, &t->fe, t->i2c->addr,
390 t->i2c->adapter, &cfg))
391 goto attach_failed;
Michael Krufkyd6eef492009-10-24 16:42:16 -0300392 tune_now = 0;
Michael Krufky93463892009-09-15 23:04:18 -0300393 break;
394 }
Davide Ferri8d009a02009-06-23 22:34:06 -0300395 case TUNER_XC4000:
396 {
397 struct xc4000_config xc4000_cfg = {
398 .i2c_address = t->i2c->addr,
istvan_v@mailbox.hu8edeb6e2011-06-06 13:03:44 -0300399 /* 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
Davide Ferri8d009a02009-06-23 22:34:06 -0300405 };
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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 default:
Michael Krufkya07c8772008-04-29 03:54:19 -0300413 if (!dvb_attach(simple_tuner_attach, &t->fe,
414 t->i2c->adapter, t->i2c->addr, t->type))
Mauro Carvalho Chehabb9ef6bb2008-04-26 11:29:34 -0300415 goto attach_failed;
416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 break;
418 }
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700419
Michael Krufkybc3e5c72007-12-21 11:18:32 -0300420 if ((NULL == analog_ops->set_params) &&
Michael Krufky1dde7a42007-10-21 13:40:56 -0300421 (fe_tuner_ops->set_analog_params)) {
Michael Krufkya07c8772008-04-29 03:54:19 -0300422
Michael Krufky7271e602008-05-26 16:08:40 +0200423 t->name = fe_tuner_ops->info.name;
Michael Krufkye18f9442007-08-21 01:25:48 -0300424
Michael Krufky4e9154b2007-10-21 19:39:50 -0300425 t->fe.analog_demod_priv = t;
Hans Verkuile8a4a9e2008-11-24 18:21:40 -0300426 memcpy(analog_ops, &tuner_analog_ops,
Michael Krufkybc3e5c72007-12-21 11:18:32 -0300427 sizeof(struct analog_demod_ops));
Michael Krufkya07c8772008-04-29 03:54:19 -0300428
Michael Krufkya55db8c2007-12-09 13:52:51 -0300429 } else {
Michael Krufky7271e602008-05-26 16:08:40 +0200430 t->name = analog_ops->info.name;
Michael Krufkye18f9442007-08-21 01:25:48 -0300431 }
432
Michael Krufky7271e602008-05-26 16:08:40 +0200433 tuner_dbg("type set to %s\n", t->name);
Michael Krufkye18f9442007-08-21 01:25:48 -0300434
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -0300435 t->mode_mask = new_mode_mask;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700436
Michael Krufkyd6eef492009-10-24 16:42:16 -0300437 /* Some tuners require more initialization setup before use,
438 such as firmware download or device calibration.
Mauro Carvalho Chehabb9ef6bb2008-04-26 11:29:34 -0300439 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 */
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300443 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 }
Mauro Carvalho Chehabb9ef6bb2008-04-26 11:29:34 -0300449
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700450 tuner_dbg("%s %s I2C addr 0x%02x with type %d used for 0x%02x\n",
Laurent Riffard604f28e2005-11-26 20:43:39 +0100451 c->adapter->name, c->driver->driver.name, c->addr << 1, type,
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700452 t->mode_mask);
Mauro Carvalho Chehabb9ef6bb2008-04-26 11:29:34 -0300453 return;
454
455attach_failed:
456 tuner_dbg("Tuner attach for type = %d failed.\n", t->type);
457 t->type = TUNER_ABSENT;
Mauro Carvalho Chehabb9ef6bb2008-04-26 11:29:34 -0300458
459 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460}
461
Mauro Carvalho Chehab0ae79d92011-02-15 01:10:20 -0300462/**
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 */
Mauro Carvalho Chehaba34ec5f2011-02-15 00:55:18 -0300476static int tuner_s_type_addr(struct v4l2_subdev *sd,
477 struct tuner_setup *tun_setup)
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700478{
Mauro Carvalho Chehaba34ec5f2011-02-15 00:55:18 -0300479 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);
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700487
Mauro Carvalho Chehab7d275bf2011-02-04 11:28:00 -0300488 if ((t->type == UNSET && ((tun_setup->addr == ADDR_UNSET) &&
489 (t->mode_mask & tun_setup->mode_mask))) ||
490 (tun_setup->addr == c->addr)) {
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -0300491 set_type(c, tun_setup->type, tun_setup->mode_mask,
492 tun_setup->config, tun_setup->tuner_callback);
Mauro Carvalho Chehabb9ef6bb2008-04-26 11:29:34 -0300493 } 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);
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700498
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300499 return 0;
500}
501
Mauro Carvalho Chehaba2894e32011-02-15 10:15:19 -0200502/**
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 */
Mauro Carvalho Chehab7d275bf2011-02-04 11:28:00 -0300511static int tuner_s_config(struct v4l2_subdev *sd,
512 const struct v4l2_priv_tun_config *cfg)
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300513{
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;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700523 }
Mauro Carvalho Chehab793cf9e2005-09-09 13:03:37 -0700524
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300525 tuner_dbg("Tuner frontend module has no way to set config\n");
Mauro Carvalho Chehab793cf9e2005-09-09 13:03:37 -0700526 return 0;
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700527}
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700528
Mauro Carvalho Chehaba2894e32011-02-15 10:15:19 -0200529/**
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 */
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300543static 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
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -0300558 mode_mask = pos->mode_mask;
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300559 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 &&
Mauro Carvalho Chehabad020dc2011-02-15 09:30:50 -0200565 (pos->mode_mask & T_ANALOG_TV))
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300566 *tv = pos;
567 }
568}
569
Mauro Carvalho Chehaba2894e32011-02-15 10:15:19 -0200570/**
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.
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300584 */
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;
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -0300600 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 */
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300603
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;
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300625 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;
Mauro Carvalho Chehabad020dc2011-02-15 09:30:50 -0200645 t->mode_mask = T_RADIO | T_ANALOG_TV;
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300646 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;
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300655 /* Sets freq to FM range */
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300656 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 }
664 }
665
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) {
Mauro Carvalho Chehabad020dc2011-02-15 09:30:50 -0200675 t->mode_mask = T_ANALOG_TV;
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300676 if (radio == NULL)
677 t->mode_mask |= T_RADIO;
678 tuner_dbg("Setting mode_mask to 0x%02x\n", t->mode_mask);
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300679 }
680
681 /* Should be just before return */
682register_client:
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300683 /* Sets a default mode */
Mauro Carvalho Chehab7d275bf2011-02-04 11:28:00 -0300684 if (t->mode_mask & T_ANALOG_TV)
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300685 t->mode = V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehab7d275bf2011-02-04 11:28:00 -0300686 else
Mauro Carvalho Chehabad020dc2011-02-15 09:30:50 -0200687 t->mode = V4L2_TUNER_RADIO;
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300688 set_type(client, t->type, t->mode_mask, t->config, t->fe.callback);
689 list_add_tail(&t->list, &tuner_list);
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -0300690
Mauro Carvalho Chehabad020dc2011-02-15 09:30:50 -0200691 tuner_info("Tuner %d found with type(s)%s%s.\n",
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -0300692 t->type,
Mauro Carvalho Chehabad020dc2011-02-15 09:30:50 -0200693 t->mode_mask & T_RADIO ? " Radio" : "",
694 t->mode_mask & T_ANALOG_TV ? " TV" : "");
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300695 return 0;
696}
697
Mauro Carvalho Chehaba2894e32011-02-15 10:15:19 -0200698/**
699 * tuner_remove - detaches a tuner
700 *
701 * @client: i2c_client descriptor
702 */
703
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300704static int tuner_remove(struct i2c_client *client)
705{
706 struct tuner *t = to_tuner(i2c_get_clientdata(client));
707
708 v4l2_device_unregister_subdev(&t->sd);
709 tuner_detach(&t->fe);
710 t->fe.analog_demod_priv = NULL;
711
712 list_del(&t->list);
713 kfree(t);
714 return 0;
715}
716
717/*
Mauro Carvalho Chehab0eec66c2011-02-15 10:27:03 -0200718 * 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).
Mauro Carvalho Chehaba1ad5ec2011-07-13 01:23:11 -0300735 * 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.
Mauro Carvalho Chehab0eec66c2011-02-15 10:27:03 -0200738 */
739static inline int check_mode(struct tuner *t, enum v4l2_tuner_type mode)
740{
Mauro Carvalho Chehaba1ad5ec2011-07-13 01:23:11 -0300741 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)
Mauro Carvalho Chehab0eec66c2011-02-15 10:27:03 -0200748 return -EINVAL;
749
750 return 0;
751}
752
753/**
Hans Verkuil4e4a31f2011-06-14 03:56:09 -0300754 * set_mode - Switch tuner to other mode.
Mauro Carvalho Chehab0eec66c2011-02-15 10:27:03 -0200755 * @t: a pointer to the module's internal struct_tuner
756 * @mode: enum v4l2_type (radio or TV)
Mauro Carvalho Chehab0eec66c2011-02-15 10:27:03 -0200757 *
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.
Hans Verkuil4e4a31f2011-06-14 03:56:09 -0300760 * Otherwise, changes the mode and returns 0.
Mauro Carvalho Chehab0eec66c2011-02-15 10:27:03 -0200761 */
Hans Verkuil4e4a31f2011-06-14 03:56:09 -0300762static int set_mode(struct tuner *t, enum v4l2_tuner_type mode)
Mauro Carvalho Chehab0eec66c2011-02-15 10:27:03 -0200763{
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 }
Mauro Carvalho Chehab0eec66c2011-02-15 10:27:03 -0200778 return 0;
779}
780
Hans Verkuil4e4a31f2011-06-14 03:56:09 -0300781/**
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
790 if (t->mode == V4L2_TUNER_RADIO) {
791 if (!freq)
792 freq = t->radio_freq;
793 set_radio_freq(client, freq);
794 } else {
795 if (!freq)
796 freq = t->tv_freq;
797 set_tv_freq(client, freq);
798 }
799}
800
Mauro Carvalho Chehab0eec66c2011-02-15 10:27:03 -0200801/*
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300802 * Functions that are specific for TV mode
803 */
804
Mauro Carvalho Chehaba2894e32011-02-15 10:15:19 -0200805/**
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 */
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300811static void set_tv_freq(struct i2c_client *c, unsigned int freq)
812{
813 struct tuner *t = to_tuner(i2c_get_clientdata(c));
814 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
815
816 struct analog_parameters params = {
817 .mode = t->mode,
818 .audmode = t->audmode,
819 .std = t->std
820 };
821
822 if (t->type == UNSET) {
Mauro Carvalho Chehab7d275bf2011-02-04 11:28:00 -0300823 tuner_warn("tuner type not set\n");
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300824 return;
825 }
826 if (NULL == analog_ops->set_params) {
Mauro Carvalho Chehab7d275bf2011-02-04 11:28:00 -0300827 tuner_warn("Tuner has no way to set tv freq\n");
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300828 return;
829 }
830 if (freq < tv_range[0] * 16 || freq > tv_range[1] * 16) {
Mauro Carvalho Chehab7d275bf2011-02-04 11:28:00 -0300831 tuner_dbg("TV freq (%d.%02d) out of range (%d-%d)\n",
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300832 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;
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -0300845 t->standby = false;
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300846
847 analog_ops->set_params(&t->fe, &params);
848}
849
Mauro Carvalho Chehaba2894e32011-02-15 10:15:19 -0200850/**
851 * tuner_fixup_std - force a given video standard variant
852 *
Hans Verkuil48783302011-06-13 09:47:56 -0300853 * @t: tuner internal struct
854 * @std: TV standard
Mauro Carvalho Chehaba2894e32011-02-15 10:15:19 -0200855 *
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 */
Hans Verkuil48783302011-06-13 09:47:56 -0300864static v4l2_std_id tuner_fixup_std(struct tuner *t, v4l2_std_id std)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865{
Hans Verkuil48783302011-06-13 09:47:56 -0300866 if (pal[0] != '-' && (std & V4L2_STD_PAL) == V4L2_STD_PAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 switch (pal[0]) {
Hans Verkuile71ced12006-12-11 15:51:43 -0300868 case '6':
Hans Verkuil48783302011-06-13 09:47:56 -0300869 return V4L2_STD_PAL_60;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 case 'b':
871 case 'B':
872 case 'g':
873 case 'G':
Hans Verkuil48783302011-06-13 09:47:56 -0300874 return V4L2_STD_PAL_BG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 case 'i':
876 case 'I':
Hans Verkuil48783302011-06-13 09:47:56 -0300877 return V4L2_STD_PAL_I;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 case 'd':
879 case 'D':
880 case 'k':
881 case 'K':
Hans Verkuil48783302011-06-13 09:47:56 -0300882 return V4L2_STD_PAL_DK;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700883 case 'M':
884 case 'm':
Hans Verkuil48783302011-06-13 09:47:56 -0300885 return V4L2_STD_PAL_M;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700886 case 'N':
887 case 'n':
Hans Verkuil48783302011-06-13 09:47:56 -0300888 if (pal[1] == 'c' || pal[1] == 'C')
889 return V4L2_STD_PAL_Nc;
890 return V4L2_STD_PAL_N;
Mauro Carvalho Chehab21d4df32005-09-09 13:03:59 -0700891 default:
Mauro Carvalho Chehab7d275bf2011-02-04 11:28:00 -0300892 tuner_warn("pal= argument not recognised\n");
Mauro Carvalho Chehab21d4df32005-09-09 13:03:59 -0700893 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 }
895 }
Hans Verkuil48783302011-06-13 09:47:56 -0300896 if (secam[0] != '-' && (std & V4L2_STD_SECAM) == V4L2_STD_SECAM) {
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700897 switch (secam[0]) {
Mauro Carvalho Chehab7e578192006-01-09 15:25:27 -0200898 case 'b':
899 case 'B':
900 case 'g':
901 case 'G':
902 case 'h':
903 case 'H':
Hans Verkuil48783302011-06-13 09:47:56 -0300904 return V4L2_STD_SECAM_B |
905 V4L2_STD_SECAM_G |
906 V4L2_STD_SECAM_H;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700907 case 'd':
908 case 'D':
909 case 'k':
910 case 'K':
Hans Verkuil48783302011-06-13 09:47:56 -0300911 return V4L2_STD_SECAM_DK;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700912 case 'l':
913 case 'L':
Hans Verkuil48783302011-06-13 09:47:56 -0300914 if ((secam[1] == 'C') || (secam[1] == 'c'))
915 return V4L2_STD_SECAM_LC;
916 return V4L2_STD_SECAM_L;
Mauro Carvalho Chehab21d4df32005-09-09 13:03:59 -0700917 default:
Mauro Carvalho Chehab7d275bf2011-02-04 11:28:00 -0300918 tuner_warn("secam= argument not recognised\n");
Mauro Carvalho Chehab21d4df32005-09-09 13:03:59 -0700919 break;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700920 }
921 }
922
Hans Verkuil48783302011-06-13 09:47:56 -0300923 if (ntsc[0] != '-' && (std & V4L2_STD_NTSC) == V4L2_STD_NTSC) {
Mauro Carvalho Chehab7e578192006-01-09 15:25:27 -0200924 switch (ntsc[0]) {
925 case 'm':
926 case 'M':
Hans Verkuil48783302011-06-13 09:47:56 -0300927 return V4L2_STD_NTSC_M;
Mauro Carvalho Chehab7e578192006-01-09 15:25:27 -0200928 case 'j':
929 case 'J':
Hans Verkuil48783302011-06-13 09:47:56 -0300930 return V4L2_STD_NTSC_M_JP;
Hans Verkuild97a11e2006-02-07 06:48:40 -0200931 case 'k':
932 case 'K':
Hans Verkuil48783302011-06-13 09:47:56 -0300933 return V4L2_STD_NTSC_M_KR;
Mauro Carvalho Chehab7e578192006-01-09 15:25:27 -0200934 default:
935 tuner_info("ntsc= argument not recognised\n");
936 break;
937 }
938 }
Hans Verkuil48783302011-06-13 09:47:56 -0300939 return std;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940}
941
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300942/*
943 * Functions that are specific for Radio mode
944 */
945
Mauro Carvalho Chehaba2894e32011-02-15 10:15:19 -0200946/**
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 */
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300952static 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) {
Mauro Carvalho Chehab7d275bf2011-02-04 11:28:00 -0300964 tuner_warn("tuner type not set\n");
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300965 return;
966 }
967 if (NULL == analog_ops->set_params) {
Mauro Carvalho Chehab7d275bf2011-02-04 11:28:00 -0300968 tuner_warn("tuner has no way to set radio frequency\n");
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300969 return;
970 }
971 if (freq < radio_range[0] * 16000 || freq > radio_range[1] * 16000) {
Mauro Carvalho Chehab7d275bf2011-02-04 11:28:00 -0300972 tuner_dbg("radio freq (%d.%02d) out of range (%d-%d)\n",
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300973 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;
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -0300986 t->standby = false;
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300987
988 analog_ops->set_params(&t->fe, &params);
989}
990
Mauro Carvalho Chehab0eec66c2011-02-15 10:27:03 -0200991/*
992 * Debug function for reporting tuner status to userspace
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300993 */
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300994
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -0300995/**
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 */
Michael Krufky4e9154b2007-10-21 19:39:50 -03001002static void tuner_status(struct dvb_frontend *fe)
Mauro Carvalho Chehab7e578192006-01-09 15:25:27 -02001003{
Michael Krufky4e9154b2007-10-21 19:39:50 -03001004 struct tuner *t = fe->analog_demod_priv;
Mauro Carvalho Chehab7e578192006-01-09 15:25:27 -02001005 unsigned long freq, freq_fraction;
Michael Krufkya07c8772008-04-29 03:54:19 -03001006 struct dvb_tuner_ops *fe_tuner_ops = &fe->ops.tuner_ops;
1007 struct analog_demod_ops *analog_ops = &fe->ops.analog_ops;
Mauro Carvalho Chehab7e578192006-01-09 15:25:27 -02001008 const char *p;
1009
1010 switch (t->mode) {
Mauro Carvalho Chehab7d275bf2011-02-04 11:28:00 -03001011 case V4L2_TUNER_RADIO:
1012 p = "radio";
1013 break;
Mauro Carvalho Chehaba1ad5ec2011-07-13 01:23:11 -03001014 case V4L2_TUNER_DIGITAL_TV: /* Used by mt20xx */
Mauro Carvalho Chehab7d275bf2011-02-04 11:28:00 -03001015 p = "digital TV";
1016 break;
1017 case V4L2_TUNER_ANALOG_TV:
1018 default:
1019 p = "analog TV";
1020 break;
Mauro Carvalho Chehab7e578192006-01-09 15:25:27 -02001021 }
1022 if (t->mode == V4L2_TUNER_RADIO) {
Hans Verkuil27487d42006-01-15 15:04:52 -02001023 freq = t->radio_freq / 16000;
1024 freq_fraction = (t->radio_freq % 16000) * 100 / 16000;
Mauro Carvalho Chehab7e578192006-01-09 15:25:27 -02001025 } else {
Hans Verkuil27487d42006-01-15 15:04:52 -02001026 freq = t->tv_freq / 16;
1027 freq_fraction = (t->tv_freq % 16) * 100 / 16;
Mauro Carvalho Chehab7e578192006-01-09 15:25:27 -02001028 }
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -03001029 tuner_info("Tuner mode: %s%s\n", p,
1030 t->standby ? " on standby mode" : "");
Mauro Carvalho Chehab7e578192006-01-09 15:25:27 -02001031 tuner_info("Frequency: %lu.%02lu MHz\n", freq, freq_fraction);
Mauro Carvalho Chehab4ae5c2e2006-03-25 15:53:38 -03001032 tuner_info("Standard: 0x%08lx\n", (unsigned long)t->std);
Hans Verkuil8a4b2752006-01-23 17:11:09 -02001033 if (t->mode != V4L2_TUNER_RADIO)
Mauro Carvalho Chehab7d275bf2011-02-04 11:28:00 -03001034 return;
Michael Krufkye18f9442007-08-21 01:25:48 -03001035 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 }
Michael Krufkybc3e5c72007-12-21 11:18:32 -03001044 if (analog_ops->has_signal)
1045 tuner_info("Signal strength: %d\n",
1046 analog_ops->has_signal(fe));
Mauro Carvalho Chehab7e578192006-01-09 15:25:27 -02001047}
Hans Verkuil8a4b2752006-01-23 17:11:09 -02001048
Mauro Carvalho Chehab0eec66c2011-02-15 10:27:03 -02001049/*
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);
Mauro Carvalho Chehab0eec66c2011-02-15 10:27:03 -02001056
Hans Verkuil4e4a31f2011-06-14 03:56:09 -03001057 if (set_mode(t, V4L2_TUNER_RADIO) == 0)
1058 set_freq(t, 0);
Mauro Carvalho Chehab0eec66c2011-02-15 10:27:03 -02001059 return 0;
1060}
1061
1062/*
1063 * Tuner callbacks to handle userspace ioctl's
1064 */
1065
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -03001066/**
1067 * tuner_s_power - controls the power state of the tuner
1068 * @sd: pointer to struct v4l2_subdev
Hans Verkuild16625e2011-06-25 10:24:49 -03001069 * @on: a zero value puts the tuner to sleep, non-zero wakes it up
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -03001070 */
Laurent Pinchart622b8282009-10-05 10:48:17 -03001071static int tuner_s_power(struct v4l2_subdev *sd, int on)
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001072{
1073 struct tuner *t = to_tuner(sd);
Michael Krufkybc3e5c72007-12-21 11:18:32 -03001074 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
Hans Verkuild16625e2011-06-25 10:24:49 -03001076 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 }
Laurent Pinchart622b8282009-10-05 10:48:17 -03001081 return 0;
Hans Verkuild16625e2011-06-25 10:24:49 -03001082 }
Laurent Pinchart622b8282009-10-05 10:48:17 -03001083
Mauro Carvalho Chehab16a5e532008-12-20 07:17:10 -03001084 tuner_dbg("Putting tuner to sleep\n");
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -03001085 t->standby = true;
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001086 if (analog_ops->standby)
1087 analog_ops->standby(&t->fe);
1088 return 0;
1089}
1090
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001091static int tuner_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
1092{
1093 struct tuner *t = to_tuner(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
Hans Verkuil4e4a31f2011-06-14 03:56:09 -03001095 if (set_mode(t, V4L2_TUNER_ANALOG_TV))
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001096 return 0;
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -07001097
Hans Verkuil48783302011-06-13 09:47:56 -03001098 t->std = tuner_fixup_std(t, std);
1099 if (t->std != std)
1100 tuner_dbg("Fixup standard %llx to %llx\n", std, t->std);
Hans Verkuil4e4a31f2011-06-14 03:56:09 -03001101 set_freq(t, 0);
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001102 return 0;
1103}
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -07001104
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001105static int tuner_s_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f)
1106{
1107 struct tuner *t = to_tuner(sd);
Michael Krufkye18f9442007-08-21 01:25:48 -03001108
Hans Verkuil4e4a31f2011-06-14 03:56:09 -03001109 if (set_mode(t, f->type) == 0)
1110 set_freq(t, f->frequency);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 return 0;
1112}
1113
Hans Verkuil338e9e12011-06-13 09:35:56 -03001114/**
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 */
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001124static 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;
1128
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -03001129 if (check_mode(t, f->type) == -EINVAL)
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001130 return 0;
Hans Verkuil5ad339a2011-06-26 05:35:34 -03001131 if (f->type == t->mode && fe_tuner_ops->get_frequency && !t->standby) {
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001132 u32 abs_freq;
1133
1134 fe_tuner_ops->get_frequency(&t->fe, &abs_freq);
1135 f->frequency = (V4L2_TUNER_RADIO == t->mode) ?
Julia Lawall75b697f2009-08-01 16:48:41 -03001136 DIV_ROUND_CLOSEST(abs_freq * 2, 125) :
1137 DIV_ROUND_CLOSEST(abs_freq, 62500);
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -03001138 } else {
Hans Verkuil5ad339a2011-06-26 05:35:34 -03001139 f->frequency = (V4L2_TUNER_RADIO == f->type) ?
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -03001140 t->radio_freq : t->tv_freq;
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001141 }
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001142 return 0;
1143}
1144
Hans Verkuil338e9e12011-06-13 09:35:56 -03001145/**
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 */
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001155static 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
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -03001161 if (check_mode(t, vt->type) == -EINVAL)
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001162 return 0;
Hans Verkuil5ad339a2011-06-26 05:35:34 -03001163 if (vt->type == t->mode && analog_ops->get_afc)
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001164 vt->afc = analog_ops->get_afc(&t->fe);
Mauro Carvalho Chehaba1ad5ec2011-07-13 01:23:11 -03001165 if (t->mode != V4L2_TUNER_RADIO) {
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001166 vt->capability |= V4L2_TUNER_CAP_NORM;
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001167 vt->rangelow = tv_range[0] * 16;
1168 vt->rangehigh = tv_range[1] * 16;
1169 return 0;
1170 }
1171
1172 /* radio mode */
Hans Verkuil5ad339a2011-06-26 05:35:34 -03001173 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;
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001177
Hans Verkuil5ad339a2011-06-26 05:35:34 -03001178 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;
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001187 }
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -03001188 vt->capability |= V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001189 vt->rangelow = radio_range[0] * 16000;
1190 vt->rangehigh = radio_range[1] * 16000;
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -03001191
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001192 return 0;
1193}
1194
Hans Verkuil338e9e12011-06-13 09:35:56 -03001195/**
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 */
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001204static int tuner_s_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
1205{
1206 struct tuner *t = to_tuner(sd);
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001207
Hans Verkuil4e4a31f2011-06-14 03:56:09 -03001208 if (set_mode(t, vt->type))
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001209 return 0;
1210
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -03001211 if (t->mode == V4L2_TUNER_RADIO)
1212 t->audmode = vt->audmode;
Hans Verkuil4e4a31f2011-06-14 03:56:09 -03001213 set_freq(t, 0);
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -03001214
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001215 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;
1222
1223 if (analog_ops->tuner_status)
1224 analog_ops->tuner_status(&t->fe);
1225 return 0;
1226}
1227
Jean Delvare21b48a72007-03-12 19:20:15 -03001228static int tuner_suspend(struct i2c_client *c, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229{
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001230 struct tuner *t = to_tuner(i2c_get_clientdata(c));
Mauro Carvalho Chehabe2f63d92011-02-04 11:15:21 -03001231 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
Hans Verkuil9dd659d2007-11-04 11:03:36 -03001233 tuner_dbg("suspend\n");
Mauro Carvalho Chehabe2f63d92011-02-04 11:15:21 -03001234
1235 if (!t->standby && analog_ops->standby)
1236 analog_ops->standby(&t->fe);
1237
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 return 0;
1239}
1240
Jean Delvare21b48a72007-03-12 19:20:15 -03001241static int tuner_resume(struct i2c_client *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242{
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001243 struct tuner *t = to_tuner(i2c_get_clientdata(c));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
Hans Verkuil9dd659d2007-11-04 11:03:36 -03001245 tuner_dbg("resume\n");
Mauro Carvalho Chehabe2f63d92011-02-04 11:15:21 -03001246
1247 if (!t->standby)
Hans Verkuil9bf0ef02011-06-13 09:21:56 -03001248 if (set_mode(t, t->mode) == 0)
Hans Verkuil4e4a31f2011-06-14 03:56:09 -03001249 set_freq(t, 0);
Mauro Carvalho Chehabe2f63d92011-02-04 11:15:21 -03001250
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 return 0;
1252}
1253
Hans Verkuil75b4c262009-04-01 03:32:22 -03001254static 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
Mauro Carvalho Chehaba2894e32011-02-15 10:15:19 -02001268/*
1269 * Callback structs
1270 */
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001271
1272static const struct v4l2_subdev_core_ops tuner_core_ops = {
1273 .log_status = tuner_log_status,
Hans Verkuilf41737e2009-04-01 03:52:39 -03001274 .s_std = tuner_s_std,
Laurent Pinchart622b8282009-10-05 10:48:17 -03001275 .s_power = tuner_s_power,
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001276};
1277
1278static const struct v4l2_subdev_tuner_ops tuner_tuner_ops = {
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001279 .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
Mauro Carvalho Chehaba2894e32011-02-15 10:15:19 -02001293/*
1294 * I2C structs and module init functions
1295 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
Jean Delvareaf294862008-05-18 20:49:40 +02001297static const struct i2c_device_id tuner_id[] = {
1298 { "tuner", }, /* autodetect */
1299 { }
1300};
1301MODULE_DEVICE_TABLE(i2c, tuner_id);
1302
Hans Verkuil02a20982010-09-15 15:36:23 -03001303static 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,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314};
1315
Hans Verkuil02a20982010-09-15 15:36:23 -03001316static __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
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -03001329MODULE_DESCRIPTION("device driver for various TV and TV+FM radio tuners");
1330MODULE_AUTHOR("Ralph Metzler, Gerd Knorr, Gunther Mayer");
1331MODULE_LICENSE("GPL");