0001
0002
0003
0004
0005
0006
0007
0008 #include <linux/delay.h>
0009 #include <linux/i2c.h>
0010 #include <linux/videodev2.h>
0011 #include <media/tuner.h>
0012 #include <media/v4l2-common.h>
0013 #include <media/tuner-types.h>
0014 #include "tuner-i2c.h"
0015 #include "tuner-simple.h"
0016
0017 static int debug;
0018 module_param(debug, int, 0644);
0019 MODULE_PARM_DESC(debug, "enable verbose debug messages");
0020
0021 #define TUNER_SIMPLE_MAX 64
0022 static unsigned int simple_devcount;
0023
0024 static int offset;
0025 module_param(offset, int, 0664);
0026 MODULE_PARM_DESC(offset, "Allows to specify an offset for tuner");
0027
0028 static unsigned int atv_input[TUNER_SIMPLE_MAX] = \
0029 { [0 ... (TUNER_SIMPLE_MAX-1)] = 0 };
0030 static unsigned int dtv_input[TUNER_SIMPLE_MAX] = \
0031 { [0 ... (TUNER_SIMPLE_MAX-1)] = 0 };
0032 module_param_array(atv_input, int, NULL, 0644);
0033 module_param_array(dtv_input, int, NULL, 0644);
0034 MODULE_PARM_DESC(atv_input, "specify atv rf input, 0 for autoselect");
0035 MODULE_PARM_DESC(dtv_input, "specify dtv rf input, 0 for autoselect");
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048 #define TEMIC_SET_PAL_I 0x05
0049 #define TEMIC_SET_PAL_DK 0x09
0050 #define TEMIC_SET_PAL_L 0x0a
0051 #define TEMIC_SET_PAL_L2 0x0b
0052 #define TEMIC_SET_PAL_BG 0x0c
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064 #define PHILIPS_SET_PAL_I 0x01
0065 #define PHILIPS_SET_PAL_BGDK 0x09
0066 #define PHILIPS_SET_PAL_L2 0x0a
0067 #define PHILIPS_SET_PAL_L 0x0b
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078 #define PHILIPS_MF_SET_STD_BG 0x01
0079 #define PHILIPS_MF_SET_STD_L 0x03
0080 #define PHILIPS_MF_SET_STD_LC 0x02
0081
0082
0083
0084 #define TUNER_RATIO_MASK 0x06
0085 #define TUNER_RATIO_SELECT_50 0x00
0086 #define TUNER_RATIO_SELECT_32 0x02
0087 #define TUNER_RATIO_SELECT_166 0x04
0088 #define TUNER_RATIO_SELECT_62 0x06
0089
0090 #define TUNER_CHARGE_PUMP 0x40
0091
0092
0093
0094 #define TUNER_POR 0x80
0095 #define TUNER_FL 0x40
0096 #define TUNER_MODE 0x38
0097 #define TUNER_AFC 0x07
0098 #define TUNER_SIGNAL 0x07
0099 #define TUNER_STEREO 0x10
0100
0101 #define TUNER_PLL_LOCKED 0x40
0102 #define TUNER_STEREO_MK3 0x04
0103
0104 static DEFINE_MUTEX(tuner_simple_list_mutex);
0105 static LIST_HEAD(hybrid_tuner_instance_list);
0106
0107 struct tuner_simple_priv {
0108 unsigned int nr;
0109 u16 last_div;
0110
0111 struct tuner_i2c_props i2c_props;
0112 struct list_head hybrid_tuner_instance_list;
0113
0114 unsigned int type;
0115 struct tunertype *tun;
0116
0117 u32 frequency;
0118 u32 bandwidth;
0119 bool radio_mode;
0120 };
0121
0122
0123
0124 static int tuner_read_status(struct dvb_frontend *fe)
0125 {
0126 struct tuner_simple_priv *priv = fe->tuner_priv;
0127 unsigned char byte;
0128
0129 if (1 != tuner_i2c_xfer_recv(&priv->i2c_props, &byte, 1))
0130 return 0;
0131
0132 return byte;
0133 }
0134
0135 static inline int tuner_signal(const int status)
0136 {
0137 return (status & TUNER_SIGNAL) << 13;
0138 }
0139
0140 static inline int tuner_stereo(const int type, const int status)
0141 {
0142 switch (type) {
0143 case TUNER_PHILIPS_FM1216ME_MK3:
0144 case TUNER_PHILIPS_FM1236_MK3:
0145 case TUNER_PHILIPS_FM1256_IH3:
0146 case TUNER_LG_NTSC_TAPE:
0147 case TUNER_TCL_MF02GIP_5N:
0148 return ((status & TUNER_SIGNAL) == TUNER_STEREO_MK3);
0149 case TUNER_PHILIPS_FM1216MK5:
0150 return status | TUNER_STEREO;
0151 default:
0152 return status & TUNER_STEREO;
0153 }
0154 }
0155
0156 static inline int tuner_islocked(const int status)
0157 {
0158 return (status & TUNER_FL);
0159 }
0160
0161 static inline int tuner_afcstatus(const int status)
0162 {
0163 return (status & TUNER_AFC) - 2;
0164 }
0165
0166
0167 static int simple_get_status(struct dvb_frontend *fe, u32 *status)
0168 {
0169 struct tuner_simple_priv *priv = fe->tuner_priv;
0170 int tuner_status;
0171
0172 if (priv->i2c_props.adap == NULL)
0173 return -EINVAL;
0174
0175 tuner_status = tuner_read_status(fe);
0176
0177 *status = 0;
0178
0179 if (tuner_islocked(tuner_status))
0180 *status = TUNER_STATUS_LOCKED;
0181 if (tuner_stereo(priv->type, tuner_status))
0182 *status |= TUNER_STATUS_STEREO;
0183
0184 tuner_dbg("AFC Status: %d\n", tuner_afcstatus(tuner_status));
0185
0186 return 0;
0187 }
0188
0189 static int simple_get_rf_strength(struct dvb_frontend *fe, u16 *strength)
0190 {
0191 struct tuner_simple_priv *priv = fe->tuner_priv;
0192 int signal;
0193
0194 if (priv->i2c_props.adap == NULL || !priv->radio_mode)
0195 return -EINVAL;
0196
0197 signal = tuner_signal(tuner_read_status(fe));
0198
0199 *strength = signal;
0200
0201 tuner_dbg("Signal strength: %d\n", signal);
0202
0203 return 0;
0204 }
0205
0206
0207
0208 static inline char *tuner_param_name(enum param_type type)
0209 {
0210 char *name;
0211
0212 switch (type) {
0213 case TUNER_PARAM_TYPE_RADIO:
0214 name = "radio";
0215 break;
0216 case TUNER_PARAM_TYPE_PAL:
0217 name = "pal";
0218 break;
0219 case TUNER_PARAM_TYPE_SECAM:
0220 name = "secam";
0221 break;
0222 case TUNER_PARAM_TYPE_NTSC:
0223 name = "ntsc";
0224 break;
0225 case TUNER_PARAM_TYPE_DIGITAL:
0226 name = "digital";
0227 break;
0228 default:
0229 name = "unknown";
0230 break;
0231 }
0232 return name;
0233 }
0234
0235 static struct tuner_params *simple_tuner_params(struct dvb_frontend *fe,
0236 enum param_type desired_type)
0237 {
0238 struct tuner_simple_priv *priv = fe->tuner_priv;
0239 struct tunertype *tun = priv->tun;
0240 int i;
0241
0242 for (i = 0; i < tun->count; i++)
0243 if (desired_type == tun->params[i].type)
0244 break;
0245
0246
0247 if (i == tun->count) {
0248 tuner_dbg("desired params (%s) undefined for tuner %d\n",
0249 tuner_param_name(desired_type), priv->type);
0250 i = 0;
0251 }
0252
0253 tuner_dbg("using tuner params #%d (%s)\n", i,
0254 tuner_param_name(tun->params[i].type));
0255
0256 return &tun->params[i];
0257 }
0258
0259 static int simple_config_lookup(struct dvb_frontend *fe,
0260 struct tuner_params *t_params,
0261 unsigned *frequency, u8 *config, u8 *cb)
0262 {
0263 struct tuner_simple_priv *priv = fe->tuner_priv;
0264 int i;
0265
0266 for (i = 0; i < t_params->count; i++) {
0267 if (*frequency > t_params->ranges[i].limit)
0268 continue;
0269 break;
0270 }
0271 if (i == t_params->count) {
0272 tuner_dbg("frequency out of range (%d > %d)\n",
0273 *frequency, t_params->ranges[i - 1].limit);
0274 *frequency = t_params->ranges[--i].limit;
0275 }
0276 *config = t_params->ranges[i].config;
0277 *cb = t_params->ranges[i].cb;
0278
0279 tuner_dbg("freq = %d.%02d (%d), range = %d, config = 0x%02x, cb = 0x%02x\n",
0280 *frequency / 16, *frequency % 16 * 100 / 16, *frequency,
0281 i, *config, *cb);
0282
0283 return i;
0284 }
0285
0286
0287
0288 static void simple_set_rf_input(struct dvb_frontend *fe,
0289 u8 *config, u8 *cb, unsigned int rf)
0290 {
0291 struct tuner_simple_priv *priv = fe->tuner_priv;
0292
0293 switch (priv->type) {
0294 case TUNER_PHILIPS_TUV1236D:
0295 switch (rf) {
0296 case 1:
0297 *cb |= 0x08;
0298 break;
0299 default:
0300 *cb &= ~0x08;
0301 break;
0302 }
0303 break;
0304 case TUNER_PHILIPS_FCV1236D:
0305 switch (rf) {
0306 case 1:
0307 *cb |= 0x01;
0308 break;
0309 default:
0310 *cb &= ~0x01;
0311 break;
0312 }
0313 break;
0314 default:
0315 break;
0316 }
0317 }
0318
0319 static int simple_std_setup(struct dvb_frontend *fe,
0320 struct analog_parameters *params,
0321 u8 *config, u8 *cb)
0322 {
0323 struct tuner_simple_priv *priv = fe->tuner_priv;
0324 int rc;
0325
0326
0327 switch (priv->type) {
0328 case TUNER_PHILIPS_SECAM:
0329
0330
0331
0332 *cb &= ~0x03;
0333 if (params->std & V4L2_STD_SECAM_L)
0334
0335 *cb |= PHILIPS_MF_SET_STD_L;
0336 else if (params->std & V4L2_STD_SECAM_LC)
0337 *cb |= PHILIPS_MF_SET_STD_LC;
0338 else
0339 *cb |= PHILIPS_MF_SET_STD_BG;
0340 break;
0341
0342 case TUNER_TEMIC_4046FM5:
0343 *cb &= ~0x0f;
0344
0345 if (params->std & V4L2_STD_PAL_BG) {
0346 *cb |= TEMIC_SET_PAL_BG;
0347
0348 } else if (params->std & V4L2_STD_PAL_I) {
0349 *cb |= TEMIC_SET_PAL_I;
0350
0351 } else if (params->std & V4L2_STD_PAL_DK) {
0352 *cb |= TEMIC_SET_PAL_DK;
0353
0354 } else if (params->std & V4L2_STD_SECAM_L) {
0355 *cb |= TEMIC_SET_PAL_L;
0356
0357 }
0358 break;
0359
0360 case TUNER_PHILIPS_FQ1216ME:
0361 *cb &= ~0x0f;
0362
0363 if (params->std & (V4L2_STD_PAL_BG|V4L2_STD_PAL_DK)) {
0364 *cb |= PHILIPS_SET_PAL_BGDK;
0365
0366 } else if (params->std & V4L2_STD_PAL_I) {
0367 *cb |= PHILIPS_SET_PAL_I;
0368
0369 } else if (params->std & V4L2_STD_SECAM_L) {
0370 *cb |= PHILIPS_SET_PAL_L;
0371
0372 }
0373 break;
0374
0375 case TUNER_PHILIPS_FCV1236D:
0376
0377
0378
0379
0380 *cb &= ~0x03;
0381 if (!(params->std & V4L2_STD_ATSC))
0382 *cb |= 2;
0383 break;
0384
0385 case TUNER_MICROTUNE_4042FI5:
0386
0387 *config |= TUNER_CHARGE_PUMP;
0388 break;
0389
0390 case TUNER_PHILIPS_TUV1236D:
0391 {
0392 struct tuner_i2c_props i2c = priv->i2c_props;
0393
0394
0395
0396
0397 u8 buffer[4] = { 0x14, 0x00, 0x17, 0x00};
0398 *cb &= ~0x40;
0399 if (params->std & V4L2_STD_ATSC) {
0400 *cb |= 0x40;
0401 buffer[1] = 0x04;
0402 }
0403
0404 i2c.addr = 0x0a;
0405 rc = tuner_i2c_xfer_send(&i2c, &buffer[0], 2);
0406 if (2 != rc)
0407 tuner_warn("i2c i/o error: rc == %d (should be 2)\n",
0408 rc);
0409 rc = tuner_i2c_xfer_send(&i2c, &buffer[2], 2);
0410 if (2 != rc)
0411 tuner_warn("i2c i/o error: rc == %d (should be 2)\n",
0412 rc);
0413 break;
0414 }
0415 }
0416 if (atv_input[priv->nr])
0417 simple_set_rf_input(fe, config, cb, atv_input[priv->nr]);
0418
0419 return 0;
0420 }
0421
0422 static int simple_set_aux_byte(struct dvb_frontend *fe, u8 config, u8 aux)
0423 {
0424 struct tuner_simple_priv *priv = fe->tuner_priv;
0425 int rc;
0426 u8 buffer[2];
0427
0428 buffer[0] = (config & ~0x38) | 0x18;
0429 buffer[1] = aux;
0430
0431 tuner_dbg("setting aux byte: 0x%02x 0x%02x\n", buffer[0], buffer[1]);
0432
0433 rc = tuner_i2c_xfer_send(&priv->i2c_props, buffer, 2);
0434 if (2 != rc)
0435 tuner_warn("i2c i/o error: rc == %d (should be 2)\n", rc);
0436
0437 return rc == 2 ? 0 : rc;
0438 }
0439
0440 static int simple_post_tune(struct dvb_frontend *fe, u8 *buffer,
0441 u16 div, u8 config, u8 cb)
0442 {
0443 struct tuner_simple_priv *priv = fe->tuner_priv;
0444 int rc;
0445
0446 switch (priv->type) {
0447 case TUNER_LG_TDVS_H06XF:
0448 simple_set_aux_byte(fe, config, 0x20);
0449 break;
0450 case TUNER_PHILIPS_FQ1216LME_MK3:
0451 simple_set_aux_byte(fe, config, 0x60);
0452 break;
0453 case TUNER_MICROTUNE_4042FI5:
0454 {
0455
0456 unsigned long timeout = jiffies + msecs_to_jiffies(1);
0457 u8 status_byte = 0;
0458
0459
0460 for (;;) {
0461 if (time_after(jiffies, timeout))
0462 return 0;
0463 rc = tuner_i2c_xfer_recv(&priv->i2c_props,
0464 &status_byte, 1);
0465 if (1 != rc) {
0466 tuner_warn("i2c i/o read error: rc == %d (should be 1)\n",
0467 rc);
0468 break;
0469 }
0470 if (status_byte & TUNER_PLL_LOCKED)
0471 break;
0472 udelay(10);
0473 }
0474
0475
0476 config &= ~TUNER_CHARGE_PUMP;
0477 buffer[0] = (div>>8) & 0x7f;
0478 buffer[1] = div & 0xff;
0479 buffer[2] = config;
0480 buffer[3] = cb;
0481 tuner_dbg("tv 0x%02x 0x%02x 0x%02x 0x%02x\n",
0482 buffer[0], buffer[1], buffer[2], buffer[3]);
0483
0484 rc = tuner_i2c_xfer_send(&priv->i2c_props, buffer, 4);
0485 if (4 != rc)
0486 tuner_warn("i2c i/o error: rc == %d (should be 4)\n",
0487 rc);
0488 break;
0489 }
0490 }
0491
0492 return 0;
0493 }
0494
0495 static int simple_radio_bandswitch(struct dvb_frontend *fe, u8 *buffer)
0496 {
0497 struct tuner_simple_priv *priv = fe->tuner_priv;
0498
0499 switch (priv->type) {
0500 case TUNER_TENA_9533_DI:
0501 case TUNER_YMEC_TVF_5533MF:
0502 tuner_dbg("This tuner doesn't have FM. Most cards have a TEA5767 for FM\n");
0503 return -EINVAL;
0504 case TUNER_PHILIPS_FM1216ME_MK3:
0505 case TUNER_PHILIPS_FM1236_MK3:
0506 case TUNER_PHILIPS_FMD1216ME_MK3:
0507 case TUNER_PHILIPS_FMD1216MEX_MK3:
0508 case TUNER_LG_NTSC_TAPE:
0509 case TUNER_PHILIPS_FM1256_IH3:
0510 case TUNER_TCL_MF02GIP_5N:
0511 buffer[3] = 0x19;
0512 break;
0513 case TUNER_PHILIPS_FM1216MK5:
0514 buffer[2] = 0x88;
0515 buffer[3] = 0x09;
0516 break;
0517 case TUNER_TNF_5335MF:
0518 buffer[3] = 0x11;
0519 break;
0520 case TUNER_LG_PAL_FM:
0521 buffer[3] = 0xa5;
0522 break;
0523 case TUNER_THOMSON_DTT761X:
0524 buffer[3] = 0x39;
0525 break;
0526 case TUNER_PHILIPS_FQ1216LME_MK3:
0527 case TUNER_PHILIPS_FQ1236_MK5:
0528 tuner_err("This tuner doesn't have FM\n");
0529
0530 buffer[3] = 0x01;
0531 break;
0532 case TUNER_MICROTUNE_4049FM5:
0533 default:
0534 buffer[3] = 0xa4;
0535 break;
0536 }
0537
0538 return 0;
0539 }
0540
0541
0542
0543 static int simple_set_tv_freq(struct dvb_frontend *fe,
0544 struct analog_parameters *params)
0545 {
0546 struct tuner_simple_priv *priv = fe->tuner_priv;
0547 u8 config, cb;
0548 u16 div;
0549 u8 buffer[4];
0550 int rc, IFPCoff, i;
0551 enum param_type desired_type;
0552 struct tuner_params *t_params;
0553
0554
0555
0556
0557
0558
0559
0560
0561
0562
0563
0564
0565
0566 if (params->std == V4L2_STD_NTSC_M_JP) {
0567 IFPCoff = 940;
0568 desired_type = TUNER_PARAM_TYPE_NTSC;
0569 } else if ((params->std & V4L2_STD_MN) &&
0570 !(params->std & ~V4L2_STD_MN)) {
0571 IFPCoff = 732;
0572 desired_type = TUNER_PARAM_TYPE_NTSC;
0573 } else if (params->std == V4L2_STD_SECAM_LC) {
0574 IFPCoff = 543;
0575 desired_type = TUNER_PARAM_TYPE_SECAM;
0576 } else {
0577 IFPCoff = 623;
0578 desired_type = TUNER_PARAM_TYPE_PAL;
0579 }
0580
0581 t_params = simple_tuner_params(fe, desired_type);
0582
0583 i = simple_config_lookup(fe, t_params, ¶ms->frequency,
0584 &config, &cb);
0585
0586 div = params->frequency + IFPCoff + offset;
0587
0588 tuner_dbg("Freq= %d.%02d MHz, V_IF=%d.%02d MHz, Offset=%d.%02d MHz, div=%0d\n",
0589 params->frequency / 16, params->frequency % 16 * 100 / 16,
0590 IFPCoff / 16, IFPCoff % 16 * 100 / 16,
0591 offset / 16, offset % 16 * 100 / 16, div);
0592
0593
0594 simple_std_setup(fe, params, &config, &cb);
0595
0596 if (t_params->cb_first_if_lower_freq && div < priv->last_div) {
0597 buffer[0] = config;
0598 buffer[1] = cb;
0599 buffer[2] = (div>>8) & 0x7f;
0600 buffer[3] = div & 0xff;
0601 } else {
0602 buffer[0] = (div>>8) & 0x7f;
0603 buffer[1] = div & 0xff;
0604 buffer[2] = config;
0605 buffer[3] = cb;
0606 }
0607 priv->last_div = div;
0608 if (t_params->has_tda9887) {
0609 struct v4l2_priv_tun_config tda9887_cfg;
0610 int tda_config = 0;
0611 int is_secam_l = (params->std & (V4L2_STD_SECAM_L |
0612 V4L2_STD_SECAM_LC)) &&
0613 !(params->std & ~(V4L2_STD_SECAM_L |
0614 V4L2_STD_SECAM_LC));
0615
0616 tda9887_cfg.tuner = TUNER_TDA9887;
0617 tda9887_cfg.priv = &tda_config;
0618
0619 if (params->std == V4L2_STD_SECAM_LC) {
0620 if (t_params->port1_active ^ t_params->port1_invert_for_secam_lc)
0621 tda_config |= TDA9887_PORT1_ACTIVE;
0622 if (t_params->port2_active ^ t_params->port2_invert_for_secam_lc)
0623 tda_config |= TDA9887_PORT2_ACTIVE;
0624 } else {
0625 if (t_params->port1_active)
0626 tda_config |= TDA9887_PORT1_ACTIVE;
0627 if (t_params->port2_active)
0628 tda_config |= TDA9887_PORT2_ACTIVE;
0629 }
0630 if (t_params->intercarrier_mode)
0631 tda_config |= TDA9887_INTERCARRIER;
0632 if (is_secam_l) {
0633 if (i == 0 && t_params->default_top_secam_low)
0634 tda_config |= TDA9887_TOP(t_params->default_top_secam_low);
0635 else if (i == 1 && t_params->default_top_secam_mid)
0636 tda_config |= TDA9887_TOP(t_params->default_top_secam_mid);
0637 else if (t_params->default_top_secam_high)
0638 tda_config |= TDA9887_TOP(t_params->default_top_secam_high);
0639 } else {
0640 if (i == 0 && t_params->default_top_low)
0641 tda_config |= TDA9887_TOP(t_params->default_top_low);
0642 else if (i == 1 && t_params->default_top_mid)
0643 tda_config |= TDA9887_TOP(t_params->default_top_mid);
0644 else if (t_params->default_top_high)
0645 tda_config |= TDA9887_TOP(t_params->default_top_high);
0646 }
0647 if (t_params->default_pll_gating_18)
0648 tda_config |= TDA9887_GATING_18;
0649 i2c_clients_command(priv->i2c_props.adap, TUNER_SET_CONFIG,
0650 &tda9887_cfg);
0651 }
0652 tuner_dbg("tv 0x%02x 0x%02x 0x%02x 0x%02x\n",
0653 buffer[0], buffer[1], buffer[2], buffer[3]);
0654
0655 rc = tuner_i2c_xfer_send(&priv->i2c_props, buffer, 4);
0656 if (4 != rc)
0657 tuner_warn("i2c i/o error: rc == %d (should be 4)\n", rc);
0658
0659 simple_post_tune(fe, &buffer[0], div, config, cb);
0660
0661 return 0;
0662 }
0663
0664 static int simple_set_radio_freq(struct dvb_frontend *fe,
0665 struct analog_parameters *params)
0666 {
0667 struct tunertype *tun;
0668 struct tuner_simple_priv *priv = fe->tuner_priv;
0669 u8 buffer[4];
0670 u16 div;
0671 int rc, j;
0672 struct tuner_params *t_params;
0673 unsigned int freq = params->frequency;
0674 bool mono = params->audmode == V4L2_TUNER_MODE_MONO;
0675
0676 tun = priv->tun;
0677
0678 for (j = tun->count-1; j > 0; j--)
0679 if (tun->params[j].type == TUNER_PARAM_TYPE_RADIO)
0680 break;
0681
0682 t_params = &tun->params[j];
0683
0684
0685 switch (t_params->radio_if) {
0686 case 0:
0687 freq += (unsigned int)(10.7*16000);
0688 break;
0689 case 1:
0690 freq += (unsigned int)(33.3*16000);
0691 break;
0692 case 2:
0693 freq += (unsigned int)(41.3*16000);
0694 break;
0695 default:
0696 tuner_warn("Unsupported radio_if value %d\n",
0697 t_params->radio_if);
0698 return 0;
0699 }
0700
0701 buffer[2] = (t_params->ranges[0].config & ~TUNER_RATIO_MASK) |
0702 TUNER_RATIO_SELECT_50;
0703
0704
0705 if (simple_radio_bandswitch(fe, &buffer[0]))
0706 return 0;
0707
0708
0709
0710
0711 div = (freq + 400) / 800;
0712
0713 if (t_params->cb_first_if_lower_freq && div < priv->last_div) {
0714 buffer[0] = buffer[2];
0715 buffer[1] = buffer[3];
0716 buffer[2] = (div>>8) & 0x7f;
0717 buffer[3] = div & 0xff;
0718 } else {
0719 buffer[0] = (div>>8) & 0x7f;
0720 buffer[1] = div & 0xff;
0721 }
0722
0723 tuner_dbg("radio 0x%02x 0x%02x 0x%02x 0x%02x\n",
0724 buffer[0], buffer[1], buffer[2], buffer[3]);
0725 priv->last_div = div;
0726
0727 if (t_params->has_tda9887) {
0728 int config = 0;
0729 struct v4l2_priv_tun_config tda9887_cfg;
0730
0731 tda9887_cfg.tuner = TUNER_TDA9887;
0732 tda9887_cfg.priv = &config;
0733
0734 if (t_params->port1_active &&
0735 !t_params->port1_fm_high_sensitivity)
0736 config |= TDA9887_PORT1_ACTIVE;
0737 if (t_params->port2_active &&
0738 !t_params->port2_fm_high_sensitivity)
0739 config |= TDA9887_PORT2_ACTIVE;
0740 if (t_params->intercarrier_mode)
0741 config |= TDA9887_INTERCARRIER;
0742 if (t_params->port1_set_for_fm_mono && mono)
0743 config &= ~TDA9887_PORT1_ACTIVE;
0744 if (t_params->fm_gain_normal)
0745 config |= TDA9887_GAIN_NORMAL;
0746 if (t_params->radio_if == 2)
0747 config |= TDA9887_RIF_41_3;
0748 i2c_clients_command(priv->i2c_props.adap, TUNER_SET_CONFIG,
0749 &tda9887_cfg);
0750 }
0751 rc = tuner_i2c_xfer_send(&priv->i2c_props, buffer, 4);
0752 if (4 != rc)
0753 tuner_warn("i2c i/o error: rc == %d (should be 4)\n", rc);
0754
0755
0756 switch (priv->type) {
0757 case TUNER_PHILIPS_FM1216ME_MK3:
0758 buffer[2] = 0x98;
0759 buffer[3] = 0x20;
0760 rc = tuner_i2c_xfer_send(&priv->i2c_props, buffer, 4);
0761 if (4 != rc)
0762 tuner_warn("i2c i/o error: rc == %d (should be 4)\n", rc);
0763 break;
0764 }
0765
0766 return 0;
0767 }
0768
0769 static int simple_set_params(struct dvb_frontend *fe,
0770 struct analog_parameters *params)
0771 {
0772 struct tuner_simple_priv *priv = fe->tuner_priv;
0773 int ret = -EINVAL;
0774
0775 if (priv->i2c_props.adap == NULL)
0776 return -EINVAL;
0777
0778 switch (params->mode) {
0779 case V4L2_TUNER_RADIO:
0780 priv->radio_mode = true;
0781 ret = simple_set_radio_freq(fe, params);
0782 priv->frequency = params->frequency * 125 / 2;
0783 break;
0784 case V4L2_TUNER_ANALOG_TV:
0785 case V4L2_TUNER_DIGITAL_TV:
0786 priv->radio_mode = false;
0787 ret = simple_set_tv_freq(fe, params);
0788 priv->frequency = params->frequency * 62500;
0789 break;
0790 }
0791 priv->bandwidth = 0;
0792
0793 return ret;
0794 }
0795
0796 static void simple_set_dvb(struct dvb_frontend *fe, u8 *buf,
0797 const u32 delsys,
0798 const u32 frequency,
0799 const u32 bandwidth)
0800 {
0801 struct tuner_simple_priv *priv = fe->tuner_priv;
0802
0803 switch (priv->type) {
0804 case TUNER_PHILIPS_FMD1216ME_MK3:
0805 case TUNER_PHILIPS_FMD1216MEX_MK3:
0806 if (bandwidth == 8000000 &&
0807 frequency >= 158870000)
0808 buf[3] |= 0x08;
0809 break;
0810 case TUNER_PHILIPS_TD1316:
0811
0812 buf[3] |= (frequency < 161000000) ? 1 :
0813 (frequency < 444000000) ? 2 : 4;
0814
0815
0816 if (bandwidth == 8000000)
0817 buf[3] |= 1 << 3;
0818 break;
0819 case TUNER_PHILIPS_TUV1236D:
0820 case TUNER_PHILIPS_FCV1236D:
0821 {
0822 unsigned int new_rf;
0823
0824 if (dtv_input[priv->nr])
0825 new_rf = dtv_input[priv->nr];
0826 else
0827 switch (delsys) {
0828 case SYS_DVBC_ANNEX_B:
0829 new_rf = 1;
0830 break;
0831 case SYS_ATSC:
0832 default:
0833 new_rf = 0;
0834 break;
0835 }
0836 simple_set_rf_input(fe, &buf[2], &buf[3], new_rf);
0837 break;
0838 }
0839 default:
0840 break;
0841 }
0842 }
0843
0844 static u32 simple_dvb_configure(struct dvb_frontend *fe, u8 *buf,
0845 const u32 delsys,
0846 const u32 freq,
0847 const u32 bw)
0848 {
0849
0850 struct tuner_simple_priv *priv = fe->tuner_priv;
0851 struct tunertype *tun = priv->tun;
0852 struct tuner_params *t_params;
0853 u8 config, cb;
0854 u32 div;
0855 int ret;
0856 u32 frequency = freq / 62500;
0857
0858 if (!tun->stepsize) {
0859
0860
0861 tuner_err("attempt to treat tuner %d (%s) as digital tuner without stepsize defined.\n",
0862 priv->type, priv->tun->name);
0863 return 0;
0864 }
0865
0866 t_params = simple_tuner_params(fe, TUNER_PARAM_TYPE_DIGITAL);
0867 ret = simple_config_lookup(fe, t_params, &frequency, &config, &cb);
0868 if (ret < 0)
0869 return 0;
0870
0871 div = ((frequency + t_params->iffreq) * 62500 + offset +
0872 tun->stepsize/2) / tun->stepsize;
0873
0874 buf[0] = div >> 8;
0875 buf[1] = div & 0xff;
0876 buf[2] = config;
0877 buf[3] = cb;
0878
0879 simple_set_dvb(fe, buf, delsys, freq, bw);
0880
0881 tuner_dbg("%s: div=%d | buf=0x%02x,0x%02x,0x%02x,0x%02x\n",
0882 tun->name, div, buf[0], buf[1], buf[2], buf[3]);
0883
0884
0885 return (div * tun->stepsize) - t_params->iffreq;
0886 }
0887
0888 static int simple_dvb_calc_regs(struct dvb_frontend *fe,
0889 u8 *buf, int buf_len)
0890 {
0891 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
0892 u32 delsys = c->delivery_system;
0893 u32 bw = c->bandwidth_hz;
0894 struct tuner_simple_priv *priv = fe->tuner_priv;
0895 u32 frequency;
0896
0897 if (buf_len < 5)
0898 return -EINVAL;
0899
0900 frequency = simple_dvb_configure(fe, buf+1, delsys, c->frequency, bw);
0901 if (frequency == 0)
0902 return -EINVAL;
0903
0904 buf[0] = priv->i2c_props.addr;
0905
0906 priv->frequency = frequency;
0907 priv->bandwidth = c->bandwidth_hz;
0908
0909 return 5;
0910 }
0911
0912 static int simple_dvb_set_params(struct dvb_frontend *fe)
0913 {
0914 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
0915 u32 delsys = c->delivery_system;
0916 u32 bw = c->bandwidth_hz;
0917 u32 freq = c->frequency;
0918 struct tuner_simple_priv *priv = fe->tuner_priv;
0919 u32 frequency;
0920 u32 prev_freq, prev_bw;
0921 int ret;
0922 u8 buf[5];
0923
0924 if (priv->i2c_props.adap == NULL)
0925 return -EINVAL;
0926
0927 prev_freq = priv->frequency;
0928 prev_bw = priv->bandwidth;
0929
0930 frequency = simple_dvb_configure(fe, buf+1, delsys, freq, bw);
0931 if (frequency == 0)
0932 return -EINVAL;
0933
0934 buf[0] = priv->i2c_props.addr;
0935
0936 priv->frequency = frequency;
0937 priv->bandwidth = bw;
0938
0939
0940 if (fe->ops.analog_ops.standby)
0941 fe->ops.analog_ops.standby(fe);
0942
0943 if (fe->ops.i2c_gate_ctrl)
0944 fe->ops.i2c_gate_ctrl(fe, 1);
0945
0946
0947
0948 ret = tuner_i2c_xfer_send(&priv->i2c_props, buf+1, 4);
0949 if (ret != 4)
0950 goto fail;
0951
0952 return 0;
0953 fail:
0954
0955 priv->frequency = prev_freq;
0956 priv->bandwidth = prev_bw;
0957
0958 return ret;
0959 }
0960
0961 static int simple_init(struct dvb_frontend *fe)
0962 {
0963 struct tuner_simple_priv *priv = fe->tuner_priv;
0964
0965 if (priv->i2c_props.adap == NULL)
0966 return -EINVAL;
0967
0968 if (priv->tun->initdata) {
0969 int ret;
0970
0971 if (fe->ops.i2c_gate_ctrl)
0972 fe->ops.i2c_gate_ctrl(fe, 1);
0973
0974 ret = tuner_i2c_xfer_send(&priv->i2c_props,
0975 priv->tun->initdata + 1,
0976 priv->tun->initdata[0]);
0977 if (ret != priv->tun->initdata[0])
0978 return ret;
0979 }
0980
0981 return 0;
0982 }
0983
0984 static int simple_sleep(struct dvb_frontend *fe)
0985 {
0986 struct tuner_simple_priv *priv = fe->tuner_priv;
0987
0988 if (priv->i2c_props.adap == NULL)
0989 return -EINVAL;
0990
0991 if (priv->tun->sleepdata) {
0992 int ret;
0993
0994 if (fe->ops.i2c_gate_ctrl)
0995 fe->ops.i2c_gate_ctrl(fe, 1);
0996
0997 ret = tuner_i2c_xfer_send(&priv->i2c_props,
0998 priv->tun->sleepdata + 1,
0999 priv->tun->sleepdata[0]);
1000 if (ret != priv->tun->sleepdata[0])
1001 return ret;
1002 }
1003
1004 return 0;
1005 }
1006
1007 static void simple_release(struct dvb_frontend *fe)
1008 {
1009 struct tuner_simple_priv *priv = fe->tuner_priv;
1010
1011 mutex_lock(&tuner_simple_list_mutex);
1012
1013 if (priv)
1014 hybrid_tuner_release_state(priv);
1015
1016 mutex_unlock(&tuner_simple_list_mutex);
1017
1018 fe->tuner_priv = NULL;
1019 }
1020
1021 static int simple_get_frequency(struct dvb_frontend *fe, u32 *frequency)
1022 {
1023 struct tuner_simple_priv *priv = fe->tuner_priv;
1024 *frequency = priv->frequency;
1025 return 0;
1026 }
1027
1028 static int simple_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
1029 {
1030 struct tuner_simple_priv *priv = fe->tuner_priv;
1031 *bandwidth = priv->bandwidth;
1032 return 0;
1033 }
1034
1035 static const struct dvb_tuner_ops simple_tuner_ops = {
1036 .init = simple_init,
1037 .sleep = simple_sleep,
1038 .set_analog_params = simple_set_params,
1039 .set_params = simple_dvb_set_params,
1040 .calc_regs = simple_dvb_calc_regs,
1041 .release = simple_release,
1042 .get_frequency = simple_get_frequency,
1043 .get_bandwidth = simple_get_bandwidth,
1044 .get_status = simple_get_status,
1045 .get_rf_strength = simple_get_rf_strength,
1046 };
1047
1048 struct dvb_frontend *simple_tuner_attach(struct dvb_frontend *fe,
1049 struct i2c_adapter *i2c_adap,
1050 u8 i2c_addr,
1051 unsigned int type)
1052 {
1053 struct tuner_simple_priv *priv = NULL;
1054 int instance;
1055
1056 if (type >= tuner_count) {
1057 printk(KERN_WARNING "%s: invalid tuner type: %d (max: %d)\n",
1058 __func__, type, tuner_count-1);
1059 return NULL;
1060 }
1061
1062
1063
1064
1065
1066 if (i2c_adap != NULL) {
1067 u8 b[1];
1068 struct i2c_msg msg = {
1069 .addr = i2c_addr, .flags = I2C_M_RD,
1070 .buf = b, .len = 1,
1071 };
1072
1073 if (fe->ops.i2c_gate_ctrl)
1074 fe->ops.i2c_gate_ctrl(fe, 1);
1075
1076 if (1 != i2c_transfer(i2c_adap, &msg, 1))
1077 printk(KERN_WARNING "tuner-simple %d-%04x: unable to probe %s, proceeding anyway.",
1078 i2c_adapter_id(i2c_adap), i2c_addr,
1079 tuners[type].name);
1080
1081 if (fe->ops.i2c_gate_ctrl)
1082 fe->ops.i2c_gate_ctrl(fe, 0);
1083 }
1084
1085 mutex_lock(&tuner_simple_list_mutex);
1086
1087 instance = hybrid_tuner_request_state(struct tuner_simple_priv, priv,
1088 hybrid_tuner_instance_list,
1089 i2c_adap, i2c_addr,
1090 "tuner-simple");
1091 switch (instance) {
1092 case 0:
1093 mutex_unlock(&tuner_simple_list_mutex);
1094 return NULL;
1095 case 1:
1096 fe->tuner_priv = priv;
1097
1098 priv->type = type;
1099 priv->tun = &tuners[type];
1100 priv->nr = simple_devcount++;
1101 break;
1102 default:
1103 fe->tuner_priv = priv;
1104 break;
1105 }
1106
1107 mutex_unlock(&tuner_simple_list_mutex);
1108
1109 memcpy(&fe->ops.tuner_ops, &simple_tuner_ops,
1110 sizeof(struct dvb_tuner_ops));
1111
1112 if (type != priv->type)
1113 tuner_warn("couldn't set type to %d. Using %d (%s) instead\n",
1114 type, priv->type, priv->tun->name);
1115 else
1116 tuner_info("type set to %d (%s)\n",
1117 priv->type, priv->tun->name);
1118
1119 if ((debug) || ((atv_input[priv->nr] > 0) ||
1120 (dtv_input[priv->nr] > 0))) {
1121 if (0 == atv_input[priv->nr])
1122 tuner_info("tuner %d atv rf input will be autoselected\n",
1123 priv->nr);
1124 else
1125 tuner_info("tuner %d atv rf input will be set to input %d (insmod option)\n",
1126 priv->nr, atv_input[priv->nr]);
1127 if (0 == dtv_input[priv->nr])
1128 tuner_info("tuner %d dtv rf input will be autoselected\n",
1129 priv->nr);
1130 else
1131 tuner_info("tuner %d dtv rf input will be set to input %d (insmod option)\n",
1132 priv->nr, dtv_input[priv->nr]);
1133 }
1134
1135 strscpy(fe->ops.tuner_ops.info.name, priv->tun->name,
1136 sizeof(fe->ops.tuner_ops.info.name));
1137
1138 return fe;
1139 }
1140 EXPORT_SYMBOL_GPL(simple_tuner_attach);
1141
1142 MODULE_DESCRIPTION("Simple 4-control-bytes style tuner driver");
1143 MODULE_AUTHOR("Ralph Metzler, Gerd Knorr, Gunther Mayer");
1144 MODULE_LICENSE("GPL");