blob: 4059ea178c2d95e680e8679319d8aa1c661fa1b1 [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:
Miroslav Slugen27b93d82011-12-13 15:36:15 -0300329 case TUNER_PHILIPS_FMD1216MEX_MK3:
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700330 buffer[0] = 0x0b;
331 buffer[1] = 0xdc;
332 buffer[2] = 0x9c;
333 buffer[3] = 0x60;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700334 i2c_master_send(c, buffer, 4);
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700335 mdelay(1);
336 buffer[2] = 0x86;
337 buffer[3] = 0x54;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700338 i2c_master_send(c, buffer, 4);
Michael Krufkya07c8772008-04-29 03:54:19 -0300339 if (!dvb_attach(simple_tuner_attach, &t->fe,
340 t->i2c->adapter, t->i2c->addr, t->type))
Mauro Carvalho Chehabb9ef6bb2008-04-26 11:29:34 -0300341 goto attach_failed;
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700342 break;
Hartmut Hackmann93df3412005-11-08 21:36:31 -0800343 case TUNER_PHILIPS_TD1316:
344 buffer[0] = 0x0b;
345 buffer[1] = 0xdc;
346 buffer[2] = 0x86;
347 buffer[3] = 0xa4;
Michael Krufkya07c8772008-04-29 03:54:19 -0300348 i2c_master_send(c, buffer, 4);
349 if (!dvb_attach(simple_tuner_attach, &t->fe,
350 t->i2c->adapter, t->i2c->addr, t->type))
Mauro Carvalho Chehabb9ef6bb2008-04-26 11:29:34 -0300351 goto attach_failed;
Markus Rechbergerac272ed2006-01-23 17:11:09 -0200352 break;
Mauro Carvalho Chehab690c5442007-10-29 11:33:18 -0300353 case TUNER_XC2028:
354 {
Michel Ludwiga37b4c92007-11-16 07:46:14 -0300355 struct xc2028_config cfg = {
356 .i2c_adap = t->i2c->adapter,
357 .i2c_addr = t->i2c->addr,
Michel Ludwiga37b4c92007-11-16 07:46:14 -0300358 };
Michael Krufkya07c8772008-04-29 03:54:19 -0300359 if (!dvb_attach(xc2028_attach, &t->fe, &cfg))
Mauro Carvalho Chehabb9ef6bb2008-04-26 11:29:34 -0300360 goto attach_failed;
Michael Krufkyd6eef492009-10-24 16:42:16 -0300361 tune_now = 0;
Mauro Carvalho Chehab690c5442007-10-29 11:33:18 -0300362 break;
363 }
Mauro Carvalho Chehab15396232006-06-23 16:13:56 -0300364 case TUNER_TDA9887:
Mauro Carvalho Chehab09fee5f2008-04-30 15:29:57 -0300365 if (!dvb_attach(tda9887_attach,
366 &t->fe, t->i2c->adapter, t->i2c->addr))
367 goto attach_failed;
Mauro Carvalho Chehab15396232006-06-23 16:13:56 -0300368 break;
Steven Toth27c685a2008-01-05 16:50:14 -0300369 case TUNER_XC5000:
Mauro Carvalho Chehabb9ef6bb2008-04-26 11:29:34 -0300370 {
Mauro Carvalho Chehab900f7342011-02-04 12:56:39 -0300371 struct xc5000_config xc5000_cfg = {
372 .i2c_address = t->i2c->addr,
373 /* if_khz will be set at dvb_attach() */
374 .if_khz = 0,
375 };
376
Michael Krufkya07c8772008-04-29 03:54:19 -0300377 if (!dvb_attach(xc5000_attach,
Michael Krufky30650962008-09-06 14:56:58 -0300378 &t->fe, t->i2c->adapter, &xc5000_cfg))
Mauro Carvalho Chehabb9ef6bb2008-04-26 11:29:34 -0300379 goto attach_failed;
Michael Krufkyd6eef492009-10-24 16:42:16 -0300380 tune_now = 0;
Steven Toth27c685a2008-01-05 16:50:14 -0300381 break;
Mauro Carvalho Chehabb9ef6bb2008-04-26 11:29:34 -0300382 }
Michael Krufky93463892009-09-15 23:04:18 -0300383 case TUNER_NXP_TDA18271:
384 {
385 struct tda18271_config cfg = {
386 .config = t->config,
Mauro Carvalho Chehabe350d442010-09-26 22:58:28 -0300387 .small_i2c = TDA18271_03_BYTE_CHUNK_INIT,
Michael Krufky93463892009-09-15 23:04:18 -0300388 };
389
390 if (!dvb_attach(tda18271_attach, &t->fe, t->i2c->addr,
391 t->i2c->adapter, &cfg))
392 goto attach_failed;
Michael Krufkyd6eef492009-10-24 16:42:16 -0300393 tune_now = 0;
Michael Krufky93463892009-09-15 23:04:18 -0300394 break;
395 }
Davide Ferri8d009a02009-06-23 22:34:06 -0300396 case TUNER_XC4000:
397 {
398 struct xc4000_config xc4000_cfg = {
399 .i2c_address = t->i2c->addr,
istvan_v@mailbox.hu8edeb6e2011-06-06 13:03:44 -0300400 /* FIXME: the correct parameters will be set */
401 /* only when the digital dvb_attach() occurs */
402 .default_pm = 0,
403 .dvb_amplitude = 0,
404 .set_smoothedcvbs = 0,
405 .if_khz = 0
Davide Ferri8d009a02009-06-23 22:34:06 -0300406 };
407 if (!dvb_attach(xc4000_attach,
408 &t->fe, t->i2c->adapter, &xc4000_cfg))
409 goto attach_failed;
410 tune_now = 0;
411 break;
412 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 default:
Michael Krufkya07c8772008-04-29 03:54:19 -0300414 if (!dvb_attach(simple_tuner_attach, &t->fe,
415 t->i2c->adapter, t->i2c->addr, t->type))
Mauro Carvalho Chehabb9ef6bb2008-04-26 11:29:34 -0300416 goto attach_failed;
417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 break;
419 }
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700420
Michael Krufkybc3e5c72007-12-21 11:18:32 -0300421 if ((NULL == analog_ops->set_params) &&
Michael Krufky1dde7a42007-10-21 13:40:56 -0300422 (fe_tuner_ops->set_analog_params)) {
Michael Krufkya07c8772008-04-29 03:54:19 -0300423
Michael Krufky7271e602008-05-26 16:08:40 +0200424 t->name = fe_tuner_ops->info.name;
Michael Krufkye18f9442007-08-21 01:25:48 -0300425
Michael Krufky4e9154b2007-10-21 19:39:50 -0300426 t->fe.analog_demod_priv = t;
Hans Verkuile8a4a9e2008-11-24 18:21:40 -0300427 memcpy(analog_ops, &tuner_analog_ops,
Michael Krufkybc3e5c72007-12-21 11:18:32 -0300428 sizeof(struct analog_demod_ops));
Michael Krufkya07c8772008-04-29 03:54:19 -0300429
Michael Krufkya55db8c2007-12-09 13:52:51 -0300430 } else {
Michael Krufky7271e602008-05-26 16:08:40 +0200431 t->name = analog_ops->info.name;
Michael Krufkye18f9442007-08-21 01:25:48 -0300432 }
433
Michael Krufky7271e602008-05-26 16:08:40 +0200434 tuner_dbg("type set to %s\n", t->name);
Michael Krufkye18f9442007-08-21 01:25:48 -0300435
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -0300436 t->mode_mask = new_mode_mask;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700437
Michael Krufkyd6eef492009-10-24 16:42:16 -0300438 /* Some tuners require more initialization setup before use,
439 such as firmware download or device calibration.
Mauro Carvalho Chehabb9ef6bb2008-04-26 11:29:34 -0300440 trying to set a frequency here will just fail
441 FIXME: better to move set_freq to the tuner code. This is needed
442 on analog tuners for PLL to properly work
443 */
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300444 if (tune_now) {
445 if (V4L2_TUNER_RADIO == t->mode)
446 set_radio_freq(c, t->radio_freq);
447 else
448 set_tv_freq(c, t->tv_freq);
449 }
Mauro Carvalho Chehabb9ef6bb2008-04-26 11:29:34 -0300450
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700451 tuner_dbg("%s %s I2C addr 0x%02x with type %d used for 0x%02x\n",
Laurent Riffard604f28e2005-11-26 20:43:39 +0100452 c->adapter->name, c->driver->driver.name, c->addr << 1, type,
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700453 t->mode_mask);
Mauro Carvalho Chehabb9ef6bb2008-04-26 11:29:34 -0300454 return;
455
456attach_failed:
457 tuner_dbg("Tuner attach for type = %d failed.\n", t->type);
458 t->type = TUNER_ABSENT;
Mauro Carvalho Chehabb9ef6bb2008-04-26 11:29:34 -0300459
460 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461}
462
Mauro Carvalho Chehab0ae79d92011-02-15 01:10:20 -0300463/**
464 * tuner_s_type_addr - Sets the tuner type for a device
465 *
466 * @sd: subdev descriptor
467 * @tun_setup: type to be associated to a given tuner i2c address
468 *
469 * This function applys the tuner config to tuner specified
470 * by tun_setup structure.
471 * If tuner I2C address is UNSET, then it will only set the device
472 * if the tuner supports the mode specified in the call.
473 * If the address is specified, the change will be applied only if
474 * tuner I2C address matches.
475 * The call can change the tuner number and the tuner mode.
476 */
Mauro Carvalho Chehaba34ec5f2011-02-15 00:55:18 -0300477static int tuner_s_type_addr(struct v4l2_subdev *sd,
478 struct tuner_setup *tun_setup)
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700479{
Mauro Carvalho Chehaba34ec5f2011-02-15 00:55:18 -0300480 struct tuner *t = to_tuner(sd);
481 struct i2c_client *c = v4l2_get_subdevdata(sd);
482
483 tuner_dbg("Calling set_type_addr for type=%d, addr=0x%02x, mode=0x%02x, config=0x%02x\n",
484 tun_setup->type,
485 tun_setup->addr,
486 tun_setup->mode_mask,
487 tun_setup->config);
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700488
Mauro Carvalho Chehab7d275bf2011-02-04 11:28:00 -0300489 if ((t->type == UNSET && ((tun_setup->addr == ADDR_UNSET) &&
490 (t->mode_mask & tun_setup->mode_mask))) ||
491 (tun_setup->addr == c->addr)) {
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -0300492 set_type(c, tun_setup->type, tun_setup->mode_mask,
493 tun_setup->config, tun_setup->tuner_callback);
Mauro Carvalho Chehabb9ef6bb2008-04-26 11:29:34 -0300494 } else
495 tuner_dbg("set addr discarded for type %i, mask %x. "
496 "Asked to change tuner at addr 0x%02x, with mask %x\n",
497 t->type, t->mode_mask,
498 tun_setup->addr, tun_setup->mode_mask);
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700499
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300500 return 0;
501}
502
Mauro Carvalho Chehaba2894e32011-02-15 10:15:19 -0200503/**
504 * tuner_s_config - Sets tuner configuration
505 *
506 * @sd: subdev descriptor
507 * @cfg: tuner configuration
508 *
509 * Calls tuner set_config() private function to set some tuner-internal
510 * parameters
511 */
Mauro Carvalho Chehab7d275bf2011-02-04 11:28:00 -0300512static int tuner_s_config(struct v4l2_subdev *sd,
513 const struct v4l2_priv_tun_config *cfg)
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300514{
515 struct tuner *t = to_tuner(sd);
516 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
517
518 if (t->type != cfg->tuner)
519 return 0;
520
521 if (analog_ops->set_config) {
522 analog_ops->set_config(&t->fe, cfg->priv);
523 return 0;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700524 }
Mauro Carvalho Chehab793cf9e2005-09-09 13:03:37 -0700525
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300526 tuner_dbg("Tuner frontend module has no way to set config\n");
Mauro Carvalho Chehab793cf9e2005-09-09 13:03:37 -0700527 return 0;
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700528}
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700529
Mauro Carvalho Chehaba2894e32011-02-15 10:15:19 -0200530/**
531 * tuner_lookup - Seek for tuner adapters
532 *
533 * @adap: i2c_adapter struct
534 * @radio: pointer to be filled if the adapter is radio
535 * @tv: pointer to be filled if the adapter is TV
536 *
537 * Search for existing radio and/or TV tuners on the given I2C adapter,
538 * discarding demod-only adapters (tda9887).
539 *
540 * Note that when this function is called from tuner_probe you can be
541 * certain no other devices will be added/deleted at the same time, I2C
542 * core protects against that.
543 */
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300544static void tuner_lookup(struct i2c_adapter *adap,
545 struct tuner **radio, struct tuner **tv)
546{
547 struct tuner *pos;
548
549 *radio = NULL;
550 *tv = NULL;
551
552 list_for_each_entry(pos, &tuner_list, list) {
553 int mode_mask;
554
555 if (pos->i2c->adapter != adap ||
556 strcmp(pos->i2c->driver->driver.name, "tuner"))
557 continue;
558
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -0300559 mode_mask = pos->mode_mask;
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300560 if (*radio == NULL && mode_mask == T_RADIO)
561 *radio = pos;
562 /* Note: currently TDA9887 is the only demod-only
563 device. If other devices appear then we need to
564 make this test more general. */
565 else if (*tv == NULL && pos->type != TUNER_TDA9887 &&
Mauro Carvalho Chehabad020dc2011-02-15 09:30:50 -0200566 (pos->mode_mask & T_ANALOG_TV))
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300567 *tv = pos;
568 }
569}
570
Mauro Carvalho Chehaba2894e32011-02-15 10:15:19 -0200571/**
572 *tuner_probe - Probes the existing tuners on an I2C bus
573 *
574 * @client: i2c_client descriptor
575 * @id: not used
576 *
577 * This routine probes for tuners at the expected I2C addresses. On most
578 * cases, if a device answers to a given I2C address, it assumes that the
579 * device is a tuner. On a few cases, however, an additional logic is needed
580 * to double check if the device is really a tuner, or to identify the tuner
581 * type, like on tea5767/5761 devices.
582 *
583 * During client attach, set_type is called by adapter's attach_inform callback.
584 * set_type must then be completed by tuner_probe.
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300585 */
586static int tuner_probe(struct i2c_client *client,
587 const struct i2c_device_id *id)
588{
589 struct tuner *t;
590 struct tuner *radio;
591 struct tuner *tv;
592
593 t = kzalloc(sizeof(struct tuner), GFP_KERNEL);
594 if (NULL == t)
595 return -ENOMEM;
596 v4l2_i2c_subdev_init(&t->sd, client, &tuner_ops);
597 t->i2c = client;
598 t->name = "(tuner unset)";
599 t->type = UNSET;
600 t->audmode = V4L2_TUNER_MODE_STEREO;
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -0300601 t->standby = 1;
602 t->radio_freq = 87.5 * 16000; /* Initial freq range */
603 t->tv_freq = 400 * 16; /* Sets freq to VHF High - needed for some PLL's to properly start */
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300604
605 if (show_i2c) {
606 unsigned char buffer[16];
607 int i, rc;
608
609 memset(buffer, 0, sizeof(buffer));
610 rc = i2c_master_recv(client, buffer, sizeof(buffer));
611 tuner_info("I2C RECV = ");
612 for (i = 0; i < rc; i++)
613 printk(KERN_CONT "%02x ", buffer[i]);
614 printk("\n");
615 }
616
617 /* autodetection code based on the i2c addr */
618 if (!no_autodetect) {
619 switch (client->addr) {
620 case 0x10:
621 if (tuner_symbol_probe(tea5761_autodetection,
622 t->i2c->adapter,
623 t->i2c->addr) >= 0) {
624 t->type = TUNER_TEA5761;
625 t->mode_mask = T_RADIO;
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300626 tuner_lookup(t->i2c->adapter, &radio, &tv);
627 if (tv)
628 tv->mode_mask &= ~T_RADIO;
629
630 goto register_client;
631 }
632 kfree(t);
633 return -ENODEV;
634 case 0x42:
635 case 0x43:
636 case 0x4a:
637 case 0x4b:
638 /* If chip is not tda8290, don't register.
639 since it can be tda9887*/
640 if (tuner_symbol_probe(tda829x_probe, t->i2c->adapter,
641 t->i2c->addr) >= 0) {
642 tuner_dbg("tda829x detected\n");
643 } else {
644 /* Default is being tda9887 */
645 t->type = TUNER_TDA9887;
Mauro Carvalho Chehabad020dc2011-02-15 09:30:50 -0200646 t->mode_mask = T_RADIO | T_ANALOG_TV;
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300647 goto register_client;
648 }
649 break;
650 case 0x60:
651 if (tuner_symbol_probe(tea5767_autodetection,
652 t->i2c->adapter, t->i2c->addr)
653 >= 0) {
654 t->type = TUNER_TEA5767;
655 t->mode_mask = T_RADIO;
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300656 /* Sets freq to FM range */
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300657 tuner_lookup(t->i2c->adapter, &radio, &tv);
658 if (tv)
659 tv->mode_mask &= ~T_RADIO;
660
661 goto register_client;
662 }
663 break;
664 }
665 }
666
667 /* Initializes only the first TV tuner on this adapter. Why only the
668 first? Because there are some devices (notably the ones with TI
669 tuners) that have more than one i2c address for the *same* device.
670 Experience shows that, except for just one case, the first
671 address is the right one. The exception is a Russian tuner
672 (ACORP_Y878F). So, the desired behavior is just to enable the
673 first found TV tuner. */
674 tuner_lookup(t->i2c->adapter, &radio, &tv);
675 if (tv == NULL) {
Mauro Carvalho Chehabad020dc2011-02-15 09:30:50 -0200676 t->mode_mask = T_ANALOG_TV;
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300677 if (radio == NULL)
678 t->mode_mask |= T_RADIO;
679 tuner_dbg("Setting mode_mask to 0x%02x\n", t->mode_mask);
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300680 }
681
682 /* Should be just before return */
683register_client:
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300684 /* Sets a default mode */
Mauro Carvalho Chehab7d275bf2011-02-04 11:28:00 -0300685 if (t->mode_mask & T_ANALOG_TV)
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300686 t->mode = V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehab7d275bf2011-02-04 11:28:00 -0300687 else
Mauro Carvalho Chehabad020dc2011-02-15 09:30:50 -0200688 t->mode = V4L2_TUNER_RADIO;
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300689 set_type(client, t->type, t->mode_mask, t->config, t->fe.callback);
690 list_add_tail(&t->list, &tuner_list);
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -0300691
Mauro Carvalho Chehabad020dc2011-02-15 09:30:50 -0200692 tuner_info("Tuner %d found with type(s)%s%s.\n",
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -0300693 t->type,
Mauro Carvalho Chehabad020dc2011-02-15 09:30:50 -0200694 t->mode_mask & T_RADIO ? " Radio" : "",
695 t->mode_mask & T_ANALOG_TV ? " TV" : "");
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300696 return 0;
697}
698
Mauro Carvalho Chehaba2894e32011-02-15 10:15:19 -0200699/**
700 * tuner_remove - detaches a tuner
701 *
702 * @client: i2c_client descriptor
703 */
704
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300705static int tuner_remove(struct i2c_client *client)
706{
707 struct tuner *t = to_tuner(i2c_get_clientdata(client));
708
709 v4l2_device_unregister_subdev(&t->sd);
710 tuner_detach(&t->fe);
711 t->fe.analog_demod_priv = NULL;
712
713 list_del(&t->list);
714 kfree(t);
715 return 0;
716}
717
718/*
Mauro Carvalho Chehab0eec66c2011-02-15 10:27:03 -0200719 * Functions to switch between Radio and TV
720 *
721 * A few cards have a separate I2C tuner for radio. Those routines
722 * take care of switching between TV/Radio mode, filtering only the
723 * commands that apply to the Radio or TV tuner.
724 */
725
726/**
727 * check_mode - Verify if tuner supports the requested mode
728 * @t: a pointer to the module's internal struct_tuner
729 *
730 * This function checks if the tuner is capable of tuning analog TV,
731 * digital TV or radio, depending on what the caller wants. If the
732 * tuner can't support that mode, it returns -EINVAL. Otherwise, it
733 * returns 0.
734 * This function is needed for boards that have a separate tuner for
735 * radio (like devices with tea5767).
Mauro Carvalho Chehaba1ad5ec2011-07-13 01:23:11 -0300736 * NOTE: mt20xx uses V4L2_TUNER_DIGITAL_TV and calls set_tv_freq to
737 * select a TV frequency. So, t_mode = T_ANALOG_TV could actually
738 * be used to represent a Digital TV too.
Mauro Carvalho Chehab0eec66c2011-02-15 10:27:03 -0200739 */
740static inline int check_mode(struct tuner *t, enum v4l2_tuner_type mode)
741{
Mauro Carvalho Chehaba1ad5ec2011-07-13 01:23:11 -0300742 int t_mode;
743 if (mode == V4L2_TUNER_RADIO)
744 t_mode = T_RADIO;
745 else
746 t_mode = T_ANALOG_TV;
747
748 if ((t_mode & t->mode_mask) == 0)
Mauro Carvalho Chehab0eec66c2011-02-15 10:27:03 -0200749 return -EINVAL;
750
751 return 0;
752}
753
754/**
Hans Verkuil4e4a31f2011-06-14 03:56:09 -0300755 * set_mode - Switch tuner to other mode.
Mauro Carvalho Chehab0eec66c2011-02-15 10:27:03 -0200756 * @t: a pointer to the module's internal struct_tuner
757 * @mode: enum v4l2_type (radio or TV)
Mauro Carvalho Chehab0eec66c2011-02-15 10:27:03 -0200758 *
759 * If tuner doesn't support the needed mode (radio or TV), prints a
760 * debug message and returns -EINVAL, changing its state to standby.
Hans Verkuil4e4a31f2011-06-14 03:56:09 -0300761 * Otherwise, changes the mode and returns 0.
Mauro Carvalho Chehab0eec66c2011-02-15 10:27:03 -0200762 */
Hans Verkuil4e4a31f2011-06-14 03:56:09 -0300763static int set_mode(struct tuner *t, enum v4l2_tuner_type mode)
Mauro Carvalho Chehab0eec66c2011-02-15 10:27:03 -0200764{
765 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
766
767 if (mode != t->mode) {
768 if (check_mode(t, mode) == -EINVAL) {
769 tuner_dbg("Tuner doesn't support mode %d. "
770 "Putting tuner to sleep\n", mode);
771 t->standby = true;
772 if (analog_ops->standby)
773 analog_ops->standby(&t->fe);
774 return -EINVAL;
775 }
776 t->mode = mode;
777 tuner_dbg("Changing to mode %d\n", mode);
778 }
Mauro Carvalho Chehab0eec66c2011-02-15 10:27:03 -0200779 return 0;
780}
781
Hans Verkuil4e4a31f2011-06-14 03:56:09 -0300782/**
783 * set_freq - Set the tuner to the desired frequency.
784 * @t: a pointer to the module's internal struct_tuner
785 * @freq: frequency to set (0 means to use the current frequency)
786 */
787static void set_freq(struct tuner *t, unsigned int freq)
788{
789 struct i2c_client *client = v4l2_get_subdevdata(&t->sd);
790
791 if (t->mode == V4L2_TUNER_RADIO) {
792 if (!freq)
793 freq = t->radio_freq;
794 set_radio_freq(client, freq);
795 } else {
796 if (!freq)
797 freq = t->tv_freq;
798 set_tv_freq(client, freq);
799 }
800}
801
Mauro Carvalho Chehab0eec66c2011-02-15 10:27:03 -0200802/*
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300803 * Functions that are specific for TV mode
804 */
805
Mauro Carvalho Chehaba2894e32011-02-15 10:15:19 -0200806/**
807 * set_tv_freq - Set tuner frequency, freq in Units of 62.5 kHz = 1/16MHz
808 *
809 * @c: i2c_client descriptor
810 * @freq: frequency
811 */
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300812static void set_tv_freq(struct i2c_client *c, unsigned int freq)
813{
814 struct tuner *t = to_tuner(i2c_get_clientdata(c));
815 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
816
817 struct analog_parameters params = {
818 .mode = t->mode,
819 .audmode = t->audmode,
820 .std = t->std
821 };
822
823 if (t->type == UNSET) {
Mauro Carvalho Chehab7d275bf2011-02-04 11:28:00 -0300824 tuner_warn("tuner type not set\n");
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300825 return;
826 }
827 if (NULL == analog_ops->set_params) {
Mauro Carvalho Chehab7d275bf2011-02-04 11:28:00 -0300828 tuner_warn("Tuner has no way to set tv freq\n");
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300829 return;
830 }
831 if (freq < tv_range[0] * 16 || freq > tv_range[1] * 16) {
Mauro Carvalho Chehab7d275bf2011-02-04 11:28:00 -0300832 tuner_dbg("TV freq (%d.%02d) out of range (%d-%d)\n",
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300833 freq / 16, freq % 16 * 100 / 16, tv_range[0],
834 tv_range[1]);
835 /* V4L2 spec: if the freq is not possible then the closest
836 possible value should be selected */
837 if (freq < tv_range[0] * 16)
838 freq = tv_range[0] * 16;
839 else
840 freq = tv_range[1] * 16;
841 }
842 params.frequency = freq;
843 tuner_dbg("tv freq set to %d.%02d\n",
844 freq / 16, freq % 16 * 100 / 16);
845 t->tv_freq = freq;
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -0300846 t->standby = false;
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300847
848 analog_ops->set_params(&t->fe, &params);
849}
850
Mauro Carvalho Chehaba2894e32011-02-15 10:15:19 -0200851/**
852 * tuner_fixup_std - force a given video standard variant
853 *
Hans Verkuil48783302011-06-13 09:47:56 -0300854 * @t: tuner internal struct
855 * @std: TV standard
Mauro Carvalho Chehaba2894e32011-02-15 10:15:19 -0200856 *
857 * A few devices or drivers have problem to detect some standard variations.
858 * On other operational systems, the drivers generally have a per-country
859 * code, and some logic to apply per-country hacks. V4L2 API doesn't provide
860 * such hacks. Instead, it relies on a proper video standard selection from
861 * the userspace application. However, as some apps are buggy, not allowing
862 * to distinguish all video standard variations, a modprobe parameter can
863 * be used to force a video standard match.
864 */
Hans Verkuil48783302011-06-13 09:47:56 -0300865static v4l2_std_id tuner_fixup_std(struct tuner *t, v4l2_std_id std)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866{
Hans Verkuil48783302011-06-13 09:47:56 -0300867 if (pal[0] != '-' && (std & V4L2_STD_PAL) == V4L2_STD_PAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 switch (pal[0]) {
Hans Verkuile71ced12006-12-11 15:51:43 -0300869 case '6':
Hans Verkuil48783302011-06-13 09:47:56 -0300870 return V4L2_STD_PAL_60;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 case 'b':
872 case 'B':
873 case 'g':
874 case 'G':
Hans Verkuil48783302011-06-13 09:47:56 -0300875 return V4L2_STD_PAL_BG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 case 'i':
877 case 'I':
Hans Verkuil48783302011-06-13 09:47:56 -0300878 return V4L2_STD_PAL_I;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 case 'd':
880 case 'D':
881 case 'k':
882 case 'K':
Hans Verkuil48783302011-06-13 09:47:56 -0300883 return V4L2_STD_PAL_DK;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700884 case 'M':
885 case 'm':
Hans Verkuil48783302011-06-13 09:47:56 -0300886 return V4L2_STD_PAL_M;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700887 case 'N':
888 case 'n':
Hans Verkuil48783302011-06-13 09:47:56 -0300889 if (pal[1] == 'c' || pal[1] == 'C')
890 return V4L2_STD_PAL_Nc;
891 return V4L2_STD_PAL_N;
Mauro Carvalho Chehab21d4df32005-09-09 13:03:59 -0700892 default:
Mauro Carvalho Chehab7d275bf2011-02-04 11:28:00 -0300893 tuner_warn("pal= argument not recognised\n");
Mauro Carvalho Chehab21d4df32005-09-09 13:03:59 -0700894 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 }
896 }
Hans Verkuil48783302011-06-13 09:47:56 -0300897 if (secam[0] != '-' && (std & V4L2_STD_SECAM) == V4L2_STD_SECAM) {
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700898 switch (secam[0]) {
Mauro Carvalho Chehab7e578192006-01-09 15:25:27 -0200899 case 'b':
900 case 'B':
901 case 'g':
902 case 'G':
903 case 'h':
904 case 'H':
Hans Verkuil48783302011-06-13 09:47:56 -0300905 return V4L2_STD_SECAM_B |
906 V4L2_STD_SECAM_G |
907 V4L2_STD_SECAM_H;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700908 case 'd':
909 case 'D':
910 case 'k':
911 case 'K':
Hans Verkuil48783302011-06-13 09:47:56 -0300912 return V4L2_STD_SECAM_DK;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700913 case 'l':
914 case 'L':
Hans Verkuil48783302011-06-13 09:47:56 -0300915 if ((secam[1] == 'C') || (secam[1] == 'c'))
916 return V4L2_STD_SECAM_LC;
917 return V4L2_STD_SECAM_L;
Mauro Carvalho Chehab21d4df32005-09-09 13:03:59 -0700918 default:
Mauro Carvalho Chehab7d275bf2011-02-04 11:28:00 -0300919 tuner_warn("secam= argument not recognised\n");
Mauro Carvalho Chehab21d4df32005-09-09 13:03:59 -0700920 break;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700921 }
922 }
923
Hans Verkuil48783302011-06-13 09:47:56 -0300924 if (ntsc[0] != '-' && (std & V4L2_STD_NTSC) == V4L2_STD_NTSC) {
Mauro Carvalho Chehab7e578192006-01-09 15:25:27 -0200925 switch (ntsc[0]) {
926 case 'm':
927 case 'M':
Hans Verkuil48783302011-06-13 09:47:56 -0300928 return V4L2_STD_NTSC_M;
Mauro Carvalho Chehab7e578192006-01-09 15:25:27 -0200929 case 'j':
930 case 'J':
Hans Verkuil48783302011-06-13 09:47:56 -0300931 return V4L2_STD_NTSC_M_JP;
Hans Verkuild97a11e2006-02-07 06:48:40 -0200932 case 'k':
933 case 'K':
Hans Verkuil48783302011-06-13 09:47:56 -0300934 return V4L2_STD_NTSC_M_KR;
Mauro Carvalho Chehab7e578192006-01-09 15:25:27 -0200935 default:
936 tuner_info("ntsc= argument not recognised\n");
937 break;
938 }
939 }
Hans Verkuil48783302011-06-13 09:47:56 -0300940 return std;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941}
942
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300943/*
944 * Functions that are specific for Radio mode
945 */
946
Mauro Carvalho Chehaba2894e32011-02-15 10:15:19 -0200947/**
948 * set_radio_freq - Set tuner frequency, freq in Units of 62.5 Hz = 1/16kHz
949 *
950 * @c: i2c_client descriptor
951 * @freq: frequency
952 */
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300953static void set_radio_freq(struct i2c_client *c, unsigned int freq)
954{
955 struct tuner *t = to_tuner(i2c_get_clientdata(c));
956 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
957
958 struct analog_parameters params = {
959 .mode = t->mode,
960 .audmode = t->audmode,
961 .std = t->std
962 };
963
964 if (t->type == UNSET) {
Mauro Carvalho Chehab7d275bf2011-02-04 11:28:00 -0300965 tuner_warn("tuner type not set\n");
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300966 return;
967 }
968 if (NULL == analog_ops->set_params) {
Mauro Carvalho Chehab7d275bf2011-02-04 11:28:00 -0300969 tuner_warn("tuner has no way to set radio frequency\n");
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300970 return;
971 }
972 if (freq < radio_range[0] * 16000 || freq > radio_range[1] * 16000) {
Mauro Carvalho Chehab7d275bf2011-02-04 11:28:00 -0300973 tuner_dbg("radio freq (%d.%02d) out of range (%d-%d)\n",
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300974 freq / 16000, freq % 16000 * 100 / 16000,
975 radio_range[0], radio_range[1]);
976 /* V4L2 spec: if the freq is not possible then the closest
977 possible value should be selected */
978 if (freq < radio_range[0] * 16000)
979 freq = radio_range[0] * 16000;
980 else
981 freq = radio_range[1] * 16000;
982 }
983 params.frequency = freq;
984 tuner_dbg("radio freq set to %d.%02d\n",
985 freq / 16000, freq % 16000 * 100 / 16000);
986 t->radio_freq = freq;
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -0300987 t->standby = false;
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300988
989 analog_ops->set_params(&t->fe, &params);
990}
991
Mauro Carvalho Chehab0eec66c2011-02-15 10:27:03 -0200992/*
993 * Debug function for reporting tuner status to userspace
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300994 */
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300995
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -0300996/**
997 * tuner_status - Dumps the current tuner status at dmesg
998 * @fe: pointer to struct dvb_frontend
999 *
1000 * This callback is used only for driver debug purposes, answering to
1001 * VIDIOC_LOG_STATUS. No changes should happen on this call.
1002 */
Michael Krufky4e9154b2007-10-21 19:39:50 -03001003static void tuner_status(struct dvb_frontend *fe)
Mauro Carvalho Chehab7e578192006-01-09 15:25:27 -02001004{
Michael Krufky4e9154b2007-10-21 19:39:50 -03001005 struct tuner *t = fe->analog_demod_priv;
Mauro Carvalho Chehab7e578192006-01-09 15:25:27 -02001006 unsigned long freq, freq_fraction;
Michael Krufkya07c8772008-04-29 03:54:19 -03001007 struct dvb_tuner_ops *fe_tuner_ops = &fe->ops.tuner_ops;
1008 struct analog_demod_ops *analog_ops = &fe->ops.analog_ops;
Mauro Carvalho Chehab7e578192006-01-09 15:25:27 -02001009 const char *p;
1010
1011 switch (t->mode) {
Mauro Carvalho Chehab7d275bf2011-02-04 11:28:00 -03001012 case V4L2_TUNER_RADIO:
1013 p = "radio";
1014 break;
Mauro Carvalho Chehaba1ad5ec2011-07-13 01:23:11 -03001015 case V4L2_TUNER_DIGITAL_TV: /* Used by mt20xx */
Mauro Carvalho Chehab7d275bf2011-02-04 11:28:00 -03001016 p = "digital TV";
1017 break;
1018 case V4L2_TUNER_ANALOG_TV:
1019 default:
1020 p = "analog TV";
1021 break;
Mauro Carvalho Chehab7e578192006-01-09 15:25:27 -02001022 }
1023 if (t->mode == V4L2_TUNER_RADIO) {
Hans Verkuil27487d42006-01-15 15:04:52 -02001024 freq = t->radio_freq / 16000;
1025 freq_fraction = (t->radio_freq % 16000) * 100 / 16000;
Mauro Carvalho Chehab7e578192006-01-09 15:25:27 -02001026 } else {
Hans Verkuil27487d42006-01-15 15:04:52 -02001027 freq = t->tv_freq / 16;
1028 freq_fraction = (t->tv_freq % 16) * 100 / 16;
Mauro Carvalho Chehab7e578192006-01-09 15:25:27 -02001029 }
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -03001030 tuner_info("Tuner mode: %s%s\n", p,
1031 t->standby ? " on standby mode" : "");
Mauro Carvalho Chehab7e578192006-01-09 15:25:27 -02001032 tuner_info("Frequency: %lu.%02lu MHz\n", freq, freq_fraction);
Mauro Carvalho Chehab4ae5c2e2006-03-25 15:53:38 -03001033 tuner_info("Standard: 0x%08lx\n", (unsigned long)t->std);
Hans Verkuil8a4b2752006-01-23 17:11:09 -02001034 if (t->mode != V4L2_TUNER_RADIO)
Mauro Carvalho Chehab7d275bf2011-02-04 11:28:00 -03001035 return;
Michael Krufkye18f9442007-08-21 01:25:48 -03001036 if (fe_tuner_ops->get_status) {
1037 u32 tuner_status;
1038
1039 fe_tuner_ops->get_status(&t->fe, &tuner_status);
1040 if (tuner_status & TUNER_STATUS_LOCKED)
1041 tuner_info("Tuner is locked.\n");
1042 if (tuner_status & TUNER_STATUS_STEREO)
1043 tuner_info("Stereo: yes\n");
1044 }
Michael Krufkybc3e5c72007-12-21 11:18:32 -03001045 if (analog_ops->has_signal)
1046 tuner_info("Signal strength: %d\n",
1047 analog_ops->has_signal(fe));
Mauro Carvalho Chehab7e578192006-01-09 15:25:27 -02001048}
Hans Verkuil8a4b2752006-01-23 17:11:09 -02001049
Mauro Carvalho Chehab0eec66c2011-02-15 10:27:03 -02001050/*
1051 * Function to splicitly change mode to radio. Probably not needed anymore
1052 */
1053
1054static int tuner_s_radio(struct v4l2_subdev *sd)
1055{
1056 struct tuner *t = to_tuner(sd);
Mauro Carvalho Chehab0eec66c2011-02-15 10:27:03 -02001057
Hans Verkuil4e4a31f2011-06-14 03:56:09 -03001058 if (set_mode(t, V4L2_TUNER_RADIO) == 0)
1059 set_freq(t, 0);
Mauro Carvalho Chehab0eec66c2011-02-15 10:27:03 -02001060 return 0;
1061}
1062
1063/*
1064 * Tuner callbacks to handle userspace ioctl's
1065 */
1066
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -03001067/**
1068 * tuner_s_power - controls the power state of the tuner
1069 * @sd: pointer to struct v4l2_subdev
Hans Verkuild16625e2011-06-25 10:24:49 -03001070 * @on: a zero value puts the tuner to sleep, non-zero wakes it up
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -03001071 */
Laurent Pinchart622b8282009-10-05 10:48:17 -03001072static int tuner_s_power(struct v4l2_subdev *sd, int on)
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001073{
1074 struct tuner *t = to_tuner(sd);
Michael Krufkybc3e5c72007-12-21 11:18:32 -03001075 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
Hans Verkuild16625e2011-06-25 10:24:49 -03001077 if (on) {
1078 if (t->standby && set_mode(t, t->mode) == 0) {
1079 tuner_dbg("Waking up tuner\n");
1080 set_freq(t, 0);
1081 }
Laurent Pinchart622b8282009-10-05 10:48:17 -03001082 return 0;
Hans Verkuild16625e2011-06-25 10:24:49 -03001083 }
Laurent Pinchart622b8282009-10-05 10:48:17 -03001084
Mauro Carvalho Chehab16a5e532008-12-20 07:17:10 -03001085 tuner_dbg("Putting tuner to sleep\n");
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -03001086 t->standby = true;
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001087 if (analog_ops->standby)
1088 analog_ops->standby(&t->fe);
1089 return 0;
1090}
1091
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001092static int tuner_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
1093{
1094 struct tuner *t = to_tuner(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
Hans Verkuil4e4a31f2011-06-14 03:56:09 -03001096 if (set_mode(t, V4L2_TUNER_ANALOG_TV))
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001097 return 0;
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -07001098
Hans Verkuil48783302011-06-13 09:47:56 -03001099 t->std = tuner_fixup_std(t, std);
1100 if (t->std != std)
1101 tuner_dbg("Fixup standard %llx to %llx\n", std, t->std);
Hans Verkuil4e4a31f2011-06-14 03:56:09 -03001102 set_freq(t, 0);
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001103 return 0;
1104}
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -07001105
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001106static int tuner_s_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f)
1107{
1108 struct tuner *t = to_tuner(sd);
Michael Krufkye18f9442007-08-21 01:25:48 -03001109
Hans Verkuil4e4a31f2011-06-14 03:56:09 -03001110 if (set_mode(t, f->type) == 0)
1111 set_freq(t, f->frequency);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 return 0;
1113}
1114
Hans Verkuil338e9e12011-06-13 09:35:56 -03001115/**
1116 * tuner_g_frequency - Get the tuned frequency for the tuner
1117 * @sd: pointer to struct v4l2_subdev
1118 * @f: pointer to struct v4l2_frequency
1119 *
1120 * At return, the structure f will be filled with tuner frequency
1121 * if the tuner matches the f->type.
1122 * Note: f->type should be initialized before calling it.
1123 * This is done by either video_ioctl2 or by the bridge driver.
1124 */
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001125static int tuner_g_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f)
1126{
1127 struct tuner *t = to_tuner(sd);
1128 struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops;
1129
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -03001130 if (check_mode(t, f->type) == -EINVAL)
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001131 return 0;
Hans Verkuil5ad339a2011-06-26 05:35:34 -03001132 if (f->type == t->mode && fe_tuner_ops->get_frequency && !t->standby) {
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001133 u32 abs_freq;
1134
1135 fe_tuner_ops->get_frequency(&t->fe, &abs_freq);
1136 f->frequency = (V4L2_TUNER_RADIO == t->mode) ?
Julia Lawall75b697f2009-08-01 16:48:41 -03001137 DIV_ROUND_CLOSEST(abs_freq * 2, 125) :
1138 DIV_ROUND_CLOSEST(abs_freq, 62500);
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -03001139 } else {
Hans Verkuil5ad339a2011-06-26 05:35:34 -03001140 f->frequency = (V4L2_TUNER_RADIO == f->type) ?
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -03001141 t->radio_freq : t->tv_freq;
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001142 }
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001143 return 0;
1144}
1145
Hans Verkuil338e9e12011-06-13 09:35:56 -03001146/**
1147 * tuner_g_tuner - Fill in tuner information
1148 * @sd: pointer to struct v4l2_subdev
1149 * @vt: pointer to struct v4l2_tuner
1150 *
1151 * At return, the structure vt will be filled with tuner information
1152 * if the tuner matches vt->type.
1153 * Note: vt->type should be initialized before calling it.
1154 * This is done by either video_ioctl2 or by the bridge driver.
1155 */
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001156static int tuner_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
1157{
1158 struct tuner *t = to_tuner(sd);
1159 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
1160 struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops;
1161
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -03001162 if (check_mode(t, vt->type) == -EINVAL)
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001163 return 0;
Hans Verkuil5ad339a2011-06-26 05:35:34 -03001164 if (vt->type == t->mode && analog_ops->get_afc)
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001165 vt->afc = analog_ops->get_afc(&t->fe);
Mauro Carvalho Chehaba1ad5ec2011-07-13 01:23:11 -03001166 if (t->mode != V4L2_TUNER_RADIO) {
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001167 vt->capability |= V4L2_TUNER_CAP_NORM;
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001168 vt->rangelow = tv_range[0] * 16;
1169 vt->rangehigh = tv_range[1] * 16;
1170 return 0;
1171 }
1172
1173 /* radio mode */
Hans Verkuil5ad339a2011-06-26 05:35:34 -03001174 if (vt->type == t->mode) {
1175 vt->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
1176 if (fe_tuner_ops->get_status) {
1177 u32 tuner_status;
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001178
Hans Verkuil5ad339a2011-06-26 05:35:34 -03001179 fe_tuner_ops->get_status(&t->fe, &tuner_status);
1180 vt->rxsubchans =
1181 (tuner_status & TUNER_STATUS_STEREO) ?
1182 V4L2_TUNER_SUB_STEREO :
1183 V4L2_TUNER_SUB_MONO;
1184 }
1185 if (analog_ops->has_signal)
1186 vt->signal = analog_ops->has_signal(&t->fe);
1187 vt->audmode = t->audmode;
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001188 }
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -03001189 vt->capability |= V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001190 vt->rangelow = radio_range[0] * 16000;
1191 vt->rangehigh = radio_range[1] * 16000;
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -03001192
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001193 return 0;
1194}
1195
Hans Verkuil338e9e12011-06-13 09:35:56 -03001196/**
1197 * tuner_s_tuner - Set the tuner's audio mode
1198 * @sd: pointer to struct v4l2_subdev
1199 * @vt: pointer to struct v4l2_tuner
1200 *
1201 * Sets the audio mode if the tuner matches vt->type.
1202 * Note: vt->type should be initialized before calling it.
1203 * This is done by either video_ioctl2 or by the bridge driver.
1204 */
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001205static int tuner_s_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
1206{
1207 struct tuner *t = to_tuner(sd);
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001208
Hans Verkuil4e4a31f2011-06-14 03:56:09 -03001209 if (set_mode(t, vt->type))
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001210 return 0;
1211
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -03001212 if (t->mode == V4L2_TUNER_RADIO)
1213 t->audmode = vt->audmode;
Hans Verkuil4e4a31f2011-06-14 03:56:09 -03001214 set_freq(t, 0);
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -03001215
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001216 return 0;
1217}
1218
1219static int tuner_log_status(struct v4l2_subdev *sd)
1220{
1221 struct tuner *t = to_tuner(sd);
1222 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
1223
1224 if (analog_ops->tuner_status)
1225 analog_ops->tuner_status(&t->fe);
1226 return 0;
1227}
1228
Jean Delvare21b48a72007-03-12 19:20:15 -03001229static int tuner_suspend(struct i2c_client *c, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230{
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001231 struct tuner *t = to_tuner(i2c_get_clientdata(c));
Mauro Carvalho Chehabe2f63d92011-02-04 11:15:21 -03001232 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
Hans Verkuil9dd659d2007-11-04 11:03:36 -03001234 tuner_dbg("suspend\n");
Mauro Carvalho Chehabe2f63d92011-02-04 11:15:21 -03001235
1236 if (!t->standby && analog_ops->standby)
1237 analog_ops->standby(&t->fe);
1238
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 return 0;
1240}
1241
Jean Delvare21b48a72007-03-12 19:20:15 -03001242static int tuner_resume(struct i2c_client *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243{
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001244 struct tuner *t = to_tuner(i2c_get_clientdata(c));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
Hans Verkuil9dd659d2007-11-04 11:03:36 -03001246 tuner_dbg("resume\n");
Mauro Carvalho Chehabe2f63d92011-02-04 11:15:21 -03001247
1248 if (!t->standby)
Hans Verkuil9bf0ef02011-06-13 09:21:56 -03001249 if (set_mode(t, t->mode) == 0)
Hans Verkuil4e4a31f2011-06-14 03:56:09 -03001250 set_freq(t, 0);
Mauro Carvalho Chehabe2f63d92011-02-04 11:15:21 -03001251
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 return 0;
1253}
1254
Hans Verkuil75b4c262009-04-01 03:32:22 -03001255static int tuner_command(struct i2c_client *client, unsigned cmd, void *arg)
1256{
1257 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1258
1259 /* TUNER_SET_CONFIG is still called by tuner-simple.c, so we have
1260 to handle it here.
1261 There must be a better way of doing this... */
1262 switch (cmd) {
1263 case TUNER_SET_CONFIG:
1264 return tuner_s_config(sd, arg);
1265 }
1266 return -ENOIOCTLCMD;
1267}
1268
Mauro Carvalho Chehaba2894e32011-02-15 10:15:19 -02001269/*
1270 * Callback structs
1271 */
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001272
1273static const struct v4l2_subdev_core_ops tuner_core_ops = {
1274 .log_status = tuner_log_status,
Hans Verkuilf41737e2009-04-01 03:52:39 -03001275 .s_std = tuner_s_std,
Laurent Pinchart622b8282009-10-05 10:48:17 -03001276 .s_power = tuner_s_power,
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001277};
1278
1279static const struct v4l2_subdev_tuner_ops tuner_tuner_ops = {
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001280 .s_radio = tuner_s_radio,
1281 .g_tuner = tuner_g_tuner,
1282 .s_tuner = tuner_s_tuner,
1283 .s_frequency = tuner_s_frequency,
1284 .g_frequency = tuner_g_frequency,
1285 .s_type_addr = tuner_s_type_addr,
1286 .s_config = tuner_s_config,
1287};
1288
1289static const struct v4l2_subdev_ops tuner_ops = {
1290 .core = &tuner_core_ops,
1291 .tuner = &tuner_tuner_ops,
1292};
1293
Mauro Carvalho Chehaba2894e32011-02-15 10:15:19 -02001294/*
1295 * I2C structs and module init functions
1296 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
Jean Delvareaf294862008-05-18 20:49:40 +02001298static const struct i2c_device_id tuner_id[] = {
1299 { "tuner", }, /* autodetect */
1300 { }
1301};
1302MODULE_DEVICE_TABLE(i2c, tuner_id);
1303
Hans Verkuil02a20982010-09-15 15:36:23 -03001304static struct i2c_driver tuner_driver = {
1305 .driver = {
1306 .owner = THIS_MODULE,
1307 .name = "tuner",
1308 },
1309 .probe = tuner_probe,
1310 .remove = tuner_remove,
1311 .command = tuner_command,
1312 .suspend = tuner_suspend,
1313 .resume = tuner_resume,
1314 .id_table = tuner_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315};
1316
Hans Verkuil02a20982010-09-15 15:36:23 -03001317static __init int init_tuner(void)
1318{
1319 return i2c_add_driver(&tuner_driver);
1320}
1321
1322static __exit void exit_tuner(void)
1323{
1324 i2c_del_driver(&tuner_driver);
1325}
1326
1327module_init(init_tuner);
1328module_exit(exit_tuner);
1329
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -03001330MODULE_DESCRIPTION("device driver for various TV and TV+FM radio tuners");
1331MODULE_AUTHOR("Ralph Metzler, Gerd Knorr, Gunther Mayer");
1332MODULE_LICENSE("GPL");