2 TDA8261 8PSK/QPSK tuner driver
3 Copyright (C) Manu Abraham (abraham.manu@gmail.com)
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.
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.
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.
21 #include <linux/init.h>
22 #include <linux/kernel.h>
23 #include <linux/module.h>
25 #include "dvb_frontend.h"
28 struct tda8261_state {
29 struct dvb_frontend *fe;
30 struct i2c_adapter *i2c;
31 struct tda8261_config *config;
38 static int tda8261_read(struct tda8261_state *state, u8 *buf)
40 struct tda8261_config *config = state->config;
42 struct i2c_msg msg[] = {
43 { .addr = config->addr, .flags = 0, .buf = NULL, .len = 0 },
44 { .addr = config->addr, .flags = I2C_M_RD,.buf = buf, .len = 1 }
47 if ((err = i2c_transfer(state->i2c, msg, 2)) != 2)
48 printk("%s: read error, err=%d\n", __func__, err);
53 static int tda8261_write(struct tda8261_state *state, u8 *buf)
55 struct tda8261_config *config = state->config;
57 struct i2c_msg msg = { .addr = config->addr, .flags = 0, .buf = buf, .len = 4 };
59 if ((err = i2c_transfer(state->i2c, &msg, 1)) != 1)
60 printk("%s: read error, err=%d\n", __func__, err);
65 static int tda8261_get_status(struct dvb_frontend *fe, u32 *status)
67 struct tda8261_state *state = fe->tuner_priv;
71 if ((err = tda8261_read(state, &result)) < 0) {
72 printk("%s: I/O Error\n", __func__);
75 if ((result >> 6) & 0x01) {
76 printk("%s: Tuner Phase Locked\n", __func__);
83 static const u32 div_tab[] = { 2000, 1000, 500, 250, 125 }; /* kHz */
84 static const u8 ref_div[] = { 0x00, 0x01, 0x02, 0x05, 0x07 };
86 static int tda8261_get_state(struct dvb_frontend *fe,
87 enum tuner_param param,
88 struct tuner_state *tstate)
90 struct tda8261_state *state = fe->tuner_priv;
94 case DVBFE_TUNER_FREQUENCY:
95 tstate->frequency = state->frequency;
97 case DVBFE_TUNER_BANDWIDTH:
98 tstate->bandwidth = 60000000; /* FIXME! need to calculate Bandwidth */
101 printk("%s: Unknown parameter (param=%d)\n", __func__, param);
109 static int tda8261_set_state(struct dvb_frontend *fe,
110 enum tuner_param param,
111 struct tuner_state *tstate)
113 struct tda8261_state *state = fe->tuner_priv;
114 struct tda8261_config *config = state->config;
115 u32 frequency, N, status = 0;
119 if (param & DVBFE_TUNER_FREQUENCY) {
121 * N = Max VCO Frequency / Channel Spacing
122 * Max VCO Frequency = VCO frequency + (channel spacing - 1)
123 * (to account for half channel spacing on either side)
125 frequency = tstate->frequency;
126 N = (frequency + (div_tab[config->step_size] - 1)) / div_tab[config->step_size];
127 buf[0] = (N >> 8) & 0xff;
129 buf[2] = (0x08 << 4) | ((ref_div[config->step_size] & 0x07) << 1);
132 printk("%s: Frequency=%d, Sending[ %02x %02x %02x %02x ]\n",
133 __func__, frequency, buf[0], buf[1], buf[2], buf[3]);
135 if ((err = tda8261_write(state, buf)) < 0) {
136 printk("%s: I/O Error\n", __func__);
139 /* sleep for some time */
142 if ((err = tda8261_get_status(fe, &status)) < 0) {
143 printk("%s: I/O Error\n", __func__);
147 printk("%s: Tuner Phase locked: status=%d\n", __func__, status);
149 printk("%s: No Phase lock: status=%d\n", __func__, status);
151 printk("%s: Unknown parameter (param=%d)\n", __func__, param);
158 static int tda8261_release(struct dvb_frontend *fe)
160 struct tda8261_state *state = fe->tuner_priv;
162 fe->tuner_priv = NULL;
167 static struct dvb_tuner_ops tda8261_ops = {
171 // .tuner_name = NULL,
172 .frequency_min = 950000,
173 .frequency_max = 2150000,
177 .set_state = tda8261_set_state,
178 .get_state = tda8261_get_state,
179 .release = tda8261_release
182 struct dvb_frontend *tda8261_attach(struct dvb_frontend *fe,
183 struct tda8261_config *config,
184 struct i2c_adapter *i2c)
186 struct tda8261_state *state = NULL;
188 if ((state = kzalloc(sizeof (struct tda8261_state), GFP_KERNEL)) == NULL)
191 state->config = config;
194 fe->tuner_priv = state;
195 fe->ops.tuner_ops = tda8261_ops;
197 fe->ops.tuner_ops.info.frequency_step = div_tab[config->step_size];
198 // fe->ops.tuner_ops.tuner_name = &config->buf;
200 // printk("%s: Attaching %s TDA8261 8PSK/QPSK tuner\n",
201 // __func__, fe->ops.tuner_ops.tuner_name);
202 printk("%s: Attaching TDA8261 8PSK/QPSK tuner\n", __func__);
212 EXPORT_SYMBOL(tda8261_attach);
213 MODULE_PARM_DESC(verbose, "Set verbosity level");
215 MODULE_AUTHOR("Manu Abraham");
216 MODULE_DESCRIPTION("TDA8261 8PSK/QPSK Tuner");
217 MODULE_LICENSE("GPL");