Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
1da177e4 LT |
2 | * |
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> | |
1da177e4 | 8 | #include <linux/kernel.h> |
1da177e4 LT |
9 | #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> | |
1da177e4 | 17 | #include <linux/init.h> |
75b4c260 | 18 | #include <linux/videodev2.h> |
1da177e4 | 19 | #include <media/tuner.h> |
4adad287 | 20 | #include <media/tuner-types.h> |
e8a4a9e7 | 21 | #include <media/v4l2-device.h> |
35ea11ff | 22 | #include <media/v4l2-ioctl.h> |
75b4c260 | 23 | #include <media/v4l2-i2c-drv.h> |
96c0b7cf | 24 | #include "mt20xx.h" |
910bb3e3 | 25 | #include "tda8290.h" |
7ab10bf7 | 26 | #include "tea5761.h" |
8d0936ed | 27 | #include "tea5767.h" |
215b95ba | 28 | #include "tuner-xc2028.h" |
4adad287 | 29 | #include "tuner-simple.h" |
31c9584c | 30 | #include "tda9887.h" |
27c685a4 | 31 | #include "xc5000.h" |
93463895 | 32 | #include "tda18271.h" |
1da177e4 LT |
33 | |
34 | #define UNSET (-1U) | |
35 | ||
9dd659de | 36 | #define PREFIX t->i2c->driver->driver.name |
241020d1 | 37 | |
a07c8779 | 38 | /** This macro allows us to probe dynamically, avoiding static links */ |
ff138171 | 39 | #ifdef CONFIG_MEDIA_ATTACH |
a07c8779 MK |
40 | #define tuner_symbol_probe(FUNCTION, ARGS...) ({ \ |
41 | int __r = -EINVAL; \ | |
42 | typeof(&FUNCTION) __a = symbol_request(FUNCTION); \ | |
43 | if (__a) { \ | |
44 | __r = (int) __a(ARGS); \ | |
a1355e53 | 45 | symbol_put(FUNCTION); \ |
a07c8779 MK |
46 | } else { \ |
47 | printk(KERN_ERR "TUNER: Unable to find " \ | |
48 | "symbol "#FUNCTION"()\n"); \ | |
49 | } \ | |
a07c8779 MK |
50 | __r; \ |
51 | }) | |
52 | ||
53 | static void tuner_detach(struct dvb_frontend *fe) | |
54 | { | |
55 | if (fe->ops.tuner_ops.release) { | |
56 | fe->ops.tuner_ops.release(fe); | |
57 | symbol_put_addr(fe->ops.tuner_ops.release); | |
58 | } | |
59 | if (fe->ops.analog_ops.release) { | |
60 | fe->ops.analog_ops.release(fe); | |
61 | symbol_put_addr(fe->ops.analog_ops.release); | |
62 | } | |
63 | } | |
64 | #else | |
65 | #define tuner_symbol_probe(FUNCTION, ARGS...) ({ \ | |
66 | FUNCTION(ARGS); \ | |
67 | }) | |
68 | ||
69 | static void tuner_detach(struct dvb_frontend *fe) | |
70 | { | |
71 | if (fe->ops.tuner_ops.release) | |
72 | fe->ops.tuner_ops.release(fe); | |
73 | if (fe->ops.analog_ops.release) | |
74 | fe->ops.analog_ops.release(fe); | |
75 | } | |
76 | #endif | |
77 | ||
f7f427e4 MK |
78 | struct tuner { |
79 | /* device */ | |
80 | struct dvb_frontend fe; | |
81 | struct i2c_client *i2c; | |
e8a4a9e7 | 82 | struct v4l2_subdev sd; |
f7f427e4 MK |
83 | struct list_head list; |
84 | unsigned int using_v4l2:1; | |
85 | ||
86 | /* keep track of the current settings */ | |
87 | v4l2_std_id std; | |
88 | unsigned int tv_freq; | |
89 | unsigned int radio_freq; | |
90 | unsigned int audmode; | |
91 | ||
92 | unsigned int mode; | |
93 | unsigned int mode_mask; /* Combination of allowable modes */ | |
94 | ||
95 | unsigned int type; /* chip type id */ | |
96 | unsigned int config; | |
7271e60a | 97 | const char *name; |
f7f427e4 MK |
98 | }; |
99 | ||
e8a4a9e7 HV |
100 | static inline struct tuner *to_tuner(struct v4l2_subdev *sd) |
101 | { | |
102 | return container_of(sd, struct tuner, sd); | |
103 | } | |
104 | ||
1da177e4 LT |
105 | |
106 | /* insmod options used at init time => read/only */ | |
ff699e6b DSL |
107 | static unsigned int addr; |
108 | static unsigned int no_autodetect; | |
109 | static unsigned int show_i2c; | |
fd3113e8 | 110 | |
1da177e4 | 111 | /* insmod options used at runtime => read/write */ |
ab166050 MK |
112 | static int tuner_debug; |
113 | ||
114 | #define tuner_warn(fmt, arg...) do { \ | |
115 | printk(KERN_WARNING "%s %d-%04x: " fmt, PREFIX, \ | |
116 | i2c_adapter_id(t->i2c->adapter), \ | |
117 | t->i2c->addr, ##arg); \ | |
118 | } while (0) | |
119 | ||
120 | #define tuner_info(fmt, arg...) do { \ | |
121 | printk(KERN_INFO "%s %d-%04x: " fmt, PREFIX, \ | |
122 | i2c_adapter_id(t->i2c->adapter), \ | |
123 | t->i2c->addr, ##arg); \ | |
124 | } while (0) | |
125 | ||
126 | #define tuner_err(fmt, arg...) do { \ | |
127 | printk(KERN_ERR "%s %d-%04x: " fmt, PREFIX, \ | |
128 | i2c_adapter_id(t->i2c->adapter), \ | |
129 | t->i2c->addr, ##arg); \ | |
130 | } while (0) | |
131 | ||
132 | #define tuner_dbg(fmt, arg...) do { \ | |
133 | if (tuner_debug) \ | |
134 | printk(KERN_DEBUG "%s %d-%04x: " fmt, PREFIX, \ | |
135 | i2c_adapter_id(t->i2c->adapter), \ | |
136 | t->i2c->addr, ##arg); \ | |
137 | } while (0) | |
138 | ||
139 | /* ------------------------------------------------------------------------ */ | |
1da177e4 | 140 | |
f7ce3cc6 | 141 | static unsigned int tv_range[2] = { 44, 958 }; |
1da177e4 LT |
142 | static unsigned int radio_range[2] = { 65, 108 }; |
143 | ||
7e578191 MCC |
144 | static char pal[] = "--"; |
145 | static char secam[] = "--"; | |
146 | static char ntsc[] = "-"; | |
147 | ||
f9195ded | 148 | |
7e578191 MCC |
149 | module_param(addr, int, 0444); |
150 | module_param(no_autodetect, int, 0444); | |
151 | module_param(show_i2c, int, 0444); | |
f9195ded | 152 | module_param_named(debug,tuner_debug, int, 0644); |
7e578191 MCC |
153 | module_param_string(pal, pal, sizeof(pal), 0644); |
154 | module_param_string(secam, secam, sizeof(secam), 0644); | |
155 | module_param_string(ntsc, ntsc, sizeof(ntsc), 0644); | |
f7ce3cc6 | 156 | module_param_array(tv_range, int, NULL, 0644); |
1da177e4 LT |
157 | module_param_array(radio_range, int, NULL, 0644); |
158 | ||
159 | MODULE_DESCRIPTION("device driver for various TV and TV+FM radio tuners"); | |
160 | MODULE_AUTHOR("Ralph Metzler, Gerd Knorr, Gunther Mayer"); | |
161 | MODULE_LICENSE("GPL"); | |
162 | ||
1da177e4 LT |
163 | /* ---------------------------------------------------------------------- */ |
164 | ||
c7919d52 MK |
165 | static void fe_set_params(struct dvb_frontend *fe, |
166 | struct analog_parameters *params) | |
e18f9444 | 167 | { |
4e9154b8 MK |
168 | struct dvb_tuner_ops *fe_tuner_ops = &fe->ops.tuner_ops; |
169 | struct tuner *t = fe->analog_demod_priv; | |
e18f9444 | 170 | |
e18f9444 MK |
171 | if (NULL == fe_tuner_ops->set_analog_params) { |
172 | tuner_warn("Tuner frontend module has no way to set freq\n"); | |
173 | return; | |
174 | } | |
c7919d52 | 175 | fe_tuner_ops->set_analog_params(fe, params); |
e18f9444 MK |
176 | } |
177 | ||
4e9154b8 | 178 | static void fe_standby(struct dvb_frontend *fe) |
e18f9444 | 179 | { |
4e9154b8 | 180 | struct dvb_tuner_ops *fe_tuner_ops = &fe->ops.tuner_ops; |
e18f9444 MK |
181 | |
182 | if (fe_tuner_ops->sleep) | |
4e9154b8 | 183 | fe_tuner_ops->sleep(fe); |
e18f9444 MK |
184 | } |
185 | ||
4e9154b8 | 186 | static int fe_has_signal(struct dvb_frontend *fe) |
1f5ef197 | 187 | { |
1419683d | 188 | u16 strength = 0; |
1f5ef197 | 189 | |
4e9154b8 MK |
190 | if (fe->ops.tuner_ops.get_rf_strength) |
191 | fe->ops.tuner_ops.get_rf_strength(fe, &strength); | |
1f5ef197 MK |
192 | |
193 | return strength; | |
194 | } | |
195 | ||
f1c9a281 MK |
196 | static int fe_set_config(struct dvb_frontend *fe, void *priv_cfg) |
197 | { | |
198 | struct dvb_tuner_ops *fe_tuner_ops = &fe->ops.tuner_ops; | |
199 | struct tuner *t = fe->analog_demod_priv; | |
200 | ||
201 | if (fe_tuner_ops->set_config) | |
202 | return fe_tuner_ops->set_config(fe, priv_cfg); | |
203 | ||
204 | tuner_warn("Tuner frontend module has no way to set config\n"); | |
205 | ||
206 | return 0; | |
207 | } | |
208 | ||
4e9154b8 | 209 | static void tuner_status(struct dvb_frontend *fe); |
1dde7a4f | 210 | |
e8a4a9e7 | 211 | static struct analog_demod_ops tuner_analog_ops = { |
c7919d52 | 212 | .set_params = fe_set_params, |
1dde7a4f | 213 | .standby = fe_standby, |
1dde7a4f | 214 | .has_signal = fe_has_signal, |
f1c9a281 | 215 | .set_config = fe_set_config, |
1dde7a4f MK |
216 | .tuner_status = tuner_status |
217 | }; | |
218 | ||
56fc08ca | 219 | /* Set tuner frequency, freq in Units of 62.5kHz = 1/16MHz */ |
1da177e4 LT |
220 | static void set_tv_freq(struct i2c_client *c, unsigned int freq) |
221 | { | |
e8a4a9e7 | 222 | struct tuner *t = to_tuner(i2c_get_clientdata(c)); |
bc3e5c7f | 223 | struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops; |
1da177e4 | 224 | |
c7919d52 MK |
225 | struct analog_parameters params = { |
226 | .mode = t->mode, | |
227 | .audmode = t->audmode, | |
228 | .std = t->std | |
229 | }; | |
230 | ||
1da177e4 | 231 | if (t->type == UNSET) { |
f7ce3cc6 | 232 | tuner_warn ("tuner type not set\n"); |
1da177e4 LT |
233 | return; |
234 | } | |
bc3e5c7f | 235 | if (NULL == analog_ops->set_params) { |
f7ce3cc6 | 236 | tuner_warn ("Tuner has no way to set tv freq\n"); |
1da177e4 LT |
237 | return; |
238 | } | |
f7ce3cc6 MCC |
239 | if (freq < tv_range[0] * 16 || freq > tv_range[1] * 16) { |
240 | tuner_dbg ("TV freq (%d.%02d) out of range (%d-%d)\n", | |
241 | freq / 16, freq % 16 * 100 / 16, tv_range[0], | |
242 | tv_range[1]); | |
27487d44 HV |
243 | /* V4L2 spec: if the freq is not possible then the closest |
244 | possible value should be selected */ | |
245 | if (freq < tv_range[0] * 16) | |
246 | freq = tv_range[0] * 16; | |
247 | else | |
248 | freq = tv_range[1] * 16; | |
1da177e4 | 249 | } |
c7919d52 MK |
250 | params.frequency = freq; |
251 | ||
bc3e5c7f | 252 | analog_ops->set_params(&t->fe, ¶ms); |
1da177e4 LT |
253 | } |
254 | ||
255 | static void set_radio_freq(struct i2c_client *c, unsigned int freq) | |
256 | { | |
e8a4a9e7 | 257 | struct tuner *t = to_tuner(i2c_get_clientdata(c)); |
bc3e5c7f | 258 | struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops; |
1da177e4 | 259 | |
c7919d52 MK |
260 | struct analog_parameters params = { |
261 | .mode = t->mode, | |
262 | .audmode = t->audmode, | |
263 | .std = t->std | |
264 | }; | |
265 | ||
1da177e4 | 266 | if (t->type == UNSET) { |
f7ce3cc6 | 267 | tuner_warn ("tuner type not set\n"); |
1da177e4 LT |
268 | return; |
269 | } | |
e545d6e2 | 270 | if (NULL == analog_ops->set_params) { |
f7ce3cc6 | 271 | tuner_warn ("tuner has no way to set radio frequency\n"); |
1da177e4 LT |
272 | return; |
273 | } | |
27487d44 | 274 | if (freq < radio_range[0] * 16000 || freq > radio_range[1] * 16000) { |
f7ce3cc6 MCC |
275 | tuner_dbg ("radio freq (%d.%02d) out of range (%d-%d)\n", |
276 | freq / 16000, freq % 16000 * 100 / 16000, | |
277 | radio_range[0], radio_range[1]); | |
27487d44 HV |
278 | /* V4L2 spec: if the freq is not possible then the closest |
279 | possible value should be selected */ | |
280 | if (freq < radio_range[0] * 16000) | |
281 | freq = radio_range[0] * 16000; | |
282 | else | |
283 | freq = radio_range[1] * 16000; | |
1da177e4 | 284 | } |
c7919d52 | 285 | params.frequency = freq; |
586b0cab | 286 | |
bc3e5c7f | 287 | analog_ops->set_params(&t->fe, ¶ms); |
1da177e4 LT |
288 | } |
289 | ||
290 | static void set_freq(struct i2c_client *c, unsigned long freq) | |
291 | { | |
e8a4a9e7 | 292 | struct tuner *t = to_tuner(i2c_get_clientdata(c)); |
1da177e4 LT |
293 | |
294 | switch (t->mode) { | |
295 | case V4L2_TUNER_RADIO: | |
296 | tuner_dbg("radio freq set to %lu.%02lu\n", | |
f7ce3cc6 MCC |
297 | freq / 16000, freq % 16000 * 100 / 16000); |
298 | set_radio_freq(c, freq); | |
27487d44 | 299 | t->radio_freq = freq; |
1da177e4 LT |
300 | break; |
301 | case V4L2_TUNER_ANALOG_TV: | |
302 | case V4L2_TUNER_DIGITAL_TV: | |
303 | tuner_dbg("tv freq set to %lu.%02lu\n", | |
f7ce3cc6 | 304 | freq / 16, freq % 16 * 100 / 16); |
1da177e4 | 305 | set_tv_freq(c, freq); |
27487d44 | 306 | t->tv_freq = freq; |
1da177e4 | 307 | break; |
6cb45879 MCC |
308 | default: |
309 | tuner_dbg("freq set: unknown mode: 0x%04x!\n",t->mode); | |
1da177e4 | 310 | } |
1da177e4 LT |
311 | } |
312 | ||
27c685a4 ST |
313 | static struct xc5000_config xc5000_cfg; |
314 | ||
f7ce3cc6 | 315 | static void set_type(struct i2c_client *c, unsigned int type, |
de956c1e | 316 | unsigned int new_mode_mask, unsigned int new_config, |
d7cba043 | 317 | int (*tuner_callback) (void *dev, int component, int cmd, int arg)) |
1da177e4 | 318 | { |
e8a4a9e7 | 319 | struct tuner *t = to_tuner(i2c_get_clientdata(c)); |
e18f9444 | 320 | struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops; |
bc3e5c7f | 321 | struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops; |
586b0cab | 322 | unsigned char buffer[4]; |
1da177e4 | 323 | |
f7ce3cc6 MCC |
324 | if (type == UNSET || type == TUNER_ABSENT) { |
325 | tuner_dbg ("tuner 0x%02x: Tuner type absent\n",c->addr); | |
1da177e4 | 326 | return; |
f7ce3cc6 MCC |
327 | } |
328 | ||
80f90fba | 329 | t->type = type; |
e7ddcd98 | 330 | /* prevent invalid config values */ |
f14a2972 | 331 | t->config = new_config < 256 ? new_config : 0; |
80f90fba HH |
332 | if (tuner_callback != NULL) { |
333 | tuner_dbg("defining GPIO callback\n"); | |
d7cba043 | 334 | t->fe.callback = tuner_callback; |
80f90fba HH |
335 | } |
336 | ||
48aa336a | 337 | if (t->mode == T_UNINITIALIZED) { |
f7ce3cc6 MCC |
338 | tuner_dbg ("tuner 0x%02x: called during i2c_client register by adapter's attach_inform\n", c->addr); |
339 | ||
1da177e4 LT |
340 | return; |
341 | } | |
56fc08ca | 342 | |
b2083199 | 343 | /* discard private data, in case set_type() was previously called */ |
a07c8779 MK |
344 | tuner_detach(&t->fe); |
345 | t->fe.analog_demod_priv = NULL; | |
be2b85a1 | 346 | |
1da177e4 LT |
347 | switch (t->type) { |
348 | case TUNER_MT2032: | |
09fee5f8 MCC |
349 | if (!dvb_attach(microtune_attach, |
350 | &t->fe, t->i2c->adapter, t->i2c->addr)) | |
351 | goto attach_failed; | |
1da177e4 LT |
352 | break; |
353 | case TUNER_PHILIPS_TDA8290: | |
5bea1cd3 | 354 | { |
09fee5f8 MCC |
355 | struct tda829x_config cfg = { |
356 | .lna_cfg = t->config, | |
09fee5f8 MCC |
357 | }; |
358 | if (!dvb_attach(tda829x_attach, &t->fe, t->i2c->adapter, | |
359 | t->i2c->addr, &cfg)) | |
360 | goto attach_failed; | |
5bea1cd3 MK |
361 | break; |
362 | } | |
586b0cab | 363 | case TUNER_TEA5767: |
a07c8779 MK |
364 | if (!dvb_attach(tea5767_attach, &t->fe, |
365 | t->i2c->adapter, t->i2c->addr)) | |
b9ef6bbb | 366 | goto attach_failed; |
f7ce3cc6 | 367 | t->mode_mask = T_RADIO; |
586b0cab | 368 | break; |
8573a9e6 | 369 | case TUNER_TEA5761: |
a07c8779 MK |
370 | if (!dvb_attach(tea5761_attach, &t->fe, |
371 | t->i2c->adapter, t->i2c->addr)) | |
b9ef6bbb | 372 | goto attach_failed; |
8573a9e6 MCC |
373 | t->mode_mask = T_RADIO; |
374 | break; | |
586b0cab MCC |
375 | case TUNER_PHILIPS_FMD1216ME_MK3: |
376 | buffer[0] = 0x0b; | |
377 | buffer[1] = 0xdc; | |
378 | buffer[2] = 0x9c; | |
379 | buffer[3] = 0x60; | |
f7ce3cc6 | 380 | i2c_master_send(c, buffer, 4); |
586b0cab MCC |
381 | mdelay(1); |
382 | buffer[2] = 0x86; | |
383 | buffer[3] = 0x54; | |
f7ce3cc6 | 384 | i2c_master_send(c, buffer, 4); |
a07c8779 MK |
385 | if (!dvb_attach(simple_tuner_attach, &t->fe, |
386 | t->i2c->adapter, t->i2c->addr, t->type)) | |
b9ef6bbb | 387 | goto attach_failed; |
586b0cab | 388 | break; |
93df3413 HH |
389 | case TUNER_PHILIPS_TD1316: |
390 | buffer[0] = 0x0b; | |
391 | buffer[1] = 0xdc; | |
392 | buffer[2] = 0x86; | |
393 | buffer[3] = 0xa4; | |
a07c8779 MK |
394 | i2c_master_send(c, buffer, 4); |
395 | if (!dvb_attach(simple_tuner_attach, &t->fe, | |
396 | t->i2c->adapter, t->i2c->addr, t->type)) | |
b9ef6bbb | 397 | goto attach_failed; |
ac272ed7 | 398 | break; |
690c544c MCC |
399 | case TUNER_XC2028: |
400 | { | |
a37b4c9b ML |
401 | struct xc2028_config cfg = { |
402 | .i2c_adap = t->i2c->adapter, | |
403 | .i2c_addr = t->i2c->addr, | |
a37b4c9b | 404 | }; |
a07c8779 | 405 | if (!dvb_attach(xc2028_attach, &t->fe, &cfg)) |
b9ef6bbb | 406 | goto attach_failed; |
690c544c MCC |
407 | break; |
408 | } | |
15396236 | 409 | case TUNER_TDA9887: |
09fee5f8 MCC |
410 | if (!dvb_attach(tda9887_attach, |
411 | &t->fe, t->i2c->adapter, t->i2c->addr)) | |
412 | goto attach_failed; | |
15396236 | 413 | break; |
27c685a4 | 414 | case TUNER_XC5000: |
b9ef6bbb | 415 | { |
27c685a4 | 416 | xc5000_cfg.i2c_address = t->i2c->addr; |
ea227863 DH |
417 | /* if_khz will be set when the digital dvb_attach() occurs */ |
418 | xc5000_cfg.if_khz = 0; | |
a07c8779 | 419 | if (!dvb_attach(xc5000_attach, |
30650961 | 420 | &t->fe, t->i2c->adapter, &xc5000_cfg)) |
b9ef6bbb | 421 | goto attach_failed; |
27c685a4 | 422 | break; |
b9ef6bbb | 423 | } |
93463895 MK |
424 | case TUNER_NXP_TDA18271: |
425 | { | |
426 | struct tda18271_config cfg = { | |
427 | .config = t->config, | |
428 | }; | |
429 | ||
430 | if (!dvb_attach(tda18271_attach, &t->fe, t->i2c->addr, | |
431 | t->i2c->adapter, &cfg)) | |
432 | goto attach_failed; | |
433 | break; | |
434 | } | |
1da177e4 | 435 | default: |
a07c8779 MK |
436 | if (!dvb_attach(simple_tuner_attach, &t->fe, |
437 | t->i2c->adapter, t->i2c->addr, t->type)) | |
b9ef6bbb MCC |
438 | goto attach_failed; |
439 | ||
1da177e4 LT |
440 | break; |
441 | } | |
f7ce3cc6 | 442 | |
bc3e5c7f | 443 | if ((NULL == analog_ops->set_params) && |
1dde7a4f | 444 | (fe_tuner_ops->set_analog_params)) { |
a07c8779 | 445 | |
7271e60a | 446 | t->name = fe_tuner_ops->info.name; |
e18f9444 | 447 | |
4e9154b8 | 448 | t->fe.analog_demod_priv = t; |
e8a4a9e7 | 449 | memcpy(analog_ops, &tuner_analog_ops, |
bc3e5c7f | 450 | sizeof(struct analog_demod_ops)); |
a07c8779 | 451 | |
a55db8cd | 452 | } else { |
7271e60a | 453 | t->name = analog_ops->info.name; |
e18f9444 MK |
454 | } |
455 | ||
7271e60a | 456 | tuner_dbg("type set to %s\n", t->name); |
e18f9444 | 457 | |
f7ce3cc6 MCC |
458 | if (t->mode_mask == T_UNINITIALIZED) |
459 | t->mode_mask = new_mode_mask; | |
460 | ||
b9ef6bbb MCC |
461 | /* xc2028/3028 and xc5000 requires a firmware to be set-up later |
462 | trying to set a frequency here will just fail | |
463 | FIXME: better to move set_freq to the tuner code. This is needed | |
464 | on analog tuners for PLL to properly work | |
465 | */ | |
466 | if (t->type != TUNER_XC2028 && t->type != TUNER_XC5000) | |
467 | set_freq(c, (V4L2_TUNER_RADIO == t->mode) ? | |
468 | t->radio_freq : t->tv_freq); | |
469 | ||
f7ce3cc6 | 470 | tuner_dbg("%s %s I2C addr 0x%02x with type %d used for 0x%02x\n", |
604f28e2 | 471 | c->adapter->name, c->driver->driver.name, c->addr << 1, type, |
f7ce3cc6 | 472 | t->mode_mask); |
b9ef6bbb MCC |
473 | return; |
474 | ||
475 | attach_failed: | |
476 | tuner_dbg("Tuner attach for type = %d failed.\n", t->type); | |
477 | t->type = TUNER_ABSENT; | |
478 | t->mode_mask = T_UNINITIALIZED; | |
479 | ||
480 | return; | |
1da177e4 LT |
481 | } |
482 | ||
f7ce3cc6 MCC |
483 | /* |
484 | * This function apply tuner config to tuner specified | |
485 | * by tun_setup structure. I addr is unset, then admin status | |
486 | * and tun addr status is more precise then current status, | |
487 | * it's applied. Otherwise status and type are applied only to | |
488 | * tuner with exactly the same addr. | |
489 | */ | |
490 | ||
491 | static void set_addr(struct i2c_client *c, struct tuner_setup *tun_setup) | |
492 | { | |
e8a4a9e7 | 493 | struct tuner *t = to_tuner(i2c_get_clientdata(c)); |
f7ce3cc6 | 494 | |
de956c1e HH |
495 | if ( (t->type == UNSET && ((tun_setup->addr == ADDR_UNSET) && |
496 | (t->mode_mask & tun_setup->mode_mask))) || | |
497 | (tun_setup->addr == c->addr)) { | |
498 | set_type(c, tun_setup->type, tun_setup->mode_mask, | |
cfeb8839 | 499 | tun_setup->config, tun_setup->tuner_callback); |
b9ef6bbb MCC |
500 | } else |
501 | tuner_dbg("set addr discarded for type %i, mask %x. " | |
502 | "Asked to change tuner at addr 0x%02x, with mask %x\n", | |
503 | t->type, t->mode_mask, | |
504 | tun_setup->addr, tun_setup->mode_mask); | |
f7ce3cc6 | 505 | } |
56fc08ca | 506 | |
f7ce3cc6 | 507 | static inline int check_mode(struct tuner *t, char *cmd) |
56fc08ca | 508 | { |
793cf9e6 | 509 | if ((1 << t->mode & t->mode_mask) == 0) { |
4d3437df | 510 | return -EINVAL; |
793cf9e6 MCC |
511 | } |
512 | ||
513 | switch (t->mode) { | |
514 | case V4L2_TUNER_RADIO: | |
515 | tuner_dbg("Cmd %s accepted for radio\n", cmd); | |
516 | break; | |
517 | case V4L2_TUNER_ANALOG_TV: | |
518 | tuner_dbg("Cmd %s accepted for analog TV\n", cmd); | |
519 | break; | |
520 | case V4L2_TUNER_DIGITAL_TV: | |
521 | tuner_dbg("Cmd %s accepted for digital TV\n", cmd); | |
522 | break; | |
56fc08ca | 523 | } |
793cf9e6 | 524 | return 0; |
56fc08ca | 525 | } |
56fc08ca | 526 | |
f7ce3cc6 | 527 | /* get more precise norm info from insmod option */ |
1da177e4 LT |
528 | static int tuner_fixup_std(struct tuner *t) |
529 | { | |
530 | if ((t->std & V4L2_STD_PAL) == V4L2_STD_PAL) { | |
1da177e4 | 531 | switch (pal[0]) { |
e71ced1a HV |
532 | case '6': |
533 | tuner_dbg ("insmod fixup: PAL => PAL-60\n"); | |
534 | t->std = V4L2_STD_PAL_60; | |
535 | break; | |
1da177e4 LT |
536 | case 'b': |
537 | case 'B': | |
538 | case 'g': | |
539 | case 'G': | |
f7ce3cc6 | 540 | tuner_dbg ("insmod fixup: PAL => PAL-BG\n"); |
1da177e4 LT |
541 | t->std = V4L2_STD_PAL_BG; |
542 | break; | |
543 | case 'i': | |
544 | case 'I': | |
f7ce3cc6 | 545 | tuner_dbg ("insmod fixup: PAL => PAL-I\n"); |
1da177e4 LT |
546 | t->std = V4L2_STD_PAL_I; |
547 | break; | |
548 | case 'd': | |
549 | case 'D': | |
550 | case 'k': | |
551 | case 'K': | |
f7ce3cc6 | 552 | tuner_dbg ("insmod fixup: PAL => PAL-DK\n"); |
1da177e4 LT |
553 | t->std = V4L2_STD_PAL_DK; |
554 | break; | |
f7ce3cc6 MCC |
555 | case 'M': |
556 | case 'm': | |
557 | tuner_dbg ("insmod fixup: PAL => PAL-M\n"); | |
558 | t->std = V4L2_STD_PAL_M; | |
559 | break; | |
560 | case 'N': | |
561 | case 'n': | |
7e578191 MCC |
562 | if (pal[1] == 'c' || pal[1] == 'C') { |
563 | tuner_dbg("insmod fixup: PAL => PAL-Nc\n"); | |
564 | t->std = V4L2_STD_PAL_Nc; | |
565 | } else { | |
566 | tuner_dbg ("insmod fixup: PAL => PAL-N\n"); | |
567 | t->std = V4L2_STD_PAL_N; | |
568 | } | |
f7ce3cc6 | 569 | break; |
21d4df37 MCC |
570 | case '-': |
571 | /* default parameter, do nothing */ | |
572 | break; | |
573 | default: | |
574 | tuner_warn ("pal= argument not recognised\n"); | |
575 | break; | |
1da177e4 LT |
576 | } |
577 | } | |
f7ce3cc6 MCC |
578 | if ((t->std & V4L2_STD_SECAM) == V4L2_STD_SECAM) { |
579 | switch (secam[0]) { | |
7e578191 MCC |
580 | case 'b': |
581 | case 'B': | |
582 | case 'g': | |
583 | case 'G': | |
584 | case 'h': | |
585 | case 'H': | |
586 | tuner_dbg("insmod fixup: SECAM => SECAM-BGH\n"); | |
587 | t->std = V4L2_STD_SECAM_B | V4L2_STD_SECAM_G | V4L2_STD_SECAM_H; | |
588 | break; | |
f7ce3cc6 MCC |
589 | case 'd': |
590 | case 'D': | |
591 | case 'k': | |
592 | case 'K': | |
593 | tuner_dbg ("insmod fixup: SECAM => SECAM-DK\n"); | |
594 | t->std = V4L2_STD_SECAM_DK; | |
595 | break; | |
596 | case 'l': | |
597 | case 'L': | |
800d3c6f MCC |
598 | if ((secam[1]=='C')||(secam[1]=='c')) { |
599 | tuner_dbg ("insmod fixup: SECAM => SECAM-L'\n"); | |
600 | t->std = V4L2_STD_SECAM_LC; | |
601 | } else { | |
602 | tuner_dbg ("insmod fixup: SECAM => SECAM-L\n"); | |
603 | t->std = V4L2_STD_SECAM_L; | |
604 | } | |
f7ce3cc6 | 605 | break; |
21d4df37 MCC |
606 | case '-': |
607 | /* default parameter, do nothing */ | |
608 | break; | |
609 | default: | |
610 | tuner_warn ("secam= argument not recognised\n"); | |
611 | break; | |
f7ce3cc6 MCC |
612 | } |
613 | } | |
614 | ||
7e578191 MCC |
615 | if ((t->std & V4L2_STD_NTSC) == V4L2_STD_NTSC) { |
616 | switch (ntsc[0]) { | |
617 | case 'm': | |
618 | case 'M': | |
619 | tuner_dbg("insmod fixup: NTSC => NTSC-M\n"); | |
620 | t->std = V4L2_STD_NTSC_M; | |
621 | break; | |
622 | case 'j': | |
623 | case 'J': | |
624 | tuner_dbg("insmod fixup: NTSC => NTSC_M_JP\n"); | |
625 | t->std = V4L2_STD_NTSC_M_JP; | |
626 | break; | |
d97a11e0 HV |
627 | case 'k': |
628 | case 'K': | |
629 | tuner_dbg("insmod fixup: NTSC => NTSC_M_KR\n"); | |
630 | t->std = V4L2_STD_NTSC_M_KR; | |
631 | break; | |
7e578191 MCC |
632 | case '-': |
633 | /* default parameter, do nothing */ | |
634 | break; | |
635 | default: | |
636 | tuner_info("ntsc= argument not recognised\n"); | |
637 | break; | |
638 | } | |
639 | } | |
1da177e4 LT |
640 | return 0; |
641 | } | |
642 | ||
4e9154b8 | 643 | static void tuner_status(struct dvb_frontend *fe) |
7e578191 | 644 | { |
4e9154b8 | 645 | struct tuner *t = fe->analog_demod_priv; |
7e578191 | 646 | unsigned long freq, freq_fraction; |
a07c8779 MK |
647 | struct dvb_tuner_ops *fe_tuner_ops = &fe->ops.tuner_ops; |
648 | struct analog_demod_ops *analog_ops = &fe->ops.analog_ops; | |
7e578191 MCC |
649 | const char *p; |
650 | ||
651 | switch (t->mode) { | |
652 | case V4L2_TUNER_RADIO: p = "radio"; break; | |
653 | case V4L2_TUNER_ANALOG_TV: p = "analog TV"; break; | |
654 | case V4L2_TUNER_DIGITAL_TV: p = "digital TV"; break; | |
655 | default: p = "undefined"; break; | |
656 | } | |
657 | if (t->mode == V4L2_TUNER_RADIO) { | |
27487d44 HV |
658 | freq = t->radio_freq / 16000; |
659 | freq_fraction = (t->radio_freq % 16000) * 100 / 16000; | |
7e578191 | 660 | } else { |
27487d44 HV |
661 | freq = t->tv_freq / 16; |
662 | freq_fraction = (t->tv_freq % 16) * 100 / 16; | |
7e578191 MCC |
663 | } |
664 | tuner_info("Tuner mode: %s\n", p); | |
665 | tuner_info("Frequency: %lu.%02lu MHz\n", freq, freq_fraction); | |
4ae5c2e5 | 666 | tuner_info("Standard: 0x%08lx\n", (unsigned long)t->std); |
8a4b275f HV |
667 | if (t->mode != V4L2_TUNER_RADIO) |
668 | return; | |
e18f9444 MK |
669 | if (fe_tuner_ops->get_status) { |
670 | u32 tuner_status; | |
671 | ||
672 | fe_tuner_ops->get_status(&t->fe, &tuner_status); | |
673 | if (tuner_status & TUNER_STATUS_LOCKED) | |
674 | tuner_info("Tuner is locked.\n"); | |
675 | if (tuner_status & TUNER_STATUS_STEREO) | |
676 | tuner_info("Stereo: yes\n"); | |
677 | } | |
bc3e5c7f MK |
678 | if (analog_ops->has_signal) |
679 | tuner_info("Signal strength: %d\n", | |
680 | analog_ops->has_signal(fe)); | |
681 | if (analog_ops->is_stereo) | |
682 | tuner_info("Stereo: %s\n", | |
683 | analog_ops->is_stereo(fe) ? "yes" : "no"); | |
7e578191 | 684 | } |
8a4b275f | 685 | |
1da177e4 LT |
686 | /* ---------------------------------------------------------------------- */ |
687 | ||
f7ce3cc6 MCC |
688 | /* |
689 | * Switch tuner to other mode. If tuner support both tv and radio, | |
690 | * set another frequency to some value (This is needed for some pal | |
691 | * tuners to avoid locking). Otherwise, just put second tuner in | |
692 | * standby mode. | |
693 | */ | |
694 | ||
695 | static inline int set_mode(struct i2c_client *client, struct tuner *t, int mode, char *cmd) | |
696 | { | |
bc3e5c7f | 697 | struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops; |
1dde7a4f | 698 | |
4ac97914 MCC |
699 | if (mode == t->mode) |
700 | return 0; | |
701 | ||
702 | t->mode = mode; | |
703 | ||
4d3437df | 704 | if (check_mode(t, cmd) == -EINVAL) { |
16a5e53d MCC |
705 | tuner_dbg("Tuner doesn't support this mode. " |
706 | "Putting tuner to sleep\n"); | |
4ac97914 | 707 | t->mode = T_STANDBY; |
bc3e5c7f MK |
708 | if (analog_ops->standby) |
709 | analog_ops->standby(&t->fe); | |
4d3437df | 710 | return -EINVAL; |
4ac97914 MCC |
711 | } |
712 | return 0; | |
f7ce3cc6 MCC |
713 | } |
714 | ||
715 | #define switch_v4l2() if (!t->using_v4l2) \ | |
4ac97914 MCC |
716 | tuner_dbg("switching to v4l2\n"); \ |
717 | t->using_v4l2 = 1; | |
f7ce3cc6 MCC |
718 | |
719 | static inline int check_v4l2(struct tuner *t) | |
720 | { | |
3bbe5a83 HV |
721 | /* bttv still uses both v4l1 and v4l2 calls to the tuner (v4l2 for |
722 | TV, v4l1 for radio), until that is fixed this code is disabled. | |
723 | Otherwise the radio (v4l1) wouldn't tune after using the TV (v4l2) | |
724 | first. */ | |
f7ce3cc6 MCC |
725 | return 0; |
726 | } | |
1da177e4 | 727 | |
e8a4a9e7 | 728 | static int tuner_s_type_addr(struct v4l2_subdev *sd, struct tuner_setup *type) |
1da177e4 | 729 | { |
e8a4a9e7 HV |
730 | struct tuner *t = to_tuner(sd); |
731 | struct i2c_client *client = v4l2_get_subdevdata(sd); | |
732 | ||
733 | tuner_dbg("Calling set_type_addr for type=%d, addr=0x%02x, mode=0x%02x, config=0x%02x\n", | |
734 | type->type, | |
735 | type->addr, | |
736 | type->mode_mask, | |
737 | type->config); | |
738 | ||
739 | set_addr(client, type); | |
740 | return 0; | |
741 | } | |
742 | ||
743 | static int tuner_s_radio(struct v4l2_subdev *sd) | |
744 | { | |
745 | struct tuner *t = to_tuner(sd); | |
746 | struct i2c_client *client = v4l2_get_subdevdata(sd); | |
747 | ||
bccfa449 | 748 | if (set_mode(client, t, V4L2_TUNER_RADIO, "s_radio") == -EINVAL) |
e8a4a9e7 HV |
749 | return 0; |
750 | if (t->radio_freq) | |
751 | set_freq(client, t->radio_freq); | |
752 | return 0; | |
753 | } | |
754 | ||
622b828a | 755 | static int tuner_s_power(struct v4l2_subdev *sd, int on) |
e8a4a9e7 HV |
756 | { |
757 | struct tuner *t = to_tuner(sd); | |
bc3e5c7f | 758 | struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops; |
1da177e4 | 759 | |
622b828a LP |
760 | if (on) |
761 | return 0; | |
762 | ||
16a5e53d MCC |
763 | tuner_dbg("Putting tuner to sleep\n"); |
764 | ||
622b828a | 765 | if (check_mode(t, "s_power") == -EINVAL) |
e8a4a9e7 HV |
766 | return 0; |
767 | t->mode = T_STANDBY; | |
768 | if (analog_ops->standby) | |
769 | analog_ops->standby(&t->fe); | |
770 | return 0; | |
771 | } | |
5e453dc7 | 772 | |
e8a4a9e7 HV |
773 | static int tuner_s_config(struct v4l2_subdev *sd, const struct v4l2_priv_tun_config *cfg) |
774 | { | |
775 | struct tuner *t = to_tuner(sd); | |
776 | struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops; | |
1da177e4 | 777 | |
e8a4a9e7 HV |
778 | if (t->type != cfg->tuner) |
779 | return 0; | |
7f171123 | 780 | |
e8a4a9e7 HV |
781 | if (analog_ops->set_config) { |
782 | analog_ops->set_config(&t->fe, cfg->priv); | |
783 | return 0; | |
7f171123 | 784 | } |
1da177e4 | 785 | |
e8a4a9e7 HV |
786 | tuner_dbg("Tuner frontend module has no way to set config\n"); |
787 | return 0; | |
788 | } | |
56fc08ca | 789 | |
e8a4a9e7 HV |
790 | /* --- v4l ioctls --- */ |
791 | /* take care: bttv does userspace copying, we'll get a | |
792 | kernel pointer here... */ | |
793 | static int tuner_s_std(struct v4l2_subdev *sd, v4l2_std_id std) | |
794 | { | |
795 | struct tuner *t = to_tuner(sd); | |
796 | struct i2c_client *client = v4l2_get_subdevdata(sd); | |
f7ce3cc6 | 797 | |
bccfa449 | 798 | if (set_mode(client, t, V4L2_TUNER_ANALOG_TV, "s_std") == -EINVAL) |
e8a4a9e7 | 799 | return 0; |
f7ce3cc6 | 800 | |
e8a4a9e7 | 801 | switch_v4l2(); |
c184ca36 | 802 | |
e8a4a9e7 HV |
803 | t->std = std; |
804 | tuner_fixup_std(t); | |
805 | if (t->tv_freq) | |
806 | set_freq(client, t->tv_freq); | |
807 | return 0; | |
808 | } | |
f7ce3cc6 | 809 | |
e8a4a9e7 HV |
810 | static int tuner_s_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f) |
811 | { | |
812 | struct tuner *t = to_tuner(sd); | |
813 | struct i2c_client *client = v4l2_get_subdevdata(sd); | |
f7ce3cc6 | 814 | |
bccfa449 | 815 | if (set_mode(client, t, f->type, "s_frequency") == -EINVAL) |
e8a4a9e7 HV |
816 | return 0; |
817 | switch_v4l2(); | |
818 | set_freq(client, f->frequency); | |
8a4b275f | 819 | |
e8a4a9e7 HV |
820 | return 0; |
821 | } | |
f7ce3cc6 | 822 | |
e8a4a9e7 HV |
823 | static int tuner_g_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f) |
824 | { | |
825 | struct tuner *t = to_tuner(sd); | |
826 | struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops; | |
f7ce3cc6 | 827 | |
bccfa449 | 828 | if (check_mode(t, "g_frequency") == -EINVAL) |
e8a4a9e7 HV |
829 | return 0; |
830 | switch_v4l2(); | |
831 | f->type = t->mode; | |
832 | if (fe_tuner_ops->get_frequency) { | |
833 | u32 abs_freq; | |
834 | ||
835 | fe_tuner_ops->get_frequency(&t->fe, &abs_freq); | |
836 | f->frequency = (V4L2_TUNER_RADIO == t->mode) ? | |
75b697f7 JL |
837 | DIV_ROUND_CLOSEST(abs_freq * 2, 125) : |
838 | DIV_ROUND_CLOSEST(abs_freq, 62500); | |
e8a4a9e7 HV |
839 | return 0; |
840 | } | |
841 | f->frequency = (V4L2_TUNER_RADIO == t->mode) ? | |
842 | t->radio_freq : t->tv_freq; | |
843 | return 0; | |
844 | } | |
f7ce3cc6 | 845 | |
e8a4a9e7 HV |
846 | static int tuner_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt) |
847 | { | |
848 | struct tuner *t = to_tuner(sd); | |
849 | struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops; | |
850 | struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops; | |
851 | ||
bccfa449 | 852 | if (check_mode(t, "g_tuner") == -EINVAL) |
e8a4a9e7 HV |
853 | return 0; |
854 | switch_v4l2(); | |
855 | ||
856 | vt->type = t->mode; | |
857 | if (analog_ops->get_afc) | |
858 | vt->afc = analog_ops->get_afc(&t->fe); | |
859 | if (t->mode == V4L2_TUNER_ANALOG_TV) | |
860 | vt->capability |= V4L2_TUNER_CAP_NORM; | |
861 | if (t->mode != V4L2_TUNER_RADIO) { | |
862 | vt->rangelow = tv_range[0] * 16; | |
863 | vt->rangehigh = tv_range[1] * 16; | |
864 | return 0; | |
865 | } | |
866 | ||
867 | /* radio mode */ | |
868 | vt->rxsubchans = | |
869 | V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO; | |
870 | if (fe_tuner_ops->get_status) { | |
871 | u32 tuner_status; | |
872 | ||
873 | fe_tuner_ops->get_status(&t->fe, &tuner_status); | |
874 | vt->rxsubchans = | |
875 | (tuner_status & TUNER_STATUS_STEREO) ? | |
876 | V4L2_TUNER_SUB_STEREO : | |
877 | V4L2_TUNER_SUB_MONO; | |
878 | } else { | |
879 | if (analog_ops->is_stereo) { | |
880 | vt->rxsubchans = | |
881 | analog_ops->is_stereo(&t->fe) ? | |
882 | V4L2_TUNER_SUB_STEREO : | |
883 | V4L2_TUNER_SUB_MONO; | |
56fc08ca | 884 | } |
1da177e4 | 885 | } |
e8a4a9e7 HV |
886 | if (analog_ops->has_signal) |
887 | vt->signal = analog_ops->has_signal(&t->fe); | |
888 | vt->capability |= | |
889 | V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO; | |
890 | vt->audmode = t->audmode; | |
891 | vt->rangelow = radio_range[0] * 16000; | |
892 | vt->rangehigh = radio_range[1] * 16000; | |
893 | return 0; | |
894 | } | |
895 | ||
896 | static int tuner_s_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt) | |
897 | { | |
898 | struct tuner *t = to_tuner(sd); | |
899 | struct i2c_client *client = v4l2_get_subdevdata(sd); | |
900 | ||
bccfa449 | 901 | if (check_mode(t, "s_tuner") == -EINVAL) |
e8a4a9e7 HV |
902 | return 0; |
903 | ||
904 | switch_v4l2(); | |
905 | ||
906 | /* do nothing unless we're a radio tuner */ | |
907 | if (t->mode != V4L2_TUNER_RADIO) | |
908 | return 0; | |
909 | t->audmode = vt->audmode; | |
910 | set_radio_freq(client, t->radio_freq); | |
911 | return 0; | |
912 | } | |
913 | ||
914 | static int tuner_log_status(struct v4l2_subdev *sd) | |
915 | { | |
916 | struct tuner *t = to_tuner(sd); | |
917 | struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops; | |
1da177e4 | 918 | |
e8a4a9e7 HV |
919 | if (analog_ops->tuner_status) |
920 | analog_ops->tuner_status(&t->fe); | |
1da177e4 LT |
921 | return 0; |
922 | } | |
923 | ||
21b48a70 | 924 | static int tuner_suspend(struct i2c_client *c, pm_message_t state) |
1da177e4 | 925 | { |
e8a4a9e7 | 926 | struct tuner *t = to_tuner(i2c_get_clientdata(c)); |
1da177e4 | 927 | |
9dd659de | 928 | tuner_dbg("suspend\n"); |
1da177e4 LT |
929 | /* FIXME: power down ??? */ |
930 | return 0; | |
931 | } | |
932 | ||
21b48a70 | 933 | static int tuner_resume(struct i2c_client *c) |
1da177e4 | 934 | { |
e8a4a9e7 | 935 | struct tuner *t = to_tuner(i2c_get_clientdata(c)); |
1da177e4 | 936 | |
9dd659de | 937 | tuner_dbg("resume\n"); |
27487d44 HV |
938 | if (V4L2_TUNER_RADIO == t->mode) { |
939 | if (t->radio_freq) | |
940 | set_freq(c, t->radio_freq); | |
941 | } else { | |
942 | if (t->tv_freq) | |
943 | set_freq(c, t->tv_freq); | |
944 | } | |
1da177e4 LT |
945 | return 0; |
946 | } | |
947 | ||
75b4c260 HV |
948 | static int tuner_command(struct i2c_client *client, unsigned cmd, void *arg) |
949 | { | |
950 | struct v4l2_subdev *sd = i2c_get_clientdata(client); | |
951 | ||
952 | /* TUNER_SET_CONFIG is still called by tuner-simple.c, so we have | |
953 | to handle it here. | |
954 | There must be a better way of doing this... */ | |
955 | switch (cmd) { | |
956 | case TUNER_SET_CONFIG: | |
957 | return tuner_s_config(sd, arg); | |
958 | } | |
959 | return -ENOIOCTLCMD; | |
960 | } | |
961 | ||
e8a4a9e7 HV |
962 | /* ----------------------------------------------------------------------- */ |
963 | ||
964 | static const struct v4l2_subdev_core_ops tuner_core_ops = { | |
965 | .log_status = tuner_log_status, | |
f41737ec | 966 | .s_std = tuner_s_std, |
622b828a | 967 | .s_power = tuner_s_power, |
e8a4a9e7 HV |
968 | }; |
969 | ||
970 | static const struct v4l2_subdev_tuner_ops tuner_tuner_ops = { | |
e8a4a9e7 HV |
971 | .s_radio = tuner_s_radio, |
972 | .g_tuner = tuner_g_tuner, | |
973 | .s_tuner = tuner_s_tuner, | |
974 | .s_frequency = tuner_s_frequency, | |
975 | .g_frequency = tuner_g_frequency, | |
976 | .s_type_addr = tuner_s_type_addr, | |
977 | .s_config = tuner_s_config, | |
978 | }; | |
979 | ||
980 | static const struct v4l2_subdev_ops tuner_ops = { | |
981 | .core = &tuner_core_ops, | |
982 | .tuner = &tuner_tuner_ops, | |
983 | }; | |
984 | ||
92de1f16 HV |
985 | /* ---------------------------------------------------------------------- */ |
986 | ||
c52c4d06 | 987 | static LIST_HEAD(tuner_list); |
92de1f16 HV |
988 | |
989 | /* Search for existing radio and/or TV tuners on the given I2C adapter. | |
9dd659de | 990 | Note that when this function is called from tuner_probe you can be |
92de1f16 HV |
991 | certain no other devices will be added/deleted at the same time, I2C |
992 | core protects against that. */ | |
993 | static void tuner_lookup(struct i2c_adapter *adap, | |
994 | struct tuner **radio, struct tuner **tv) | |
995 | { | |
996 | struct tuner *pos; | |
997 | ||
998 | *radio = NULL; | |
999 | *tv = NULL; | |
1000 | ||
1001 | list_for_each_entry(pos, &tuner_list, list) { | |
1002 | int mode_mask; | |
1003 | ||
1004 | if (pos->i2c->adapter != adap || | |
0c846743 | 1005 | strcmp(pos->i2c->driver->driver.name, "tuner")) |
92de1f16 HV |
1006 | continue; |
1007 | ||
1008 | mode_mask = pos->mode_mask & ~T_STANDBY; | |
1009 | if (*radio == NULL && mode_mask == T_RADIO) | |
1010 | *radio = pos; | |
1011 | /* Note: currently TDA9887 is the only demod-only | |
1012 | device. If other devices appear then we need to | |
1013 | make this test more general. */ | |
1014 | else if (*tv == NULL && pos->type != TUNER_TDA9887 && | |
1015 | (pos->mode_mask & (T_ANALOG_TV | T_DIGITAL_TV))) | |
1016 | *tv = pos; | |
1017 | } | |
1018 | } | |
1019 | ||
1020 | /* During client attach, set_type is called by adapter's attach_inform callback. | |
9dd659de | 1021 | set_type must then be completed by tuner_probe. |
92de1f16 | 1022 | */ |
d2653e92 JD |
1023 | static int tuner_probe(struct i2c_client *client, |
1024 | const struct i2c_device_id *id) | |
92de1f16 | 1025 | { |
92de1f16 HV |
1026 | struct tuner *t; |
1027 | struct tuner *radio; | |
1028 | struct tuner *tv; | |
1029 | ||
92de1f16 | 1030 | t = kzalloc(sizeof(struct tuner), GFP_KERNEL); |
9dd659de | 1031 | if (NULL == t) |
92de1f16 | 1032 | return -ENOMEM; |
e8a4a9e7 | 1033 | v4l2_i2c_subdev_init(&t->sd, client, &tuner_ops); |
92de1f16 | 1034 | t->i2c = client; |
7271e60a | 1035 | t->name = "(tuner unset)"; |
92de1f16 HV |
1036 | t->type = UNSET; |
1037 | t->audmode = V4L2_TUNER_MODE_STEREO; | |
1038 | t->mode_mask = T_UNINITIALIZED; | |
1039 | ||
1040 | if (show_i2c) { | |
1041 | unsigned char buffer[16]; | |
1042 | int i, rc; | |
1043 | ||
1044 | memset(buffer, 0, sizeof(buffer)); | |
1045 | rc = i2c_master_recv(client, buffer, sizeof(buffer)); | |
1046 | tuner_info("I2C RECV = "); | |
1047 | for (i = 0; i < rc; i++) | |
1048 | printk(KERN_CONT "%02x ", buffer[i]); | |
1049 | printk("\n"); | |
1050 | } | |
9dd659de | 1051 | /* HACK: This test was added to avoid tuner to probe tda9840 and |
92de1f16 | 1052 | tea6415c on the MXB card */ |
9dd659de HV |
1053 | if (client->adapter->id == I2C_HW_SAA7146 && client->addr < 0x4a) { |
1054 | kfree(t); | |
92de1f16 | 1055 | return -ENODEV; |
9dd659de | 1056 | } |
92de1f16 HV |
1057 | |
1058 | /* autodetection code based on the i2c addr */ | |
1059 | if (!no_autodetect) { | |
9dd659de | 1060 | switch (client->addr) { |
92de1f16 | 1061 | case 0x10: |
a07c8779 MK |
1062 | if (tuner_symbol_probe(tea5761_autodetection, |
1063 | t->i2c->adapter, | |
1064 | t->i2c->addr) >= 0) { | |
92de1f16 HV |
1065 | t->type = TUNER_TEA5761; |
1066 | t->mode_mask = T_RADIO; | |
1067 | t->mode = T_STANDBY; | |
1068 | /* Sets freq to FM range */ | |
1069 | t->radio_freq = 87.5 * 16000; | |
1070 | tuner_lookup(t->i2c->adapter, &radio, &tv); | |
1071 | if (tv) | |
1072 | tv->mode_mask &= ~T_RADIO; | |
1073 | ||
1074 | goto register_client; | |
1075 | } | |
867e835f | 1076 | return -ENODEV; |
92de1f16 HV |
1077 | case 0x42: |
1078 | case 0x43: | |
1079 | case 0x4a: | |
1080 | case 0x4b: | |
1081 | /* If chip is not tda8290, don't register. | |
1082 | since it can be tda9887*/ | |
a07c8779 | 1083 | if (tuner_symbol_probe(tda829x_probe, t->i2c->adapter, |
b538d28c | 1084 | t->i2c->addr) >= 0) { |
92de1f16 HV |
1085 | tuner_dbg("tda829x detected\n"); |
1086 | } else { | |
1087 | /* Default is being tda9887 */ | |
1088 | t->type = TUNER_TDA9887; | |
1089 | t->mode_mask = T_RADIO | T_ANALOG_TV | | |
1090 | T_DIGITAL_TV; | |
1091 | t->mode = T_STANDBY; | |
1092 | goto register_client; | |
1093 | } | |
1094 | break; | |
1095 | case 0x60: | |
a07c8779 MK |
1096 | if (tuner_symbol_probe(tea5767_autodetection, |
1097 | t->i2c->adapter, t->i2c->addr) | |
b538d28c | 1098 | >= 0) { |
92de1f16 HV |
1099 | t->type = TUNER_TEA5767; |
1100 | t->mode_mask = T_RADIO; | |
1101 | t->mode = T_STANDBY; | |
1102 | /* Sets freq to FM range */ | |
1103 | t->radio_freq = 87.5 * 16000; | |
1104 | tuner_lookup(t->i2c->adapter, &radio, &tv); | |
1105 | if (tv) | |
1106 | tv->mode_mask &= ~T_RADIO; | |
1107 | ||
1108 | goto register_client; | |
1109 | } | |
1110 | break; | |
1111 | } | |
1112 | } | |
1113 | ||
1114 | /* Initializes only the first TV tuner on this adapter. Why only the | |
1115 | first? Because there are some devices (notably the ones with TI | |
1116 | tuners) that have more than one i2c address for the *same* device. | |
1117 | Experience shows that, except for just one case, the first | |
1118 | address is the right one. The exception is a Russian tuner | |
1119 | (ACORP_Y878F). So, the desired behavior is just to enable the | |
1120 | first found TV tuner. */ | |
1121 | tuner_lookup(t->i2c->adapter, &radio, &tv); | |
1122 | if (tv == NULL) { | |
1123 | t->mode_mask = T_ANALOG_TV | T_DIGITAL_TV; | |
1124 | if (radio == NULL) | |
1125 | t->mode_mask |= T_RADIO; | |
1126 | tuner_dbg("Setting mode_mask to 0x%02x\n", t->mode_mask); | |
1127 | t->tv_freq = 400 * 16; /* Sets freq to VHF High */ | |
1128 | t->radio_freq = 87.5 * 16000; /* Sets freq to FM range */ | |
1129 | } | |
1130 | ||
1131 | /* Should be just before return */ | |
1132 | register_client: | |
9dd659de HV |
1133 | tuner_info("chip found @ 0x%x (%s)\n", client->addr << 1, |
1134 | client->adapter->name); | |
92de1f16 HV |
1135 | |
1136 | /* Sets a default mode */ | |
1137 | if (t->mode_mask & T_ANALOG_TV) { | |
864a6b81 | 1138 | t->mode = V4L2_TUNER_ANALOG_TV; |
92de1f16 | 1139 | } else if (t->mode_mask & T_RADIO) { |
864a6b81 | 1140 | t->mode = V4L2_TUNER_RADIO; |
92de1f16 | 1141 | } else { |
864a6b81 | 1142 | t->mode = V4L2_TUNER_DIGITAL_TV; |
92de1f16 | 1143 | } |
d7cba043 | 1144 | set_type(client, t->type, t->mode_mask, t->config, t->fe.callback); |
9dd659de | 1145 | list_add_tail(&t->list, &tuner_list); |
92de1f16 HV |
1146 | return 0; |
1147 | } | |
1148 | ||
9dd659de | 1149 | static int tuner_remove(struct i2c_client *client) |
92de1f16 | 1150 | { |
e8a4a9e7 | 1151 | struct tuner *t = to_tuner(i2c_get_clientdata(client)); |
92de1f16 | 1152 | |
e8a4a9e7 | 1153 | v4l2_device_unregister_subdev(&t->sd); |
a07c8779 MK |
1154 | tuner_detach(&t->fe); |
1155 | t->fe.analog_demod_priv = NULL; | |
92de1f16 HV |
1156 | |
1157 | list_del(&t->list); | |
1158 | kfree(t); | |
92de1f16 HV |
1159 | return 0; |
1160 | } | |
1161 | ||
1da177e4 LT |
1162 | /* ----------------------------------------------------------------------- */ |
1163 | ||
af294867 JD |
1164 | /* This driver supports many devices and the idea is to let the driver |
1165 | detect which device is present. So rather than listing all supported | |
1166 | devices here, we pretend to support a single, fake device type. */ | |
1167 | static const struct i2c_device_id tuner_id[] = { | |
1168 | { "tuner", }, /* autodetect */ | |
1169 | { } | |
1170 | }; | |
1171 | MODULE_DEVICE_TABLE(i2c, tuner_id); | |
1172 | ||
9dd659de HV |
1173 | static struct v4l2_i2c_driver_data v4l2_i2c_data = { |
1174 | .name = "tuner", | |
9dd659de HV |
1175 | .probe = tuner_probe, |
1176 | .remove = tuner_remove, | |
75b4c260 | 1177 | .command = tuner_command, |
21b48a70 | 1178 | .suspend = tuner_suspend, |
9dd659de | 1179 | .resume = tuner_resume, |
af294867 | 1180 | .id_table = tuner_id, |
1da177e4 | 1181 | }; |
1da177e4 | 1182 | |
1da177e4 LT |
1183 | /* |
1184 | * Overrides for Emacs so that we follow Linus's tabbing style. | |
1185 | * --------------------------------------------------------------------------- | |
1186 | * Local variables: | |
1187 | * c-basic-offset: 8 | |
1188 | * End: | |
1189 | */ |