blob: da5e5a8169fa6ad1b456c107e018912e5f5705d6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -08002
3 i2c tv tuner chip device driver
4 controls the philips tda8290+75 tuner chip combo.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
Michael Krufky910bb3e2007-08-27 21:22:20 -030019
20 This "tda8290" module was split apart from the original "tuner" module.
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -080021*/
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/i2c.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/delay.h>
Michael Krufkyffbb8072007-08-21 01:14:12 -030025#include <linux/videodev.h>
Michael Krufky910bb3e2007-08-27 21:22:20 -030026#include "tda8290.h"
Michael Krufky746d97322007-08-25 19:08:45 -030027#include "tda827x.h"
Michael Krufky5bea1cd2007-10-22 09:56:38 -030028#include "tda18271.h"
Michael Krufky910bb3e2007-08-27 21:22:20 -030029
Michael Krufky5bea1cd2007-10-22 09:56:38 -030030static int tuner_debug;
Michael Krufky746d97322007-08-25 19:08:45 -030031module_param_named(debug, tuner_debug, int, 0644);
Michael Krufky910bb3e2007-08-27 21:22:20 -030032MODULE_PARM_DESC(debug, "enable verbose debug messages");
33
34#define PREFIX "tda8290 "
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36/* ---------------------------------------------------------------------- */
37
Michael Krufkyb2083192007-05-29 22:54:06 -030038struct tda8290_priv {
Michael Krufkydb8a6952007-08-21 01:24:42 -030039 struct tuner_i2c_props i2c_props;
40
Michael Krufkyb2083192007-05-29 22:54:06 -030041 unsigned char tda8290_easy_mode;
Michael Krufky746d97322007-08-25 19:08:45 -030042
Michael Krufkyb2083192007-05-29 22:54:06 -030043 unsigned char tda827x_addr;
44 unsigned char tda827x_ver;
Michael Krufky910bb3e2007-08-27 21:22:20 -030045
Michael Krufky746d97322007-08-25 19:08:45 -030046 struct tda827x_config cfg;
Michael Krufkyb2083192007-05-29 22:54:06 -030047};
48
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -080049/*---------------------------------------------------------------------*/
50
Michael Krufky746d97322007-08-25 19:08:45 -030051static void tda8290_i2c_bridge(struct tuner *t, int close)
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -080052{
Michael Krufky746d97322007-08-25 19:08:45 -030053 struct tda8290_priv *priv = t->priv;
Michael Krufkydb8a6952007-08-21 01:24:42 -030054
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -080055 unsigned char enable[2] = { 0x21, 0xC0 };
Hartmut Hackmann0157a9c2006-02-07 06:49:09 -020056 unsigned char disable[2] = { 0x21, 0x00 };
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -080057 unsigned char *msg;
58 if(close) {
59 msg = enable;
Michael Krufkydb8a6952007-08-21 01:24:42 -030060 tuner_i2c_xfer_send(&priv->i2c_props, msg, 2);
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -080061 /* let the bridge stabilize */
62 msleep(20);
63 } else {
64 msg = disable;
Michael Krufkydb8a6952007-08-21 01:24:42 -030065 tuner_i2c_xfer_send(&priv->i2c_props, msg, 2);
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -080066 }
67}
68
Michael Krufky5bea1cd2007-10-22 09:56:38 -030069static void tda8295_i2c_bridge(struct tuner *t, int close)
70{
71 struct tda8290_priv *priv = t->priv;
72
73 unsigned char enable[2] = { 0x45, 0xc1 };
74 unsigned char disable[2] = { 0x46, 0x00 };
75 unsigned char buf[3] = { 0x45, 0x01, 0x00 };
76 unsigned char *msg;
77 if (close) {
78 msg = enable;
79 tuner_i2c_xfer_send(&priv->i2c_props, msg, 2);
80 /* let the bridge stabilize */
81 msleep(20);
82 } else {
83 msg = disable;
84 tuner_i2c_xfer_send(&priv->i2c_props, msg, 1);
85 tuner_i2c_xfer_recv(&priv->i2c_props, &msg[1], 1);
86
87 buf[2] = msg[1];
88 buf[2] &= ~0x04;
89 tuner_i2c_xfer_send(&priv->i2c_props, buf, 3);
90 msleep(5);
91
92 msg[1] |= 0x04;
93 tuner_i2c_xfer_send(&priv->i2c_props, msg, 2);
94 }
95}
96
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -080097/*---------------------------------------------------------------------*/
98
Michael Krufky746d97322007-08-25 19:08:45 -030099static void set_audio(struct tuner *t)
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800100{
Michael Krufky746d97322007-08-25 19:08:45 -0300101 struct tda8290_priv *priv = t->priv;
Michael Krufky910bb3e2007-08-27 21:22:20 -0300102 char* mode;
103
Michael Krufky746d97322007-08-25 19:08:45 -0300104 priv->cfg.tda827x_lpsel = 0;
105 if (t->std & V4L2_STD_MN) {
106 priv->cfg.sgIF = 92;
Michael Krufky910bb3e2007-08-27 21:22:20 -0300107 priv->tda8290_easy_mode = 0x01;
Michael Krufky746d97322007-08-25 19:08:45 -0300108 priv->cfg.tda827x_lpsel = 1;
Michael Krufky910bb3e2007-08-27 21:22:20 -0300109 mode = "MN";
Michael Krufky746d97322007-08-25 19:08:45 -0300110 } else if (t->std & V4L2_STD_B) {
111 priv->cfg.sgIF = 108;
Michael Krufky910bb3e2007-08-27 21:22:20 -0300112 priv->tda8290_easy_mode = 0x02;
113 mode = "B";
Michael Krufky746d97322007-08-25 19:08:45 -0300114 } else if (t->std & V4L2_STD_GH) {
115 priv->cfg.sgIF = 124;
Michael Krufky910bb3e2007-08-27 21:22:20 -0300116 priv->tda8290_easy_mode = 0x04;
117 mode = "GH";
Michael Krufky746d97322007-08-25 19:08:45 -0300118 } else if (t->std & V4L2_STD_PAL_I) {
119 priv->cfg.sgIF = 124;
Michael Krufky910bb3e2007-08-27 21:22:20 -0300120 priv->tda8290_easy_mode = 0x08;
121 mode = "I";
Michael Krufky746d97322007-08-25 19:08:45 -0300122 } else if (t->std & V4L2_STD_DK) {
123 priv->cfg.sgIF = 124;
Michael Krufky910bb3e2007-08-27 21:22:20 -0300124 priv->tda8290_easy_mode = 0x10;
125 mode = "DK";
Michael Krufky746d97322007-08-25 19:08:45 -0300126 } else if (t->std & V4L2_STD_SECAM_L) {
127 priv->cfg.sgIF = 124;
Michael Krufky910bb3e2007-08-27 21:22:20 -0300128 priv->tda8290_easy_mode = 0x20;
129 mode = "L";
Michael Krufky746d97322007-08-25 19:08:45 -0300130 } else if (t->std & V4L2_STD_SECAM_LC) {
131 priv->cfg.sgIF = 20;
Michael Krufky910bb3e2007-08-27 21:22:20 -0300132 priv->tda8290_easy_mode = 0x40;
133 mode = "LC";
134 } else {
Michael Krufky746d97322007-08-25 19:08:45 -0300135 priv->cfg.sgIF = 124;
Michael Krufky910bb3e2007-08-27 21:22:20 -0300136 priv->tda8290_easy_mode = 0x10;
137 mode = "xx";
138 }
139
Michael Krufky746d97322007-08-25 19:08:45 -0300140 if (t->mode == V4L2_TUNER_RADIO)
141 priv->cfg.sgIF = 88; /* if frequency is 5.5 MHz */
Michael Krufky910bb3e2007-08-27 21:22:20 -0300142
143 tuner_dbg("setting tda8290 to system %s\n", mode);
144}
145
Michael Krufky746d97322007-08-25 19:08:45 -0300146static void tda8290_set_freq(struct tuner *t, unsigned int freq)
Michael Krufky910bb3e2007-08-27 21:22:20 -0300147{
Michael Krufky746d97322007-08-25 19:08:45 -0300148 struct tda8290_priv *priv = t->priv;
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800149 unsigned char soft_reset[] = { 0x00, 0x00 };
Michael Krufkyb2083192007-05-29 22:54:06 -0300150 unsigned char easy_mode[] = { 0x01, priv->tda8290_easy_mode };
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800151 unsigned char expert_mode[] = { 0x01, 0x80 };
Hartmut Hackmann0157a9c2006-02-07 06:49:09 -0200152 unsigned char agc_out_on[] = { 0x02, 0x00 };
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800153 unsigned char gainset_off[] = { 0x28, 0x14 };
154 unsigned char if_agc_spd[] = { 0x0f, 0x88 };
155 unsigned char adc_head_6[] = { 0x05, 0x04 };
156 unsigned char adc_head_9[] = { 0x05, 0x02 };
157 unsigned char adc_head_12[] = { 0x05, 0x01 };
158 unsigned char pll_bw_nom[] = { 0x0d, 0x47 };
159 unsigned char pll_bw_low[] = { 0x0d, 0x27 };
160 unsigned char gainset_2[] = { 0x28, 0x64 };
161 unsigned char agc_rst_on[] = { 0x0e, 0x0b };
162 unsigned char agc_rst_off[] = { 0x0e, 0x09 };
163 unsigned char if_agc_set[] = { 0x0f, 0x81 };
164 unsigned char addr_adc_sat = 0x1a;
165 unsigned char addr_agc_stat = 0x1d;
166 unsigned char addr_pll_stat = 0x1b;
167 unsigned char adc_sat, agc_stat,
Nickolay V. Shmyrev9a741ec2005-11-08 21:37:50 -0800168 pll_stat;
Hartmut Hackmann58ef4f92007-04-27 12:31:12 -0300169 int i;
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800170
Michael Krufky746d97322007-08-25 19:08:45 -0300171 struct analog_parameters params = {
172 .frequency = freq,
173 .mode = t->mode,
174 .audmode = t->audmode,
175 .std = t->std
176 };
Michael Krufky910bb3e2007-08-27 21:22:20 -0300177
Michael Krufky746d97322007-08-25 19:08:45 -0300178 set_audio(t);
179
180 tuner_dbg("tda827xa config is 0x%02x\n", t->config);
Michael Krufkydb8a6952007-08-21 01:24:42 -0300181 tuner_i2c_xfer_send(&priv->i2c_props, easy_mode, 2);
182 tuner_i2c_xfer_send(&priv->i2c_props, agc_out_on, 2);
183 tuner_i2c_xfer_send(&priv->i2c_props, soft_reset, 2);
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800184 msleep(1);
185
Michael Krufkyb2083192007-05-29 22:54:06 -0300186 expert_mode[1] = priv->tda8290_easy_mode + 0x80;
Michael Krufkydb8a6952007-08-21 01:24:42 -0300187 tuner_i2c_xfer_send(&priv->i2c_props, expert_mode, 2);
188 tuner_i2c_xfer_send(&priv->i2c_props, gainset_off, 2);
189 tuner_i2c_xfer_send(&priv->i2c_props, if_agc_spd, 2);
Michael Krufkyb2083192007-05-29 22:54:06 -0300190 if (priv->tda8290_easy_mode & 0x60)
Michael Krufkydb8a6952007-08-21 01:24:42 -0300191 tuner_i2c_xfer_send(&priv->i2c_props, adc_head_9, 2);
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800192 else
Michael Krufkydb8a6952007-08-21 01:24:42 -0300193 tuner_i2c_xfer_send(&priv->i2c_props, adc_head_6, 2);
194 tuner_i2c_xfer_send(&priv->i2c_props, pll_bw_nom, 2);
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800195
Michael Krufky746d97322007-08-25 19:08:45 -0300196 tda8290_i2c_bridge(t, 1);
197
198 if (t->fe.ops.tuner_ops.set_analog_params)
199 t->fe.ops.tuner_ops.set_analog_params(&t->fe, &params);
200
Hartmut Hackmann58ef4f92007-04-27 12:31:12 -0300201 for (i = 0; i < 3; i++) {
Michael Krufkydb8a6952007-08-21 01:24:42 -0300202 tuner_i2c_xfer_send(&priv->i2c_props, &addr_pll_stat, 1);
203 tuner_i2c_xfer_recv(&priv->i2c_props, &pll_stat, 1);
Hartmut Hackmann58ef4f92007-04-27 12:31:12 -0300204 if (pll_stat & 0x80) {
Michael Krufkydb8a6952007-08-21 01:24:42 -0300205 tuner_i2c_xfer_send(&priv->i2c_props, &addr_adc_sat, 1);
206 tuner_i2c_xfer_recv(&priv->i2c_props, &adc_sat, 1);
207 tuner_i2c_xfer_send(&priv->i2c_props, &addr_agc_stat, 1);
208 tuner_i2c_xfer_recv(&priv->i2c_props, &agc_stat, 1);
Hartmut Hackmann58ef4f92007-04-27 12:31:12 -0300209 tuner_dbg("tda8290 is locked, AGC: %d\n", agc_stat);
210 break;
211 } else {
212 tuner_dbg("tda8290 not locked, no signal?\n");
213 msleep(100);
214 }
215 }
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800216 /* adjust headroom resp. gain */
Hartmut Hackmann4ac95af2005-11-08 21:38:38 -0800217 if ((agc_stat > 115) || (!(pll_stat & 0x80) && (adc_sat < 20))) {
218 tuner_dbg("adjust gain, step 1. Agc: %d, ADC stat: %d, lock: %d\n",
219 agc_stat, adc_sat, pll_stat & 0x80);
Michael Krufkydb8a6952007-08-21 01:24:42 -0300220 tuner_i2c_xfer_send(&priv->i2c_props, gainset_2, 2);
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800221 msleep(100);
Michael Krufkydb8a6952007-08-21 01:24:42 -0300222 tuner_i2c_xfer_send(&priv->i2c_props, &addr_agc_stat, 1);
223 tuner_i2c_xfer_recv(&priv->i2c_props, &agc_stat, 1);
224 tuner_i2c_xfer_send(&priv->i2c_props, &addr_pll_stat, 1);
225 tuner_i2c_xfer_recv(&priv->i2c_props, &pll_stat, 1);
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800226 if ((agc_stat > 115) || !(pll_stat & 0x80)) {
Hartmut Hackmann4ac95af2005-11-08 21:38:38 -0800227 tuner_dbg("adjust gain, step 2. Agc: %d, lock: %d\n",
228 agc_stat, pll_stat & 0x80);
Michael Krufky746d97322007-08-25 19:08:45 -0300229 if (priv->cfg.agcf)
230 priv->cfg.agcf(&t->fe);
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800231 msleep(100);
Michael Krufkydb8a6952007-08-21 01:24:42 -0300232 tuner_i2c_xfer_send(&priv->i2c_props, &addr_agc_stat, 1);
233 tuner_i2c_xfer_recv(&priv->i2c_props, &agc_stat, 1);
234 tuner_i2c_xfer_send(&priv->i2c_props, &addr_pll_stat, 1);
235 tuner_i2c_xfer_recv(&priv->i2c_props, &pll_stat, 1);
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800236 if((agc_stat > 115) || !(pll_stat & 0x80)) {
237 tuner_dbg("adjust gain, step 3. Agc: %d\n", agc_stat);
Michael Krufkydb8a6952007-08-21 01:24:42 -0300238 tuner_i2c_xfer_send(&priv->i2c_props, adc_head_12, 2);
239 tuner_i2c_xfer_send(&priv->i2c_props, pll_bw_low, 2);
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800240 msleep(100);
241 }
242 }
243 }
244
245 /* l/ l' deadlock? */
Michael Krufkyb2083192007-05-29 22:54:06 -0300246 if(priv->tda8290_easy_mode & 0x60) {
Michael Krufkydb8a6952007-08-21 01:24:42 -0300247 tuner_i2c_xfer_send(&priv->i2c_props, &addr_adc_sat, 1);
248 tuner_i2c_xfer_recv(&priv->i2c_props, &adc_sat, 1);
249 tuner_i2c_xfer_send(&priv->i2c_props, &addr_pll_stat, 1);
250 tuner_i2c_xfer_recv(&priv->i2c_props, &pll_stat, 1);
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800251 if ((adc_sat > 20) || !(pll_stat & 0x80)) {
Hartmut Hackmann4ac95af2005-11-08 21:38:38 -0800252 tuner_dbg("trying to resolve SECAM L deadlock\n");
Michael Krufkydb8a6952007-08-21 01:24:42 -0300253 tuner_i2c_xfer_send(&priv->i2c_props, agc_rst_on, 2);
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800254 msleep(40);
Michael Krufkydb8a6952007-08-21 01:24:42 -0300255 tuner_i2c_xfer_send(&priv->i2c_props, agc_rst_off, 2);
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800256 }
257 }
258
Michael Krufky746d97322007-08-25 19:08:45 -0300259 tda8290_i2c_bridge(t, 0);
Michael Krufkydb8a6952007-08-21 01:24:42 -0300260 tuner_i2c_xfer_send(&priv->i2c_props, if_agc_set, 2);
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800261}
262
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800263/*---------------------------------------------------------------------*/
264
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300265static void tda8295_power(struct tuner *t, int enable)
266{
267 struct tda8290_priv *priv = t->priv;
268 unsigned char buf[] = { 0x30, 0x00 }; /* clb_stdbt */
269
270 tuner_i2c_xfer_send(&priv->i2c_props, &buf[0], 1);
271 tuner_i2c_xfer_recv(&priv->i2c_props, &buf[1], 1);
272
273 if (enable)
274 buf[1] = 0x01;
275 else
276 buf[1] = 0x03;
277
278 tuner_i2c_xfer_send(&priv->i2c_props, buf, 2);
279}
280
281static void tda8295_set_easy_mode(struct tuner *t, int enable)
282{
283 struct tda8290_priv *priv = t->priv;
284 unsigned char buf[] = { 0x01, 0x00 };
285
286 tuner_i2c_xfer_send(&priv->i2c_props, &buf[0], 1);
287 tuner_i2c_xfer_recv(&priv->i2c_props, &buf[1], 1);
288
289 if (enable)
290 buf[1] = 0x01; /* rising edge sets regs 0x02 - 0x23 */
291 else
292 buf[1] = 0x00; /* reset active bit */
293
294 tuner_i2c_xfer_send(&priv->i2c_props, buf, 2);
295}
296
297static void tda8295_set_video_std(struct tuner *t)
298{
299 struct tda8290_priv *priv = t->priv;
300 unsigned char buf[] = { 0x00, priv->tda8290_easy_mode };
301
302 tuner_i2c_xfer_send(&priv->i2c_props, buf, 2);
303
304 tda8295_set_easy_mode(t, 1);
305 msleep(20);
306 tda8295_set_easy_mode(t, 0);
307}
308
309/*---------------------------------------------------------------------*/
310
311static void tda8295_agc1_out(struct tuner *t, int enable)
312{
313 struct tda8290_priv *priv = t->priv;
314 unsigned char buf[] = { 0x02, 0x00 }; /* DIV_FUNC */
315
316 tuner_i2c_xfer_send(&priv->i2c_props, &buf[0], 1);
317 tuner_i2c_xfer_recv(&priv->i2c_props, &buf[1], 1);
318
319 if (enable)
320 buf[1] &= ~0x40;
321 else
322 buf[1] |= 0x40;
323
324 tuner_i2c_xfer_send(&priv->i2c_props, buf, 2);
325}
326
327static void tda8295_agc2_out(struct tuner *t, int enable)
328{
329 struct tda8290_priv *priv = t->priv;
330 unsigned char set_gpio_cf[] = { 0x44, 0x00 };
331 unsigned char set_gpio_val[] = { 0x46, 0x00 };
332
333 tuner_i2c_xfer_send(&priv->i2c_props, &set_gpio_cf[0], 1);
334 tuner_i2c_xfer_recv(&priv->i2c_props, &set_gpio_cf[1], 1);
335 tuner_i2c_xfer_send(&priv->i2c_props, &set_gpio_val[0], 1);
336 tuner_i2c_xfer_recv(&priv->i2c_props, &set_gpio_val[1], 1);
337
338 set_gpio_cf[1] &= 0xf0; /* clear GPIO_0 bits 3-0 */
339
340 if (enable) {
341 set_gpio_cf[1] |= 0x01; /* config GPIO_0 as Open Drain Out */
342 set_gpio_val[1] &= 0xfe; /* set GPIO_0 pin low */
343 }
344 tuner_i2c_xfer_send(&priv->i2c_props, set_gpio_cf, 2);
345 tuner_i2c_xfer_send(&priv->i2c_props, set_gpio_val, 2);
346}
347
348static int tda8295_has_signal(struct tuner *t)
349{
350 struct tda8290_priv *priv = t->priv;
351
352 unsigned char hvpll_stat = 0x26;
353 unsigned char ret;
354
355 tuner_i2c_xfer_send(&priv->i2c_props, &hvpll_stat, 1);
356 tuner_i2c_xfer_recv(&priv->i2c_props, &ret, 1);
357 return (ret & 0x01) ? 65535 : 0;
358}
359
360/*---------------------------------------------------------------------*/
361
362static void tda8295_set_freq(struct tuner *t, unsigned int freq)
363{
364 struct tda8290_priv *priv = t->priv;
365 u16 ifc;
366
367 unsigned char blanking_mode[] = { 0x1d, 0x00 };
368
369 struct analog_parameters params = {
370 .frequency = freq,
371 .mode = t->mode,
372 .audmode = t->audmode,
373 .std = t->std
374 };
375
376 set_audio(t);
377
378 ifc = priv->cfg.sgIF; /* FIXME */
379
380 tuner_dbg("%s: ifc = %u, freq = %d\n", __FUNCTION__, ifc, freq);
381
382 tda8295_power(t, 1);
383 tda8295_agc1_out(t, 1);
384
385 tuner_i2c_xfer_send(&priv->i2c_props, &blanking_mode[0], 1);
386 tuner_i2c_xfer_recv(&priv->i2c_props, &blanking_mode[1], 1);
387
388 tda8295_set_video_std(t);
389
390 blanking_mode[1] = 0x03;
391 tuner_i2c_xfer_send(&priv->i2c_props, blanking_mode, 2);
392 msleep(20);
393
394 tda8295_i2c_bridge(t, 1);
395
396 if (t->fe.ops.tuner_ops.set_analog_params)
397 t->fe.ops.tuner_ops.set_analog_params(&t->fe, &params);
398
399 if (priv->cfg.agcf)
400 priv->cfg.agcf(&t->fe);
401
402 if (tda8295_has_signal(t))
403 tuner_dbg("tda8295 is locked\n");
404 else
405 tuner_dbg("tda8295 not locked, no signal?\n");
406
407 tda8295_i2c_bridge(t, 0);
408}
409
410/*---------------------------------------------------------------------*/
411
Michael Krufky746d97322007-08-25 19:08:45 -0300412static int tda8290_has_signal(struct tuner *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
Michael Krufky746d97322007-08-25 19:08:45 -0300414 struct tda8290_priv *priv = t->priv;
Michael Krufkydb8a6952007-08-21 01:24:42 -0300415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 unsigned char i2c_get_afc[1] = { 0x1B };
417 unsigned char afc = 0;
418
Michael Krufkydb8a6952007-08-21 01:24:42 -0300419 tuner_i2c_xfer_send(&priv->i2c_props, i2c_get_afc, ARRAY_SIZE(i2c_get_afc));
420 tuner_i2c_xfer_recv(&priv->i2c_props, &afc, 1);
Michael Krufky746d97322007-08-25 19:08:45 -0300421 return (afc & 0x80)? 65535:0;
Michael Krufky910bb3e2007-08-27 21:22:20 -0300422}
423
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800424/*---------------------------------------------------------------------*/
425
Michael Krufky746d97322007-08-25 19:08:45 -0300426static void tda8290_standby(struct tuner *t)
Mauro Carvalho Chehab793cf9e2005-09-09 13:03:37 -0700427{
Michael Krufky746d97322007-08-25 19:08:45 -0300428 struct tda8290_priv *priv = t->priv;
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800429 unsigned char cb1[] = { 0x30, 0xD0 };
430 unsigned char tda8290_standby[] = { 0x00, 0x02 };
Hartmut Hackmann0157a9c2006-02-07 06:49:09 -0200431 unsigned char tda8290_agc_tri[] = { 0x02, 0x20 };
Michael Krufkyb2083192007-05-29 22:54:06 -0300432 struct i2c_msg msg = {.addr = priv->tda827x_addr, .flags=0, .buf=cb1, .len = 2};
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800433
Michael Krufky746d97322007-08-25 19:08:45 -0300434 tda8290_i2c_bridge(t, 1);
Michael Krufkyb2083192007-05-29 22:54:06 -0300435 if (priv->tda827x_ver != 0)
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800436 cb1[1] = 0x90;
Michael Krufkydb8a6952007-08-21 01:24:42 -0300437 i2c_transfer(priv->i2c_props.adap, &msg, 1);
Michael Krufky746d97322007-08-25 19:08:45 -0300438 tda8290_i2c_bridge(t, 0);
Michael Krufkydb8a6952007-08-21 01:24:42 -0300439 tuner_i2c_xfer_send(&priv->i2c_props, tda8290_agc_tri, 2);
440 tuner_i2c_xfer_send(&priv->i2c_props, tda8290_standby, 2);
Mauro Carvalho Chehab793cf9e2005-09-09 13:03:37 -0700441}
442
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300443static void tda8295_standby(struct tuner *t)
444{
445 tda8295_agc1_out(t, 0); /* Put AGC in tri-state */
446
447 tda8295_power(t, 0);
448}
449
Michael Krufky746d97322007-08-25 19:08:45 -0300450static void tda8290_init_if(struct tuner *t)
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800451{
Michael Krufky746d97322007-08-25 19:08:45 -0300452 struct tda8290_priv *priv = t->priv;
Michael Krufkydb8a6952007-08-21 01:24:42 -0300453
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800454 unsigned char set_VS[] = { 0x30, 0x6F };
Hartmut Hackmann58ef4f92007-04-27 12:31:12 -0300455 unsigned char set_GP00_CF[] = { 0x20, 0x01 };
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800456 unsigned char set_GP01_CF[] = { 0x20, 0x0B };
457
Michael Krufky746d97322007-08-25 19:08:45 -0300458 if ((t->config == 1) || (t->config == 2))
Michael Krufkydb8a6952007-08-21 01:24:42 -0300459 tuner_i2c_xfer_send(&priv->i2c_props, set_GP00_CF, 2);
Hartmut Hackmann58ef4f92007-04-27 12:31:12 -0300460 else
Michael Krufkydb8a6952007-08-21 01:24:42 -0300461 tuner_i2c_xfer_send(&priv->i2c_props, set_GP01_CF, 2);
462 tuner_i2c_xfer_send(&priv->i2c_props, set_VS, 2);
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800463}
464
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300465static void tda8295_init_if(struct tuner *t)
466{
467 struct tda8290_priv *priv = t->priv;
468
469 static unsigned char set_adc_ctl[] = { 0x33, 0x14 };
470 static unsigned char set_adc_ctl2[] = { 0x34, 0x00 };
471 static unsigned char set_pll_reg6[] = { 0x3e, 0x63 };
472 static unsigned char set_pll_reg0[] = { 0x38, 0x23 };
473 static unsigned char set_pll_reg7[] = { 0x3f, 0x01 };
474 static unsigned char set_pll_reg10[] = { 0x42, 0x61 };
475 static unsigned char set_gpio_reg0[] = { 0x44, 0x0b };
476
477 tda8295_power(t, 1);
478
479 tda8295_set_easy_mode(t, 0);
480 tda8295_set_video_std(t);
481
482 tuner_i2c_xfer_send(&priv->i2c_props, set_adc_ctl, 2);
483 tuner_i2c_xfer_send(&priv->i2c_props, set_adc_ctl2, 2);
484 tuner_i2c_xfer_send(&priv->i2c_props, set_pll_reg6, 2);
485 tuner_i2c_xfer_send(&priv->i2c_props, set_pll_reg0, 2);
486 tuner_i2c_xfer_send(&priv->i2c_props, set_pll_reg7, 2);
487 tuner_i2c_xfer_send(&priv->i2c_props, set_pll_reg10, 2);
488 tuner_i2c_xfer_send(&priv->i2c_props, set_gpio_reg0, 2);
489
490 tda8295_agc1_out(t, 0);
491 tda8295_agc2_out(t, 0);
492}
493
Michael Krufky746d97322007-08-25 19:08:45 -0300494static void tda8290_init_tuner(struct tuner *t)
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800495{
Michael Krufky746d97322007-08-25 19:08:45 -0300496 struct tda8290_priv *priv = t->priv;
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800497 unsigned char tda8275_init[] = { 0x00, 0x00, 0x00, 0x40, 0xdC, 0x04, 0xAf,
Nickolay V. Shmyrev9a741ec2005-11-08 21:37:50 -0800498 0x3F, 0x2A, 0x04, 0xFF, 0x00, 0x00, 0x40 };
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800499 unsigned char tda8275a_init[] = { 0x00, 0x00, 0x00, 0x00, 0xdC, 0x05, 0x8b,
Nickolay V. Shmyrev9a741ec2005-11-08 21:37:50 -0800500 0x0c, 0x04, 0x20, 0xFF, 0x00, 0x00, 0x4b };
Michael Krufkyb2083192007-05-29 22:54:06 -0300501 struct i2c_msg msg = {.addr = priv->tda827x_addr, .flags=0,
Ricardo Cerqueirac2f6f9d2005-11-08 21:37:51 -0800502 .buf=tda8275_init, .len = 14};
Michael Krufkyb2083192007-05-29 22:54:06 -0300503 if (priv->tda827x_ver != 0)
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800504 msg.buf = tda8275a_init;
505
Michael Krufky746d97322007-08-25 19:08:45 -0300506 tda8290_i2c_bridge(t, 1);
Michael Krufkydb8a6952007-08-21 01:24:42 -0300507 i2c_transfer(priv->i2c_props.adap, &msg, 1);
Michael Krufky746d97322007-08-25 19:08:45 -0300508 tda8290_i2c_bridge(t, 0);
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800509}
510
511/*---------------------------------------------------------------------*/
512
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300513static void tda829x_release(struct tuner *t)
Michael Krufky024cf532007-06-04 15:20:11 -0300514{
Michael Krufky746d97322007-08-25 19:08:45 -0300515 if (t->fe.ops.tuner_ops.release)
516 t->fe.ops.tuner_ops.release(&t->fe);
Michael Krufky910bb3e2007-08-27 21:22:20 -0300517
Michael Krufky746d97322007-08-25 19:08:45 -0300518 kfree(t->priv);
519 t->priv = NULL;
Michael Krufky024cf532007-06-04 15:20:11 -0300520}
521
Michael Krufky1dde7a42007-10-21 13:40:56 -0300522static struct analog_tuner_ops tda8290_tuner_ops = {
Michael Krufky746d97322007-08-25 19:08:45 -0300523 .set_tv_freq = tda8290_set_freq,
524 .set_radio_freq = tda8290_set_freq,
525 .has_signal = tda8290_has_signal,
526 .standby = tda8290_standby,
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300527 .release = tda829x_release,
528};
529
Michael Krufky1dde7a42007-10-21 13:40:56 -0300530static struct analog_tuner_ops tda8295_tuner_ops = {
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300531 .set_tv_freq = tda8295_set_freq,
532 .set_radio_freq = tda8295_set_freq,
533 .has_signal = tda8295_has_signal,
534 .standby = tda8295_standby,
535 .release = tda829x_release,
Michael Krufky7fd8b262007-06-06 16:15:15 -0300536};
537
Michael Krufky746d97322007-08-25 19:08:45 -0300538int tda8290_attach(struct tuner *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539{
Michael Krufkyb2083192007-05-29 22:54:06 -0300540 struct tda8290_priv *priv = NULL;
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800541 u8 data;
542 int i, ret, tuners_found;
543 u32 tuner_addrs;
544 struct i2c_msg msg = {.flags=I2C_M_RD, .buf=&data, .len = 1};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Michael Krufkyb2083192007-05-29 22:54:06 -0300546 priv = kzalloc(sizeof(struct tda8290_priv), GFP_KERNEL);
547 if (priv == NULL)
Michael Krufky746d97322007-08-25 19:08:45 -0300548 return -ENOMEM;
549 t->priv = priv;
Michael Krufkyb2083192007-05-29 22:54:06 -0300550
Michael Krufky746d97322007-08-25 19:08:45 -0300551 priv->i2c_props.addr = t->i2c.addr;
552 priv->i2c_props.adap = t->i2c.adapter;
553 priv->cfg.config = &t->config;
554 priv->cfg.tuner_callback = t->tuner_callback;
Michael Krufkydb8a6952007-08-21 01:24:42 -0300555
Michael Krufky746d97322007-08-25 19:08:45 -0300556 tda8290_i2c_bridge(t, 1);
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800557 /* probe for tuner chip */
558 tuners_found = 0;
559 tuner_addrs = 0;
560 for (i=0x60; i<= 0x63; i++) {
561 msg.addr = i;
Michael Krufkydb8a6952007-08-21 01:24:42 -0300562 ret = i2c_transfer(priv->i2c_props.adap, &msg, 1);
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800563 if (ret == 1) {
564 tuners_found++;
565 tuner_addrs = (tuner_addrs << 8) + i;
566 }
567 }
568 /* if there is more than one tuner, we expect the right one is
569 behind the bridge and we choose the highest address that doesn't
570 give a response now
571 */
Michael Krufky746d97322007-08-25 19:08:45 -0300572 tda8290_i2c_bridge(t, 0);
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800573 if(tuners_found > 1)
574 for (i = 0; i < tuners_found; i++) {
575 msg.addr = tuner_addrs & 0xff;
Michael Krufkydb8a6952007-08-21 01:24:42 -0300576 ret = i2c_transfer(priv->i2c_props.adap, &msg, 1);
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800577 if(ret == 1)
578 tuner_addrs = tuner_addrs >> 8;
579 else
580 break;
581 }
582 if (tuner_addrs == 0) {
583 tuner_addrs = 0x61;
Michael Krufky910bb3e2007-08-27 21:22:20 -0300584 tuner_info("could not clearly identify tuner address, defaulting to %x\n",
Nickolay V. Shmyrev01cb9632005-11-08 21:38:00 -0800585 tuner_addrs);
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800586 } else {
587 tuner_addrs = tuner_addrs & 0xff;
Michael Krufky910bb3e2007-08-27 21:22:20 -0300588 tuner_info("setting tuner address to %x\n", tuner_addrs);
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800589 }
Michael Krufkyb2083192007-05-29 22:54:06 -0300590 priv->tda827x_addr = tuner_addrs;
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800591 msg.addr = tuner_addrs;
592
Michael Krufky746d97322007-08-25 19:08:45 -0300593 tda8290_i2c_bridge(t, 1);
594
Michael Krufkydb8a6952007-08-21 01:24:42 -0300595 ret = i2c_transfer(priv->i2c_props.adap, &msg, 1);
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800596 if( ret != 1)
Michael Krufky910bb3e2007-08-27 21:22:20 -0300597 tuner_warn("TDA827x access failed!\n");
598
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800599 if ((data & 0x3c) == 0) {
Michael Krufky746d97322007-08-25 19:08:45 -0300600 strlcpy(t->i2c.name, "tda8290+75", sizeof(t->i2c.name));
Michael Krufkyb2083192007-05-29 22:54:06 -0300601 priv->tda827x_ver = 0;
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800602 } else {
Michael Krufky746d97322007-08-25 19:08:45 -0300603 strlcpy(t->i2c.name, "tda8290+75a", sizeof(t->i2c.name));
Michael Krufkyb2083192007-05-29 22:54:06 -0300604 priv->tda827x_ver = 2;
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800605 }
Michael Krufky746d97322007-08-25 19:08:45 -0300606 tda827x_attach(&t->fe, priv->tda827x_addr,
607 priv->i2c_props.adap, &priv->cfg);
Michael Krufky7fd8b262007-06-06 16:15:15 -0300608
Michael Krufky746d97322007-08-25 19:08:45 -0300609 /* FIXME: tda827x module doesn't probe the tuner until
610 * tda827x_initial_sleep is called
611 */
612 if (t->fe.ops.tuner_ops.sleep)
613 t->fe.ops.tuner_ops.sleep(&t->fe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Michael Krufky1dde7a42007-10-21 13:40:56 -0300615 t->fe.ops.analog_demod_ops = &tda8290_tuner_ops;
Michael Krufky746d97322007-08-25 19:08:45 -0300616
617 tuner_info("type set to %s\n", t->i2c.name);
618
619 priv->cfg.tda827x_lpsel = 0;
620 t->mode = V4L2_TUNER_ANALOG_TV;
621
622 tda8290_init_tuner(t);
623 tda8290_init_if(t);
624 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625}
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300626EXPORT_SYMBOL_GPL(tda8290_attach);
627
628int tda8295_attach(struct tuner *t)
629{
630 struct tda8290_priv *priv = NULL;
631 u8 data;
632 int i, ret, tuners_found;
633 u32 tuner_addrs;
634 struct i2c_msg msg = { .flags = I2C_M_RD, .buf = &data, .len = 1 };
635
636 priv = kzalloc(sizeof(struct tda8290_priv), GFP_KERNEL);
637 if (priv == NULL)
638 return -ENOMEM;
639 t->priv = priv;
640
641 priv->i2c_props.addr = t->i2c.addr;
642 priv->i2c_props.adap = t->i2c.adapter;
643
644 tda8295_i2c_bridge(t, 1);
645 /* probe for tuner chip */
646 tuners_found = 0;
647 tuner_addrs = 0;
648 for (i = 0x60; i <= 0x63; i++) {
649 msg.addr = i;
650 ret = i2c_transfer(priv->i2c_props.adap, &msg, 1);
651 if (ret == 1) {
652 tuners_found++;
653 tuner_addrs = (tuner_addrs << 8) + i;
654 }
655 }
656 /* if there is more than one tuner, we expect the right one is
657 behind the bridge and we choose the highest address that doesn't
658 give a response now
659 */
660 tda8295_i2c_bridge(t, 0);
661 if (tuners_found > 1)
662 for (i = 0; i < tuners_found; i++) {
663 msg.addr = tuner_addrs & 0xff;
664 ret = i2c_transfer(priv->i2c_props.adap, &msg, 1);
665 if (ret == 1)
666 tuner_addrs = tuner_addrs >> 8;
667 else
668 break;
669 }
670 if (tuner_addrs == 0) {
671 tuner_addrs = 0x60;
672 tuner_info("could not clearly identify tuner address, "
673 "defaulting to %x\n", tuner_addrs);
674 } else {
675 tuner_addrs = tuner_addrs & 0xff;
676 tuner_info("setting tuner address to %x\n", tuner_addrs);
677 }
678 priv->tda827x_addr = tuner_addrs;
679 msg.addr = tuner_addrs;
680
681 tda8295_i2c_bridge(t, 1);
682 ret = i2c_transfer(priv->i2c_props.adap, &msg, 1);
683 tda8295_i2c_bridge(t, 0);
684 if (ret != 1)
685 tuner_warn("TDA827x access failed!\n");
686 if ((data & 0x3c) == 0) {
687 strlcpy(t->i2c.name, "tda8295+18271", sizeof(t->i2c.name));
688 tda18271_attach(&t->fe, priv->tda827x_addr,
689 priv->i2c_props.adap);
690 priv->tda827x_ver = 4;
691 } else {
692 strlcpy(t->i2c.name, "tda8295+75a", sizeof(t->i2c.name));
693 tda827x_attach(&t->fe, priv->tda827x_addr,
694 priv->i2c_props.adap, &priv->cfg);
695
696 /* FIXME: tda827x module doesn't probe the tuner until
697 * tda827x_initial_sleep is called
698 */
699 if (t->fe.ops.tuner_ops.sleep)
700 t->fe.ops.tuner_ops.sleep(&t->fe);
701 priv->tda827x_ver = 2;
702 }
703 priv->tda827x_ver |= 1; /* signifies 8295 vs 8290 */
704 tuner_info("type set to %s\n", t->i2c.name);
705
Michael Krufky1dde7a42007-10-21 13:40:56 -0300706 t->fe.ops.analog_demod_ops = &tda8295_tuner_ops;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300707
708 priv->cfg.tda827x_lpsel = 0;
709 t->mode = V4L2_TUNER_ANALOG_TV;
710
711 tda8295_init_if(t);
712 return 0;
713}
714EXPORT_SYMBOL_GPL(tda8295_attach);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Michael Krufky746d97322007-08-25 19:08:45 -0300716int tda8290_probe(struct tuner *t)
Hartmut Hackmann95736032005-11-08 21:38:00 -0800717{
Michael Krufky910bb3e2007-08-27 21:22:20 -0300718 struct tuner_i2c_props i2c_props = {
Michael Krufky746d97322007-08-25 19:08:45 -0300719 .adap = t->i2c.adapter,
720 .addr = t->i2c.addr
Michael Krufky910bb3e2007-08-27 21:22:20 -0300721 };
Michael Krufkydb8a6952007-08-21 01:24:42 -0300722
Hartmut Hackmann44fd06f2006-02-27 00:09:11 -0300723 unsigned char soft_reset[] = { 0x00, 0x00 };
724 unsigned char easy_mode_b[] = { 0x01, 0x02 };
725 unsigned char easy_mode_g[] = { 0x01, 0x04 };
726 unsigned char restore_9886[] = { 0x00, 0xd6, 0x30 };
Hartmut Hackmann95736032005-11-08 21:38:00 -0800727 unsigned char addr_dto_lsb = 0x07;
728 unsigned char data;
729
Michael Krufky910bb3e2007-08-27 21:22:20 -0300730 tuner_i2c_xfer_send(&i2c_props, easy_mode_b, 2);
731 tuner_i2c_xfer_send(&i2c_props, soft_reset, 2);
732 tuner_i2c_xfer_send(&i2c_props, &addr_dto_lsb, 1);
733 tuner_i2c_xfer_recv(&i2c_props, &data, 1);
Hartmut Hackmann95736032005-11-08 21:38:00 -0800734 if (data == 0) {
Michael Krufky910bb3e2007-08-27 21:22:20 -0300735 tuner_i2c_xfer_send(&i2c_props, easy_mode_g, 2);
736 tuner_i2c_xfer_send(&i2c_props, soft_reset, 2);
737 tuner_i2c_xfer_send(&i2c_props, &addr_dto_lsb, 1);
738 tuner_i2c_xfer_recv(&i2c_props, &data, 1);
Hartmut Hackmann95736032005-11-08 21:38:00 -0800739 if (data == 0x7b) {
740 return 0;
741 }
742 }
Michael Krufky910bb3e2007-08-27 21:22:20 -0300743 tuner_i2c_xfer_send(&i2c_props, restore_9886, 3);
Hartmut Hackmann95736032005-11-08 21:38:00 -0800744 return -1;
745}
Michael Krufky910bb3e2007-08-27 21:22:20 -0300746EXPORT_SYMBOL_GPL(tda8290_probe);
Michael Krufky910bb3e2007-08-27 21:22:20 -0300747
748MODULE_DESCRIPTION("Philips TDA8290 + TDA8275 / TDA8275a tuner driver");
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300749MODULE_AUTHOR("Gerd Knorr, Hartmut Hackmann, Michael Krufky");
Michael Krufky910bb3e2007-08-27 21:22:20 -0300750MODULE_LICENSE("GPL");
751
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752/*
753 * Overrides for Emacs so that we follow Linus's tabbing style.
754 * ---------------------------------------------------------------------------
755 * Local variables:
756 * c-basic-offset: 8
757 * End:
758 */