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