blob: e6855a46f433f6453f84abf069fa013a7d32863b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
3 * i2c tv tuner chip device driver
4 * core core, i.e. kernel interfaces, registering and so on
5 */
6
7#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/string.h>
10#include <linux/timer.h>
11#include <linux/delay.h>
12#include <linux/errno.h>
13#include <linux/slab.h>
14#include <linux/poll.h>
15#include <linux/i2c.h>
16#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/init.h>
Hans Verkuil75b4c262009-04-01 03:32:22 -030018#include <linux/videodev2.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <media/tuner.h>
Michael Krufky4adad282007-08-27 21:59:08 -030020#include <media/tuner-types.h>
Hans Verkuile8a4a9e2008-11-24 18:21:40 -030021#include <media/v4l2-device.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030022#include <media/v4l2-ioctl.h>
Michael Krufky96c0b7c2007-08-27 21:23:08 -030023#include "mt20xx.h"
Michael Krufky910bb3e2007-08-27 21:22:20 -030024#include "tda8290.h"
Michael Krufky7ab10bf2007-08-27 21:23:40 -030025#include "tea5761.h"
Michael Krufky8d0936e2007-08-27 21:24:27 -030026#include "tea5767.h"
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -030027#include "tuner-xc2028.h"
Michael Krufky4adad282007-08-27 21:59:08 -030028#include "tuner-simple.h"
Michael Krufky31c95842007-10-21 20:48:48 -030029#include "tda9887.h"
Steven Toth27c685a2008-01-05 16:50:14 -030030#include "xc5000.h"
Michael Krufky93463892009-09-15 23:04:18 -030031#include "tda18271.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33#define UNSET (-1U)
34
Hans Verkuil9dd659d2007-11-04 11:03:36 -030035#define PREFIX t->i2c->driver->driver.name
Michael Krufky241020d2007-10-30 09:46:10 -030036
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -030037/*
38 * Driver modprobe parameters
39 */
40
41/* insmod options used at init time => read/only */
42static unsigned int addr;
43static unsigned int no_autodetect;
44static unsigned int show_i2c;
45
46module_param(addr, int, 0444);
47module_param(no_autodetect, int, 0444);
48module_param(show_i2c, int, 0444);
49
50/* insmod options used at runtime => read/write */
51static int tuner_debug;
52static unsigned int tv_range[2] = { 44, 958 };
53static unsigned int radio_range[2] = { 65, 108 };
54static char pal[] = "--";
55static char secam[] = "--";
56static char ntsc[] = "-";
57
58module_param_named(debug,tuner_debug, int, 0644);
59module_param_array(tv_range, int, NULL, 0644);
60module_param_array(radio_range, int, NULL, 0644);
61module_param_string(pal, pal, sizeof(pal), 0644);
62module_param_string(secam, secam, sizeof(secam), 0644);
63module_param_string(ntsc, ntsc, sizeof(ntsc), 0644);
64
65/*
66 * Static vars
67 */
68
69static struct xc5000_config xc5000_cfg;
70static LIST_HEAD(tuner_list);
71
72/*
73 * Debug macros
74 */
75
76#define tuner_warn(fmt, arg...) do { \
77 printk(KERN_WARNING "%s %d-%04x: " fmt, PREFIX, \
78 i2c_adapter_id(t->i2c->adapter), \
79 t->i2c->addr, ##arg); \
80 } while (0)
81
82#define tuner_info(fmt, arg...) do { \
83 printk(KERN_INFO "%s %d-%04x: " fmt, PREFIX, \
84 i2c_adapter_id(t->i2c->adapter), \
85 t->i2c->addr, ##arg); \
86 } while (0)
87
88#define tuner_err(fmt, arg...) do { \
89 printk(KERN_ERR "%s %d-%04x: " fmt, PREFIX, \
90 i2c_adapter_id(t->i2c->adapter), \
91 t->i2c->addr, ##arg); \
92 } while (0)
93
94#define tuner_dbg(fmt, arg...) do { \
95 if (tuner_debug) \
96 printk(KERN_DEBUG "%s %d-%04x: " fmt, PREFIX, \
97 i2c_adapter_id(t->i2c->adapter), \
98 t->i2c->addr, ##arg); \
99 } while (0)
100
101/*
102 * Internal struct used inside the driver
103 */
104
105struct tuner {
106 /* device */
107 struct dvb_frontend fe;
108 struct i2c_client *i2c;
109 struct v4l2_subdev sd;
110 struct list_head list;
111
112 /* keep track of the current settings */
113 v4l2_std_id std;
114 unsigned int tv_freq;
115 unsigned int radio_freq;
116 unsigned int audmode;
117
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -0300118 enum v4l2_tuner_type mode;
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300119 unsigned int mode_mask; /* Combination of allowable modes */
120
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -0300121 bool standby; /* Standby mode */
122
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300123 unsigned int type; /* chip type id */
124 unsigned int config;
125 const char *name;
126};
127
128/*
129 * tuner attach/detach logic
130 */
131
Michael Krufkya07c8772008-04-29 03:54:19 -0300132/** This macro allows us to probe dynamically, avoiding static links */
Mauro Carvalho Chehabff138172008-04-29 23:02:33 -0300133#ifdef CONFIG_MEDIA_ATTACH
Michael Krufkya07c8772008-04-29 03:54:19 -0300134#define tuner_symbol_probe(FUNCTION, ARGS...) ({ \
135 int __r = -EINVAL; \
136 typeof(&FUNCTION) __a = symbol_request(FUNCTION); \
137 if (__a) { \
138 __r = (int) __a(ARGS); \
Andrew Mortona1355e52008-04-30 11:40:17 -0300139 symbol_put(FUNCTION); \
Michael Krufkya07c8772008-04-29 03:54:19 -0300140 } else { \
141 printk(KERN_ERR "TUNER: Unable to find " \
142 "symbol "#FUNCTION"()\n"); \
143 } \
Michael Krufkya07c8772008-04-29 03:54:19 -0300144 __r; \
145})
146
147static void tuner_detach(struct dvb_frontend *fe)
148{
149 if (fe->ops.tuner_ops.release) {
150 fe->ops.tuner_ops.release(fe);
151 symbol_put_addr(fe->ops.tuner_ops.release);
152 }
153 if (fe->ops.analog_ops.release) {
154 fe->ops.analog_ops.release(fe);
155 symbol_put_addr(fe->ops.analog_ops.release);
156 }
157}
158#else
159#define tuner_symbol_probe(FUNCTION, ARGS...) ({ \
160 FUNCTION(ARGS); \
161})
162
163static void tuner_detach(struct dvb_frontend *fe)
164{
165 if (fe->ops.tuner_ops.release)
166 fe->ops.tuner_ops.release(fe);
167 if (fe->ops.analog_ops.release)
168 fe->ops.analog_ops.release(fe);
169}
170#endif
171
Michael Krufkyf7f427e2007-12-16 22:02:26 -0300172
Hans Verkuile8a4a9e2008-11-24 18:21:40 -0300173static inline struct tuner *to_tuner(struct v4l2_subdev *sd)
174{
175 return container_of(sd, struct tuner, sd);
176}
177
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300178/*
179 * struct analog_demod_ops callbacks
180 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Michael Krufkyc7919d52007-12-08 17:06:30 -0300182static void fe_set_params(struct dvb_frontend *fe,
183 struct analog_parameters *params)
Michael Krufkye18f9442007-08-21 01:25:48 -0300184{
Michael Krufky4e9154b2007-10-21 19:39:50 -0300185 struct dvb_tuner_ops *fe_tuner_ops = &fe->ops.tuner_ops;
186 struct tuner *t = fe->analog_demod_priv;
Michael Krufkye18f9442007-08-21 01:25:48 -0300187
Michael Krufkye18f9442007-08-21 01:25:48 -0300188 if (NULL == fe_tuner_ops->set_analog_params) {
189 tuner_warn("Tuner frontend module has no way to set freq\n");
190 return;
191 }
Michael Krufkyc7919d52007-12-08 17:06:30 -0300192 fe_tuner_ops->set_analog_params(fe, params);
Michael Krufkye18f9442007-08-21 01:25:48 -0300193}
194
Michael Krufky4e9154b2007-10-21 19:39:50 -0300195static void fe_standby(struct dvb_frontend *fe)
Michael Krufkye18f9442007-08-21 01:25:48 -0300196{
Michael Krufky4e9154b2007-10-21 19:39:50 -0300197 struct dvb_tuner_ops *fe_tuner_ops = &fe->ops.tuner_ops;
Michael Krufkye18f9442007-08-21 01:25:48 -0300198
199 if (fe_tuner_ops->sleep)
Michael Krufky4e9154b2007-10-21 19:39:50 -0300200 fe_tuner_ops->sleep(fe);
Michael Krufkye18f9442007-08-21 01:25:48 -0300201}
202
Michael Krufky4e9154b2007-10-21 19:39:50 -0300203static int fe_has_signal(struct dvb_frontend *fe)
Michael Krufky1f5ef192007-08-31 17:38:02 -0300204{
Michael Krufky14196832007-10-14 18:11:53 -0300205 u16 strength = 0;
Michael Krufky1f5ef192007-08-31 17:38:02 -0300206
Michael Krufky4e9154b2007-10-21 19:39:50 -0300207 if (fe->ops.tuner_ops.get_rf_strength)
208 fe->ops.tuner_ops.get_rf_strength(fe, &strength);
Michael Krufky1f5ef192007-08-31 17:38:02 -0300209
210 return strength;
211}
212
Michael Krufkyf1c9a282007-12-16 19:27:23 -0300213static int fe_set_config(struct dvb_frontend *fe, void *priv_cfg)
214{
215 struct dvb_tuner_ops *fe_tuner_ops = &fe->ops.tuner_ops;
216 struct tuner *t = fe->analog_demod_priv;
217
218 if (fe_tuner_ops->set_config)
219 return fe_tuner_ops->set_config(fe, priv_cfg);
220
221 tuner_warn("Tuner frontend module has no way to set config\n");
222
223 return 0;
224}
225
Michael Krufky4e9154b2007-10-21 19:39:50 -0300226static void tuner_status(struct dvb_frontend *fe);
Michael Krufky1dde7a42007-10-21 13:40:56 -0300227
Hans Verkuile8a4a9e2008-11-24 18:21:40 -0300228static struct analog_demod_ops tuner_analog_ops = {
Michael Krufkyc7919d52007-12-08 17:06:30 -0300229 .set_params = fe_set_params,
Michael Krufky1dde7a42007-10-21 13:40:56 -0300230 .standby = fe_standby,
Michael Krufky1dde7a42007-10-21 13:40:56 -0300231 .has_signal = fe_has_signal,
Michael Krufkyf1c9a282007-12-16 19:27:23 -0300232 .set_config = fe_set_config,
Michael Krufky1dde7a42007-10-21 13:40:56 -0300233 .tuner_status = tuner_status
234};
235
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300236/*
237 * Functions that are common to both TV and radio
238 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300240static void set_tv_freq(struct i2c_client *c, unsigned int freq);
241static void set_radio_freq(struct i2c_client *c, unsigned int freq);
242static const struct v4l2_subdev_ops tuner_ops;
Steven Toth27c685a2008-01-05 16:50:14 -0300243
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700244static void set_type(struct i2c_client *c, unsigned int type,
Hartmut Hackmannde956c12007-04-27 12:31:12 -0300245 unsigned int new_mode_mask, unsigned int new_config,
Michael Krufkyd7cba042008-09-12 13:31:45 -0300246 int (*tuner_callback) (void *dev, int component, int cmd, int arg))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
Hans Verkuile8a4a9e2008-11-24 18:21:40 -0300248 struct tuner *t = to_tuner(i2c_get_clientdata(c));
Michael Krufkye18f9442007-08-21 01:25:48 -0300249 struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops;
Michael Krufkybc3e5c72007-12-21 11:18:32 -0300250 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700251 unsigned char buffer[4];
Michael Krufkyd6eef492009-10-24 16:42:16 -0300252 int tune_now = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700254 if (type == UNSET || type == TUNER_ABSENT) {
255 tuner_dbg ("tuner 0x%02x: Tuner type absent\n",c->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 return;
257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 t->type = type;
Michael Krufkye7ddcd92009-03-28 15:35:26 -0300260 /* prevent invalid config values */
Roel Kluinf14a2972009-10-23 07:59:42 -0300261 t->config = new_config < 256 ? new_config : 0;
Hartmut Hackmanncfeb8832007-04-27 12:31:17 -0300262 if (tuner_callback != NULL) {
263 tuner_dbg("defining GPIO callback\n");
Michael Krufkyd7cba042008-09-12 13:31:45 -0300264 t->fe.callback = tuner_callback;
Hartmut Hackmanncfeb8832007-04-27 12:31:17 -0300265 }
Hartmut Hackmann80f90fb2007-04-27 12:31:18 -0300266
Michael Krufkyb2083192007-05-29 22:54:06 -0300267 /* discard private data, in case set_type() was previously called */
Michael Krufkya07c8772008-04-29 03:54:19 -0300268 tuner_detach(&t->fe);
269 t->fe.analog_demod_priv = NULL;
Michael Krufkybe2b85a2007-06-04 14:40:27 -0300270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 switch (t->type) {
272 case TUNER_MT2032:
Mauro Carvalho Chehab09fee5f2008-04-30 15:29:57 -0300273 if (!dvb_attach(microtune_attach,
274 &t->fe, t->i2c->adapter, t->i2c->addr))
275 goto attach_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 break;
277 case TUNER_PHILIPS_TDA8290:
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300278 {
Mauro Carvalho Chehab09fee5f2008-04-30 15:29:57 -0300279 struct tda829x_config cfg = {
280 .lna_cfg = t->config,
Mauro Carvalho Chehab09fee5f2008-04-30 15:29:57 -0300281 };
282 if (!dvb_attach(tda829x_attach, &t->fe, t->i2c->adapter,
283 t->i2c->addr, &cfg))
284 goto attach_failed;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300285 break;
286 }
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700287 case TUNER_TEA5767:
Michael Krufkya07c8772008-04-29 03:54:19 -0300288 if (!dvb_attach(tea5767_attach, &t->fe,
289 t->i2c->adapter, t->i2c->addr))
Mauro Carvalho Chehabb9ef6bb2008-04-26 11:29:34 -0300290 goto attach_failed;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700291 t->mode_mask = T_RADIO;
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700292 break;
Mauro Carvalho Chehab8573a9e2007-04-08 01:09:11 -0300293 case TUNER_TEA5761:
Michael Krufkya07c8772008-04-29 03:54:19 -0300294 if (!dvb_attach(tea5761_attach, &t->fe,
295 t->i2c->adapter, t->i2c->addr))
Mauro Carvalho Chehabb9ef6bb2008-04-26 11:29:34 -0300296 goto attach_failed;
Mauro Carvalho Chehab8573a9e2007-04-08 01:09:11 -0300297 t->mode_mask = T_RADIO;
298 break;
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700299 case TUNER_PHILIPS_FMD1216ME_MK3:
300 buffer[0] = 0x0b;
301 buffer[1] = 0xdc;
302 buffer[2] = 0x9c;
303 buffer[3] = 0x60;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700304 i2c_master_send(c, buffer, 4);
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700305 mdelay(1);
306 buffer[2] = 0x86;
307 buffer[3] = 0x54;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700308 i2c_master_send(c, buffer, 4);
Michael Krufkya07c8772008-04-29 03:54:19 -0300309 if (!dvb_attach(simple_tuner_attach, &t->fe,
310 t->i2c->adapter, t->i2c->addr, t->type))
Mauro Carvalho Chehabb9ef6bb2008-04-26 11:29:34 -0300311 goto attach_failed;
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700312 break;
Hartmut Hackmann93df3412005-11-08 21:36:31 -0800313 case TUNER_PHILIPS_TD1316:
314 buffer[0] = 0x0b;
315 buffer[1] = 0xdc;
316 buffer[2] = 0x86;
317 buffer[3] = 0xa4;
Michael Krufkya07c8772008-04-29 03:54:19 -0300318 i2c_master_send(c, buffer, 4);
319 if (!dvb_attach(simple_tuner_attach, &t->fe,
320 t->i2c->adapter, t->i2c->addr, t->type))
Mauro Carvalho Chehabb9ef6bb2008-04-26 11:29:34 -0300321 goto attach_failed;
Markus Rechbergerac272ed2006-01-23 17:11:09 -0200322 break;
Mauro Carvalho Chehab690c5442007-10-29 11:33:18 -0300323 case TUNER_XC2028:
324 {
Michel Ludwiga37b4c92007-11-16 07:46:14 -0300325 struct xc2028_config cfg = {
326 .i2c_adap = t->i2c->adapter,
327 .i2c_addr = t->i2c->addr,
Michel Ludwiga37b4c92007-11-16 07:46:14 -0300328 };
Michael Krufkya07c8772008-04-29 03:54:19 -0300329 if (!dvb_attach(xc2028_attach, &t->fe, &cfg))
Mauro Carvalho Chehabb9ef6bb2008-04-26 11:29:34 -0300330 goto attach_failed;
Michael Krufkyd6eef492009-10-24 16:42:16 -0300331 tune_now = 0;
Mauro Carvalho Chehab690c5442007-10-29 11:33:18 -0300332 break;
333 }
Mauro Carvalho Chehab15396232006-06-23 16:13:56 -0300334 case TUNER_TDA9887:
Mauro Carvalho Chehab09fee5f2008-04-30 15:29:57 -0300335 if (!dvb_attach(tda9887_attach,
336 &t->fe, t->i2c->adapter, t->i2c->addr))
337 goto attach_failed;
Mauro Carvalho Chehab15396232006-06-23 16:13:56 -0300338 break;
Steven Toth27c685a2008-01-05 16:50:14 -0300339 case TUNER_XC5000:
Mauro Carvalho Chehabb9ef6bb2008-04-26 11:29:34 -0300340 {
Steven Toth27c685a2008-01-05 16:50:14 -0300341 xc5000_cfg.i2c_address = t->i2c->addr;
Devin Heitmuellerea227862009-03-11 02:58:53 -0300342 /* if_khz will be set when the digital dvb_attach() occurs */
343 xc5000_cfg.if_khz = 0;
Michael Krufkya07c8772008-04-29 03:54:19 -0300344 if (!dvb_attach(xc5000_attach,
Michael Krufky30650962008-09-06 14:56:58 -0300345 &t->fe, t->i2c->adapter, &xc5000_cfg))
Mauro Carvalho Chehabb9ef6bb2008-04-26 11:29:34 -0300346 goto attach_failed;
Michael Krufkyd6eef492009-10-24 16:42:16 -0300347 tune_now = 0;
Steven Toth27c685a2008-01-05 16:50:14 -0300348 break;
Mauro Carvalho Chehabb9ef6bb2008-04-26 11:29:34 -0300349 }
Michael Krufky93463892009-09-15 23:04:18 -0300350 case TUNER_NXP_TDA18271:
351 {
352 struct tda18271_config cfg = {
353 .config = t->config,
Mauro Carvalho Chehabe350d442010-09-26 22:58:28 -0300354 .small_i2c = TDA18271_03_BYTE_CHUNK_INIT,
Michael Krufky93463892009-09-15 23:04:18 -0300355 };
356
357 if (!dvb_attach(tda18271_attach, &t->fe, t->i2c->addr,
358 t->i2c->adapter, &cfg))
359 goto attach_failed;
Michael Krufkyd6eef492009-10-24 16:42:16 -0300360 tune_now = 0;
Michael Krufky93463892009-09-15 23:04:18 -0300361 break;
362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 default:
Michael Krufkya07c8772008-04-29 03:54:19 -0300364 if (!dvb_attach(simple_tuner_attach, &t->fe,
365 t->i2c->adapter, t->i2c->addr, t->type))
Mauro Carvalho Chehabb9ef6bb2008-04-26 11:29:34 -0300366 goto attach_failed;
367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 break;
369 }
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700370
Michael Krufkybc3e5c72007-12-21 11:18:32 -0300371 if ((NULL == analog_ops->set_params) &&
Michael Krufky1dde7a42007-10-21 13:40:56 -0300372 (fe_tuner_ops->set_analog_params)) {
Michael Krufkya07c8772008-04-29 03:54:19 -0300373
Michael Krufky7271e602008-05-26 16:08:40 +0200374 t->name = fe_tuner_ops->info.name;
Michael Krufkye18f9442007-08-21 01:25:48 -0300375
Michael Krufky4e9154b2007-10-21 19:39:50 -0300376 t->fe.analog_demod_priv = t;
Hans Verkuile8a4a9e2008-11-24 18:21:40 -0300377 memcpy(analog_ops, &tuner_analog_ops,
Michael Krufkybc3e5c72007-12-21 11:18:32 -0300378 sizeof(struct analog_demod_ops));
Michael Krufkya07c8772008-04-29 03:54:19 -0300379
Michael Krufkya55db8c2007-12-09 13:52:51 -0300380 } else {
Michael Krufky7271e602008-05-26 16:08:40 +0200381 t->name = analog_ops->info.name;
Michael Krufkye18f9442007-08-21 01:25:48 -0300382 }
383
Michael Krufky7271e602008-05-26 16:08:40 +0200384 tuner_dbg("type set to %s\n", t->name);
Michael Krufkye18f9442007-08-21 01:25:48 -0300385
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -0300386 t->mode_mask = new_mode_mask;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700387
Michael Krufkyd6eef492009-10-24 16:42:16 -0300388 /* Some tuners require more initialization setup before use,
389 such as firmware download or device calibration.
Mauro Carvalho Chehabb9ef6bb2008-04-26 11:29:34 -0300390 trying to set a frequency here will just fail
391 FIXME: better to move set_freq to the tuner code. This is needed
392 on analog tuners for PLL to properly work
393 */
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300394 if (tune_now) {
395 if (V4L2_TUNER_RADIO == t->mode)
396 set_radio_freq(c, t->radio_freq);
397 else
398 set_tv_freq(c, t->tv_freq);
399 }
Mauro Carvalho Chehabb9ef6bb2008-04-26 11:29:34 -0300400
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700401 tuner_dbg("%s %s I2C addr 0x%02x with type %d used for 0x%02x\n",
Laurent Riffard604f28e2005-11-26 20:43:39 +0100402 c->adapter->name, c->driver->driver.name, c->addr << 1, type,
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700403 t->mode_mask);
Mauro Carvalho Chehabb9ef6bb2008-04-26 11:29:34 -0300404 return;
405
406attach_failed:
407 tuner_dbg("Tuner attach for type = %d failed.\n", t->type);
408 t->type = TUNER_ABSENT;
Mauro Carvalho Chehabb9ef6bb2008-04-26 11:29:34 -0300409
410 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411}
412
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700413/*
414 * This function apply tuner config to tuner specified
415 * by tun_setup structure. I addr is unset, then admin status
416 * and tun addr status is more precise then current status,
417 * it's applied. Otherwise status and type are applied only to
418 * tuner with exactly the same addr.
419*/
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700420
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700421static void set_addr(struct i2c_client *c, struct tuner_setup *tun_setup)
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700422{
Hans Verkuile8a4a9e2008-11-24 18:21:40 -0300423 struct tuner *t = to_tuner(i2c_get_clientdata(c));
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700424
Hartmut Hackmannde956c12007-04-27 12:31:12 -0300425 if ( (t->type == UNSET && ((tun_setup->addr == ADDR_UNSET) &&
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -0300426 (t->mode_mask & tun_setup->mode_mask))) ||
427 (tun_setup->addr == c->addr)) {
428 set_type(c, tun_setup->type, tun_setup->mode_mask,
429 tun_setup->config, tun_setup->tuner_callback);
Mauro Carvalho Chehabb9ef6bb2008-04-26 11:29:34 -0300430 } else
431 tuner_dbg("set addr discarded for type %i, mask %x. "
432 "Asked to change tuner at addr 0x%02x, with mask %x\n",
433 t->type, t->mode_mask,
434 tun_setup->addr, tun_setup->mode_mask);
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700435}
436
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300437static int tuner_s_type_addr(struct v4l2_subdev *sd, struct tuner_setup *type)
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700438{
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300439 struct tuner *t = to_tuner(sd);
440 struct i2c_client *client = v4l2_get_subdevdata(sd);
441
442 tuner_dbg("Calling set_type_addr for type=%d, addr=0x%02x, mode=0x%02x, config=0x%02x\n",
443 type->type,
444 type->addr,
445 type->mode_mask,
446 type->config);
447
448 set_addr(client, type);
449 return 0;
450}
451
452static int tuner_s_config(struct v4l2_subdev *sd, const struct v4l2_priv_tun_config *cfg)
453{
454 struct tuner *t = to_tuner(sd);
455 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
456
457 if (t->type != cfg->tuner)
458 return 0;
459
460 if (analog_ops->set_config) {
461 analog_ops->set_config(&t->fe, cfg->priv);
462 return 0;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700463 }
Mauro Carvalho Chehab793cf9e2005-09-09 13:03:37 -0700464
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300465 tuner_dbg("Tuner frontend module has no way to set config\n");
Mauro Carvalho Chehab793cf9e2005-09-09 13:03:37 -0700466 return 0;
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700467}
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700468
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300469/* Search for existing radio and/or TV tuners on the given I2C adapter.
470 Note that when this function is called from tuner_probe you can be
471 certain no other devices will be added/deleted at the same time, I2C
472 core protects against that. */
473static void tuner_lookup(struct i2c_adapter *adap,
474 struct tuner **radio, struct tuner **tv)
475{
476 struct tuner *pos;
477
478 *radio = NULL;
479 *tv = NULL;
480
481 list_for_each_entry(pos, &tuner_list, list) {
482 int mode_mask;
483
484 if (pos->i2c->adapter != adap ||
485 strcmp(pos->i2c->driver->driver.name, "tuner"))
486 continue;
487
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -0300488 mode_mask = pos->mode_mask;
489 pos->standby = 1;
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300490 if (*radio == NULL && mode_mask == T_RADIO)
491 *radio = pos;
492 /* Note: currently TDA9887 is the only demod-only
493 device. If other devices appear then we need to
494 make this test more general. */
495 else if (*tv == NULL && pos->type != TUNER_TDA9887 &&
496 (pos->mode_mask & (T_ANALOG_TV | T_DIGITAL_TV)))
497 *tv = pos;
498 }
499}
500
501/* During client attach, set_type is called by adapter's attach_inform callback.
502 set_type must then be completed by tuner_probe.
503 */
504static int tuner_probe(struct i2c_client *client,
505 const struct i2c_device_id *id)
506{
507 struct tuner *t;
508 struct tuner *radio;
509 struct tuner *tv;
510
511 t = kzalloc(sizeof(struct tuner), GFP_KERNEL);
512 if (NULL == t)
513 return -ENOMEM;
514 v4l2_i2c_subdev_init(&t->sd, client, &tuner_ops);
515 t->i2c = client;
516 t->name = "(tuner unset)";
517 t->type = UNSET;
518 t->audmode = V4L2_TUNER_MODE_STEREO;
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -0300519 t->standby = 1;
520 t->radio_freq = 87.5 * 16000; /* Initial freq range */
521 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 -0300522
523 if (show_i2c) {
524 unsigned char buffer[16];
525 int i, rc;
526
527 memset(buffer, 0, sizeof(buffer));
528 rc = i2c_master_recv(client, buffer, sizeof(buffer));
529 tuner_info("I2C RECV = ");
530 for (i = 0; i < rc; i++)
531 printk(KERN_CONT "%02x ", buffer[i]);
532 printk("\n");
533 }
534
535 /* autodetection code based on the i2c addr */
536 if (!no_autodetect) {
537 switch (client->addr) {
538 case 0x10:
539 if (tuner_symbol_probe(tea5761_autodetection,
540 t->i2c->adapter,
541 t->i2c->addr) >= 0) {
542 t->type = TUNER_TEA5761;
543 t->mode_mask = T_RADIO;
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300544 tuner_lookup(t->i2c->adapter, &radio, &tv);
545 if (tv)
546 tv->mode_mask &= ~T_RADIO;
547
548 goto register_client;
549 }
550 kfree(t);
551 return -ENODEV;
552 case 0x42:
553 case 0x43:
554 case 0x4a:
555 case 0x4b:
556 /* If chip is not tda8290, don't register.
557 since it can be tda9887*/
558 if (tuner_symbol_probe(tda829x_probe, t->i2c->adapter,
559 t->i2c->addr) >= 0) {
560 tuner_dbg("tda829x detected\n");
561 } else {
562 /* Default is being tda9887 */
563 t->type = TUNER_TDA9887;
564 t->mode_mask = T_RADIO | T_ANALOG_TV |
565 T_DIGITAL_TV;
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300566 goto register_client;
567 }
568 break;
569 case 0x60:
570 if (tuner_symbol_probe(tea5767_autodetection,
571 t->i2c->adapter, t->i2c->addr)
572 >= 0) {
573 t->type = TUNER_TEA5767;
574 t->mode_mask = T_RADIO;
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300575 /* Sets freq to FM range */
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300576 tuner_lookup(t->i2c->adapter, &radio, &tv);
577 if (tv)
578 tv->mode_mask &= ~T_RADIO;
579
580 goto register_client;
581 }
582 break;
583 }
584 }
585
586 /* Initializes only the first TV tuner on this adapter. Why only the
587 first? Because there are some devices (notably the ones with TI
588 tuners) that have more than one i2c address for the *same* device.
589 Experience shows that, except for just one case, the first
590 address is the right one. The exception is a Russian tuner
591 (ACORP_Y878F). So, the desired behavior is just to enable the
592 first found TV tuner. */
593 tuner_lookup(t->i2c->adapter, &radio, &tv);
594 if (tv == NULL) {
595 t->mode_mask = T_ANALOG_TV | T_DIGITAL_TV;
596 if (radio == NULL)
597 t->mode_mask |= T_RADIO;
598 tuner_dbg("Setting mode_mask to 0x%02x\n", t->mode_mask);
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300599 }
600
601 /* Should be just before return */
602register_client:
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300603 /* Sets a default mode */
604 if (t->mode_mask & T_ANALOG_TV) {
605 t->mode = V4L2_TUNER_ANALOG_TV;
606 } else if (t->mode_mask & T_RADIO) {
607 t->mode = V4L2_TUNER_RADIO;
608 } else {
609 t->mode = V4L2_TUNER_DIGITAL_TV;
610 }
611 set_type(client, t->type, t->mode_mask, t->config, t->fe.callback);
612 list_add_tail(&t->list, &tuner_list);
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -0300613
614 tuner_info("Tuner %d found with type(s)%s%s%s.\n",
615 t->type,
616 t->mode_mask & T_RADIO ? " radio" : "",
617 t->mode_mask & T_ANALOG_TV ? " TV" : "",
618 t->mode_mask & T_ANALOG_TV ? " DTV" : "");
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300619 return 0;
620}
621
622static int tuner_remove(struct i2c_client *client)
623{
624 struct tuner *t = to_tuner(i2c_get_clientdata(client));
625
626 v4l2_device_unregister_subdev(&t->sd);
627 tuner_detach(&t->fe);
628 t->fe.analog_demod_priv = NULL;
629
630 list_del(&t->list);
631 kfree(t);
632 return 0;
633}
634
635/*
636 * Functions that are specific for TV mode
637 */
638
639/* Set tuner frequency, freq in Units of 62.5kHz = 1/16MHz */
640static void set_tv_freq(struct i2c_client *c, unsigned int freq)
641{
642 struct tuner *t = to_tuner(i2c_get_clientdata(c));
643 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
644
645 struct analog_parameters params = {
646 .mode = t->mode,
647 .audmode = t->audmode,
648 .std = t->std
649 };
650
651 if (t->type == UNSET) {
652 tuner_warn ("tuner type not set\n");
653 return;
654 }
655 if (NULL == analog_ops->set_params) {
656 tuner_warn ("Tuner has no way to set tv freq\n");
657 return;
658 }
659 if (freq < tv_range[0] * 16 || freq > tv_range[1] * 16) {
660 tuner_dbg ("TV freq (%d.%02d) out of range (%d-%d)\n",
661 freq / 16, freq % 16 * 100 / 16, tv_range[0],
662 tv_range[1]);
663 /* V4L2 spec: if the freq is not possible then the closest
664 possible value should be selected */
665 if (freq < tv_range[0] * 16)
666 freq = tv_range[0] * 16;
667 else
668 freq = tv_range[1] * 16;
669 }
670 params.frequency = freq;
671 tuner_dbg("tv freq set to %d.%02d\n",
672 freq / 16, freq % 16 * 100 / 16);
673 t->tv_freq = freq;
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -0300674 t->standby = false;
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300675
676 analog_ops->set_params(&t->fe, &params);
677}
678
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700679/* get more precise norm info from insmod option */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680static int tuner_fixup_std(struct tuner *t)
681{
682 if ((t->std & V4L2_STD_PAL) == V4L2_STD_PAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 switch (pal[0]) {
Hans Verkuile71ced12006-12-11 15:51:43 -0300684 case '6':
685 tuner_dbg ("insmod fixup: PAL => PAL-60\n");
686 t->std = V4L2_STD_PAL_60;
687 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 case 'b':
689 case 'B':
690 case 'g':
691 case 'G':
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700692 tuner_dbg ("insmod fixup: PAL => PAL-BG\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 t->std = V4L2_STD_PAL_BG;
694 break;
695 case 'i':
696 case 'I':
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700697 tuner_dbg ("insmod fixup: PAL => PAL-I\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 t->std = V4L2_STD_PAL_I;
699 break;
700 case 'd':
701 case 'D':
702 case 'k':
703 case 'K':
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700704 tuner_dbg ("insmod fixup: PAL => PAL-DK\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 t->std = V4L2_STD_PAL_DK;
706 break;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700707 case 'M':
708 case 'm':
709 tuner_dbg ("insmod fixup: PAL => PAL-M\n");
710 t->std = V4L2_STD_PAL_M;
711 break;
712 case 'N':
713 case 'n':
Mauro Carvalho Chehab7e578192006-01-09 15:25:27 -0200714 if (pal[1] == 'c' || pal[1] == 'C') {
715 tuner_dbg("insmod fixup: PAL => PAL-Nc\n");
716 t->std = V4L2_STD_PAL_Nc;
717 } else {
718 tuner_dbg ("insmod fixup: PAL => PAL-N\n");
719 t->std = V4L2_STD_PAL_N;
720 }
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700721 break;
Mauro Carvalho Chehab21d4df32005-09-09 13:03:59 -0700722 case '-':
723 /* default parameter, do nothing */
724 break;
725 default:
726 tuner_warn ("pal= argument not recognised\n");
727 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 }
729 }
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700730 if ((t->std & V4L2_STD_SECAM) == V4L2_STD_SECAM) {
731 switch (secam[0]) {
Mauro Carvalho Chehab7e578192006-01-09 15:25:27 -0200732 case 'b':
733 case 'B':
734 case 'g':
735 case 'G':
736 case 'h':
737 case 'H':
738 tuner_dbg("insmod fixup: SECAM => SECAM-BGH\n");
739 t->std = V4L2_STD_SECAM_B | V4L2_STD_SECAM_G | V4L2_STD_SECAM_H;
740 break;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700741 case 'd':
742 case 'D':
743 case 'k':
744 case 'K':
745 tuner_dbg ("insmod fixup: SECAM => SECAM-DK\n");
746 t->std = V4L2_STD_SECAM_DK;
747 break;
748 case 'l':
749 case 'L':
Mauro Carvalho Chehab800d3c62005-11-13 16:07:48 -0800750 if ((secam[1]=='C')||(secam[1]=='c')) {
751 tuner_dbg ("insmod fixup: SECAM => SECAM-L'\n");
752 t->std = V4L2_STD_SECAM_LC;
753 } else {
754 tuner_dbg ("insmod fixup: SECAM => SECAM-L\n");
755 t->std = V4L2_STD_SECAM_L;
756 }
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700757 break;
Mauro Carvalho Chehab21d4df32005-09-09 13:03:59 -0700758 case '-':
759 /* default parameter, do nothing */
760 break;
761 default:
762 tuner_warn ("secam= argument not recognised\n");
763 break;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700764 }
765 }
766
Mauro Carvalho Chehab7e578192006-01-09 15:25:27 -0200767 if ((t->std & V4L2_STD_NTSC) == V4L2_STD_NTSC) {
768 switch (ntsc[0]) {
769 case 'm':
770 case 'M':
771 tuner_dbg("insmod fixup: NTSC => NTSC-M\n");
772 t->std = V4L2_STD_NTSC_M;
773 break;
774 case 'j':
775 case 'J':
776 tuner_dbg("insmod fixup: NTSC => NTSC_M_JP\n");
777 t->std = V4L2_STD_NTSC_M_JP;
778 break;
Hans Verkuild97a11e2006-02-07 06:48:40 -0200779 case 'k':
780 case 'K':
781 tuner_dbg("insmod fixup: NTSC => NTSC_M_KR\n");
782 t->std = V4L2_STD_NTSC_M_KR;
783 break;
Mauro Carvalho Chehab7e578192006-01-09 15:25:27 -0200784 case '-':
785 /* default parameter, do nothing */
786 break;
787 default:
788 tuner_info("ntsc= argument not recognised\n");
789 break;
790 }
791 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 return 0;
793}
794
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300795/*
796 * Functions that are specific for Radio mode
797 */
798
799static void set_radio_freq(struct i2c_client *c, unsigned int freq)
800{
801 struct tuner *t = to_tuner(i2c_get_clientdata(c));
802 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
803
804 struct analog_parameters params = {
805 .mode = t->mode,
806 .audmode = t->audmode,
807 .std = t->std
808 };
809
810 if (t->type == UNSET) {
811 tuner_warn ("tuner type not set\n");
812 return;
813 }
814 if (NULL == analog_ops->set_params) {
815 tuner_warn ("tuner has no way to set radio frequency\n");
816 return;
817 }
818 if (freq < radio_range[0] * 16000 || freq > radio_range[1] * 16000) {
819 tuner_dbg ("radio freq (%d.%02d) out of range (%d-%d)\n",
820 freq / 16000, freq % 16000 * 100 / 16000,
821 radio_range[0], radio_range[1]);
822 /* V4L2 spec: if the freq is not possible then the closest
823 possible value should be selected */
824 if (freq < radio_range[0] * 16000)
825 freq = radio_range[0] * 16000;
826 else
827 freq = radio_range[1] * 16000;
828 }
829 params.frequency = freq;
830 tuner_dbg("radio freq set to %d.%02d\n",
831 freq / 16000, freq % 16000 * 100 / 16000);
832 t->radio_freq = freq;
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -0300833 t->standby = false;
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300834
835 analog_ops->set_params(&t->fe, &params);
836}
837
Mauro Carvalho Chehabe2d25a22011-02-04 10:09:07 -0300838/**
839 * check_mode - Verify if tuner supports the requested mode
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -0300840 * @t: a pointer to the module's internal struct_tuner
Mauro Carvalho Chehabe2d25a22011-02-04 10:09:07 -0300841 *
842 * This function checks if the tuner is capable of tuning analog TV,
843 * digital TV or radio, depending on what the caller wants. If the
844 * tuner can't support that mode, it returns -EINVAL. Otherwise, it
845 * returns 0.
846 * This function is needed for boards that have a separate tuner for
847 * radio (like devices with tea5767).
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300848 */
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -0300849static inline int check_mode(struct tuner *t, enum v4l2_tuner_type mode)
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300850{
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -0300851 if ((1 << mode & t->mode_mask) == 0) {
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300852 return -EINVAL;
853 }
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300854 return 0;
855}
856
Mauro Carvalho Chehabe2d25a22011-02-04 10:09:07 -0300857/**
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -0300858 * set_mode_freq - Switch tuner to other mode.
859 * @client: struct i2c_client pointer
860 * @t: a pointer to the module's internal struct_tuner
861 * @mode: enum v4l2_type (radio or TV)
862 * @freq: frequency to set (0 means to use the previous one)
Mauro Carvalho Chehabe2d25a22011-02-04 10:09:07 -0300863 *
864 * If tuner doesn't support the needed mode (radio or TV), prints a
865 * debug message and returns -EINVAL, changing internal state to T_STANDBY.
866 * Otherwise, changes the state and sets frequency to the last value, if
867 * the tuner can sleep or if it supports both Radio and TV.
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300868 */
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -0300869static int set_mode_freq(struct i2c_client *client, struct tuner *t,
870 enum v4l2_tuner_type mode, unsigned int freq)
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300871{
872 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
873
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -0300874 if (mode != t->mode) {
875 if (check_mode(t, mode) == -EINVAL) {
876 tuner_dbg("Tuner doesn't support mode %d. "
877 "Putting tuner to sleep\n", mode);
878 t->standby = true;
879 if (analog_ops->standby)
880 analog_ops->standby(&t->fe);
881 return -EINVAL;
882 }
883 t->mode = mode;
884 tuner_dbg("Changing to mode %d\n", mode);
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300885 }
Mauro Carvalho Chehabe2d25a22011-02-04 10:09:07 -0300886 if (t->mode == V4L2_TUNER_RADIO) {
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -0300887 if (freq)
888 t->radio_freq = freq;
889 set_radio_freq(client, t->radio_freq);
Mauro Carvalho Chehabe2d25a22011-02-04 10:09:07 -0300890 } else {
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -0300891 if (freq)
892 t->tv_freq = freq;
893 set_tv_freq(client, t->tv_freq);
Mauro Carvalho Chehabe2d25a22011-02-04 10:09:07 -0300894 }
895
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300896 return 0;
897}
898
Mauro Carvalho Chehabe2d25a22011-02-04 10:09:07 -0300899/*
900 * Functions that should be broken into separate radio/TV functions
901 */
902
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300903static void set_freq(struct i2c_client *c, unsigned long freq)
904{
905 struct tuner *t = to_tuner(i2c_get_clientdata(c));
906
907 switch (t->mode) {
908 case V4L2_TUNER_RADIO:
909 set_radio_freq(c, freq);
910 break;
911 case V4L2_TUNER_ANALOG_TV:
912 case V4L2_TUNER_DIGITAL_TV:
913 set_tv_freq(c, freq);
914 break;
915 default:
916 tuner_dbg("freq set: unknown mode: 0x%04x!\n",t->mode);
917 }
918}
919
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -0300920/**
921 * tuner_status - Dumps the current tuner status at dmesg
922 * @fe: pointer to struct dvb_frontend
923 *
924 * This callback is used only for driver debug purposes, answering to
925 * VIDIOC_LOG_STATUS. No changes should happen on this call.
926 */
Michael Krufky4e9154b2007-10-21 19:39:50 -0300927static void tuner_status(struct dvb_frontend *fe)
Mauro Carvalho Chehab7e578192006-01-09 15:25:27 -0200928{
Michael Krufky4e9154b2007-10-21 19:39:50 -0300929 struct tuner *t = fe->analog_demod_priv;
Mauro Carvalho Chehab7e578192006-01-09 15:25:27 -0200930 unsigned long freq, freq_fraction;
Michael Krufkya07c8772008-04-29 03:54:19 -0300931 struct dvb_tuner_ops *fe_tuner_ops = &fe->ops.tuner_ops;
932 struct analog_demod_ops *analog_ops = &fe->ops.analog_ops;
Mauro Carvalho Chehab7e578192006-01-09 15:25:27 -0200933 const char *p;
934
935 switch (t->mode) {
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -0300936 case V4L2_TUNER_RADIO:
937 p = "radio";
938 break;
939 case V4L2_TUNER_DIGITAL_TV:
940 p = "digital TV";
941 break;
942 case V4L2_TUNER_ANALOG_TV:
943 default:
944 p = "analog TV";
945 break;
Mauro Carvalho Chehab7e578192006-01-09 15:25:27 -0200946 }
947 if (t->mode == V4L2_TUNER_RADIO) {
Hans Verkuil27487d42006-01-15 15:04:52 -0200948 freq = t->radio_freq / 16000;
949 freq_fraction = (t->radio_freq % 16000) * 100 / 16000;
Mauro Carvalho Chehab7e578192006-01-09 15:25:27 -0200950 } else {
Hans Verkuil27487d42006-01-15 15:04:52 -0200951 freq = t->tv_freq / 16;
952 freq_fraction = (t->tv_freq % 16) * 100 / 16;
Mauro Carvalho Chehab7e578192006-01-09 15:25:27 -0200953 }
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -0300954 tuner_info("Tuner mode: %s%s\n", p,
955 t->standby ? " on standby mode" : "");
Mauro Carvalho Chehab7e578192006-01-09 15:25:27 -0200956 tuner_info("Frequency: %lu.%02lu MHz\n", freq, freq_fraction);
Mauro Carvalho Chehab4ae5c2e2006-03-25 15:53:38 -0300957 tuner_info("Standard: 0x%08lx\n", (unsigned long)t->std);
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200958 if (t->mode != V4L2_TUNER_RADIO)
959 return;
Michael Krufkye18f9442007-08-21 01:25:48 -0300960 if (fe_tuner_ops->get_status) {
961 u32 tuner_status;
962
963 fe_tuner_ops->get_status(&t->fe, &tuner_status);
964 if (tuner_status & TUNER_STATUS_LOCKED)
965 tuner_info("Tuner is locked.\n");
966 if (tuner_status & TUNER_STATUS_STEREO)
967 tuner_info("Stereo: yes\n");
968 }
Michael Krufkybc3e5c72007-12-21 11:18:32 -0300969 if (analog_ops->has_signal)
970 tuner_info("Signal strength: %d\n",
971 analog_ops->has_signal(fe));
Mauro Carvalho Chehab7e578192006-01-09 15:25:27 -0200972}
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200973
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -0300974/**
975 * tuner_s_power - controls the power state of the tuner
976 * @sd: pointer to struct v4l2_subdev
977 * @on: a zero value puts the tuner to sleep
978 */
Laurent Pinchart622b8282009-10-05 10:48:17 -0300979static int tuner_s_power(struct v4l2_subdev *sd, int on)
Hans Verkuile8a4a9e2008-11-24 18:21:40 -0300980{
981 struct tuner *t = to_tuner(sd);
Michael Krufkybc3e5c72007-12-21 11:18:32 -0300982 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -0300984 /* FIXME: Why this function don't wake the tuner if on != 0 ? */
Laurent Pinchart622b8282009-10-05 10:48:17 -0300985 if (on)
986 return 0;
987
Mauro Carvalho Chehab16a5e532008-12-20 07:17:10 -0300988 tuner_dbg("Putting tuner to sleep\n");
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -0300989 t->standby = true;
Hans Verkuile8a4a9e2008-11-24 18:21:40 -0300990 if (analog_ops->standby)
991 analog_ops->standby(&t->fe);
992 return 0;
993}
994
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300995/* ---------------------------------------------------------------------- */
996
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -0300997static int tuner_s_radio(struct v4l2_subdev *sd)
Hans Verkuile8a4a9e2008-11-24 18:21:40 -0300998{
999 struct tuner *t = to_tuner(sd);
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -03001000 struct i2c_client *client = v4l2_get_subdevdata(sd);
Mauro Carvalho Chehab7f171122007-10-18 19:56:47 -03001001
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -03001002 if (set_mode_freq(client, t, V4L2_TUNER_RADIO, 0) == -EINVAL)
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001003 return 0;
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001004 return 0;
1005}
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -07001006
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001007/* --- v4l ioctls --- */
1008/* take care: bttv does userspace copying, we'll get a
1009 kernel pointer here... */
1010static int tuner_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
1011{
1012 struct tuner *t = to_tuner(sd);
1013 struct i2c_client *client = v4l2_get_subdevdata(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -03001015 if (set_mode_freq(client, t, V4L2_TUNER_ANALOG_TV, 0) == -EINVAL)
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001016 return 0;
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -07001017
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001018 t->std = std;
1019 tuner_fixup_std(t);
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -03001020
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001021 return 0;
1022}
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -07001023
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001024static int tuner_s_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f)
1025{
1026 struct tuner *t = to_tuner(sd);
1027 struct i2c_client *client = v4l2_get_subdevdata(sd);
Michael Krufkye18f9442007-08-21 01:25:48 -03001028
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -03001029 if (set_mode_freq(client, t, f->type, f->frequency) == -EINVAL)
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001030 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
1032 return 0;
1033}
1034
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001035static int tuner_g_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f)
1036{
1037 struct tuner *t = to_tuner(sd);
1038 struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops;
1039
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -03001040 if (check_mode(t, f->type) == -EINVAL)
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001041 return 0;
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001042 f->type = t->mode;
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -03001043 if (fe_tuner_ops->get_frequency && !t->standby) {
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001044 u32 abs_freq;
1045
1046 fe_tuner_ops->get_frequency(&t->fe, &abs_freq);
1047 f->frequency = (V4L2_TUNER_RADIO == t->mode) ?
Julia Lawall75b697f2009-08-01 16:48:41 -03001048 DIV_ROUND_CLOSEST(abs_freq * 2, 125) :
1049 DIV_ROUND_CLOSEST(abs_freq, 62500);
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -03001050 } else {
1051 f->frequency = (V4L2_TUNER_RADIO == t->mode) ?
1052 t->radio_freq : t->tv_freq;
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001053 }
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001054 return 0;
1055}
1056
1057static int tuner_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
1058{
1059 struct tuner *t = to_tuner(sd);
1060 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
1061 struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops;
1062
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -03001063 if (check_mode(t, vt->type) == -EINVAL)
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001064 return 0;
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001065 vt->type = t->mode;
1066 if (analog_ops->get_afc)
1067 vt->afc = analog_ops->get_afc(&t->fe);
1068 if (t->mode == V4L2_TUNER_ANALOG_TV)
1069 vt->capability |= V4L2_TUNER_CAP_NORM;
1070 if (t->mode != V4L2_TUNER_RADIO) {
1071 vt->rangelow = tv_range[0] * 16;
1072 vt->rangehigh = tv_range[1] * 16;
1073 return 0;
1074 }
1075
1076 /* radio mode */
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -03001077 vt->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001078 if (fe_tuner_ops->get_status) {
1079 u32 tuner_status;
1080
1081 fe_tuner_ops->get_status(&t->fe, &tuner_status);
1082 vt->rxsubchans =
1083 (tuner_status & TUNER_STATUS_STEREO) ?
1084 V4L2_TUNER_SUB_STEREO :
1085 V4L2_TUNER_SUB_MONO;
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001086 }
1087 if (analog_ops->has_signal)
1088 vt->signal = analog_ops->has_signal(&t->fe);
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -03001089 vt->capability |= V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001090 vt->audmode = t->audmode;
1091 vt->rangelow = radio_range[0] * 16000;
1092 vt->rangehigh = radio_range[1] * 16000;
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -03001093
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001094 return 0;
1095}
1096
1097static int tuner_s_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
1098{
1099 struct tuner *t = to_tuner(sd);
1100 struct i2c_client *client = v4l2_get_subdevdata(sd);
1101
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -03001102 if (set_mode_freq(client, t, vt->type, 0) == -EINVAL)
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001103 return 0;
1104
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -03001105 if (t->mode == V4L2_TUNER_RADIO)
1106 t->audmode = vt->audmode;
1107
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001108 return 0;
1109}
1110
1111static int tuner_log_status(struct v4l2_subdev *sd)
1112{
1113 struct tuner *t = to_tuner(sd);
1114 struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
1115
1116 if (analog_ops->tuner_status)
1117 analog_ops->tuner_status(&t->fe);
1118 return 0;
1119}
1120
Jean Delvare21b48a72007-03-12 19:20:15 -03001121static int tuner_suspend(struct i2c_client *c, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122{
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001123 struct tuner *t = to_tuner(i2c_get_clientdata(c));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
Hans Verkuil9dd659d2007-11-04 11:03:36 -03001125 tuner_dbg("suspend\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 /* FIXME: power down ??? */
1127 return 0;
1128}
1129
Jean Delvare21b48a72007-03-12 19:20:15 -03001130static int tuner_resume(struct i2c_client *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131{
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001132 struct tuner *t = to_tuner(i2c_get_clientdata(c));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
Hans Verkuil9dd659d2007-11-04 11:03:36 -03001134 tuner_dbg("resume\n");
Mauro Carvalho Chehabcbde6892011-02-04 10:42:09 -03001135 if (V4L2_TUNER_RADIO == t->mode)
1136 set_freq(c, t->radio_freq);
1137 else
1138 set_freq(c, t->tv_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 return 0;
1140}
1141
Hans Verkuil75b4c262009-04-01 03:32:22 -03001142static int tuner_command(struct i2c_client *client, unsigned cmd, void *arg)
1143{
1144 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1145
1146 /* TUNER_SET_CONFIG is still called by tuner-simple.c, so we have
1147 to handle it here.
1148 There must be a better way of doing this... */
1149 switch (cmd) {
1150 case TUNER_SET_CONFIG:
1151 return tuner_s_config(sd, arg);
1152 }
1153 return -ENOIOCTLCMD;
1154}
1155
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001156/* ----------------------------------------------------------------------- */
1157
1158static const struct v4l2_subdev_core_ops tuner_core_ops = {
1159 .log_status = tuner_log_status,
Hans Verkuilf41737e2009-04-01 03:52:39 -03001160 .s_std = tuner_s_std,
Laurent Pinchart622b8282009-10-05 10:48:17 -03001161 .s_power = tuner_s_power,
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001162};
1163
1164static const struct v4l2_subdev_tuner_ops tuner_tuner_ops = {
Hans Verkuile8a4a9e2008-11-24 18:21:40 -03001165 .s_radio = tuner_s_radio,
1166 .g_tuner = tuner_g_tuner,
1167 .s_tuner = tuner_s_tuner,
1168 .s_frequency = tuner_s_frequency,
1169 .g_frequency = tuner_g_frequency,
1170 .s_type_addr = tuner_s_type_addr,
1171 .s_config = tuner_s_config,
1172};
1173
1174static const struct v4l2_subdev_ops tuner_ops = {
1175 .core = &tuner_core_ops,
1176 .tuner = &tuner_tuner_ops,
1177};
1178
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179/* ----------------------------------------------------------------------- */
1180
Jean Delvareaf294862008-05-18 20:49:40 +02001181/* This driver supports many devices and the idea is to let the driver
1182 detect which device is present. So rather than listing all supported
1183 devices here, we pretend to support a single, fake device type. */
1184static const struct i2c_device_id tuner_id[] = {
1185 { "tuner", }, /* autodetect */
1186 { }
1187};
1188MODULE_DEVICE_TABLE(i2c, tuner_id);
1189
Hans Verkuil02a20982010-09-15 15:36:23 -03001190static struct i2c_driver tuner_driver = {
1191 .driver = {
1192 .owner = THIS_MODULE,
1193 .name = "tuner",
1194 },
1195 .probe = tuner_probe,
1196 .remove = tuner_remove,
1197 .command = tuner_command,
1198 .suspend = tuner_suspend,
1199 .resume = tuner_resume,
1200 .id_table = tuner_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201};
1202
Hans Verkuil02a20982010-09-15 15:36:23 -03001203static __init int init_tuner(void)
1204{
1205 return i2c_add_driver(&tuner_driver);
1206}
1207
1208static __exit void exit_tuner(void)
1209{
1210 i2c_del_driver(&tuner_driver);
1211}
1212
1213module_init(init_tuner);
1214module_exit(exit_tuner);
1215
Mauro Carvalho Chehab9f3f71e2011-02-03 23:32:07 -03001216MODULE_DESCRIPTION("device driver for various TV and TV+FM radio tuners");
1217MODULE_AUTHOR("Ralph Metzler, Gerd Knorr, Gunther Mayer");
1218MODULE_LICENSE("GPL");