Merge branch 'for-chris-4.12' of git://git.kernel.org/pub/scm/linux/kernel/git/fdmana...
[sfrench/cifs-2.6.git] / drivers / media / dvb-frontends / tua6100.c
1 /**
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
27 #include <linux/slab.h>
28 #include <linux/module.h>
29 #include <linux/dvb/frontend.h>
30 #include <asm/types.h>
31
32 #include "tua6100.h"
33
34 struct tua6100_priv {
35         /* i2c details */
36         int i2c_address;
37         struct i2c_adapter *i2c;
38         u32 frequency;
39 };
40
41 static void tua6100_release(struct dvb_frontend *fe)
42 {
43         kfree(fe->tuner_priv);
44         fe->tuner_priv = NULL;
45 }
46
47 static int tua6100_sleep(struct dvb_frontend *fe)
48 {
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 };
53
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__);
58         }
59         if (fe->ops.i2c_gate_ctrl)
60                 fe->ops.i2c_gate_ctrl(fe, 0);
61
62         return (ret == 1) ? 0 : ret;
63 }
64
65 static int tua6100_set_params(struct dvb_frontend *fe)
66 {
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 };
77
78 #define _R 4
79 #define _P 32
80 #define _ri 4000000
81
82         // setup register 0
83         if (c->frequency < 2000000)
84                 reg0[1] = 0x03;
85         else
86                 reg0[1] = 0x07;
87
88         // setup register 1
89         if (c->frequency < 1630000)
90                 reg1[1] = 0x2c;
91         else
92                 reg1[1] = 0x0c;
93
94         if (_P == 64)
95                 reg1[1] |= 0x40;
96         if (c->frequency >= 1525000)
97                 reg1[1] |= 0x80;
98
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;
108
109         /*
110          * The N divisor ratio (note: c->frequency is in kHz, but we
111          * need it in Hz)
112          */
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;
119
120         // Finally, calculate and store the value for A
121         reg1[3] |= (prediv - (div*_P)) & 0x7f;
122
123 #undef _R
124 #undef _P
125 #undef _ri
126
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;
131
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;
136
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;
141
142         if (fe->ops.i2c_gate_ctrl)
143                 fe->ops.i2c_gate_ctrl(fe, 0);
144
145         return 0;
146 }
147
148 static int tua6100_get_frequency(struct dvb_frontend *fe, u32 *frequency)
149 {
150         struct tua6100_priv *priv = fe->tuner_priv;
151         *frequency = priv->frequency;
152         return 0;
153 }
154
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,
161         },
162         .release = tua6100_release,
163         .sleep = tua6100_sleep,
164         .set_params = tua6100_set_params,
165         .get_frequency = tua6100_get_frequency,
166 };
167
168 struct dvb_frontend *tua6100_attach(struct dvb_frontend *fe, int addr, struct i2c_adapter *i2c)
169 {
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;
176
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);
182
183         if (ret != 2)
184                 return NULL;
185
186         priv = kzalloc(sizeof(struct tua6100_priv), GFP_KERNEL);
187         if (priv == NULL)
188                 return NULL;
189
190         priv->i2c_address = addr;
191         priv->i2c = i2c;
192
193         memcpy(&fe->ops.tuner_ops, &tua6100_tuner_ops, sizeof(struct dvb_tuner_ops));
194         fe->tuner_priv = priv;
195         return fe;
196 }
197 EXPORT_SYMBOL(tua6100_attach);
198
199 MODULE_DESCRIPTION("DVB tua6100 driver");
200 MODULE_AUTHOR("Andrew de Quincey");
201 MODULE_LICENSE("GPL");