blob: 9cbd511d749e914338b1d20cfa6786fe163a2296 [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;
133 N = (frequency + (div_tab[config->step_size] - 1)) / div_tab[config->step_size];
Manu Abrahamc7d85a22007-09-24 13:27:06 -0300134 printk("%s: Step size=%d, Divider=%d, PG=0x%02x (%d)\n",
135 __func__, config->step_size, div_tab[config->step_size], N, N);
136
Manu Abraham00360202007-09-22 13:30:09 -0300137 buf[0] = (N >> 8) & 0xff;
138 buf[1] = N & 0xff;
Manu Abrahamc7d85a22007-09-24 13:27:06 -0300139 buf[2] = (0x01 << 7) | ((ref_div[config->step_size] & 0x07) << 1);
Manu Abraham00360202007-09-22 13:30:09 -0300140 buf[3] = 0xc0;
141 /* Set params */
Manu Abraham00360202007-09-22 13:30:09 -0300142 if ((err = tda8261_write(state, buf)) < 0) {
143 printk("%s: I/O Error\n", __func__);
144 return err;
145 }
146 /* sleep for some time */
Manu Abrahamc7d85a22007-09-24 13:27:06 -0300147 printk("%s: Waiting to Phase LOCK\n", __func__);
148 msleep(20);
Manu Abraham00360202007-09-22 13:30:09 -0300149 /* check status */
150 if ((err = tda8261_get_status(fe, &status)) < 0) {
151 printk("%s: I/O Error\n", __func__);
152 return err;
153 }
Manu Abraham0b8f15d2007-09-22 13:36:34 -0300154 if (status == 1) {
Manu Abraham00360202007-09-22 13:30:09 -0300155 printk("%s: Tuner Phase locked: status=%d\n", __func__, status);
Manu Abrahamc7d85a22007-09-24 13:27:06 -0300156 state->frequency = frequency; /* cache successful state */
Manu Abraham0b8f15d2007-09-22 13:36:34 -0300157 } else {
Manu Abraham00360202007-09-22 13:30:09 -0300158 printk("%s: No Phase lock: status=%d\n", __func__, status);
Manu Abraham0b8f15d2007-09-22 13:36:34 -0300159 }
Manu Abraham00360202007-09-22 13:30:09 -0300160 } else {
161 printk("%s: Unknown parameter (param=%d)\n", __func__, param);
162 return -EINVAL;
163 }
164
165 return 0;
166}
167
168static int tda8261_release(struct dvb_frontend *fe)
169{
170 struct tda8261_state *state = fe->tuner_priv;
171
172 fe->tuner_priv = NULL;
173 kfree(state);
174 return 0;
175}
176
177static struct dvb_tuner_ops tda8261_ops = {
178
179 .info = {
180 .name = "TDA8261",
181// .tuner_name = NULL,
182 .frequency_min = 950000,
183 .frequency_max = 2150000,
184 .frequency_step = 0
185 },
186
187 .set_state = tda8261_set_state,
188 .get_state = tda8261_get_state,
Manu Abrahamf6e63822007-09-28 19:06:06 -0300189 .get_status = tda8261_get_status,
Manu Abraham00360202007-09-22 13:30:09 -0300190 .release = tda8261_release
191};
192
193struct dvb_frontend *tda8261_attach(struct dvb_frontend *fe,
Manu Abraham41e11512007-09-22 21:28:11 -0300194 const struct tda8261_config *config,
Manu Abraham00360202007-09-22 13:30:09 -0300195 struct i2c_adapter *i2c)
196{
197 struct tda8261_state *state = NULL;
198
199 if ((state = kzalloc(sizeof (struct tda8261_state), GFP_KERNEL)) == NULL)
200 goto exit;
201
202 state->config = config;
203 state->i2c = i2c;
204 state->fe = fe;
205 fe->tuner_priv = state;
206 fe->ops.tuner_ops = tda8261_ops;
207
208 fe->ops.tuner_ops.info.frequency_step = div_tab[config->step_size];
209// fe->ops.tuner_ops.tuner_name = &config->buf;
210
211// printk("%s: Attaching %s TDA8261 8PSK/QPSK tuner\n",
212// __func__, fe->ops.tuner_ops.tuner_name);
213 printk("%s: Attaching TDA8261 8PSK/QPSK tuner\n", __func__);
214
Manu Abraham00360202007-09-22 13:30:09 -0300215 return fe;
216
217exit:
218 kfree(state);
219 return NULL;
220}
221
222EXPORT_SYMBOL(tda8261_attach);
223MODULE_PARM_DESC(verbose, "Set verbosity level");
224
225MODULE_AUTHOR("Manu Abraham");
226MODULE_DESCRIPTION("TDA8261 8PSK/QPSK Tuner");
227MODULE_LICENSE("GPL");