blob: e0df93191b9e8cd6b2a62774d11630e78470ba75 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Manu Abraham00360202007-09-22 13:30:09 -030025
26#include "dvb_frontend.h"
27#include "tda8261.h"
28
29struct tda8261_state {
Manu Abraham41e11512007-09-22 21:28:11 -030030 struct dvb_frontend *fe;
31 struct i2c_adapter *i2c;
32 const struct tda8261_config *config;
Manu Abraham00360202007-09-22 13:30:09 -030033
34 /* state cache */
35 u32 frequency;
36 u32 bandwidth;
37};
38
39static int tda8261_read(struct tda8261_state *state, u8 *buf)
40{
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;
Arvo Järve225ee0c2010-01-10 17:15:57 -030043 struct i2c_msg msg = { .addr = config->addr, .flags = I2C_M_RD,.buf = buf, .len = 1 };
Manu Abraham00360202007-09-22 13:30:09 -030044
Manu Abrahamc7d85a22007-09-24 13:27:06 -030045 if ((err = i2c_transfer(state->i2c, &msg, 1)) != 1)
Alan Coxeb2e2652012-09-02 03:30:15 -030046 pr_err("%s: read error, err=%d\n", __func__, err);
Manu Abraham00360202007-09-22 13:30:09 -030047
48 return err;
49}
50
51static int tda8261_write(struct tda8261_state *state, u8 *buf)
52{
Manu Abraham41e11512007-09-22 21:28:11 -030053 const struct tda8261_config *config = state->config;
Manu Abraham00360202007-09-22 13:30:09 -030054 int err = 0;
55 struct i2c_msg msg = { .addr = config->addr, .flags = 0, .buf = buf, .len = 4 };
56
57 if ((err = i2c_transfer(state->i2c, &msg, 1)) != 1)
Alan Coxeb2e2652012-09-02 03:30:15 -030058 pr_err("%s: write error, err=%d\n", __func__, err);
Manu Abraham00360202007-09-22 13:30:09 -030059
60 return err;
61}
62
63static int tda8261_get_status(struct dvb_frontend *fe, u32 *status)
64{
65 struct tda8261_state *state = fe->tuner_priv;
66 u8 result = 0;
67 int err = 0;
68
Manu Abrahamf6e63822007-09-28 19:06:06 -030069 *status = 0;
70
Manu Abraham00360202007-09-22 13:30:09 -030071 if ((err = tda8261_read(state, &result)) < 0) {
Alan Coxeb2e2652012-09-02 03:30:15 -030072 pr_err("%s: I/O Error\n", __func__);
Manu Abraham00360202007-09-22 13:30:09 -030073 return err;
74 }
75 if ((result >> 6) & 0x01) {
Alan Coxeb2e2652012-09-02 03:30:15 -030076 pr_debug("%s: Tuner Phase Locked\n", __func__);
Manu Abraham00360202007-09-22 13:30:09 -030077 *status = 1;
78 }
79
80 return err;
81}
82
83static const u32 div_tab[] = { 2000, 1000, 500, 250, 125 }; /* kHz */
84static const u8 ref_div[] = { 0x00, 0x01, 0x02, 0x05, 0x07 };
85
Mauro Carvalho Chehabe4176682015-11-11 18:07:44 -020086static int tda8261_get_frequency(struct dvb_frontend *fe, u32 *frequency)
Manu Abraham00360202007-09-22 13:30:09 -030087{
88 struct tda8261_state *state = fe->tuner_priv;
Manu Abraham00360202007-09-22 13:30:09 -030089
Mauro Carvalho Chehabe4176682015-11-11 18:07:44 -020090 *frequency = state->frequency;
Manu Abraham00360202007-09-22 13:30:09 -030091
Mauro Carvalho Chehabe4176682015-11-11 18:07:44 -020092 return 0;
Manu Abraham00360202007-09-22 13:30:09 -030093}
94
Mauro Carvalho Chehabe4176682015-11-11 18:07:44 -020095static int tda8261_set_params(struct dvb_frontend *fe)
Manu Abraham00360202007-09-22 13:30:09 -030096{
Mauro Carvalho Chehabe4176682015-11-11 18:07:44 -020097 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Manu Abraham00360202007-09-22 13:30:09 -030098 struct tda8261_state *state = fe->tuner_priv;
Manu Abraham41e11512007-09-22 21:28:11 -030099 const struct tda8261_config *config = state->config;
Manu Abraham00360202007-09-22 13:30:09 -0300100 u32 frequency, N, status = 0;
101 u8 buf[4];
102 int err = 0;
103
Mauro Carvalho Chehabe4176682015-11-11 18:07:44 -0200104 /*
105 * N = Max VCO Frequency / Channel Spacing
106 * Max VCO Frequency = VCO frequency + (channel spacing - 1)
107 * (to account for half channel spacing on either side)
108 */
109 frequency = c->frequency;
110 if ((frequency < 950000) || (frequency > 2150000)) {
111 pr_warn("%s: Frequency beyond limits, frequency=%d\n",
112 __func__, frequency);
Manu Abraham00360202007-09-22 13:30:09 -0300113 return -EINVAL;
114 }
Mauro Carvalho Chehabe4176682015-11-11 18:07:44 -0200115 N = (frequency + (div_tab[config->step_size] - 1)) / div_tab[config->step_size];
116 pr_debug("%s: Step size=%d, Divider=%d, PG=0x%02x (%d)\n",
117 __func__, config->step_size, div_tab[config->step_size], N, N);
118
119 buf[0] = (N >> 8) & 0xff;
120 buf[1] = N & 0xff;
121 buf[2] = (0x01 << 7) | ((ref_div[config->step_size] & 0x07) << 1);
122
123 if (frequency < 1450000)
124 buf[3] = 0x00;
125 else if (frequency < 2000000)
126 buf[3] = 0x40;
127 else if (frequency < 2150000)
128 buf[3] = 0x80;
129
130 /* Set params */
131 err = tda8261_write(state, buf);
132 if (err < 0) {
133 pr_err("%s: I/O Error\n", __func__);
134 return err;
135 }
136 /* sleep for some time */
137 pr_debug("%s: Waiting to Phase LOCK\n", __func__);
138 msleep(20);
139 /* check status */
140 if ((err = tda8261_get_status(fe, &status)) < 0) {
141 pr_err("%s: I/O Error\n", __func__);
142 return err;
143 }
144 if (status == 1) {
145 pr_debug("%s: Tuner Phase locked: status=%d\n", __func__,
146 status);
147 state->frequency = frequency; /* cache successful state */
148 } else {
149 pr_debug("%s: No Phase lock: status=%d\n", __func__, status);
150 }
Manu Abraham00360202007-09-22 13:30:09 -0300151
152 return 0;
153}
154
155static int tda8261_release(struct dvb_frontend *fe)
156{
157 struct tda8261_state *state = fe->tuner_priv;
158
159 fe->tuner_priv = NULL;
160 kfree(state);
161 return 0;
162}
163
Julia Lawall14c4bf32016-09-11 11:44:12 -0300164static const struct dvb_tuner_ops tda8261_ops = {
Manu Abraham00360202007-09-22 13:30:09 -0300165
166 .info = {
167 .name = "TDA8261",
Manu Abraham00360202007-09-22 13:30:09 -0300168 .frequency_min = 950000,
169 .frequency_max = 2150000,
170 .frequency_step = 0
171 },
172
Mauro Carvalho Chehabe4176682015-11-11 18:07:44 -0200173 .set_params = tda8261_set_params,
174 .get_frequency = tda8261_get_frequency,
Manu Abrahamf6e63822007-09-28 19:06:06 -0300175 .get_status = tda8261_get_status,
Manu Abraham00360202007-09-22 13:30:09 -0300176 .release = tda8261_release
177};
178
179struct dvb_frontend *tda8261_attach(struct dvb_frontend *fe,
Manu Abraham41e11512007-09-22 21:28:11 -0300180 const struct tda8261_config *config,
Manu Abraham00360202007-09-22 13:30:09 -0300181 struct i2c_adapter *i2c)
182{
183 struct tda8261_state *state = NULL;
184
185 if ((state = kzalloc(sizeof (struct tda8261_state), GFP_KERNEL)) == NULL)
186 goto exit;
187
188 state->config = config;
189 state->i2c = i2c;
190 state->fe = fe;
191 fe->tuner_priv = state;
192 fe->ops.tuner_ops = tda8261_ops;
193
194 fe->ops.tuner_ops.info.frequency_step = div_tab[config->step_size];
Manu Abraham00360202007-09-22 13:30:09 -0300195
Alan Coxeb2e2652012-09-02 03:30:15 -0300196 pr_info("%s: Attaching TDA8261 8PSK/QPSK tuner\n", __func__);
Manu Abraham00360202007-09-22 13:30:09 -0300197
Manu Abraham00360202007-09-22 13:30:09 -0300198 return fe;
199
200exit:
201 kfree(state);
202 return NULL;
203}
204
205EXPORT_SYMBOL(tda8261_attach);
Manu Abraham00360202007-09-22 13:30:09 -0300206
207MODULE_AUTHOR("Manu Abraham");
208MODULE_DESCRIPTION("TDA8261 8PSK/QPSK Tuner");
209MODULE_LICENSE("GPL");