Linux 4.16.11
[linux/fpc-iii.git] / drivers / media / dvb-frontends / tua6100.c
blob1d41abd47f043432c01c6e937347f67699fc39cc
1 /*
2 * Driver for Infineon tua6100 pll.
4 * (c) 2006 Andrew de Quincey
6 * Based on code found in budget-av.c, which has the following:
7 * Compiled from various sources by Michael Hunold <michael@mihu.de>
9 * CI interface support (c) 2004 Olivier Gournet <ogournet@anevia.com> &
10 * Andrew de Quincey <adq_dvb@lidskialf.net>
12 * Copyright (C) 2002 Ralph Metzler <rjkm@metzlerbros.de>
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.
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.
27 #include <linux/slab.h>
28 #include <linux/module.h>
29 #include <linux/dvb/frontend.h>
30 #include <asm/types.h>
32 #include "tua6100.h"
34 struct tua6100_priv {
35 /* i2c details */
36 int i2c_address;
37 struct i2c_adapter *i2c;
38 u32 frequency;
41 static void tua6100_release(struct dvb_frontend *fe)
43 kfree(fe->tuner_priv);
44 fe->tuner_priv = NULL;
47 static int tua6100_sleep(struct dvb_frontend *fe)
49 struct tua6100_priv *priv = fe->tuner_priv;
50 int ret;
51 u8 reg0[] = { 0x00, 0x00 };
52 struct i2c_msg msg = { .addr = priv->i2c_address, .flags = 0, .buf = reg0, .len = 2 };
54 if (fe->ops.i2c_gate_ctrl)
55 fe->ops.i2c_gate_ctrl(fe, 1);
56 if ((ret = i2c_transfer (priv->i2c, &msg, 1)) != 1) {
57 printk("%s: i2c error\n", __func__);
59 if (fe->ops.i2c_gate_ctrl)
60 fe->ops.i2c_gate_ctrl(fe, 0);
62 return (ret == 1) ? 0 : ret;
65 static int tua6100_set_params(struct dvb_frontend *fe)
67 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
68 struct tua6100_priv *priv = fe->tuner_priv;
69 u32 div;
70 u32 prediv;
71 u8 reg0[] = { 0x00, 0x00 };
72 u8 reg1[] = { 0x01, 0x00, 0x00, 0x00 };
73 u8 reg2[] = { 0x02, 0x00, 0x00 };
74 struct i2c_msg msg0 = { .addr = priv->i2c_address, .flags = 0, .buf = reg0, .len = 2 };
75 struct i2c_msg msg1 = { .addr = priv->i2c_address, .flags = 0, .buf = reg1, .len = 4 };
76 struct i2c_msg msg2 = { .addr = priv->i2c_address, .flags = 0, .buf = reg2, .len = 3 };
78 #define _R 4
79 #define _P 32
80 #define _ri 4000000
82 // setup register 0
83 if (c->frequency < 2000000)
84 reg0[1] = 0x03;
85 else
86 reg0[1] = 0x07;
88 // setup register 1
89 if (c->frequency < 1630000)
90 reg1[1] = 0x2c;
91 else
92 reg1[1] = 0x0c;
94 if (_P == 64)
95 reg1[1] |= 0x40;
96 if (c->frequency >= 1525000)
97 reg1[1] |= 0x80;
99 // register 2
100 reg2[1] = (_R >> 8) & 0x03;
101 reg2[2] = _R;
102 if (c->frequency < 1455000)
103 reg2[1] |= 0x1c;
104 else if (c->frequency < 1630000)
105 reg2[1] |= 0x0c;
106 else
107 reg2[1] |= 0x1c;
110 * The N divisor ratio (note: c->frequency is in kHz, but we
111 * need it in Hz)
113 prediv = (c->frequency * _R) / (_ri / 1000);
114 div = prediv / _P;
115 reg1[1] |= (div >> 9) & 0x03;
116 reg1[2] = div >> 1;
117 reg1[3] = (div << 7);
118 priv->frequency = ((div * _P) * (_ri / 1000)) / _R;
120 // Finally, calculate and store the value for A
121 reg1[3] |= (prediv - (div*_P)) & 0x7f;
123 #undef _R
124 #undef _P
125 #undef _ri
127 if (fe->ops.i2c_gate_ctrl)
128 fe->ops.i2c_gate_ctrl(fe, 1);
129 if (i2c_transfer(priv->i2c, &msg0, 1) != 1)
130 return -EIO;
132 if (fe->ops.i2c_gate_ctrl)
133 fe->ops.i2c_gate_ctrl(fe, 1);
134 if (i2c_transfer(priv->i2c, &msg2, 1) != 1)
135 return -EIO;
137 if (fe->ops.i2c_gate_ctrl)
138 fe->ops.i2c_gate_ctrl(fe, 1);
139 if (i2c_transfer(priv->i2c, &msg1, 1) != 1)
140 return -EIO;
142 if (fe->ops.i2c_gate_ctrl)
143 fe->ops.i2c_gate_ctrl(fe, 0);
145 return 0;
148 static int tua6100_get_frequency(struct dvb_frontend *fe, u32 *frequency)
150 struct tua6100_priv *priv = fe->tuner_priv;
151 *frequency = priv->frequency;
152 return 0;
155 static const struct dvb_tuner_ops tua6100_tuner_ops = {
156 .info = {
157 .name = "Infineon TUA6100",
158 .frequency_min = 950000,
159 .frequency_max = 2150000,
160 .frequency_step = 1000,
162 .release = tua6100_release,
163 .sleep = tua6100_sleep,
164 .set_params = tua6100_set_params,
165 .get_frequency = tua6100_get_frequency,
168 struct dvb_frontend *tua6100_attach(struct dvb_frontend *fe, int addr, struct i2c_adapter *i2c)
170 struct tua6100_priv *priv = NULL;
171 u8 b1 [] = { 0x80 };
172 u8 b2 [] = { 0x00 };
173 struct i2c_msg msg [] = { { .addr = addr, .flags = 0, .buf = b1, .len = 1 },
174 { .addr = addr, .flags = I2C_M_RD, .buf = b2, .len = 1 } };
175 int ret;
177 if (fe->ops.i2c_gate_ctrl)
178 fe->ops.i2c_gate_ctrl(fe, 1);
179 ret = i2c_transfer (i2c, msg, 2);
180 if (fe->ops.i2c_gate_ctrl)
181 fe->ops.i2c_gate_ctrl(fe, 0);
183 if (ret != 2)
184 return NULL;
186 priv = kzalloc(sizeof(struct tua6100_priv), GFP_KERNEL);
187 if (priv == NULL)
188 return NULL;
190 priv->i2c_address = addr;
191 priv->i2c = i2c;
193 memcpy(&fe->ops.tuner_ops, &tua6100_tuner_ops, sizeof(struct dvb_tuner_ops));
194 fe->tuner_priv = priv;
195 return fe;
197 EXPORT_SYMBOL(tua6100_attach);
199 MODULE_DESCRIPTION("DVB tua6100 driver");
200 MODULE_AUTHOR("Andrew de Quincey");
201 MODULE_LICENSE("GPL");