Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / drivers / media / dvb-frontends / ix2505v.c
blob965012ad5c596b4335abfaefcfde9f28aa2826a0
1 /*
2 * Driver for Sharp IX2505V (marked B0017) DVB-S silicon tuner
4 * Copyright (C) 2010 Malcolm Priestley
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License Version 2, as
8 * published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
17 #include <linux/module.h>
18 #include <linux/dvb/frontend.h>
19 #include <linux/slab.h>
20 #include <linux/types.h>
22 #include "ix2505v.h"
24 static int ix2505v_debug;
25 #define dprintk(level, args...) do { \
26 if (ix2505v_debug & level) \
27 printk(KERN_DEBUG "ix2505v: " args); \
28 } while (0)
30 #define deb_info(args...) dprintk(0x01, args)
31 #define deb_i2c(args...) dprintk(0x02, args)
33 struct ix2505v_state {
34 struct i2c_adapter *i2c;
35 const struct ix2505v_config *config;
36 u32 frequency;
40 * Data read format of the Sharp IX2505V B0017
42 * byte1: 1 | 1 | 0 | 0 | 0 | MA1 | MA0 | 1
43 * byte2: POR | FL | RD2 | RD1 | RD0 | X | X | X
45 * byte1 = address
46 * byte2;
47 * POR = Power on Reset (VCC H=<2.2v L=>2.2v)
48 * FL = Phase Lock (H=lock L=unlock)
49 * RD0-2 = Reserved internal operations
51 * Only POR can be used to check the tuner is present
53 * Caution: after byte2 the I2C reverts to write mode continuing to read
54 * may corrupt tuning data.
58 static int ix2505v_read_status_reg(struct ix2505v_state *state)
60 u8 addr = state->config->tuner_address;
61 u8 b2[] = {0};
62 int ret;
64 struct i2c_msg msg[1] = {
65 { .addr = addr, .flags = I2C_M_RD, .buf = b2, .len = 1 }
68 ret = i2c_transfer(state->i2c, msg, 1);
69 deb_i2c("Read %s ", __func__);
71 return (ret == 1) ? (int) b2[0] : -1;
74 static int ix2505v_write(struct ix2505v_state *state, u8 buf[], u8 count)
76 struct i2c_msg msg[1] = {
77 { .addr = state->config->tuner_address, .flags = 0,
78 .buf = buf, .len = count },
81 int ret;
83 ret = i2c_transfer(state->i2c, msg, 1);
85 if (ret != 1) {
86 deb_i2c("%s: i2c error, ret=%d\n", __func__, ret);
87 return -EIO;
90 return 0;
93 static void ix2505v_release(struct dvb_frontend *fe)
95 struct ix2505v_state *state = fe->tuner_priv;
97 fe->tuner_priv = NULL;
98 kfree(state);
103 * Data write format of the Sharp IX2505V B0017
105 * byte1: 1 | 1 | 0 | 0 | 0 | 0(MA1)| 0(MA0)| 0
106 * byte2: 0 | BG1 | BG2 | N8 | N7 | N6 | N5 | N4
107 * byte3: N3 | N2 | N1 | A5 | A4 | A3 | A2 | A1
108 * byte4: 1 | 1(C1) | 1(C0) | PD5 | PD4 | TM | 0(RTS)| 1(REF)
109 * byte5: BA2 | BA1 | BA0 | PSC | PD3 |PD2/TS2|DIV/TS1|PD0/TS0
111 * byte1 = address
113 * Write order
114 * 1) byte1 -> byte2 -> byte3 -> byte4 -> byte5
115 * 2) byte1 -> byte4 -> byte5 -> byte2 -> byte3
116 * 3) byte1 -> byte2 -> byte3 -> byte4
117 * 4) byte1 -> byte4 -> byte5 -> byte2
118 * 5) byte1 -> byte2 -> byte3
119 * 6) byte1 -> byte4 -> byte5
120 * 7) byte1 -> byte2
121 * 8) byte1 -> byte4
123 * Recommended Setup
124 * 1 -> 8 -> 6
127 static int ix2505v_set_params(struct dvb_frontend *fe)
129 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
130 struct ix2505v_state *state = fe->tuner_priv;
131 u32 frequency = c->frequency;
132 u32 b_w = (c->symbol_rate * 27) / 32000;
133 u32 div_factor, N , A, x;
134 int ret = 0, len;
135 u8 gain, cc, ref, psc, local_osc, lpf;
136 u8 data[4] = {0};
138 if ((frequency < fe->ops.info.frequency_min)
139 || (frequency > fe->ops.info.frequency_max))
140 return -EINVAL;
142 if (state->config->tuner_gain)
143 gain = (state->config->tuner_gain < 4)
144 ? state->config->tuner_gain : 0;
145 else
146 gain = 0x0;
148 if (state->config->tuner_chargepump)
149 cc = state->config->tuner_chargepump;
150 else
151 cc = 0x3;
153 ref = 8; /* REF =1 */
154 psc = 32; /* PSC = 0 */
156 div_factor = (frequency * ref) / 40; /* local osc = 4Mhz */
157 x = div_factor / psc;
158 N = x/100;
159 A = ((x - (N * 100)) * psc) / 100;
161 data[0] = ((gain & 0x3) << 5) | (N >> 3);
162 data[1] = (N << 5) | (A & 0x1f);
163 data[2] = 0x81 | ((cc & 0x3) << 5) ; /*PD5,PD4 & TM = 0|C1,C0|REF=1*/
165 deb_info("Frq=%d x=%d N=%d A=%d\n", frequency, x, N, A);
167 if (frequency <= 1065000)
168 local_osc = (6 << 5) | 2;
169 else if (frequency <= 1170000)
170 local_osc = (7 << 5) | 2;
171 else if (frequency <= 1300000)
172 local_osc = (1 << 5);
173 else if (frequency <= 1445000)
174 local_osc = (2 << 5);
175 else if (frequency <= 1607000)
176 local_osc = (3 << 5);
177 else if (frequency <= 1778000)
178 local_osc = (4 << 5);
179 else if (frequency <= 1942000)
180 local_osc = (5 << 5);
181 else /*frequency up to 2150000*/
182 local_osc = (6 << 5);
184 data[3] = local_osc; /* all other bits set 0 */
186 if (b_w <= 10000)
187 lpf = 0xc;
188 else if (b_w <= 12000)
189 lpf = 0x2;
190 else if (b_w <= 14000)
191 lpf = 0xa;
192 else if (b_w <= 16000)
193 lpf = 0x6;
194 else if (b_w <= 18000)
195 lpf = 0xe;
196 else if (b_w <= 20000)
197 lpf = 0x1;
198 else if (b_w <= 22000)
199 lpf = 0x9;
200 else if (b_w <= 24000)
201 lpf = 0x5;
202 else if (b_w <= 26000)
203 lpf = 0xd;
204 else if (b_w <= 28000)
205 lpf = 0x3;
206 else
207 lpf = 0xb;
209 deb_info("Osc=%x b_w=%x lpf=%x\n", local_osc, b_w, lpf);
210 deb_info("Data 0=[%4phN]\n", data);
212 if (fe->ops.i2c_gate_ctrl)
213 fe->ops.i2c_gate_ctrl(fe, 1);
215 len = sizeof(data);
216 ret |= ix2505v_write(state, data, len);
218 data[2] |= 0x4; /* set TM = 1 other bits same */
220 if (fe->ops.i2c_gate_ctrl)
221 fe->ops.i2c_gate_ctrl(fe, 1);
223 len = 1;
224 ret |= ix2505v_write(state, &data[2], len); /* write byte 4 only */
226 msleep(10);
228 data[2] |= ((lpf >> 2) & 0x3) << 3; /* lpf */
229 data[3] |= (lpf & 0x3) << 2;
231 deb_info("Data 2=[%x%x]\n", data[2], data[3]);
233 if (fe->ops.i2c_gate_ctrl)
234 fe->ops.i2c_gate_ctrl(fe, 1);
236 len = 2;
237 ret |= ix2505v_write(state, &data[2], len); /* write byte 4 & 5 */
239 if (state->config->min_delay_ms)
240 msleep(state->config->min_delay_ms);
242 state->frequency = frequency;
244 return ret;
247 static int ix2505v_get_frequency(struct dvb_frontend *fe, u32 *frequency)
249 struct ix2505v_state *state = fe->tuner_priv;
251 *frequency = state->frequency;
253 return 0;
256 static const struct dvb_tuner_ops ix2505v_tuner_ops = {
257 .info = {
258 .name = "Sharp IX2505V (B0017)",
259 .frequency_min = 950000,
260 .frequency_max = 2175000
262 .release = ix2505v_release,
263 .set_params = ix2505v_set_params,
264 .get_frequency = ix2505v_get_frequency,
267 struct dvb_frontend *ix2505v_attach(struct dvb_frontend *fe,
268 const struct ix2505v_config *config,
269 struct i2c_adapter *i2c)
271 struct ix2505v_state *state = NULL;
272 int ret;
274 if (NULL == config) {
275 deb_i2c("%s: no config ", __func__);
276 goto error;
279 state = kzalloc(sizeof(struct ix2505v_state), GFP_KERNEL);
280 if (NULL == state)
281 return NULL;
283 state->config = config;
284 state->i2c = i2c;
286 if (state->config->tuner_write_only) {
287 if (fe->ops.i2c_gate_ctrl)
288 fe->ops.i2c_gate_ctrl(fe, 1);
290 ret = ix2505v_read_status_reg(state);
292 if (ret & 0x80) {
293 deb_i2c("%s: No IX2505V found\n", __func__);
294 goto error;
297 if (fe->ops.i2c_gate_ctrl)
298 fe->ops.i2c_gate_ctrl(fe, 0);
301 fe->tuner_priv = state;
303 memcpy(&fe->ops.tuner_ops, &ix2505v_tuner_ops,
304 sizeof(struct dvb_tuner_ops));
305 deb_i2c("%s: initialization (%s addr=0x%02x) ok\n",
306 __func__, fe->ops.tuner_ops.info.name, config->tuner_address);
308 return fe;
310 error:
311 kfree(state);
312 return NULL;
314 EXPORT_SYMBOL(ix2505v_attach);
316 module_param_named(debug, ix2505v_debug, int, 0644);
317 MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
318 MODULE_DESCRIPTION("DVB IX2505V tuner driver");
319 MODULE_AUTHOR("Malcolm Priestley");
320 MODULE_LICENSE("GPL");