blob: 05ee16d29851fe1dcb4cadf42ff8976ed1b74cac [file] [log] [blame]
Andrew de Quincey00be2e72006-09-05 17:53:06 -03001/**
2 * Driver for Infineon tua6100 pll.
3 *
4 * (c) 2006 Andrew de Quincey
5 *
6 * Based on code found in budget-av.c, which has the following:
7 * Compiled from various sources by Michael Hunold <michael@mihu.de>
8 *
9 * CI interface support (c) 2004 Olivier Gournet <ogournet@anevia.com> &
10 * Andrew de Quincey <adq_dvb@lidskialf.net>
11 *
12 * Copyright (C) 2002 Ralph Metzler <rjkm@metzlerbros.de>
13 *
14 * Copyright (C) 1999-2002 Ralph Metzler
15 * & Marcus Metzler for convergence integrated media GmbH
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 */
30
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Andrew de Quincey00be2e72006-09-05 17:53:06 -030032#include <linux/module.h>
33#include <linux/dvb/frontend.h>
34#include <asm/types.h>
35
36#include "tua6100.h"
37
38struct tua6100_priv {
39 /* i2c details */
40 int i2c_address;
41 struct i2c_adapter *i2c;
42 u32 frequency;
43};
44
Mauro Carvalho Chehabf2709c22016-11-18 20:30:51 -020045static void tua6100_release(struct dvb_frontend *fe)
46{
47 kfree(fe->tuner_priv);
48 fe->tuner_priv = NULL;
49}
50
Andrew de Quincey00be2e72006-09-05 17:53:06 -030051static int tua6100_sleep(struct dvb_frontend *fe)
52{
53 struct tua6100_priv *priv = fe->tuner_priv;
54 int ret;
55 u8 reg0[] = { 0x00, 0x00 };
56 struct i2c_msg msg = { .addr = priv->i2c_address, .flags = 0, .buf = reg0, .len = 2 };
57
58 if (fe->ops.i2c_gate_ctrl)
59 fe->ops.i2c_gate_ctrl(fe, 1);
60 if ((ret = i2c_transfer (priv->i2c, &msg, 1)) != 1) {
Harvey Harrison271ddbf2008-04-08 23:20:00 -030061 printk("%s: i2c error\n", __func__);
Andrew de Quincey00be2e72006-09-05 17:53:06 -030062 }
63 if (fe->ops.i2c_gate_ctrl)
64 fe->ops.i2c_gate_ctrl(fe, 0);
65
66 return (ret == 1) ? 0 : ret;
67}
68
Mauro Carvalho Chehab14d24d12011-12-24 12:24:33 -030069static int tua6100_set_params(struct dvb_frontend *fe)
Andrew de Quincey00be2e72006-09-05 17:53:06 -030070{
Mauro Carvalho Chehab59182882011-12-23 17:56:57 -030071 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Andrew de Quincey00be2e72006-09-05 17:53:06 -030072 struct tua6100_priv *priv = fe->tuner_priv;
73 u32 div;
74 u32 prediv;
75 u8 reg0[] = { 0x00, 0x00 };
76 u8 reg1[] = { 0x01, 0x00, 0x00, 0x00 };
77 u8 reg2[] = { 0x02, 0x00, 0x00 };
78 struct i2c_msg msg0 = { .addr = priv->i2c_address, .flags = 0, .buf = reg0, .len = 2 };
79 struct i2c_msg msg1 = { .addr = priv->i2c_address, .flags = 0, .buf = reg1, .len = 4 };
80 struct i2c_msg msg2 = { .addr = priv->i2c_address, .flags = 0, .buf = reg2, .len = 3 };
81
82#define _R 4
83#define _P 32
84#define _ri 4000000
85
86 // setup register 0
Mauro Carvalho Chehab59182882011-12-23 17:56:57 -030087 if (c->frequency < 2000000)
Andrew de Quincey00be2e72006-09-05 17:53:06 -030088 reg0[1] = 0x03;
Mauro Carvalho Chehab59182882011-12-23 17:56:57 -030089 else
Andrew de Quincey00be2e72006-09-05 17:53:06 -030090 reg0[1] = 0x07;
Andrew de Quincey00be2e72006-09-05 17:53:06 -030091
92 // setup register 1
Mauro Carvalho Chehab59182882011-12-23 17:56:57 -030093 if (c->frequency < 1630000)
Andrew de Quincey00be2e72006-09-05 17:53:06 -030094 reg1[1] = 0x2c;
Mauro Carvalho Chehab59182882011-12-23 17:56:57 -030095 else
Andrew de Quincey00be2e72006-09-05 17:53:06 -030096 reg1[1] = 0x0c;
Mauro Carvalho Chehab59182882011-12-23 17:56:57 -030097
Andrew de Quincey00be2e72006-09-05 17:53:06 -030098 if (_P == 64)
99 reg1[1] |= 0x40;
Mauro Carvalho Chehab59182882011-12-23 17:56:57 -0300100 if (c->frequency >= 1525000)
Andrew de Quincey00be2e72006-09-05 17:53:06 -0300101 reg1[1] |= 0x80;
102
103 // register 2
104 reg2[1] = (_R >> 8) & 0x03;
105 reg2[2] = _R;
Mauro Carvalho Chehab59182882011-12-23 17:56:57 -0300106 if (c->frequency < 1455000)
Andrew de Quincey00be2e72006-09-05 17:53:06 -0300107 reg2[1] |= 0x1c;
Mauro Carvalho Chehab59182882011-12-23 17:56:57 -0300108 else if (c->frequency < 1630000)
Andrew de Quincey00be2e72006-09-05 17:53:06 -0300109 reg2[1] |= 0x0c;
Mauro Carvalho Chehab59182882011-12-23 17:56:57 -0300110 else
Andrew de Quincey00be2e72006-09-05 17:53:06 -0300111 reg2[1] |= 0x1c;
Andrew de Quincey00be2e72006-09-05 17:53:06 -0300112
Mauro Carvalho Chehab59182882011-12-23 17:56:57 -0300113 /*
114 * The N divisor ratio (note: c->frequency is in kHz, but we
115 * need it in Hz)
116 */
117 prediv = (c->frequency * _R) / (_ri / 1000);
Andrew de Quincey00be2e72006-09-05 17:53:06 -0300118 div = prediv / _P;
119 reg1[1] |= (div >> 9) & 0x03;
120 reg1[2] = div >> 1;
121 reg1[3] = (div << 7);
122 priv->frequency = ((div * _P) * (_ri / 1000)) / _R;
123
124 // Finally, calculate and store the value for A
125 reg1[3] |= (prediv - (div*_P)) & 0x7f;
126
127#undef _R
128#undef _P
129#undef _ri
130
131 if (fe->ops.i2c_gate_ctrl)
132 fe->ops.i2c_gate_ctrl(fe, 1);
133 if (i2c_transfer(priv->i2c, &msg0, 1) != 1)
134 return -EIO;
135
136 if (fe->ops.i2c_gate_ctrl)
137 fe->ops.i2c_gate_ctrl(fe, 1);
138 if (i2c_transfer(priv->i2c, &msg2, 1) != 1)
139 return -EIO;
140
141 if (fe->ops.i2c_gate_ctrl)
142 fe->ops.i2c_gate_ctrl(fe, 1);
143 if (i2c_transfer(priv->i2c, &msg1, 1) != 1)
144 return -EIO;
145
146 if (fe->ops.i2c_gate_ctrl)
147 fe->ops.i2c_gate_ctrl(fe, 0);
148
149 return 0;
150}
151
152static int tua6100_get_frequency(struct dvb_frontend *fe, u32 *frequency)
153{
154 struct tua6100_priv *priv = fe->tuner_priv;
155 *frequency = priv->frequency;
156 return 0;
157}
158
Julia Lawall14c4bf32016-09-11 11:44:12 -0300159static const struct dvb_tuner_ops tua6100_tuner_ops = {
Andrew de Quincey00be2e72006-09-05 17:53:06 -0300160 .info = {
161 .name = "Infineon TUA6100",
162 .frequency_min = 950000,
163 .frequency_max = 2150000,
164 .frequency_step = 1000,
165 },
Mauro Carvalho Chehabf2709c22016-11-18 20:30:51 -0200166 .release = tua6100_release,
Andrew de Quincey00be2e72006-09-05 17:53:06 -0300167 .sleep = tua6100_sleep,
168 .set_params = tua6100_set_params,
169 .get_frequency = tua6100_get_frequency,
170};
171
172struct dvb_frontend *tua6100_attach(struct dvb_frontend *fe, int addr, struct i2c_adapter *i2c)
173{
174 struct tua6100_priv *priv = NULL;
175 u8 b1 [] = { 0x80 };
176 u8 b2 [] = { 0x00 };
177 struct i2c_msg msg [] = { { .addr = addr, .flags = 0, .buf = b1, .len = 1 },
178 { .addr = addr, .flags = I2C_M_RD, .buf = b2, .len = 1 } };
179 int ret;
180
181 if (fe->ops.i2c_gate_ctrl)
182 fe->ops.i2c_gate_ctrl(fe, 1);
183 ret = i2c_transfer (i2c, msg, 2);
184 if (fe->ops.i2c_gate_ctrl)
185 fe->ops.i2c_gate_ctrl(fe, 0);
186
187 if (ret != 2)
188 return NULL;
189
190 priv = kzalloc(sizeof(struct tua6100_priv), GFP_KERNEL);
191 if (priv == NULL)
192 return NULL;
193
194 priv->i2c_address = addr;
195 priv->i2c = i2c;
196
197 memcpy(&fe->ops.tuner_ops, &tua6100_tuner_ops, sizeof(struct dvb_tuner_ops));
198 fe->tuner_priv = priv;
199 return fe;
200}
201EXPORT_SYMBOL(tua6100_attach);
202
203MODULE_DESCRIPTION("DVB tua6100 driver");
204MODULE_AUTHOR("Andrew de Quincey");
205MODULE_LICENSE("GPL");