blob: 7ca965987f40ac10d3f3acdb48e88557e46a0290 [file] [log] [blame]
Manu Abraham3e978a82009-12-04 05:56:35 -03001/*
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Manu Abraham3e978a82009-12-04 05:56:35 -030024
25#include "dvb_frontend.h"
26#include "tda665x.h"
27
28struct tda665x_state {
29 struct dvb_frontend *fe;
30 struct i2c_adapter *i2c;
31 const struct tda665x_config *config;
32
33 u32 frequency;
34 u32 bandwidth;
35};
36
37static int tda665x_read(struct tda665x_state *state, u8 *buf)
38{
39 const struct tda665x_config *config = state->config;
40 int err = 0;
41 struct i2c_msg msg = { .addr = config->addr, .flags = I2C_M_RD, .buf = buf, .len = 2 };
42
43 err = i2c_transfer(state->i2c, &msg, 1);
44 if (err != 1)
45 goto exit;
46
47 return err;
48exit:
Manu Abrahamf5ae4f62009-12-15 08:47:21 -030049 printk(KERN_ERR "%s: I/O Error err=<%d>\n", __func__, err);
Manu Abraham3e978a82009-12-04 05:56:35 -030050 return err;
51}
52
53static int tda665x_write(struct tda665x_state *state, u8 *buf, u8 length)
54{
55 const struct tda665x_config *config = state->config;
56 int err = 0;
57 struct i2c_msg msg = { .addr = config->addr, .flags = 0, .buf = buf, .len = length };
58
59 err = i2c_transfer(state->i2c, &msg, 1);
60 if (err != 1)
61 goto exit;
62
63 return err;
64exit:
Manu Abrahamf5ae4f62009-12-15 08:47:21 -030065 printk(KERN_ERR "%s: I/O Error err=<%d>\n", __func__, err);
Manu Abraham3e978a82009-12-04 05:56:35 -030066 return err;
67}
68
Mauro Carvalho Chehab8fdc25b2015-11-11 17:40:27 -020069static int tda665x_get_frequency(struct dvb_frontend *fe, u32 *frequency)
70{
71 struct tda665x_state *state = fe->tuner_priv;
72
73 *frequency = state->frequency;
74
75 return 0;
76}
77
Manu Abraham3e978a82009-12-04 05:56:35 -030078static int tda665x_get_status(struct dvb_frontend *fe, u32 *status)
79{
80 struct tda665x_state *state = fe->tuner_priv;
81 u8 result = 0;
82 int err = 0;
83
84 *status = 0;
85
86 err = tda665x_read(state, &result);
87 if (err < 0)
88 goto exit;
89
90 if ((result >> 6) & 0x01) {
Manu Abrahamf5ae4f62009-12-15 08:47:21 -030091 printk(KERN_DEBUG "%s: Tuner Phase Locked\n", __func__);
Manu Abraham3e978a82009-12-04 05:56:35 -030092 *status = 1;
93 }
94
95 return err;
96exit:
Manu Abrahamf5ae4f62009-12-15 08:47:21 -030097 printk(KERN_ERR "%s: I/O Error\n", __func__);
Manu Abraham3e978a82009-12-04 05:56:35 -030098 return err;
99}
100
Mauro Carvalho Chehab8e6c4be2015-11-11 17:33:59 -0200101static int tda665x_set_frequency(struct dvb_frontend *fe,
102 u32 new_frequency)
Manu Abraham3e978a82009-12-04 05:56:35 -0300103{
104 struct tda665x_state *state = fe->tuner_priv;
105 const struct tda665x_config *config = state->config;
106 u32 frequency, status = 0;
107 u8 buf[4];
108 int err = 0;
109
Mauro Carvalho Chehab8e6c4be2015-11-11 17:33:59 -0200110 if ((new_frequency < config->frequency_max)
111 || (new_frequency > config->frequency_min)) {
112 printk(KERN_ERR "%s: Frequency beyond limits, frequency=%d\n",
113 __func__, new_frequency);
Manu Abraham3e978a82009-12-04 05:56:35 -0300114 return -EINVAL;
115 }
116
Mauro Carvalho Chehab8e6c4be2015-11-11 17:33:59 -0200117 frequency = new_frequency;
118
119 frequency += config->frequency_offst;
120 frequency *= config->ref_multiplier;
121 frequency += config->ref_divider >> 1;
122 frequency /= config->ref_divider;
123
124 buf[0] = (u8) ((frequency & 0x7f00) >> 8);
125 buf[1] = (u8) (frequency & 0x00ff) >> 0;
126 buf[2] = 0x80 | 0x40 | 0x02;
127 buf[3] = 0x00;
128
129 /* restore frequency */
130 frequency = new_frequency;
131
132 if (frequency < 153000000) {
133 /* VHF-L */
134 buf[3] |= 0x01; /* fc, Low Band, 47 - 153 MHz */
135 if (frequency < 68000000)
136 buf[3] |= 0x40; /* 83uA */
137 if (frequency < 1040000000)
138 buf[3] |= 0x60; /* 122uA */
139 if (frequency < 1250000000)
140 buf[3] |= 0x80; /* 163uA */
141 else
142 buf[3] |= 0xa0; /* 254uA */
143 } else if (frequency < 438000000) {
144 /* VHF-H */
145 buf[3] |= 0x02; /* fc, Mid Band, 153 - 438 MHz */
146 if (frequency < 230000000)
147 buf[3] |= 0x40;
148 if (frequency < 300000000)
149 buf[3] |= 0x60;
150 else
151 buf[3] |= 0x80;
152 } else {
153 /* UHF */
154 buf[3] |= 0x04; /* fc, High Band, 438 - 862 MHz */
155 if (frequency < 470000000)
156 buf[3] |= 0x60;
157 if (frequency < 526000000)
158 buf[3] |= 0x80;
159 else
160 buf[3] |= 0xa0;
161 }
162
163 /* Set params */
164 err = tda665x_write(state, buf, 5);
165 if (err < 0)
166 goto exit;
167
168 /* sleep for some time */
169 printk(KERN_DEBUG "%s: Waiting to Phase LOCK\n", __func__);
170 msleep(20);
171 /* check status */
172 err = tda665x_get_status(fe, &status);
173 if (err < 0)
174 goto exit;
175
176 if (status == 1) {
177 printk(KERN_DEBUG "%s: Tuner Phase locked: status=%d\n",
178 __func__, status);
179 state->frequency = frequency; /* cache successful state */
180 } else {
181 printk(KERN_ERR "%s: No Phase lock: status=%d\n",
182 __func__, status);
183 }
184
Manu Abraham3e978a82009-12-04 05:56:35 -0300185 return 0;
186exit:
Manu Abrahamf5ae4f62009-12-15 08:47:21 -0300187 printk(KERN_ERR "%s: I/O Error\n", __func__);
Manu Abraham3e978a82009-12-04 05:56:35 -0300188 return err;
189}
190
Mauro Carvalho Chehab8fdc25b2015-11-11 17:40:27 -0200191static int tda665x_set_params(struct dvb_frontend *fe)
192{
193 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
194
195 tda665x_set_frequency(fe, c->frequency);
196
197 return 0;
198}
199
Manu Abraham3e978a82009-12-04 05:56:35 -0300200static int tda665x_release(struct dvb_frontend *fe)
201{
202 struct tda665x_state *state = fe->tuner_priv;
203
204 fe->tuner_priv = NULL;
205 kfree(state);
206 return 0;
207}
208
Julia Lawall14c4bf32016-09-11 11:44:12 -0300209static const struct dvb_tuner_ops tda665x_ops = {
Manu Abraham3e978a82009-12-04 05:56:35 -0300210 .get_status = tda665x_get_status,
Mauro Carvalho Chehab8fdc25b2015-11-11 17:40:27 -0200211 .set_params = tda665x_set_params,
212 .get_frequency = tda665x_get_frequency,
Manu Abraham3e978a82009-12-04 05:56:35 -0300213 .release = tda665x_release
214};
215
216struct dvb_frontend *tda665x_attach(struct dvb_frontend *fe,
217 const struct tda665x_config *config,
218 struct i2c_adapter *i2c)
219{
220 struct tda665x_state *state = NULL;
221 struct dvb_tuner_info *info;
222
Manu Abrahamf5ae4f62009-12-15 08:47:21 -0300223 state = kzalloc(sizeof(struct tda665x_state), GFP_KERNEL);
Peter Senna Tschudin0df10422012-09-12 08:55:59 -0300224 if (!state)
225 return NULL;
Manu Abraham3e978a82009-12-04 05:56:35 -0300226
227 state->config = config;
228 state->i2c = i2c;
229 state->fe = fe;
230 fe->tuner_priv = state;
231 fe->ops.tuner_ops = tda665x_ops;
232 info = &fe->ops.tuner_ops.info;
233
Manu Abrahamf5ae4f62009-12-15 08:47:21 -0300234 memcpy(info->name, config->name, sizeof(config->name));
Manu Abraham3e978a82009-12-04 05:56:35 -0300235 info->frequency_min = config->frequency_min;
236 info->frequency_max = config->frequency_max;
237 info->frequency_step = config->frequency_offst;
238
Manu Abrahamf5ae4f62009-12-15 08:47:21 -0300239 printk(KERN_DEBUG "%s: Attaching TDA665x (%s) tuner\n", __func__, info->name);
Manu Abraham3e978a82009-12-04 05:56:35 -0300240
241 return fe;
Manu Abraham3e978a82009-12-04 05:56:35 -0300242}
243EXPORT_SYMBOL(tda665x_attach);
244
245MODULE_DESCRIPTION("TDA665x driver");
246MODULE_AUTHOR("Manu Abraham");
247MODULE_LICENSE("GPL");