0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include <linux/slab.h>
0011 #include <linux/module.h>
0012 #include <linux/dvb/frontend.h>
0013 #include <asm/types.h>
0014
0015 #include "tda826x.h"
0016
0017 static int debug;
0018 #define dprintk(args...) \
0019 do { \
0020 if (debug) printk(KERN_DEBUG "tda826x: " args); \
0021 } while (0)
0022
0023 struct tda826x_priv {
0024
0025 int i2c_address;
0026 struct i2c_adapter *i2c;
0027 u8 has_loopthrough:1;
0028 u32 frequency;
0029 };
0030
0031 static void tda826x_release(struct dvb_frontend *fe)
0032 {
0033 kfree(fe->tuner_priv);
0034 fe->tuner_priv = NULL;
0035 }
0036
0037 static int tda826x_sleep(struct dvb_frontend *fe)
0038 {
0039 struct tda826x_priv *priv = fe->tuner_priv;
0040 int ret;
0041 u8 buf [] = { 0x00, 0x8d };
0042 struct i2c_msg msg = { .addr = priv->i2c_address, .flags = 0, .buf = buf, .len = 2 };
0043
0044 dprintk("%s:\n", __func__);
0045
0046 if (!priv->has_loopthrough)
0047 buf[1] = 0xad;
0048
0049 if (fe->ops.i2c_gate_ctrl)
0050 fe->ops.i2c_gate_ctrl(fe, 1);
0051 if ((ret = i2c_transfer (priv->i2c, &msg, 1)) != 1) {
0052 dprintk("%s: i2c error\n", __func__);
0053 }
0054 if (fe->ops.i2c_gate_ctrl)
0055 fe->ops.i2c_gate_ctrl(fe, 0);
0056
0057 return (ret == 1) ? 0 : ret;
0058 }
0059
0060 static int tda826x_set_params(struct dvb_frontend *fe)
0061 {
0062 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
0063 struct tda826x_priv *priv = fe->tuner_priv;
0064 int ret;
0065 u32 div;
0066 u32 ksyms;
0067 u32 bandwidth;
0068 u8 buf [11];
0069 struct i2c_msg msg = { .addr = priv->i2c_address, .flags = 0, .buf = buf, .len = 11 };
0070
0071 dprintk("%s:\n", __func__);
0072
0073 div = (p->frequency + (1000-1)) / 1000;
0074
0075
0076
0077 ksyms = p->symbol_rate / 1000;
0078 bandwidth = (878 * ksyms + 6500000) / 1000000 + 1;
0079 if (bandwidth < 5)
0080 bandwidth = 5;
0081 else if (bandwidth > 36)
0082 bandwidth = 36;
0083
0084 buf[0] = 0x00;
0085 buf[1] = 0x09;
0086 if (!priv->has_loopthrough)
0087 buf[1] |= 0x20;
0088 buf[2] = (1<<5) | 0x0b;
0089 buf[3] = div >> 7;
0090 buf[4] = div << 1;
0091 buf[5] = ((bandwidth - 5) << 3) | 7;
0092 buf[6] = 0xfe;
0093 buf[7] = 0x83;
0094 buf[8] = 0x80;
0095 buf[9] = 0x1a;
0096 buf[10] = 0xd4;
0097
0098 if (fe->ops.i2c_gate_ctrl)
0099 fe->ops.i2c_gate_ctrl(fe, 1);
0100 if ((ret = i2c_transfer (priv->i2c, &msg, 1)) != 1) {
0101 dprintk("%s: i2c error\n", __func__);
0102 }
0103 if (fe->ops.i2c_gate_ctrl)
0104 fe->ops.i2c_gate_ctrl(fe, 0);
0105
0106 priv->frequency = div * 1000;
0107
0108 return (ret == 1) ? 0 : ret;
0109 }
0110
0111 static int tda826x_get_frequency(struct dvb_frontend *fe, u32 *frequency)
0112 {
0113 struct tda826x_priv *priv = fe->tuner_priv;
0114 *frequency = priv->frequency;
0115 return 0;
0116 }
0117
0118 static const struct dvb_tuner_ops tda826x_tuner_ops = {
0119 .info = {
0120 .name = "Philips TDA826X",
0121 .frequency_min_hz = 950 * MHz,
0122 .frequency_max_hz = 2175 * MHz
0123 },
0124 .release = tda826x_release,
0125 .sleep = tda826x_sleep,
0126 .set_params = tda826x_set_params,
0127 .get_frequency = tda826x_get_frequency,
0128 };
0129
0130 struct dvb_frontend *tda826x_attach(struct dvb_frontend *fe, int addr, struct i2c_adapter *i2c, int has_loopthrough)
0131 {
0132 struct tda826x_priv *priv = NULL;
0133 u8 b1 [] = { 0, 0 };
0134 struct i2c_msg msg[2] = {
0135 { .addr = addr, .flags = 0, .buf = NULL, .len = 0 },
0136 { .addr = addr, .flags = I2C_M_RD, .buf = b1, .len = 2 }
0137 };
0138 int ret;
0139
0140 dprintk("%s:\n", __func__);
0141
0142 if (fe->ops.i2c_gate_ctrl)
0143 fe->ops.i2c_gate_ctrl(fe, 1);
0144 ret = i2c_transfer (i2c, msg, 2);
0145 if (fe->ops.i2c_gate_ctrl)
0146 fe->ops.i2c_gate_ctrl(fe, 0);
0147
0148 if (ret != 2)
0149 return NULL;
0150 if (!(b1[1] & 0x80))
0151 return NULL;
0152
0153 priv = kzalloc(sizeof(struct tda826x_priv), GFP_KERNEL);
0154 if (priv == NULL)
0155 return NULL;
0156
0157 priv->i2c_address = addr;
0158 priv->i2c = i2c;
0159 priv->has_loopthrough = has_loopthrough;
0160
0161 memcpy(&fe->ops.tuner_ops, &tda826x_tuner_ops, sizeof(struct dvb_tuner_ops));
0162
0163 fe->tuner_priv = priv;
0164
0165 return fe;
0166 }
0167 EXPORT_SYMBOL(tda826x_attach);
0168
0169 module_param(debug, int, 0644);
0170 MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
0171
0172 MODULE_DESCRIPTION("DVB TDA826x driver");
0173 MODULE_AUTHOR("Andrew de Quincey");
0174 MODULE_LICENSE("GPL");