blob: 4ac7c04388966ddbdb1b924048b788d99ac9daeb [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 Krufkyab166052007-12-09 02:26:48 -030026#include "tuner-i2c.h"
Michael Krufky910bb3e2007-08-27 21:22:20 -030027#include "tda8290.h"
Michael Krufky746d97322007-08-25 19:08:45 -030028#include "tda827x.h"
Michael Krufky5bea1cd2007-10-22 09:56:38 -030029#include "tda18271.h"
Michael Krufky910bb3e2007-08-27 21:22:20 -030030
Michael Krufkyab166052007-12-09 02:26:48 -030031static int debug;
32module_param(debug, int, 0644);
Michael Krufky910bb3e2007-08-27 21:22:20 -030033MODULE_PARM_DESC(debug, "enable verbose debug messages");
34
Hans Verkuil9dd659d2007-11-04 11:03:36 -030035#define PREFIX "tda8290"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37/* ---------------------------------------------------------------------- */
38
Michael Krufkyb2083192007-05-29 22:54:06 -030039struct tda8290_priv {
Michael Krufkydb8a6952007-08-21 01:24:42 -030040 struct tuner_i2c_props i2c_props;
41
Michael Krufkyb2083192007-05-29 22:54:06 -030042 unsigned char tda8290_easy_mode;
Michael Krufky746d97322007-08-25 19:08:45 -030043
Michael Krufkyb2083192007-05-29 22:54:06 -030044 unsigned char tda827x_addr;
Michael Krufky8c125f2c2007-10-27 02:00:57 -030045
46 unsigned char ver;
47#define TDA8290 1
48#define TDA8295 2
49#define TDA8275 4
50#define TDA8275A 8
51#define TDA18271 16
Michael Krufky910bb3e2007-08-27 21:22:20 -030052
Michael Krufky746d97322007-08-25 19:08:45 -030053 struct tda827x_config cfg;
Michael Krufkyb2083192007-05-29 22:54:06 -030054};
55
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -080056/*---------------------------------------------------------------------*/
57
Michael Krufkya72dd302007-10-24 09:30:17 -030058static int tda8290_i2c_bridge(struct dvb_frontend *fe, int close)
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -080059{
Michael Krufky4e9154b2007-10-21 19:39:50 -030060 struct tda8290_priv *priv = fe->analog_demod_priv;
Michael Krufkydb8a6952007-08-21 01:24:42 -030061
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -080062 unsigned char enable[2] = { 0x21, 0xC0 };
Hartmut Hackmann0157a9c2006-02-07 06:49:09 -020063 unsigned char disable[2] = { 0x21, 0x00 };
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -080064 unsigned char *msg;
Michael Krufkya72dd302007-10-24 09:30:17 -030065
66 if (close) {
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -080067 msg = enable;
Michael Krufkydb8a6952007-08-21 01:24:42 -030068 tuner_i2c_xfer_send(&priv->i2c_props, msg, 2);
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -080069 /* let the bridge stabilize */
70 msleep(20);
71 } else {
72 msg = disable;
Michael Krufkydb8a6952007-08-21 01:24:42 -030073 tuner_i2c_xfer_send(&priv->i2c_props, msg, 2);
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -080074 }
Michael Krufkya72dd302007-10-24 09:30:17 -030075
76 return 0;
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -080077}
78
Michael Krufkya72dd302007-10-24 09:30:17 -030079static int tda8295_i2c_bridge(struct dvb_frontend *fe, int close)
Michael Krufky5bea1cd2007-10-22 09:56:38 -030080{
Michael Krufky4e9154b2007-10-21 19:39:50 -030081 struct tda8290_priv *priv = fe->analog_demod_priv;
Michael Krufky5bea1cd2007-10-22 09:56:38 -030082
83 unsigned char enable[2] = { 0x45, 0xc1 };
84 unsigned char disable[2] = { 0x46, 0x00 };
85 unsigned char buf[3] = { 0x45, 0x01, 0x00 };
86 unsigned char *msg;
Michael Krufkya72dd302007-10-24 09:30:17 -030087
Michael Krufky5bea1cd2007-10-22 09:56:38 -030088 if (close) {
89 msg = enable;
90 tuner_i2c_xfer_send(&priv->i2c_props, msg, 2);
91 /* let the bridge stabilize */
92 msleep(20);
93 } else {
94 msg = disable;
95 tuner_i2c_xfer_send(&priv->i2c_props, msg, 1);
96 tuner_i2c_xfer_recv(&priv->i2c_props, &msg[1], 1);
97
98 buf[2] = msg[1];
99 buf[2] &= ~0x04;
100 tuner_i2c_xfer_send(&priv->i2c_props, buf, 3);
101 msleep(5);
102
103 msg[1] |= 0x04;
104 tuner_i2c_xfer_send(&priv->i2c_props, msg, 2);
105 }
Michael Krufkya72dd302007-10-24 09:30:17 -0300106
107 return 0;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300108}
109
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800110/*---------------------------------------------------------------------*/
111
Michael Krufkyc7919d52007-12-08 17:06:30 -0300112static void set_audio(struct dvb_frontend *fe,
113 struct analog_parameters *params)
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800114{
Michael Krufky4e9154b2007-10-21 19:39:50 -0300115 struct tda8290_priv *priv = fe->analog_demod_priv;
Michael Krufky910bb3e2007-08-27 21:22:20 -0300116 char* mode;
117
Michael Krufkyc7919d52007-12-08 17:06:30 -0300118 if (params->std & V4L2_STD_MN) {
Michael Krufky910bb3e2007-08-27 21:22:20 -0300119 priv->tda8290_easy_mode = 0x01;
Michael Krufky910bb3e2007-08-27 21:22:20 -0300120 mode = "MN";
Michael Krufkyc7919d52007-12-08 17:06:30 -0300121 } else if (params->std & V4L2_STD_B) {
Michael Krufky910bb3e2007-08-27 21:22:20 -0300122 priv->tda8290_easy_mode = 0x02;
123 mode = "B";
Michael Krufkyc7919d52007-12-08 17:06:30 -0300124 } else if (params->std & V4L2_STD_GH) {
Michael Krufky910bb3e2007-08-27 21:22:20 -0300125 priv->tda8290_easy_mode = 0x04;
126 mode = "GH";
Michael Krufkyc7919d52007-12-08 17:06:30 -0300127 } else if (params->std & V4L2_STD_PAL_I) {
Michael Krufky910bb3e2007-08-27 21:22:20 -0300128 priv->tda8290_easy_mode = 0x08;
129 mode = "I";
Michael Krufkyc7919d52007-12-08 17:06:30 -0300130 } else if (params->std & V4L2_STD_DK) {
Michael Krufky910bb3e2007-08-27 21:22:20 -0300131 priv->tda8290_easy_mode = 0x10;
132 mode = "DK";
Michael Krufkyc7919d52007-12-08 17:06:30 -0300133 } else if (params->std & V4L2_STD_SECAM_L) {
Michael Krufky910bb3e2007-08-27 21:22:20 -0300134 priv->tda8290_easy_mode = 0x20;
135 mode = "L";
Michael Krufkyc7919d52007-12-08 17:06:30 -0300136 } else if (params->std & V4L2_STD_SECAM_LC) {
Michael Krufky910bb3e2007-08-27 21:22:20 -0300137 priv->tda8290_easy_mode = 0x40;
138 mode = "LC";
139 } else {
Michael Krufky910bb3e2007-08-27 21:22:20 -0300140 priv->tda8290_easy_mode = 0x10;
141 mode = "xx";
142 }
143
Michael Krufky8c125f2c2007-10-27 02:00:57 -0300144 tuner_dbg("setting tda829x to system %s\n", mode);
Michael Krufky910bb3e2007-08-27 21:22:20 -0300145}
146
Michael Krufkyc7919d52007-12-08 17:06:30 -0300147static void tda8290_set_params(struct dvb_frontend *fe,
148 struct analog_parameters *params)
Michael Krufky910bb3e2007-08-27 21:22:20 -0300149{
Michael Krufky4e9154b2007-10-21 19:39:50 -0300150 struct tda8290_priv *priv = fe->analog_demod_priv;
Michael Krufky4e9154b2007-10-21 19:39:50 -0300151
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800152 unsigned char soft_reset[] = { 0x00, 0x00 };
Michael Krufkyb2083192007-05-29 22:54:06 -0300153 unsigned char easy_mode[] = { 0x01, priv->tda8290_easy_mode };
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800154 unsigned char expert_mode[] = { 0x01, 0x80 };
Hartmut Hackmann0157a9c2006-02-07 06:49:09 -0200155 unsigned char agc_out_on[] = { 0x02, 0x00 };
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800156 unsigned char gainset_off[] = { 0x28, 0x14 };
157 unsigned char if_agc_spd[] = { 0x0f, 0x88 };
158 unsigned char adc_head_6[] = { 0x05, 0x04 };
159 unsigned char adc_head_9[] = { 0x05, 0x02 };
160 unsigned char adc_head_12[] = { 0x05, 0x01 };
161 unsigned char pll_bw_nom[] = { 0x0d, 0x47 };
162 unsigned char pll_bw_low[] = { 0x0d, 0x27 };
163 unsigned char gainset_2[] = { 0x28, 0x64 };
164 unsigned char agc_rst_on[] = { 0x0e, 0x0b };
165 unsigned char agc_rst_off[] = { 0x0e, 0x09 };
166 unsigned char if_agc_set[] = { 0x0f, 0x81 };
167 unsigned char addr_adc_sat = 0x1a;
168 unsigned char addr_agc_stat = 0x1d;
169 unsigned char addr_pll_stat = 0x1b;
170 unsigned char adc_sat, agc_stat,
Nickolay V. Shmyrev9a741ec2005-11-08 21:37:50 -0800171 pll_stat;
Hartmut Hackmann58ef4f92007-04-27 12:31:12 -0300172 int i;
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800173
Michael Krufkyc7919d52007-12-08 17:06:30 -0300174 set_audio(fe, params);
Michael Krufky746d97322007-08-25 19:08:45 -0300175
Michael Krufkyab166052007-12-09 02:26:48 -0300176 if (priv->cfg.config)
177 tuner_dbg("tda827xa config is 0x%02x\n", *priv->cfg.config);
Michael Krufkydb8a6952007-08-21 01:24:42 -0300178 tuner_i2c_xfer_send(&priv->i2c_props, easy_mode, 2);
179 tuner_i2c_xfer_send(&priv->i2c_props, agc_out_on, 2);
180 tuner_i2c_xfer_send(&priv->i2c_props, soft_reset, 2);
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800181 msleep(1);
182
Michael Krufkyb2083192007-05-29 22:54:06 -0300183 expert_mode[1] = priv->tda8290_easy_mode + 0x80;
Michael Krufkydb8a6952007-08-21 01:24:42 -0300184 tuner_i2c_xfer_send(&priv->i2c_props, expert_mode, 2);
185 tuner_i2c_xfer_send(&priv->i2c_props, gainset_off, 2);
186 tuner_i2c_xfer_send(&priv->i2c_props, if_agc_spd, 2);
Michael Krufkyb2083192007-05-29 22:54:06 -0300187 if (priv->tda8290_easy_mode & 0x60)
Michael Krufkydb8a6952007-08-21 01:24:42 -0300188 tuner_i2c_xfer_send(&priv->i2c_props, adc_head_9, 2);
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800189 else
Michael Krufkydb8a6952007-08-21 01:24:42 -0300190 tuner_i2c_xfer_send(&priv->i2c_props, adc_head_6, 2);
191 tuner_i2c_xfer_send(&priv->i2c_props, pll_bw_nom, 2);
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800192
Michael Krufky4e9154b2007-10-21 19:39:50 -0300193 tda8290_i2c_bridge(fe, 1);
Michael Krufky746d97322007-08-25 19:08:45 -0300194
Michael Krufky4e9154b2007-10-21 19:39:50 -0300195 if (fe->ops.tuner_ops.set_analog_params)
Michael Krufkyc7919d52007-12-08 17:06:30 -0300196 fe->ops.tuner_ops.set_analog_params(fe, params);
Michael Krufky746d97322007-08-25 19:08:45 -0300197
Hartmut Hackmann58ef4f92007-04-27 12:31:12 -0300198 for (i = 0; i < 3; i++) {
Michael Krufkydb8a6952007-08-21 01:24:42 -0300199 tuner_i2c_xfer_send(&priv->i2c_props, &addr_pll_stat, 1);
200 tuner_i2c_xfer_recv(&priv->i2c_props, &pll_stat, 1);
Hartmut Hackmann58ef4f92007-04-27 12:31:12 -0300201 if (pll_stat & 0x80) {
Michael Krufkydb8a6952007-08-21 01:24:42 -0300202 tuner_i2c_xfer_send(&priv->i2c_props, &addr_adc_sat, 1);
203 tuner_i2c_xfer_recv(&priv->i2c_props, &adc_sat, 1);
204 tuner_i2c_xfer_send(&priv->i2c_props, &addr_agc_stat, 1);
205 tuner_i2c_xfer_recv(&priv->i2c_props, &agc_stat, 1);
Hartmut Hackmann58ef4f92007-04-27 12:31:12 -0300206 tuner_dbg("tda8290 is locked, AGC: %d\n", agc_stat);
207 break;
208 } else {
209 tuner_dbg("tda8290 not locked, no signal?\n");
210 msleep(100);
211 }
212 }
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800213 /* adjust headroom resp. gain */
Hartmut Hackmann4ac95af2005-11-08 21:38:38 -0800214 if ((agc_stat > 115) || (!(pll_stat & 0x80) && (adc_sat < 20))) {
215 tuner_dbg("adjust gain, step 1. Agc: %d, ADC stat: %d, lock: %d\n",
216 agc_stat, adc_sat, pll_stat & 0x80);
Michael Krufkydb8a6952007-08-21 01:24:42 -0300217 tuner_i2c_xfer_send(&priv->i2c_props, gainset_2, 2);
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800218 msleep(100);
Michael Krufkydb8a6952007-08-21 01:24:42 -0300219 tuner_i2c_xfer_send(&priv->i2c_props, &addr_agc_stat, 1);
220 tuner_i2c_xfer_recv(&priv->i2c_props, &agc_stat, 1);
221 tuner_i2c_xfer_send(&priv->i2c_props, &addr_pll_stat, 1);
222 tuner_i2c_xfer_recv(&priv->i2c_props, &pll_stat, 1);
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800223 if ((agc_stat > 115) || !(pll_stat & 0x80)) {
Hartmut Hackmann4ac95af2005-11-08 21:38:38 -0800224 tuner_dbg("adjust gain, step 2. Agc: %d, lock: %d\n",
225 agc_stat, pll_stat & 0x80);
Michael Krufky746d97322007-08-25 19:08:45 -0300226 if (priv->cfg.agcf)
Michael Krufky4e9154b2007-10-21 19:39:50 -0300227 priv->cfg.agcf(fe);
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800228 msleep(100);
Michael Krufkydb8a6952007-08-21 01:24:42 -0300229 tuner_i2c_xfer_send(&priv->i2c_props, &addr_agc_stat, 1);
230 tuner_i2c_xfer_recv(&priv->i2c_props, &agc_stat, 1);
231 tuner_i2c_xfer_send(&priv->i2c_props, &addr_pll_stat, 1);
232 tuner_i2c_xfer_recv(&priv->i2c_props, &pll_stat, 1);
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800233 if((agc_stat > 115) || !(pll_stat & 0x80)) {
234 tuner_dbg("adjust gain, step 3. Agc: %d\n", agc_stat);
Michael Krufkydb8a6952007-08-21 01:24:42 -0300235 tuner_i2c_xfer_send(&priv->i2c_props, adc_head_12, 2);
236 tuner_i2c_xfer_send(&priv->i2c_props, pll_bw_low, 2);
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800237 msleep(100);
238 }
239 }
240 }
241
242 /* l/ l' deadlock? */
Michael Krufkyb2083192007-05-29 22:54:06 -0300243 if(priv->tda8290_easy_mode & 0x60) {
Michael Krufkydb8a6952007-08-21 01:24:42 -0300244 tuner_i2c_xfer_send(&priv->i2c_props, &addr_adc_sat, 1);
245 tuner_i2c_xfer_recv(&priv->i2c_props, &adc_sat, 1);
246 tuner_i2c_xfer_send(&priv->i2c_props, &addr_pll_stat, 1);
247 tuner_i2c_xfer_recv(&priv->i2c_props, &pll_stat, 1);
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800248 if ((adc_sat > 20) || !(pll_stat & 0x80)) {
Hartmut Hackmann4ac95af2005-11-08 21:38:38 -0800249 tuner_dbg("trying to resolve SECAM L deadlock\n");
Michael Krufkydb8a6952007-08-21 01:24:42 -0300250 tuner_i2c_xfer_send(&priv->i2c_props, agc_rst_on, 2);
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800251 msleep(40);
Michael Krufkydb8a6952007-08-21 01:24:42 -0300252 tuner_i2c_xfer_send(&priv->i2c_props, agc_rst_off, 2);
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800253 }
254 }
255
Michael Krufky4e9154b2007-10-21 19:39:50 -0300256 tda8290_i2c_bridge(fe, 0);
Michael Krufkydb8a6952007-08-21 01:24:42 -0300257 tuner_i2c_xfer_send(&priv->i2c_props, if_agc_set, 2);
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800258}
259
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800260/*---------------------------------------------------------------------*/
261
Michael Krufky4e9154b2007-10-21 19:39:50 -0300262static void tda8295_power(struct dvb_frontend *fe, int enable)
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300263{
Michael Krufky4e9154b2007-10-21 19:39:50 -0300264 struct tda8290_priv *priv = fe->analog_demod_priv;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300265 unsigned char buf[] = { 0x30, 0x00 }; /* clb_stdbt */
266
267 tuner_i2c_xfer_send(&priv->i2c_props, &buf[0], 1);
268 tuner_i2c_xfer_recv(&priv->i2c_props, &buf[1], 1);
269
270 if (enable)
271 buf[1] = 0x01;
272 else
273 buf[1] = 0x03;
274
275 tuner_i2c_xfer_send(&priv->i2c_props, buf, 2);
276}
277
Michael Krufky4e9154b2007-10-21 19:39:50 -0300278static void tda8295_set_easy_mode(struct dvb_frontend *fe, int enable)
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300279{
Michael Krufky4e9154b2007-10-21 19:39:50 -0300280 struct tda8290_priv *priv = fe->analog_demod_priv;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300281 unsigned char buf[] = { 0x01, 0x00 };
282
283 tuner_i2c_xfer_send(&priv->i2c_props, &buf[0], 1);
284 tuner_i2c_xfer_recv(&priv->i2c_props, &buf[1], 1);
285
286 if (enable)
287 buf[1] = 0x01; /* rising edge sets regs 0x02 - 0x23 */
288 else
289 buf[1] = 0x00; /* reset active bit */
290
291 tuner_i2c_xfer_send(&priv->i2c_props, buf, 2);
292}
293
Michael Krufky4e9154b2007-10-21 19:39:50 -0300294static void tda8295_set_video_std(struct dvb_frontend *fe)
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300295{
Michael Krufky4e9154b2007-10-21 19:39:50 -0300296 struct tda8290_priv *priv = fe->analog_demod_priv;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300297 unsigned char buf[] = { 0x00, priv->tda8290_easy_mode };
298
299 tuner_i2c_xfer_send(&priv->i2c_props, buf, 2);
300
Michael Krufky4e9154b2007-10-21 19:39:50 -0300301 tda8295_set_easy_mode(fe, 1);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300302 msleep(20);
Michael Krufky4e9154b2007-10-21 19:39:50 -0300303 tda8295_set_easy_mode(fe, 0);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300304}
305
306/*---------------------------------------------------------------------*/
307
Michael Krufky4e9154b2007-10-21 19:39:50 -0300308static void tda8295_agc1_out(struct dvb_frontend *fe, int enable)
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300309{
Michael Krufky4e9154b2007-10-21 19:39:50 -0300310 struct tda8290_priv *priv = fe->analog_demod_priv;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300311 unsigned char buf[] = { 0x02, 0x00 }; /* DIV_FUNC */
312
313 tuner_i2c_xfer_send(&priv->i2c_props, &buf[0], 1);
314 tuner_i2c_xfer_recv(&priv->i2c_props, &buf[1], 1);
315
316 if (enable)
317 buf[1] &= ~0x40;
318 else
319 buf[1] |= 0x40;
320
321 tuner_i2c_xfer_send(&priv->i2c_props, buf, 2);
322}
323
Michael Krufky4e9154b2007-10-21 19:39:50 -0300324static void tda8295_agc2_out(struct dvb_frontend *fe, int enable)
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300325{
Michael Krufky4e9154b2007-10-21 19:39:50 -0300326 struct tda8290_priv *priv = fe->analog_demod_priv;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300327 unsigned char set_gpio_cf[] = { 0x44, 0x00 };
328 unsigned char set_gpio_val[] = { 0x46, 0x00 };
329
330 tuner_i2c_xfer_send(&priv->i2c_props, &set_gpio_cf[0], 1);
331 tuner_i2c_xfer_recv(&priv->i2c_props, &set_gpio_cf[1], 1);
332 tuner_i2c_xfer_send(&priv->i2c_props, &set_gpio_val[0], 1);
333 tuner_i2c_xfer_recv(&priv->i2c_props, &set_gpio_val[1], 1);
334
335 set_gpio_cf[1] &= 0xf0; /* clear GPIO_0 bits 3-0 */
336
337 if (enable) {
338 set_gpio_cf[1] |= 0x01; /* config GPIO_0 as Open Drain Out */
339 set_gpio_val[1] &= 0xfe; /* set GPIO_0 pin low */
340 }
341 tuner_i2c_xfer_send(&priv->i2c_props, set_gpio_cf, 2);
342 tuner_i2c_xfer_send(&priv->i2c_props, set_gpio_val, 2);
343}
344
Michael Krufky4e9154b2007-10-21 19:39:50 -0300345static int tda8295_has_signal(struct dvb_frontend *fe)
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300346{
Michael Krufky4e9154b2007-10-21 19:39:50 -0300347 struct tda8290_priv *priv = fe->analog_demod_priv;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300348
349 unsigned char hvpll_stat = 0x26;
350 unsigned char ret;
351
352 tuner_i2c_xfer_send(&priv->i2c_props, &hvpll_stat, 1);
353 tuner_i2c_xfer_recv(&priv->i2c_props, &ret, 1);
354 return (ret & 0x01) ? 65535 : 0;
355}
356
357/*---------------------------------------------------------------------*/
358
Michael Krufkyc7919d52007-12-08 17:06:30 -0300359static void tda8295_set_params(struct dvb_frontend *fe,
360 struct analog_parameters *params)
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300361{
Michael Krufky4e9154b2007-10-21 19:39:50 -0300362 struct tda8290_priv *priv = fe->analog_demod_priv;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300363
364 unsigned char blanking_mode[] = { 0x1d, 0x00 };
365
Michael Krufkyc7919d52007-12-08 17:06:30 -0300366 set_audio(fe, params);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300367
Michael Krufkyc7919d52007-12-08 17:06:30 -0300368 tuner_dbg("%s: freq = %d\n", __FUNCTION__, params->frequency);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300369
Michael Krufky4e9154b2007-10-21 19:39:50 -0300370 tda8295_power(fe, 1);
371 tda8295_agc1_out(fe, 1);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300372
373 tuner_i2c_xfer_send(&priv->i2c_props, &blanking_mode[0], 1);
374 tuner_i2c_xfer_recv(&priv->i2c_props, &blanking_mode[1], 1);
375
Michael Krufky4e9154b2007-10-21 19:39:50 -0300376 tda8295_set_video_std(fe);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300377
378 blanking_mode[1] = 0x03;
379 tuner_i2c_xfer_send(&priv->i2c_props, blanking_mode, 2);
380 msleep(20);
381
Michael Krufky4e9154b2007-10-21 19:39:50 -0300382 tda8295_i2c_bridge(fe, 1);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300383
Michael Krufky4e9154b2007-10-21 19:39:50 -0300384 if (fe->ops.tuner_ops.set_analog_params)
Michael Krufkyc7919d52007-12-08 17:06:30 -0300385 fe->ops.tuner_ops.set_analog_params(fe, params);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300386
387 if (priv->cfg.agcf)
Michael Krufky4e9154b2007-10-21 19:39:50 -0300388 priv->cfg.agcf(fe);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300389
Michael Krufky4e9154b2007-10-21 19:39:50 -0300390 if (tda8295_has_signal(fe))
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300391 tuner_dbg("tda8295 is locked\n");
392 else
393 tuner_dbg("tda8295 not locked, no signal?\n");
394
Michael Krufky4e9154b2007-10-21 19:39:50 -0300395 tda8295_i2c_bridge(fe, 0);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300396}
397
398/*---------------------------------------------------------------------*/
399
Michael Krufky4e9154b2007-10-21 19:39:50 -0300400static int tda8290_has_signal(struct dvb_frontend *fe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401{
Michael Krufky4e9154b2007-10-21 19:39:50 -0300402 struct tda8290_priv *priv = fe->analog_demod_priv;
Michael Krufkydb8a6952007-08-21 01:24:42 -0300403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 unsigned char i2c_get_afc[1] = { 0x1B };
405 unsigned char afc = 0;
406
Michael Krufkydb8a6952007-08-21 01:24:42 -0300407 tuner_i2c_xfer_send(&priv->i2c_props, i2c_get_afc, ARRAY_SIZE(i2c_get_afc));
408 tuner_i2c_xfer_recv(&priv->i2c_props, &afc, 1);
Michael Krufky746d97322007-08-25 19:08:45 -0300409 return (afc & 0x80)? 65535:0;
Michael Krufky910bb3e2007-08-27 21:22:20 -0300410}
411
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800412/*---------------------------------------------------------------------*/
413
Michael Krufky4e9154b2007-10-21 19:39:50 -0300414static void tda8290_standby(struct dvb_frontend *fe)
Mauro Carvalho Chehab793cf9e2005-09-09 13:03:37 -0700415{
Michael Krufky4e9154b2007-10-21 19:39:50 -0300416 struct tda8290_priv *priv = fe->analog_demod_priv;
417
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800418 unsigned char cb1[] = { 0x30, 0xD0 };
419 unsigned char tda8290_standby[] = { 0x00, 0x02 };
Hartmut Hackmann0157a9c2006-02-07 06:49:09 -0200420 unsigned char tda8290_agc_tri[] = { 0x02, 0x20 };
Michael Krufkyb2083192007-05-29 22:54:06 -0300421 struct i2c_msg msg = {.addr = priv->tda827x_addr, .flags=0, .buf=cb1, .len = 2};
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800422
Michael Krufky4e9154b2007-10-21 19:39:50 -0300423 tda8290_i2c_bridge(fe, 1);
Michael Krufky8c125f2c2007-10-27 02:00:57 -0300424 if (priv->ver & TDA8275A)
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800425 cb1[1] = 0x90;
Michael Krufkydb8a6952007-08-21 01:24:42 -0300426 i2c_transfer(priv->i2c_props.adap, &msg, 1);
Michael Krufky4e9154b2007-10-21 19:39:50 -0300427 tda8290_i2c_bridge(fe, 0);
Michael Krufkydb8a6952007-08-21 01:24:42 -0300428 tuner_i2c_xfer_send(&priv->i2c_props, tda8290_agc_tri, 2);
429 tuner_i2c_xfer_send(&priv->i2c_props, tda8290_standby, 2);
Mauro Carvalho Chehab793cf9e2005-09-09 13:03:37 -0700430}
431
Michael Krufky4e9154b2007-10-21 19:39:50 -0300432static void tda8295_standby(struct dvb_frontend *fe)
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300433{
Michael Krufky4e9154b2007-10-21 19:39:50 -0300434 tda8295_agc1_out(fe, 0); /* Put AGC in tri-state */
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300435
Michael Krufky4e9154b2007-10-21 19:39:50 -0300436 tda8295_power(fe, 0);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300437}
438
Michael Krufky4e9154b2007-10-21 19:39:50 -0300439static void tda8290_init_if(struct dvb_frontend *fe)
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800440{
Michael Krufky4e9154b2007-10-21 19:39:50 -0300441 struct tda8290_priv *priv = fe->analog_demod_priv;
Michael Krufkydb8a6952007-08-21 01:24:42 -0300442
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800443 unsigned char set_VS[] = { 0x30, 0x6F };
Hartmut Hackmann58ef4f92007-04-27 12:31:12 -0300444 unsigned char set_GP00_CF[] = { 0x20, 0x01 };
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800445 unsigned char set_GP01_CF[] = { 0x20, 0x0B };
446
Michael Krufkyab166052007-12-09 02:26:48 -0300447 if ((priv->cfg.config) &&
448 ((*priv->cfg.config == 1) || (*priv->cfg.config == 2)))
Michael Krufkydb8a6952007-08-21 01:24:42 -0300449 tuner_i2c_xfer_send(&priv->i2c_props, set_GP00_CF, 2);
Hartmut Hackmann58ef4f92007-04-27 12:31:12 -0300450 else
Michael Krufkydb8a6952007-08-21 01:24:42 -0300451 tuner_i2c_xfer_send(&priv->i2c_props, set_GP01_CF, 2);
452 tuner_i2c_xfer_send(&priv->i2c_props, set_VS, 2);
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800453}
454
Michael Krufky4e9154b2007-10-21 19:39:50 -0300455static void tda8295_init_if(struct dvb_frontend *fe)
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300456{
Michael Krufky4e9154b2007-10-21 19:39:50 -0300457 struct tda8290_priv *priv = fe->analog_demod_priv;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300458
459 static unsigned char set_adc_ctl[] = { 0x33, 0x14 };
460 static unsigned char set_adc_ctl2[] = { 0x34, 0x00 };
461 static unsigned char set_pll_reg6[] = { 0x3e, 0x63 };
462 static unsigned char set_pll_reg0[] = { 0x38, 0x23 };
463 static unsigned char set_pll_reg7[] = { 0x3f, 0x01 };
464 static unsigned char set_pll_reg10[] = { 0x42, 0x61 };
465 static unsigned char set_gpio_reg0[] = { 0x44, 0x0b };
466
Michael Krufky4e9154b2007-10-21 19:39:50 -0300467 tda8295_power(fe, 1);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300468
Michael Krufky4e9154b2007-10-21 19:39:50 -0300469 tda8295_set_easy_mode(fe, 0);
470 tda8295_set_video_std(fe);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300471
472 tuner_i2c_xfer_send(&priv->i2c_props, set_adc_ctl, 2);
473 tuner_i2c_xfer_send(&priv->i2c_props, set_adc_ctl2, 2);
474 tuner_i2c_xfer_send(&priv->i2c_props, set_pll_reg6, 2);
475 tuner_i2c_xfer_send(&priv->i2c_props, set_pll_reg0, 2);
476 tuner_i2c_xfer_send(&priv->i2c_props, set_pll_reg7, 2);
477 tuner_i2c_xfer_send(&priv->i2c_props, set_pll_reg10, 2);
478 tuner_i2c_xfer_send(&priv->i2c_props, set_gpio_reg0, 2);
479
Michael Krufky4e9154b2007-10-21 19:39:50 -0300480 tda8295_agc1_out(fe, 0);
481 tda8295_agc2_out(fe, 0);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300482}
483
Michael Krufky4e9154b2007-10-21 19:39:50 -0300484static void tda8290_init_tuner(struct dvb_frontend *fe)
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800485{
Michael Krufky4e9154b2007-10-21 19:39:50 -0300486 struct tda8290_priv *priv = fe->analog_demod_priv;
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800487 unsigned char tda8275_init[] = { 0x00, 0x00, 0x00, 0x40, 0xdC, 0x04, 0xAf,
Nickolay V. Shmyrev9a741ec2005-11-08 21:37:50 -0800488 0x3F, 0x2A, 0x04, 0xFF, 0x00, 0x00, 0x40 };
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800489 unsigned char tda8275a_init[] = { 0x00, 0x00, 0x00, 0x00, 0xdC, 0x05, 0x8b,
Nickolay V. Shmyrev9a741ec2005-11-08 21:37:50 -0800490 0x0c, 0x04, 0x20, 0xFF, 0x00, 0x00, 0x4b };
Michael Krufkyb2083192007-05-29 22:54:06 -0300491 struct i2c_msg msg = {.addr = priv->tda827x_addr, .flags=0,
Ricardo Cerqueirac2f6f9d2005-11-08 21:37:51 -0800492 .buf=tda8275_init, .len = 14};
Michael Krufky8c125f2c2007-10-27 02:00:57 -0300493 if (priv->ver & TDA8275A)
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800494 msg.buf = tda8275a_init;
495
Michael Krufky4e9154b2007-10-21 19:39:50 -0300496 tda8290_i2c_bridge(fe, 1);
Michael Krufkydb8a6952007-08-21 01:24:42 -0300497 i2c_transfer(priv->i2c_props.adap, &msg, 1);
Michael Krufky4e9154b2007-10-21 19:39:50 -0300498 tda8290_i2c_bridge(fe, 0);
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800499}
500
501/*---------------------------------------------------------------------*/
502
Michael Krufky4e9154b2007-10-21 19:39:50 -0300503static void tda829x_release(struct dvb_frontend *fe)
Michael Krufky024cf532007-06-04 15:20:11 -0300504{
Michael Krufkya4f263b2008-01-06 15:52:56 -0300505 struct tda8290_priv *priv = fe->analog_demod_priv;
506
507 /* dont try to release the tuner
508 * if we didn't attach it from this module */
509 if ((priv->ver > TDA8290) && (priv->ver > TDA8295))
510 if (fe->ops.tuner_ops.release)
511 fe->ops.tuner_ops.release(fe);
Michael Krufky910bb3e2007-08-27 21:22:20 -0300512
Michael Krufky4e9154b2007-10-21 19:39:50 -0300513 kfree(fe->analog_demod_priv);
514 fe->analog_demod_priv = NULL;
Michael Krufky024cf532007-06-04 15:20:11 -0300515}
516
Michael Krufkyf21e0d72008-01-02 03:01:54 -0300517static struct tda18271_config tda829x_tda18271_config = {
518 .gate = TDA18271_GATE_ANALOG,
519};
520
Michael Krufky8c125f2c2007-10-27 02:00:57 -0300521static int tda829x_find_tuner(struct dvb_frontend *fe)
522{
523 struct tda8290_priv *priv = fe->analog_demod_priv;
Michael Krufkybc3e5c72007-12-21 11:18:32 -0300524 struct analog_demod_ops *analog_ops = &fe->ops.analog_ops;
Michael Krufky8c125f2c2007-10-27 02:00:57 -0300525 int i, ret, tuners_found;
526 u32 tuner_addrs;
527 u8 data;
528 struct i2c_msg msg = { .flags = I2C_M_RD, .buf = &data, .len = 1 };
529
Michael Krufkybc3e5c72007-12-21 11:18:32 -0300530 if (NULL == analog_ops->i2c_gate_ctrl)
Michael Krufky8c125f2c2007-10-27 02:00:57 -0300531 return -EINVAL;
532
Michael Krufkybc3e5c72007-12-21 11:18:32 -0300533 analog_ops->i2c_gate_ctrl(fe, 1);
Michael Krufky8c125f2c2007-10-27 02:00:57 -0300534
535 /* probe for tuner chip */
536 tuners_found = 0;
537 tuner_addrs = 0;
538 for (i = 0x60; i <= 0x63; i++) {
539 msg.addr = i;
540 ret = i2c_transfer(priv->i2c_props.adap, &msg, 1);
541 if (ret == 1) {
542 tuners_found++;
543 tuner_addrs = (tuner_addrs << 8) + i;
544 }
545 }
546 /* if there is more than one tuner, we expect the right one is
547 behind the bridge and we choose the highest address that doesn't
548 give a response now
549 */
550
Michael Krufkybc3e5c72007-12-21 11:18:32 -0300551 analog_ops->i2c_gate_ctrl(fe, 0);
Michael Krufky8c125f2c2007-10-27 02:00:57 -0300552
553 if (tuners_found > 1)
554 for (i = 0; i < tuners_found; i++) {
555 msg.addr = tuner_addrs & 0xff;
556 ret = i2c_transfer(priv->i2c_props.adap, &msg, 1);
557 if (ret == 1)
558 tuner_addrs = tuner_addrs >> 8;
559 else
560 break;
561 }
562
563 if (tuner_addrs == 0) {
564 tuner_addrs = 0x60;
565 tuner_info("could not clearly identify tuner address, "
566 "defaulting to %x\n", tuner_addrs);
567 } else {
568 tuner_addrs = tuner_addrs & 0xff;
569 tuner_info("setting tuner address to %x\n", tuner_addrs);
570 }
571 priv->tda827x_addr = tuner_addrs;
572 msg.addr = tuner_addrs;
573
Michael Krufkybc3e5c72007-12-21 11:18:32 -0300574 analog_ops->i2c_gate_ctrl(fe, 1);
Michael Krufky8c125f2c2007-10-27 02:00:57 -0300575 ret = i2c_transfer(priv->i2c_props.adap, &msg, 1);
576
577 if (ret != 1) {
578 tuner_warn("tuner access failed!\n");
579 return -EREMOTEIO;
580 }
581
Michael Krufky255b5112008-01-01 22:52:09 -0300582 if ((data == 0x83) || (data == 0x84)) {
Michael Krufky8c125f2c2007-10-27 02:00:57 -0300583 priv->ver |= TDA18271;
Michael Krufky68816472007-12-08 16:25:41 -0300584 tda18271_attach(fe, priv->tda827x_addr,
Michael Krufkye435f952007-12-09 22:23:30 -0300585 priv->i2c_props.adap,
Michael Krufkyf21e0d72008-01-02 03:01:54 -0300586 &tda829x_tda18271_config);
Michael Krufky8c125f2c2007-10-27 02:00:57 -0300587 } else {
588 if ((data & 0x3c) == 0)
589 priv->ver |= TDA8275;
590 else
591 priv->ver |= TDA8275A;
592
Michael Krufky68816472007-12-08 16:25:41 -0300593 tda827x_attach(fe, priv->tda827x_addr,
Michael Krufky8c125f2c2007-10-27 02:00:57 -0300594 priv->i2c_props.adap, &priv->cfg);
Michael Krufky8c125f2c2007-10-27 02:00:57 -0300595 }
Michael Krufky68816472007-12-08 16:25:41 -0300596 if (fe->ops.tuner_ops.init)
597 fe->ops.tuner_ops.init(fe);
Michael Krufky63c25482007-11-23 15:08:11 -0300598
Michael Krufky68816472007-12-08 16:25:41 -0300599 if (fe->ops.tuner_ops.sleep)
600 fe->ops.tuner_ops.sleep(fe);
Michael Krufky63c25482007-11-23 15:08:11 -0300601
Michael Krufkybc3e5c72007-12-21 11:18:32 -0300602 analog_ops->i2c_gate_ctrl(fe, 0);
Michael Krufky8c125f2c2007-10-27 02:00:57 -0300603
Michael Krufky8c125f2c2007-10-27 02:00:57 -0300604 return 0;
605}
606
Michael Krufkyf1f32842007-11-03 22:14:54 -0300607static int tda8290_probe(struct tuner_i2c_props *i2c_props)
608{
609#define TDA8290_ID 0x89
610 unsigned char tda8290_id[] = { 0x1f, 0x00 };
611
612 /* detect tda8290 */
613 tuner_i2c_xfer_send(i2c_props, &tda8290_id[0], 1);
614 tuner_i2c_xfer_recv(i2c_props, &tda8290_id[1], 1);
615
616 if (tda8290_id[1] == TDA8290_ID) {
Michael Krufkyab166052007-12-09 02:26:48 -0300617 if (debug)
Michael Krufkyf1f32842007-11-03 22:14:54 -0300618 printk(KERN_DEBUG "%s: tda8290 detected @ %d-%04x\n",
619 __FUNCTION__, i2c_adapter_id(i2c_props->adap),
620 i2c_props->addr);
621 return 0;
622 }
623
Michael Krufky1f3a4e322007-11-04 10:51:28 -0300624 return -ENODEV;
Michael Krufkyf1f32842007-11-03 22:14:54 -0300625}
626
627static int tda8295_probe(struct tuner_i2c_props *i2c_props)
628{
629#define TDA8295_ID 0x8a
630 unsigned char tda8295_id[] = { 0x2f, 0x00 };
631
632 /* detect tda8295 */
633 tuner_i2c_xfer_send(i2c_props, &tda8295_id[0], 1);
634 tuner_i2c_xfer_recv(i2c_props, &tda8295_id[1], 1);
635
636 if (tda8295_id[1] == TDA8295_ID) {
Michael Krufkyab166052007-12-09 02:26:48 -0300637 if (debug)
Michael Krufkyf1f32842007-11-03 22:14:54 -0300638 printk(KERN_DEBUG "%s: tda8295 detected @ %d-%04x\n",
639 __FUNCTION__, i2c_adapter_id(i2c_props->adap),
640 i2c_props->addr);
641 return 0;
642 }
643
Michael Krufky1f3a4e322007-11-04 10:51:28 -0300644 return -ENODEV;
Michael Krufkyf1f32842007-11-03 22:14:54 -0300645}
646
Michael Krufkybc3e5c72007-12-21 11:18:32 -0300647static struct analog_demod_ops tda8290_ops = {
Michael Krufkya55db8c2007-12-09 13:52:51 -0300648 .info = {
649 .name = "TDA8290",
650 },
Michael Krufkyc7919d52007-12-08 17:06:30 -0300651 .set_params = tda8290_set_params,
Michael Krufky746d97322007-08-25 19:08:45 -0300652 .has_signal = tda8290_has_signal,
653 .standby = tda8290_standby,
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300654 .release = tda829x_release,
Michael Krufkya72dd302007-10-24 09:30:17 -0300655 .i2c_gate_ctrl = tda8290_i2c_bridge,
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300656};
657
Michael Krufkybc3e5c72007-12-21 11:18:32 -0300658static struct analog_demod_ops tda8295_ops = {
Michael Krufkya55db8c2007-12-09 13:52:51 -0300659 .info = {
660 .name = "TDA8295",
661 },
Michael Krufkyc7919d52007-12-08 17:06:30 -0300662 .set_params = tda8295_set_params,
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300663 .has_signal = tda8295_has_signal,
664 .standby = tda8295_standby,
665 .release = tda829x_release,
Michael Krufkya72dd302007-10-24 09:30:17 -0300666 .i2c_gate_ctrl = tda8295_i2c_bridge,
Michael Krufky7fd8b262007-06-06 16:15:15 -0300667};
668
Michael Krufkyab166052007-12-09 02:26:48 -0300669struct dvb_frontend *tda829x_attach(struct dvb_frontend *fe,
670 struct i2c_adapter *i2c_adap, u8 i2c_addr,
671 struct tda829x_config *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
Michael Krufkyb2083192007-05-29 22:54:06 -0300673 struct tda8290_priv *priv = NULL;
Michael Krufkyab166052007-12-09 02:26:48 -0300674 char *name;
Michael Krufky8c125f2c2007-10-27 02:00:57 -0300675
Michael Krufkyb2083192007-05-29 22:54:06 -0300676 priv = kzalloc(sizeof(struct tda8290_priv), GFP_KERNEL);
677 if (priv == NULL)
Michael Krufkyab166052007-12-09 02:26:48 -0300678 return NULL;
Michael Krufky8c125f2c2007-10-27 02:00:57 -0300679 fe->analog_demod_priv = priv;
Michael Krufkyb2083192007-05-29 22:54:06 -0300680
Michael Krufkyab166052007-12-09 02:26:48 -0300681 priv->i2c_props.addr = i2c_addr;
682 priv->i2c_props.adap = i2c_adap;
683 if (cfg) {
684 priv->cfg.config = cfg->lna_cfg;
685 priv->cfg.tuner_callback = cfg->tuner_callback;
686 }
Michael Krufkydb8a6952007-08-21 01:24:42 -0300687
Michael Krufkyf1f32842007-11-03 22:14:54 -0300688 if (tda8290_probe(&priv->i2c_props) == 0) {
Michael Krufky8c125f2c2007-10-27 02:00:57 -0300689 priv->ver = TDA8290;
Michael Krufkybc3e5c72007-12-21 11:18:32 -0300690 memcpy(&fe->ops.analog_ops, &tda8290_ops,
691 sizeof(struct analog_demod_ops));
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800692 }
Michael Krufky8c125f2c2007-10-27 02:00:57 -0300693
Michael Krufkyf1f32842007-11-03 22:14:54 -0300694 if (tda8295_probe(&priv->i2c_props) == 0) {
Michael Krufky8c125f2c2007-10-27 02:00:57 -0300695 priv->ver = TDA8295;
Michael Krufkybc3e5c72007-12-21 11:18:32 -0300696 memcpy(&fe->ops.analog_ops, &tda8295_ops,
697 sizeof(struct analog_demod_ops));
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800698 }
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800699
Michael Krufkyc9076272007-12-24 04:36:14 -0300700 if ((!(cfg) || (TDA829X_PROBE_TUNER == cfg->probe_tuner)) &&
701 (tda829x_find_tuner(fe) < 0))
Michael Krufkyfa746aee2007-12-09 05:16:10 -0300702 goto fail;
Michael Krufky746d97322007-08-25 19:08:45 -0300703
Michael Krufkyab166052007-12-09 02:26:48 -0300704 switch (priv->ver) {
Michael Krufkyc9076272007-12-24 04:36:14 -0300705 case TDA8290:
706 name = "tda8290";
707 break;
708 case TDA8295:
709 name = "tda8295";
710 break;
Michael Krufkyab166052007-12-09 02:26:48 -0300711 case TDA8290 | TDA8275:
712 name = "tda8290+75";
713 break;
714 case TDA8295 | TDA8275:
715 name = "tda8295+75";
716 break;
717 case TDA8290 | TDA8275A:
718 name = "tda8290+75a";
719 break;
720 case TDA8295 | TDA8275A:
721 name = "tda8295+75a";
722 break;
723 case TDA8290 | TDA18271:
724 name = "tda8290+18271";
725 break;
726 case TDA8295 | TDA18271:
727 name = "tda8295+18271";
728 break;
729 default:
730 goto fail;
731 }
732 tuner_info("type set to %s\n", name);
733
Michael Krufky8c125f2c2007-10-27 02:00:57 -0300734 if (priv->ver & TDA8290) {
735 tda8290_init_tuner(fe);
736 tda8290_init_if(fe);
737 } else if (priv->ver & TDA8295)
738 tda8295_init_if(fe);
Michael Krufky746d97322007-08-25 19:08:45 -0300739
Michael Krufkyab166052007-12-09 02:26:48 -0300740 return fe;
Michael Krufkyfa746aee2007-12-09 05:16:10 -0300741
742fail:
743 tda829x_release(fe);
Michael Krufkyab166052007-12-09 02:26:48 -0300744 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745}
Michael Krufky8c125f2c2007-10-27 02:00:57 -0300746EXPORT_SYMBOL_GPL(tda829x_attach);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
Michael Krufkyab166052007-12-09 02:26:48 -0300748int tda829x_probe(struct i2c_adapter *i2c_adap, u8 i2c_addr)
Hartmut Hackmann95736032005-11-08 21:38:00 -0800749{
Michael Krufky910bb3e2007-08-27 21:22:20 -0300750 struct tuner_i2c_props i2c_props = {
Michael Krufkyab166052007-12-09 02:26:48 -0300751 .adap = i2c_adap,
752 .addr = i2c_addr,
Michael Krufky910bb3e2007-08-27 21:22:20 -0300753 };
Michael Krufkydb8a6952007-08-21 01:24:42 -0300754
Hartmut Hackmann44fd06f2006-02-27 00:09:11 -0300755 unsigned char soft_reset[] = { 0x00, 0x00 };
756 unsigned char easy_mode_b[] = { 0x01, 0x02 };
757 unsigned char easy_mode_g[] = { 0x01, 0x04 };
758 unsigned char restore_9886[] = { 0x00, 0xd6, 0x30 };
Hartmut Hackmann95736032005-11-08 21:38:00 -0800759 unsigned char addr_dto_lsb = 0x07;
760 unsigned char data;
Michael Krufkya818e1c2007-11-04 11:03:22 -0300761#define PROBE_BUFFER_SIZE 8
762 unsigned char buf[PROBE_BUFFER_SIZE];
763 int i;
764
765 /* rule out tda9887, which would return the same byte repeatedly */
766 tuner_i2c_xfer_send(&i2c_props, soft_reset, 1);
767 tuner_i2c_xfer_recv(&i2c_props, buf, PROBE_BUFFER_SIZE);
768 for (i = 1; i < PROBE_BUFFER_SIZE; i++) {
Michael Krufkybbe1e0b2007-11-05 09:54:42 -0300769 if (buf[i] != buf[0])
770 break;
Michael Krufkya818e1c2007-11-04 11:03:22 -0300771 }
772
773 /* all bytes are equal, not a tda829x - probably a tda9887 */
774 if (i == PROBE_BUFFER_SIZE)
775 return -ENODEV;
Hartmut Hackmann95736032005-11-08 21:38:00 -0800776
Michael Krufkyf1f32842007-11-03 22:14:54 -0300777 if ((tda8290_probe(&i2c_props) == 0) ||
778 (tda8295_probe(&i2c_props) == 0))
779 return 0;
780
781 /* fall back to old probing method */
Michael Krufky910bb3e2007-08-27 21:22:20 -0300782 tuner_i2c_xfer_send(&i2c_props, easy_mode_b, 2);
783 tuner_i2c_xfer_send(&i2c_props, soft_reset, 2);
784 tuner_i2c_xfer_send(&i2c_props, &addr_dto_lsb, 1);
785 tuner_i2c_xfer_recv(&i2c_props, &data, 1);
Hartmut Hackmann95736032005-11-08 21:38:00 -0800786 if (data == 0) {
Michael Krufky910bb3e2007-08-27 21:22:20 -0300787 tuner_i2c_xfer_send(&i2c_props, easy_mode_g, 2);
788 tuner_i2c_xfer_send(&i2c_props, soft_reset, 2);
789 tuner_i2c_xfer_send(&i2c_props, &addr_dto_lsb, 1);
790 tuner_i2c_xfer_recv(&i2c_props, &data, 1);
Hartmut Hackmann95736032005-11-08 21:38:00 -0800791 if (data == 0x7b) {
792 return 0;
793 }
794 }
Michael Krufky910bb3e2007-08-27 21:22:20 -0300795 tuner_i2c_xfer_send(&i2c_props, restore_9886, 3);
Michael Krufky1f3a4e322007-11-04 10:51:28 -0300796 return -ENODEV;
Hartmut Hackmann95736032005-11-08 21:38:00 -0800797}
Michael Krufkyf1f32842007-11-03 22:14:54 -0300798EXPORT_SYMBOL_GPL(tda829x_probe);
Michael Krufky910bb3e2007-08-27 21:22:20 -0300799
Michael Krufky8c125f2c2007-10-27 02:00:57 -0300800MODULE_DESCRIPTION("Philips/NXP TDA8290/TDA8295 analog IF demodulator driver");
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300801MODULE_AUTHOR("Gerd Knorr, Hartmut Hackmann, Michael Krufky");
Michael Krufky910bb3e2007-08-27 21:22:20 -0300802MODULE_LICENSE("GPL");
803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804/*
805 * Overrides for Emacs so that we follow Linus's tabbing style.
806 * ---------------------------------------------------------------------------
807 * Local variables:
808 * c-basic-offset: 8
809 * End:
810 */