Merge branch 'fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/jlbec/ocfs2
[sfrench/cifs-2.6.git] / drivers / media / dvb / frontends / tda665x.c
1 /*
2         TDA665x tuner driver
3         Copyright (C) Manu Abraham (abraham.manu@gmail.com)
4
5         This program is free software; you can redistribute it and/or modify
6         it under the terms of the GNU General Public License as published by
7         the Free Software Foundation; either version 2 of the License, or
8         (at your option) any later version.
9
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.
14
15         You should have received a copy of the GNU General Public License
16         along with this program; if not, write to the Free Software
17         Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #include <linux/init.h>
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23
24 #include "dvb_frontend.h"
25 #include "tda665x.h"
26
27 struct tda665x_state {
28         struct dvb_frontend             *fe;
29         struct i2c_adapter              *i2c;
30         const struct tda665x_config     *config;
31
32         u32 frequency;
33         u32 bandwidth;
34 };
35
36 static int tda665x_read(struct tda665x_state *state, u8 *buf)
37 {
38         const struct tda665x_config *config = state->config;
39         int err = 0;
40         struct i2c_msg msg = { .addr = config->addr, .flags = I2C_M_RD, .buf = buf, .len = 2 };
41
42         err = i2c_transfer(state->i2c, &msg, 1);
43         if (err != 1)
44                 goto exit;
45
46         return err;
47 exit:
48         printk(KERN_ERR "%s: I/O Error err=<%d>\n", __func__, err);
49         return err;
50 }
51
52 static int tda665x_write(struct tda665x_state *state, u8 *buf, u8 length)
53 {
54         const struct tda665x_config *config = state->config;
55         int err = 0;
56         struct i2c_msg msg = { .addr = config->addr, .flags = 0, .buf = buf, .len = length };
57
58         err = i2c_transfer(state->i2c, &msg, 1);
59         if (err != 1)
60                 goto exit;
61
62         return err;
63 exit:
64         printk(KERN_ERR "%s: I/O Error err=<%d>\n", __func__, err);
65         return err;
66 }
67
68 static int tda665x_get_state(struct dvb_frontend *fe,
69                              enum tuner_param param,
70                              struct tuner_state *tstate)
71 {
72         struct tda665x_state *state = fe->tuner_priv;
73         int err = 0;
74
75         switch (param) {
76         case DVBFE_TUNER_FREQUENCY:
77                 tstate->frequency = state->frequency;
78                 break;
79         case DVBFE_TUNER_BANDWIDTH:
80                 break;
81         default:
82                 printk(KERN_ERR "%s: Unknown parameter (param=%d)\n", __func__, param);
83                 err = -EINVAL;
84                 break;
85         }
86
87         return err;
88 }
89
90 static int tda665x_get_status(struct dvb_frontend *fe, u32 *status)
91 {
92         struct tda665x_state *state = fe->tuner_priv;
93         u8 result = 0;
94         int err = 0;
95
96         *status = 0;
97
98         err = tda665x_read(state, &result);
99         if (err < 0)
100                 goto exit;
101
102         if ((result >> 6) & 0x01) {
103                 printk(KERN_DEBUG "%s: Tuner Phase Locked\n", __func__);
104                 *status = 1;
105         }
106
107         return err;
108 exit:
109         printk(KERN_ERR "%s: I/O Error\n", __func__);
110         return err;
111 }
112
113 static int tda665x_set_state(struct dvb_frontend *fe,
114                              enum tuner_param param,
115                              struct tuner_state *tstate)
116 {
117         struct tda665x_state *state = fe->tuner_priv;
118         const struct tda665x_config *config = state->config;
119         u32 frequency, status = 0;
120         u8 buf[4];
121         int err = 0;
122
123         if (param & DVBFE_TUNER_FREQUENCY) {
124
125                 frequency = tstate->frequency;
126                 if ((frequency < config->frequency_max) || (frequency > config->frequency_min)) {
127                         printk(KERN_ERR "%s: Frequency beyond limits, frequency=%d\n", __func__, frequency);
128                         return -EINVAL;
129                 }
130
131                 frequency += config->frequency_offst;
132                 frequency *= config->ref_multiplier;
133                 frequency += config->ref_divider >> 1;
134                 frequency /= config->ref_divider;
135
136                 buf[0] = (u8) (frequency & 0x7f00) >> 8;
137                 buf[1] = (u8) (frequency & 0x00ff) >> 0;
138                 buf[2] = 0x80 | 0x40 | 0x02;
139                 buf[3] = 0x00;
140
141                 /* restore frequency */
142                 frequency = tstate->frequency;
143
144                 if (frequency < 153000000) {
145                         /* VHF-L */
146                         buf[3] |= 0x01; /* fc, Low Band, 47 - 153 MHz */
147                         if (frequency < 68000000)
148                                 buf[3] |= 0x40; /* 83uA */
149                         if (frequency < 1040000000)
150                                 buf[3] |= 0x60; /* 122uA */
151                         if (frequency < 1250000000)
152                                 buf[3] |= 0x80; /* 163uA */
153                         else
154                                 buf[3] |= 0xa0; /* 254uA */
155                 } else if (frequency < 438000000) {
156                         /* VHF-H */
157                         buf[3] |= 0x02; /* fc, Mid Band, 153 - 438 MHz */
158                         if (frequency < 230000000)
159                                 buf[3] |= 0x40;
160                         if (frequency < 300000000)
161                                 buf[3] |= 0x60;
162                         else
163                                 buf[3] |= 0x80;
164                 } else {
165                         /* UHF */
166                         buf[3] |= 0x04; /* fc, High Band, 438 - 862 MHz */
167                         if (frequency < 470000000)
168                                 buf[3] |= 0x60;
169                         if (frequency < 526000000)
170                                 buf[3] |= 0x80;
171                         else
172                                 buf[3] |= 0xa0;
173                 }
174
175                 /* Set params */
176                 err = tda665x_write(state, buf, 5);
177                 if (err < 0)
178                         goto exit;
179
180                 /* sleep for some time */
181                 printk(KERN_DEBUG "%s: Waiting to Phase LOCK\n", __func__);
182                 msleep(20);
183                 /* check status */
184                 err = tda665x_get_status(fe, &status);
185                 if (err < 0)
186                         goto exit;
187
188                 if (status == 1) {
189                         printk(KERN_DEBUG "%s: Tuner Phase locked: status=%d\n", __func__, status);
190                         state->frequency = frequency; /* cache successful state */
191                 } else {
192                         printk(KERN_ERR "%s: No Phase lock: status=%d\n", __func__, status);
193                 }
194         } else {
195                 printk(KERN_ERR "%s: Unknown parameter (param=%d)\n", __func__, param);
196                 return -EINVAL;
197         }
198
199         return 0;
200 exit:
201         printk(KERN_ERR "%s: I/O Error\n", __func__);
202         return err;
203 }
204
205 static int tda665x_release(struct dvb_frontend *fe)
206 {
207         struct tda665x_state *state = fe->tuner_priv;
208
209         fe->tuner_priv = NULL;
210         kfree(state);
211         return 0;
212 }
213
214 static struct dvb_tuner_ops tda665x_ops = {
215
216         .set_state      = tda665x_set_state,
217         .get_state      = tda665x_get_state,
218         .get_status     = tda665x_get_status,
219         .release        = tda665x_release
220 };
221
222 struct dvb_frontend *tda665x_attach(struct dvb_frontend *fe,
223                                     const struct tda665x_config *config,
224                                     struct i2c_adapter *i2c)
225 {
226         struct tda665x_state *state = NULL;
227         struct dvb_tuner_info *info;
228
229         state = kzalloc(sizeof(struct tda665x_state), GFP_KERNEL);
230         if (state == NULL)
231                 goto exit;
232
233         state->config           = config;
234         state->i2c              = i2c;
235         state->fe               = fe;
236         fe->tuner_priv          = state;
237         fe->ops.tuner_ops       = tda665x_ops;
238         info                     = &fe->ops.tuner_ops.info;
239
240         memcpy(info->name, config->name, sizeof(config->name));
241         info->frequency_min     = config->frequency_min;
242         info->frequency_max     = config->frequency_max;
243         info->frequency_step    = config->frequency_offst;
244
245         printk(KERN_DEBUG "%s: Attaching TDA665x (%s) tuner\n", __func__, info->name);
246
247         return fe;
248
249 exit:
250         kfree(state);
251         return NULL;
252 }
253 EXPORT_SYMBOL(tda665x_attach);
254
255 MODULE_DESCRIPTION("TDA665x driver");
256 MODULE_AUTHOR("Manu Abraham");
257 MODULE_LICENSE("GPL");