blob: 1b9f4dc1b0032db8c1aec6c797d9659e448b9af5 [file] [log] [blame]
Manu Abraham00360202007-09-22 13:30:09 -03001/*
2 TDA8261 8PSK/QPSK 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
21#include <linux/init.h>
22#include <linux/kernel.h>
23#include <linux/module.h>
24
25#include "dvb_frontend.h"
26#include "tda8261.h"
27
28struct tda8261_state {
Manu Abraham41e11512007-09-22 21:28:11 -030029 struct dvb_frontend *fe;
30 struct i2c_adapter *i2c;
31 const struct tda8261_config *config;
Manu Abraham00360202007-09-22 13:30:09 -030032
33 /* state cache */
34 u32 frequency;
35 u32 bandwidth;
36};
37
38static int tda8261_read(struct tda8261_state *state, u8 *buf)
39{
Manu Abrahamc7d85a22007-09-24 13:27:06 -030040 struct dvb_frontend *fe = state->fe;
Manu Abraham41e11512007-09-22 21:28:11 -030041 const struct tda8261_config *config = state->config;
Manu Abraham00360202007-09-22 13:30:09 -030042 int err = 0;
Manu Abrahamc7d85a22007-09-24 13:27:06 -030043 struct i2c_msg msg = { .addr = config->addr, .flags = I2C_M_RD,.buf = buf, .len = 2 };
Manu Abraham00360202007-09-22 13:30:09 -030044
Manu Abrahamc7d85a22007-09-24 13:27:06 -030045 if (fe->ops.i2c_gate_ctrl)
46 fe->ops.i2c_gate_ctrl(fe, 1);
47
48 if ((err = i2c_transfer(state->i2c, &msg, 1)) != 1)
Manu Abraham00360202007-09-22 13:30:09 -030049 printk("%s: read error, err=%d\n", __func__, err);
50
51 return err;
52}
53
54static int tda8261_write(struct tda8261_state *state, u8 *buf)
55{
Manu Abrahamc7d85a22007-09-24 13:27:06 -030056 struct dvb_frontend *fe = state->fe;
Manu Abraham41e11512007-09-22 21:28:11 -030057 const struct tda8261_config *config = state->config;
Manu Abraham00360202007-09-22 13:30:09 -030058 int err = 0;
59 struct i2c_msg msg = { .addr = config->addr, .flags = 0, .buf = buf, .len = 4 };
60
Manu Abrahamc7d85a22007-09-24 13:27:06 -030061 if (fe->ops.i2c_gate_ctrl)
62 fe->ops.i2c_gate_ctrl(fe, 1);
63
Manu Abraham00360202007-09-22 13:30:09 -030064 if ((err = i2c_transfer(state->i2c, &msg, 1)) != 1)
Manu Abrahamc7d85a22007-09-24 13:27:06 -030065 printk("%s: write error, err=%d\n", __func__, err);
Manu Abraham00360202007-09-22 13:30:09 -030066
67 return err;
68}
69
70static int tda8261_get_status(struct dvb_frontend *fe, u32 *status)
71{
72 struct tda8261_state *state = fe->tuner_priv;
73 u8 result = 0;
74 int err = 0;
75
Manu Abrahamf6e63822007-09-28 19:06:06 -030076 *status = 0;
77
Manu Abraham00360202007-09-22 13:30:09 -030078 if ((err = tda8261_read(state, &result)) < 0) {
79 printk("%s: I/O Error\n", __func__);
80 return err;
81 }
82 if ((result >> 6) & 0x01) {
83 printk("%s: Tuner Phase Locked\n", __func__);
84 *status = 1;
85 }
86
87 return err;
88}
89
90static const u32 div_tab[] = { 2000, 1000, 500, 250, 125 }; /* kHz */
91static const u8 ref_div[] = { 0x00, 0x01, 0x02, 0x05, 0x07 };
92
93static int tda8261_get_state(struct dvb_frontend *fe,
94 enum tuner_param param,
95 struct tuner_state *tstate)
96{
97 struct tda8261_state *state = fe->tuner_priv;
98 int err = 0;
99
100 switch (param) {
101 case DVBFE_TUNER_FREQUENCY:
102 tstate->frequency = state->frequency;
103 break;
104 case DVBFE_TUNER_BANDWIDTH:
105 tstate->bandwidth = 60000000; /* FIXME! need to calculate Bandwidth */
106 break;
107 default:
108 printk("%s: Unknown parameter (param=%d)\n", __func__, param);
109 err = -EINVAL;
110 break;
111 }
112
113 return err;
114}
115
116static int tda8261_set_state(struct dvb_frontend *fe,
117 enum tuner_param param,
118 struct tuner_state *tstate)
119{
120 struct tda8261_state *state = fe->tuner_priv;
Manu Abraham41e11512007-09-22 21:28:11 -0300121 const struct tda8261_config *config = state->config;
Manu Abraham00360202007-09-22 13:30:09 -0300122 u32 frequency, N, status = 0;
123 u8 buf[4];
124 int err = 0;
125
126 if (param & DVBFE_TUNER_FREQUENCY) {
127 /**
128 * N = Max VCO Frequency / Channel Spacing
129 * Max VCO Frequency = VCO frequency + (channel spacing - 1)
130 * (to account for half channel spacing on either side)
131 */
132 frequency = tstate->frequency;
Manu Abrahamcf6efeb2007-10-08 19:04:03 -0300133 if ((frequency < 950000) || (frequency > 2150000)) {
134 printk("%s: Frequency beyond limits, frequency=%d\n", __func__, frequency);
135 return -EINVAL;
136 }
Manu Abraham00360202007-09-22 13:30:09 -0300137 N = (frequency + (div_tab[config->step_size] - 1)) / div_tab[config->step_size];
Manu Abrahamc7d85a22007-09-24 13:27:06 -0300138 printk("%s: Step size=%d, Divider=%d, PG=0x%02x (%d)\n",
139 __func__, config->step_size, div_tab[config->step_size], N, N);
140
Manu Abraham00360202007-09-22 13:30:09 -0300141 buf[0] = (N >> 8) & 0xff;
142 buf[1] = N & 0xff;
Manu Abrahamc7d85a22007-09-24 13:27:06 -0300143 buf[2] = (0x01 << 7) | ((ref_div[config->step_size] & 0x07) << 1);
Manu Abrahamcf6efeb2007-10-08 19:04:03 -0300144
145 if (frequency < 1450000)
146 buf[3] = 0x00;
147 if (frequency < 2000000)
148 buf[3] = 0x40;
149 if (frequency < 2150000)
150 buf[3] = 0x80;
151
Manu Abraham00360202007-09-22 13:30:09 -0300152 /* Set params */
Manu Abraham00360202007-09-22 13:30:09 -0300153 if ((err = tda8261_write(state, buf)) < 0) {
154 printk("%s: I/O Error\n", __func__);
155 return err;
156 }
157 /* sleep for some time */
Manu Abrahamc7d85a22007-09-24 13:27:06 -0300158 printk("%s: Waiting to Phase LOCK\n", __func__);
159 msleep(20);
Manu Abraham00360202007-09-22 13:30:09 -0300160 /* check status */
161 if ((err = tda8261_get_status(fe, &status)) < 0) {
162 printk("%s: I/O Error\n", __func__);
163 return err;
164 }
Manu Abraham0b8f15d2007-09-22 13:36:34 -0300165 if (status == 1) {
Manu Abraham00360202007-09-22 13:30:09 -0300166 printk("%s: Tuner Phase locked: status=%d\n", __func__, status);
Manu Abrahamc7d85a22007-09-24 13:27:06 -0300167 state->frequency = frequency; /* cache successful state */
Manu Abraham0b8f15d2007-09-22 13:36:34 -0300168 } else {
Manu Abraham00360202007-09-22 13:30:09 -0300169 printk("%s: No Phase lock: status=%d\n", __func__, status);
Manu Abraham0b8f15d2007-09-22 13:36:34 -0300170 }
Manu Abraham00360202007-09-22 13:30:09 -0300171 } else {
172 printk("%s: Unknown parameter (param=%d)\n", __func__, param);
173 return -EINVAL;
174 }
175
176 return 0;
177}
178
179static int tda8261_release(struct dvb_frontend *fe)
180{
181 struct tda8261_state *state = fe->tuner_priv;
182
183 fe->tuner_priv = NULL;
184 kfree(state);
185 return 0;
186}
187
188static struct dvb_tuner_ops tda8261_ops = {
189
190 .info = {
191 .name = "TDA8261",
192// .tuner_name = NULL,
193 .frequency_min = 950000,
194 .frequency_max = 2150000,
195 .frequency_step = 0
196 },
197
198 .set_state = tda8261_set_state,
199 .get_state = tda8261_get_state,
Manu Abrahamf6e63822007-09-28 19:06:06 -0300200 .get_status = tda8261_get_status,
Manu Abraham00360202007-09-22 13:30:09 -0300201 .release = tda8261_release
202};
203
204struct dvb_frontend *tda8261_attach(struct dvb_frontend *fe,
Manu Abraham41e11512007-09-22 21:28:11 -0300205 const struct tda8261_config *config,
Manu Abraham00360202007-09-22 13:30:09 -0300206 struct i2c_adapter *i2c)
207{
208 struct tda8261_state *state = NULL;
209
210 if ((state = kzalloc(sizeof (struct tda8261_state), GFP_KERNEL)) == NULL)
211 goto exit;
212
213 state->config = config;
214 state->i2c = i2c;
215 state->fe = fe;
216 fe->tuner_priv = state;
217 fe->ops.tuner_ops = tda8261_ops;
218
219 fe->ops.tuner_ops.info.frequency_step = div_tab[config->step_size];
220// fe->ops.tuner_ops.tuner_name = &config->buf;
221
222// printk("%s: Attaching %s TDA8261 8PSK/QPSK tuner\n",
223// __func__, fe->ops.tuner_ops.tuner_name);
224 printk("%s: Attaching TDA8261 8PSK/QPSK tuner\n", __func__);
225
Manu Abraham00360202007-09-22 13:30:09 -0300226 return fe;
227
228exit:
229 kfree(state);
230 return NULL;
231}
232
233EXPORT_SYMBOL(tda8261_attach);
234MODULE_PARM_DESC(verbose, "Set verbosity level");
235
236MODULE_AUTHOR("Manu Abraham");
237MODULE_DESCRIPTION("TDA8261 8PSK/QPSK Tuner");
238MODULE_LICENSE("GPL");