This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / drivers / media / dvb / frontends / tua6100.c
1 /**
2  * Driver for Infineon tua6100 pll.
3  *
4  * (c) 2006 Andrew de Quincey
5  *
6  * Based on code found in budget-av.c, which has the following:
7  * Compiled from various sources by Michael Hunold <michael@mihu.de>
8  *
9  * CI interface support (c) 2004 Olivier Gournet <ogournet@anevia.com> &
10  *                               Andrew de Quincey <adq_dvb@lidskialf.net>
11  *
12  * Copyright (C) 2002 Ralph Metzler <rjkm@metzlerbros.de>
13  *
14  * Copyright (C) 1999-2002 Ralph  Metzler
15  *                       & Marcus Metzler for convergence integrated media GmbH
16  * This program is free software; you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License as published by
18  * the Free Software Foundation; either version 2 of the License, or
19  * (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with this program; if not, write to the Free Software
28  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29  */
30
31 #include <linux/module.h>
32 #include <linux/dvb/frontend.h>
33 #include <asm/types.h>
34
35 #include "tua6100.h"
36
37 struct tua6100_priv {
38         /* i2c details */
39         int i2c_address;
40         struct i2c_adapter *i2c;
41         u32 frequency;
42 };
43
44 static int tua6100_release(struct dvb_frontend *fe)
45 {
46         kfree(fe->tuner_priv);
47         fe->tuner_priv = NULL;
48         return 0;
49 }
50
51 static int tua6100_sleep(struct dvb_frontend *fe)
52 {
53         struct tua6100_priv *priv = fe->tuner_priv;
54         int ret;
55         u8 reg0[] = { 0x00, 0x00 };
56         struct i2c_msg msg = { .addr = priv->i2c_address, .flags = 0, .buf = reg0, .len = 2 };
57
58         if (fe->ops.i2c_gate_ctrl)
59                 fe->ops.i2c_gate_ctrl(fe, 1);
60         if ((ret = i2c_transfer (priv->i2c, &msg, 1)) != 1) {
61                 printk("%s: i2c error\n", __FUNCTION__);
62         }
63         if (fe->ops.i2c_gate_ctrl)
64                 fe->ops.i2c_gate_ctrl(fe, 0);
65
66         return (ret == 1) ? 0 : ret;
67 }
68
69 static int tua6100_set_params(struct dvb_frontend *fe,
70                               struct dvb_frontend_parameters *params)
71 {
72         struct tua6100_priv *priv = fe->tuner_priv;
73         u32 div;
74         u32 prediv;
75         u8 reg0[] = { 0x00, 0x00 };
76         u8 reg1[] = { 0x01, 0x00, 0x00, 0x00 };
77         u8 reg2[] = { 0x02, 0x00, 0x00 };
78         struct i2c_msg msg0 = { .addr = priv->i2c_address, .flags = 0, .buf = reg0, .len = 2 };
79         struct i2c_msg msg1 = { .addr = priv->i2c_address, .flags = 0, .buf = reg1, .len = 4 };
80         struct i2c_msg msg2 = { .addr = priv->i2c_address, .flags = 0, .buf = reg2, .len = 3 };
81
82 #define _R 4
83 #define _P 32
84 #define _ri 4000000
85
86         // setup register 0
87         if (params->frequency < 2000000) {
88                 reg0[1] = 0x03;
89         } else {
90                 reg0[1] = 0x07;
91         }
92
93         // setup register 1
94         if (params->frequency < 1630000) {
95                 reg1[1] = 0x2c;
96         } else {
97                 reg1[1] = 0x0c;
98         }
99         if (_P == 64)
100                 reg1[1] |= 0x40;
101         if (params->frequency >= 1525000)
102                 reg1[1] |= 0x80;
103
104         // register 2
105         reg2[1] = (_R >> 8) & 0x03;
106         reg2[2] = _R;
107         if (params->frequency < 1455000) {
108                 reg2[1] |= 0x1c;
109         } else if (params->frequency < 1630000) {
110                 reg2[1] |= 0x0c;
111         } else {
112                 reg2[1] |= 0x1c;
113         }
114
115         // The N divisor ratio (note: params->frequency is in kHz, but we need it in Hz)
116         prediv = (params->frequency * _R) / (_ri / 1000);
117         div = prediv / _P;
118         reg1[1] |= (div >> 9) & 0x03;
119         reg1[2] = div >> 1;
120         reg1[3] = (div << 7);
121         priv->frequency = ((div * _P) * (_ri / 1000)) / _R;
122
123         // Finally, calculate and store the value for A
124         reg1[3] |= (prediv - (div*_P)) & 0x7f;
125
126 #undef _R
127 #undef _P
128 #undef _ri
129
130         if (fe->ops.i2c_gate_ctrl)
131                 fe->ops.i2c_gate_ctrl(fe, 1);
132         if (i2c_transfer(priv->i2c, &msg0, 1) != 1)
133                 return -EIO;
134
135         if (fe->ops.i2c_gate_ctrl)
136                 fe->ops.i2c_gate_ctrl(fe, 1);
137         if (i2c_transfer(priv->i2c, &msg2, 1) != 1)
138                 return -EIO;
139
140         if (fe->ops.i2c_gate_ctrl)
141                 fe->ops.i2c_gate_ctrl(fe, 1);
142         if (i2c_transfer(priv->i2c, &msg1, 1) != 1)
143                 return -EIO;
144
145         if (fe->ops.i2c_gate_ctrl)
146                 fe->ops.i2c_gate_ctrl(fe, 0);
147
148         return 0;
149 }
150
151 static int tua6100_get_frequency(struct dvb_frontend *fe, u32 *frequency)
152 {
153         struct tua6100_priv *priv = fe->tuner_priv;
154         *frequency = priv->frequency;
155         return 0;
156 }
157
158 static struct dvb_tuner_ops tua6100_tuner_ops = {
159         .info = {
160                 .name = "Infineon TUA6100",
161                 .frequency_min = 950000,
162                 .frequency_max = 2150000,
163                 .frequency_step = 1000,
164         },
165         .release = tua6100_release,
166         .sleep = tua6100_sleep,
167         .set_params = tua6100_set_params,
168         .get_frequency = tua6100_get_frequency,
169 };
170
171 struct dvb_frontend *tua6100_attach(struct dvb_frontend *fe, int addr, struct i2c_adapter *i2c)
172 {
173         struct tua6100_priv *priv = NULL;
174         u8 b1 [] = { 0x80 };
175         u8 b2 [] = { 0x00 };
176         struct i2c_msg msg [] = { { .addr = addr, .flags = 0, .buf = b1, .len = 1 },
177                                   { .addr = addr, .flags = I2C_M_RD, .buf = b2, .len = 1 } };
178         int ret;
179
180         if (fe->ops.i2c_gate_ctrl)
181                 fe->ops.i2c_gate_ctrl(fe, 1);
182         ret = i2c_transfer (i2c, msg, 2);
183         if (fe->ops.i2c_gate_ctrl)
184                 fe->ops.i2c_gate_ctrl(fe, 0);
185
186         if (ret != 2)
187                 return NULL;
188
189         priv = kzalloc(sizeof(struct tua6100_priv), GFP_KERNEL);
190         if (priv == NULL)
191                 return NULL;
192
193         priv->i2c_address = addr;
194         priv->i2c = i2c;
195
196         memcpy(&fe->ops.tuner_ops, &tua6100_tuner_ops, sizeof(struct dvb_tuner_ops));
197         fe->tuner_priv = priv;
198         return fe;
199 }
200 EXPORT_SYMBOL(tua6100_attach);
201
202 MODULE_DESCRIPTION("DVB tua6100 driver");
203 MODULE_AUTHOR("Andrew de Quincey");
204 MODULE_LICENSE("GPL");