V4L/DVB (6433): Move all tda8275/8275a tuning code from tda8290 module into tda827x...
[pandora-kernel.git] / drivers / media / video / tda8290.c
1 /*
2
3    i2c tv tuner chip device driver
4    controls the philips tda8290+75 tuner chip combo.
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
20    This "tda8290" module was split apart from the original "tuner" module.
21 */
22
23 #include <linux/i2c.h>
24 #include <linux/delay.h>
25 #include <linux/videodev.h>
26 #include "tda8290.h"
27 #include "tda827x.h"
28
29 static int tuner_debug = 0;
30 module_param_named(debug, tuner_debug, int, 0644);
31 MODULE_PARM_DESC(debug, "enable verbose debug messages");
32
33 #define PREFIX "tda8290 "
34
35 /* ---------------------------------------------------------------------- */
36
37 struct tda8290_priv {
38         struct tuner_i2c_props i2c_props;
39
40         unsigned char tda8290_easy_mode;
41
42         unsigned char tda827x_addr;
43         unsigned char tda827x_ver;
44
45         struct tda827x_config cfg;
46 };
47
48 /*---------------------------------------------------------------------*/
49
50 static void tda8290_i2c_bridge(struct tuner *t, int close)
51 {
52         struct tda8290_priv *priv = t->priv;
53
54         unsigned char  enable[2] = { 0x21, 0xC0 };
55         unsigned char disable[2] = { 0x21, 0x00 };
56         unsigned char *msg;
57         if(close) {
58                 msg = enable;
59                 tuner_i2c_xfer_send(&priv->i2c_props, msg, 2);
60                 /* let the bridge stabilize */
61                 msleep(20);
62         } else {
63                 msg = disable;
64                 tuner_i2c_xfer_send(&priv->i2c_props, msg, 2);
65         }
66 }
67
68 /*---------------------------------------------------------------------*/
69
70 static void set_audio(struct tuner *t)
71 {
72         struct tda8290_priv *priv = t->priv;
73         char* mode;
74
75         priv->cfg.tda827x_lpsel = 0;
76         if (t->std & V4L2_STD_MN) {
77                 priv->cfg.sgIF = 92;
78                 priv->tda8290_easy_mode = 0x01;
79                 priv->cfg.tda827x_lpsel = 1;
80                 mode = "MN";
81         } else if (t->std & V4L2_STD_B) {
82                 priv->cfg.sgIF = 108;
83                 priv->tda8290_easy_mode = 0x02;
84                 mode = "B";
85         } else if (t->std & V4L2_STD_GH) {
86                 priv->cfg.sgIF = 124;
87                 priv->tda8290_easy_mode = 0x04;
88                 mode = "GH";
89         } else if (t->std & V4L2_STD_PAL_I) {
90                 priv->cfg.sgIF = 124;
91                 priv->tda8290_easy_mode = 0x08;
92                 mode = "I";
93         } else if (t->std & V4L2_STD_DK) {
94                 priv->cfg.sgIF = 124;
95                 priv->tda8290_easy_mode = 0x10;
96                 mode = "DK";
97         } else if (t->std & V4L2_STD_SECAM_L) {
98                 priv->cfg.sgIF = 124;
99                 priv->tda8290_easy_mode = 0x20;
100                 mode = "L";
101         } else if (t->std & V4L2_STD_SECAM_LC) {
102                 priv->cfg.sgIF = 20;
103                 priv->tda8290_easy_mode = 0x40;
104                 mode = "LC";
105         } else {
106                 priv->cfg.sgIF = 124;
107                 priv->tda8290_easy_mode = 0x10;
108                 mode = "xx";
109         }
110
111         if (t->mode == V4L2_TUNER_RADIO)
112                 priv->cfg.sgIF = 88; /* if frequency is 5.5 MHz */
113
114         tuner_dbg("setting tda8290 to system %s\n", mode);
115 }
116
117 static void tda8290_set_freq(struct tuner *t, unsigned int freq)
118 {
119         struct tda8290_priv *priv = t->priv;
120         unsigned char soft_reset[]  = { 0x00, 0x00 };
121         unsigned char easy_mode[]   = { 0x01, priv->tda8290_easy_mode };
122         unsigned char expert_mode[] = { 0x01, 0x80 };
123         unsigned char agc_out_on[]  = { 0x02, 0x00 };
124         unsigned char gainset_off[] = { 0x28, 0x14 };
125         unsigned char if_agc_spd[]  = { 0x0f, 0x88 };
126         unsigned char adc_head_6[]  = { 0x05, 0x04 };
127         unsigned char adc_head_9[]  = { 0x05, 0x02 };
128         unsigned char adc_head_12[] = { 0x05, 0x01 };
129         unsigned char pll_bw_nom[]  = { 0x0d, 0x47 };
130         unsigned char pll_bw_low[]  = { 0x0d, 0x27 };
131         unsigned char gainset_2[]   = { 0x28, 0x64 };
132         unsigned char agc_rst_on[]  = { 0x0e, 0x0b };
133         unsigned char agc_rst_off[] = { 0x0e, 0x09 };
134         unsigned char if_agc_set[]  = { 0x0f, 0x81 };
135         unsigned char addr_adc_sat  = 0x1a;
136         unsigned char addr_agc_stat = 0x1d;
137         unsigned char addr_pll_stat = 0x1b;
138         unsigned char adc_sat, agc_stat,
139                       pll_stat;
140         int i;
141
142         struct analog_parameters params = {
143                 .frequency = freq,
144                 .mode      = t->mode,
145                 .audmode   = t->audmode,
146                 .std       = t->std
147         };
148
149         set_audio(t);
150
151         tuner_dbg("tda827xa config is 0x%02x\n", t->config);
152         tuner_i2c_xfer_send(&priv->i2c_props, easy_mode, 2);
153         tuner_i2c_xfer_send(&priv->i2c_props, agc_out_on, 2);
154         tuner_i2c_xfer_send(&priv->i2c_props, soft_reset, 2);
155         msleep(1);
156
157         expert_mode[1] = priv->tda8290_easy_mode + 0x80;
158         tuner_i2c_xfer_send(&priv->i2c_props, expert_mode, 2);
159         tuner_i2c_xfer_send(&priv->i2c_props, gainset_off, 2);
160         tuner_i2c_xfer_send(&priv->i2c_props, if_agc_spd, 2);
161         if (priv->tda8290_easy_mode & 0x60)
162                 tuner_i2c_xfer_send(&priv->i2c_props, adc_head_9, 2);
163         else
164                 tuner_i2c_xfer_send(&priv->i2c_props, adc_head_6, 2);
165         tuner_i2c_xfer_send(&priv->i2c_props, pll_bw_nom, 2);
166
167         tda8290_i2c_bridge(t, 1);
168
169         if (t->fe.ops.tuner_ops.set_analog_params)
170                 t->fe.ops.tuner_ops.set_analog_params(&t->fe, &params);
171
172         for (i = 0; i < 3; i++) {
173                 tuner_i2c_xfer_send(&priv->i2c_props, &addr_pll_stat, 1);
174                 tuner_i2c_xfer_recv(&priv->i2c_props, &pll_stat, 1);
175                 if (pll_stat & 0x80) {
176                         tuner_i2c_xfer_send(&priv->i2c_props, &addr_adc_sat, 1);
177                         tuner_i2c_xfer_recv(&priv->i2c_props, &adc_sat, 1);
178                         tuner_i2c_xfer_send(&priv->i2c_props, &addr_agc_stat, 1);
179                         tuner_i2c_xfer_recv(&priv->i2c_props, &agc_stat, 1);
180                         tuner_dbg("tda8290 is locked, AGC: %d\n", agc_stat);
181                         break;
182                 } else {
183                         tuner_dbg("tda8290 not locked, no signal?\n");
184                         msleep(100);
185                 }
186         }
187         /* adjust headroom resp. gain */
188         if ((agc_stat > 115) || (!(pll_stat & 0x80) && (adc_sat < 20))) {
189                 tuner_dbg("adjust gain, step 1. Agc: %d, ADC stat: %d, lock: %d\n",
190                            agc_stat, adc_sat, pll_stat & 0x80);
191                 tuner_i2c_xfer_send(&priv->i2c_props, gainset_2, 2);
192                 msleep(100);
193                 tuner_i2c_xfer_send(&priv->i2c_props, &addr_agc_stat, 1);
194                 tuner_i2c_xfer_recv(&priv->i2c_props, &agc_stat, 1);
195                 tuner_i2c_xfer_send(&priv->i2c_props, &addr_pll_stat, 1);
196                 tuner_i2c_xfer_recv(&priv->i2c_props, &pll_stat, 1);
197                 if ((agc_stat > 115) || !(pll_stat & 0x80)) {
198                         tuner_dbg("adjust gain, step 2. Agc: %d, lock: %d\n",
199                                    agc_stat, pll_stat & 0x80);
200                         if (priv->cfg.agcf)
201                                 priv->cfg.agcf(&t->fe);
202                         msleep(100);
203                         tuner_i2c_xfer_send(&priv->i2c_props, &addr_agc_stat, 1);
204                         tuner_i2c_xfer_recv(&priv->i2c_props, &agc_stat, 1);
205                         tuner_i2c_xfer_send(&priv->i2c_props, &addr_pll_stat, 1);
206                         tuner_i2c_xfer_recv(&priv->i2c_props, &pll_stat, 1);
207                         if((agc_stat > 115) || !(pll_stat & 0x80)) {
208                                 tuner_dbg("adjust gain, step 3. Agc: %d\n", agc_stat);
209                                 tuner_i2c_xfer_send(&priv->i2c_props, adc_head_12, 2);
210                                 tuner_i2c_xfer_send(&priv->i2c_props, pll_bw_low, 2);
211                                 msleep(100);
212                         }
213                 }
214         }
215
216         /* l/ l' deadlock? */
217         if(priv->tda8290_easy_mode & 0x60) {
218                 tuner_i2c_xfer_send(&priv->i2c_props, &addr_adc_sat, 1);
219                 tuner_i2c_xfer_recv(&priv->i2c_props, &adc_sat, 1);
220                 tuner_i2c_xfer_send(&priv->i2c_props, &addr_pll_stat, 1);
221                 tuner_i2c_xfer_recv(&priv->i2c_props, &pll_stat, 1);
222                 if ((adc_sat > 20) || !(pll_stat & 0x80)) {
223                         tuner_dbg("trying to resolve SECAM L deadlock\n");
224                         tuner_i2c_xfer_send(&priv->i2c_props, agc_rst_on, 2);
225                         msleep(40);
226                         tuner_i2c_xfer_send(&priv->i2c_props, agc_rst_off, 2);
227                 }
228         }
229
230         tda8290_i2c_bridge(t, 0);
231         tuner_i2c_xfer_send(&priv->i2c_props, if_agc_set, 2);
232 }
233
234 /*---------------------------------------------------------------------*/
235
236 static int tda8290_has_signal(struct tuner *t)
237 {
238         struct tda8290_priv *priv = t->priv;
239
240         unsigned char i2c_get_afc[1] = { 0x1B };
241         unsigned char afc = 0;
242
243         tuner_i2c_xfer_send(&priv->i2c_props, i2c_get_afc, ARRAY_SIZE(i2c_get_afc));
244         tuner_i2c_xfer_recv(&priv->i2c_props, &afc, 1);
245         return (afc & 0x80)? 65535:0;
246 }
247
248 /*---------------------------------------------------------------------*/
249
250 static void tda8290_standby(struct tuner *t)
251 {
252         struct tda8290_priv *priv = t->priv;
253         unsigned char cb1[] = { 0x30, 0xD0 };
254         unsigned char tda8290_standby[] = { 0x00, 0x02 };
255         unsigned char tda8290_agc_tri[] = { 0x02, 0x20 };
256         struct i2c_msg msg = {.addr = priv->tda827x_addr, .flags=0, .buf=cb1, .len = 2};
257
258         tda8290_i2c_bridge(t, 1);
259         if (priv->tda827x_ver != 0)
260                 cb1[1] = 0x90;
261         i2c_transfer(priv->i2c_props.adap, &msg, 1);
262         tda8290_i2c_bridge(t, 0);
263         tuner_i2c_xfer_send(&priv->i2c_props, tda8290_agc_tri, 2);
264         tuner_i2c_xfer_send(&priv->i2c_props, tda8290_standby, 2);
265 }
266
267 static void tda8290_init_if(struct tuner *t)
268 {
269         struct tda8290_priv *priv = t->priv;
270
271         unsigned char set_VS[] = { 0x30, 0x6F };
272         unsigned char set_GP00_CF[] = { 0x20, 0x01 };
273         unsigned char set_GP01_CF[] = { 0x20, 0x0B };
274
275         if ((t->config == 1) || (t->config == 2))
276                 tuner_i2c_xfer_send(&priv->i2c_props, set_GP00_CF, 2);
277         else
278                 tuner_i2c_xfer_send(&priv->i2c_props, set_GP01_CF, 2);
279         tuner_i2c_xfer_send(&priv->i2c_props, set_VS, 2);
280 }
281
282 static void tda8290_init_tuner(struct tuner *t)
283 {
284         struct tda8290_priv *priv = t->priv;
285         unsigned char tda8275_init[]  = { 0x00, 0x00, 0x00, 0x40, 0xdC, 0x04, 0xAf,
286                                           0x3F, 0x2A, 0x04, 0xFF, 0x00, 0x00, 0x40 };
287         unsigned char tda8275a_init[] = { 0x00, 0x00, 0x00, 0x00, 0xdC, 0x05, 0x8b,
288                                           0x0c, 0x04, 0x20, 0xFF, 0x00, 0x00, 0x4b };
289         struct i2c_msg msg = {.addr = priv->tda827x_addr, .flags=0,
290                               .buf=tda8275_init, .len = 14};
291         if (priv->tda827x_ver != 0)
292                 msg.buf = tda8275a_init;
293
294         tda8290_i2c_bridge(t, 1);
295         i2c_transfer(priv->i2c_props.adap, &msg, 1);
296         tda8290_i2c_bridge(t, 0);
297 }
298
299 /*---------------------------------------------------------------------*/
300
301 static void tda8290_release(struct tuner *t)
302 {
303         if (t->fe.ops.tuner_ops.release)
304                 t->fe.ops.tuner_ops.release(&t->fe);
305
306         kfree(t->priv);
307         t->priv = NULL;
308 }
309
310 static struct tuner_operations tda8290_tuner_ops = {
311         .set_tv_freq    = tda8290_set_freq,
312         .set_radio_freq = tda8290_set_freq,
313         .has_signal     = tda8290_has_signal,
314         .standby        = tda8290_standby,
315         .release        = tda8290_release,
316 };
317
318 int tda8290_attach(struct tuner *t)
319 {
320         struct tda8290_priv *priv = NULL;
321         u8 data;
322         int i, ret, tuners_found;
323         u32 tuner_addrs;
324         struct i2c_msg msg = {.flags=I2C_M_RD, .buf=&data, .len = 1};
325
326         priv = kzalloc(sizeof(struct tda8290_priv), GFP_KERNEL);
327         if (priv == NULL)
328                 return -ENOMEM;
329         t->priv = priv;
330
331         priv->i2c_props.addr     = t->i2c.addr;
332         priv->i2c_props.adap     = t->i2c.adapter;
333         priv->cfg.config         = &t->config;
334         priv->cfg.tuner_callback = t->tuner_callback;
335
336         tda8290_i2c_bridge(t, 1);
337         /* probe for tuner chip */
338         tuners_found = 0;
339         tuner_addrs = 0;
340         for (i=0x60; i<= 0x63; i++) {
341                 msg.addr = i;
342                 ret = i2c_transfer(priv->i2c_props.adap, &msg, 1);
343                 if (ret == 1) {
344                         tuners_found++;
345                         tuner_addrs = (tuner_addrs << 8) + i;
346                 }
347         }
348         /* if there is more than one tuner, we expect the right one is
349            behind the bridge and we choose the highest address that doesn't
350            give a response now
351          */
352         tda8290_i2c_bridge(t, 0);
353         if(tuners_found > 1)
354                 for (i = 0; i < tuners_found; i++) {
355                         msg.addr = tuner_addrs  & 0xff;
356                         ret = i2c_transfer(priv->i2c_props.adap, &msg, 1);
357                         if(ret == 1)
358                                 tuner_addrs = tuner_addrs >> 8;
359                         else
360                                 break;
361                 }
362         if (tuner_addrs == 0) {
363                 tuner_addrs = 0x61;
364                 tuner_info("could not clearly identify tuner address, defaulting to %x\n",
365                              tuner_addrs);
366         } else {
367                 tuner_addrs = tuner_addrs & 0xff;
368                 tuner_info("setting tuner address to %x\n", tuner_addrs);
369         }
370         priv->tda827x_addr = tuner_addrs;
371         msg.addr = tuner_addrs;
372
373         tda8290_i2c_bridge(t, 1);
374
375         ret = i2c_transfer(priv->i2c_props.adap, &msg, 1);
376         if( ret != 1)
377                 tuner_warn("TDA827x access failed!\n");
378
379         if ((data & 0x3c) == 0) {
380                 strlcpy(t->i2c.name, "tda8290+75", sizeof(t->i2c.name));
381                 priv->tda827x_ver = 0;
382         } else {
383                 strlcpy(t->i2c.name, "tda8290+75a", sizeof(t->i2c.name));
384                 priv->tda827x_ver = 2;
385         }
386         tda827x_attach(&t->fe, priv->tda827x_addr,
387                        priv->i2c_props.adap, &priv->cfg);
388
389         /* FIXME: tda827x module doesn't probe the tuner until
390          * tda827x_initial_sleep is called
391          */
392         if (t->fe.ops.tuner_ops.sleep)
393                 t->fe.ops.tuner_ops.sleep(&t->fe);
394
395         memcpy(&t->ops, &tda8290_tuner_ops, sizeof(struct tuner_operations));
396
397         tuner_info("type set to %s\n", t->i2c.name);
398
399         priv->cfg.tda827x_lpsel = 0;
400         t->mode = V4L2_TUNER_ANALOG_TV;
401
402         tda8290_init_tuner(t);
403         tda8290_init_if(t);
404         return 0;
405 }
406
407 int tda8290_probe(struct tuner *t)
408 {
409         struct tuner_i2c_props i2c_props = {
410                 .adap = t->i2c.adapter,
411                 .addr = t->i2c.addr
412         };
413
414         unsigned char soft_reset[]   = { 0x00, 0x00 };
415         unsigned char easy_mode_b[]  = { 0x01, 0x02 };
416         unsigned char easy_mode_g[]  = { 0x01, 0x04 };
417         unsigned char restore_9886[] = { 0x00, 0xd6, 0x30 };
418         unsigned char addr_dto_lsb = 0x07;
419         unsigned char data;
420
421         tuner_i2c_xfer_send(&i2c_props, easy_mode_b, 2);
422         tuner_i2c_xfer_send(&i2c_props, soft_reset, 2);
423         tuner_i2c_xfer_send(&i2c_props, &addr_dto_lsb, 1);
424         tuner_i2c_xfer_recv(&i2c_props, &data, 1);
425         if (data == 0) {
426                 tuner_i2c_xfer_send(&i2c_props, easy_mode_g, 2);
427                 tuner_i2c_xfer_send(&i2c_props, soft_reset, 2);
428                 tuner_i2c_xfer_send(&i2c_props, &addr_dto_lsb, 1);
429                 tuner_i2c_xfer_recv(&i2c_props, &data, 1);
430                 if (data == 0x7b) {
431                         return 0;
432                 }
433         }
434         tuner_i2c_xfer_send(&i2c_props, restore_9886, 3);
435         return -1;
436 }
437
438 EXPORT_SYMBOL_GPL(tda8290_probe);
439 EXPORT_SYMBOL_GPL(tda8290_attach);
440
441 MODULE_DESCRIPTION("Philips TDA8290 + TDA8275 / TDA8275a tuner driver");
442 MODULE_AUTHOR("Gerd Knorr, Hartmut Hackmann");
443 MODULE_LICENSE("GPL");
444
445 /*
446  * Overrides for Emacs so that we follow Linus's tabbing style.
447  * ---------------------------------------------------------------------------
448  * Local variables:
449  * c-basic-offset: 8
450  * End:
451  */