Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ix2505v.c
Go to the documentation of this file.
1 
21 #include <linux/module.h>
22 #include <linux/dvb/frontend.h>
23 #include <linux/slab.h>
24 #include <linux/types.h>
25 
26 #include "ix2505v.h"
27 
28 static int ix2505v_debug;
29 #define dprintk(level, args...) do { \
30  if (ix2505v_debug & level) \
31  printk(KERN_DEBUG "ix2505v: " args); \
32 } while (0)
33 
34 #define deb_info(args...) dprintk(0x01, args)
35 #define deb_i2c(args...) dprintk(0x02, args)
36 
37 struct ix2505v_state {
38  struct i2c_adapter *i2c;
39  const struct ix2505v_config *config;
41 };
42 
62 static int ix2505v_read_status_reg(struct ix2505v_state *state)
63 {
64  u8 addr = state->config->tuner_address;
65  u8 b2[] = {0};
66  int ret;
67 
68  struct i2c_msg msg[1] = {
69  { .addr = addr, .flags = I2C_M_RD, .buf = b2, .len = 1 }
70  };
71 
72  ret = i2c_transfer(state->i2c, msg, 1);
73  deb_i2c("Read %s ", __func__);
74 
75  return (ret == 1) ? (int) b2[0] : -1;
76 }
77 
78 static int ix2505v_write(struct ix2505v_state *state, u8 buf[], u8 count)
79 {
80  struct i2c_msg msg[1] = {
81  { .addr = state->config->tuner_address, .flags = 0,
82  .buf = buf, .len = count },
83  };
84 
85  int ret;
86 
87  ret = i2c_transfer(state->i2c, msg, 1);
88 
89  if (ret != 1) {
90  deb_i2c("%s: i2c error, ret=%d\n", __func__, ret);
91  return -EIO;
92  }
93 
94  return 0;
95 }
96 
97 static int ix2505v_release(struct dvb_frontend *fe)
98 {
99  struct ix2505v_state *state = fe->tuner_priv;
100 
101  fe->tuner_priv = NULL;
102  kfree(state);
103 
104  return 0;
105 }
106 
132 static int ix2505v_set_params(struct dvb_frontend *fe)
133 {
135  struct ix2505v_state *state = fe->tuner_priv;
136  u32 frequency = c->frequency;
137  u32 b_w = (c->symbol_rate * 27) / 32000;
138  u32 div_factor, N , A, x;
139  int ret = 0, len;
140  u8 gain, cc, ref, psc, local_osc, lpf;
141  u8 data[4] = {0};
142 
143  if ((frequency < fe->ops.info.frequency_min)
144  || (frequency > fe->ops.info.frequency_max))
145  return -EINVAL;
146 
147  if (state->config->tuner_gain)
148  gain = (state->config->tuner_gain < 4)
149  ? state->config->tuner_gain : 0;
150  else
151  gain = 0x0;
152 
153  if (state->config->tuner_chargepump)
154  cc = state->config->tuner_chargepump;
155  else
156  cc = 0x3;
157 
158  ref = 8; /* REF =1 */
159  psc = 32; /* PSC = 0 */
160 
161  div_factor = (frequency * ref) / 40; /* local osc = 4Mhz */
162  x = div_factor / psc;
163  N = x/100;
164  A = ((x - (N * 100)) * psc) / 100;
165 
166  data[0] = ((gain & 0x3) << 5) | (N >> 3);
167  data[1] = (N << 5) | (A & 0x1f);
168  data[2] = 0x81 | ((cc & 0x3) << 5) ; /*PD5,PD4 & TM = 0|C1,C0|REF=1*/
169 
170  deb_info("Frq=%d x=%d N=%d A=%d\n", frequency, x, N, A);
171 
172  if (frequency <= 1065000)
173  local_osc = (6 << 5) | 2;
174  else if (frequency <= 1170000)
175  local_osc = (7 << 5) | 2;
176  else if (frequency <= 1300000)
177  local_osc = (1 << 5);
178  else if (frequency <= 1445000)
179  local_osc = (2 << 5);
180  else if (frequency <= 1607000)
181  local_osc = (3 << 5);
182  else if (frequency <= 1778000)
183  local_osc = (4 << 5);
184  else if (frequency <= 1942000)
185  local_osc = (5 << 5);
186  else /*frequency up to 2150000*/
187  local_osc = (6 << 5);
188 
189  data[3] = local_osc; /* all other bits set 0 */
190 
191  if (b_w <= 10000)
192  lpf = 0xc;
193  else if (b_w <= 12000)
194  lpf = 0x2;
195  else if (b_w <= 14000)
196  lpf = 0xa;
197  else if (b_w <= 16000)
198  lpf = 0x6;
199  else if (b_w <= 18000)
200  lpf = 0xe;
201  else if (b_w <= 20000)
202  lpf = 0x1;
203  else if (b_w <= 22000)
204  lpf = 0x9;
205  else if (b_w <= 24000)
206  lpf = 0x5;
207  else if (b_w <= 26000)
208  lpf = 0xd;
209  else if (b_w <= 28000)
210  lpf = 0x3;
211  else
212  lpf = 0xb;
213 
214  deb_info("Osc=%x b_w=%x lpf=%x\n", local_osc, b_w, lpf);
215  deb_info("Data 0=[%x%x%x%x]\n", data[0], data[1], data[2], data[3]);
216 
217  if (fe->ops.i2c_gate_ctrl)
218  fe->ops.i2c_gate_ctrl(fe, 1);
219 
220  len = sizeof(data);
221  ret |= ix2505v_write(state, data, len);
222 
223  data[2] |= 0x4; /* set TM = 1 other bits same */
224 
225  if (fe->ops.i2c_gate_ctrl)
226  fe->ops.i2c_gate_ctrl(fe, 1);
227 
228  len = 1;
229  ret |= ix2505v_write(state, &data[2], len); /* write byte 4 only */
230 
231  msleep(10);
232 
233  data[2] |= ((lpf >> 2) & 0x3) << 3; /* lpf */
234  data[3] |= (lpf & 0x3) << 2;
235 
236  deb_info("Data 2=[%x%x]\n", data[2], data[3]);
237 
238  if (fe->ops.i2c_gate_ctrl)
239  fe->ops.i2c_gate_ctrl(fe, 1);
240 
241  len = 2;
242  ret |= ix2505v_write(state, &data[2], len); /* write byte 4 & 5 */
243 
244  if (state->config->min_delay_ms)
245  msleep(state->config->min_delay_ms);
246 
247  state->frequency = frequency;
248 
249  return ret;
250 }
251 
252 static int ix2505v_get_frequency(struct dvb_frontend *fe, u32 *frequency)
253 {
254  struct ix2505v_state *state = fe->tuner_priv;
255 
256  *frequency = state->frequency;
257 
258  return 0;
259 }
260 
261 static struct dvb_tuner_ops ix2505v_tuner_ops = {
262  .info = {
263  .name = "Sharp IX2505V (B0017)",
264  .frequency_min = 950000,
265  .frequency_max = 2175000
266  },
267  .release = ix2505v_release,
268  .set_params = ix2505v_set_params,
269  .get_frequency = ix2505v_get_frequency,
270 };
271 
273  const struct ix2505v_config *config,
274  struct i2c_adapter *i2c)
275 {
276  struct ix2505v_state *state = NULL;
277  int ret;
278 
279  if (NULL == config) {
280  deb_i2c("%s: no config ", __func__);
281  goto error;
282  }
283 
284  state = kzalloc(sizeof(struct ix2505v_state), GFP_KERNEL);
285  if (NULL == state)
286  return NULL;
287 
288  state->config = config;
289  state->i2c = i2c;
290 
291  if (state->config->tuner_write_only) {
292  if (fe->ops.i2c_gate_ctrl)
293  fe->ops.i2c_gate_ctrl(fe, 1);
294 
295  ret = ix2505v_read_status_reg(state);
296 
297  if (ret & 0x80) {
298  deb_i2c("%s: No IX2505V found\n", __func__);
299  goto error;
300  }
301 
302  if (fe->ops.i2c_gate_ctrl)
303  fe->ops.i2c_gate_ctrl(fe, 0);
304  }
305 
306  fe->tuner_priv = state;
307 
308  memcpy(&fe->ops.tuner_ops, &ix2505v_tuner_ops,
309  sizeof(struct dvb_tuner_ops));
310  deb_i2c("%s: initialization (%s addr=0x%02x) ok\n",
311  __func__, fe->ops.tuner_ops.info.name, config->tuner_address);
312 
313  return fe;
314 
315 error:
316  kfree(state);
317  return NULL;
318 }
320 
321 module_param_named(debug, ix2505v_debug, int, 0644);
322 MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
323 MODULE_DESCRIPTION("DVB IX2505V tuner driver");
324 MODULE_AUTHOR("Malcolm Priestley");
325 MODULE_LICENSE("GPL");