blob: a6cd2b362b6e2be4010bd5daa96dfb538a07d29e [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
76 if ((err = tda8261_read(state, &result)) < 0) {
77 printk("%s: I/O Error\n", __func__);
78 return err;
79 }
80 if ((result >> 6) & 0x01) {
81 printk("%s: Tuner Phase Locked\n", __func__);
82 *status = 1;
83 }
84
85 return err;
86}
87
88static const u32 div_tab[] = { 2000, 1000, 500, 250, 125 }; /* kHz */
89static const u8 ref_div[] = { 0x00, 0x01, 0x02, 0x05, 0x07 };
90
91static int tda8261_get_state(struct dvb_frontend *fe,
92 enum tuner_param param,
93 struct tuner_state *tstate)
94{
95 struct tda8261_state *state = fe->tuner_priv;
96 int err = 0;
97
98 switch (param) {
99 case DVBFE_TUNER_FREQUENCY:
100 tstate->frequency = state->frequency;
101 break;
102 case DVBFE_TUNER_BANDWIDTH:
103 tstate->bandwidth = 60000000; /* FIXME! need to calculate Bandwidth */
104 break;
105 default:
106 printk("%s: Unknown parameter (param=%d)\n", __func__, param);
107 err = -EINVAL;
108 break;
109 }
110
111 return err;
112}
113
114static int tda8261_set_state(struct dvb_frontend *fe,
115 enum tuner_param param,
116 struct tuner_state *tstate)
117{
118 struct tda8261_state *state = fe->tuner_priv;
Manu Abraham41e11512007-09-22 21:28:11 -0300119 const struct tda8261_config *config = state->config;
Manu Abraham00360202007-09-22 13:30:09 -0300120 u32 frequency, N, status = 0;
121 u8 buf[4];
122 int err = 0;
123
124 if (param & DVBFE_TUNER_FREQUENCY) {
125 /**
126 * N = Max VCO Frequency / Channel Spacing
127 * Max VCO Frequency = VCO frequency + (channel spacing - 1)
128 * (to account for half channel spacing on either side)
129 */
130 frequency = tstate->frequency;
131 N = (frequency + (div_tab[config->step_size] - 1)) / div_tab[config->step_size];
Manu Abrahamc7d85a22007-09-24 13:27:06 -0300132 printk("%s: Step size=%d, Divider=%d, PG=0x%02x (%d)\n",
133 __func__, config->step_size, div_tab[config->step_size], N, N);
134
Manu Abraham00360202007-09-22 13:30:09 -0300135 buf[0] = (N >> 8) & 0xff;
136 buf[1] = N & 0xff;
Manu Abrahamc7d85a22007-09-24 13:27:06 -0300137 buf[2] = (0x01 << 7) | ((ref_div[config->step_size] & 0x07) << 1);
Manu Abraham00360202007-09-22 13:30:09 -0300138 buf[3] = 0xc0;
139 /* Set params */
Manu Abraham00360202007-09-22 13:30:09 -0300140 if ((err = tda8261_write(state, buf)) < 0) {
141 printk("%s: I/O Error\n", __func__);
142 return err;
143 }
144 /* sleep for some time */
Manu Abrahamc7d85a22007-09-24 13:27:06 -0300145 printk("%s: Waiting to Phase LOCK\n", __func__);
146 msleep(20);
Manu Abraham00360202007-09-22 13:30:09 -0300147 /* check status */
148 if ((err = tda8261_get_status(fe, &status)) < 0) {
149 printk("%s: I/O Error\n", __func__);
150 return err;
151 }
Manu Abraham0b8f15d2007-09-22 13:36:34 -0300152 if (status == 1) {
Manu Abraham00360202007-09-22 13:30:09 -0300153 printk("%s: Tuner Phase locked: status=%d\n", __func__, status);
Manu Abrahamc7d85a22007-09-24 13:27:06 -0300154 state->frequency = frequency; /* cache successful state */
Manu Abraham0b8f15d2007-09-22 13:36:34 -0300155 } else {
Manu Abraham00360202007-09-22 13:30:09 -0300156 printk("%s: No Phase lock: status=%d\n", __func__, status);
Manu Abraham0b8f15d2007-09-22 13:36:34 -0300157 }
Manu Abraham00360202007-09-22 13:30:09 -0300158 } else {
159 printk("%s: Unknown parameter (param=%d)\n", __func__, param);
160 return -EINVAL;
161 }
162
163 return 0;
164}
165
166static int tda8261_release(struct dvb_frontend *fe)
167{
168 struct tda8261_state *state = fe->tuner_priv;
169
170 fe->tuner_priv = NULL;
171 kfree(state);
172 return 0;
173}
174
175static struct dvb_tuner_ops tda8261_ops = {
176
177 .info = {
178 .name = "TDA8261",
179// .tuner_name = NULL,
180 .frequency_min = 950000,
181 .frequency_max = 2150000,
182 .frequency_step = 0
183 },
184
185 .set_state = tda8261_set_state,
186 .get_state = tda8261_get_state,
187 .release = tda8261_release
188};
189
190struct dvb_frontend *tda8261_attach(struct dvb_frontend *fe,
Manu Abraham41e11512007-09-22 21:28:11 -0300191 const struct tda8261_config *config,
Manu Abraham00360202007-09-22 13:30:09 -0300192 struct i2c_adapter *i2c)
193{
194 struct tda8261_state *state = NULL;
195
196 if ((state = kzalloc(sizeof (struct tda8261_state), GFP_KERNEL)) == NULL)
197 goto exit;
198
199 state->config = config;
200 state->i2c = i2c;
201 state->fe = fe;
202 fe->tuner_priv = state;
203 fe->ops.tuner_ops = tda8261_ops;
204
205 fe->ops.tuner_ops.info.frequency_step = div_tab[config->step_size];
206// fe->ops.tuner_ops.tuner_name = &config->buf;
207
208// printk("%s: Attaching %s TDA8261 8PSK/QPSK tuner\n",
209// __func__, fe->ops.tuner_ops.tuner_name);
210 printk("%s: Attaching TDA8261 8PSK/QPSK tuner\n", __func__);
211
Manu Abraham00360202007-09-22 13:30:09 -0300212 return fe;
213
214exit:
215 kfree(state);
216 return NULL;
217}
218
219EXPORT_SYMBOL(tda8261_attach);
220MODULE_PARM_DESC(verbose, "Set verbosity level");
221
222MODULE_AUTHOR("Manu Abraham");
223MODULE_DESCRIPTION("TDA8261 8PSK/QPSK Tuner");
224MODULE_LICENSE("GPL");