Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
tua6100.c
Go to the documentation of this file.
1 
31 #include <linux/slab.h>
32 #include <linux/module.h>
33 #include <linux/dvb/frontend.h>
34 #include <asm/types.h>
35 
36 #include "tua6100.h"
37 
38 struct tua6100_priv {
39  /* i2c details */
41  struct i2c_adapter *i2c;
43 };
44 
45 static int tua6100_release(struct dvb_frontend *fe)
46 {
47  kfree(fe->tuner_priv);
48  fe->tuner_priv = NULL;
49  return 0;
50 }
51 
52 static int tua6100_sleep(struct dvb_frontend *fe)
53 {
54  struct tua6100_priv *priv = fe->tuner_priv;
55  int ret;
56  u8 reg0[] = { 0x00, 0x00 };
57  struct i2c_msg msg = { .addr = priv->i2c_address, .flags = 0, .buf = reg0, .len = 2 };
58 
59  if (fe->ops.i2c_gate_ctrl)
60  fe->ops.i2c_gate_ctrl(fe, 1);
61  if ((ret = i2c_transfer (priv->i2c, &msg, 1)) != 1) {
62  printk("%s: i2c error\n", __func__);
63  }
64  if (fe->ops.i2c_gate_ctrl)
65  fe->ops.i2c_gate_ctrl(fe, 0);
66 
67  return (ret == 1) ? 0 : ret;
68 }
69 
70 static int tua6100_set_params(struct dvb_frontend *fe)
71 {
73  struct tua6100_priv *priv = fe->tuner_priv;
74  u32 div;
75  u32 prediv;
76  u8 reg0[] = { 0x00, 0x00 };
77  u8 reg1[] = { 0x01, 0x00, 0x00, 0x00 };
78  u8 reg2[] = { 0x02, 0x00, 0x00 };
79  struct i2c_msg msg0 = { .addr = priv->i2c_address, .flags = 0, .buf = reg0, .len = 2 };
80  struct i2c_msg msg1 = { .addr = priv->i2c_address, .flags = 0, .buf = reg1, .len = 4 };
81  struct i2c_msg msg2 = { .addr = priv->i2c_address, .flags = 0, .buf = reg2, .len = 3 };
82 
83 #define _R 4
84 #define _P 32
85 #define _ri 4000000
86 
87  // setup register 0
88  if (c->frequency < 2000000)
89  reg0[1] = 0x03;
90  else
91  reg0[1] = 0x07;
92 
93  // setup register 1
94  if (c->frequency < 1630000)
95  reg1[1] = 0x2c;
96  else
97  reg1[1] = 0x0c;
98 
99  if (_P == 64)
100  reg1[1] |= 0x40;
101  if (c->frequency >= 1525000)
102  reg1[1] |= 0x80;
103 
104  // register 2
105  reg2[1] = (_R >> 8) & 0x03;
106  reg2[2] = _R;
107  if (c->frequency < 1455000)
108  reg2[1] |= 0x1c;
109  else if (c->frequency < 1630000)
110  reg2[1] |= 0x0c;
111  else
112  reg2[1] |= 0x1c;
113 
114  /*
115  * The N divisor ratio (note: c->frequency is in kHz, but we
116  * need it in Hz)
117  */
118  prediv = (c->frequency * _R) / (_ri / 1000);
119  div = prediv / _P;
120  reg1[1] |= (div >> 9) & 0x03;
121  reg1[2] = div >> 1;
122  reg1[3] = (div << 7);
123  priv->frequency = ((div * _P) * (_ri / 1000)) / _R;
124 
125  // Finally, calculate and store the value for A
126  reg1[3] |= (prediv - (div*_P)) & 0x7f;
127 
128 #undef _R
129 #undef _P
130 #undef _ri
131 
132  if (fe->ops.i2c_gate_ctrl)
133  fe->ops.i2c_gate_ctrl(fe, 1);
134  if (i2c_transfer(priv->i2c, &msg0, 1) != 1)
135  return -EIO;
136 
137  if (fe->ops.i2c_gate_ctrl)
138  fe->ops.i2c_gate_ctrl(fe, 1);
139  if (i2c_transfer(priv->i2c, &msg2, 1) != 1)
140  return -EIO;
141 
142  if (fe->ops.i2c_gate_ctrl)
143  fe->ops.i2c_gate_ctrl(fe, 1);
144  if (i2c_transfer(priv->i2c, &msg1, 1) != 1)
145  return -EIO;
146 
147  if (fe->ops.i2c_gate_ctrl)
148  fe->ops.i2c_gate_ctrl(fe, 0);
149 
150  return 0;
151 }
152 
153 static int tua6100_get_frequency(struct dvb_frontend *fe, u32 *frequency)
154 {
155  struct tua6100_priv *priv = fe->tuner_priv;
156  *frequency = priv->frequency;
157  return 0;
158 }
159 
160 static struct dvb_tuner_ops tua6100_tuner_ops = {
161  .info = {
162  .name = "Infineon TUA6100",
163  .frequency_min = 950000,
164  .frequency_max = 2150000,
165  .frequency_step = 1000,
166  },
167  .release = tua6100_release,
168  .sleep = tua6100_sleep,
169  .set_params = tua6100_set_params,
170  .get_frequency = tua6100_get_frequency,
171 };
172 
173 struct dvb_frontend *tua6100_attach(struct dvb_frontend *fe, int addr, struct i2c_adapter *i2c)
174 {
175  struct tua6100_priv *priv = NULL;
176  u8 b1 [] = { 0x80 };
177  u8 b2 [] = { 0x00 };
178  struct i2c_msg msg [] = { { .addr = addr, .flags = 0, .buf = b1, .len = 1 },
179  { .addr = addr, .flags = I2C_M_RD, .buf = b2, .len = 1 } };
180  int ret;
181 
182  if (fe->ops.i2c_gate_ctrl)
183  fe->ops.i2c_gate_ctrl(fe, 1);
184  ret = i2c_transfer (i2c, msg, 2);
185  if (fe->ops.i2c_gate_ctrl)
186  fe->ops.i2c_gate_ctrl(fe, 0);
187 
188  if (ret != 2)
189  return NULL;
190 
191  priv = kzalloc(sizeof(struct tua6100_priv), GFP_KERNEL);
192  if (priv == NULL)
193  return NULL;
194 
195  priv->i2c_address = addr;
196  priv->i2c = i2c;
197 
198  memcpy(&fe->ops.tuner_ops, &tua6100_tuner_ops, sizeof(struct dvb_tuner_ops));
199  fe->tuner_priv = priv;
200  return fe;
201 }
203 
204 MODULE_DESCRIPTION("DVB tua6100 driver");
205 MODULE_AUTHOR("Andrew de Quincey");
206 MODULE_LICENSE("GPL");