blob: 2c7fe18cedeb939c5926ca4c80847210c10ba806 [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#define UNSET (-1U)
44
Mauro Carvalho Chehab7d275bf2011-02-04 11:28:00 -030045#define PREFIX (t->i2c->driver->driver.name)
Michael Krufky241020d2007-10-30 09:46:10 -030046
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -030047/*
48 * Driver modprobe parameters
49 */
50
51/* insmod options used at init time => read/only */
52static unsigned int addr;
53static unsigned int no_autodetect;
54static unsigned int show_i2c;
55
56module_param(addr, int, 0444);
57module_param(no_autodetect, int, 0444);
58module_param(show_i2c, int, 0444);
59
60/* insmod options used at runtime => read/write */
61static int tuner_debug;
62static unsigned int tv_range[2] = { 44, 958 };
63static unsigned int radio_range[2] = { 65, 108 };
64static char pal[] = "--";
65static char secam[] = "--";
66static char ntsc[] = "-";
67
Mauro Carvalho Chehab7d275bf2011-02-04 11:28:00 -030068module_param_named(debug, tuner_debug, int, 0644);
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -030069module_param_array(tv_range, int, NULL, 0644);
70module_param_array(radio_range, int, NULL, 0644);
71module_param_string(pal, pal, sizeof(pal), 0644);
72module_param_string(secam, secam, sizeof(secam), 0644);
73module_param_string(ntsc, ntsc, sizeof(ntsc), 0644);
74
75/*
76 * Static vars
77 */
78
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -030079static LIST_HEAD(tuner_list);
Mauro Carvalho Chehaba2894e32011-02-15 10:15:19 -020080static const struct v4l2_subdev_ops tuner_ops;
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -030081
82/*
83 * Debug macros
84 */
85
86#define tuner_warn(fmt, arg...) do { \
87 printk(KERN_WARNING "%s %d-%04x: " fmt, PREFIX, \
88 i2c_adapter_id(t->i2c->adapter), \
89 t->i2c->addr, ##arg); \
90 } while (0)
91
92#define tuner_info(fmt, arg...) do { \
93 printk(KERN_INFO "%s %d-%04x: " fmt, PREFIX, \
94 i2c_adapter_id(t->i2c->adapter), \
95 t->i2c->addr, ##arg); \
96 } while (0)
97
98#define tuner_err(fmt, arg...) do { \
99 printk(KERN_ERR "%s %d-%04x: " fmt, PREFIX, \
100 i2c_adapter_id(t->i2c->adapter), \
101 t->i2c->addr, ##arg); \
102 } while (0)
103
104#define tuner_dbg(fmt, arg...) do { \
105 if (tuner_debug) \
106 printk(KERN_DEBUG "%s %d-%04x: " fmt, PREFIX, \
107 i2c_adapter_id(t->i2c->adapter), \
108 t->i2c->addr, ##arg); \
109 } while (0)
110
111/*
112 * Internal struct used inside the driver
113 */
114
115struct tuner {
116 /* device */
117 struct dvb_frontend fe;
118 struct i2c_client *i2c;
119 struct v4l2_subdev sd;
120 struct list_head list;
121
122 /* keep track of the current settings */
123 v4l2_std_id std;
124 unsigned int tv_freq;
125 unsigned int radio_freq;
126 unsigned int audmode;
127
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -0300128 enum v4l2_tuner_type mode;
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300129 unsigned int mode_mask; /* Combination of allowable modes */
130
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -0300131 bool standby; /* Standby mode */
132
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300133 unsigned int type; /* chip type id */
134 unsigned int config;
135 const char *name;
136};
137
138/*
Mauro Carvalho Chehaba2894e32011-02-15 10:15:19 -0200139 * Function prototypes
140 */
141
142static void set_tv_freq(struct i2c_client *c, unsigned int freq);
143static void set_radio_freq(struct i2c_client *c, unsigned int freq);
144
145/*
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300146 * tuner attach/detach logic
147 */
148
Mauro Carvalho Chehab0ae79d92011-02-15 01:10:20 -0300149/* This macro allows us to probe dynamically, avoiding static links */
Mauro Carvalho Chehabff138172008-04-29 23:02:33 -0300150#ifdef CONFIG_MEDIA_ATTACH
Michael Krufkya07c8772008-04-29 03:54:19 -0300151#define tuner_symbol_probe(FUNCTION, ARGS...) ({ \
152 int __r = -EINVAL; \
153 typeof(&FUNCTION) __a = symbol_request(FUNCTION); \
154 if (__a) { \
155 __r = (int) __a(ARGS); \
Andrew Mortona1355e52008-04-30 11:40:17 -0300156 symbol_put(FUNCTION); \
Michael Krufkya07c8772008-04-29 03:54:19 -0300157 } else { \
158 printk(KERN_ERR "TUNER: Unable to find " \
159 "symbol "#FUNCTION"()\n"); \
160 } \
Michael Krufkya07c8772008-04-29 03:54:19 -0300161 __r; \
162})
163
164static void tuner_detach(struct dvb_frontend *fe)
165{
166 if (fe->ops.tuner_ops.release) {
167 fe->ops.tuner_ops.release(fe);
168 symbol_put_addr(fe->ops.tuner_ops.release);
169 }
170 if (fe->ops.analog_ops.release) {
171 fe->ops.analog_ops.release(fe);
172 symbol_put_addr(fe->ops.analog_ops.release);
173 }
174}
175#else
176#define tuner_symbol_probe(FUNCTION, ARGS...) ({ \
177 FUNCTION(ARGS); \
178})
179
180static void tuner_detach(struct dvb_frontend *fe)
181{
182 if (fe->ops.tuner_ops.release)
183 fe->ops.tuner_ops.release(fe);
184 if (fe->ops.analog_ops.release)
185 fe->ops.analog_ops.release(fe);
186}
187#endif
188
Michael Krufkyf7f427e2007-12-16 22:02:26 -0300189
Hans Verkuile8a4a9e2008-11-24 18:21:40 -0300190static inline struct tuner *to_tuner(struct v4l2_subdev *sd)
191{
192 return container_of(sd, struct tuner, sd);
193}
194
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300195/*
196 * struct analog_demod_ops callbacks
197 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Michael Krufkyc7919d52007-12-08 17:06:30 -0300199static void fe_set_params(struct dvb_frontend *fe,
200 struct analog_parameters *params)
Michael Krufkye18f9442007-08-21 01:25:48 -0300201{
Michael Krufky4e9154b2007-10-21 19:39:50 -0300202 struct dvb_tuner_ops *fe_tuner_ops = &fe->ops.tuner_ops;
203 struct tuner *t = fe->analog_demod_priv;
Michael Krufkye18f9442007-08-21 01:25:48 -0300204
Michael Krufkye18f9442007-08-21 01:25:48 -0300205 if (NULL == fe_tuner_ops->set_analog_params) {
206 tuner_warn("Tuner frontend module has no way to set freq\n");
207 return;
208 }
Michael Krufkyc7919d52007-12-08 17:06:30 -0300209 fe_tuner_ops->set_analog_params(fe, params);
Michael Krufkye18f9442007-08-21 01:25:48 -0300210}
211
Michael Krufky4e9154b2007-10-21 19:39:50 -0300212static void fe_standby(struct dvb_frontend *fe)
Michael Krufkye18f9442007-08-21 01:25:48 -0300213{
Michael Krufky4e9154b2007-10-21 19:39:50 -0300214 struct dvb_tuner_ops *fe_tuner_ops = &fe->ops.tuner_ops;
Michael Krufkye18f9442007-08-21 01:25:48 -0300215
216 if (fe_tuner_ops->sleep)
Michael Krufky4e9154b2007-10-21 19:39:50 -0300217 fe_tuner_ops->sleep(fe);
Michael Krufkye18f9442007-08-21 01:25:48 -0300218}
219
Michael Krufky4e9154b2007-10-21 19:39:50 -0300220static int fe_has_signal(struct dvb_frontend *fe)
Michael Krufky1f5ef192007-08-31 17:38:02 -0300221{
Michael Krufky14196832007-10-14 18:11:53 -0300222 u16 strength = 0;
Michael Krufky1f5ef192007-08-31 17:38:02 -0300223
Michael Krufky4e9154b2007-10-21 19:39:50 -0300224 if (fe->ops.tuner_ops.get_rf_strength)
225 fe->ops.tuner_ops.get_rf_strength(fe, &strength);
Michael Krufky1f5ef192007-08-31 17:38:02 -0300226
227 return strength;
228}
229
Michael Krufkyf1c9a282007-12-16 19:27:23 -0300230static int fe_set_config(struct dvb_frontend *fe, void *priv_cfg)
231{
232 struct dvb_tuner_ops *fe_tuner_ops = &fe->ops.tuner_ops;
233 struct tuner *t = fe->analog_demod_priv;
234
235 if (fe_tuner_ops->set_config)
236 return fe_tuner_ops->set_config(fe, priv_cfg);
237
238 tuner_warn("Tuner frontend module has no way to set config\n");
239
240 return 0;
241}
242
Michael Krufky4e9154b2007-10-21 19:39:50 -0300243static void tuner_status(struct dvb_frontend *fe);
Michael Krufky1dde7a42007-10-21 13:40:56 -0300244
Hans Verkuile8a4a9e2008-11-24 18:21:40 -0300245static struct analog_demod_ops tuner_analog_ops = {
Michael Krufkyc7919d52007-12-08 17:06:30 -0300246 .set_params = fe_set_params,
Michael Krufky1dde7a42007-10-21 13:40:56 -0300247 .standby = fe_standby,
Michael Krufky1dde7a42007-10-21 13:40:56 -0300248 .has_signal = fe_has_signal,
Michael Krufkyf1c9a282007-12-16 19:27:23 -0300249 .set_config = fe_set_config,
Michael Krufky1dde7a42007-10-21 13:40:56 -0300250 .tuner_status = tuner_status
251};
252
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300253/*
Mauro Carvalho Chehaba2894e32011-02-15 10:15:19 -0200254 * Functions to select between radio and TV and tuner probe functions
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300255 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Mauro Carvalho Chehaba2894e32011-02-15 10:15:19 -0200257/**
258 * set_type - Sets the tuner type for a given device
259 *
260 * @c: i2c_client descriptoy
261 * @type: type of the tuner (e. g. tuner number)
262 * @new_mode_mask: Indicates if tuner supports TV and/or Radio
263 * @new_config: an optional parameter ranging from 0-255 used by
264 a few tuners to adjust an internal parameter,
265 like LNA mode
266 * @tuner_callback: an optional function to be called when switching
267 * to analog mode
268 *
269 * This function applys the tuner config to tuner specified
270 * by tun_setup structure. It contains several per-tuner initialization "magic"
271 */
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700272static void set_type(struct i2c_client *c, unsigned int type,
Hartmut Hackmannde956c12007-04-27 12:31:12 -0300273 unsigned int new_mode_mask, unsigned int new_config,
Michael Krufkyd7cba042008-09-12 13:31:45 -0300274 int (*tuner_callback) (void *dev, int component, int cmd, int arg))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
Hans Verkuile8a4a9e2008-11-24 18:21:40 -0300276 struct tuner *t = to_tuner(i2c_get_clientdata(c));
Michael Krufkye18f9442007-08-21 01:25:48 -0300277 struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops;
Michael Krufkybc3e5c72007-12-21 11:18:32 -0300278 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700279 unsigned char buffer[4];
Michael Krufkyd6eef492009-10-24 16:42:16 -0300280 int tune_now = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700282 if (type == UNSET || type == TUNER_ABSENT) {
Mauro Carvalho Chehab7d275bf2011-02-04 11:28:00 -0300283 tuner_dbg("tuner 0x%02x: Tuner type absent\n", c->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 return;
285 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 t->type = type;
Michael Krufkye7ddcd92009-03-28 15:35:26 -0300288 /* prevent invalid config values */
Roel Kluinf14a2972009-10-23 07:59:42 -0300289 t->config = new_config < 256 ? new_config : 0;
Hartmut Hackmanncfeb8832007-04-27 12:31:17 -0300290 if (tuner_callback != NULL) {
291 tuner_dbg("defining GPIO callback\n");
Michael Krufkyd7cba042008-09-12 13:31:45 -0300292 t->fe.callback = tuner_callback;
Hartmut Hackmanncfeb8832007-04-27 12:31:17 -0300293 }
Hartmut Hackmann80f90fb2007-04-27 12:31:18 -0300294
Michael Krufkyb2083192007-05-29 22:54:06 -0300295 /* discard private data, in case set_type() was previously called */
Michael Krufkya07c8772008-04-29 03:54:19 -0300296 tuner_detach(&t->fe);
297 t->fe.analog_demod_priv = NULL;
Michael Krufkybe2b85a2007-06-04 14:40:27 -0300298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 switch (t->type) {
300 case TUNER_MT2032:
Mauro Carvalho Chehab09fee5f2008-04-30 15:29:57 -0300301 if (!dvb_attach(microtune_attach,
302 &t->fe, t->i2c->adapter, t->i2c->addr))
303 goto attach_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 break;
305 case TUNER_PHILIPS_TDA8290:
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300306 {
Mauro Carvalho Chehab09fee5f2008-04-30 15:29:57 -0300307 struct tda829x_config cfg = {
308 .lna_cfg = t->config,
Mauro Carvalho Chehab09fee5f2008-04-30 15:29:57 -0300309 };
310 if (!dvb_attach(tda829x_attach, &t->fe, t->i2c->adapter,
311 t->i2c->addr, &cfg))
312 goto attach_failed;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300313 break;
314 }
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700315 case TUNER_TEA5767:
Michael Krufkya07c8772008-04-29 03:54:19 -0300316 if (!dvb_attach(tea5767_attach, &t->fe,
317 t->i2c->adapter, t->i2c->addr))
Mauro Carvalho Chehabb9ef6bb2008-04-26 11:29:34 -0300318 goto attach_failed;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700319 t->mode_mask = T_RADIO;
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700320 break;
Mauro Carvalho Chehab8573a9e2007-04-08 01:09:11 -0300321 case TUNER_TEA5761:
Michael Krufkya07c8772008-04-29 03:54:19 -0300322 if (!dvb_attach(tea5761_attach, &t->fe,
323 t->i2c->adapter, t->i2c->addr))
Mauro Carvalho Chehabb9ef6bb2008-04-26 11:29:34 -0300324 goto attach_failed;
Mauro Carvalho Chehab8573a9e2007-04-08 01:09:11 -0300325 t->mode_mask = T_RADIO;
326 break;
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700327 case TUNER_PHILIPS_FMD1216ME_MK3:
328 buffer[0] = 0x0b;
329 buffer[1] = 0xdc;
330 buffer[2] = 0x9c;
331 buffer[3] = 0x60;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700332 i2c_master_send(c, buffer, 4);
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700333 mdelay(1);
334 buffer[2] = 0x86;
335 buffer[3] = 0x54;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700336 i2c_master_send(c, buffer, 4);
Michael Krufkya07c8772008-04-29 03:54:19 -0300337 if (!dvb_attach(simple_tuner_attach, &t->fe,
338 t->i2c->adapter, t->i2c->addr, t->type))
Mauro Carvalho Chehabb9ef6bb2008-04-26 11:29:34 -0300339 goto attach_failed;
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700340 break;
Hartmut Hackmann93df3412005-11-08 21:36:31 -0800341 case TUNER_PHILIPS_TD1316:
342 buffer[0] = 0x0b;
343 buffer[1] = 0xdc;
344 buffer[2] = 0x86;
345 buffer[3] = 0xa4;
Michael Krufkya07c8772008-04-29 03:54:19 -0300346 i2c_master_send(c, buffer, 4);
347 if (!dvb_attach(simple_tuner_attach, &t->fe,
348 t->i2c->adapter, t->i2c->addr, t->type))
Mauro Carvalho Chehabb9ef6bb2008-04-26 11:29:34 -0300349 goto attach_failed;
Markus Rechbergerac272ed2006-01-23 17:11:09 -0200350 break;
Mauro Carvalho Chehab690c5442007-10-29 11:33:18 -0300351 case TUNER_XC2028:
352 {
Michel Ludwiga37b4c92007-11-16 07:46:14 -0300353 struct xc2028_config cfg = {
354 .i2c_adap = t->i2c->adapter,
355 .i2c_addr = t->i2c->addr,
Michel Ludwiga37b4c92007-11-16 07:46:14 -0300356 };
Michael Krufkya07c8772008-04-29 03:54:19 -0300357 if (!dvb_attach(xc2028_attach, &t->fe, &cfg))
Mauro Carvalho Chehabb9ef6bb2008-04-26 11:29:34 -0300358 goto attach_failed;
Michael Krufkyd6eef492009-10-24 16:42:16 -0300359 tune_now = 0;
Mauro Carvalho Chehab690c5442007-10-29 11:33:18 -0300360 break;
361 }
Mauro Carvalho Chehab15396232006-06-23 16:13:56 -0300362 case TUNER_TDA9887:
Mauro Carvalho Chehab09fee5f2008-04-30 15:29:57 -0300363 if (!dvb_attach(tda9887_attach,
364 &t->fe, t->i2c->adapter, t->i2c->addr))
365 goto attach_failed;
Mauro Carvalho Chehab15396232006-06-23 16:13:56 -0300366 break;
Steven Toth27c685a2008-01-05 16:50:14 -0300367 case TUNER_XC5000:
Mauro Carvalho Chehabb9ef6bb2008-04-26 11:29:34 -0300368 {
Mauro Carvalho Chehab900f7342011-02-04 12:56:39 -0300369 struct xc5000_config xc5000_cfg = {
370 .i2c_address = t->i2c->addr,
371 /* if_khz will be set at dvb_attach() */
372 .if_khz = 0,
373 };
374
Michael Krufkya07c8772008-04-29 03:54:19 -0300375 if (!dvb_attach(xc5000_attach,
Michael Krufky30650962008-09-06 14:56:58 -0300376 &t->fe, t->i2c->adapter, &xc5000_cfg))
Mauro Carvalho Chehabb9ef6bb2008-04-26 11:29:34 -0300377 goto attach_failed;
Michael Krufkyd6eef492009-10-24 16:42:16 -0300378 tune_now = 0;
Steven Toth27c685a2008-01-05 16:50:14 -0300379 break;
Mauro Carvalho Chehabb9ef6bb2008-04-26 11:29:34 -0300380 }
Michael Krufky93463892009-09-15 23:04:18 -0300381 case TUNER_NXP_TDA18271:
382 {
383 struct tda18271_config cfg = {
384 .config = t->config,
Mauro Carvalho Chehabe350d442010-09-26 22:58:28 -0300385 .small_i2c = TDA18271_03_BYTE_CHUNK_INIT,
Michael Krufky93463892009-09-15 23:04:18 -0300386 };
387
388 if (!dvb_attach(tda18271_attach, &t->fe, t->i2c->addr,
389 t->i2c->adapter, &cfg))
390 goto attach_failed;
Michael Krufkyd6eef492009-10-24 16:42:16 -0300391 tune_now = 0;
Michael Krufky93463892009-09-15 23:04:18 -0300392 break;
393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 default:
Michael Krufkya07c8772008-04-29 03:54:19 -0300395 if (!dvb_attach(simple_tuner_attach, &t->fe,
396 t->i2c->adapter, t->i2c->addr, t->type))
Mauro Carvalho Chehabb9ef6bb2008-04-26 11:29:34 -0300397 goto attach_failed;
398
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 break;
400 }
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700401
Michael Krufkybc3e5c72007-12-21 11:18:32 -0300402 if ((NULL == analog_ops->set_params) &&
Michael Krufky1dde7a42007-10-21 13:40:56 -0300403 (fe_tuner_ops->set_analog_params)) {
Michael Krufkya07c8772008-04-29 03:54:19 -0300404
Michael Krufky7271e602008-05-26 16:08:40 +0200405 t->name = fe_tuner_ops->info.name;
Michael Krufkye18f9442007-08-21 01:25:48 -0300406
Michael Krufky4e9154b2007-10-21 19:39:50 -0300407 t->fe.analog_demod_priv = t;
Hans Verkuile8a4a9e2008-11-24 18:21:40 -0300408 memcpy(analog_ops, &tuner_analog_ops,
Michael Krufkybc3e5c72007-12-21 11:18:32 -0300409 sizeof(struct analog_demod_ops));
Michael Krufkya07c8772008-04-29 03:54:19 -0300410
Michael Krufkya55db8c2007-12-09 13:52:51 -0300411 } else {
Michael Krufky7271e602008-05-26 16:08:40 +0200412 t->name = analog_ops->info.name;
Michael Krufkye18f9442007-08-21 01:25:48 -0300413 }
414
Michael Krufky7271e602008-05-26 16:08:40 +0200415 tuner_dbg("type set to %s\n", t->name);
Michael Krufkye18f9442007-08-21 01:25:48 -0300416
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -0300417 t->mode_mask = new_mode_mask;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700418
Michael Krufkyd6eef492009-10-24 16:42:16 -0300419 /* Some tuners require more initialization setup before use,
420 such as firmware download or device calibration.
Mauro Carvalho Chehabb9ef6bb2008-04-26 11:29:34 -0300421 trying to set a frequency here will just fail
422 FIXME: better to move set_freq to the tuner code. This is needed
423 on analog tuners for PLL to properly work
424 */
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300425 if (tune_now) {
426 if (V4L2_TUNER_RADIO == t->mode)
427 set_radio_freq(c, t->radio_freq);
428 else
429 set_tv_freq(c, t->tv_freq);
430 }
Mauro Carvalho Chehabb9ef6bb2008-04-26 11:29:34 -0300431
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700432 tuner_dbg("%s %s I2C addr 0x%02x with type %d used for 0x%02x\n",
Laurent Riffard604f28e2005-11-26 20:43:39 +0100433 c->adapter->name, c->driver->driver.name, c->addr << 1, type,
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700434 t->mode_mask);
Mauro Carvalho Chehabb9ef6bb2008-04-26 11:29:34 -0300435 return;
436
437attach_failed:
438 tuner_dbg("Tuner attach for type = %d failed.\n", t->type);
439 t->type = TUNER_ABSENT;
Mauro Carvalho Chehabb9ef6bb2008-04-26 11:29:34 -0300440
441 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442}
443
Mauro Carvalho Chehab0ae79d92011-02-15 01:10:20 -0300444/**
445 * tuner_s_type_addr - Sets the tuner type for a device
446 *
447 * @sd: subdev descriptor
448 * @tun_setup: type to be associated to a given tuner i2c address
449 *
450 * This function applys the tuner config to tuner specified
451 * by tun_setup structure.
452 * If tuner I2C address is UNSET, then it will only set the device
453 * if the tuner supports the mode specified in the call.
454 * If the address is specified, the change will be applied only if
455 * tuner I2C address matches.
456 * The call can change the tuner number and the tuner mode.
457 */
Mauro Carvalho Chehaba34ec5f2011-02-15 00:55:18 -0300458static int tuner_s_type_addr(struct v4l2_subdev *sd,
459 struct tuner_setup *tun_setup)
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700460{
Mauro Carvalho Chehaba34ec5f2011-02-15 00:55:18 -0300461 struct tuner *t = to_tuner(sd);
462 struct i2c_client *c = v4l2_get_subdevdata(sd);
463
464 tuner_dbg("Calling set_type_addr for type=%d, addr=0x%02x, mode=0x%02x, config=0x%02x\n",
465 tun_setup->type,
466 tun_setup->addr,
467 tun_setup->mode_mask,
468 tun_setup->config);
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700469
Mauro Carvalho Chehab7d275bf2011-02-04 11:28:00 -0300470 if ((t->type == UNSET && ((tun_setup->addr == ADDR_UNSET) &&
471 (t->mode_mask & tun_setup->mode_mask))) ||
472 (tun_setup->addr == c->addr)) {
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -0300473 set_type(c, tun_setup->type, tun_setup->mode_mask,
474 tun_setup->config, tun_setup->tuner_callback);
Mauro Carvalho Chehabb9ef6bb2008-04-26 11:29:34 -0300475 } else
476 tuner_dbg("set addr discarded for type %i, mask %x. "
477 "Asked to change tuner at addr 0x%02x, with mask %x\n",
478 t->type, t->mode_mask,
479 tun_setup->addr, tun_setup->mode_mask);
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700480
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300481 return 0;
482}
483
Mauro Carvalho Chehaba2894e32011-02-15 10:15:19 -0200484/**
485 * tuner_s_config - Sets tuner configuration
486 *
487 * @sd: subdev descriptor
488 * @cfg: tuner configuration
489 *
490 * Calls tuner set_config() private function to set some tuner-internal
491 * parameters
492 */
Mauro Carvalho Chehab7d275bf2011-02-04 11:28:00 -0300493static int tuner_s_config(struct v4l2_subdev *sd,
494 const struct v4l2_priv_tun_config *cfg)
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300495{
496 struct tuner *t = to_tuner(sd);
497 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
498
499 if (t->type != cfg->tuner)
500 return 0;
501
502 if (analog_ops->set_config) {
503 analog_ops->set_config(&t->fe, cfg->priv);
504 return 0;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700505 }
Mauro Carvalho Chehab793cf9e2005-09-09 13:03:37 -0700506
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300507 tuner_dbg("Tuner frontend module has no way to set config\n");
Mauro Carvalho Chehab793cf9e2005-09-09 13:03:37 -0700508 return 0;
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700509}
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700510
Mauro Carvalho Chehaba2894e32011-02-15 10:15:19 -0200511/**
512 * tuner_lookup - Seek for tuner adapters
513 *
514 * @adap: i2c_adapter struct
515 * @radio: pointer to be filled if the adapter is radio
516 * @tv: pointer to be filled if the adapter is TV
517 *
518 * Search for existing radio and/or TV tuners on the given I2C adapter,
519 * discarding demod-only adapters (tda9887).
520 *
521 * Note that when this function is called from tuner_probe you can be
522 * certain no other devices will be added/deleted at the same time, I2C
523 * core protects against that.
524 */
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300525static void tuner_lookup(struct i2c_adapter *adap,
526 struct tuner **radio, struct tuner **tv)
527{
528 struct tuner *pos;
529
530 *radio = NULL;
531 *tv = NULL;
532
533 list_for_each_entry(pos, &tuner_list, list) {
534 int mode_mask;
535
536 if (pos->i2c->adapter != adap ||
537 strcmp(pos->i2c->driver->driver.name, "tuner"))
538 continue;
539
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -0300540 mode_mask = pos->mode_mask;
541 pos->standby = 1;
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300542 if (*radio == NULL && mode_mask == T_RADIO)
543 *radio = pos;
544 /* Note: currently TDA9887 is the only demod-only
545 device. If other devices appear then we need to
546 make this test more general. */
547 else if (*tv == NULL && pos->type != TUNER_TDA9887 &&
Mauro Carvalho Chehabad020dc2011-02-15 09:30:50 -0200548 (pos->mode_mask & T_ANALOG_TV))
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300549 *tv = pos;
550 }
551}
552
Mauro Carvalho Chehaba2894e32011-02-15 10:15:19 -0200553/**
554 *tuner_probe - Probes the existing tuners on an I2C bus
555 *
556 * @client: i2c_client descriptor
557 * @id: not used
558 *
559 * This routine probes for tuners at the expected I2C addresses. On most
560 * cases, if a device answers to a given I2C address, it assumes that the
561 * device is a tuner. On a few cases, however, an additional logic is needed
562 * to double check if the device is really a tuner, or to identify the tuner
563 * type, like on tea5767/5761 devices.
564 *
565 * During client attach, set_type is called by adapter's attach_inform callback.
566 * set_type must then be completed by tuner_probe.
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300567 */
568static int tuner_probe(struct i2c_client *client,
569 const struct i2c_device_id *id)
570{
571 struct tuner *t;
572 struct tuner *radio;
573 struct tuner *tv;
574
575 t = kzalloc(sizeof(struct tuner), GFP_KERNEL);
576 if (NULL == t)
577 return -ENOMEM;
578 v4l2_i2c_subdev_init(&t->sd, client, &tuner_ops);
579 t->i2c = client;
580 t->name = "(tuner unset)";
581 t->type = UNSET;
582 t->audmode = V4L2_TUNER_MODE_STEREO;
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -0300583 t->standby = 1;
584 t->radio_freq = 87.5 * 16000; /* Initial freq range */
585 t->tv_freq = 400 * 16; /* Sets freq to VHF High - needed for some PLL's to properly start */
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300586
587 if (show_i2c) {
588 unsigned char buffer[16];
589 int i, rc;
590
591 memset(buffer, 0, sizeof(buffer));
592 rc = i2c_master_recv(client, buffer, sizeof(buffer));
593 tuner_info("I2C RECV = ");
594 for (i = 0; i < rc; i++)
595 printk(KERN_CONT "%02x ", buffer[i]);
596 printk("\n");
597 }
598
599 /* autodetection code based on the i2c addr */
600 if (!no_autodetect) {
601 switch (client->addr) {
602 case 0x10:
603 if (tuner_symbol_probe(tea5761_autodetection,
604 t->i2c->adapter,
605 t->i2c->addr) >= 0) {
606 t->type = TUNER_TEA5761;
607 t->mode_mask = T_RADIO;
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300608 tuner_lookup(t->i2c->adapter, &radio, &tv);
609 if (tv)
610 tv->mode_mask &= ~T_RADIO;
611
612 goto register_client;
613 }
614 kfree(t);
615 return -ENODEV;
616 case 0x42:
617 case 0x43:
618 case 0x4a:
619 case 0x4b:
620 /* If chip is not tda8290, don't register.
621 since it can be tda9887*/
622 if (tuner_symbol_probe(tda829x_probe, t->i2c->adapter,
623 t->i2c->addr) >= 0) {
624 tuner_dbg("tda829x detected\n");
625 } else {
626 /* Default is being tda9887 */
627 t->type = TUNER_TDA9887;
Mauro Carvalho Chehabad020dc2011-02-15 09:30:50 -0200628 t->mode_mask = T_RADIO | T_ANALOG_TV;
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300629 goto register_client;
630 }
631 break;
632 case 0x60:
633 if (tuner_symbol_probe(tea5767_autodetection,
634 t->i2c->adapter, t->i2c->addr)
635 >= 0) {
636 t->type = TUNER_TEA5767;
637 t->mode_mask = T_RADIO;
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300638 /* Sets freq to FM range */
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300639 tuner_lookup(t->i2c->adapter, &radio, &tv);
640 if (tv)
641 tv->mode_mask &= ~T_RADIO;
642
643 goto register_client;
644 }
645 break;
646 }
647 }
648
649 /* Initializes only the first TV tuner on this adapter. Why only the
650 first? Because there are some devices (notably the ones with TI
651 tuners) that have more than one i2c address for the *same* device.
652 Experience shows that, except for just one case, the first
653 address is the right one. The exception is a Russian tuner
654 (ACORP_Y878F). So, the desired behavior is just to enable the
655 first found TV tuner. */
656 tuner_lookup(t->i2c->adapter, &radio, &tv);
657 if (tv == NULL) {
Mauro Carvalho Chehabad020dc2011-02-15 09:30:50 -0200658 t->mode_mask = T_ANALOG_TV;
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300659 if (radio == NULL)
660 t->mode_mask |= T_RADIO;
661 tuner_dbg("Setting mode_mask to 0x%02x\n", t->mode_mask);
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300662 }
663
664 /* Should be just before return */
665register_client:
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300666 /* Sets a default mode */
Mauro Carvalho Chehab7d275bf2011-02-04 11:28:00 -0300667 if (t->mode_mask & T_ANALOG_TV)
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300668 t->mode = V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehab7d275bf2011-02-04 11:28:00 -0300669 else
Mauro Carvalho Chehabad020dc2011-02-15 09:30:50 -0200670 t->mode = V4L2_TUNER_RADIO;
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300671 set_type(client, t->type, t->mode_mask, t->config, t->fe.callback);
672 list_add_tail(&t->list, &tuner_list);
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -0300673
Mauro Carvalho Chehabad020dc2011-02-15 09:30:50 -0200674 tuner_info("Tuner %d found with type(s)%s%s.\n",
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -0300675 t->type,
Mauro Carvalho Chehabad020dc2011-02-15 09:30:50 -0200676 t->mode_mask & T_RADIO ? " Radio" : "",
677 t->mode_mask & T_ANALOG_TV ? " TV" : "");
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300678 return 0;
679}
680
Mauro Carvalho Chehaba2894e32011-02-15 10:15:19 -0200681/**
682 * tuner_remove - detaches a tuner
683 *
684 * @client: i2c_client descriptor
685 */
686
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300687static int tuner_remove(struct i2c_client *client)
688{
689 struct tuner *t = to_tuner(i2c_get_clientdata(client));
690
691 v4l2_device_unregister_subdev(&t->sd);
692 tuner_detach(&t->fe);
693 t->fe.analog_demod_priv = NULL;
694
695 list_del(&t->list);
696 kfree(t);
697 return 0;
698}
699
700/*
701 * Functions that are specific for TV mode
702 */
703
Mauro Carvalho Chehaba2894e32011-02-15 10:15:19 -0200704/**
705 * set_tv_freq - Set tuner frequency, freq in Units of 62.5 kHz = 1/16MHz
706 *
707 * @c: i2c_client descriptor
708 * @freq: frequency
709 */
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300710static void set_tv_freq(struct i2c_client *c, unsigned int freq)
711{
712 struct tuner *t = to_tuner(i2c_get_clientdata(c));
713 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
714
715 struct analog_parameters params = {
716 .mode = t->mode,
717 .audmode = t->audmode,
718 .std = t->std
719 };
720
721 if (t->type == UNSET) {
Mauro Carvalho Chehab7d275bf2011-02-04 11:28:00 -0300722 tuner_warn("tuner type not set\n");
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300723 return;
724 }
725 if (NULL == analog_ops->set_params) {
Mauro Carvalho Chehab7d275bf2011-02-04 11:28:00 -0300726 tuner_warn("Tuner has no way to set tv freq\n");
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300727 return;
728 }
729 if (freq < tv_range[0] * 16 || freq > tv_range[1] * 16) {
Mauro Carvalho Chehab7d275bf2011-02-04 11:28:00 -0300730 tuner_dbg("TV freq (%d.%02d) out of range (%d-%d)\n",
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300731 freq / 16, freq % 16 * 100 / 16, tv_range[0],
732 tv_range[1]);
733 /* V4L2 spec: if the freq is not possible then the closest
734 possible value should be selected */
735 if (freq < tv_range[0] * 16)
736 freq = tv_range[0] * 16;
737 else
738 freq = tv_range[1] * 16;
739 }
740 params.frequency = freq;
741 tuner_dbg("tv freq set to %d.%02d\n",
742 freq / 16, freq % 16 * 100 / 16);
743 t->tv_freq = freq;
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -0300744 t->standby = false;
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300745
746 analog_ops->set_params(&t->fe, &params);
747}
748
Mauro Carvalho Chehaba2894e32011-02-15 10:15:19 -0200749/**
750 * tuner_fixup_std - force a given video standard variant
751 *
752 * @t: tuner internal struct
753 *
754 * A few devices or drivers have problem to detect some standard variations.
755 * On other operational systems, the drivers generally have a per-country
756 * code, and some logic to apply per-country hacks. V4L2 API doesn't provide
757 * such hacks. Instead, it relies on a proper video standard selection from
758 * the userspace application. However, as some apps are buggy, not allowing
759 * to distinguish all video standard variations, a modprobe parameter can
760 * be used to force a video standard match.
761 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762static int tuner_fixup_std(struct tuner *t)
763{
764 if ((t->std & V4L2_STD_PAL) == V4L2_STD_PAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 switch (pal[0]) {
Hans Verkuile71ced12006-12-11 15:51:43 -0300766 case '6':
Mauro Carvalho Chehab7d275bf2011-02-04 11:28:00 -0300767 tuner_dbg("insmod fixup: PAL => PAL-60\n");
Hans Verkuile71ced12006-12-11 15:51:43 -0300768 t->std = V4L2_STD_PAL_60;
769 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 case 'b':
771 case 'B':
772 case 'g':
773 case 'G':
Mauro Carvalho Chehab7d275bf2011-02-04 11:28:00 -0300774 tuner_dbg("insmod fixup: PAL => PAL-BG\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 t->std = V4L2_STD_PAL_BG;
776 break;
777 case 'i':
778 case 'I':
Mauro Carvalho Chehab7d275bf2011-02-04 11:28:00 -0300779 tuner_dbg("insmod fixup: PAL => PAL-I\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 t->std = V4L2_STD_PAL_I;
781 break;
782 case 'd':
783 case 'D':
784 case 'k':
785 case 'K':
Mauro Carvalho Chehab7d275bf2011-02-04 11:28:00 -0300786 tuner_dbg("insmod fixup: PAL => PAL-DK\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 t->std = V4L2_STD_PAL_DK;
788 break;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700789 case 'M':
790 case 'm':
Mauro Carvalho Chehab7d275bf2011-02-04 11:28:00 -0300791 tuner_dbg("insmod fixup: PAL => PAL-M\n");
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700792 t->std = V4L2_STD_PAL_M;
793 break;
794 case 'N':
795 case 'n':
Mauro Carvalho Chehab7e578192006-01-09 15:25:27 -0200796 if (pal[1] == 'c' || pal[1] == 'C') {
797 tuner_dbg("insmod fixup: PAL => PAL-Nc\n");
798 t->std = V4L2_STD_PAL_Nc;
799 } else {
Mauro Carvalho Chehab7d275bf2011-02-04 11:28:00 -0300800 tuner_dbg("insmod fixup: PAL => PAL-N\n");
Mauro Carvalho Chehab7e578192006-01-09 15:25:27 -0200801 t->std = V4L2_STD_PAL_N;
802 }
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700803 break;
Mauro Carvalho Chehab21d4df32005-09-09 13:03:59 -0700804 case '-':
805 /* default parameter, do nothing */
806 break;
807 default:
Mauro Carvalho Chehab7d275bf2011-02-04 11:28:00 -0300808 tuner_warn("pal= argument not recognised\n");
Mauro Carvalho Chehab21d4df32005-09-09 13:03:59 -0700809 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 }
811 }
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700812 if ((t->std & V4L2_STD_SECAM) == V4L2_STD_SECAM) {
813 switch (secam[0]) {
Mauro Carvalho Chehab7e578192006-01-09 15:25:27 -0200814 case 'b':
815 case 'B':
816 case 'g':
817 case 'G':
818 case 'h':
819 case 'H':
820 tuner_dbg("insmod fixup: SECAM => SECAM-BGH\n");
Mauro Carvalho Chehab7d275bf2011-02-04 11:28:00 -0300821 t->std = V4L2_STD_SECAM_B |
822 V4L2_STD_SECAM_G |
823 V4L2_STD_SECAM_H;
Mauro Carvalho Chehab7e578192006-01-09 15:25:27 -0200824 break;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700825 case 'd':
826 case 'D':
827 case 'k':
828 case 'K':
Mauro Carvalho Chehab7d275bf2011-02-04 11:28:00 -0300829 tuner_dbg("insmod fixup: SECAM => SECAM-DK\n");
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700830 t->std = V4L2_STD_SECAM_DK;
831 break;
832 case 'l':
833 case 'L':
Mauro Carvalho Chehab7d275bf2011-02-04 11:28:00 -0300834 if ((secam[1] == 'C') || (secam[1] == 'c')) {
835 tuner_dbg("insmod fixup: SECAM => SECAM-L'\n");
Mauro Carvalho Chehab800d3c62005-11-13 16:07:48 -0800836 t->std = V4L2_STD_SECAM_LC;
837 } else {
Mauro Carvalho Chehab7d275bf2011-02-04 11:28:00 -0300838 tuner_dbg("insmod fixup: SECAM => SECAM-L\n");
Mauro Carvalho Chehab800d3c62005-11-13 16:07:48 -0800839 t->std = V4L2_STD_SECAM_L;
840 }
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700841 break;
Mauro Carvalho Chehab21d4df32005-09-09 13:03:59 -0700842 case '-':
843 /* default parameter, do nothing */
844 break;
845 default:
Mauro Carvalho Chehab7d275bf2011-02-04 11:28:00 -0300846 tuner_warn("secam= argument not recognised\n");
Mauro Carvalho Chehab21d4df32005-09-09 13:03:59 -0700847 break;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700848 }
849 }
850
Mauro Carvalho Chehab7e578192006-01-09 15:25:27 -0200851 if ((t->std & V4L2_STD_NTSC) == V4L2_STD_NTSC) {
852 switch (ntsc[0]) {
853 case 'm':
854 case 'M':
855 tuner_dbg("insmod fixup: NTSC => NTSC-M\n");
856 t->std = V4L2_STD_NTSC_M;
857 break;
858 case 'j':
859 case 'J':
860 tuner_dbg("insmod fixup: NTSC => NTSC_M_JP\n");
861 t->std = V4L2_STD_NTSC_M_JP;
862 break;
Hans Verkuild97a11e2006-02-07 06:48:40 -0200863 case 'k':
864 case 'K':
865 tuner_dbg("insmod fixup: NTSC => NTSC_M_KR\n");
866 t->std = V4L2_STD_NTSC_M_KR;
867 break;
Mauro Carvalho Chehab7e578192006-01-09 15:25:27 -0200868 case '-':
869 /* default parameter, do nothing */
870 break;
871 default:
872 tuner_info("ntsc= argument not recognised\n");
873 break;
874 }
875 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 return 0;
877}
878
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300879/*
880 * Functions that are specific for Radio mode
881 */
882
Mauro Carvalho Chehaba2894e32011-02-15 10:15:19 -0200883/**
884 * set_radio_freq - Set tuner frequency, freq in Units of 62.5 Hz = 1/16kHz
885 *
886 * @c: i2c_client descriptor
887 * @freq: frequency
888 */
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300889static void set_radio_freq(struct i2c_client *c, unsigned int freq)
890{
891 struct tuner *t = to_tuner(i2c_get_clientdata(c));
892 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
893
894 struct analog_parameters params = {
895 .mode = t->mode,
896 .audmode = t->audmode,
897 .std = t->std
898 };
899
900 if (t->type == UNSET) {
Mauro Carvalho Chehab7d275bf2011-02-04 11:28:00 -0300901 tuner_warn("tuner type not set\n");
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300902 return;
903 }
904 if (NULL == analog_ops->set_params) {
Mauro Carvalho Chehab7d275bf2011-02-04 11:28:00 -0300905 tuner_warn("tuner has no way to set radio frequency\n");
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300906 return;
907 }
908 if (freq < radio_range[0] * 16000 || freq > radio_range[1] * 16000) {
Mauro Carvalho Chehab7d275bf2011-02-04 11:28:00 -0300909 tuner_dbg("radio freq (%d.%02d) out of range (%d-%d)\n",
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300910 freq / 16000, freq % 16000 * 100 / 16000,
911 radio_range[0], radio_range[1]);
912 /* V4L2 spec: if the freq is not possible then the closest
913 possible value should be selected */
914 if (freq < radio_range[0] * 16000)
915 freq = radio_range[0] * 16000;
916 else
917 freq = radio_range[1] * 16000;
918 }
919 params.frequency = freq;
920 tuner_dbg("radio freq set to %d.%02d\n",
921 freq / 16000, freq % 16000 * 100 / 16000);
922 t->radio_freq = freq;
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -0300923 t->standby = false;
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300924
925 analog_ops->set_params(&t->fe, &params);
926}
927
Mauro Carvalho Chehabe2d25a22011-02-04 10:09:07 -0300928/**
929 * check_mode - Verify if tuner supports the requested mode
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -0300930 * @t: a pointer to the module's internal struct_tuner
Mauro Carvalho Chehabe2d25a22011-02-04 10:09:07 -0300931 *
932 * This function checks if the tuner is capable of tuning analog TV,
933 * digital TV or radio, depending on what the caller wants. If the
934 * tuner can't support that mode, it returns -EINVAL. Otherwise, it
935 * returns 0.
936 * This function is needed for boards that have a separate tuner for
937 * radio (like devices with tea5767).
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300938 */
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -0300939static inline int check_mode(struct tuner *t, enum v4l2_tuner_type mode)
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300940{
Mauro Carvalho Chehab7d275bf2011-02-04 11:28:00 -0300941 if ((1 << mode & t->mode_mask) == 0)
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300942 return -EINVAL;
Mauro Carvalho Chehab7d275bf2011-02-04 11:28:00 -0300943
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300944 return 0;
945}
946
Mauro Carvalho Chehabe2d25a22011-02-04 10:09:07 -0300947/**
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -0300948 * set_mode_freq - Switch tuner to other mode.
949 * @client: struct i2c_client pointer
950 * @t: a pointer to the module's internal struct_tuner
951 * @mode: enum v4l2_type (radio or TV)
952 * @freq: frequency to set (0 means to use the previous one)
Mauro Carvalho Chehabe2d25a22011-02-04 10:09:07 -0300953 *
954 * If tuner doesn't support the needed mode (radio or TV), prints a
955 * debug message and returns -EINVAL, changing internal state to T_STANDBY.
956 * Otherwise, changes the state and sets frequency to the last value, if
957 * the tuner can sleep or if it supports both Radio and TV.
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300958 */
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -0300959static int set_mode_freq(struct i2c_client *client, struct tuner *t,
Mauro Carvalho Chehab7d275bf2011-02-04 11:28:00 -0300960 enum v4l2_tuner_type mode, unsigned int freq)
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300961{
962 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
963
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -0300964 if (mode != t->mode) {
965 if (check_mode(t, mode) == -EINVAL) {
966 tuner_dbg("Tuner doesn't support mode %d. "
967 "Putting tuner to sleep\n", mode);
968 t->standby = true;
969 if (analog_ops->standby)
970 analog_ops->standby(&t->fe);
971 return -EINVAL;
972 }
973 t->mode = mode;
974 tuner_dbg("Changing to mode %d\n", mode);
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300975 }
Mauro Carvalho Chehabe2d25a22011-02-04 10:09:07 -0300976 if (t->mode == V4L2_TUNER_RADIO) {
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -0300977 if (freq)
978 t->radio_freq = freq;
979 set_radio_freq(client, t->radio_freq);
Mauro Carvalho Chehabe2d25a22011-02-04 10:09:07 -0300980 } else {
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -0300981 if (freq)
982 t->tv_freq = freq;
983 set_tv_freq(client, t->tv_freq);
Mauro Carvalho Chehabe2d25a22011-02-04 10:09:07 -0300984 }
985
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300986 return 0;
987}
988
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -0300989/**
990 * tuner_status - Dumps the current tuner status at dmesg
991 * @fe: pointer to struct dvb_frontend
992 *
993 * This callback is used only for driver debug purposes, answering to
994 * VIDIOC_LOG_STATUS. No changes should happen on this call.
995 */
Michael Krufky4e9154b2007-10-21 19:39:50 -0300996static void tuner_status(struct dvb_frontend *fe)
Mauro Carvalho Chehab7e578192006-01-09 15:25:27 -0200997{
Michael Krufky4e9154b2007-10-21 19:39:50 -0300998 struct tuner *t = fe->analog_demod_priv;
Mauro Carvalho Chehab7e578192006-01-09 15:25:27 -0200999 unsigned long freq, freq_fraction;
Michael Krufkya07c8772008-04-29 03:54:19 -03001000 struct dvb_tuner_ops *fe_tuner_ops = &fe->ops.tuner_ops;
1001 struct analog_demod_ops *analog_ops = &fe->ops.analog_ops;
Mauro Carvalho Chehab7e578192006-01-09 15:25:27 -02001002 const char *p;
1003
1004 switch (t->mode) {
Mauro Carvalho Chehab7d275bf2011-02-04 11:28:00 -03001005 case V4L2_TUNER_RADIO:
1006 p = "radio";
1007 break;
1008 case V4L2_TUNER_DIGITAL_TV:
1009 p = "digital TV";
1010 break;
1011 case V4L2_TUNER_ANALOG_TV:
1012 default:
1013 p = "analog TV";
1014 break;
Mauro Carvalho Chehab7e578192006-01-09 15:25:27 -02001015 }
1016 if (t->mode == V4L2_TUNER_RADIO) {
Hans Verkuil27487d42006-01-15 15:04:52 -02001017 freq = t->radio_freq / 16000;
1018 freq_fraction = (t->radio_freq % 16000) * 100 / 16000;
Mauro Carvalho Chehab7e578192006-01-09 15:25:27 -02001019 } else {
Hans Verkuil27487d42006-01-15 15:04:52 -02001020 freq = t->tv_freq / 16;
1021 freq_fraction = (t->tv_freq % 16) * 100 / 16;
Mauro Carvalho Chehab7e578192006-01-09 15:25:27 -02001022 }
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -03001023 tuner_info("Tuner mode: %s%s\n", p,
1024 t->standby ? " on standby mode" : "");
Mauro Carvalho Chehab7e578192006-01-09 15:25:27 -02001025 tuner_info("Frequency: %lu.%02lu MHz\n", freq, freq_fraction);
Mauro Carvalho Chehab4ae5c2e2006-03-25 15:53:38 -03001026 tuner_info("Standard: 0x%08lx\n", (unsigned long)t->std);
Hans Verkuil8a4b2752006-01-23 17:11:09 -02001027 if (t->mode != V4L2_TUNER_RADIO)
Mauro Carvalho Chehab7d275bf2011-02-04 11:28:00 -03001028 return;
Michael Krufkye18f9442007-08-21 01:25:48 -03001029 if (fe_tuner_ops->get_status) {
1030 u32 tuner_status;
1031
1032 fe_tuner_ops->get_status(&t->fe, &tuner_status);
1033 if (tuner_status & TUNER_STATUS_LOCKED)
1034 tuner_info("Tuner is locked.\n");
1035 if (tuner_status & TUNER_STATUS_STEREO)
1036 tuner_info("Stereo: yes\n");
1037 }
Michael Krufkybc3e5c72007-12-21 11:18:32 -03001038 if (analog_ops->has_signal)
1039 tuner_info("Signal strength: %d\n",
1040 analog_ops->has_signal(fe));
Mauro Carvalho Chehab7e578192006-01-09 15:25:27 -02001041}
Hans Verkuil8a4b2752006-01-23 17:11:09 -02001042
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -03001043/**
1044 * tuner_s_power - controls the power state of the tuner
1045 * @sd: pointer to struct v4l2_subdev
1046 * @on: a zero value puts the tuner to sleep
1047 */
Laurent Pinchart622b8282009-10-05 10:48:17 -03001048static int tuner_s_power(struct v4l2_subdev *sd, int on)
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001049{
1050 struct tuner *t = to_tuner(sd);
Michael Krufkybc3e5c72007-12-21 11:18:32 -03001051 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -03001053 /* FIXME: Why this function don't wake the tuner if on != 0 ? */
Laurent Pinchart622b8282009-10-05 10:48:17 -03001054 if (on)
1055 return 0;
1056
Mauro Carvalho Chehab16a5e532008-12-20 07:17:10 -03001057 tuner_dbg("Putting tuner to sleep\n");
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -03001058 t->standby = true;
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001059 if (analog_ops->standby)
1060 analog_ops->standby(&t->fe);
1061 return 0;
1062}
1063
Mauro Carvalho Chehaba2894e32011-02-15 10:15:19 -02001064/*
1065 * Function to splicitly change mode to radio. Probably not needed anymore
1066 */
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -03001067
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -03001068static int tuner_s_radio(struct v4l2_subdev *sd)
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001069{
1070 struct tuner *t = to_tuner(sd);
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -03001071 struct i2c_client *client = v4l2_get_subdevdata(sd);
Mauro Carvalho Chehab7f171122007-10-18 19:56:47 -03001072
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -03001073 if (set_mode_freq(client, t, V4L2_TUNER_RADIO, 0) == -EINVAL)
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001074 return 0;
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001075 return 0;
1076}
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -07001077
Mauro Carvalho Chehaba2894e32011-02-15 10:15:19 -02001078/*
1079 * Tuner callbacks to handle userspace ioctl's
1080 */
1081
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001082static int tuner_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
1083{
1084 struct tuner *t = to_tuner(sd);
1085 struct i2c_client *client = v4l2_get_subdevdata(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -03001087 if (set_mode_freq(client, t, V4L2_TUNER_ANALOG_TV, 0) == -EINVAL)
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001088 return 0;
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -07001089
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001090 t->std = std;
1091 tuner_fixup_std(t);
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -03001092
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001093 return 0;
1094}
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -07001095
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001096static int tuner_s_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f)
1097{
1098 struct tuner *t = to_tuner(sd);
1099 struct i2c_client *client = v4l2_get_subdevdata(sd);
Michael Krufkye18f9442007-08-21 01:25:48 -03001100
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -03001101 if (set_mode_freq(client, t, f->type, f->frequency) == -EINVAL)
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001102 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
1104 return 0;
1105}
1106
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001107static int tuner_g_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f)
1108{
1109 struct tuner *t = to_tuner(sd);
1110 struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops;
1111
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -03001112 if (check_mode(t, f->type) == -EINVAL)
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001113 return 0;
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001114 f->type = t->mode;
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -03001115 if (fe_tuner_ops->get_frequency && !t->standby) {
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001116 u32 abs_freq;
1117
1118 fe_tuner_ops->get_frequency(&t->fe, &abs_freq);
1119 f->frequency = (V4L2_TUNER_RADIO == t->mode) ?
Julia Lawall75b697f2009-08-01 16:48:41 -03001120 DIV_ROUND_CLOSEST(abs_freq * 2, 125) :
1121 DIV_ROUND_CLOSEST(abs_freq, 62500);
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -03001122 } else {
1123 f->frequency = (V4L2_TUNER_RADIO == t->mode) ?
1124 t->radio_freq : t->tv_freq;
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001125 }
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001126 return 0;
1127}
1128
1129static int tuner_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
1130{
1131 struct tuner *t = to_tuner(sd);
1132 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
1133 struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops;
1134
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -03001135 if (check_mode(t, vt->type) == -EINVAL)
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001136 return 0;
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001137 vt->type = t->mode;
1138 if (analog_ops->get_afc)
1139 vt->afc = analog_ops->get_afc(&t->fe);
1140 if (t->mode == V4L2_TUNER_ANALOG_TV)
1141 vt->capability |= V4L2_TUNER_CAP_NORM;
1142 if (t->mode != V4L2_TUNER_RADIO) {
1143 vt->rangelow = tv_range[0] * 16;
1144 vt->rangehigh = tv_range[1] * 16;
1145 return 0;
1146 }
1147
1148 /* radio mode */
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -03001149 vt->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001150 if (fe_tuner_ops->get_status) {
1151 u32 tuner_status;
1152
1153 fe_tuner_ops->get_status(&t->fe, &tuner_status);
1154 vt->rxsubchans =
1155 (tuner_status & TUNER_STATUS_STEREO) ?
1156 V4L2_TUNER_SUB_STEREO :
1157 V4L2_TUNER_SUB_MONO;
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001158 }
1159 if (analog_ops->has_signal)
1160 vt->signal = analog_ops->has_signal(&t->fe);
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -03001161 vt->capability |= V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001162 vt->audmode = t->audmode;
1163 vt->rangelow = radio_range[0] * 16000;
1164 vt->rangehigh = radio_range[1] * 16000;
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -03001165
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001166 return 0;
1167}
1168
1169static int tuner_s_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
1170{
1171 struct tuner *t = to_tuner(sd);
1172 struct i2c_client *client = v4l2_get_subdevdata(sd);
1173
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -03001174 if (set_mode_freq(client, t, vt->type, 0) == -EINVAL)
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001175 return 0;
1176
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -03001177 if (t->mode == V4L2_TUNER_RADIO)
1178 t->audmode = vt->audmode;
1179
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001180 return 0;
1181}
1182
1183static int tuner_log_status(struct v4l2_subdev *sd)
1184{
1185 struct tuner *t = to_tuner(sd);
1186 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
1187
1188 if (analog_ops->tuner_status)
1189 analog_ops->tuner_status(&t->fe);
1190 return 0;
1191}
1192
Jean Delvare21b48a72007-03-12 19:20:15 -03001193static int tuner_suspend(struct i2c_client *c, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194{
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001195 struct tuner *t = to_tuner(i2c_get_clientdata(c));
Mauro Carvalho Chehabe2f63d92011-02-04 11:15:21 -03001196 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
Hans Verkuil9dd659d2007-11-04 11:03:36 -03001198 tuner_dbg("suspend\n");
Mauro Carvalho Chehabe2f63d92011-02-04 11:15:21 -03001199
1200 if (!t->standby && analog_ops->standby)
1201 analog_ops->standby(&t->fe);
1202
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 return 0;
1204}
1205
Jean Delvare21b48a72007-03-12 19:20:15 -03001206static int tuner_resume(struct i2c_client *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207{
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001208 struct tuner *t = to_tuner(i2c_get_clientdata(c));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
Hans Verkuil9dd659d2007-11-04 11:03:36 -03001210 tuner_dbg("resume\n");
Mauro Carvalho Chehabe2f63d92011-02-04 11:15:21 -03001211
1212 if (!t->standby)
1213 set_mode_freq(c, t, t->type, 0);
1214
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 return 0;
1216}
1217
Hans Verkuil75b4c262009-04-01 03:32:22 -03001218static int tuner_command(struct i2c_client *client, unsigned cmd, void *arg)
1219{
1220 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1221
1222 /* TUNER_SET_CONFIG is still called by tuner-simple.c, so we have
1223 to handle it here.
1224 There must be a better way of doing this... */
1225 switch (cmd) {
1226 case TUNER_SET_CONFIG:
1227 return tuner_s_config(sd, arg);
1228 }
1229 return -ENOIOCTLCMD;
1230}
1231
Mauro Carvalho Chehaba2894e32011-02-15 10:15:19 -02001232/*
1233 * Callback structs
1234 */
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001235
1236static const struct v4l2_subdev_core_ops tuner_core_ops = {
1237 .log_status = tuner_log_status,
Hans Verkuilf41737e2009-04-01 03:52:39 -03001238 .s_std = tuner_s_std,
Laurent Pinchart622b8282009-10-05 10:48:17 -03001239 .s_power = tuner_s_power,
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001240};
1241
1242static const struct v4l2_subdev_tuner_ops tuner_tuner_ops = {
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001243 .s_radio = tuner_s_radio,
1244 .g_tuner = tuner_g_tuner,
1245 .s_tuner = tuner_s_tuner,
1246 .s_frequency = tuner_s_frequency,
1247 .g_frequency = tuner_g_frequency,
1248 .s_type_addr = tuner_s_type_addr,
1249 .s_config = tuner_s_config,
1250};
1251
1252static const struct v4l2_subdev_ops tuner_ops = {
1253 .core = &tuner_core_ops,
1254 .tuner = &tuner_tuner_ops,
1255};
1256
Mauro Carvalho Chehaba2894e32011-02-15 10:15:19 -02001257/*
1258 * I2C structs and module init functions
1259 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Jean Delvareaf294862008-05-18 20:49:40 +02001261static const struct i2c_device_id tuner_id[] = {
1262 { "tuner", }, /* autodetect */
1263 { }
1264};
1265MODULE_DEVICE_TABLE(i2c, tuner_id);
1266
Hans Verkuil02a20982010-09-15 15:36:23 -03001267static struct i2c_driver tuner_driver = {
1268 .driver = {
1269 .owner = THIS_MODULE,
1270 .name = "tuner",
1271 },
1272 .probe = tuner_probe,
1273 .remove = tuner_remove,
1274 .command = tuner_command,
1275 .suspend = tuner_suspend,
1276 .resume = tuner_resume,
1277 .id_table = tuner_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278};
1279
Hans Verkuil02a20982010-09-15 15:36:23 -03001280static __init int init_tuner(void)
1281{
1282 return i2c_add_driver(&tuner_driver);
1283}
1284
1285static __exit void exit_tuner(void)
1286{
1287 i2c_del_driver(&tuner_driver);
1288}
1289
1290module_init(init_tuner);
1291module_exit(exit_tuner);
1292
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -03001293MODULE_DESCRIPTION("device driver for various TV and TV+FM radio tuners");
1294MODULE_AUTHOR("Ralph Metzler, Gerd Knorr, Gunther Mayer");
1295MODULE_LICENSE("GPL");