0001
0002
0003
0004
0005
0006
0007
0008 #include <linux/module.h>
0009 #include <linux/dvb/frontend.h>
0010 #include <linux/slab.h>
0011 #include <linux/types.h>
0012
0013 #include "ix2505v.h"
0014
0015 static int ix2505v_debug;
0016 #define dprintk(level, args...) do { \
0017 if (ix2505v_debug & level) \
0018 printk(KERN_DEBUG "ix2505v: " args); \
0019 } while (0)
0020
0021 #define deb_info(args...) dprintk(0x01, args)
0022 #define deb_i2c(args...) dprintk(0x02, args)
0023
0024 struct ix2505v_state {
0025 struct i2c_adapter *i2c;
0026 const struct ix2505v_config *config;
0027 u32 frequency;
0028 };
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049 static int ix2505v_read_status_reg(struct ix2505v_state *state)
0050 {
0051 u8 addr = state->config->tuner_address;
0052 u8 b2[] = {0};
0053 int ret;
0054
0055 struct i2c_msg msg[1] = {
0056 { .addr = addr, .flags = I2C_M_RD, .buf = b2, .len = 1 }
0057 };
0058
0059 ret = i2c_transfer(state->i2c, msg, 1);
0060 deb_i2c("Read %s ", __func__);
0061
0062 return (ret == 1) ? (int) b2[0] : -1;
0063 }
0064
0065 static int ix2505v_write(struct ix2505v_state *state, u8 buf[], u8 count)
0066 {
0067 struct i2c_msg msg[1] = {
0068 { .addr = state->config->tuner_address, .flags = 0,
0069 .buf = buf, .len = count },
0070 };
0071
0072 int ret;
0073
0074 ret = i2c_transfer(state->i2c, msg, 1);
0075
0076 if (ret != 1) {
0077 deb_i2c("%s: i2c error, ret=%d\n", __func__, ret);
0078 return -EIO;
0079 }
0080
0081 return 0;
0082 }
0083
0084 static void ix2505v_release(struct dvb_frontend *fe)
0085 {
0086 struct ix2505v_state *state = fe->tuner_priv;
0087
0088 fe->tuner_priv = NULL;
0089 kfree(state);
0090
0091 }
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112
0113
0114
0115
0116
0117
0118 static int ix2505v_set_params(struct dvb_frontend *fe)
0119 {
0120 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
0121 struct ix2505v_state *state = fe->tuner_priv;
0122 u32 frequency = c->frequency;
0123 u32 b_w = (c->symbol_rate * 27) / 32000;
0124 u32 div_factor, N , A, x;
0125 int ret = 0, len;
0126 u8 gain, cc, ref, psc, local_osc, lpf;
0127 u8 data[4] = {0};
0128
0129 if ((frequency < fe->ops.info.frequency_min_hz / kHz)
0130 || (frequency > fe->ops.info.frequency_max_hz / kHz))
0131 return -EINVAL;
0132
0133 if (state->config->tuner_gain)
0134 gain = (state->config->tuner_gain < 4)
0135 ? state->config->tuner_gain : 0;
0136 else
0137 gain = 0x0;
0138
0139 if (state->config->tuner_chargepump)
0140 cc = state->config->tuner_chargepump;
0141 else
0142 cc = 0x3;
0143
0144 ref = 8;
0145 psc = 32;
0146
0147 div_factor = (frequency * ref) / 40;
0148 x = div_factor / psc;
0149 N = x/100;
0150 A = ((x - (N * 100)) * psc) / 100;
0151
0152 data[0] = ((gain & 0x3) << 5) | (N >> 3);
0153 data[1] = (N << 5) | (A & 0x1f);
0154 data[2] = 0x81 | ((cc & 0x3) << 5) ;
0155
0156 deb_info("Frq=%d x=%d N=%d A=%d\n", frequency, x, N, A);
0157
0158 if (frequency <= 1065000)
0159 local_osc = (6 << 5) | 2;
0160 else if (frequency <= 1170000)
0161 local_osc = (7 << 5) | 2;
0162 else if (frequency <= 1300000)
0163 local_osc = (1 << 5);
0164 else if (frequency <= 1445000)
0165 local_osc = (2 << 5);
0166 else if (frequency <= 1607000)
0167 local_osc = (3 << 5);
0168 else if (frequency <= 1778000)
0169 local_osc = (4 << 5);
0170 else if (frequency <= 1942000)
0171 local_osc = (5 << 5);
0172 else
0173 local_osc = (6 << 5);
0174
0175 data[3] = local_osc;
0176
0177 if (b_w <= 10000)
0178 lpf = 0xc;
0179 else if (b_w <= 12000)
0180 lpf = 0x2;
0181 else if (b_w <= 14000)
0182 lpf = 0xa;
0183 else if (b_w <= 16000)
0184 lpf = 0x6;
0185 else if (b_w <= 18000)
0186 lpf = 0xe;
0187 else if (b_w <= 20000)
0188 lpf = 0x1;
0189 else if (b_w <= 22000)
0190 lpf = 0x9;
0191 else if (b_w <= 24000)
0192 lpf = 0x5;
0193 else if (b_w <= 26000)
0194 lpf = 0xd;
0195 else if (b_w <= 28000)
0196 lpf = 0x3;
0197 else
0198 lpf = 0xb;
0199
0200 deb_info("Osc=%x b_w=%x lpf=%x\n", local_osc, b_w, lpf);
0201 deb_info("Data 0=[%4phN]\n", data);
0202
0203 if (fe->ops.i2c_gate_ctrl)
0204 fe->ops.i2c_gate_ctrl(fe, 1);
0205
0206 len = sizeof(data);
0207 ret |= ix2505v_write(state, data, len);
0208
0209 data[2] |= 0x4;
0210
0211 if (fe->ops.i2c_gate_ctrl)
0212 fe->ops.i2c_gate_ctrl(fe, 1);
0213
0214 len = 1;
0215 ret |= ix2505v_write(state, &data[2], len);
0216
0217 msleep(10);
0218
0219 data[2] |= ((lpf >> 2) & 0x3) << 3;
0220 data[3] |= (lpf & 0x3) << 2;
0221
0222 deb_info("Data 2=[%x%x]\n", data[2], data[3]);
0223
0224 if (fe->ops.i2c_gate_ctrl)
0225 fe->ops.i2c_gate_ctrl(fe, 1);
0226
0227 len = 2;
0228 ret |= ix2505v_write(state, &data[2], len);
0229
0230 if (state->config->min_delay_ms)
0231 msleep(state->config->min_delay_ms);
0232
0233 state->frequency = frequency;
0234
0235 return ret;
0236 }
0237
0238 static int ix2505v_get_frequency(struct dvb_frontend *fe, u32 *frequency)
0239 {
0240 struct ix2505v_state *state = fe->tuner_priv;
0241
0242 *frequency = state->frequency;
0243
0244 return 0;
0245 }
0246
0247 static const struct dvb_tuner_ops ix2505v_tuner_ops = {
0248 .info = {
0249 .name = "Sharp IX2505V (B0017)",
0250 .frequency_min_hz = 950 * MHz,
0251 .frequency_max_hz = 2175 * MHz
0252 },
0253 .release = ix2505v_release,
0254 .set_params = ix2505v_set_params,
0255 .get_frequency = ix2505v_get_frequency,
0256 };
0257
0258 struct dvb_frontend *ix2505v_attach(struct dvb_frontend *fe,
0259 const struct ix2505v_config *config,
0260 struct i2c_adapter *i2c)
0261 {
0262 struct ix2505v_state *state = NULL;
0263 int ret;
0264
0265 if (NULL == config) {
0266 deb_i2c("%s: no config ", __func__);
0267 goto error;
0268 }
0269
0270 state = kzalloc(sizeof(struct ix2505v_state), GFP_KERNEL);
0271 if (NULL == state)
0272 return NULL;
0273
0274 state->config = config;
0275 state->i2c = i2c;
0276
0277 if (state->config->tuner_write_only) {
0278 if (fe->ops.i2c_gate_ctrl)
0279 fe->ops.i2c_gate_ctrl(fe, 1);
0280
0281 ret = ix2505v_read_status_reg(state);
0282
0283 if (ret & 0x80) {
0284 deb_i2c("%s: No IX2505V found\n", __func__);
0285 goto error;
0286 }
0287
0288 if (fe->ops.i2c_gate_ctrl)
0289 fe->ops.i2c_gate_ctrl(fe, 0);
0290 }
0291
0292 fe->tuner_priv = state;
0293
0294 memcpy(&fe->ops.tuner_ops, &ix2505v_tuner_ops,
0295 sizeof(struct dvb_tuner_ops));
0296 deb_i2c("%s: initialization (%s addr=0x%02x) ok\n",
0297 __func__, fe->ops.tuner_ops.info.name, config->tuner_address);
0298
0299 return fe;
0300
0301 error:
0302 kfree(state);
0303 return NULL;
0304 }
0305 EXPORT_SYMBOL(ix2505v_attach);
0306
0307 module_param_named(debug, ix2505v_debug, int, 0644);
0308 MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
0309 MODULE_DESCRIPTION("DVB IX2505V tuner driver");
0310 MODULE_AUTHOR("Malcolm Priestley");
0311 MODULE_LICENSE("GPL");