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