blob: 9de66769b62301b3b4dd006d85281238b55bd9fc [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>
Michael Krufkyffbb8072007-08-21 01:14:12 -030018#include <linux/videodev.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>
Michael Krufky5e453dc2006-01-09 15:32:31 -020021#include <media/v4l2-common.h>
Michael Krufky8218b0b2007-06-26 13:12:08 -030022#include "tuner-driver.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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30#define UNSET (-1U)
31
32/* standard i2c insmod options */
33static unsigned short normal_i2c[] = {
Adrian Bunk04d934f2007-10-24 09:06:47 -030034#if defined(CONFIG_TUNER_TEA5761) || (defined(CONFIG_TUNER_TEA5761_MODULE) && defined(MODULE))
Mauro Carvalho Chehab8573a9e2007-04-08 01:09:11 -030035 0x10,
36#endif
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -080037 0x42, 0x43, 0x4a, 0x4b, /* tda8290 */
Mauro Carvalho Chehabf5bec392005-06-23 22:05:13 -070038 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
39 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 I2C_CLIENT_END
41};
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -070042
Linus Torvalds1da177e2005-04-16 15:20:36 -070043I2C_CLIENT_INSMOD;
44
45/* insmod options used at init time => read/only */
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -070046static unsigned int addr = 0;
Mauro Carvalho Chehabc5287ba2005-07-15 03:56:28 -070047static unsigned int no_autodetect = 0;
Mauro Carvalho Chehabfd3113e2005-07-31 22:34:43 -070048static unsigned int show_i2c = 0;
Mauro Carvalho Chehabfd3113e2005-07-31 22:34:43 -070049
Linus Torvalds1da177e2005-04-16 15:20:36 -070050/* insmod options used at runtime => read/write */
Hans Verkuilf9195de2006-01-11 19:01:01 -020051int tuner_debug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -070053static unsigned int tv_range[2] = { 44, 958 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070054static unsigned int radio_range[2] = { 65, 108 };
55
Mauro Carvalho Chehab7e578192006-01-09 15:25:27 -020056static char pal[] = "--";
57static char secam[] = "--";
58static char ntsc[] = "-";
59
Hans Verkuilf9195de2006-01-11 19:01:01 -020060
Mauro Carvalho Chehab7e578192006-01-09 15:25:27 -020061module_param(addr, int, 0444);
62module_param(no_autodetect, int, 0444);
63module_param(show_i2c, int, 0444);
Hans Verkuilf9195de2006-01-11 19:01:01 -020064module_param_named(debug,tuner_debug, int, 0644);
Mauro Carvalho Chehab7e578192006-01-09 15:25:27 -020065module_param_string(pal, pal, sizeof(pal), 0644);
66module_param_string(secam, secam, sizeof(secam), 0644);
67module_param_string(ntsc, ntsc, sizeof(ntsc), 0644);
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -070068module_param_array(tv_range, int, NULL, 0644);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069module_param_array(radio_range, int, NULL, 0644);
70
71MODULE_DESCRIPTION("device driver for various TV and TV+FM radio tuners");
72MODULE_AUTHOR("Ralph Metzler, Gerd Knorr, Gunther Mayer");
73MODULE_LICENSE("GPL");
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075static struct i2c_driver driver;
76static struct i2c_client client_template;
77
78/* ---------------------------------------------------------------------- */
79
Michael Krufkye18f9442007-08-21 01:25:48 -030080static void fe_set_freq(struct tuner *t, unsigned int freq)
81{
82 struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops;
83
84 struct analog_parameters params = {
85 .frequency = freq,
86 .mode = t->mode,
87 .audmode = t->audmode,
88 .std = t->std
89 };
90
91 if (NULL == fe_tuner_ops->set_analog_params) {
92 tuner_warn("Tuner frontend module has no way to set freq\n");
93 return;
94 }
95 fe_tuner_ops->set_analog_params(&t->fe, &params);
96}
97
98static void fe_release(struct tuner *t)
99{
100 struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops;
101
102 if (fe_tuner_ops->release)
103 fe_tuner_ops->release(&t->fe);
Michael Krufkye2be32a2007-10-21 14:35:21 -0300104
105 t->fe.ops.analog_demod_ops = NULL;
Michael Krufkye18f9442007-08-21 01:25:48 -0300106}
107
108static void fe_standby(struct tuner *t)
109{
110 struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops;
111
112 if (fe_tuner_ops->sleep)
113 fe_tuner_ops->sleep(&t->fe);
114}
115
Michael Krufky1f5ef192007-08-31 17:38:02 -0300116static int fe_has_signal(struct tuner *t)
117{
118 struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops;
Michael Krufky14196832007-10-14 18:11:53 -0300119 u16 strength = 0;
Michael Krufky1f5ef192007-08-31 17:38:02 -0300120
121 if (fe_tuner_ops->get_rf_strength)
122 fe_tuner_ops->get_rf_strength(&t->fe, &strength);
123
124 return strength;
125}
126
Michael Krufky1dde7a42007-10-21 13:40:56 -0300127static void tuner_status(struct tuner *t);
128
129static struct analog_tuner_ops tuner_core_ops = {
130 .set_tv_freq = fe_set_freq,
131 .set_radio_freq = fe_set_freq,
132 .standby = fe_standby,
133 .release = fe_release,
134 .has_signal = fe_has_signal,
135 .tuner_status = tuner_status
136};
137
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700138/* Set tuner frequency, freq in Units of 62.5kHz = 1/16MHz */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139static void set_tv_freq(struct i2c_client *c, unsigned int freq)
140{
141 struct tuner *t = i2c_get_clientdata(c);
Michael Krufky1dde7a42007-10-21 13:40:56 -0300142 struct analog_tuner_ops *ops = t->fe.ops.analog_demod_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144 if (t->type == UNSET) {
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700145 tuner_warn ("tuner type not set\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 return;
147 }
Michael Krufky1dde7a42007-10-21 13:40:56 -0300148 if ((NULL == ops) || (NULL == ops->set_tv_freq)) {
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700149 tuner_warn ("Tuner has no way to set tv freq\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 return;
151 }
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700152 if (freq < tv_range[0] * 16 || freq > tv_range[1] * 16) {
153 tuner_dbg ("TV freq (%d.%02d) out of range (%d-%d)\n",
154 freq / 16, freq % 16 * 100 / 16, tv_range[0],
155 tv_range[1]);
Hans Verkuil27487d42006-01-15 15:04:52 -0200156 /* V4L2 spec: if the freq is not possible then the closest
157 possible value should be selected */
158 if (freq < tv_range[0] * 16)
159 freq = tv_range[0] * 16;
160 else
161 freq = tv_range[1] * 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 }
Michael Krufky1dde7a42007-10-21 13:40:56 -0300163 ops->set_tv_freq(t, freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164}
165
166static void set_radio_freq(struct i2c_client *c, unsigned int freq)
167{
168 struct tuner *t = i2c_get_clientdata(c);
Michael Krufky1dde7a42007-10-21 13:40:56 -0300169 struct analog_tuner_ops *ops = t->fe.ops.analog_demod_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171 if (t->type == UNSET) {
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700172 tuner_warn ("tuner type not set\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 return;
174 }
Michael Krufky1dde7a42007-10-21 13:40:56 -0300175 if ((NULL == ops) || (NULL == ops->set_radio_freq)) {
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700176 tuner_warn ("tuner has no way to set radio frequency\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 return;
178 }
Hans Verkuil27487d42006-01-15 15:04:52 -0200179 if (freq < radio_range[0] * 16000 || freq > radio_range[1] * 16000) {
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700180 tuner_dbg ("radio freq (%d.%02d) out of range (%d-%d)\n",
181 freq / 16000, freq % 16000 * 100 / 16000,
182 radio_range[0], radio_range[1]);
Hans Verkuil27487d42006-01-15 15:04:52 -0200183 /* V4L2 spec: if the freq is not possible then the closest
184 possible value should be selected */
185 if (freq < radio_range[0] * 16000)
186 freq = radio_range[0] * 16000;
187 else
188 freq = radio_range[1] * 16000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 }
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700190
Michael Krufky1dde7a42007-10-21 13:40:56 -0300191 ops->set_radio_freq(t, freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192}
193
194static void set_freq(struct i2c_client *c, unsigned long freq)
195{
196 struct tuner *t = i2c_get_clientdata(c);
197
198 switch (t->mode) {
199 case V4L2_TUNER_RADIO:
200 tuner_dbg("radio freq set to %lu.%02lu\n",
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700201 freq / 16000, freq % 16000 * 100 / 16000);
202 set_radio_freq(c, freq);
Hans Verkuil27487d42006-01-15 15:04:52 -0200203 t->radio_freq = freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 break;
205 case V4L2_TUNER_ANALOG_TV:
206 case V4L2_TUNER_DIGITAL_TV:
207 tuner_dbg("tv freq set to %lu.%02lu\n",
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700208 freq / 16, freq % 16 * 100 / 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 set_tv_freq(c, freq);
Hans Verkuil27487d42006-01-15 15:04:52 -0200210 t->tv_freq = freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 break;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300212 default:
213 tuner_dbg("freq set: unknown mode: 0x%04x!\n",t->mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215}
216
Michael Krufky293197c2007-08-28 17:20:42 -0300217static void tuner_i2c_address_check(struct tuner *t)
218{
219 if ((t->type == UNSET || t->type == TUNER_ABSENT) ||
220 ((t->i2c.addr < 0x64) || (t->i2c.addr > 0x6f)))
221 return;
222
223 tuner_warn("====================== WARNING! ======================\n");
224 tuner_warn("Support for tuners in i2c address range 0x64 thru 0x6f\n");
225 tuner_warn("will soon be dropped. This message indicates that your\n");
226 tuner_warn("hardware has a %s tuner at i2c address 0x%02x.\n",
227 t->i2c.name, t->i2c.addr);
228 tuner_warn("To ensure continued support for your device, please\n");
229 tuner_warn("send a copy of this message, along with full dmesg\n");
230 tuner_warn("output to v4l-dvb-maintainer@linuxtv.org\n");
231 tuner_warn("Please use subject line: \"obsolete tuner i2c address.\"\n");
232 tuner_warn("driver: %s, addr: 0x%02x, type: %d (%s)\n",
233 t->i2c.adapter->name, t->i2c.addr, t->type,
234 tuners[t->type].name);
235 tuner_warn("====================== WARNING! ======================\n");
236}
237
Michael Krufky4adad282007-08-27 21:59:08 -0300238static void attach_simple_tuner(struct tuner *t)
239{
240 struct simple_tuner_config cfg = {
241 .type = t->type,
242 .tun = &tuners[t->type]
243 };
244 simple_tuner_attach(&t->fe, t->i2c.adapter, t->i2c.addr, &cfg);
245}
246
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700247static void set_type(struct i2c_client *c, unsigned int type,
Hartmut Hackmannde956c12007-04-27 12:31:12 -0300248 unsigned int new_mode_mask, unsigned int new_config,
Hartmut Hackmanncfeb8832007-04-27 12:31:17 -0300249 int (*tuner_callback) (void *dev, int command,int arg))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
251 struct tuner *t = i2c_get_clientdata(c);
Michael Krufkye18f9442007-08-21 01:25:48 -0300252 struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops;
Michael Krufky1dde7a42007-10-21 13:40:56 -0300253 struct analog_tuner_ops *ops = t->fe.ops.analog_demod_ops;
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700254 unsigned char buffer[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700256 if (type == UNSET || type == TUNER_ABSENT) {
257 tuner_dbg ("tuner 0x%02x: Tuner type absent\n",c->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 return;
259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700261 if (type >= tuner_count) {
262 tuner_warn ("tuner 0x%02x: Tuner count greater than %d\n",c->addr,tuner_count);
263 return;
264 }
265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 t->type = type;
Hartmut Hackmanncfeb8832007-04-27 12:31:17 -0300267 t->config = new_config;
268 if (tuner_callback != NULL) {
269 tuner_dbg("defining GPIO callback\n");
270 t->tuner_callback = tuner_callback;
271 }
Hartmut Hackmann80f90fb2007-04-27 12:31:18 -0300272
273 /* This code detects calls by card attach_inform */
274 if (NULL == t->i2c.dev.driver) {
275 tuner_dbg ("tuner 0x%02x: called during i2c_client register by adapter's attach_inform\n", c->addr);
276
277 return;
278 }
279
Michael Krufkyb2083192007-05-29 22:54:06 -0300280 /* discard private data, in case set_type() was previously called */
Michael Krufky1dde7a42007-10-21 13:40:56 -0300281 if ((ops) && (ops->release))
282 ops->release(t);
Michael Krufky052c50d2007-06-04 16:00:45 -0300283 else {
284 kfree(t->priv);
285 t->priv = NULL;
286 }
Michael Krufkybe2b85a2007-06-04 14:40:27 -0300287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 switch (t->type) {
289 case TUNER_MT2032:
Michael Krufky96c0b7c2007-08-27 21:23:08 -0300290 microtune_attach(&t->fe, t->i2c.adapter, t->i2c.addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 break;
292 case TUNER_PHILIPS_TDA8290:
Michael Krufky910bb3e2007-08-27 21:22:20 -0300293 {
Michael Krufky746d97322007-08-25 19:08:45 -0300294 tda8290_attach(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 break;
Michael Krufky910bb3e2007-08-27 21:22:20 -0300296 }
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300297 case TUNER_PHILIPS_TDA8295:
298 {
299 tda8295_attach(t);
300 break;
301 }
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700302 case TUNER_TEA5767:
Michael Krufky8d0936e2007-08-27 21:24:27 -0300303 if (tea5767_attach(&t->fe, t->i2c.adapter, t->i2c.addr) == NULL) {
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700304 t->type = TUNER_ABSENT;
305 t->mode_mask = T_UNINITIALIZED;
306 return;
307 }
308 t->mode_mask = T_RADIO;
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700309 break;
Mauro Carvalho Chehab8573a9e2007-04-08 01:09:11 -0300310 case TUNER_TEA5761:
Michael Krufky7ab10bf2007-08-27 21:23:40 -0300311 if (tea5761_attach(&t->fe, t->i2c.adapter, t->i2c.addr) == NULL) {
Mauro Carvalho Chehab8573a9e2007-04-08 01:09:11 -0300312 t->type = TUNER_ABSENT;
313 t->mode_mask = T_UNINITIALIZED;
Adrian Bunk9ee476a2007-06-05 05:22:00 -0300314 return;
Mauro Carvalho Chehab8573a9e2007-04-08 01:09:11 -0300315 }
316 t->mode_mask = T_RADIO;
317 break;
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700318 case TUNER_PHILIPS_FMD1216ME_MK3:
319 buffer[0] = 0x0b;
320 buffer[1] = 0xdc;
321 buffer[2] = 0x9c;
322 buffer[3] = 0x60;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700323 i2c_master_send(c, buffer, 4);
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700324 mdelay(1);
325 buffer[2] = 0x86;
326 buffer[3] = 0x54;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700327 i2c_master_send(c, buffer, 4);
Michael Krufky4adad282007-08-27 21:59:08 -0300328 attach_simple_tuner(t);
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700329 break;
Hartmut Hackmann93df3412005-11-08 21:36:31 -0800330 case TUNER_PHILIPS_TD1316:
331 buffer[0] = 0x0b;
332 buffer[1] = 0xdc;
333 buffer[2] = 0x86;
334 buffer[3] = 0xa4;
335 i2c_master_send(c,buffer,4);
Michael Krufky4adad282007-08-27 21:59:08 -0300336 attach_simple_tuner(t);
Markus Rechbergerac272ed2006-01-23 17:11:09 -0200337 break;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300338 case TUNER_XC2028:
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300339 {
340 int rc=xc2028_attach(&t->fe, t->i2c.adapter, t->i2c.addr,
341 &c->dev, c->adapter->algo_data,
342 t->tuner_callback);
343 if (rc<0) {
344 t->type = TUNER_ABSENT;
345 t->mode_mask = T_UNINITIALIZED;
346 return;
347 }
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300348 break;
Mauro Carvalho Chehab215b95b2007-10-23 15:24:06 -0300349 }
Mauro Carvalho Chehab15396232006-06-23 16:13:56 -0300350 case TUNER_TDA9887:
Michael Krufkydb8a6952007-08-21 01:24:42 -0300351 tda9887_tuner_init(t);
Mauro Carvalho Chehab15396232006-06-23 16:13:56 -0300352 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 default:
Michael Krufky4adad282007-08-27 21:59:08 -0300354 attach_simple_tuner(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 break;
356 }
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700357
Michael Krufkye2be32a2007-10-21 14:35:21 -0300358 ops = t->fe.ops.analog_demod_ops;
359
Michael Krufky1dde7a42007-10-21 13:40:56 -0300360 if (((NULL == ops) ||
361 ((NULL == ops->set_tv_freq) && (NULL == ops->set_radio_freq))) &&
362 (fe_tuner_ops->set_analog_params)) {
Michael Krufkye18f9442007-08-21 01:25:48 -0300363 strlcpy(t->i2c.name, fe_tuner_ops->info.name, sizeof(t->i2c.name));
364
Michael Krufky1dde7a42007-10-21 13:40:56 -0300365 t->fe.ops.analog_demod_ops = &tuner_core_ops;
Michael Krufkye18f9442007-08-21 01:25:48 -0300366 }
367
368 tuner_info("type set to %s\n", t->i2c.name);
369
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700370 if (t->mode_mask == T_UNINITIALIZED)
371 t->mode_mask = new_mode_mask;
372
Hans Verkuil27487d42006-01-15 15:04:52 -0200373 set_freq(c, (V4L2_TUNER_RADIO == t->mode) ? t->radio_freq : t->tv_freq);
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700374 tuner_dbg("%s %s I2C addr 0x%02x with type %d used for 0x%02x\n",
Laurent Riffard604f28e2005-11-26 20:43:39 +0100375 c->adapter->name, c->driver->driver.name, c->addr << 1, type,
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700376 t->mode_mask);
Michael Krufky293197c2007-08-28 17:20:42 -0300377 tuner_i2c_address_check(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378}
379
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700380/*
381 * This function apply tuner config to tuner specified
382 * by tun_setup structure. I addr is unset, then admin status
383 * and tun addr status is more precise then current status,
384 * it's applied. Otherwise status and type are applied only to
385 * tuner with exactly the same addr.
386*/
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700387
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700388static void set_addr(struct i2c_client *c, struct tuner_setup *tun_setup)
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700389{
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700390 struct tuner *t = i2c_get_clientdata(c);
391
Mauro Carvalho Chehab15396232006-06-23 16:13:56 -0300392 tuner_dbg("set addr for type %i\n", t->type);
393
Hartmut Hackmannde956c12007-04-27 12:31:12 -0300394 if ( (t->type == UNSET && ((tun_setup->addr == ADDR_UNSET) &&
395 (t->mode_mask & tun_setup->mode_mask))) ||
396 (tun_setup->addr == c->addr)) {
397 set_type(c, tun_setup->type, tun_setup->mode_mask,
Hartmut Hackmanncfeb8832007-04-27 12:31:17 -0300398 tun_setup->config, tun_setup->tuner_callback);
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700399 }
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700400}
401
402static inline int check_mode(struct tuner *t, char *cmd)
403{
Mauro Carvalho Chehab793cf9e2005-09-09 13:03:37 -0700404 if ((1 << t->mode & t->mode_mask) == 0) {
405 return EINVAL;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700406 }
Mauro Carvalho Chehab793cf9e2005-09-09 13:03:37 -0700407
408 switch (t->mode) {
409 case V4L2_TUNER_RADIO:
410 tuner_dbg("Cmd %s accepted for radio\n", cmd);
411 break;
412 case V4L2_TUNER_ANALOG_TV:
413 tuner_dbg("Cmd %s accepted for analog TV\n", cmd);
414 break;
415 case V4L2_TUNER_DIGITAL_TV:
416 tuner_dbg("Cmd %s accepted for digital TV\n", cmd);
417 break;
418 }
419 return 0;
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700420}
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700421
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700422/* get more precise norm info from insmod option */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423static int tuner_fixup_std(struct tuner *t)
424{
425 if ((t->std & V4L2_STD_PAL) == V4L2_STD_PAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 switch (pal[0]) {
Hans Verkuile71ced12006-12-11 15:51:43 -0300427 case '6':
428 tuner_dbg ("insmod fixup: PAL => PAL-60\n");
429 t->std = V4L2_STD_PAL_60;
430 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 case 'b':
432 case 'B':
433 case 'g':
434 case 'G':
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700435 tuner_dbg ("insmod fixup: PAL => PAL-BG\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 t->std = V4L2_STD_PAL_BG;
437 break;
438 case 'i':
439 case 'I':
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700440 tuner_dbg ("insmod fixup: PAL => PAL-I\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 t->std = V4L2_STD_PAL_I;
442 break;
443 case 'd':
444 case 'D':
445 case 'k':
446 case 'K':
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700447 tuner_dbg ("insmod fixup: PAL => PAL-DK\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 t->std = V4L2_STD_PAL_DK;
449 break;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700450 case 'M':
451 case 'm':
452 tuner_dbg ("insmod fixup: PAL => PAL-M\n");
453 t->std = V4L2_STD_PAL_M;
454 break;
455 case 'N':
456 case 'n':
Mauro Carvalho Chehab7e578192006-01-09 15:25:27 -0200457 if (pal[1] == 'c' || pal[1] == 'C') {
458 tuner_dbg("insmod fixup: PAL => PAL-Nc\n");
459 t->std = V4L2_STD_PAL_Nc;
460 } else {
461 tuner_dbg ("insmod fixup: PAL => PAL-N\n");
462 t->std = V4L2_STD_PAL_N;
463 }
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700464 break;
Mauro Carvalho Chehab21d4df32005-09-09 13:03:59 -0700465 case '-':
466 /* default parameter, do nothing */
467 break;
468 default:
469 tuner_warn ("pal= argument not recognised\n");
470 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 }
472 }
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700473 if ((t->std & V4L2_STD_SECAM) == V4L2_STD_SECAM) {
474 switch (secam[0]) {
Mauro Carvalho Chehab7e578192006-01-09 15:25:27 -0200475 case 'b':
476 case 'B':
477 case 'g':
478 case 'G':
479 case 'h':
480 case 'H':
481 tuner_dbg("insmod fixup: SECAM => SECAM-BGH\n");
482 t->std = V4L2_STD_SECAM_B | V4L2_STD_SECAM_G | V4L2_STD_SECAM_H;
483 break;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700484 case 'd':
485 case 'D':
486 case 'k':
487 case 'K':
488 tuner_dbg ("insmod fixup: SECAM => SECAM-DK\n");
489 t->std = V4L2_STD_SECAM_DK;
490 break;
491 case 'l':
492 case 'L':
Mauro Carvalho Chehab800d3c62005-11-13 16:07:48 -0800493 if ((secam[1]=='C')||(secam[1]=='c')) {
494 tuner_dbg ("insmod fixup: SECAM => SECAM-L'\n");
495 t->std = V4L2_STD_SECAM_LC;
496 } else {
497 tuner_dbg ("insmod fixup: SECAM => SECAM-L\n");
498 t->std = V4L2_STD_SECAM_L;
499 }
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700500 break;
Mauro Carvalho Chehab21d4df32005-09-09 13:03:59 -0700501 case '-':
502 /* default parameter, do nothing */
503 break;
504 default:
505 tuner_warn ("secam= argument not recognised\n");
506 break;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700507 }
508 }
509
Mauro Carvalho Chehab7e578192006-01-09 15:25:27 -0200510 if ((t->std & V4L2_STD_NTSC) == V4L2_STD_NTSC) {
511 switch (ntsc[0]) {
512 case 'm':
513 case 'M':
514 tuner_dbg("insmod fixup: NTSC => NTSC-M\n");
515 t->std = V4L2_STD_NTSC_M;
516 break;
517 case 'j':
518 case 'J':
519 tuner_dbg("insmod fixup: NTSC => NTSC_M_JP\n");
520 t->std = V4L2_STD_NTSC_M_JP;
521 break;
Hans Verkuild97a11e2006-02-07 06:48:40 -0200522 case 'k':
523 case 'K':
524 tuner_dbg("insmod fixup: NTSC => NTSC_M_KR\n");
525 t->std = V4L2_STD_NTSC_M_KR;
526 break;
Mauro Carvalho Chehab7e578192006-01-09 15:25:27 -0200527 case '-':
528 /* default parameter, do nothing */
529 break;
530 default:
531 tuner_info("ntsc= argument not recognised\n");
532 break;
533 }
534 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 return 0;
536}
537
Michael Krufkydb8a6952007-08-21 01:24:42 -0300538static void tuner_status(struct tuner *t)
Mauro Carvalho Chehab7e578192006-01-09 15:25:27 -0200539{
Mauro Carvalho Chehab7e578192006-01-09 15:25:27 -0200540 unsigned long freq, freq_fraction;
Michael Krufkye18f9442007-08-21 01:25:48 -0300541 struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops;
Michael Krufky1dde7a42007-10-21 13:40:56 -0300542 struct analog_tuner_ops *ops = t->fe.ops.analog_demod_ops;
Mauro Carvalho Chehab7e578192006-01-09 15:25:27 -0200543 const char *p;
544
545 switch (t->mode) {
546 case V4L2_TUNER_RADIO: p = "radio"; break;
547 case V4L2_TUNER_ANALOG_TV: p = "analog TV"; break;
548 case V4L2_TUNER_DIGITAL_TV: p = "digital TV"; break;
549 default: p = "undefined"; break;
550 }
551 if (t->mode == V4L2_TUNER_RADIO) {
Hans Verkuil27487d42006-01-15 15:04:52 -0200552 freq = t->radio_freq / 16000;
553 freq_fraction = (t->radio_freq % 16000) * 100 / 16000;
Mauro Carvalho Chehab7e578192006-01-09 15:25:27 -0200554 } else {
Hans Verkuil27487d42006-01-15 15:04:52 -0200555 freq = t->tv_freq / 16;
556 freq_fraction = (t->tv_freq % 16) * 100 / 16;
Mauro Carvalho Chehab7e578192006-01-09 15:25:27 -0200557 }
558 tuner_info("Tuner mode: %s\n", p);
559 tuner_info("Frequency: %lu.%02lu MHz\n", freq, freq_fraction);
Mauro Carvalho Chehab4ae5c2e2006-03-25 15:53:38 -0300560 tuner_info("Standard: 0x%08lx\n", (unsigned long)t->std);
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200561 if (t->mode != V4L2_TUNER_RADIO)
562 return;
Michael Krufkye18f9442007-08-21 01:25:48 -0300563 if (fe_tuner_ops->get_status) {
564 u32 tuner_status;
565
566 fe_tuner_ops->get_status(&t->fe, &tuner_status);
567 if (tuner_status & TUNER_STATUS_LOCKED)
568 tuner_info("Tuner is locked.\n");
569 if (tuner_status & TUNER_STATUS_STEREO)
570 tuner_info("Stereo: yes\n");
571 }
Michael Krufky1dde7a42007-10-21 13:40:56 -0300572 if ((ops) && (ops->has_signal)) {
573 tuner_info("Signal strength: %d\n", ops->has_signal(t));
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200574 }
Michael Krufky1dde7a42007-10-21 13:40:56 -0300575 if ((ops) && (ops->is_stereo)) {
576 tuner_info("Stereo: %s\n", ops->is_stereo(t) ? "yes" : "no");
Mauro Carvalho Chehab7e578192006-01-09 15:25:27 -0200577 }
578}
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200579
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580/* ---------------------------------------------------------------------- */
581
Hans Verkuilba8fc392006-06-25 15:34:39 -0300582/* static vars: used only in tuner_attach and tuner_probe */
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700583static unsigned default_mode_mask;
584
585/* During client attach, set_type is called by adapter's attach_inform callback.
586 set_type must then be completed by tuner_attach.
587 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588static int tuner_attach(struct i2c_adapter *adap, int addr, int kind)
589{
590 struct tuner *t;
591
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700592 client_template.adapter = adap;
593 client_template.addr = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
Panagiotis Issaris74081872006-01-11 19:40:56 -0200595 t = kzalloc(sizeof(struct tuner), GFP_KERNEL);
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700596 if (NULL == t)
597 return -ENOMEM;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700598 memcpy(&t->i2c, &client_template, sizeof(struct i2c_client));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 i2c_set_clientdata(&t->i2c, t);
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700600 t->type = UNSET;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700601 t->audmode = V4L2_TUNER_MODE_STEREO;
602 t->mode_mask = T_UNINITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Mauro Carvalho Chehabfd3113e2005-07-31 22:34:43 -0700604 if (show_i2c) {
605 unsigned char buffer[16];
606 int i,rc;
607
608 memset(buffer, 0, sizeof(buffer));
609 rc = i2c_master_recv(&t->i2c, buffer, sizeof(buffer));
Mauro Carvalho Chehab67678362005-11-08 21:38:02 -0800610 tuner_info("I2C RECV = ");
Mauro Carvalho Chehabfd3113e2005-07-31 22:34:43 -0700611 for (i=0;i<rc;i++)
612 printk("%02x ",buffer[i]);
613 printk("\n");
614 }
Michael Hunoldc28089a2006-11-28 08:14:44 -0300615 /* HACK: This test were added to avoid tuner to probe tda9840 and tea6415c on the MXB card */
616 if (adap->id == I2C_HW_SAA7146 && addr < 0x4a)
617 return -ENODEV;
618
Markus Rechberger257c6452006-01-23 17:11:11 -0200619 /* autodetection code based on the i2c addr */
Mauro Carvalho Chehabc5287ba2005-07-15 03:56:28 -0700620 if (!no_autodetect) {
Mauro Carvalho Chehab13dd38d2005-11-08 21:37:57 -0800621 switch (addr) {
Mauro Carvalho Chehab8573a9e2007-04-08 01:09:11 -0300622 case 0x10:
Michael Krufky7ab10bf2007-08-27 21:23:40 -0300623 if (tea5761_autodetection(t->i2c.adapter, t->i2c.addr) != EINVAL) {
Mauro Carvalho Chehab8573a9e2007-04-08 01:09:11 -0300624 t->type = TUNER_TEA5761;
625 t->mode_mask = T_RADIO;
626 t->mode = T_STANDBY;
627 t->radio_freq = 87.5 * 16000; /* Sets freq to FM range */
628 default_mode_mask &= ~T_RADIO;
629
630 goto register_client;
631 }
632 break;
Hartmut Hackmann07345f52005-11-08 21:38:09 -0800633 case 0x42:
634 case 0x43:
635 case 0x4a:
636 case 0x4b:
637 /* If chip is not tda8290, don't register.
638 since it can be tda9887*/
Michael Krufky746d97322007-08-25 19:08:45 -0300639 if (tda8290_probe(t) == 0) {
Mauro Carvalho Chehab15396232006-06-23 16:13:56 -0300640 tuner_dbg("chip at addr %x is a tda8290\n", addr);
641 } else {
642 /* Default is being tda9887 */
643 t->type = TUNER_TDA9887;
644 t->mode_mask = T_RADIO | T_ANALOG_TV | T_DIGITAL_TV;
645 t->mode = T_STANDBY;
646 goto register_client;
Hartmut Hackmann07345f52005-11-08 21:38:09 -0800647 }
648 break;
Mauro Carvalho Chehab13dd38d2005-11-08 21:37:57 -0800649 case 0x60:
Michael Krufky8d0936e2007-08-27 21:24:27 -0300650 if (tea5767_autodetection(t->i2c.adapter, t->i2c.addr) != EINVAL) {
Mauro Carvalho Chehabc5287ba2005-07-15 03:56:28 -0700651 t->type = TUNER_TEA5767;
652 t->mode_mask = T_RADIO;
653 t->mode = T_STANDBY;
Hans Verkuil27487d42006-01-15 15:04:52 -0200654 t->radio_freq = 87.5 * 16000; /* Sets freq to FM range */
Mauro Carvalho Chehabc5287ba2005-07-15 03:56:28 -0700655 default_mode_mask &= ~T_RADIO;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700656
Mauro Carvalho Chehab67678362005-11-08 21:38:02 -0800657 goto register_client;
Mauro Carvalho Chehabc5287ba2005-07-15 03:56:28 -0700658 }
Hartmut Hackmann07345f52005-11-08 21:38:09 -0800659 break;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700660 }
661 }
662
663 /* Initializes only the first adapter found */
664 if (default_mode_mask != T_UNINITIALIZED) {
665 tuner_dbg ("Setting mode_mask to 0x%02x\n", default_mode_mask);
666 t->mode_mask = default_mode_mask;
Hans Verkuil27487d42006-01-15 15:04:52 -0200667 t->tv_freq = 400 * 16; /* Sets freq to VHF High */
668 t->radio_freq = 87.5 * 16000; /* Sets freq to FM range */
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700669 default_mode_mask = T_UNINITIALIZED;
670 }
671
672 /* Should be just before return */
Mauro Carvalho Chehab67678362005-11-08 21:38:02 -0800673register_client:
674 tuner_info("chip found @ 0x%x (%s)\n", addr << 1, adap->name);
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700675 i2c_attach_client (&t->i2c);
Hartmut Hackmanncfeb8832007-04-27 12:31:17 -0300676 set_type (&t->i2c,t->type, t->mode_mask, t->config, t->tuner_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 return 0;
678}
679
680static int tuner_probe(struct i2c_adapter *adap)
681{
682 if (0 != addr) {
Mauro Carvalho Chehabf5bec392005-06-23 22:05:13 -0700683 normal_i2c[0] = addr;
684 normal_i2c[1] = I2C_CLIENT_END;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Michael Krufkya1dec512007-08-24 01:13:07 -0300687 /* HACK: Ignore 0x6b and 0x6f on cx88 boards.
688 * FusionHDTV5 RT Gold has an ir receiver at 0x6b
689 * and an RTC at 0x6f which can get corrupted if probed.
690 */
Michael Krufky9bc37ca2007-09-08 15:17:13 -0300691 if ((adap->id == I2C_HW_B_CX2388x) ||
692 (adap->id == I2C_HW_B_CX23885)) {
Michael Krufkya1dec512007-08-24 01:13:07 -0300693 unsigned int i = 0;
694
695 while (i < I2C_CLIENT_MAX_OPTS && ignore[i] != I2C_CLIENT_END)
696 i += 2;
697 if (i + 4 < I2C_CLIENT_MAX_OPTS) {
698 ignore[i+0] = adap->nr;
699 ignore[i+1] = 0x6b;
700 ignore[i+2] = adap->nr;
701 ignore[i+3] = 0x6f;
702 ignore[i+4] = I2C_CLIENT_END;
703 } else
704 printk(KERN_WARNING "tuner: "
705 "too many options specified "
706 "in i2c probe ignore list!\n");
707 }
708
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700709 default_mode_mask = T_RADIO | T_ANALOG_TV | T_DIGITAL_TV;
Mauro Carvalho Chehab391cd722005-06-23 22:02:43 -0700710
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 if (adap->class & I2C_CLASS_TV_ANALOG)
712 return i2c_probe(adap, &addr_data, tuner_attach);
713 return 0;
714}
715
716static int tuner_detach(struct i2c_client *client)
717{
718 struct tuner *t = i2c_get_clientdata(client);
Michael Krufky1dde7a42007-10-21 13:40:56 -0300719 struct analog_tuner_ops *ops = t->fe.ops.analog_demod_ops;
Mauro Carvalho Chehab391cd722005-06-23 22:02:43 -0700720 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700722 err = i2c_detach_client(&t->i2c);
Mauro Carvalho Chehab391cd722005-06-23 22:02:43 -0700723 if (err) {
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700724 tuner_warn
725 ("Client deregistration failed, client not detached.\n");
Mauro Carvalho Chehab391cd722005-06-23 22:02:43 -0700726 return err;
727 }
728
Michael Krufky1dde7a42007-10-21 13:40:56 -0300729 if ((ops) && (ops->release))
730 ops->release(t);
Michael Krufky052c50d2007-06-04 16:00:45 -0300731 else {
732 kfree(t->priv);
733 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 kfree(t);
735 return 0;
736}
737
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700738/*
739 * Switch tuner to other mode. If tuner support both tv and radio,
740 * set another frequency to some value (This is needed for some pal
741 * tuners to avoid locking). Otherwise, just put second tuner in
742 * standby mode.
743 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700745static inline int set_mode(struct i2c_client *client, struct tuner *t, int mode, char *cmd)
746{
Michael Krufky1dde7a42007-10-21 13:40:56 -0300747 struct analog_tuner_ops *ops = t->fe.ops.analog_demod_ops;
748
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800749 if (mode == t->mode)
750 return 0;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700751
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800752 t->mode = mode;
Mauro Carvalho Chehab793cf9e2005-09-09 13:03:37 -0700753
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800754 if (check_mode(t, cmd) == EINVAL) {
755 t->mode = T_STANDBY;
Michael Krufky1dde7a42007-10-21 13:40:56 -0300756 if ((ops) && (ops->standby))
757 ops->standby(t);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800758 return EINVAL;
759 }
760 return 0;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700761}
762
763#define switch_v4l2() if (!t->using_v4l2) \
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800764 tuner_dbg("switching to v4l2\n"); \
765 t->using_v4l2 = 1;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700766
767static inline int check_v4l2(struct tuner *t)
768{
Hans Verkuil3bbe5a82006-04-01 15:27:52 -0300769 /* bttv still uses both v4l1 and v4l2 calls to the tuner (v4l2 for
770 TV, v4l1 for radio), until that is fixed this code is disabled.
771 Otherwise the radio (v4l1) wouldn't tune after using the TV (v4l2)
772 first. */
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700773 return 0;
774}
775
776static int tuner_command(struct i2c_client *client, unsigned int cmd, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
778 struct tuner *t = i2c_get_clientdata(client);
Michael Krufkye18f9442007-08-21 01:25:48 -0300779 struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops;
Michael Krufky1dde7a42007-10-21 13:40:56 -0300780 struct analog_tuner_ops *ops = t->fe.ops.analog_demod_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
Hans Verkuilf9195de2006-01-11 19:01:01 -0200782 if (tuner_debug>1)
Michael Krufky5e453dc2006-01-09 15:32:31 -0200783 v4l_i2c_print_ioctl(&(t->i2c),cmd);
784
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700785 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 /* --- configuration --- */
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700787 case TUNER_SET_TYPE_ADDR:
Hartmut Hackmannde956c12007-04-27 12:31:12 -0300788 tuner_dbg ("Calling set_type_addr for type=%d, addr=0x%02x, mode=0x%02x, config=0x%02x\n",
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700789 ((struct tuner_setup *)arg)->type,
790 ((struct tuner_setup *)arg)->addr,
Hartmut Hackmannde956c12007-04-27 12:31:12 -0300791 ((struct tuner_setup *)arg)->mode_mask,
792 ((struct tuner_setup *)arg)->config);
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700793
794 set_addr(client, (struct tuner_setup *)arg);
Mauro Carvalho Chehab391cd722005-06-23 22:02:43 -0700795 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 case AUDC_SET_RADIO:
Hans Verkuil27487d42006-01-15 15:04:52 -0200797 if (set_mode(client, t, V4L2_TUNER_RADIO, "AUDC_SET_RADIO")
798 == EINVAL)
799 return 0;
800 if (t->radio_freq)
801 set_freq(client, t->radio_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 break;
Mauro Carvalho Chehab793cf9e2005-09-09 13:03:37 -0700803 case TUNER_SET_STANDBY:
Hans Verkuil27487d42006-01-15 15:04:52 -0200804 if (check_mode(t, "TUNER_SET_STANDBY") == EINVAL)
805 return 0;
Mauro Carvalho Chehab15396232006-06-23 16:13:56 -0300806 t->mode = T_STANDBY;
Michael Krufky1dde7a42007-10-21 13:40:56 -0300807 if ((ops) && (ops->standby))
808 ops->standby(t);
Hans Verkuil27487d42006-01-15 15:04:52 -0200809 break;
Mauro Carvalho Chehab985bc962006-07-23 06:31:19 -0300810#ifdef CONFIG_VIDEO_V4L1
Mauro Carvalho Chehabfd3113e2005-07-31 22:34:43 -0700811 case VIDIOCSAUDIO:
812 if (check_mode(t, "VIDIOCSAUDIO") == EINVAL)
813 return 0;
814 if (check_v4l2(t) == EINVAL)
815 return 0;
816
817 /* Should be implemented, since bttv calls it */
818 tuner_dbg("VIDIOCSAUDIO not implemented.\n");
Mauro Carvalho Chehabfd3113e2005-07-31 22:34:43 -0700819 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 case VIDIOCSCHAN:
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700821 {
822 static const v4l2_std_id map[] = {
823 [VIDEO_MODE_PAL] = V4L2_STD_PAL,
824 [VIDEO_MODE_NTSC] = V4L2_STD_NTSC_M,
825 [VIDEO_MODE_SECAM] = V4L2_STD_SECAM,
826 [4 /* bttv */ ] = V4L2_STD_PAL_M,
827 [5 /* bttv */ ] = V4L2_STD_PAL_N,
828 [6 /* bttv */ ] = V4L2_STD_NTSC_M_JP,
829 };
830 struct video_channel *vc = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700832 if (check_v4l2(t) == EINVAL)
833 return 0;
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700834
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700835 if (set_mode(client,t,V4L2_TUNER_ANALOG_TV, "VIDIOCSCHAN")==EINVAL)
836 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700838 if (vc->norm < ARRAY_SIZE(map))
839 t->std = map[vc->norm];
840 tuner_fixup_std(t);
Hans Verkuil27487d42006-01-15 15:04:52 -0200841 if (t->tv_freq)
842 set_tv_freq(client, t->tv_freq);
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700843 return 0;
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700844 }
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700845 case VIDIOCSFREQ:
846 {
847 unsigned long *v = arg;
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700848
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700849 if (check_mode(t, "VIDIOCSFREQ") == EINVAL)
850 return 0;
851 if (check_v4l2(t) == EINVAL)
852 return 0;
853
854 set_freq(client, *v);
855 return 0;
856 }
857 case VIDIOCGTUNER:
858 {
859 struct video_tuner *vt = arg;
860
861 if (check_mode(t, "VIDIOCGTUNER") == EINVAL)
862 return 0;
863 if (check_v4l2(t) == EINVAL)
864 return 0;
865
866 if (V4L2_TUNER_RADIO == t->mode) {
Michael Krufkye18f9442007-08-21 01:25:48 -0300867 if (fe_tuner_ops->get_status) {
868 u32 tuner_status;
869
870 fe_tuner_ops->get_status(&t->fe, &tuner_status);
Michael Krufky1f5ef192007-08-31 17:38:02 -0300871 if (tuner_status & TUNER_STATUS_STEREO)
872 vt->flags |= VIDEO_TUNER_STEREO_ON;
873 else
874 vt->flags &= ~VIDEO_TUNER_STEREO_ON;
Michael Krufkye18f9442007-08-21 01:25:48 -0300875 } else {
Michael Krufky1dde7a42007-10-21 13:40:56 -0300876 if ((ops) && (ops->is_stereo)) {
877 if (ops->is_stereo(t))
Michael Krufkye18f9442007-08-21 01:25:48 -0300878 vt->flags |=
879 VIDEO_TUNER_STEREO_ON;
880 else
881 vt->flags &=
882 ~VIDEO_TUNER_STEREO_ON;
883 }
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700884 }
Michael Krufky1dde7a42007-10-21 13:40:56 -0300885 if ((ops) && (ops->has_signal))
886 vt->signal = ops->has_signal(t);
Michael Krufky1f5ef192007-08-31 17:38:02 -0300887
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700888 vt->flags |= VIDEO_TUNER_LOW; /* Allow freqs at 62.5 Hz */
889
890 vt->rangelow = radio_range[0] * 16000;
891 vt->rangehigh = radio_range[1] * 16000;
892
893 } else {
894 vt->rangelow = tv_range[0] * 16;
895 vt->rangehigh = tv_range[1] * 16;
896 }
897
898 return 0;
899 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 case VIDIOCGAUDIO:
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700901 {
902 struct video_audio *va = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700904 if (check_mode(t, "VIDIOCGAUDIO") == EINVAL)
905 return 0;
906 if (check_v4l2(t) == EINVAL)
907 return 0;
908
Michael Krufkye18f9442007-08-21 01:25:48 -0300909 if (V4L2_TUNER_RADIO == t->mode) {
910 if (fe_tuner_ops->get_status) {
911 u32 tuner_status;
912
913 fe_tuner_ops->get_status(&t->fe, &tuner_status);
914 va->mode = (tuner_status & TUNER_STATUS_STEREO)
915 ? VIDEO_SOUND_STEREO : VIDEO_SOUND_MONO;
Michael Krufky1dde7a42007-10-21 13:40:56 -0300916 } else if ((ops) && (ops->is_stereo))
917 va->mode = ops->is_stereo(t)
Michael Krufkye18f9442007-08-21 01:25:48 -0300918 ? VIDEO_SOUND_STEREO : VIDEO_SOUND_MONO;
919 }
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700920 return 0;
921 }
Mauro Carvalho Chehab985bc962006-07-23 06:31:19 -0300922#endif
Mauro Carvalho Chehab7f171122007-10-18 19:56:47 -0300923 case TUNER_SET_CONFIG:
924 {
925 struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops;
926 struct v4l2_priv_tun_config *cfg = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Mauro Carvalho Chehab7f171122007-10-18 19:56:47 -0300928 if (t->type != cfg->tuner)
929 break;
930
931 if (t->type == TUNER_TDA9887) {
932 t->tda9887_config = *(unsigned int *)cfg->priv;
Mauro Carvalho Chehab985bc962006-07-23 06:31:19 -0300933 set_freq(client, t->tv_freq);
Mauro Carvalho Chehab7f171122007-10-18 19:56:47 -0300934 break;
Mauro Carvalho Chehab985bc962006-07-23 06:31:19 -0300935 }
Mauro Carvalho Chehab7f171122007-10-18 19:56:47 -0300936
937 if (NULL == fe_tuner_ops->set_config) {
938 tuner_warn("Tuner frontend module has no way to "
939 "set config\n");
940 break;
941 }
942 fe_tuner_ops->set_config(&t->fe, cfg->priv);
943
Mauro Carvalho Chehab985bc962006-07-23 06:31:19 -0300944 break;
Mauro Carvalho Chehab7f171122007-10-18 19:56:47 -0300945 }
Mauro Carvalho Chehab985bc962006-07-23 06:31:19 -0300946 /* --- v4l ioctls --- */
947 /* take care: bttv does userspace copying, we'll get a
948 kernel pointer here... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 case VIDIOC_S_STD:
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700950 {
951 v4l2_std_id *id = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700953 if (set_mode (client, t, V4L2_TUNER_ANALOG_TV, "VIDIOC_S_STD")
954 == EINVAL)
955 return 0;
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700956
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700957 switch_v4l2();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700959 t->std = *id;
960 tuner_fixup_std(t);
Hans Verkuil27487d42006-01-15 15:04:52 -0200961 if (t->tv_freq)
962 set_freq(client, t->tv_freq);
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700963 break;
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700964 }
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700965 case VIDIOC_S_FREQUENCY:
966 {
967 struct v4l2_frequency *f = arg;
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700968
Hans Verkuil4f725cb2006-06-24 09:47:56 -0300969 if (set_mode (client, t, f->type, "VIDIOC_S_FREQUENCY")
970 == EINVAL)
971 return 0;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700972 switch_v4l2();
Hans Verkuil27487d42006-01-15 15:04:52 -0200973 set_freq(client,f->frequency);
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700974
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700975 break;
976 }
977 case VIDIOC_G_FREQUENCY:
978 {
979 struct v4l2_frequency *f = arg;
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700980
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700981 if (check_mode(t, "VIDIOC_G_FREQUENCY") == EINVAL)
982 return 0;
983 switch_v4l2();
984 f->type = t->mode;
Michael Krufkye18f9442007-08-21 01:25:48 -0300985 if (fe_tuner_ops->get_frequency) {
986 u32 abs_freq;
987
988 fe_tuner_ops->get_frequency(&t->fe, &abs_freq);
989 f->frequency = (V4L2_TUNER_RADIO == t->mode) ?
990 (abs_freq * 2 + 125/2) / 125 :
991 (abs_freq + 62500/2) / 62500;
992 break;
993 }
Hans Verkuil27487d42006-01-15 15:04:52 -0200994 f->frequency = (V4L2_TUNER_RADIO == t->mode) ?
995 t->radio_freq : t->tv_freq;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700996 break;
997 }
998 case VIDIOC_G_TUNER:
999 {
1000 struct v4l2_tuner *tuner = arg;
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -07001001
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -07001002 if (check_mode(t, "VIDIOC_G_TUNER") == EINVAL)
1003 return 0;
1004 switch_v4l2();
1005
Hans Verkuil8a4b2752006-01-23 17:11:09 -02001006 tuner->type = t->mode;
Michael Krufky1dde7a42007-10-21 13:40:56 -03001007 if ((ops) && (ops->get_afc))
1008 tuner->afc = ops->get_afc(t);
Hans Verkuilab4cecf2006-04-01 16:40:21 -03001009 if (t->mode == V4L2_TUNER_ANALOG_TV)
1010 tuner->capability |= V4L2_TUNER_CAP_NORM;
Hans Verkuil8a4b2752006-01-23 17:11:09 -02001011 if (t->mode != V4L2_TUNER_RADIO) {
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -07001012 tuner->rangelow = tv_range[0] * 16;
1013 tuner->rangehigh = tv_range[1] * 16;
Hans Verkuil8a4b2752006-01-23 17:11:09 -02001014 break;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -07001015 }
Hans Verkuil8a4b2752006-01-23 17:11:09 -02001016
1017 /* radio mode */
Hans Verkuil8a4b2752006-01-23 17:11:09 -02001018 tuner->rxsubchans =
1019 V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
Michael Krufkye18f9442007-08-21 01:25:48 -03001020 if (fe_tuner_ops->get_status) {
1021 u32 tuner_status;
Hans Verkuil8a4b2752006-01-23 17:11:09 -02001022
Michael Krufkye18f9442007-08-21 01:25:48 -03001023 fe_tuner_ops->get_status(&t->fe, &tuner_status);
1024 tuner->rxsubchans = (tuner_status & TUNER_STATUS_STEREO) ?
1025 V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO;
Michael Krufkye18f9442007-08-21 01:25:48 -03001026 } else {
Michael Krufky1dde7a42007-10-21 13:40:56 -03001027 if ((ops) && (ops->is_stereo)) {
1028 tuner->rxsubchans = ops->is_stereo(t) ?
Michael Krufkye18f9442007-08-21 01:25:48 -03001029 V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO;
1030 }
Michael Krufkye18f9442007-08-21 01:25:48 -03001031 }
Michael Krufky1dde7a42007-10-21 13:40:56 -03001032 if ((ops) && (ops->has_signal))
1033 tuner->signal = ops->has_signal(t);
Hans Verkuil8a4b2752006-01-23 17:11:09 -02001034 tuner->capability |=
1035 V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
1036 tuner->audmode = t->audmode;
1037 tuner->rangelow = radio_range[0] * 16000;
1038 tuner->rangehigh = radio_range[1] * 16000;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -07001039 break;
1040 }
1041 case VIDIOC_S_TUNER:
1042 {
1043 struct v4l2_tuner *tuner = arg;
1044
1045 if (check_mode(t, "VIDIOC_S_TUNER") == EINVAL)
1046 return 0;
1047
1048 switch_v4l2();
1049
Hans Verkuil8a4b2752006-01-23 17:11:09 -02001050 /* do nothing unless we're a radio tuner */
1051 if (t->mode != V4L2_TUNER_RADIO)
1052 break;
1053 t->audmode = tuner->audmode;
1054 set_radio_freq(client, t->radio_freq);
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -07001055 break;
1056 }
Hans Verkuilcd43c3f2006-01-09 15:25:15 -02001057 case VIDIOC_LOG_STATUS:
Michael Krufky1dde7a42007-10-21 13:40:56 -03001058 if ((ops) && (ops->tuner_status))
1059 ops->tuner_status(t);
Hans Verkuilcd43c3f2006-01-09 15:25:15 -02001060 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 }
1062
1063 return 0;
1064}
1065
Jean Delvare21b48a72007-03-12 19:20:15 -03001066static int tuner_suspend(struct i2c_client *c, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067{
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -07001068 struct tuner *t = i2c_get_clientdata (c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -07001070 tuner_dbg ("suspend\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 /* FIXME: power down ??? */
1072 return 0;
1073}
1074
Jean Delvare21b48a72007-03-12 19:20:15 -03001075static int tuner_resume(struct i2c_client *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076{
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -07001077 struct tuner *t = i2c_get_clientdata (c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -07001079 tuner_dbg ("resume\n");
Hans Verkuil27487d42006-01-15 15:04:52 -02001080 if (V4L2_TUNER_RADIO == t->mode) {
1081 if (t->radio_freq)
1082 set_freq(c, t->radio_freq);
1083 } else {
1084 if (t->tv_freq)
1085 set_freq(c, t->tv_freq);
1086 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 return 0;
1088}
1089
1090/* ----------------------------------------------------------------------- */
1091
1092static struct i2c_driver driver = {
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -07001093 .id = I2C_DRIVERID_TUNER,
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -07001094 .attach_adapter = tuner_probe,
1095 .detach_client = tuner_detach,
1096 .command = tuner_command,
Jean Delvare21b48a72007-03-12 19:20:15 -03001097 .suspend = tuner_suspend,
1098 .resume = tuner_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 .driver = {
Mauro Carvalho Chehabcab462f2006-01-09 15:53:26 -02001100 .name = "tuner",
Mauro Carvalho Chehabcab462f2006-01-09 15:53:26 -02001101 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102};
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -07001103static struct i2c_client client_template = {
Jean Delvarefae91e72005-08-15 19:57:04 +02001104 .name = "(tuner unset)",
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -07001105 .driver = &driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106};
1107
1108static int __init tuner_init_module(void)
1109{
1110 return i2c_add_driver(&driver);
1111}
1112
1113static void __exit tuner_cleanup_module(void)
1114{
1115 i2c_del_driver(&driver);
1116}
1117
1118module_init(tuner_init_module);
1119module_exit(tuner_cleanup_module);
1120
1121/*
1122 * Overrides for Emacs so that we follow Linus's tabbing style.
1123 * ---------------------------------------------------------------------------
1124 * Local variables:
1125 * c-basic-offset: 8
1126 * End:
1127 */