Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * descriptions + helper functions for simple dvb plls.
0004  *
0005  * (c) 2004 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
0006  */
0007 
0008 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0009 
0010 #include <linux/slab.h>
0011 #include <linux/module.h>
0012 #include <linux/idr.h>
0013 #include <linux/dvb/frontend.h>
0014 #include <asm/types.h>
0015 
0016 #include "dvb-pll.h"
0017 
0018 #define dprintk(fmt, arg...) \
0019     printk(KERN_DEBUG pr_fmt("%s: " fmt), __func__, ##arg)
0020 
0021 struct dvb_pll_priv {
0022     /* pll number */
0023     int nr;
0024 
0025     /* i2c details */
0026     int pll_i2c_address;
0027     struct i2c_adapter *i2c;
0028 
0029     /* the PLL descriptor */
0030     const struct dvb_pll_desc *pll_desc;
0031 
0032     /* cached frequency/bandwidth */
0033     u32 frequency;
0034     u32 bandwidth;
0035 };
0036 
0037 #define DVB_PLL_MAX 64
0038 static DEFINE_IDA(pll_ida);
0039 
0040 static int debug;
0041 module_param(debug, int, 0644);
0042 MODULE_PARM_DESC(debug, "enable verbose debug messages");
0043 
0044 static unsigned int id[DVB_PLL_MAX] =
0045     { [ 0 ... (DVB_PLL_MAX-1) ] = DVB_PLL_UNDEFINED };
0046 module_param_array(id, int, NULL, 0644);
0047 MODULE_PARM_DESC(id, "force pll id to use (DEBUG ONLY)");
0048 
0049 /* ----------------------------------------------------------- */
0050 
0051 struct dvb_pll_desc {
0052     const char *name;
0053     u32  min;
0054     u32  max;
0055     u32  iffreq;
0056     void (*set)(struct dvb_frontend *fe, u8 *buf);
0057     u8   *initdata;
0058     u8   *initdata2;
0059     u8   *sleepdata;
0060     int  count;
0061     struct {
0062         u32 limit;
0063         u32 stepsize;
0064         u8  config;
0065         u8  cb;
0066     } entries[];
0067 };
0068 
0069 /* ----------------------------------------------------------- */
0070 /* descriptions                                                */
0071 
0072 static const struct dvb_pll_desc dvb_pll_thomson_dtt7579 = {
0073     .name  = "Thomson dtt7579",
0074     .min   = 177 * MHz,
0075     .max   = 858 * MHz,
0076     .iffreq= 36166667,
0077     .sleepdata = (u8[]){ 2, 0xb4, 0x03 },
0078     .count = 4,
0079     .entries = {
0080         {  443250000, 166667, 0xb4, 0x02 },
0081         {  542000000, 166667, 0xb4, 0x08 },
0082         {  771000000, 166667, 0xbc, 0x08 },
0083         {  999999999, 166667, 0xf4, 0x08 },
0084     },
0085 };
0086 
0087 static void thomson_dtt759x_bw(struct dvb_frontend *fe, u8 *buf)
0088 {
0089     u32 bw = fe->dtv_property_cache.bandwidth_hz;
0090     if (bw == 7000000)
0091         buf[3] |= 0x10;
0092 }
0093 
0094 static const struct dvb_pll_desc dvb_pll_thomson_dtt759x = {
0095     .name  = "Thomson dtt759x",
0096     .min   = 177 * MHz,
0097     .max   = 896 * MHz,
0098     .set   = thomson_dtt759x_bw,
0099     .iffreq= 36166667,
0100     .sleepdata = (u8[]){ 2, 0x84, 0x03 },
0101     .count = 5,
0102     .entries = {
0103         {  264000000, 166667, 0xb4, 0x02 },
0104         {  470000000, 166667, 0xbc, 0x02 },
0105         {  735000000, 166667, 0xbc, 0x08 },
0106         {  835000000, 166667, 0xf4, 0x08 },
0107         {  999999999, 166667, 0xfc, 0x08 },
0108     },
0109 };
0110 
0111 static void thomson_dtt7520x_bw(struct dvb_frontend *fe, u8 *buf)
0112 {
0113     u32 bw = fe->dtv_property_cache.bandwidth_hz;
0114     if (bw == 8000000)
0115         buf[3] ^= 0x10;
0116 }
0117 
0118 static const struct dvb_pll_desc dvb_pll_thomson_dtt7520x = {
0119     .name  = "Thomson dtt7520x",
0120     .min   = 185 * MHz,
0121     .max   = 900 * MHz,
0122     .set   = thomson_dtt7520x_bw,
0123     .iffreq = 36166667,
0124     .count = 7,
0125     .entries = {
0126         {  305000000, 166667, 0xb4, 0x12 },
0127         {  405000000, 166667, 0xbc, 0x12 },
0128         {  445000000, 166667, 0xbc, 0x12 },
0129         {  465000000, 166667, 0xf4, 0x18 },
0130         {  735000000, 166667, 0xfc, 0x18 },
0131         {  835000000, 166667, 0xbc, 0x18 },
0132         {  999999999, 166667, 0xfc, 0x18 },
0133     },
0134 };
0135 
0136 static const struct dvb_pll_desc dvb_pll_lg_z201 = {
0137     .name  = "LG z201",
0138     .min   = 174 * MHz,
0139     .max   = 862 * MHz,
0140     .iffreq= 36166667,
0141     .sleepdata = (u8[]){ 2, 0xbc, 0x03 },
0142     .count = 5,
0143     .entries = {
0144         {  157500000, 166667, 0xbc, 0x01 },
0145         {  443250000, 166667, 0xbc, 0x02 },
0146         {  542000000, 166667, 0xbc, 0x04 },
0147         {  830000000, 166667, 0xf4, 0x04 },
0148         {  999999999, 166667, 0xfc, 0x04 },
0149     },
0150 };
0151 
0152 static const struct dvb_pll_desc dvb_pll_unknown_1 = {
0153     .name  = "unknown 1", /* used by dntv live dvb-t */
0154     .min   = 174 * MHz,
0155     .max   = 862 * MHz,
0156     .iffreq= 36166667,
0157     .count = 9,
0158     .entries = {
0159         {  150000000, 166667, 0xb4, 0x01 },
0160         {  173000000, 166667, 0xbc, 0x01 },
0161         {  250000000, 166667, 0xb4, 0x02 },
0162         {  400000000, 166667, 0xbc, 0x02 },
0163         {  420000000, 166667, 0xf4, 0x02 },
0164         {  470000000, 166667, 0xfc, 0x02 },
0165         {  600000000, 166667, 0xbc, 0x08 },
0166         {  730000000, 166667, 0xf4, 0x08 },
0167         {  999999999, 166667, 0xfc, 0x08 },
0168     },
0169 };
0170 
0171 /* Infineon TUA6010XS
0172  * used in Thomson Cable Tuner
0173  */
0174 static const struct dvb_pll_desc dvb_pll_tua6010xs = {
0175     .name  = "Infineon TUA6010XS",
0176     .min   = 44250 * kHz,
0177     .max   = 858 * MHz,
0178     .iffreq= 36125000,
0179     .count = 3,
0180     .entries = {
0181         {  115750000, 62500, 0x8e, 0x03 },
0182         {  403250000, 62500, 0x8e, 0x06 },
0183         {  999999999, 62500, 0x8e, 0x85 },
0184     },
0185 };
0186 
0187 /* Panasonic env57h1xd5 (some Philips PLL ?) */
0188 static const struct dvb_pll_desc dvb_pll_env57h1xd5 = {
0189     .name  = "Panasonic ENV57H1XD5",
0190     .min   = 44250 * kHz,
0191     .max   = 858 * MHz,
0192     .iffreq= 36125000,
0193     .count = 4,
0194     .entries = {
0195         {  153000000, 166667, 0xc2, 0x41 },
0196         {  470000000, 166667, 0xc2, 0x42 },
0197         {  526000000, 166667, 0xc2, 0x84 },
0198         {  999999999, 166667, 0xc2, 0xa4 },
0199     },
0200 };
0201 
0202 /* Philips TDA6650/TDA6651
0203  * used in Panasonic ENV77H11D5
0204  */
0205 static void tda665x_bw(struct dvb_frontend *fe, u8 *buf)
0206 {
0207     u32 bw = fe->dtv_property_cache.bandwidth_hz;
0208     if (bw == 8000000)
0209         buf[3] |= 0x08;
0210 }
0211 
0212 static const struct dvb_pll_desc dvb_pll_tda665x = {
0213     .name  = "Philips TDA6650/TDA6651",
0214     .min   = 44250 * kHz,
0215     .max   = 858 * MHz,
0216     .set   = tda665x_bw,
0217     .iffreq= 36166667,
0218     .initdata = (u8[]){ 4, 0x0b, 0xf5, 0x85, 0xab },
0219     .count = 12,
0220     .entries = {
0221         {   93834000, 166667, 0xca, 0x61 /* 011 0 0 0  01 */ },
0222         {  123834000, 166667, 0xca, 0xa1 /* 101 0 0 0  01 */ },
0223         {  161000000, 166667, 0xca, 0xa1 /* 101 0 0 0  01 */ },
0224         {  163834000, 166667, 0xca, 0xc2 /* 110 0 0 0  10 */ },
0225         {  253834000, 166667, 0xca, 0x62 /* 011 0 0 0  10 */ },
0226         {  383834000, 166667, 0xca, 0xa2 /* 101 0 0 0  10 */ },
0227         {  443834000, 166667, 0xca, 0xc2 /* 110 0 0 0  10 */ },
0228         {  444000000, 166667, 0xca, 0xc4 /* 110 0 0 1  00 */ },
0229         {  583834000, 166667, 0xca, 0x64 /* 011 0 0 1  00 */ },
0230         {  793834000, 166667, 0xca, 0xa4 /* 101 0 0 1  00 */ },
0231         {  444834000, 166667, 0xca, 0xc4 /* 110 0 0 1  00 */ },
0232         {  861000000, 166667, 0xca, 0xe4 /* 111 0 0 1  00 */ },
0233     }
0234 };
0235 
0236 /* Infineon TUA6034
0237  * used in LG TDTP E102P
0238  */
0239 static void tua6034_bw(struct dvb_frontend *fe, u8 *buf)
0240 {
0241     u32 bw = fe->dtv_property_cache.bandwidth_hz;
0242     if (bw == 7000000)
0243         buf[3] |= 0x08;
0244 }
0245 
0246 static const struct dvb_pll_desc dvb_pll_tua6034 = {
0247     .name  = "Infineon TUA6034",
0248     .min   = 44250 * kHz,
0249     .max   = 858 * MHz,
0250     .iffreq= 36166667,
0251     .count = 3,
0252     .set   = tua6034_bw,
0253     .entries = {
0254         {  174500000, 62500, 0xce, 0x01 },
0255         {  230000000, 62500, 0xce, 0x02 },
0256         {  999999999, 62500, 0xce, 0x04 },
0257     },
0258 };
0259 
0260 /* ALPS TDED4
0261  * used in Nebula-Cards and USB boxes
0262  */
0263 static void tded4_bw(struct dvb_frontend *fe, u8 *buf)
0264 {
0265     u32 bw = fe->dtv_property_cache.bandwidth_hz;
0266     if (bw == 8000000)
0267         buf[3] |= 0x04;
0268 }
0269 
0270 static const struct dvb_pll_desc dvb_pll_tded4 = {
0271     .name = "ALPS TDED4",
0272     .min =  47 * MHz,
0273     .max = 863 * MHz,
0274     .iffreq= 36166667,
0275     .set   = tded4_bw,
0276     .count = 4,
0277     .entries = {
0278         { 153000000, 166667, 0x85, 0x01 },
0279         { 470000000, 166667, 0x85, 0x02 },
0280         { 823000000, 166667, 0x85, 0x08 },
0281         { 999999999, 166667, 0x85, 0x88 },
0282     }
0283 };
0284 
0285 /* ALPS TDHU2
0286  * used in AverTVHD MCE A180
0287  */
0288 static const struct dvb_pll_desc dvb_pll_tdhu2 = {
0289     .name = "ALPS TDHU2",
0290     .min =  54 * MHz,
0291     .max = 864 * MHz,
0292     .iffreq= 44000000,
0293     .count = 4,
0294     .entries = {
0295         { 162000000, 62500, 0x85, 0x01 },
0296         { 426000000, 62500, 0x85, 0x02 },
0297         { 782000000, 62500, 0x85, 0x08 },
0298         { 999999999, 62500, 0x85, 0x88 },
0299     }
0300 };
0301 
0302 /* Samsung TBMV30111IN / TBMV30712IN1
0303  * used in Air2PC ATSC - 2nd generation (nxt2002)
0304  */
0305 static const struct dvb_pll_desc dvb_pll_samsung_tbmv = {
0306     .name = "Samsung TBMV30111IN / TBMV30712IN1",
0307     .min =  54 * MHz,
0308     .max = 860 * MHz,
0309     .iffreq= 44000000,
0310     .count = 6,
0311     .entries = {
0312         { 172000000, 166667, 0xb4, 0x01 },
0313         { 214000000, 166667, 0xb4, 0x02 },
0314         { 467000000, 166667, 0xbc, 0x02 },
0315         { 721000000, 166667, 0xbc, 0x08 },
0316         { 841000000, 166667, 0xf4, 0x08 },
0317         { 999999999, 166667, 0xfc, 0x02 },
0318     }
0319 };
0320 
0321 /*
0322  * Philips SD1878 Tuner.
0323  */
0324 static const struct dvb_pll_desc dvb_pll_philips_sd1878_tda8261 = {
0325     .name  = "Philips SD1878",
0326     .min   =  950 * MHz,
0327     .max   = 2150 * MHz,
0328     .iffreq= 249, /* zero-IF, offset 249 is to round up */
0329     .count = 4,
0330     .entries = {
0331         { 1250000, 500, 0xc4, 0x00},
0332         { 1450000, 500, 0xc4, 0x40},
0333         { 2050000, 500, 0xc4, 0x80},
0334         { 2150000, 500, 0xc4, 0xc0},
0335     },
0336 };
0337 
0338 static void opera1_bw(struct dvb_frontend *fe, u8 *buf)
0339 {
0340     struct dtv_frontend_properties *c = &fe->dtv_property_cache;
0341     struct dvb_pll_priv *priv = fe->tuner_priv;
0342     u32 b_w  = (c->symbol_rate * 27) / 32000;
0343     struct i2c_msg msg = {
0344         .addr = priv->pll_i2c_address,
0345         .flags = 0,
0346         .buf = buf,
0347         .len = 4
0348     };
0349     int result;
0350     u8 lpf;
0351 
0352     if (fe->ops.i2c_gate_ctrl)
0353         fe->ops.i2c_gate_ctrl(fe, 1);
0354 
0355     result = i2c_transfer(priv->i2c, &msg, 1);
0356     if (result != 1)
0357         pr_err("%s: i2c_transfer failed:%d",
0358             __func__, result);
0359 
0360     if (b_w <= 10000)
0361         lpf = 0xc;
0362     else if (b_w <= 12000)
0363         lpf = 0x2;
0364     else if (b_w <= 14000)
0365         lpf = 0xa;
0366     else if (b_w <= 16000)
0367         lpf = 0x6;
0368     else if (b_w <= 18000)
0369         lpf = 0xe;
0370     else if (b_w <= 20000)
0371         lpf = 0x1;
0372     else if (b_w <= 22000)
0373         lpf = 0x9;
0374     else if (b_w <= 24000)
0375         lpf = 0x5;
0376     else if (b_w <= 26000)
0377         lpf = 0xd;
0378     else if (b_w <= 28000)
0379         lpf = 0x3;
0380         else
0381         lpf = 0xb;
0382     buf[2] ^= 0x1c; /* Flip bits 3-5 */
0383     /* Set lpf */
0384     buf[2] |= ((lpf >> 2) & 0x3) << 3;
0385     buf[3] |= (lpf & 0x3) << 2;
0386 
0387     return;
0388 }
0389 
0390 static const struct dvb_pll_desc dvb_pll_opera1 = {
0391     .name  = "Opera Tuner",
0392     .min   =  900 * MHz,
0393     .max   = 2250 * MHz,
0394     .initdata = (u8[]){ 4, 0x08, 0xe5, 0xe1, 0x00 },
0395     .initdata2 = (u8[]){ 4, 0x08, 0xe5, 0xe5, 0x00 },
0396     .iffreq= 0,
0397     .set   = opera1_bw,
0398     .count = 8,
0399     .entries = {
0400         { 1064000, 500, 0xf9, 0xc2 },
0401         { 1169000, 500, 0xf9, 0xe2 },
0402         { 1299000, 500, 0xf9, 0x20 },
0403         { 1444000, 500, 0xf9, 0x40 },
0404         { 1606000, 500, 0xf9, 0x60 },
0405         { 1777000, 500, 0xf9, 0x80 },
0406         { 1941000, 500, 0xf9, 0xa0 },
0407         { 2250000, 500, 0xf9, 0xc0 },
0408     }
0409 };
0410 
0411 static void samsung_dtos403ih102a_set(struct dvb_frontend *fe, u8 *buf)
0412 {
0413     struct dvb_pll_priv *priv = fe->tuner_priv;
0414     struct i2c_msg msg = {
0415         .addr = priv->pll_i2c_address,
0416         .flags = 0,
0417         .buf = buf,
0418         .len = 4
0419     };
0420     int result;
0421 
0422     if (fe->ops.i2c_gate_ctrl)
0423         fe->ops.i2c_gate_ctrl(fe, 1);
0424 
0425     result = i2c_transfer(priv->i2c, &msg, 1);
0426     if (result != 1)
0427         pr_err("%s: i2c_transfer failed:%d",
0428             __func__, result);
0429 
0430     buf[2] = 0x9e;
0431     buf[3] = 0x90;
0432 
0433     return;
0434 }
0435 
0436 /* unknown pll used in Samsung DTOS403IH102A DVB-C tuner */
0437 static const struct dvb_pll_desc dvb_pll_samsung_dtos403ih102a = {
0438     .name   = "Samsung DTOS403IH102A",
0439     .min    = 44250 * kHz,
0440     .max    = 858 * MHz,
0441     .iffreq =  36125000,
0442     .count  = 8,
0443     .set    = samsung_dtos403ih102a_set,
0444     .entries = {
0445         { 135000000, 62500, 0xbe, 0x01 },
0446         { 177000000, 62500, 0xf6, 0x01 },
0447         { 370000000, 62500, 0xbe, 0x02 },
0448         { 450000000, 62500, 0xf6, 0x02 },
0449         { 466000000, 62500, 0xfe, 0x02 },
0450         { 538000000, 62500, 0xbe, 0x08 },
0451         { 826000000, 62500, 0xf6, 0x08 },
0452         { 999999999, 62500, 0xfe, 0x08 },
0453     }
0454 };
0455 
0456 /* Samsung TDTC9251DH0 DVB-T NIM, as used on AirStar 2 */
0457 static const struct dvb_pll_desc dvb_pll_samsung_tdtc9251dh0 = {
0458     .name   = "Samsung TDTC9251DH0",
0459     .min    =  48 * MHz,
0460     .max    = 863 * MHz,
0461     .iffreq =  36166667,
0462     .count  = 3,
0463     .entries = {
0464         { 157500000, 166667, 0xcc, 0x09 },
0465         { 443000000, 166667, 0xcc, 0x0a },
0466         { 863000000, 166667, 0xcc, 0x08 },
0467     }
0468 };
0469 
0470 /* Samsung TBDU18132 DVB-S NIM with TSA5059 PLL, used in SkyStar2 DVB-S 2.3 */
0471 static const struct dvb_pll_desc dvb_pll_samsung_tbdu18132 = {
0472     .name = "Samsung TBDU18132",
0473     .min    =  950 * MHz,
0474     .max    = 2150 * MHz, /* guesses */
0475     .iffreq = 0,
0476     .count = 2,
0477     .entries = {
0478         { 1550000, 125, 0x84, 0x82 },
0479         { 4095937, 125, 0x84, 0x80 },
0480     }
0481     /* TSA5059 PLL has a 17 bit divisor rather than the 15 bits supported
0482      * by this driver.  The two extra bits are 0x60 in the third byte.  15
0483      * bits is enough for over 4 GHz, which is enough to cover the range
0484      * of this tuner.  We could use the additional divisor bits by adding
0485      * more entries, e.g.
0486      { 0x0ffff * 125 + 125/2, 125, 0x84 | 0x20, },
0487      { 0x17fff * 125 + 125/2, 125, 0x84 | 0x40, },
0488      { 0x1ffff * 125 + 125/2, 125, 0x84 | 0x60, }, */
0489 };
0490 
0491 /* Samsung TBMU24112 DVB-S NIM with SL1935 zero-IF tuner */
0492 static const struct dvb_pll_desc dvb_pll_samsung_tbmu24112 = {
0493     .name = "Samsung TBMU24112",
0494     .min    =  950 * MHz,
0495     .max    = 2150 * MHz, /* guesses */
0496     .iffreq = 0,
0497     .count = 2,
0498     .entries = {
0499         { 1500000, 125, 0x84, 0x18 },
0500         { 9999999, 125, 0x84, 0x08 },
0501     }
0502 };
0503 
0504 /* Alps TDEE4 DVB-C NIM, used on Cablestar 2 */
0505 /* byte 4 : 1  *   *   AGD R3  R2  R1  R0
0506  * byte 5 : C1 *   RE  RTS BS4 BS3 BS2 BS1
0507  * AGD = 1, R3 R2 R1 R0 = 0 1 0 1 => byte 4 = 1**10101 = 0x95
0508  * Range(MHz)  C1 *  RE RTS BS4 BS3 BS2 BS1  Byte 5
0509  *  47 - 153   0  *  0   0   0   0   0   1   0x01
0510  * 153 - 430   0  *  0   0   0   0   1   0   0x02
0511  * 430 - 822   0  *  0   0   1   0   0   0   0x08
0512  * 822 - 862   1  *  0   0   1   0   0   0   0x88 */
0513 static const struct dvb_pll_desc dvb_pll_alps_tdee4 = {
0514     .name = "ALPS TDEE4",
0515     .min    =  47 * MHz,
0516     .max    = 862 * MHz,
0517     .iffreq =  36125000,
0518     .count = 4,
0519     .entries = {
0520         { 153000000, 62500, 0x95, 0x01 },
0521         { 430000000, 62500, 0x95, 0x02 },
0522         { 822000000, 62500, 0x95, 0x08 },
0523         { 999999999, 62500, 0x95, 0x88 },
0524     }
0525 };
0526 
0527 /* Infineon TUA6034 ISDB-T, used in Friio */
0528 /* CP cur. 50uA, AGC takeover: 103dBuV, PORT3 on */
0529 static const struct dvb_pll_desc dvb_pll_tua6034_friio = {
0530     .name   = "Infineon TUA6034 ISDB-T (Friio)",
0531     .min    =  90 * MHz,
0532     .max    = 770 * MHz,
0533     .iffreq =  57000000,
0534     .initdata = (u8[]){ 4, 0x9a, 0x50, 0xb2, 0x08 },
0535     .sleepdata = (u8[]){ 4, 0x9a, 0x70, 0xb3, 0x0b },
0536     .count = 3,
0537     .entries = {
0538         { 170000000, 142857, 0xba, 0x09 },
0539         { 470000000, 142857, 0xba, 0x0a },
0540         { 770000000, 142857, 0xb2, 0x08 },
0541     }
0542 };
0543 
0544 /* Philips TDA6651 ISDB-T, used in Earthsoft PT1 */
0545 static const struct dvb_pll_desc dvb_pll_tda665x_earth_pt1 = {
0546     .name   = "Philips TDA6651 ISDB-T (EarthSoft PT1)",
0547     .min    =  90 * MHz,
0548     .max    = 770 * MHz,
0549     .iffreq =  57000000,
0550     .initdata = (u8[]){ 5, 0x0e, 0x7f, 0xc1, 0x80, 0x80 },
0551     .count = 10,
0552     .entries = {
0553         { 140000000, 142857, 0xc1, 0x81 },
0554         { 170000000, 142857, 0xc1, 0xa1 },
0555         { 220000000, 142857, 0xc1, 0x62 },
0556         { 330000000, 142857, 0xc1, 0xa2 },
0557         { 402000000, 142857, 0xc1, 0xe2 },
0558         { 450000000, 142857, 0xc1, 0x64 },
0559         { 550000000, 142857, 0xc1, 0x84 },
0560         { 600000000, 142857, 0xc1, 0xa4 },
0561         { 700000000, 142857, 0xc1, 0xc4 },
0562         { 770000000, 142857, 0xc1, 0xe4 },
0563     }
0564 };
0565 
0566 /* ----------------------------------------------------------- */
0567 
0568 static const struct dvb_pll_desc *pll_list[] = {
0569     [DVB_PLL_UNDEFINED]              = NULL,
0570     [DVB_PLL_THOMSON_DTT7579]        = &dvb_pll_thomson_dtt7579,
0571     [DVB_PLL_THOMSON_DTT759X]        = &dvb_pll_thomson_dtt759x,
0572     [DVB_PLL_THOMSON_DTT7520X]       = &dvb_pll_thomson_dtt7520x,
0573     [DVB_PLL_LG_Z201]                = &dvb_pll_lg_z201,
0574     [DVB_PLL_UNKNOWN_1]              = &dvb_pll_unknown_1,
0575     [DVB_PLL_TUA6010XS]              = &dvb_pll_tua6010xs,
0576     [DVB_PLL_ENV57H1XD5]             = &dvb_pll_env57h1xd5,
0577     [DVB_PLL_TUA6034]                = &dvb_pll_tua6034,
0578     [DVB_PLL_TDA665X]                = &dvb_pll_tda665x,
0579     [DVB_PLL_TDED4]                  = &dvb_pll_tded4,
0580     [DVB_PLL_TDEE4]                  = &dvb_pll_alps_tdee4,
0581     [DVB_PLL_TDHU2]                  = &dvb_pll_tdhu2,
0582     [DVB_PLL_SAMSUNG_TBMV]           = &dvb_pll_samsung_tbmv,
0583     [DVB_PLL_PHILIPS_SD1878_TDA8261] = &dvb_pll_philips_sd1878_tda8261,
0584     [DVB_PLL_OPERA1]                 = &dvb_pll_opera1,
0585     [DVB_PLL_SAMSUNG_DTOS403IH102A]  = &dvb_pll_samsung_dtos403ih102a,
0586     [DVB_PLL_SAMSUNG_TDTC9251DH0]    = &dvb_pll_samsung_tdtc9251dh0,
0587     [DVB_PLL_SAMSUNG_TBDU18132]  = &dvb_pll_samsung_tbdu18132,
0588     [DVB_PLL_SAMSUNG_TBMU24112]      = &dvb_pll_samsung_tbmu24112,
0589     [DVB_PLL_TUA6034_FRIIO]          = &dvb_pll_tua6034_friio,
0590     [DVB_PLL_TDA665X_EARTH_PT1]      = &dvb_pll_tda665x_earth_pt1,
0591 };
0592 
0593 /* ----------------------------------------------------------- */
0594 /* code                                                        */
0595 
0596 static int dvb_pll_configure(struct dvb_frontend *fe, u8 *buf,
0597                  const u32 frequency)
0598 {
0599     struct dvb_pll_priv *priv = fe->tuner_priv;
0600     const struct dvb_pll_desc *desc = priv->pll_desc;
0601     u32 div;
0602     int i;
0603 
0604     for (i = 0; i < desc->count; i++) {
0605         if (frequency > desc->entries[i].limit)
0606             continue;
0607         break;
0608     }
0609 
0610     if (debug)
0611         dprintk("pll: %s: freq=%d | i=%d/%d\n", desc->name,
0612                frequency, i, desc->count);
0613     if (i == desc->count)
0614         return -EINVAL;
0615 
0616     div = (frequency + desc->iffreq +
0617            desc->entries[i].stepsize/2) / desc->entries[i].stepsize;
0618     buf[0] = div >> 8;
0619     buf[1] = div & 0xff;
0620     buf[2] = desc->entries[i].config;
0621     buf[3] = desc->entries[i].cb;
0622 
0623     if (desc->set)
0624         desc->set(fe, buf);
0625 
0626     if (debug)
0627         dprintk("pll: %s: div=%d | buf=0x%02x,0x%02x,0x%02x,0x%02x\n",
0628                desc->name, div, buf[0], buf[1], buf[2], buf[3]);
0629 
0630     // calculate the frequency we set it to
0631     return (div * desc->entries[i].stepsize) - desc->iffreq;
0632 }
0633 
0634 static void dvb_pll_release(struct dvb_frontend *fe)
0635 {
0636     kfree(fe->tuner_priv);
0637     fe->tuner_priv = NULL;
0638 }
0639 
0640 static int dvb_pll_sleep(struct dvb_frontend *fe)
0641 {
0642     struct dvb_pll_priv *priv = fe->tuner_priv;
0643 
0644     if (priv->i2c == NULL)
0645         return -EINVAL;
0646 
0647     if (priv->pll_desc->sleepdata) {
0648         struct i2c_msg msg = { .flags = 0,
0649             .addr = priv->pll_i2c_address,
0650             .buf = priv->pll_desc->sleepdata + 1,
0651             .len = priv->pll_desc->sleepdata[0] };
0652 
0653         int result;
0654 
0655         if (fe->ops.i2c_gate_ctrl)
0656             fe->ops.i2c_gate_ctrl(fe, 1);
0657         if ((result = i2c_transfer(priv->i2c, &msg, 1)) != 1) {
0658             return result;
0659         }
0660         return 0;
0661     }
0662     /* Shouldn't be called when initdata is NULL, maybe BUG()? */
0663     return -EINVAL;
0664 }
0665 
0666 static int dvb_pll_set_params(struct dvb_frontend *fe)
0667 {
0668     struct dtv_frontend_properties *c = &fe->dtv_property_cache;
0669     struct dvb_pll_priv *priv = fe->tuner_priv;
0670     u8 buf[4];
0671     struct i2c_msg msg =
0672         { .addr = priv->pll_i2c_address, .flags = 0,
0673           .buf = buf, .len = sizeof(buf) };
0674     int result;
0675     u32 frequency = 0;
0676 
0677     if (priv->i2c == NULL)
0678         return -EINVAL;
0679 
0680     result = dvb_pll_configure(fe, buf, c->frequency);
0681     if (result < 0)
0682         return result;
0683     else
0684         frequency = result;
0685 
0686     if (fe->ops.i2c_gate_ctrl)
0687         fe->ops.i2c_gate_ctrl(fe, 1);
0688     if ((result = i2c_transfer(priv->i2c, &msg, 1)) != 1) {
0689         return result;
0690     }
0691 
0692     priv->frequency = frequency;
0693     priv->bandwidth = c->bandwidth_hz;
0694 
0695     return 0;
0696 }
0697 
0698 static int dvb_pll_calc_regs(struct dvb_frontend *fe,
0699                  u8 *buf, int buf_len)
0700 {
0701     struct dtv_frontend_properties *c = &fe->dtv_property_cache;
0702     struct dvb_pll_priv *priv = fe->tuner_priv;
0703     int result;
0704     u32 frequency = 0;
0705 
0706     if (buf_len < 5)
0707         return -EINVAL;
0708 
0709     result = dvb_pll_configure(fe, buf + 1, c->frequency);
0710     if (result < 0)
0711         return result;
0712     else
0713         frequency = result;
0714 
0715     buf[0] = priv->pll_i2c_address;
0716 
0717     priv->frequency = frequency;
0718     priv->bandwidth = c->bandwidth_hz;
0719 
0720     return 5;
0721 }
0722 
0723 static int dvb_pll_get_frequency(struct dvb_frontend *fe, u32 *frequency)
0724 {
0725     struct dvb_pll_priv *priv = fe->tuner_priv;
0726     *frequency = priv->frequency;
0727     return 0;
0728 }
0729 
0730 static int dvb_pll_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
0731 {
0732     struct dvb_pll_priv *priv = fe->tuner_priv;
0733     *bandwidth = priv->bandwidth;
0734     return 0;
0735 }
0736 
0737 static int dvb_pll_init(struct dvb_frontend *fe)
0738 {
0739     struct dvb_pll_priv *priv = fe->tuner_priv;
0740 
0741     if (priv->i2c == NULL)
0742         return -EINVAL;
0743 
0744     if (priv->pll_desc->initdata) {
0745         struct i2c_msg msg = { .flags = 0,
0746             .addr = priv->pll_i2c_address,
0747             .buf = priv->pll_desc->initdata + 1,
0748             .len = priv->pll_desc->initdata[0] };
0749 
0750         int result;
0751         if (fe->ops.i2c_gate_ctrl)
0752             fe->ops.i2c_gate_ctrl(fe, 1);
0753         result = i2c_transfer(priv->i2c, &msg, 1);
0754         if (result != 1)
0755             return result;
0756         if (priv->pll_desc->initdata2) {
0757             msg.buf = priv->pll_desc->initdata2 + 1;
0758             msg.len = priv->pll_desc->initdata2[0];
0759             if (fe->ops.i2c_gate_ctrl)
0760                 fe->ops.i2c_gate_ctrl(fe, 1);
0761             result = i2c_transfer(priv->i2c, &msg, 1);
0762             if (result != 1)
0763                 return result;
0764         }
0765         return 0;
0766     }
0767     /* Shouldn't be called when initdata is NULL, maybe BUG()? */
0768     return -EINVAL;
0769 }
0770 
0771 static const struct dvb_tuner_ops dvb_pll_tuner_ops = {
0772     .release = dvb_pll_release,
0773     .sleep = dvb_pll_sleep,
0774     .init = dvb_pll_init,
0775     .set_params = dvb_pll_set_params,
0776     .calc_regs = dvb_pll_calc_regs,
0777     .get_frequency = dvb_pll_get_frequency,
0778     .get_bandwidth = dvb_pll_get_bandwidth,
0779 };
0780 
0781 struct dvb_frontend *dvb_pll_attach(struct dvb_frontend *fe, int pll_addr,
0782                     struct i2c_adapter *i2c,
0783                     unsigned int pll_desc_id)
0784 {
0785     u8 *b1;
0786     struct i2c_msg msg = { .addr = pll_addr, .flags = I2C_M_RD, .len = 1 };
0787     struct dvb_pll_priv *priv = NULL;
0788     int ret;
0789     const struct dvb_pll_desc *desc;
0790     int nr;
0791 
0792     b1 = kmalloc(1, GFP_KERNEL);
0793     if (!b1)
0794         return NULL;
0795 
0796     b1[0] = 0;
0797     msg.buf = b1;
0798 
0799     nr = ida_simple_get(&pll_ida, 0, DVB_PLL_MAX, GFP_KERNEL);
0800     if (nr < 0) {
0801         kfree(b1);
0802         return NULL;
0803     }
0804 
0805     if (id[nr] > DVB_PLL_UNDEFINED && id[nr] < ARRAY_SIZE(pll_list))
0806         pll_desc_id = id[nr];
0807 
0808     BUG_ON(pll_desc_id < 1 || pll_desc_id >= ARRAY_SIZE(pll_list));
0809 
0810     desc = pll_list[pll_desc_id];
0811 
0812     if (i2c != NULL) {
0813         if (fe->ops.i2c_gate_ctrl)
0814             fe->ops.i2c_gate_ctrl(fe, 1);
0815 
0816         ret = i2c_transfer (i2c, &msg, 1);
0817         if (ret != 1)
0818             goto out;
0819         if (fe->ops.i2c_gate_ctrl)
0820                  fe->ops.i2c_gate_ctrl(fe, 0);
0821     }
0822 
0823     priv = kzalloc(sizeof(struct dvb_pll_priv), GFP_KERNEL);
0824     if (!priv)
0825         goto out;
0826 
0827     priv->pll_i2c_address = pll_addr;
0828     priv->i2c = i2c;
0829     priv->pll_desc = desc;
0830     priv->nr = nr;
0831 
0832     memcpy(&fe->ops.tuner_ops, &dvb_pll_tuner_ops,
0833            sizeof(struct dvb_tuner_ops));
0834 
0835     strscpy(fe->ops.tuner_ops.info.name, desc->name,
0836         sizeof(fe->ops.tuner_ops.info.name));
0837 
0838     fe->ops.tuner_ops.info.frequency_min_hz = desc->min;
0839     fe->ops.tuner_ops.info.frequency_max_hz = desc->max;
0840 
0841     dprintk("%s tuner, frequency range: %u...%u\n",
0842         desc->name, desc->min, desc->max);
0843 
0844     if (!desc->initdata)
0845         fe->ops.tuner_ops.init = NULL;
0846     if (!desc->sleepdata)
0847         fe->ops.tuner_ops.sleep = NULL;
0848 
0849     fe->tuner_priv = priv;
0850 
0851     if ((debug) || (id[priv->nr] == pll_desc_id)) {
0852         dprintk("dvb-pll[%d]", priv->nr);
0853         if (i2c != NULL)
0854             pr_cont(" %d-%04x", i2c_adapter_id(i2c), pll_addr);
0855         pr_cont(": id# %d (%s) attached, %s\n", pll_desc_id, desc->name,
0856                id[priv->nr] == pll_desc_id ?
0857                 "insmod option" : "autodetected");
0858     }
0859 
0860     kfree(b1);
0861 
0862     return fe;
0863 out:
0864     kfree(b1);
0865     ida_simple_remove(&pll_ida, nr);
0866 
0867     return NULL;
0868 }
0869 EXPORT_SYMBOL(dvb_pll_attach);
0870 
0871 
0872 static int
0873 dvb_pll_probe(struct i2c_client *client, const struct i2c_device_id *id)
0874 {
0875     struct dvb_pll_config *cfg;
0876     struct dvb_frontend *fe;
0877     unsigned int desc_id;
0878 
0879     cfg = client->dev.platform_data;
0880     fe = cfg->fe;
0881     i2c_set_clientdata(client, fe);
0882     desc_id = (unsigned int) id->driver_data;
0883 
0884     if (!dvb_pll_attach(fe, client->addr, client->adapter, desc_id))
0885         return -ENOMEM;
0886 
0887     /*
0888      * Unset tuner_ops.release (== dvb_pll_release)
0889      * which has been just set in the above dvb_pll_attach(),
0890      * because if tuner_ops.release was left defined,
0891      * this module would be 'put' twice on exit:
0892      * once by dvb_frontend_detach() and another by dvb_module_release().
0893      *
0894      * dvb_pll_release is instead executed in the i2c driver's .remove(),
0895      * keeping dvb_pll_attach untouched for legacy (dvb_attach) drivers.
0896      */
0897     fe->ops.tuner_ops.release = NULL;
0898     dev_info(&client->dev, "DVB Simple Tuner attached.\n");
0899     return 0;
0900 }
0901 
0902 static int dvb_pll_remove(struct i2c_client *client)
0903 {
0904     struct dvb_frontend *fe = i2c_get_clientdata(client);
0905     struct dvb_pll_priv *priv = fe->tuner_priv;
0906 
0907     ida_simple_remove(&pll_ida, priv->nr);
0908     dvb_pll_release(fe);
0909     return 0;
0910 }
0911 
0912 
0913 static const struct i2c_device_id dvb_pll_id[] = {
0914     {"dtt7579",     DVB_PLL_THOMSON_DTT7579},
0915     {"dtt759x",     DVB_PLL_THOMSON_DTT759X},
0916     {"z201",        DVB_PLL_LG_Z201},
0917     {"unknown_1",       DVB_PLL_UNKNOWN_1},
0918     {"tua6010xs",       DVB_PLL_TUA6010XS},
0919     {"env57h1xd5",      DVB_PLL_ENV57H1XD5},
0920     {"tua6034",     DVB_PLL_TUA6034},
0921     {"tda665x",     DVB_PLL_TDA665X},
0922     {"tded4",       DVB_PLL_TDED4},
0923     {"tdhu2",       DVB_PLL_TDHU2},
0924     {"tbmv",        DVB_PLL_SAMSUNG_TBMV},
0925     {"sd1878_tda8261",  DVB_PLL_PHILIPS_SD1878_TDA8261},
0926     {"opera1",      DVB_PLL_OPERA1},
0927     {"dtos403ih102a",   DVB_PLL_SAMSUNG_DTOS403IH102A},
0928     {"tdtc9251dh0",     DVB_PLL_SAMSUNG_TDTC9251DH0},
0929     {"tbdu18132",       DVB_PLL_SAMSUNG_TBDU18132},
0930     {"tbmu24112",       DVB_PLL_SAMSUNG_TBMU24112},
0931     {"tdee4",       DVB_PLL_TDEE4},
0932     {"dtt7520x",        DVB_PLL_THOMSON_DTT7520X},
0933     {"tua6034_friio",   DVB_PLL_TUA6034_FRIIO},
0934     {"tda665x_earthpt1",    DVB_PLL_TDA665X_EARTH_PT1},
0935     {}
0936 };
0937 
0938 
0939 MODULE_DEVICE_TABLE(i2c, dvb_pll_id);
0940 
0941 static struct i2c_driver dvb_pll_driver = {
0942     .driver = {
0943         .name = "dvb_pll",
0944     },
0945     .probe    = dvb_pll_probe,
0946     .remove   = dvb_pll_remove,
0947     .id_table = dvb_pll_id,
0948 };
0949 
0950 module_i2c_driver(dvb_pll_driver);
0951 
0952 MODULE_DESCRIPTION("dvb pll library");
0953 MODULE_AUTHOR("Gerd Knorr");
0954 MODULE_LICENSE("GPL");